//HUD camp menu.
// ---- Created with 3Dmigoto v1.3.11 on Wed Jul 25 22:34:15 2018

cbuffer CMatrixParam : register(b0)
{
  row_major float4x4 wmat : packoffset(c0);
  row_major float4x4 vmat : packoffset(c4);
  row_major float4x4 pmat : packoffset(c8);
}

cbuffer CColorParam : register(b1)
{
  float4 uColor : packoffset(c0);
  float4 uSpe : packoffset(c1);
}

cbuffer CUVShiftParam : register(b2)
{
  float2 uUVShift : packoffset(c0);
}

cbuffer CFogParam : register(b3)
{
  float4 fogparam : packoffset(c0);
  float3 fogcolor : packoffset(c1);
}



// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
Texture2D<float4> StereoParams : register(t125);
// Depth buffer copied to this input with 3Dmigoto:
// Texture2D<float> DepthBuffer : register(t110);
Texture2D<float> DepthBuffer : register(t110);



static const float near = 0.0001;
static const float far = 1;

float world_z_from_depth_buffer(float x, float y)
{
    uint width, height;
    float z;

    DepthBuffer.GetDimensions(width, height);

    x = min(max((x / 2 + 0.5) * width, 0), width - 1);
    y = min(max((-y / 2 + 0.5) * height, 0), height - 1);
    z = DepthBuffer.Load(int3(x, y, 0)).x;
    if (z == 1)
        return 0;

  return (far*near/((1-z)*near) + (far*z));
}

float adjust_from_depth_buffer(float x, float y, float numsamples)
{
    float4 stereo = StereoParams.Load(0);
    float separation = stereo.x; float convergence = stereo.y;
    float old_offset, offset, w, sampled_w, distance;
    uint i;

    // Stereo cursor: To improve the accuracy of the stereo cursor, we
    // sample a number of points on the depth buffer, starting at the near
    // clipping plane and working towards original x + separation.
    //
    // You can think of this as a line in three dimensional space that
    // starts at each eye and stretches out towards infinity. We sample 255
    // points along this line (evenly spaced in the X axis) and compare
    // with the depth buffer to find where the line is first intersected.
    //
    // Note: The reason for sampling 255 points came from a restriction in
    // DX9/SM3 where loops had to run a constant number of iterations and
    // there was no way to set that number from within the shader itself.
    // I'm not sure if the same restriction applies in DX11 with SM4/5 - if
    // it doesn't, we could change this to check each pixel instead for
    // better accuracy.
    //
    // Based on DarkStarSword's stereo crosshair code originally developed
    // for Miasmata, adapted to Unity, then translated to HLSL.

    offset = (near - convergence) * separation; // Z = X offset from center
    distance = separation - offset;         // Total distance to cover (separation - starting X offset)

    old_offset = offset;
    for (i = 0; i < numsamples; i++) {
        offset += distance / numsamples;

        // Calculate depth for this point on the line:
        w = (separation * convergence*3.333333) / (separation - offset);

        sampled_w = world_z_from_depth_buffer(x + offset, y);
        if (sampled_w == 0)
            return separation;

        // If the sampled depth is closer than the calculated depth,
        // we have found something that intersects the line, so exit
        // the loop and return the last point that was not intersected:
        if (w > sampled_w)
            break;

        old_offset = offset;
    }

    return old_offset;
}

void main(
  float2 v0 : TEXCOORD0,
  float4 v1 : COLOR0,
  float3 v2 : POSITION0,
  out float2 o0 : TEXCOORD0,
  out float4 o1 : COLOR0,
  out float4 o2 : COLOR1,
  out float4 o3 : TEXCOORD7,
  out float4 o4 : SV_Position0)
{
  float4 r0,r1,r2;
  uint4 bitmask, uiDest;
  float4 fDest;

  float4 stereo = StereoParams.Load(0);
  float4 iniparams = IniParams.Load(0);
  float4 iniparams1 = IniParams.Load(int2(1,0));
  float4 iniparams3 = IniParams.Load(int2(3,0));
  
  r0.xyzw = wmat._m00_m01_m02_m03 * v2.xxxx;
  r1.xyzw = wmat._m10_m11_m12_m13 * v2.yyyy;
  r0.xyzw = r1.xyzw + r0.xyzw;
  r1.xyzw = wmat._m20_m21_m22_m23 * v2.zzzz;
  r0.xyzw = r1.xyzw + r0.xyzw;
  r1.xyzw = float4(1,1,1,1) * wmat._m30_m31_m32_m33;
  r0.xyzw = r1.xyzw + r0.xyzw;
  r1.xyzw = vmat._m00_m01_m02_m03 * r0.xxxx;
  r2.xyzw = vmat._m10_m11_m12_m13 * r0.yyyy;
  r1.xyzw = r2.xyzw + r1.xyzw;
  r2.xyzw = vmat._m20_m21_m22_m23 * r0.zzzz;
  r1.xyzw = r2.xyzw + r1.xyzw;
  r0.xyzw = vmat._m30_m31_m32_m33 * r0.wwww;
  r0.xyzw = r1.zxyw + r0.zxyw;
  r1.xyzw = pmat._m00_m01_m02_m03 * r0.yyyy;
  r2.xyzw = pmat._m10_m11_m12_m13 * r0.zzzz;
  r1.xyzw = r2.xyzw + r1.xyzw;
  r2.xyzw = pmat._m20_m21_m22_m23 * r0.xxxx;
  r1.xyzw = r2.xyzw + r1.xyzw;
  r2.xyzw = pmat._m30_m31_m32_m33 * r0.wwww;
  o4.xyzw = r2.xyzw + r1.xyzw;
  r1.xyzw = uColor.xyzw * v1.xyzw;
  o1.xyzw = min(float4(1,1,1,1), r1.xyzw);
  r0.y = fogparam.w;
  r0.x = r0.x;
  r0.z = fogparam.x;
  r0.w = fogparam.y;
  r1.x = fogparam.z;
  r0.w = -r0.w;
  r0.x = r0.x + r0.w;
  r0.x = r0.x * r0.z;
  r0.x = max(0, r0.x);
  r0.x = min(1, r0.x);
  r0.x = log2(r0.x);
  r0.x = r1.x * r0.x;
  r0.w = exp2(r0.x);
  r0.x = 0;
  r0.x = cmp(r0.x == r0.y);
  if (r0.x != 0) {
    r0.w = 1;
  }
  r0.w = r0.w;
  r0.w = r0.w;
  r0.x = -r0.w;
  r0.x = 1 + r0.x;
  o3.xyz = fogcolor.xyz * r0.xxx;
  o2.xyzw = uSpe.xyzw;
  o0.xy = uUVShift.xy + v0.xy;
  o3.w = r0.w;
  if (o4.z==0 && iniparams3.w!=10 && iniparams3.w!=20 && iniparams3.w!=21)
  {
    if (iniparams1.z==2)
	{
	  o4.x+=adjust_from_depth_buffer(o4.x,o4.y,iniparams1.y);
	}
    else if (iniparams1.z==1)
	{
	  o4.x+=adjust_from_depth_buffer(0,0,iniparams1.y);
	}
	else
	{
	  o4.x+=stereo.x*iniparams.x+stereo.x*o4.y*0.25*iniparams1.x;
	}
    o4.x=o4.x*iniparams.y;
  }
  if (iniparams3.w==20)
  {
	o4.x+=stereo.x;
  }
  if (iniparams3.w==21)
  {
	o4.x+=stereo.x*(o4.w-stereo.y*0.125);
  }
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v1.3.11 on Wed Jul 25 22:34:15 2018
//
//
// Buffer Definitions:
//
// cbuffer CMatrixParam
// {
//
//   row_major float4x4 wmat;           // Offset:    0 Size:    64
//   row_major float4x4 vmat;           // Offset:   64 Size:    64
//   row_major float4x4 pmat;           // Offset:  128 Size:    64
//
// }
//
// cbuffer CColorParam
// {
//
//   float4 uColor;                     // Offset:    0 Size:    16
//   float4 uSpe;                       // Offset:   16 Size:    16
//
// }
//
// cbuffer CUVShiftParam
// {
//
//   float2 uUVShift;                   // Offset:    0 Size:     8
//
// }
//
// cbuffer CFogParam
// {
//
//   float4 fogparam;                   // Offset:    0 Size:    16
//   float3 fogcolor;                   // Offset:   16 Size:    12
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// CMatrixParam                      cbuffer      NA          NA    0        1
// CColorParam                       cbuffer      NA          NA    1        1
// CUVShiftParam                     cbuffer      NA          NA    2        1
// CFogParam                         cbuffer      NA          NA    3        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD                 0   xy          0     NONE   float   xy
// COLOR                    0   xyzw        1     NONE   float   xyzw
// POSITION                 0   xyz         2     NONE   float   xyz
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD                 0   xy          0     NONE   float   xy
// COLOR                    0   xyzw        1     NONE   float   xyzw
// COLOR                    1   xyzw        2     NONE   float   xyzw
// TEXCOORD                 7   xyzw        3     NONE   float   xyzw
// SV_Position              0   xyzw        4      POS   float   xyzw
//
vs_5_0
dcl_globalFlags refactoringAllowed | skipOptimization
dcl_constantbuffer cb0[12], immediateIndexed
dcl_constantbuffer cb1[2], immediateIndexed
dcl_constantbuffer cb2[1], immediateIndexed
dcl_constantbuffer cb3[2], immediateIndexed
dcl_input v0.xy
dcl_input v1.xyzw
dcl_input v2.xyz
dcl_output o0.xy
dcl_output o1.xyzw
dcl_output o2.xyzw
dcl_output o3.xyzw
dcl_output_siv o4.xyzw, position
dcl_temps 3
mul r0.xyzw, v2.xxxx, cb0[0].xyzw
mul r1.xyzw, v2.yyyy, cb0[1].xyzw
add r0.xyzw, r0.xyzw, r1.xyzw
mul r1.xyzw, v2.zzzz, cb0[2].xyzw
add r0.xyzw, r0.xyzw, r1.xyzw
mul r1.xyzw, cb0[3].xyzw, l(1.000000, 1.000000, 1.000000, 1.000000)
add r0.xyzw, r0.xyzw, r1.xyzw
mul r1.xyzw, r0.xxxx, cb0[4].xyzw
mul r2.xyzw, r0.yyyy, cb0[5].xyzw
add r1.xyzw, r1.xyzw, r2.xyzw
mul r2.xyzw, r0.zzzz, cb0[6].xyzw
add r1.xyzw, r1.xyzw, r2.xyzw
mul r0.xyzw, r0.wwww, cb0[7].xyzw
add r0.xyzw, r0.zxyw, r1.zxyw
mul r1.xyzw, r0.yyyy, cb0[8].xyzw
mul r2.xyzw, r0.zzzz, cb0[9].xyzw
add r1.xyzw, r1.xyzw, r2.xyzw
mul r2.xyzw, r0.xxxx, cb0[10].xyzw
add r1.xyzw, r1.xyzw, r2.xyzw
mul r2.xyzw, r0.wwww, cb0[11].xyzw
add o4.xyzw, r1.xyzw, r2.xyzw
mul r1.xyzw, v1.xyzw, cb1[0].xyzw
min o1.xyzw, r1.xyzw, l(1.000000, 1.000000, 1.000000, 1.000000)
mov r0.y, cb3[0].w
mov r0.x, r0.x
mov r0.z, cb3[0].x
mov r0.w, cb3[0].y
mov r1.x, cb3[0].z
mov r0.w, -r0.w
add r0.x, r0.w, r0.x
mul r0.x, r0.z, r0.x
max r0.x, r0.x, l(0.000000)
min r0.x, r0.x, l(1.000000)
log r0.x, r0.x
mul r0.x, r0.x, r1.x
exp r0.w, r0.x
itof r0.x, l(0)
eq r0.x, r0.x, r0.y
if_nz r0.x
  mov r0.w, l(1.000000)
endif
mov r0.w, r0.w
mov r0.w, r0.w
mov r0.x, -r0.w
add r0.x, r0.x, l(1.000000)
mul o3.xyz, r0.xxxx, cb3[1].xyzx
mov o2.xyzw, cb1[1].xyzw
add o0.xy, v0.xyxx, cb2[0].xyxx
mov o3.w, r0.w
ret
// Approximately 50 instruction slots used

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 