//HUD 2 AWOFAI
// ---- Created with 3Dmigoto v0.6.164 on Sun Apr 12 09:38:34 2026
cbuffer cb4 : register(b4)
{
  float4 cb4[276];
}




// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
Texture2D<float4> StereoParams : register(t125);
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 (far*near/((1-z)*near) + (far*z));
}

float adjust_from_depth_buffer(float x, float y, float multiplier)
{
    float4 stereo = StereoParams.Load(0);
	float4 testeo = IniParams.Load(int2(90,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;

    offset = (near - convergence) * separation ;
    distance = separation - offset;

    old_offset = offset;
    
    for (i = 0; i < 255; i++) {
        offset += distance / 255.0;
		//w = (separation * convergence*37.3101845) / (separation - offset);
		//w = (separation * convergence*5) / (separation - offset);
        w = (separation * convergence * multiplier) / (separation - offset);
		sampled_w = world_z_from_depth_buffer(x + offset, y);
        
        if (sampled_w == 0)
            return separation;

        if (w > sampled_w)
            break;

        old_offset = offset;
    }

    return old_offset;
}


void main(
  float4 v0 : POSITION0,
  float4 v1 : COLOR0,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : TEXCOORD8,
  out float4 o2 : COLOR0,
  out float4 o3 : COLOR1,
  out float4 o4 : TEXCOORD9,
  out float4 o5 : TEXCOORD0,
  out float4 o6 : TEXCOORD1,
  out float4 o7 : TEXCOORD2,
  out float4 o8 : TEXCOORD3,
  out float4 o9 : TEXCOORD4,
  out float4 o10 : TEXCOORD5,
  out float4 o11 : TEXCOORD6,
  out float4 o12 : TEXCOORD7)
{
  float4 r0,r1,r2;
  uint4 bitmask, uiDest;
  float4 fDest;
  
float4 s = StereoParams.Load(0);
float HUDdepth = IniParams[80].z; 
float Map = IniParams[88].x;
float BoxDepth = IniParams[80].w;  

float cursor_x = IniParams[0].x;
float cursor_y = IniParams[0].y;  

  r1.xyzw = cb4[21].xyzw * v0.yyyy;
  r1.xyzw = cb4[20].xyzw * v0.xxxx + r1.xyzw;
  r1.xyzw = cb4[22].xyzw * v0.zzzz + r1.xyzw;
  r0.xyzw = cb4[23].xyzw * v0.wwww + r1.xyzw;
  o2.xyzw = cb4[32].xyzw * v1.xyzw;
  r1.x = cb4[29].z * v0.y;
  r1.x = cb4[28].z * v0.x + r1.x;
  r1.x = cb4[30].z * v0.z + r1.x;
  r1.x = cb4[31].z * v0.w + r1.x;
  r1.x = max(0.100000001, r1.x);
  r2.y = rcp(r1.x);
  r2.x = (uint)r2.y & 0x7fffffff;
  r2.x = (int)r2.x == 0x7f800000;
  r1.x = r2.x ? asfloat(0x7f800000) : r2.y;
  r1.yzw = cb4[25].xyz * v0.yyy;
  r1.yzw = cb4[24].xyz * v0.xxx + r1.yzw;
  r1.yzw = cb4[26].xyz * v0.zzz + r1.yzw;
  r1.yzw = cb4[27].xyz * v0.www + r1.yzw;
  r1.y = dot(r1.yzw, r1.yzw);
  r2.y = rsqrt(abs(r1.y));
  r2.x = (int)r2.y == 0x7f800000;
  r1.y = r2.x ? asfloat(0x7f800000) : r2.y;
  r2.y = rcp(r1.y);
  r2.x = (uint)r2.y & 0x7fffffff;
  r2.x = (int)r2.x == 0x7f800000;
  r1.y = r2.x ? asfloat(0x7f800000) : r2.y;
  r1.z = r1.y * r1.y;
  r1.y = cb4[33].x * r1.y;
  r1.x = r1.z * r1.x;
  r1.x = cb4[33].x * r1.x;
  r1.x = r1.y * 4 + r1.x;
  r1.x = cb4[33].x * r1.x;
  r1.x = exp2(r1.x);
  r2.y = rcp(r1.x);
  r2.x = (uint)r2.y & 0x7fffffff;
  r2.x = (int)r2.x == 0x7f800000;
  o4.x = r2.x ? asfloat(0x7f800000) : r2.y;
  o0.xyzw = r0.xyzw;
  
if (Map != 1){
	if (o0.w != 1) {
		o0.x -= s.x * (o0.w - s.y);
		o0.x += s.x * HUDdepth;
	}
}
if (Map == 1){
		if (o0.w != 1) {
		o0.x -= s.x * (o0.w - s.y);
		float2 cursor_clip = (float2(cursor_x, cursor_y) * 2 - 1) * float2(1, -1);
		float box_offset = adjust_from_depth_buffer(cursor_clip.x, cursor_clip.y, 5);
		o0.x += box_offset * o0.w;
		//o0.x += s.x * BoxDepth;
}
}  
  
  o1.xyzw = r0.xyzw;
  o3.xyzw = float4(0,0,0,1);
  o4.yzw = float3(0,0,0);
  o5.xyzw = float4(0,0,0,1);
  o6.xyzw = float4(0,0,0,1);
  o7.xyzw = float4(0,0,0,1);
  o8.xyzw = float4(0,0,0,1);
  o9.xyzw = float4(0,0,0,1);
  o10.xyzw = float4(0,0,0,1);
  o11.xyzw = float4(0,0,0,1);
  o12.xyzw = float4(0,0,0,1);
  return;
}
