//DMC5 HUD 1.
// ---- Created with 3Dmigoto v1.3.15 on Sat Mar 09 17:41:07 2019

cbuffer SceneInfo : register(b0)
{
  row_major float4x4 viewProjMat : packoffset(c0);
  row_major float3x4 transposeViewMat : packoffset(c4);
  row_major float3x4 transposeViewInvMat : packoffset(c7);
  float4 projElement[2] : packoffset(c10);
  float4 projInvElements[2] : packoffset(c12);
  row_major float4x4 viewProjInvMat : packoffset(c14);
  row_major float4x4 prevViewProjMat : packoffset(c18);
  float3 ZToLinear : packoffset(c22);
  float subdivisionLevel : packoffset(c22.w);
  float2 screenSize : packoffset(c23);
  float2 screenInverseSize : packoffset(c23.z);
  float2 cullingHelper : packoffset(c24);
  float cameraNearPlane : packoffset(c24.z);
  float cameraFarPlane : packoffset(c24.w);
  float4 viewFrustum[6] : packoffset(c25);
  float4 clipplane : packoffset(c31);
}

cbuffer GUIConstant : register(b1)
{
  row_major float4x4 guiViewMatrix : packoffset(c0);
  row_major float4x4 guiProjMatrix : packoffset(c4);
  row_major float4x4 guiWorldMat : packoffset(c8);
  float guiIntensity : packoffset(c12);
  float guiSaturation : packoffset(c12.y);
  float guiSoftParticleDist : packoffset(c12.z);
  float guiFilterParam : packoffset(c12.w);
  float guiFilterMipLevel : packoffset(c13);
  float guiAlphaBlendRate : packoffset(c13.y);
  float guiStencilScale : packoffset(c13.z);
  float guiConstantPadding0 : packoffset(c13.w);
}

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

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

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; // 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*100) / (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(
  float3 v0 : POSITION0,
  float4 v1 : COLOR0,
  float2 v2 : TEXCOORD0,
  out float4 o0 : SV_Position0,
  out float4 o1 : Color0,
  out float2 o2 : TexCoord0,
  out float2 p2 : TexCoord1)
{
  float4 r0,r1;
  uint4 bitmask, uiDest;
  float4 fDest;

  float4 stereo = StereoParams.Load(0);
  float4 iniparams = IniParams.Load(0);
  float4 iniparams1 = IniParams.Load(int2(1,0));
  float4 iniparams3 = IniParams.Load(int2(3,0));
  
  r0.xyzw = guiWorldMat._m10_m11_m12_m13 * v0.yyyy;
  r0.xyzw = v0.xxxx * guiWorldMat._m00_m01_m02_m03 + r0.xyzw;
  r0.xyzw = v0.zzzz * guiWorldMat._m20_m21_m22_m23 + r0.xyzw;
  r0.xyzw = guiWorldMat._m30_m31_m32_m33 + r0.xyzw;
  r1.xyzw = guiViewMatrix._m10_m11_m12_m13 * r0.yyyy;
  r1.xyzw = r0.xxxx * guiViewMatrix._m00_m01_m02_m03 + r1.xyzw;
  r1.xyzw = r0.zzzz * guiViewMatrix._m20_m21_m22_m23 + r1.xyzw;
  r0.xyzw = r0.wwww * guiViewMatrix._m30_m31_m32_m33 + r1.xyzw;
  r1.xyzw = viewProjMat._m10_m11_m12_m13 * r0.yyyy;
  r1.xyzw = r0.xxxx * viewProjMat._m00_m01_m02_m03 + r1.xyzw;
  r1.xyzw = r0.zzzz * viewProjMat._m20_m21_m22_m23 + r1.xyzw;
  o0.xyzw = r0.wwww * viewProjMat._m30_m31_m32_m33 + r1.xyzw;
  o1.xyzw = v1.xyzw;
  o2.xy = v2.xy;
  if (driver_sep)
  {
	o0.x += driver_sep * o0.w + driver_neg_sep_conv;
  }
  else
  {
	o0.x+=stereo.x*(o0.w-stereo.y);
  }
  o0.x=o0.x*iniparams.y;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v1.3.15 on Sat Mar 09 17:41:07 2019
//
//
// Buffer Definitions:
//
// cbuffer SceneInfo
// {
//
//   row_major float4x4 viewProjMat;    // Offset:    0 Size:    64
//   row_major float3x4 transposeViewMat;// Offset:   64 Size:    48 [unused]
//   row_major float3x4 transposeViewInvMat;// Offset:  112 Size:    48 [unused]
//   float4 projElement[2];             // Offset:  160 Size:    32 [unused]
//   float4 projInvElements[2];         // Offset:  192 Size:    32 [unused]
//   row_major float4x4 viewProjInvMat; // Offset:  224 Size:    64 [unused]
//   row_major float4x4 prevViewProjMat;// Offset:  288 Size:    64 [unused]
//   float3 ZToLinear;                  // Offset:  352 Size:    12 [unused]
//   float subdivisionLevel;            // Offset:  364 Size:     4 [unused]
//   float2 screenSize;                 // Offset:  368 Size:     8 [unused]
//   float2 screenInverseSize;          // Offset:  376 Size:     8 [unused]
//   float2 cullingHelper;              // Offset:  384 Size:     8 [unused]
//   float cameraNearPlane;             // Offset:  392 Size:     4 [unused]
//   float cameraFarPlane;              // Offset:  396 Size:     4 [unused]
//   float4 viewFrustum[6];             // Offset:  400 Size:    96 [unused]
//   float4 clipplane;                  // Offset:  496 Size:    16 [unused]
//
// }
//
// cbuffer GUIConstant
// {
//
//   row_major float4x4 guiViewMatrix;  // Offset:    0 Size:    64
//   row_major float4x4 guiProjMatrix;  // Offset:   64 Size:    64 [unused]
//   row_major float4x4 guiWorldMat;    // Offset:  128 Size:    64
//   float guiIntensity;                // Offset:  192 Size:     4 [unused]
//   float guiSaturation;               // Offset:  196 Size:     4 [unused]
//   float guiSoftParticleDist;         // Offset:  200 Size:     4 [unused]
//   float guiFilterParam;              // Offset:  204 Size:     4 [unused]
//   float guiFilterMipLevel;           // Offset:  208 Size:     4 [unused]
//   float guiAlphaBlendRate;           // Offset:  212 Size:     4 [unused]
//   float guiStencilScale;             // Offset:  216 Size:     4 [unused]
//   float guiConstantPadding0;         // Offset:  220 Size:     4 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// SceneInfo                         cbuffer      NA          NA    0        1
// GUIConstant                       cbuffer      NA          NA    1        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyz         0     NONE   float   xyz
// COLOR                    0   xyzw        1     NONE   float   xyzw
// TEXCOORD                 0   xy          2     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
// TexCoord                 1     zw        2     NONE   float
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[4], immediateIndexed
dcl_constantbuffer cb1[12], immediateIndexed
dcl_input v0.xyz
dcl_input v1.xyzw
dcl_input v2.xy
dcl_output_siv o0.xyzw, position
dcl_output o1.xyzw
dcl_output o2.xy
dcl_temps 2
mul r0.xyzw, v0.yyyy, cb1[9].xyzw
mad r0.xyzw, v0.xxxx, cb1[8].xyzw, r0.xyzw
mad r0.xyzw, v0.zzzz, cb1[10].xyzw, r0.xyzw
add r0.xyzw, r0.xyzw, cb1[11].xyzw
mul r1.xyzw, r0.yyyy, cb1[1].xyzw
mad r1.xyzw, r0.xxxx, cb1[0].xyzw, r1.xyzw
mad r1.xyzw, r0.zzzz, cb1[2].xyzw, r1.xyzw
mad r0.xyzw, r0.wwww, cb1[3].xyzw, r1.xyzw
mul r1.xyzw, r0.yyyy, cb0[1].xyzw
mad r1.xyzw, r0.xxxx, cb0[0].xyzw, r1.xyzw
mad r1.xyzw, r0.zzzz, cb0[2].xyzw, r1.xyzw
mad o0.xyzw, r0.wwww, cb0[3].xyzw, r1.xyzw
mov o1.xyzw, v1.xyzw
mov o2.xy, v2.xyxx
ret
// Approximately 15 instruction slots used

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