//HUD VS 2.
// ---- Created with 3Dmigoto v0.6.181 on Sat Dec 30 16:29:21 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);
  row_major float2x4 cTextureTransform : packoffset(c2);
}

cbuffer Buffer4 : register(b4)
{
  float _generated_dummy_variable_4 : packoffset(c0);
  float4 cMaterialVariable2 : packoffset(c39);
  float4 cMaterialVariable3 : packoffset(c40);
  float4 cMaterialVariable4 : packoffset(c41);
  float4 cMaterialVariable5 : packoffset(c42);
}



// 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,
  float2 v1 : TEXCOORD0,
  out float4 o0 : SV_POSITION0,
  out float3 o1 : TEXCOORD0,
  out float p1 : TEXCOORD3,
  out float2 o2 : TEXCOORD1,
  out float2 p2 : TEXCOORD2,
  out float o3 : TEXCOORD4,
  out float o4 : 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);
  }
  
  r1.x = dot(r0.xyzw, r0.xyzw);
  o4.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 = cMaterialVariable5.xyz;
  r0.x = dot(v1.xy, cMaterialVariable3.xy);
  o2.x = cMaterialVariable3.w + r0.x;
  r0.x = dot(v1.xy, cMaterialVariable4.xy);
  o2.y = cMaterialVariable4.w + r0.x;
  r0.x = dot(v1.xy, cTextureTransform._m00_m01);
  p2.x = cTextureTransform._m03 + r0.x;
  r0.x = dot(v1.xy, cTextureTransform._m10_m11);
  p2.y = cTextureTransform._m13 + r0.x;
  o3.x = cMaterialVariable2.x;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v0.6.181 on Sat Dec 30 16:29:21 2023
//
//
// Buffer Definitions:
//
// cbuffer Buffer1
// {
//
//   float _generated_dummy_variable_1; // Offset:    0 Size:     4 [unused]
//   float4 cFogParams;                 // Offset:   96 Size:    16
//   float4 cClipPlane0;                // Offset:  384 Size:    16
//
// }
//
// cbuffer Buffer2
// {
//
//   float4 cScreenCenter;              // Offset:    0 Size:    16
//   float4 cClipping;                  // Offset:   16 Size:    16
//   row_major float2x4 cTextureTransform;// Offset:   32 Size:    32
//
// }
//
// cbuffer Buffer4
// {
//
//   float _generated_dummy_variable_4; // Offset:    0 Size:     4 [unused]
//   float4 cMaterialVariable2;         // Offset:  624 Size:    16
//   float4 cMaterialVariable3;         // Offset:  640 Size:    16
//   float4 cMaterialVariable4;         // Offset:  656 Size:    16
//   float4 cMaterialVariable5;         // Offset:  672 Size:    16
//
// }
//
//
// 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
// TEXCOORD                 0   xy          1     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                 3      w        1     NONE   float      w
// TEXCOORD                 1   xy          2     NONE   float   xy
// TEXCOORD                 2     zw        2     NONE   float     zw
// TEXCOORD                 4   x           3     NONE   float   x
// SV_ClipDistance          0   x           4  CLIPDST   float   x
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer CB1[25], immediateIndexed
dcl_constantbuffer CB2[4], immediateIndexed
dcl_constantbuffer CB4[43], immediateIndexed
dcl_input v0.xyz
dcl_input v1.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.x
dcl_output_siv o4.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 o4.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)
mov o1.xyz, cb4[42].xyzx
dp2 r0.x, v1.xyxx, cb4[40].xyxx
add o2.x, r0.x, cb4[40].w
dp2 r0.x, v1.xyxx, cb4[41].xyxx
add o2.y, r0.x, cb4[41].w
dp2 r0.x, v1.xyxx, cb2[2].xyxx
add o2.z, r0.x, cb2[2].w
dp2 r0.x, v1.xyxx, cb2[3].xyxx
add o2.w, r0.x, cb2[3].w
mov o3.x, cb4[39].x
ret
// Approximately 23 instruction slots used

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