//HUD
// ---- Created with 3Dmigoto v0.6.164 on Tue Mar 31 15:58:06 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,
  float4 v2 : TEXCOORD0,
  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,
  out uint o13 : P0)
{
  float4 r0,r1,r2,r3,r4,r5;
  uint4 bitmask, uiDest;
  float4 fDest;
  
float crosshair = IniParams[14].x; 
//float4 s = StereoParams.Load(0);
float4 texFilter = IniParams.Load(int2(14,0)); 
float4 testeo = IniParams.Load(int2(90,0));
float HUDdepth = IniParams[80].z; 
float BoxDepth = IniParams[80].w; 
float Map = IniParams[88].x; 

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


  r5.xyzw = StereoParams.Load(int4(0,0,0,0)).xyzw;
  o13.x = r5.z;
  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[28].xyzw * v1.xyzw;
  r1.xyz = cb4[25].xyz * v0.yyy;
  r1.xyz = cb4[24].xyz * v0.xxx + r1.xyz;
  r1.xyz = cb4[26].xyz * v0.zzz + r1.xyz;
  r1.xyz = cb4[27].xyz * v0.www + r1.xyz;
  r1.x = dot(r1.xyz, r1.xyz);
  r2.y = rsqrt(abs(r1.x));
  r2.x = (int)r2.y == (int)0x7f800000;
  r1.x = r2.x ? asfloat(0x7f800000) : r2.y;
  r2.y = rcp(r1.x);
  r2.x = (int)r2.y & 0x7fffffff;
  r2.x = (int)r2.x == (int)0x7f800000;
  r1.x = r2.x ? asfloat(0x7f800000) : r2.y;
  r1.x = cb4[29].x * r1.x;
  r1.x = exp2(r1.x);
  r2.y = rcp(r1.x);
  r2.x = (int)r2.y & 0x7fffffff;
  r2.x = (int)r2.x == (int)0x7f800000;
  o4.x = r2.x ? asfloat(0x7f800000) : r2.y;
  o5.xy = v2.xy;
  r3.xyzw = r0.xyzw;
  o1.xyzw = r0.xyzw;
  o3.xyzw = float4(0,0,0,1);
  o4.yzw = float3(1,0,0);
  o5.zw = float2(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);
 

  
  r4.x = r3.w + -r5.y;
  r4.y = r5.w * r5.x;
  r4.x = r4.x * r4.y + r3.x;
  r4.y = (r3.w != 1.0f);
  r3.x = r4.y ? r4.x : r3.x;
  o0.xyzw = r3.xyzw;





if (Map != 1){

	if (o0.w != 1  ){
		if (crosshair == 15){
		o0.x -= r5.x * (o0.w - r5.y);
			float adjustment = adjust_from_depth_buffer(0, 0, 36);
			o0.x += adjustment * o0.w;
		}
		else if (crosshair == 16){
			o0.x -= r5.x * (o0.w - r5.y);
			o0.x += r5.x * HUDdepth;
		}
	}
}


if (Map == 1){
		if (crosshair == 16 && o0.w != 1){
			o0.x -= r5.x * (o0.w - r5.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 += r5.x * BoxDepth;
		}
}



	
  
/* 
if (crosshair == 15){
o0.x -= r5.x * (o0.w - r5.y);
//o0.x += r5.x * o0.w * testeo.x;
float adjustment = adjust_from_depth_buffer(0, 0);
o0.x += adjustment * o0.w;
}
*/

/*
o0.x -= r5.x * (o0.w - r5.y);
if (crosshair == 15){
float adjustment = adjust_from_depth_buffer(0, 0);
o0.x += adjustment * o0.w;
}
*/
	
	
//uint width, height;
//DepthBuffer.GetDimensions(width, height);
//if (width > 0) {
//    float depth = DepthBuffer.Load(int3(width/2, height/2, 0)).x;
//    o2.rgba = float4(depth, depth, depth, 1);
//}	


  
  return;
}