//UI MAGIC (HUD 2)

cbuffer GUIConstant : register(b0)
{
  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);
}



// 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 = guiProjMatrix._m10_m11_m12_m13 * r0.yyyy;
  r1.xyzw = r0.xxxx * guiProjMatrix._m00_m01_m02_m03 + r1.xyzw;
  r1.xyzw = r0.zzzz * guiProjMatrix._m20_m21_m22_m23 + r1.xyzw;
  r0.xyzw = r0.wwww * guiProjMatrix._m30_m31_m32_m33 + r1.xyzw;
  o0.xyzw = r0.xyzw;
  r0.xy = float2(1,1) + r0.xy;
  r0.xy = float2(0.5,0.5) * r0.xy;
  p2.xy = r0.xy * float2(1,-1) + float2(0,1);
  o1.xyzw = v1.xyzw;
  o2.xy = v2.xy;
  if (iniparams3.x!=98 && iniparams3.x!=20 && o0.w==1)
  {
    if (iniparams1.w==3)
    {
      float2 center = mul(mul(guiWorldMat[3], guiViewMatrix), guiProjMatrix).xy;
	  o0.x+=adjust_from_depth_buffer(center.x, center.y);
    }
    else if (iniparams1.w==2)
    {
      o0.x+=adjust_from_depth_buffer(0,0);
    }
    else
    {
      o0.x+=stereo.x*iniparams.x;
    }
	o0.x=o0.x*iniparams.y;
  }
  ////////////////////////////////////////////////////// 
  // Disable FIX
  if (IniParams[0].w == 0)
	return;
 
  // Load our Stuff
  float4 tex_filter = IniParams.Load(int2(3,0));
  float4 render = IniParams.Load(int2(2, 0));
  
  // Out factors
  float monitorCount = round((render.z/ render.w) / IniParams[0].z* 100) / 100;
  monitorCount = monitorCount > 2.0 ? monitorCount : 1;
  float uiRenderSize = round (render.z / monitorCount * 1000) / 1000;
  float factor = round((render.z - uiRenderSize)/ render.z * 1000) / 1000;
  float split = round((1.0 / monitorCount) * 1000) / 1000 + 0.01;
  //////////////////////////////////////////////////////
  
  if (render.x == render.z && render.y == render.w)
  {	
	if (o0.x <= -split)
	{
		o0.x += factor;
		p2.x += round (factor/2.00 * 100)/100;
	}
	else if (o0.x >= split)
	{
		o0.x -= factor;
		p2.x -= round (factor/2.00 * 100)/100;
	}
  }
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v1.3.12 on Sat Jan 26 00:40:11 2019
//
//
// Buffer Definitions:
//
// cbuffer GUIConstant
// {
//
//   row_major float4x4 guiViewMatrix;  // Offset:    0 Size:    64
//   row_major float4x4 guiProjMatrix;  // Offset:   64 Size:    64
//   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]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// GUIConstant                       cbuffer      NA          NA    0        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     zw
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[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_output o2.zw
dcl_temps 2
mul r0.xyzw, v0.yyyy, cb0[9].xyzw
mad r0.xyzw, v0.xxxx, cb0[8].xyzw, r0.xyzw
mad r0.xyzw, v0.zzzz, cb0[10].xyzw, r0.xyzw
add r0.xyzw, r0.xyzw, cb0[11].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 r0.xyzw, r0.wwww, cb0[3].xyzw, r1.xyzw
mul r1.xyzw, r0.yyyy, cb0[5].xyzw
mad r1.xyzw, r0.xxxx, cb0[4].xyzw, r1.xyzw
mad r1.xyzw, r0.zzzz, cb0[6].xyzw, r1.xyzw
mad r0.xyzw, r0.wwww, cb0[7].xyzw, r1.xyzw
mov o0.xyzw, r0.xyzw
add r0.xy, r0.xyxx, l(1.000000, 1.000000, 0.000000, 0.000000)
mul r0.xy, r0.xyxx, l(0.500000, 0.500000, 0.000000, 0.000000)
mad o2.zw, r0.xxxy, l(0.000000, 0.000000, 1.000000, -1.000000), l(0.000000, 0.000000, 0.000000, 1.000000)
mov o1.xyzw, v1.xyzw
mov o2.xy, v2.xyxx
ret
// Approximately 19 instruction slots used

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