// HUD
// Objective marker pushed to depth via texture filtering
// Crosshairs pushed to depth based on position check

// cbuffer PSCB : register(b12)
// {
  // float4x4 g_xView : packoffset(c0);
  // float4x4 g_xProj : packoffset(c4);
  // float4x4 g_xViewProj : packoffset(c8);
  // float4x4 g_xInvView : packoffset(c12);
  // float4x4 g_xInvProj : packoffset(c16);
  // float4x4 g_xInvViewProj : packoffset(c20);
  // float3 g_xCamPos : packoffset(c24);
  // float2 g_xRcpScreenDims : packoffset(c25);
  // float2 g_xDepthProjViewUnpack : packoffset(c25.z);
  // float4 g_axViewFrustumPlanes[6] : packoffset(c26);
// }

// cbuffer PSCB2 : register(b13)
// {
  // float2 g_xCameraTanHalfFOV : packoffset(c0);
  // float2 g_xTwoOverScreenDims : packoffset(c0.z);
  // float3 g_xLightPos : packoffset(c1);
  // float g_fLightRadius : packoffset(c1.w);
  // float4x4 g_xLightOrientation : packoffset(c2);
  // float4x4 g_xCustomVolumeMatrix : packoffset(c6);
  // float3 g_xLightDir : packoffset(c10);
  // float g_fRcpLightRadius : packoffset(c10.w);
  // float g_fLightFalloffScale : packoffset(c11);
  // float g_fLightFalloffPower : packoffset(c11.y);
  // float g_fLightAngleOuter : packoffset(c11.z);
  // float g_fRcpLightAngleRange : packoffset(c11.w);
  // float3 g_xLightColour : packoffset(c12);
  // bool g_bUseLimitAngles : packoffset(c12.w);
  // float g_fArcCosLimitAngle : packoffset(c13);
  // float g_fRcpProjCubeLerpDist : packoffset(c13.y);
  // bool g_bUseProjCube : packoffset(c13.z);
  // bool g_bUseDualProjCube : packoffset(c13.w);
  // float g_fLightMeshRadius : packoffset(c14);
  // float2 g_xDepthProjection : packoffset(c14.y);
  // float g_fShadowStrength : packoffset(c14.w);
// }

struct vb_entry
{
  float2 vb_position;
  float2 vb_texcoord;
  uint color0;
};


StructuredBuffer<struct vb_entry> vb_struct : register(t100);

Texture2D<float4> IconFilter1 : register(t101);

// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
Texture2D<float4> StereoParams : register(t125);
#include <crosshair.hlsl>



float4 find_quad_center(uint id)
{
	id = id / 6 * 6;

	float4 pos1 = float4(vb_struct[id].vb_position,0,1);
	float4 pos2 = float4(vb_struct[id + 2].vb_position,0,1);

	return (pos1 + pos2) / 2;
}


// bool is_horiz_fullscreen()
// {
	// The input position to this shader is in pixels, so rather than use
	// calc_pos, just use the the screen resolution:
	// int2 resolution = int2(round(IniParams.Load(int2(1, 0)).zw));

	// A full screen effect will always use two trianges:
	// int vertex_count = int(round(IniParams.Load(int2(2, 0)).z));
	// if (vertex_count != 6)
		// return false;

	// Look up the positions of four vertices that should match the corners
	// (for this game):
	// float2 bl = vb_struct[0].vb_position;
	// float2 tl = vb_struct[1].vb_position;
	// float2 tr = vb_struct[2].vb_position;
	// float2 br = vb_struct[4].vb_position;

	// Check that all four corners are are at or beyond the edge of the
	// screen. Use a threshold of half a pixel to compensate for floating
	// point error
	// return (abs(float4(tl.x, tr.x - resolution.x,
		// bl.x, br.x - resolution.x)) <= 0.5);
// }

uint lookup_icon_filter(uint id, Texture2D<float4> IconFilter)
{
	uint width, height;
	id = id / 6 * 6;
	IconFilter.GetDimensions(width, height);
	float2 coord1 = vb_struct[id].vb_texcoord;
	float2 coord2 = vb_struct[id + 2].vb_texcoord;
	uint2 coord = round((coord1 + coord2) / 2 * float2(width, height));

	float4 colour = IconFilter.Load(int3(coord, 0));

	if (all(abs(colour - float4(0, 1, 0, 1)) <= 1/512))
		return 3;
	if (all(abs(colour - float4(0, 0, 1, 1)) <= 1/512))
		return 1;
	return 0;
}





void main( 
  float2 v0 : Texcoords0,
  float4 v1 : Colour0,
  float4 v2 : SV_Position0,
  out float2 o0 : Texcoords0,
  out float4 o1 : Colour0,
  out float4 o2 : SV_Position0,
uint id : SV_VertexID
  )
  
{
  o0.xy = v0.xy;
  o1.xyzw = v1.xyzw;
  o2.xyzw = v2.xyzw;
  
float4 stereo = StereoParams.Load(0);
float4 params = IniParams.Load(0);
float4 tex_filter = IniParams.Load(int2(2,0));
uint action = 0;
float4 icon_center = find_quad_center(id);
float adj;

if (tex_filter.x == 2 && o2.x > -0.2 && o2.x < 0.2)
{
	action = 1;
}
else if (tex_filter.x == 3)
{	
	action = 2;
} 
else if (tex_filter.x == 4)
{	
	action=1;
}
else if (tex_filter.x == 5)
{	
	action = lookup_icon_filter(id, IconFilter1);
}	
	
switch (action) {
case 1:
	o2.x += adjust_from_depth_buffer(0,0)*0.9;
	// adj= adjust_from_depth_buffer(0,0);
	// o2.x += adj *(0.5+(stereo.x/adj/2))*0.9;
	break;
case 2:
	o2.x += adjust_from_depth_buffer(o2.x, o2.y)*0.9;
	// adj = adjust_from_depth_buffer(o2.x, o2.y);
	// o2.x +=adj *adj/stereo.x*params.w;
	
	break;
case 3:
	o2.x += adjust_from_depth_buffer(icon_center.x, icon_center.y)*0.9;
	break;
default:
	o2.x += stereo.x * params.z;
	break;
}
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.2.43 on Mon Oct 03 02:35:32 2016
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// Texcoords                0   xy          0     NONE   float   xy  
// Colour                   0   xyzw        1     NONE   float   xyzw
// SV_Position              0   xyzw        2     NONE   float   xyzw
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// Texcoords                0   xy          0     NONE   float   xy  
// Colour                   0   xyzw        1     NONE   float   xyzw
// SV_Position              0   xyzw        2      POS   float   xyzw
//
vs_4_0
dcl_input v0.xy
dcl_input v1.xyzw
dcl_input v2.xyzw
dcl_output o0.xy
dcl_output o1.xyzw
dcl_output_siv o2.xyzw, position
mov o0.xy, v0.xyxx
mov o1.xyzw, v1.xyzw
mov o2.xyzw, v2.xyzw
ret 
// Approximately 4 instruction slots used

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