// PreFix Shadows

cbuffer FrameConstants : register(b0)
{
  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 SunlightParams : register(b4)
{
  float4 g_xLightDir : packoffset(c0);
  float4 g_xLightColour : packoffset(c1);
  float4 g_axCSMBlurKernel[2] : packoffset(c2);
  float2 g_xCameraTanHalfFOV : packoffset(c4);
  float2 g_xTwoOverScreenDims : packoffset(c4.z);
  float g_fCSMStrength : packoffset(c5);
  float g_fCSMSize : packoffset(c5.y);
}

cbuffer SunlightCascadeParamsPS : register(b5)
{
  row_major float4x4 g_axShadowViewProj[2] : packoffset(c0);
  float2 g_afCSMBlurKernelWeights : packoffset(c8);
  float g_fBlendArea : packoffset(c8.z);
  float g_fMapIndex : packoffset(c8.w);
}

SamplerComparisonState g_xShadowCmpSampler_s : register(s8);
Texture2D<float4> g_xCSM : register(t26);
Texture2D<float4> g_xDepthStencil : register(t28);


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


void main( 
  float4 v0 : SV_Position0,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xy = (int2)v0.xy;
  r0.zw = float2(0,0);
  r0.xyzw = g_xDepthStencil.Load(r0.xyz).xyzw;
  r0.x = g_xDepthProjViewUnpack.y + -r0.x;
  r0.x = g_xDepthProjViewUnpack.x / r0.x;
  r0.yz = g_xCameraTanHalfFOV.xy * r0.xx;
  r1.xy = v0.xy * g_xTwoOverScreenDims.xy + float2(-1,-1);
  r0.yz = r1.xy * r0.yz;
float4 stereo = StereoParams.Load(0);
float4 params = IniParams.Load(0);
r0.y -= stereo.x * (r0.x - stereo.y)*g_xInvProj._m00;
  r1.xyzw = g_xInvView._m01_m11_m21_m31 * r0.zzzz;
  r1.xyzw = g_xInvView._m00_m10_m20_m30 * r0.yyyy + r1.xyzw;
  r0.xyzw = g_xInvView._m02_m12_m22_m32 * r0.xxxx + r1.xyzw;
  r0.xyzw = g_xInvView._m03_m13_m23_m33 + r0.xyzw;
  r1.xyz = g_axShadowViewProj[0]._m10_m11_m12 * r0.yyy;
  r1.xyz = r0.xxx * g_axShadowViewProj[0]._m00_m01_m02 + r1.xyz;
  r0.xyz = r0.zzz * g_axShadowViewProj[0]._m20_m21_m22 + r1.xyz;
  r0.xyz = r0.www * g_axShadowViewProj[0]._m30_m31_m32 + r0.xyz;
  r1.xy = r0.xy * float2(0.5,0.5) + float2(0.5,0.5);
  r1.z = 1 + -r1.y;
  r0.w = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r1.xz, r0.z).x;
  r2.xyzw = g_axCSMBlurKernel[0].xyzw * g_afCSMBlurKernelWeights.xxxx + r1.xzxz;
  r1.y = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r2.xy, r0.z).x;
  r1.w = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r2.zw, r0.z).x;
  r0.w = r1.y + r0.w;
  r0.w = r0.w + r1.w;
  r1.yw = g_axCSMBlurKernel[1].xy * g_afCSMBlurKernelWeights.xx + r1.xz;
  r1.xz = r1.xz * g_fCSMSize + float2(0.5,0.5);
  r1.xz = float2(0.5,0.5) * r1.xz;
  r1.xz = frac(r1.xz);
  r1.xz = cmp(float2(0.5,0.5) < r1.xz);
  r1.y = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r1.yw, r0.z).x;
  r0.xyz = r0.xyz * float3(0.5,0.5,1) + float3(0,0,-0.5);
  r0.xyz = cmp(float3(0.5,0.5,0.5) >= abs(r0.xyz));
  r0.xyz = r0.xyz ? float3(1,1,1) : 0;
  r0.x = dot(r0.xyz, float3(1,1,1));
  r0.x = cmp(r0.x >= 3);
  r0.y = r1.y + r0.w;
  r0.z = r0.x ? g_fCSMStrength : 0;
  r0.y = r0.y * r0.z;
  r0.y = -r0.y * 0.25 + 1;
  r0.z = g_fMapIndex * 0.100000001 + 0.5;
  r0.w = g_fMapIndex * 0.100000001 + -r0.z;
  r2.z = r0.y * r0.w + r0.z;
  r0.zw = float2(1,0);
  r2.xw = r0.yy * r0.zw + float2(0,1);
  r0.y = r1.x ? 1.000000 : 0;
  r0.z = r1.z ? -1 : -0;
  r0.y = r0.y + r0.z;
  r2.y = abs(r0.y) * 0.100000001 + 0.200000003;
  o0.xyzw = r0.xxxx ? r2.xyzw : float4(1,1,1,1);
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//
// Buffer Definitions: 
//
// cbuffer FrameConstants
// {
//
//   float4x4 g_xView;                  // Offset:    0 Size:    64 [unused]
//   float4x4 g_xProj;                  // Offset:   64 Size:    64 [unused]
//   float4x4 g_xViewProj;              // Offset:  128 Size:    64 [unused]
//   float4x4 g_xInvView;               // Offset:  192 Size:    64
//   float4x4 g_xInvProj;               // Offset:  256 Size:    64 [unused]
//   float4x4 g_xInvViewProj;           // Offset:  320 Size:    64 [unused]
//   float3 g_xCamPos;                  // Offset:  384 Size:    12 [unused]
//   float2 g_xRcpScreenDims;           // Offset:  400 Size:     8 [unused]
//   float2 g_xDepthProjViewUnpack;     // Offset:  408 Size:     8
//   float4 g_axViewFrustumPlanes[6];   // Offset:  416 Size:    96 [unused]
//
// }
//
// cbuffer SunlightParams
// {
//
//   float4 g_xLightDir;                // Offset:    0 Size:    16 [unused]
//   float4 g_xLightColour;             // Offset:   16 Size:    16 [unused]
//   float4 g_axCSMBlurKernel[2];       // Offset:   32 Size:    32
//   float2 g_xCameraTanHalfFOV;        // Offset:   64 Size:     8
//   float2 g_xTwoOverScreenDims;       // Offset:   72 Size:     8
//   float g_fCSMStrength;              // Offset:   80 Size:     4
//   float g_fCSMSize;                  // Offset:   84 Size:     4
//
// }
//
// cbuffer SunlightCascadeParamsPS
// {
//
//   row_major float4x4 g_axShadowViewProj[2];// Offset:    0 Size:   128
//   float2 g_afCSMBlurKernelWeights;   // Offset:  128 Size:     8
//   float g_fBlendArea;                // Offset:  136 Size:     4 [unused]
//   float g_fMapIndex;                 // Offset:  140 Size:     4
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// g_xShadowCmpSampler             sampler_c      NA          NA    8        1
// g_xCSM                            texture  float4          2d   26        1
// g_xDepthStencil                   texture  float4          2d   28        1
// FrameConstants                    cbuffer      NA          NA    0        1
// SunlightParams                    cbuffer      NA          NA    4        1
// SunlightCascadeParamsPS           cbuffer      NA          NA    5        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float   xy  
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
//
ps_4_0
dcl_constantbuffer cb0[26], immediateIndexed
dcl_constantbuffer cb4[6], immediateIndexed
dcl_constantbuffer cb5[9], immediateIndexed
dcl_sampler s8, mode_comparison
dcl_resource_texture2d (float,float,float,float) t26
dcl_resource_texture2d (float,float,float,float) t28
dcl_input_ps_siv linear noperspective v0.xy, position
dcl_output o0.xyzw
dcl_temps 3
ftoi r0.xy, v0.xyxx
mov r0.zw, l(0,0,0,0)
ld r0.xyzw, r0.xyzw, t28.xyzw
add r0.x, -r0.x, cb0[25].w
div r0.x, cb0[25].z, r0.x
mul r0.yz, r0.xxxx, cb4[4].xxyx
mad r1.xy, v0.xyxx, cb4[4].zwzz, l(-1.000000, -1.000000, 0.000000, 0.000000)
mul r0.yz, r0.yyzy, r1.xxyx
mul r1.xyzw, r0.zzzz, cb0[13].xyzw
mad r1.xyzw, cb0[12].xyzw, r0.yyyy, r1.xyzw
mad r0.xyzw, cb0[14].xyzw, r0.xxxx, r1.xyzw
add r0.xyzw, r0.xyzw, cb0[15].xyzw
mul r1.xyz, r0.yyyy, cb5[1].xyzx
mad r1.xyz, r0.xxxx, cb5[0].xyzx, r1.xyzx
mad r0.xyz, r0.zzzz, cb5[2].xyzx, r1.xyzx
mad r0.xyz, r0.wwww, cb5[3].xyzx, r0.xyzx
mad r1.xy, r0.xyxx, l(0.500000, 0.500000, 0.000000, 0.000000), l(0.500000, 0.500000, 0.000000, 0.000000)
add r1.z, -r1.y, l(1.000000)
sample_c r0.w, r1.xzxx, t26.xxxx, s8, r0.z
mad r2.xyzw, cb4[2].xyzw, cb5[8].xxxx, r1.xzxz
sample_c r1.y, r2.xyxx, t26.xxxx, s8, r0.z
sample_c r1.w, r2.zwzz, t26.xxxx, s8, r0.z
add r0.w, r0.w, r1.y
add r0.w, r1.w, r0.w
mad r1.yw, cb4[3].xxxy, cb5[8].xxxx, r1.xxxz
mad r1.xz, r1.xxzx, cb4[5].yyyy, l(0.500000, 0.000000, 0.500000, 0.000000)
mul r1.xz, r1.xxzx, l(0.500000, 0.000000, 0.500000, 0.000000)
frc r1.xz, r1.xxzx
lt r1.xz, l(0.500000, 0.000000, 0.500000, 0.000000), r1.xxzx
sample_c r1.y, r1.ywyy, t26.xxxx, s8, r0.z
mad r0.xyz, r0.xyzx, l(0.500000, 0.500000, 1.000000, 0.000000), l(0.000000, 0.000000, -0.500000, 0.000000)
ge r0.xyz, l(0.500000, 0.500000, 0.500000, 0.000000), |r0.xyzx|
and r0.xyz, r0.xyzx, l(0x3f800000, 0x3f800000, 0x3f800000, 0)
dp3 r0.x, r0.xyzx, l(1.000000, 1.000000, 1.000000, 0.000000)
ge r0.x, r0.x, l(3.000000)
add r0.y, r0.w, r1.y
and r0.z, r0.x, cb4[5].x
mul r0.y, r0.z, r0.y
mad r0.y, -r0.y, l(0.250000), l(1.000000)
mad r0.z, cb5[8].w, l(0.100000), l(0.500000)
mad r0.w, cb5[8].w, l(0.100000), -r0.z
mad r2.z, r0.y, r0.w, r0.z
mov r0.zw, l(0,0,1.000000,0)
mad r2.xw, r0.yyyy, r0.zzzw, l(0.000000, 0.000000, 0.000000, 1.000000)
and r0.y, r1.x, l(0x3f800000)
movc r0.z, r1.z, l(-1.000000), l(-0.000000)
add r0.y, r0.z, r0.y
mad r2.y, |r0.y|, l(0.100000), l(0.200000)
movc o0.xyzw, r0.xxxx, r2.xyzw, l(1.000000,1.000000,1.000000,1.000000)
ret 
// Approximately 50 instruction slots used

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