//HUD VS 1.
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v0.6.181 on Sat Dec 30 15:28:31 2023
//
//
// Buffer Definitions:
//
// cbuffer Buffer1
// {
//
//   float _generated_dummy_variable_1; // Index:    0.x              Components:     1 [unused]
//   float4 cFogParams;                 // Index:    6.xyzw           Components:     4
//   float cSelfIllumScale;             // Index:   22.x              Components:     1
//   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
//
// }
//
//
// 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   xyzw
// 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   xyz         1     NONE   float   xyz
// TEXCOORD                 5      w        1     NONE   float      w
// TEXCOORD                 1   xy          2     NONE   float   xy
// TEXCOORD                 2     zw        2     NONE   float     zw
// TEXCOORD                 3   xy          3     NONE   float   xy
// TEXCOORD                 4     zw        3     NONE   float     zw
// TEXCOORD                 6   x           4     NONE   float   x
// 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[38], immediateIndexed
//dcl_input v0.xyz
//dcl_input v1.xyzw
//dcl_input v2.xy
//dcl_output_siv o0.xyzw, position
//dcl_output o1.xyz
//dcl_output o1.w
//dcl_output o2.xy
//dcl_output o2.zw
//dcl_output o3.xy
//dcl_output o3.zw
//dcl_output o4.x
//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
//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 o1.w, r0.x, l(1.000000)
//mul o1.xyz, v1.xyzx, cb1[22].xxxx
//add o2.xz, -v2.yyxy, l(1.000000, 0.000000, 1.000000, 0.000000)
//add r0.xy, -v2.yxyy, l(1.000000, 1.000000, 0.000000, 0.000000)
//mov_sat r0.z, cb4[37].x
//mad r0.z, r0.z, l(0.458000), l(-0.020000)
//add r0.xy, r0.xyxx, r0.zzzz
//add r0.zw, r0.zzzz, v2.yyyx
//mov o2.yw, r0.wwwx
//mov o3.yw, r0.yyyz
//mov o3.xz, v2.yyxy
//mov o4.x, v1.w
//ret
//// Approximately 24 instruction slots used
//
///////////////////////////////// HLSL Code /////////////////////////////////
// ---- Created with 3Dmigoto v0.6.181 on Sat Dec 30 15:28:31 2023

cbuffer Buffer1 : register(b1)
{
  float _generated_dummy_variable_1 : packoffset(c0);
  float4 cFogParams : packoffset(c6);
  float cSelfIllumScale : packoffset(c22);
  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);
}



// 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 float3 o1 : TEXCOORD0,
  out float p1 : TEXCOORD5,
  out float2 o2 : TEXCOORD1,
  out float2 p2 : TEXCOORD2,
  out float2 o3 : TEXCOORD3,
  out float2 p3 : TEXCOORD4,
  out float o4 : TEXCOORD6,
  out float o5 : SV_ClipDistance0)
{
  float4 r0,r1;
  uint4 bitmask, uiDest;
  float4 fDest;
  
  float4 stereo = StereoParams.Load(0);
  
  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);
  }
  
  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);
  p1.x = min(1, r0.x);
  o1.xyz = cSelfIllumScale * v1.xyz;
  o2.x = float2(1,1) + -v2.y;
  p2.x = float2(1,1) + -v2.x;
  r0.xy = float2(1,1) + -v2.yx;
  r0.z = saturate(cMaterialVariable0.x);
  r0.z = r0.z * 0.458000004 + -0.0199999996;
  r0.xy = r0.zz + r0.xy;
  r0.zw = v2.yx + r0.zz;
  o2.y = r0.w;
  p2.y = r0.x;
  o3.y = r0.y;
  p3.y = r0.z;
  o3.x = v2.y;
  p3.x = v2.x;
  o4.x = v1.w;
  return;
}
//////////////////////////////// HLSL Errors ////////////////////////////////
// E:\Epic\GhostbustersRemastered\ShaderFixes\26280a9edf7e5dfb-vs_replace.txt(62,3-7): error X3018: invalid subscript 'xz'
/////////////////////////////////////////////////////////////////////////////
