//aiming related 
Buffer<float4> t113 : register(t113);

Buffer<float4> t112 : register(t112);


cbuffer _Globals : register(b0)
{
  float4 gClipPlane : packoffset(c0);
  float4 gZPassNearZClamp : packoffset(c1);
  float4 gCameraPositionVS : packoffset(c2);
  float4 gEyeSpaceDepth : packoffset(c3);
  float4 gWorldCameraPosition : packoffset(c4);
  row_major float4x4 gClusteredLightingParamsVS : packoffset(c5);
  float4 gScreenParametersVS : packoffset(c9);
  row_major float4x4 gDecalProjectionMatrix : packoffset(c10);
  row_major float4x4 gPrevProjectionViewMatrix : packoffset(c14);
  row_major float4x4 gCurrWorldMatrix : packoffset(c18);
  row_major float4x4 gCurrProjectionViewMatrix : packoffset(c22);
  float4 gUVOffset01 : packoffset(c26);
  float4 gUVOffset23 : packoffset(c27);
  float4 gUVOffset45 : packoffset(c28);
  float4 gDiffuseColour : packoffset(c29);
  row_major float4x4 gMirrorTexCoordMatrix : packoffset(c30);
  row_major float4x4 gBillboardRotateMatrix : packoffset(c34);
  float4 gBillboardRotatePivot : packoffset(c38);
  float4 gFarPlaneAdjustParams : packoffset(c39);
  float4 gUV0Scale : packoffset(c40);
  float4 gUV0Offset : packoffset(c41);
  float4 gAnimatedFoliageParameters[5] : packoffset(c42);
  float4 gVertexAnimationMultipliers : packoffset(c47);
  float4 gVertexAnimationPeriodMultipliers : packoffset(c48);
  float4 gScreenDecalMiscVS_1 : packoffset(c49);
  float4 gScreenDecalMiscVS_2 : packoffset(c50);
  float4 gGlassDamageWorldPosVS_0 : packoffset(c51);
  float4 gGlassDamageWorldPosVS_1 : packoffset(c52);
  float4 gGlassDamageWorldPosVS_2 : packoffset(c53);
  float4 gGlassAllCrackedWorldPosVS : packoffset(c54);
  float4 gGlassDamageWorldTanVS_0 : packoffset(c55);
  float4 gGlassDamageWorldTanVS_1 : packoffset(c56);
  float4 gGlassDamageWorldTanVS_2 : packoffset(c57);
  float4 gGlassAllCrackedWorldTanVS : packoffset(c58);
  float4 gLPVGridInfoForVS[2] : packoffset(c59);
  float4 gVehicleLightParameters[3] : packoffset(c61);
  float4 gPerModelParamsVS : packoffset(c64);
  float4 gDebugSolidAngleParams : packoffset(c65);
}

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);
  r0.xyz = cmp(float3(0.0404499993,0.0404499993,0.0404499993) >= gDiffuseColour.xyz);
  r1.xy = gDiffuseColour.xy * float2(0.0773993805,0.0773993805);
  r1.zw = saturate(gDiffuseColour.xy * float2(0.947867274,0.947867274) + float2(0.0521326996,0.0521326996));
  r1.zw = log2(r1.zw);
  r1.zw = float2(2.4000001,2.4000001) * r1.zw;
  r1.zw = exp2(r1.zw);
  o1.xy = r0.xy ? r1.xy : r1.zw;
  r0.x = gDiffuseColour.z * 0.0773993805;
  r0.y = saturate(gDiffuseColour.z * 0.947867274 + 0.0521326996);
  r0.y = log2(r0.y);
  r0.y = 2.4000001 * r0.y;
  r0.y = exp2(r0.y);
  o1.z = r0.z ? r0.x : r0.y;
  o2.xy = v1.xy * gUV0Scale.xy + gUV0Offset.xy;
  o1.w = gDiffuseColour.w;

/////////////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.7 && 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 if(o0.x > -0.4 && o0.x < 0.4 && o0.y > -0.4 && o0.y < 0.7)
{
o0.x+=stereo.x*params.z*1.9*params2.z;
} 

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


//aiming gameplay HUD
else
{
///////////////////////////////////////////////////RIGHT LOWER MENUE
if(o0.x > 0.7 && 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:58:33 2018
//
//
// Buffer Definitions:
//
// cbuffer $Globals
// {
//
//   float4 gClipPlane;                 // Offset:    0 Size:    16 [unused]
//   float4 gZPassNearZClamp;           // Offset:   16 Size:    16 [unused]
//   float4 gCameraPositionVS;          // Offset:   32 Size:    16 [unused]
//   float4 gEyeSpaceDepth;             // Offset:   48 Size:    16 [unused]
//   float4 gWorldCameraPosition;       // Offset:   64 Size:    16 [unused]
//   row_major float4x4 gClusteredLightingParamsVS;// Offset:   80 Size:    64 [unused]
//   float4 gScreenParametersVS;        // Offset:  144 Size:    16 [unused]
//   row_major float4x4 gDecalProjectionMatrix;// Offset:  160 Size:    64 [unused]
//   row_major float4x4 gPrevProjectionViewMatrix;// Offset:  224 Size:    64 [unused]
//   row_major float4x4 gCurrWorldMatrix;// Offset:  288 Size:    64 [unused]
//   row_major float4x4 gCurrProjectionViewMatrix;// Offset:  352 Size:    64 [unused]
//   float4 gUVOffset01;                // Offset:  416 Size:    16 [unused]
//   float4 gUVOffset23;                // Offset:  432 Size:    16 [unused]
//   float4 gUVOffset45;                // Offset:  448 Size:    16 [unused]
//   float4 gDiffuseColour;             // Offset:  464 Size:    16
//   row_major float4x4 gMirrorTexCoordMatrix;// Offset:  480 Size:    64 [unused]
//   row_major float4x4 gBillboardRotateMatrix;// Offset:  544 Size:    64 [unused]
//   float4 gBillboardRotatePivot;      // Offset:  608 Size:    16 [unused]
//   float4 gFarPlaneAdjustParams;      // Offset:  624 Size:    16 [unused]
//   float4 gUV0Scale;                  // Offset:  640 Size:    16
//   float4 gUV0Offset;                 // Offset:  656 Size:    16
//   float4 gAnimatedFoliageParameters[5];// Offset:  672 Size:    80 [unused]
//   float4 gVertexAnimationMultipliers;// Offset:  752 Size:    16 [unused]
//   float4 gVertexAnimationPeriodMultipliers;// Offset:  768 Size:    16 [unused]
//   float4 gScreenDecalMiscVS_1;       // Offset:  784 Size:    16 [unused]
//   float4 gScreenDecalMiscVS_2;       // Offset:  800 Size:    16 [unused]
//   float4 gGlassDamageWorldPosVS_0;   // Offset:  816 Size:    16 [unused]
//   float4 gGlassDamageWorldPosVS_1;   // Offset:  832 Size:    16 [unused]
//   float4 gGlassDamageWorldPosVS_2;   // Offset:  848 Size:    16 [unused]
//   float4 gGlassAllCrackedWorldPosVS; // Offset:  864 Size:    16 [unused]
//   float4 gGlassDamageWorldTanVS_0;   // Offset:  880 Size:    16 [unused]
//   float4 gGlassDamageWorldTanVS_1;   // Offset:  896 Size:    16 [unused]
//   float4 gGlassDamageWorldTanVS_2;   // Offset:  912 Size:    16 [unused]
//   float4 gGlassAllCrackedWorldTanVS; // Offset:  928 Size:    16 [unused]
//   float4 gLPVGridInfoForVS[2];       // Offset:  944 Size:    32 [unused]
//   float4 gVehicleLightParameters[3]; // Offset:  976 Size:    48 [unused]
//   float4 gPerModelParamsVS;          // Offset: 1024 Size:    16 [unused]
//   float4 gDebugSolidAngleParams;     // Offset: 1040 Size:    16 [unused]
//
// }
//
// 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
// $Globals                          cbuffer      NA          NA    0        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 cb0[42], immediateIndexed
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
ge r0.xyz, l(0.040450, 0.040450, 0.040450, 0.000000), cb0[29].xyzx
mul r1.xy, l(0.0773993805, 0.0773993805, 0.000000, 0.000000), cb0[29].xyxx
mad_sat r1.zw, cb0[29].xxxy, l(0.000000, 0.000000, 0.947867274, 0.947867274), l(0.000000, 0.000000, 0.0521326996, 0.0521326996)
log r1.zw, r1.zzzw
mul r1.zw, r1.zzzw, l(0.000000, 0.000000, 2.400000, 2.400000)
exp r1.zw, r1.zzzw
movc o1.xy, r0.xyxx, r1.xyxx, r1.zwzz
mul r0.x, l(0.0773993805), cb0[29].z
mad_sat r0.y, cb0[29].z, l(0.947867274), l(0.0521326996)
log r0.y, r0.y
mul r0.y, r0.y, l(2.400000)
exp r0.y, r0.y
movc o1.z, r0.z, r0.x, r0.y
mad o2.xy, v1.xyxx, cb0[40].xyxx, cb0[41].xyxx
mov o1.w, cb0[29].w
ret
// Approximately 35 instruction slots used

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