//MGS2 HUD VS 1.
// ---- Created with 3Dmigoto v0.6.181 on Wed Nov  8 23:23:29 2023

cbuffer _Globals : register(b0)
{
  row_major float4x4 gVS_Projection : packoffset(c300);
  int gPs_AlphaTestEnable : packoffset(c304);
  int gPs_AlphaTestType : packoffset(c305);
  float gPs_AlphaTestRefValue : packoffset(c306);
  float gPs_AlphaTestThreshold : packoffset(c307);
  int gPs_MultiSampleMaskEnable : packoffset(c308);
  int gPs_MultiSampleMask : packoffset(c309);
  float4 gVS_I2FScale : packoffset(c0);
  row_major float4x4 gVS_Mat : packoffset(c1);
  float4 gVS_HScale : packoffset(c5);
  float4 gVS_HOffset : packoffset(c6);
  float4 gVS_PScale : packoffset(c7);
  float4 gVS_POffset : packoffset(c8);
  float4 gVS_BaseCol : packoffset(c9);
  float4 gVS_UVMulAdd : packoffset(c10);
  float4 gVS_SnapParams : packoffset(c11);
  int gVS_IsLuminance8 : packoffset(c12);
  row_major float4x4 g_View : packoffset(c310);
  row_major float4x4 g_ViewProjection : packoffset(c314);
  row_major float4x4 g_World : packoffset(c318);
  row_major float4x4 g_WorldViewProjection : packoffset(c322);
  row_major float4x3 g_JointWorld[32] : packoffset(c326);
  float4 g_RenderTargetDimensions : packoffset(c454);
  float4 g_RenderTargetNearFar : packoffset(c455);
  float4 g_RenderTargetDepthRange : packoffset(c456);
  float4 g_AdditiveColor : packoffset(c457);
  float4 g_ModulateColor : packoffset(c458);
  float4 g_EyePosition : packoffset(c459);
  float4 g_ExternalShaderParam : packoffset(c460);
  float4 g_LightColor0 : packoffset(c461);
  float4 g_LightPosition0 : packoffset(c462);
  float4 g_LightAttenuation0 : packoffset(c463);
  float4 g_LightColor1 : packoffset(c464);
  float4 g_LightPosition1 : packoffset(c465);
  float4 g_LightAttenuation1 : packoffset(c466);
  float4 g_LightColor2 : packoffset(c467);
  float4 g_LightPosition2 : packoffset(c468);
  float4 g_LightAttenuation2 : packoffset(c469);
  float4 g_LightColor3 : packoffset(c470);
  float4 g_LightPosition3 : packoffset(c471);
  float4 g_LightAttenuation3 : packoffset(c472);
  float4 g_LightColor4 : packoffset(c473);
  float4 g_LightPosition4 : packoffset(c474);
  float4 g_LightAttenuation4 : packoffset(c475);
  float4 g_LightColor5 : packoffset(c476);
  float4 g_LightPosition5 : packoffset(c477);
  float4 g_LightAttenuation5 : packoffset(c478);
  float4 g_LightColor6 : packoffset(c479);
  float4 g_LightPosition6 : packoffset(c480);
  float4 g_LightAttenuation6 : packoffset(c481);
  float4 g_LightColor7 : packoffset(c482);
  float4 g_LightPosition7 : packoffset(c483);
  float4 g_LightAttenuation7 : packoffset(c484);
}



// 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.0001;
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));
  // return z;
}

float adjust_from_depth_buffer(float x, float y)
{
    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 * 0; // Z = X offset from center
    distance = separation - offset;         // Total distance to cover (separation - starting X offset)

    old_offset = offset;
    for (i = 0; i < 500; i++) {
        offset += distance / 500.0;

        // Calculate depth for this point on the line:
        w = (separation * convergence*0.02) / (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;
}

void main(
  float4 v0 : POSITION0,
  float4 v1 : TEXCOORD1,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : TEXCOORD1,
  out int o2 : TEXCOORD2)
{
  float4 r0;
  uint4 bitmask, uiDest;
  float4 fDest;
  
  float4 iniparams3 = IniParams.Load(int2(3,0));
  
  r0.x = dot(gVS_Mat._m00_m01_m02_m03, v0.xyzw);
  r0.y = dot(gVS_Mat._m10_m11_m12_m13, v0.xyzw);
  r0.xy = gVS_I2FScale.xy * r0.xy;
  r0.z = dot(gVS_Mat._m20_m21_m22_m23, v0.xyzw);
  r0.w = dot(gVS_Mat._m30_m31_m32_m33, v0.xyzw);
  o0.xyzw = r0.xyzw * gVS_HScale.xyzw + gVS_HOffset.xyzw;
  
  //if (o0.w==1 && iniparams3.w==2 && o0.x>=-0.5 && o0.x<0.5 && o0.y>=-0.6 && o0.y<0.7) {
  //  o0.x+=adjust_from_depth_buffer(0,0);
  //}
  
  r0.xyzw = saturate(gVS_BaseCol.xyzw * float4(1.9921875,1.9921875,1.9921875,1.9921875));
  o1.xyzw = v1.xyzw * r0.xyzw;
  o2.x = gVS_IsLuminance8;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v0.6.181 on Wed Nov  8 23:23:29 2023
//
//
// Buffer Definitions:
//
// cbuffer $Globals
// {
//
//   row_major float4x4 gVS_Projection; // Offset: 4800 Size:    64 [unused]
//   int gPs_AlphaTestEnable;           // Offset: 4864 Size:     4 [unused]
//   int gPs_AlphaTestType;             // Offset: 4880 Size:     4 [unused]
//   float gPs_AlphaTestRefValue;       // Offset: 4896 Size:     4 [unused]
//   float gPs_AlphaTestThreshold;      // Offset: 4912 Size:     4 [unused]
//   int gPs_MultiSampleMaskEnable;     // Offset: 4928 Size:     4 [unused]
//   int gPs_MultiSampleMask;           // Offset: 4944 Size:     4 [unused]
//   float4 gVS_I2FScale;               // Offset:    0 Size:    16
//   row_major float4x4 gVS_Mat;        // Offset:   16 Size:    64
//   float4 gVS_HScale;                 // Offset:   80 Size:    16
//   float4 gVS_HOffset;                // Offset:   96 Size:    16
//   float4 gVS_PScale;                 // Offset:  112 Size:    16 [unused]
//   float4 gVS_POffset;                // Offset:  128 Size:    16 [unused]
//   float4 gVS_BaseCol;                // Offset:  144 Size:    16
//   float4 gVS_UVMulAdd;               // Offset:  160 Size:    16 [unused]
//   float4 gVS_SnapParams;             // Offset:  176 Size:    16 [unused]
//   int gVS_IsLuminance8;              // Offset:  192 Size:     4
//   row_major float4x4 g_View;         // Offset: 4960 Size:    64 [unused]
//   row_major float4x4 g_ViewProjection;// Offset: 5024 Size:    64 [unused]
//   row_major float4x4 g_World;        // Offset: 5088 Size:    64 [unused]
//   row_major float4x4 g_WorldViewProjection;// Offset: 5152 Size:    64 [unused]
//   row_major float4x3 g_JointWorld[32];// Offset: 5216 Size:  2044 [unused]
//   float4 g_RenderTargetDimensions;   // Offset: 7264 Size:    16 [unused]
//   float4 g_RenderTargetNearFar;      // Offset: 7280 Size:    16 [unused]
//   float4 g_RenderTargetDepthRange;   // Offset: 7296 Size:    16 [unused]
//   float4 g_AdditiveColor;            // Offset: 7312 Size:    16 [unused]
//   float4 g_ModulateColor;            // Offset: 7328 Size:    16 [unused]
//   float4 g_EyePosition;              // Offset: 7344 Size:    16 [unused]
//   float4 g_ExternalShaderParam;      // Offset: 7360 Size:    16 [unused]
//   float4 g_LightColor0;              // Offset: 7376 Size:    16 [unused]
//   float4 g_LightPosition0;           // Offset: 7392 Size:    16 [unused]
//   float4 g_LightAttenuation0;        // Offset: 7408 Size:    16 [unused]
//   float4 g_LightColor1;              // Offset: 7424 Size:    16 [unused]
//   float4 g_LightPosition1;           // Offset: 7440 Size:    16 [unused]
//   float4 g_LightAttenuation1;        // Offset: 7456 Size:    16 [unused]
//   float4 g_LightColor2;              // Offset: 7472 Size:    16 [unused]
//   float4 g_LightPosition2;           // Offset: 7488 Size:    16 [unused]
//   float4 g_LightAttenuation2;        // Offset: 7504 Size:    16 [unused]
//   float4 g_LightColor3;              // Offset: 7520 Size:    16 [unused]
//   float4 g_LightPosition3;           // Offset: 7536 Size:    16 [unused]
//   float4 g_LightAttenuation3;        // Offset: 7552 Size:    16 [unused]
//   float4 g_LightColor4;              // Offset: 7568 Size:    16 [unused]
//   float4 g_LightPosition4;           // Offset: 7584 Size:    16 [unused]
//   float4 g_LightAttenuation4;        // Offset: 7600 Size:    16 [unused]
//   float4 g_LightColor5;              // Offset: 7616 Size:    16 [unused]
//   float4 g_LightPosition5;           // Offset: 7632 Size:    16 [unused]
//   float4 g_LightAttenuation5;        // Offset: 7648 Size:    16 [unused]
//   float4 g_LightColor6;              // Offset: 7664 Size:    16 [unused]
//   float4 g_LightPosition6;           // Offset: 7680 Size:    16 [unused]
//   float4 g_LightAttenuation6;        // Offset: 7696 Size:    16 [unused]
//   float4 g_LightColor7;              // Offset: 7712 Size:    16 [unused]
//   float4 g_LightPosition7;           // Offset: 7728 Size:    16 [unused]
//   float4 g_LightAttenuation7;        // Offset: 7744 Size:    16 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// $Globals                          cbuffer      NA          NA    0        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float   xyzw
// TEXCOORD                 1   xyzw        1     NONE   float   xyzw
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_POSITION              0   xyzw        0      POS   float   xyzw
// TEXCOORD                 1   xyzw        1     NONE   float   xyzw
// TEXCOORD                 2   x           2     NONE     int   x
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer CB0[13], immediateIndexed
dcl_input v0.xyzw
dcl_input v1.xyzw
dcl_output_siv o0.xyzw, position
dcl_output o1.xyzw
dcl_output o2.x
dcl_temps 1
dp4 r0.x, cb0[1].xyzw, v0.xyzw
dp4 r0.y, cb0[2].xyzw, v0.xyzw
mul r0.xy, r0.xyxx, cb0[0].xyxx
dp4 r0.z, cb0[3].xyzw, v0.xyzw
dp4 r0.w, cb0[4].xyzw, v0.xyzw
mad o0.xyzw, r0.xyzw, cb0[5].xyzw, cb0[6].xyzw
mul_sat r0.xyzw, l(1.99218750, 1.99218750, 1.99218750, 1.99218750), cb0[9].xyzw
mul o1.xyzw, r0.xyzw, v1.xyzw
mov o2.x, cb0[12].x
ret
// Approximately 10 instruction slots used

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