//target and more HUD
// ---- Created with 3Dmigoto v1.3.11 on Tue Nov 20 18:51:52 2018
Buffer<float4> t113 : register(t113);

Buffer<float4> t112 : register(t112);


cbuffer ProjectionViewMatrix : register(b1)
{
  row_major float4x4 gProjectionViewMatrix : packoffset(c0);
  row_major float4x4 gPreviousProjViewMatrix : packoffset(c4);
  float4 gJittering : packoffset(c8);
}

cbuffer GlobalMatrixSwapFlag : register(b6)
{
  uint4 gGlobalMatrixSwapFlag : packoffset(c0);
}



/////////////HUD FIX START declarations ///////////////


cbuffer DriverStereoParams : register(b12)
{
    float driver_neg_sep_conv;
    float driver_sep;
}




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



static const float near = 0.01;
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);
  if (stereo.x==0) {return 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*5) / (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;
}

/////////////HUD FIX END declarations ///////////////


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

  if (gGlobalMatrixSwapFlag.x != 0) {
    r0.xyzw = t112.Load(float4(0,0,0,0)).xyzw;
    r1.xyzw = t112.Load(float4(1,1,1,1)).xyzw;
    r2.xyzw = t112.Load(float4(2,2,2,2)).xyzw;
  } else {
    r0.xyzw = t113.Load(float4(0,0,0,0)).xyzw;
    r1.xyzw = t113.Load(float4(1,1,1,1)).xyzw;
    r2.xyzw = t113.Load(float4(2,2,2,2)).xyzw;
  }
  r3.xyz = v0.xyz;
  r3.w = 1;
  r0.x = dot(r0.xyzw, r3.xyzw);
  r0.y = dot(r1.xyzw, r3.xyzw);
  r0.z = dot(r2.xyzw, r3.xyzw);
  r0.w = 1;
  o0.x = dot(gProjectionViewMatrix._m00_m01_m02_m03, r0.xyzw);
  o0.y = dot(gProjectionViewMatrix._m10_m11_m12_m13, r0.xyzw);
  o0.z = dot(gProjectionViewMatrix._m20_m21_m22_m23, r0.xyzw);
  o0.w = dot(gProjectionViewMatrix._m30_m31_m32_m33, r0.xyzw);
  o1.xyzw = float4(1,1,1,1);
  o2.xy = v1.xy;


/////////////HUD FIX START ///////////////

//init stere params basics
  float4 stereo = StereoParams.Load(0);
  float4 params = IniParams.Load(0);
  
//sacling HUD target marks
  float4 params2 = IniParams.Load(int2(2,0));
//filter for crosshair
  float4 tex_filter = IniParams.Load(int2(2,0));
//constant for checking aming
  float4 params3 = IniParams.Load(int2(3,0));
//sacling HUD except target marks
  float4 params4 = IniParams.Load(int2(4,0));
//ScaleCrosshairDepth while aiming
float4 crosscaleaming = IniParams.Load(int2(3,0));

//unbind all HUD elements from convergence, developed by DarkStarSword
o0.x += driver_sep * o0.w*0 + driver_neg_sep_conv*0.00000000001;



//normal gameplay HUD
if(params3.z==0.0)
{
///////////////////////////////////////////////////RIGHT LOWER MENUE
if(o0.x > 0.6 && o0.y < -0.3)
{
o0.x+=stereo.x*params.z*1.1*params4.z;
} 

///////////////////////////////////////////////////LEFT LOWER MEUNE 
else if(o0.y < -0.2 && o0.x < 0.4)
{
o0.x+=stereo.x*params.z*1.1*params4.z;
}

else
{
o0.x+=stereo.x*params.z*1.9*params2.z;
}
}


//aiming gameplay HUD
else
{
///////////////////////////////////////////////////RIGHT LOWER MENUE
if(o0.x > 0.6 && o0.y < -0.3)
{
o0.x+=stereo.x*params.z*1.1/2*params4.z;
} 

///////////////////////////////////////////////////LEFT LOWER MEUNE 
else if(o0.y < -0.1 && o0.x < 0.4)
{
o0.x+=stereo.x*params.z*1.1/2*params4.z;
}

else
{
o0.x+=stereo.x*params.z*1.9*crosscaleaming.w*params2.z;
}
}

//////////////////end of HUD FIX///////////////

return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.0.10011.16384
//
//   using 3Dmigoto v1.3.11 on Tue Nov 20 18:51:52 2018
//
//
// Buffer Definitions:
//
// cbuffer ProjectionViewMatrix
// {
//
//   row_major float4x4 gProjectionViewMatrix;// Offset:    0 Size:    64
//   row_major float4x4 gPreviousProjViewMatrix;// Offset:   64 Size:    64 [unused]
//   float4 gJittering;                 // Offset:  128 Size:    16 [unused]
//
// }
//
// cbuffer GlobalMatrixSwapFlag
// {
//
//   uint4 gGlobalMatrixSwapFlag;       // Offset:    0 Size:    16
//
// }
//
// tbuffer PreviousWorldMatrix
// {
//
//   row_major float3x4 gPreviousWorldMatrix[4];// Offset:    0 Size:   192
//
// }
//
// tbuffer WorldMatrix
// {
//
//   row_major float3x4 gWorldMatrix[4];// Offset:    0 Size:   192
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// PreviousWorldMatrix               tbuffer      NA          NA  112        1
// WorldMatrix                       tbuffer      NA          NA  113        1
// ProjectionViewMatrix              cbuffer      NA          NA    1        1
// GlobalMatrixSwapFlag              cbuffer      NA          NA    6        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyz         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
// COLOR                    0   xyzw        1     NONE   float   xyzw
// TEXCOORD                 0   xy          2     NONE   float   xy
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb1[4], immediateIndexed
dcl_constantbuffer cb6[1], immediateIndexed
dcl_resource_buffer (mixed,mixed,mixed,mixed) t112
dcl_resource_buffer (mixed,mixed,mixed,mixed) t113
dcl_input v0.xyz
dcl_input v1.xy
dcl_output_siv o0.xyzw, position
dcl_output o1.xyzw
dcl_output o2.xy
dcl_temps 4
if_nz cb6[0].x
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r0.xyzw, l(0, 0, 0, 0), t112.xyzw
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r1.xyzw, l(1, 1, 1, 1), t112.xyzw
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r2.xyzw, l(2, 2, 2, 2), t112.xyzw
else
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r0.xyzw, l(0, 0, 0, 0), t113.xyzw
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r1.xyzw, l(1, 1, 1, 1), t113.xyzw
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r2.xyzw, l(2, 2, 2, 2), t113.xyzw
endif
mov r3.xyz, v0.xyzx
mov r3.w, l(1.000000)
dp4 r0.x, r0.xyzw, r3.xyzw
dp4 r0.y, r1.xyzw, r3.xyzw
dp4 r0.z, r2.xyzw, r3.xyzw
mov r0.w, l(1.000000)
dp4 o0.x, cb1[0].xyzw, r0.xyzw
dp4 o0.y, cb1[1].xyzw, r0.xyzw
dp4 o0.z, cb1[2].xyzw, r0.xyzw
dp4 o0.w, cb1[3].xyzw, r0.xyzw
mov o1.xyzw, l(1.000000,1.000000,1.000000,1.000000)
mov o2.xy, v1.xyxx
ret
// Approximately 22 instruction slots used

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