//target foto fix ---- need peer shader
// ---- Created with 3Dmigoto v1.3.11 on Tue Nov 27 18:47:56 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 PerInstanceOffsets : register(b3)
{
  uint4 gPerInstanceOffsets[1500] : packoffset(c0);
}

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,
  float4 v2 : TANGENT0,
  float3 v3 : NORMAL0,
  uint v4 : SV_InstanceID0,
  out float4 o0 : SV_Position0,
  out float4 o1 : TEXCOORD0,
  out float4 o2 : TEXCOORD1,
  out float4 o3 : TEXCOORD2,
  out uint o4 : INSTANCEOFFS0,
  out float4 o5 : TEXCOORD3)
{
// Needs manual fix for instruction:
// unknown dcl_: dcl_input_sgv v4.x, instance_id
  float4 r0,r1,r2,r3,r4;
  uint4 bitmask, uiDest;
  float4 fDest;

  if (gGlobalMatrixSwapFlag.x != 0) {
    r0.x = v4.x;
    r0.y = 3 * (int)gPerInstanceOffsets[r0.x].y;
    r1.xyzw = t112.Load(r0.y).xyzw;
    r0.xy = mad((int2)gPerInstanceOffsets[r0.x].yy, int2(3,3), int2(1,2));
    r2.xyzw = t112.Load(r0.x).xyzw;
    r0.xyzw = t112.Load(r0.y).xyzw;
  } else {
    r3.x = v4.x;
    r3.y = 3 * (int)gPerInstanceOffsets[r3.x].y;
    r1.xyzw = t113.Load(r3.y).xyzw;
    r3.xy = mad((int2)gPerInstanceOffsets[r3.x].yy, int2(3,3), int2(1,2));
    r2.xyzw = t113.Load(r3.x).xyzw;
    r0.xyzw = t113.Load(r3.y).xyzw;
  }
  r3.xyz = v0.xyz;
  r3.w = 1;
  r4.x = dot(r1.xyzw, r3.xyzw);
  r4.y = dot(r2.xyzw, r3.xyzw);
  r4.z = dot(r0.xyzw, r3.xyzw);
  r3.x = dot(r1.xyz, v3.xyz);
  r3.y = dot(r2.xyz, v3.xyz);
  r3.z = dot(r0.xyz, v3.xyz);
  r0.w = dot(r3.xyz, r3.xyz);
  r0.w = rsqrt(r0.w);
  o2.xyz = r3.xyz * r0.www;
  r1.x = dot(r1.xyz, v2.xyz);
  r1.y = dot(r2.xyz, v2.xyz);
  r1.z = dot(r0.xyz, v2.xyz);
  r0.x = dot(r1.xyz, r1.xyz);
  r0.x = rsqrt(r0.x);
  o3.xyz = r1.xyz * r0.xxx;
  r4.w = 1;
  r0.x = dot(gProjectionViewMatrix._m00_m01_m02_m03, r4.xyzw);
  r0.y = dot(gProjectionViewMatrix._m10_m11_m12_m13, r4.xyzw);
  o0.z = dot(gProjectionViewMatrix._m20_m21_m22_m23, r4.xyzw);
  r0.z = dot(gProjectionViewMatrix._m30_m31_m32_m33, r4.xyzw);
  o0.xy = gJittering.xy * r0.zz + r0.xy;
  o0.w = r0.z;
  o3.w = v2.w;
  o5.xy = v1.xy;
  o5.zw = float2(0,0);
  o1.xyz = r4.xyz;
  r0.x = v4.x;
  o4.x = gPerInstanceOffsets[r0.x].z;
  


/////////////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));


//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


o0.x+=adjust_from_depth_buffer(0,0,300)*o0.w;




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

  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.0.10011.16384
//
//   using 3Dmigoto v1.3.11 on Tue Nov 27 18:47:56 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
//
// }
//
// cbuffer PerInstanceOffsets
// {
//
//   uint4 gPerInstanceOffsets[1500];   // Offset:    0 Size: 24000
//
// }
//
// 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
// PerInstanceOffsets                cbuffer      NA          NA    3        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
// TANGENT                  0   xyzw        2     NONE   float   xyzw
// NORMAL                   0   xyz         3     NONE   float   xyz
// SV_InstanceID            0   x           4   INSTID    uint   x
//
//
// 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                 1   xyz         2     NONE   float   xyz
// TEXCOORD                 2   xyzw        3     NONE   float   xyzw
// INSTANCEOFFS             0   x           4     NONE    uint   x
// TEXCOORD                 3   xyzw        5     NONE   float   xyzw
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb1[9], immediateIndexed
dcl_constantbuffer cb3[1500], dynamicIndexed
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_input v2.xyzw
dcl_input v3.xyz
dcl_input_sgv v4.x, instance_id
dcl_output_siv o0.xyzw, position
dcl_output o1.xyz
dcl_output o2.xyz
dcl_output o3.xyzw
dcl_output o4.x
dcl_output o5.xyzw
dcl_temps 5
if_nz cb6[0].x
  mov r0.x, v4.x
  imul null, r0.y, l(3), cb3[r0.x + 0].y
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r1.xyzw, r0.yyyy, t112.xyzw
  imad r0.xy, cb3[r0.x + 0].yyyy, l(3, 3, 0, 0), l(1, 2, 0, 0)
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r2.xyzw, r0.xxxx, t112.xyzw
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r0.xyzw, r0.yyyy, t112.xyzw
else
  mov r3.x, v4.x
  imul null, r3.y, l(3), cb3[r3.x + 0].y
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r1.xyzw, r3.yyyy, t113.xyzw
  imad r3.xy, cb3[r3.x + 0].yyyy, l(3, 3, 0, 0), l(1, 2, 0, 0)
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r2.xyzw, r3.xxxx, t113.xyzw
  ld_indexable(buffer)(mixed,mixed,mixed,mixed) r0.xyzw, r3.yyyy, t113.xyzw
endif
mov r3.xyz, v0.xyzx
mov r3.w, l(1.000000)
dp4 r4.x, r1.xyzw, r3.xyzw
dp4 r4.y, r2.xyzw, r3.xyzw
dp4 r4.z, r0.xyzw, r3.xyzw
dp3 r3.x, r1.xyzx, v3.xyzx
dp3 r3.y, r2.xyzx, v3.xyzx
dp3 r3.z, r0.xyzx, v3.xyzx
dp3 r0.w, r3.xyzx, r3.xyzx
rsq r0.w, r0.w
mul o2.xyz, r0.wwww, r3.xyzx
dp3 r1.x, r1.xyzx, v2.xyzx
dp3 r1.y, r2.xyzx, v2.xyzx
dp3 r1.z, r0.xyzx, v2.xyzx
dp3 r0.x, r1.xyzx, r1.xyzx
rsq r0.x, r0.x
mul o3.xyz, r0.xxxx, r1.xyzx
mov r4.w, l(1.000000)
dp4 r0.x, cb1[0].xyzw, r4.xyzw
dp4 r0.y, cb1[1].xyzw, r4.xyzw
dp4 o0.z, cb1[2].xyzw, r4.xyzw
dp4 r0.z, cb1[3].xyzw, r4.xyzw
mad o0.xy, cb1[8].xyxx, r0.zzzz, r0.xyxx
mov o0.w, r0.z
mov o3.w, v2.w
mov o5.xy, v1.xyxx
mov o5.zw, l(0,0,0,0)
mov o1.xyz, r4.xyzx
mov r0.x, v4.x
mov o4.x, cb3[r0.x + 0].z
ret
// Approximately 46 instruction slots used

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