//HUD VS 9. QTE orange color.
// ---- Created with 3Dmigoto v0.6.181 on Mon Dec 25 12:50:02 2023
cbuffer cb0 : register(b0)
{
  float4 cb0[6];
}




// 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*20) / (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 : TEXCOORD0,
  float2 v2 : TEXCOORD1,
  out float4 o0 : SV_Position0,
  out float4 o1 : TEXCOORD0,
  out float4 o2 : TEXCOORD1,
  out float4 o3 : TEXCOORD2,
  out float2 o4 : TEXCOORD3)
{
  float4 r0;
  uint4 bitmask, uiDest;
  float4 fDest;
  
  float4 stereo = StereoParams.Load(0);
  float4 iniparams3 = IniParams.Load(int2(3,0));
  float4 iniparams9 = IniParams.Load(int2(9,0));
  float4 iniparams10 = IniParams.Load(int2(10,0));
  float4 iniparams11 = IniParams.Load(int2(11,0));
  
  o0.x = dot(v0.xyzw, cb0[0].xyzw);
  o0.y = dot(v0.xyzw, cb0[1].xyzw);
  o0.z = dot(v0.xyzw, cb0[2].xyzw);
  o0.w = dot(v0.xyzw, cb0[3].xyzw);
  float2 coords = o0.xy/o0.w;
  float2 originalCoords = o0.xy/o0.w;
  float2 correction = 0;
  correction.x = 0.5625*(iniparams9.x/iniparams9.y);
  correction.y = (iniparams9.y/iniparams9.x)/0.5625;
  if (correction.x>1) {
    coords.x = coords.x*correction.x;
  } else if (correction.y>1) {
    coords.y = coords.y*correction.y;
  }
  if (o0.w!=1.0 && o0.w<20.0) {
    o0.x+=stereo.x*(o0.w-stereo.y);
  } else if (iniparams11.x==1 && iniparams3.w==1 && (v1.y<0.2314453125 || (v1.x>=0.1811523 && v1.x<0.1953125 && v1.y>=0.890 && v1.y<0.897)) && coords.x>=-0.25 && coords.x<0.25 && coords.y>=-0.25 && coords.y<0.25) {
    o0.x+=adjust_from_depth_buffer(originalCoords.x,originalCoords.y,200)*o0.w;
  } else if (iniparams11.x==1 && (iniparams3.w==1 || iniparams3.w==2) && coords.y<0.7 && (coords.x>=-0.8 || coords.y<0.8) && (coords.x<0.5 || coords.y<-0.2) && (coords.x>=-0.6 || coords.y>=-0.6) && (coords.x<0.6 || coords.y>=-0.6)) {
    // && (v1.x>=0.183 && v1.x<0.195 && v1.y>=0.385 && v1.y<0.42)
    o0.x+=adjust_from_depth_buffer(originalCoords.x,originalCoords.y,200)*o0.w;
  }
  r0.xyz = log2(cb0[5].xyz);
  r0.xyz = float3(2.20000005,2.20000005,2.20000005) * r0.xyz;
  o1.xyz = exp2(r0.xyz);
  o1.w = cb0[5].w;
  r0.xyz = log2(cb0[4].xyz);
  r0.xyz = float3(2.20000005,2.20000005,2.20000005) * r0.xyz;
  o2.xyz = exp2(r0.xyz);
  o2.w = cb0[4].w;
  o3.xyzw = v2.xyxy;
  o4.xy = v1.xy;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) D3D Shader Disassembler
//
//   using 3Dmigoto v0.6.181 on Mon Dec 25 12:50:02 2023
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float   xyzw
// TEXCOORD                 0   xy          1     NONE   float   xy
// TEXCOORD                 1   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   xyzw        2     NONE   float   xyzw
// TEXCOORD                 2   xyzw        3     NONE   float   xyzw
// TEXCOORD                 3   xy          4     NONE   float   xy
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer CB0[6], immediateIndexed
dcl_input v0.xyzw
dcl_input v1.xy
dcl_input v2.xy
dcl_output_siv o0.xyzw, position
dcl_output o1.xyzw
dcl_output o2.xyzw
dcl_output o3.xyzw
dcl_output o4.xy
dcl_temps 1
dp4 o0.x, v0.xyzw, cb0[0].xyzw
dp4 o0.y, v0.xyzw, cb0[1].xyzw
dp4 o0.z, v0.xyzw, cb0[2].xyzw
dp4 o0.w, v0.xyzw, cb0[3].xyzw
log r0.xyz, cb0[5].xyzx
mul r0.xyz, r0.xyzx, l(2.200000, 2.200000, 2.200000, 0.000000)
exp o1.xyz, r0.xyzx
mov o1.w, cb0[5].w
log r0.xyz, cb0[4].xyzx
mul r0.xyz, r0.xyzx, l(2.200000, 2.200000, 2.200000, 0.000000)
exp o2.xyz, r0.xyzx
mov o2.w, cb0[4].w
mov o3.xyzw, v2.xyxy
mov o4.xy, v1.xyxx
ret
// Approximately 0 instruction slots used

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