// Lens Flare VS Fixed

cbuffer WorldBuffer : register(b0)
{

  struct
  {
    row_major float4x4 ViewProject;

    struct
    {
      row_major float4x4 World;
      row_major float4x4 WorldViewProject;
    } PerInstance[256];

  } WorldParameters : packoffset(c0);

}

cbuffer StreamDeclBuffer : register(b3)
{
  float4 NormalScaleOffset : packoffset(c0);
  float4 TexcoordScales : packoffset(c1);
}

cbuffer InstanceBuffer : register(b5)
{

  struct
  {
    float4 InstanceParams[8];
  } InstanceParameters[256] : packoffset(c0);

}

Texture2D<float4> p_Vertex_40BA65604160590_Param_texture : register(t0);


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

// Depth buffer copied from other shaders to this input with 3Dmigoto:
Texture2D<float4> DepthBuffer : register(t110);

static const float near = 0.1;
static const float far = 40000;

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));
	
	return z * 1;
	
	if (z == 1)
		return 0;

	// Derive world Z from depth buffer. This is a kluge since I don't know
	// the correct scaling, and the Z buffer seems to be (1 - what I expected).
	// Might be able to determine the correct way to scale it from other shaders.
	return far*near/(((1-z)*near) + (far*z));
}

float adjust_from_depth_buffer(float x, float y)
{
	float4 stereo = StereoParams.Load(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 < 255; i++) 
	{
		offset += distance / 255.0;

		// Calculate depth for this point on the line:
		w = (separation * convergence) / (separation - offset);

		sampled_w = world_z_from_depth_buffer(x, y);
		if (sampled_w == 0)
			return 0;

		// 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( 
  float4 v0 : COLOR0,
  float4 v1 : TEXCOORD0,
  float3 v2 : POSITION0,
  uint v3 : SV_InstanceID0,
  uint v4 : SV_VertexID0,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : COLOR0,
  out float4 o2 : COLOR1,
  out float4 o3 : TEXCOORD0)
{
  float4 r0,r1,r2;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.zw = float2(0,0);
  r1.x = (uint)v3.x << 3;
  r0.xy = (int2)InstanceParameters[r1.x].InstanceParams[0].xy;
  r0.xyzw = p_Vertex_40BA65604160590_Param_texture.Load(r0.xyz).xyzw;
  r1.y = cmp(r0.w < 0.00999999978);
  o2.xyzw = r0.xyzw;
  r0.xyz = r1.yyy ? float3(0,0,0) : v2.xyz;
  r2.xyzw = WorldParameters.PerInstance[r1.x/4].WorldViewProject._m10_m11_m12_m13 * r0.yyyy;
  r2.xyzw = r0.xxxx * WorldParameters.PerInstance[r1.x/4].WorldViewProject._m00_m01_m02_m03 + r2.xyzw;
  r0.xyzw = r0.zzzz * WorldParameters.PerInstance[r1.x/4].WorldViewProject._m20_m21_m22_m23 + r2.xyzw;
  o0.xyzw = WorldParameters.PerInstance[r1.x/4].WorldViewProject._m30_m31_m32_m33 + r0.xyzw;
  o1.xyzw = v0.zyxw;
  o3.xy = TexcoordScales.xx * v1.xy;
  o3.zw = float2(0,0);
  
  float4 stereo = StereoParams.Load(0);
  o0.x += stereo.x * (o0.w - stereo.y);  
 
// Normalise the effect to W==convergence to neutralise the driver's stereo
// correction. Only do this while stereo is enabled, convergence is not zero
// and the effect is in front of the camera (otherwise bad things happen):
if (stereo.y && o0.w > 0) {
	o0 = o0 / o0.w * stereo.y;
}

 
 
  
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 6.3.9600.17672
//
//   using 3Dmigoto v1.2.27 on Fri Feb 05 01:36:37 2016
//
//
// Buffer Definitions: 
//
// cbuffer WorldBuffer
// {
//
//   struct WorldStruct
//   {
//       
//       row_major float4x4 ViewProject;// Offset:    0
//       
//       struct WorldStruct_PerInstanceData
//       {
//           
//           row_major float4x4 World;  // Offset:   64
//           row_major float4x4 WorldViewProject;// Offset:  128
//
//       } PerInstance[256];            // Offset:   64
//
//   } WorldParameters;                 // Offset:    0 Size: 32832
//
// }
//
// cbuffer StreamDeclBuffer
// {
//
//   float4 NormalScaleOffset;          // Offset:    0 Size:    16 [unused]
//   float4 TexcoordScales;             // Offset:   16 Size:    16
//
// }
//
// cbuffer InstanceBuffer
// {
//
//   struct InstanceStruct
//   {
//       
//       float4 InstanceParams[8];      // Offset:    0
//
//   } InstanceParameters[256];         // Offset:    0 Size: 32768
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// p_Vertex_40BA65604160590_Param_texture    texture  float4          2d    0        1
// WorldBuffer                       cbuffer      NA          NA    0        1
// StreamDeclBuffer                  cbuffer      NA          NA    3        1
// InstanceBuffer                    cbuffer      NA          NA    5        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// COLOR                    0   xyzw        0     NONE   float   xyzw
// TEXCOORD                 0   xyzw        1     NONE   float   xy  
// POSITION                 0   xyz         2     NONE   float   xyz 
// SV_InstanceID            0   x           3   INSTID    uint   x   
// SV_VertexID              0   x           4   VERTID    uint       
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_POSITION              0   xyzw        0      POS   float   xyzw
// COLOR                    0   xyzw        1     NONE   float   xyzw
// COLOR                    1   xyzw        2     NONE   float   xyzw
// TEXCOORD                 0   xyzw        3     NONE   float   xyzw
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[2052], dynamicIndexed
dcl_constantbuffer cb3[2], immediateIndexed
dcl_constantbuffer cb5[2041], dynamicIndexed
dcl_resource_texture2d (float,float,float,float) t0
dcl_input v0.xyzw
dcl_input v1.xy
dcl_input v2.xyz
dcl_input_sgv v3.x, instance_id
dcl_output_siv o0.xyzw, position
dcl_output o1.xyzw
dcl_output o2.xyzw
dcl_output o3.xyzw
dcl_temps 3
mov r0.zw, l(0,0,0,0)
ishl r1.x, v3.x, l(3)
ftoi r0.xy, cb5[r1.x + 0].xyxx
ld_indexable(texture2d)(float,float,float,float) r0.xyzw, r0.xyzw, t0.xyzw
lt r1.y, r0.w, l(0.010000)
mov o2.xyzw, r0.xyzw
movc r0.xyz, r1.yyyy, l(0xffc00000,0xffc00000,0xffc00000,0), v2.xyzx
mul r2.xyzw, r0.yyyy, cb0[r1.x + 9].xyzw
mad r2.xyzw, r0.xxxx, cb0[r1.x + 8].xyzw, r2.xyzw
mad r0.xyzw, r0.zzzz, cb0[r1.x + 10].xyzw, r2.xyzw
add o0.xyzw, r0.xyzw, cb0[r1.x + 11].xyzw
mov o1.xyzw, v0.zyxw
mul o3.xy, v1.xyxx, cb3[1].xxxx
mov o3.zw, l(0,0,0,0)
ret 
// Approximately 15 instruction slots used

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