// UI?
// ---- Created with geo-11 v0.7.10 on Sun Aug 23 00:03:36 2026

cbuffer ViewProjectionMatrixConstants : register(b0)
{
  row_major float4x4 View : packoffset(c0);
  row_major float4x4 Projection : packoffset(c4);
}

cbuffer WorldMatrixConstants : register(b1)
{
  row_major float4x4 World : packoffset(c0);
}



// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
Texture2D<float4> StereoParams : register(t125);
Texture2D<float> DepthBuffer : register(t110);

static const float near = 0.00001;
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));
}

// from masterotaku's Batman Arkam Knight fix:
// https://helixmod.blogspot.com/2020/12/batman-arkham-knight-dx11.html
float adjust_from_depth_buffer(float x, float y, float numsamples)
{
    float4 stereo = StereoParams.Load(0);
    // if separation is not present (depth is 0), abort
    if (stereo.x==0) {return 0;}
    float separation = stereo.x;
    float convergence = stereo.y;
    float old_offset, offset, w, sampled_w, distance;
    uint i;

    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 * 0.1) / (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 : SV_Position0,
  float4 v1 : COLOR0,
  float2 v2 : TEXCOORD0,
  out float4 o0 : SV_Position0,
  out float4 o1 : COLOR0,
  out float2 o2 : TEXCOORD0)
{
  float4 r0,r1;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xyz = World._m11_m11_m11 * View._m10_m11_m12;
  r0.xyz = World._m10_m10_m10 * View._m00_m01_m02 + r0.xyz;
  r0.xyz = World._m12_m12_m12 * View._m20_m21_m22 + r0.xyz;
  r0.xyz = World._m13_m13_m13 * View._m30_m31_m32 + r0.xyz;
  r0.xyz = v0.yyy * r0.xyz;
  r1.xyz = World._m01_m01_m01 * View._m10_m11_m12;
  r1.xyz = World._m00_m00_m00 * View._m00_m01_m02 + r1.xyz;
  r1.xyz = World._m02_m02_m02 * View._m20_m21_m22 + r1.xyz;
  r1.xyz = World._m03_m03_m03 * View._m30_m31_m32 + r1.xyz;
  r0.xyz = v0.xxx * r1.xyz + r0.xyz;
  r1.xyz = World._m21_m21_m21 * View._m10_m11_m12;
  r1.xyz = World._m20_m20_m20 * View._m00_m01_m02 + r1.xyz;
  r1.xyz = World._m22_m22_m22 * View._m20_m21_m22 + r1.xyz;
  r1.xyz = World._m23_m23_m23 * View._m30_m31_m32 + r1.xyz;
  r0.xyz = v0.zzz * r1.xyz + r0.xyz;
  r1.xyz = World._m31_m31_m31 * View._m10_m11_m12;
  r1.xyz = World._m30_m30_m30 * View._m00_m01_m02 + r1.xyz;
  r1.xyz = World._m32_m32_m32 * View._m20_m21_m22 + r1.xyz;
  r1.xyz = World._m33_m33_m33 * View._m30_m31_m32 + r1.xyz;
  r0.xyz = r1.xyz + r0.xyz;
  r1.xyzw = Projection._m10_m11_m12_m13 * r0.yyyy;
  r1.xyzw = r0.xxxx * Projection._m00_m01_m02_m03 + r1.xyzw;
  r0.xyzw = r0.zzzz * Projection._m20_m21_m22_m23 + r1.xyzw;
  o0.xyzw = Projection._m30_m31_m32_m33 + r0.xyzw;

    //o0.x += adjust_from_depth_buffer(0,0,255);
  
  o1.xyzw = v1.xyzw;
  o2.xy = v2.xy;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v0.7.10 on Sun Aug 23 00:03:36 2026
//
//
// Buffer Definitions:
//
// cbuffer ViewProjectionMatrixConstants
// {
//
//   row_major float4x4 View;           // Offset:    0 Size:    64
//   row_major float4x4 Projection;     // Offset:   64 Size:    64
//
// }
//
// cbuffer WorldMatrixConstants
// {
//
//   row_major float4x4 World;          // Offset:    0 Size:    64
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// ViewProjectionMatrixConstants     cbuffer      NA          NA    0        1
// WorldMatrixConstants              cbuffer      NA          NA    1        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_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
//
vs_4_0
dcl_constantbuffer CB0[8], immediateIndexed
dcl_constantbuffer CB1[4], 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.xyz, cb0[1].xyzx, cb1[1].yyyy
mad r0.xyz, cb1[1].xxxx, cb0[0].xyzx, r0.xyzx
mad r0.xyz, cb1[1].zzzz, cb0[2].xyzx, r0.xyzx
mad r0.xyz, cb1[1].wwww, cb0[3].xyzx, r0.xyzx
mul r0.xyz, r0.xyzx, v0.yyyy
mul r1.xyz, cb0[1].xyzx, cb1[0].yyyy
mad r1.xyz, cb1[0].xxxx, cb0[0].xyzx, r1.xyzx
mad r1.xyz, cb1[0].zzzz, cb0[2].xyzx, r1.xyzx
mad r1.xyz, cb1[0].wwww, cb0[3].xyzx, r1.xyzx
mad r0.xyz, v0.xxxx, r1.xyzx, r0.xyzx
mul r1.xyz, cb0[1].xyzx, cb1[2].yyyy
mad r1.xyz, cb1[2].xxxx, cb0[0].xyzx, r1.xyzx
mad r1.xyz, cb1[2].zzzz, cb0[2].xyzx, r1.xyzx
mad r1.xyz, cb1[2].wwww, cb0[3].xyzx, r1.xyzx
mad r0.xyz, v0.zzzz, r1.xyzx, r0.xyzx
mul r1.xyz, cb0[1].xyzx, cb1[3].yyyy
mad r1.xyz, cb1[3].xxxx, cb0[0].xyzx, r1.xyzx
mad r1.xyz, cb1[3].zzzz, cb0[2].xyzx, r1.xyzx
mad r1.xyz, cb1[3].wwww, cb0[3].xyzx, r1.xyzx
add r0.xyz, r0.xyzx, r1.xyzx
mul r1.xyzw, r0.yyyy, cb0[5].xyzw
mad r1.xyzw, r0.xxxx, cb0[4].xyzw, r1.xyzw
mad r0.xyzw, r0.zzzz, cb0[6].xyzw, r1.xyzw
add o0.xyzw, r0.xyzw, cb0[7].xyzw
mov o1.xyzw, v1.xyzw
mov o2.xy, v2.xyxx
ret
// Approximately 27 instruction slots used

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