//HUD VS 4.
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v0.6.181 on Sat Dec 30 17:18:55 2023
//
//
// Buffer Definitions:
//
// cbuffer Buffer1
// {
//
//   float _generated_dummy_variable_1; // Index:    0.x              Components:     1 [unused]
//   float4 cFogParams;                 // Index:    6.xyzw           Components:     4
//   float4 cClipPlane0;                // Index:   24.xyzw           Components:     4
//
// }
//
// cbuffer Buffer2
// {
//
//   float4 cScreenCenter;              // Index:    0.xyzw           Components:     4
//   float4 cClipping;                  // Index:    1.xyzw           Components:     4
//
// }
//
// cbuffer Buffer4
// {
//
//   float _generated_dummy_variable_4; // Index:    0.x              Components:     1 [unused]
//   float4 cMaterialVariable0;         // Index:   37.xyzw           Components:     4
//   float4 cMaterialVariable1;         // Index:   38.xyzw           Components:     4
//   float4 cMaterialVariable2;         // Index:   39.xyzw           Components:     4
//   float4 cMaterialVariable3;         // Index:   40.xyzw           Components:     4
//   float4 cMaterialVariable4;         // Index:   41.xyzw           Components:     4
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// Buffer1                           cbuffer      NA          NA    1        1
// Buffer2                           cbuffer      NA          NA    2        1
// Buffer4                           cbuffer      NA          NA    4        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float   xyz
// COLOR                    0   xyzw        1     NONE   float   xyz
// TEXCOORD                 0   xy          2     NONE   float   xy
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_POSITION              0   xyzw        0      POS   float   xyzw
// TEXCOORD                 0   xyzw        1     NONE   float   xyzw
// TEXCOORD                 1   xy          2     NONE   float   xy
// TEXCOORD                 2     zw        2     NONE   float     zw
// TEXCOORD                 3   xy          3     NONE   float   xy
// TEXCOORD                 4     z         3     NONE   float     z
// TEXCOORD                 5      w        3     NONE   float      w
// TEXCOORD                 6   x           4     NONE   float   x
// TEXCOORD                 7    y          4     NONE   float    y
// TEXCOORD                 8     z         4     NONE   float     z
// SV_ClipDistance          0   x           5  CLIPDST   float   x
//
//vs_5_0
//dcl_globalFlags refactoringAllowed
//dcl_constantbuffer CB1[25], immediateIndexed
//dcl_constantbuffer CB2[2], immediateIndexed
//dcl_constantbuffer CB4[42], immediateIndexed
//dcl_input v0.xyz
//dcl_input v1.xyz
//dcl_input v2.xy
//dcl_output_siv o0.xyzw, position
//dcl_output o1.xyzw
//dcl_output o2.xy
//dcl_output o2.zw
//dcl_output o3.xy
//dcl_output o3.z
//dcl_output o3.w
//dcl_output o4.x
//dcl_output o4.y
//dcl_output o4.z
//dcl_output_siv o5.x, clip_distance
//dcl_temps 2
//add r0.xy, v0.xyxx, -cb2[0].xyxx
//mul r0.xy, r0.xyxx, cb2[0].zwzz
//add r1.x, v0.z, -cb2[1].x
//mul r0.z, r1.x, cb2[1].y
//mov r0.w, l(1.000000)
//mov o0.xyzw, r0.xyzw
//mov o1.z, v1.y
//mov o1.w, cb4[39].y
//mul r1.xy, v2.xyxx, l(0.500000, 0.500000, 0.000000, 0.000000)
//mul r1.zw, r1.xxxy, cb4[37].xxxy
//add r1.z, r1.w, r1.z
//add o1.x, r1.z, cb4[37].w
//mul r1.zw, r1.xxxy, cb4[38].xxxy
//add r1.z, r1.w, r1.z
//add o1.y, r1.z, cb4[38].w
//mul r1.zw, r1.xxxy, cb4[40].xxxy
//mul r1.xy, r1.xyxx, cb4[41].xyxx
//add r1.x, r1.y, r1.x
//add o2.w, r1.x, cb4[41].w
//add r1.x, r1.w, r1.z
//add o2.z, r1.x, cb4[40].w
//mad o2.xy, v2.xyxx, l(1.010000, 1.010000, 0.000000, 0.000000), l(-0.020000, -0.020000, 0.000000, 0.000000)
//dp4 r1.x, r0.xyzw, r0.xyzw
//dp4 o5.x, cb1[24].xyzw, r0.xyzw
//sqrt r0.x, r1.x
//mad r0.x, r0.x, cb1[6].x, cb1[6].y
//max r0.x, r0.x, cb1[6].w
//min o3.z, r0.x, l(1.000000)
//mov o3.xy, v2.xyxx
//mov o3.w, v1.x
//mov o4.xz, cb4[39].xxzx
//mov o4.y, v1.z
//ret
//// Approximately 33 instruction slots used
//
///////////////////////////////// HLSL Code /////////////////////////////////
// ---- Created with 3Dmigoto v0.6.181 on Sat Dec 30 17:18:55 2023

cbuffer Buffer1 : register(b1)
{
  float _generated_dummy_variable_1 : packoffset(c0);
  float4 cFogParams : packoffset(c6);
  float4 cClipPlane0 : packoffset(c24);
}

cbuffer Buffer2 : register(b2)
{
  float4 cScreenCenter : packoffset(c0);
  float4 cClipping : packoffset(c1);
}

cbuffer Buffer4 : register(b4)
{
  float _generated_dummy_variable_4 : packoffset(c0);
  float4 cMaterialVariable0 : packoffset(c37);
  float4 cMaterialVariable1 : packoffset(c38);
  float4 cMaterialVariable2 : packoffset(c39);
  float4 cMaterialVariable3 : packoffset(c40);
  float4 cMaterialVariable4 : packoffset(c41);
}



// 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.00001;
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/((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*0; // 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*4) / (separation - offset);

        sampled_w = world_z_from_depth_buffer(x + offset, y);
        if (sampled_w == 0)
		{
			return separation;
		}
		//else if (sampled_w<1.01 && sampled_w>0.99)
		//{
		//	return 0;
		//} 

        // 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(
  float4 v0 : POSITION0,
  float4 v1 : COLOR0,
  float2 v2 : TEXCOORD0,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : TEXCOORD0,
  out float2 o2 : TEXCOORD1,
  out float2 p2 : TEXCOORD2,
  out float2 o3 : TEXCOORD3,
  out float p3 : TEXCOORD4,
  out float q3 : TEXCOORD5,
  out float o4 : TEXCOORD6,
  out float p4 : TEXCOORD7,
  out float q4 : TEXCOORD8,
  out float o5 : SV_ClipDistance0)
{
  float4 r0,r1;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xy = -cScreenCenter.xy + v0.xy;
  r0.xy = cScreenCenter.zw * r0.xy;
  r1.x = -cClipping.x + v0.z;
  r0.z = cClipping.y * r1.x;
  r0.w = 1;
  o0.xyzw = r0.xyzw;
  
  if (o0.w==1) {
    o0.x+=adjust_from_depth_buffer(0,0,200);
  }
  
  o1.z = v1.y;
  o1.w = cMaterialVariable2.y;
  r1.xy = float2(0.5,0.5) * v2.xy;
  r1.zw = cMaterialVariable0.xy * r1.xy;
  r1.z = r1.z + r1.w;
  o1.x = cMaterialVariable0.w + r1.z;
  r1.zw = cMaterialVariable1.xy * r1.xy;
  r1.z = r1.z + r1.w;
  o1.y = cMaterialVariable1.w + r1.z;
  r1.zw = cMaterialVariable3.xy * r1.xy;
  r1.xy = cMaterialVariable4.xy * r1.xy;
  r1.x = r1.x + r1.y;
  p2.y = cMaterialVariable4.w + r1.x;
  r1.x = r1.z + r1.w;
  p2.x = cMaterialVariable3.w + r1.x;
  o2.xy = v2.xy * float2(1.00999999,1.00999999) + float2(-0.0199999996,-0.0199999996);
  r1.x = dot(r0.xyzw, r0.xyzw);
  o5.x = dot(cClipPlane0.xyzw, r0.xyzw);
  r0.x = sqrt(r1.x);
  r0.x = r0.x * cFogParams.x + cFogParams.y;
  r0.x = max(cFogParams.w, r0.x);
  p3.x = min(1, r0.x);
  o3.xy = v2.xy;
  q3.x = v1.x;
  o4.x = cMaterialVariable2.x;
  q4.x = cMaterialVariable2.z;
  p4.x = v1.z;
  return;
}
//////////////////////////////// HLSL Errors ////////////////////////////////
// E:\Epic\GhostbustersRemastered\ShaderFixes\d30e611d68770d6a-vs_replace.txt(44,13-14): error X3003: redefinition of 'p3'
/////////////////////////////////////////////////////////////////////////////
