//Lens flares VS 1.
// ---- Created with 3Dmigoto v1.3.16 on Sat Oct  2 10:51:29 2021

cbuffer vblock : register(b0)
{
  float4 globalvieworigin : packoffset(c0);
  float4 viewmatrixz : packoffset(c1);
  float4 particlevsparm0 : packoffset(c2);
  float4 projectionmatrixz : packoffset(c3);
  float4 mvpmatrixx : packoffset(c4);
  float4 mvpmatrixy : packoffset(c5);
  float4 mvpmatrixw : packoffset(c6);
  float4 mvpmatrixz : packoffset(c7);
  float4 vfxdynamicvpparam0 : packoffset(c8);
  float4 vfxdynamicvpparam1 : packoffset(c9);
  float4 fogdistanceparam : packoffset(c10);
  float4 fogcolorpacked : packoffset(c11);
}



// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
Texture2D<float4> StereoParams : register(t125);
// Depth buffer copied to this input with 3Dmigoto:
// Texture2D<float> DepthBuffer : register(t110);
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/((1-z)*near) + (far*z));
  // return z;
}

float adjust_from_depth_buffer(float x, float y)
{
    float4 stereo = StereoParams.Load(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;

    // 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 * 0; // Z = X offset from center
    distance = separation - offset;         // Total distance to cover (separation - starting X offset)

    old_offset = offset;
    for (i = 0; i < 500; i++) {
        offset += distance / 500.0;

        // Calculate depth for this point on the line:
        w = (separation * convergence*0.2525) / (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(
  float4 v0 : POSITION0,
  float4 v1 : TEXCOORD0,
  float4 v2 : COLOR0,
  float4 v3 : NORMAL0,
  float4 v4 : TEXCOORD2,
  out float4 o0 : COLOR0,
  out float4 o1 : TEXCOORD0,
  out float4 o2 : TEXCOORD5,
  out float4 o3 : SV_Position0)
{
  float4 r0,r1,r2,r3;
  uint4 bitmask, uiDest;
  float4 fDest;
  
  float4 stereo = StereoParams.Load(0);
  float4 iniparams1 = IniParams.Load(int2(1,0));
  
  r0.xyz = -globalvieworigin.xyz + v0.xyz;
  r0.w = v0.w;
  r1.x = dot(r0.xyzw, viewmatrixz.xyzw);
  r1.z = cmp(particlevsparm0.x != 0.000000);
  r1.w = particlevsparm0.x + r1.x;
  r1.w = r1.x * r1.w;
  r1.w = particlevsparm0.x / r1.w;
  r1.w = particlevsparm0.y * r1.w;
  r2.x = cmp(r1.x == 0.000000);
  r1.w = r2.x ? -0 : -r1.w;
  r2.x = projectionmatrixz.z + r1.w;
  r3.x = dot(r0.xyzw, mvpmatrixx.xyzw);
  r3.y = dot(r0.xyzw, mvpmatrixy.xyzw);
  r1.y = 1;
  r2.y = projectionmatrixz.w;
  r3.z = dot(r1.xy, r2.xy);
  r3.w = dot(r0.xyzw, mvpmatrixw.xyzw);
  r2.z = dot(r0.xyzw, mvpmatrixz.xyzw);
  //r3.x+=stereo.x*(r3.w-stereo.y);
  r2.xyw = r3.xyw;
  o3.xyzw = r1.zzzz ? r3.xyzw : r2.xyzw;
  r1.xyz = v4.yyy * v2.xyz;
  r2.xyz = float3(6,6,6) * r1.xyz;
  r0.w = cmp(0 < v4.z);
  r1.xyz = r1.xyz * float3(6,6,6) + float3(9.99999975e-06,9.99999975e-06,9.99999975e-06);
  r1.xyz = max(float3(0,0,0), r1.xyz);
  r1.xyz = log2(r1.xyz);
  r1.xyz = float3(2.20000005,2.20000005,2.20000005) * r1.xyz;
  r1.xyz = exp2(r1.xyz);
  r1.xyz = r0.www ? r1.xyz : r2.xyz;
  r0.w = cmp(0.899999976 < vfxdynamicvpparam0.z);
  if (r0.w != 0) {
    r0.w = max(r1.x, r1.y);
    r0.w = max(r0.w, r1.z);
    r1.w = cmp(0 < vfxdynamicvpparam0.y);
    r2.x = cmp(vfxdynamicvpparam0.y < 1);
    r1.w = r1.w ? r2.x : 0;
    r2.x = cmp(r0.w >= vfxdynamicvpparam0.y);
    r2.y = 1 + -vfxdynamicvpparam0.y;
    r2.y = vfxdynamicvpparam0.x / r2.y;
    r0.w = -vfxdynamicvpparam0.y + r0.w;
    r0.w = r2.y * r0.w;
    r0.w = r2.x ? r0.w : 0;
    r0.w = r1.w ? r0.w : vfxdynamicvpparam0.x;
    r1.xyz = r1.xyz * r0.www;
  }
  r0.w = max(r1.x, r1.y);
  r0.w = max(r0.w, r1.z);
  r1.w = cmp(0 < r0.w);
  r2.xyz = r1.xyz / r0.www;
  o0.xyz = r1.www ? r2.xyz : r1.xyz;
  r1.x = cmp(0.899999976 < vfxdynamicvpparam1.x);
  if (r1.x != 0) {
    r1.x = cmp(fogdistanceparam.w == 1.000000);
    r2.xy = globalvieworigin.xy;
    r2.z = 0;
    r3.xy = v0.xy;
    r3.z = 0;
    r1.yzw = r3.xyz + -r2.xyz;
    r0.xyz = r1.xxx ? r1.yzw : r0.xyz;
    r0.x = dot(r0.xyz, r0.xyz);
    r0.x = sqrt(r0.x);
    r0.x = -fogdistanceparam.x + r0.x;
    r0.x = max(0, r0.x);
    r0.x = min(fogdistanceparam.y, r0.x);
    r0.x = fogdistanceparam.z * r0.x;
    r0.x = exp2(-r0.x);
    r0.x = min(1, r0.x);
    r0.x = 1 + -r0.x;
    r0.x = min(fogcolorpacked.w, r0.x);
    o2.w = max(0, r0.x);
    o2.xyz = fogcolorpacked.xyz;
  } else {
    o2.xyzw = float4(1,1,1,0);
  }
  o0.w = v2.w;
  o1.xy = v1.xy;
  o1.w = v3.w;
  o1.z = r0.w;
  if (o3.w==1) {
    if (iniparams1.w==0) {
	  o3=0;
	} else if (iniparams1.w==1) {
	  o3.x+=adjust_from_depth_buffer(o3.x,o3.y);
	} else if (iniparams1.w==2) {
	  o3.x+=adjust_from_depth_buffer(0,0);
	} else if (iniparams1.w==3) {
	  o3.x+=stereo.x;
	}
  }
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 6.3.9600.16384
//
//   using 3Dmigoto v1.3.16 on Sat Oct  2 10:51:29 2021
//
//
// Buffer Definitions:
//
// cbuffer vblock
// {
//
//   float4 globalvieworigin;           // Offset:    0 Size:    16
//   float4 viewmatrixz;                // Offset:   16 Size:    16
//   float4 particlevsparm0;            // Offset:   32 Size:    16
//   float4 projectionmatrixz;          // Offset:   48 Size:    16
//   float4 mvpmatrixx;                 // Offset:   64 Size:    16
//   float4 mvpmatrixy;                 // Offset:   80 Size:    16
//   float4 mvpmatrixw;                 // Offset:   96 Size:    16
//   float4 mvpmatrixz;                 // Offset:  112 Size:    16
//   float4 vfxdynamicvpparam0;         // Offset:  128 Size:    16
//   float4 vfxdynamicvpparam1;         // Offset:  144 Size:    16
//   float4 fogdistanceparam;           // Offset:  160 Size:    16
//   float4 fogcolorpacked;             // Offset:  176 Size:    16
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// vblock                            cbuffer      NA          NA    0        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float   xyzw
// TEXCOORD                 0   xyzw        1     NONE   float   xy
// COLOR                    0   xyzw        2     NONE   float   xyzw
// NORMAL                   0   xyzw        3     NONE   float      w
// TEXCOORD                 2   xyzw        4     NONE   float    yz
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// COLOR                    0   xyzw        0     NONE   float   xyzw
// TEXCOORD                 0   xyzw        1     NONE   float   xyzw
// TEXCOORD                 5   xyzw        2     NONE   float   xyzw
// SV_Position              0   xyzw        3      POS   float   xyzw
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[12], immediateIndexed
dcl_input v0.xyzw
dcl_input v1.xy
dcl_input v2.xyzw
dcl_input v3.w
dcl_input v4.yz
dcl_output o0.xyzw
dcl_output o1.xyzw
dcl_output o2.xyzw
dcl_output_siv o3.xyzw, position
dcl_temps 4
add r0.xyz, v0.xyzx, -cb0[0].xyzx
mov r0.w, v0.w
dp4 r1.x, r0.xyzw, cb0[1].xyzw
ne r1.z, cb0[2].x, l(0.000000)
add r1.w, r1.x, cb0[2].x
mul r1.w, r1.w, r1.x
div r1.w, cb0[2].x, r1.w
mul r1.w, r1.w, cb0[2].y
eq r2.x, r1.x, l(0.000000)
movc r1.w, r2.x, l(-0.000000), -r1.w
add r2.x, r1.w, cb0[3].z
dp4 r3.x, r0.xyzw, cb0[4].xyzw
dp4 r3.y, r0.xyzw, cb0[5].xyzw
mov r1.y, l(1.000000)
mov r2.y, cb0[3].w
dp2 r3.z, r1.xyxx, r2.xyxx
dp4 r3.w, r0.xyzw, cb0[6].xyzw
dp4 r2.z, r0.xyzw, cb0[7].xyzw
mov r2.xyw, r3.xyxw
movc o3.xyzw, r1.zzzz, r3.xyzw, r2.xyzw
mul r1.xyz, v2.xyzx, v4.yyyy
mul r2.xyz, r1.xyzx, l(6.000000, 6.000000, 6.000000, 0.000000)
lt r0.w, l(0.000000), v4.z
mad r1.xyz, r1.xyzx, l(6.000000, 6.000000, 6.000000, 0.000000), l(0.000010, 0.000010, 0.000010, 0.000000)
max r1.xyz, r1.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
log r1.xyz, r1.xyzx
mul r1.xyz, r1.xyzx, l(2.200000, 2.200000, 2.200000, 0.000000)
exp r1.xyz, r1.xyzx
movc r1.xyz, r0.wwww, r1.xyzx, r2.xyzx
lt r0.w, l(0.900000), cb0[8].z
if_nz r0.w
  max r0.w, r1.y, r1.x
  max r0.w, r1.z, r0.w
  lt r1.w, l(0.000000), cb0[8].y
  lt r2.x, cb0[8].y, l(1.000000)
  and r1.w, r1.w, r2.x
  ge r2.x, r0.w, cb0[8].y
  add r2.y, -cb0[8].y, l(1.000000)
  div r2.y, cb0[8].x, r2.y
  add r0.w, r0.w, -cb0[8].y
  mul r0.w, r0.w, r2.y
  and r0.w, r0.w, r2.x
  movc r0.w, r1.w, r0.w, cb0[8].x
  mul r1.xyz, r0.wwww, r1.xyzx
endif
max r0.w, r1.y, r1.x
max r0.w, r1.z, r0.w
lt r1.w, l(0.000000), r0.w
div r2.xyz, r1.xyzx, r0.wwww
movc o0.xyz, r1.wwww, r2.xyzx, r1.xyzx
lt r1.x, l(0.900000), cb0[9].x
if_nz r1.x
  eq r1.x, cb0[10].w, l(1.000000)
  mov r2.xy, cb0[0].xyxx
  mov r2.z, l(0)
  mov r3.xy, v0.xyxx
  mov r3.z, l(0)
  add r1.yzw, -r2.xxyz, r3.xxyz
  movc r0.xyz, r1.xxxx, r1.yzwy, r0.xyzx
  dp3 r0.x, r0.xyzx, r0.xyzx
  sqrt r0.x, r0.x
  add r0.x, r0.x, -cb0[10].x
  max r0.x, r0.x, l(0.000000)
  min r0.x, r0.x, cb0[10].y
  mul r0.x, r0.x, cb0[10].z
  exp r0.x, -r0.x
  min r0.x, r0.x, l(1.000000)
  add r0.x, -r0.x, l(1.000000)
  min r0.x, r0.x, cb0[11].w
  max o2.w, r0.x, l(0.000000)
  mov o2.xyz, cb0[11].xyzx
else
  mov o2.xyzw, l(1.000000,1.000000,1.000000,0)
endif
mov o0.w, v2.w
mov o1.xy, v1.xyxx
mov o1.w, v3.w
mov o1.z, r0.w
ret
// Approximately 79 instruction slots used

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