// Pre-fix 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);
}

SamplerState g_xPointClamp_s : register(s1);
SamplerState g_xTrilinearClamp_s : register(s3);
SamplerComparisonState g_xShadowCmpSampler_s : register(s8);
Texture2D<float4> g_xMaterialsTexture : register(t0);
Texture2D<float4> g_xNormalBuffer : register(t24);
Texture2D<float4> g_xAlbedoBuffer : register(t25);
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,r3,r4,r5,r6,r7;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xy = v0.xy * g_xTwoOverScreenDims.xy + float2(-1,-1);
  r1.xy = (int2)v0.xy;
  r1.zw = float2(0,0);
  r2.xyzw = g_xDepthStencil.Load(r1.xyz).xyzw;
  r0.z = g_xDepthProjViewUnpack.y + -r2.x;
  r0.z = g_xDepthProjViewUnpack.x / r0.z;
  r2.xy = g_xCameraTanHalfFOV.xy * r0.zz;
  r0.xy = r2.xy * r0.xy;
float4 stereo = StereoParams.Load(0);
float4 params = IniParams.Load(0);
r0.x -= stereo.x * (r0.z - stereo.y)*g_xInvProj._m00;
  r2.xyzw = g_xInvView._m01_m11_m21_m31 * r0.yyyy;
  r2.xyzw = g_xInvView._m00_m10_m20_m30 * r0.xxxx + r2.xyzw;
  r0.xyzw = g_xInvView._m02_m12_m22_m32 * r0.zzzz + r2.xyzw;
  r0.xyzw = g_xInvView._m03_m13_m23_m33 + r0.xyzw;
  r2.xyz = g_xCamPos.xyz + -r0.xyz;
  r1.z = dot(r2.xyz, r2.xyz);
  r1.z = rsqrt(r1.z);
  r3.xyz = r2.xyz * r1.zzz;
  r2.xyz = r2.xyz * r1.zzz + g_xLightDir.xyz;
  r4.xyzw = g_xNormalBuffer.Load(r1.xyw).xyzw;
  r1.xyzw = g_xAlbedoBuffer.Load(r1.xyw).xyzw;
  r2.w = dot(r4.xy, r4.xy);
  r3.w = rsqrt(r2.w);
  r5.z = r2.w * 2 + -1;
  r4.xy = r4.xy * r3.ww;
  r2.w = -r5.z * r5.z + 1;
  r2.w = sqrt(r2.w);
  r5.xy = r4.xy * r2.ww;
  r2.w = saturate(dot(r5.xyz, r3.xyz));
  r2.w = 1 + -r2.w;
  r3.x = r2.w * r2.w;
  r3.x = r3.x * r3.x;
  r2.w = r3.x * r2.w;
  r3.z = r1.w;
  r3.yw = float2(0.994140625,0.998046875);
  r6.xyzw = g_xMaterialsTexture.Sample(g_xTrilinearClamp_s, r3.wz).xyzw;
  r7.xyzw = g_xMaterialsTexture.Sample(g_xPointClamp_s, r3.yz).xyzw;
  r1.w = r7.w + r7.w;
  r3.y = r6.w + r6.w;
  r3.w = 256 * r6.x;
  r3.w = r3.w * r3.w;
  r2.w = r3.y * r2.w;
  r3.y = dot(r5.xyz, g_xLightDir.xyz);
  r3.y = min(1, r3.y);
  r4.x = 0.5 + r3.y;
  r4.x = max(0, r4.x);
  r2.w = r4.x * r2.w;
  r4.x = dot(r2.xyz, r2.xyz);
  r4.x = rsqrt(r4.x);
  r2.xyz = r4.xxx * r2.xyz;
  r2.x = dot(r5.xyz, r2.xyz);
  r2.x = max(9.99999997e-007, r2.x);
  r2.x = log2(r2.x);
  r2.x = r3.w * r2.x;
  r2.x = exp2(r2.x);
  r2.x = r3.w * r2.x;
  r2.xw = float2(0.03125,2.5) * r2.xw;
  r2.y = max(0, r3.y);
  r3.x = r3.y * 0.490234375 + 0.4921875;
  r3.xyzw = g_xMaterialsTexture.Sample(g_xTrilinearClamp_s, r3.xz).xyzw;
  r2.z = r3.x + r3.x;
  r3.xyz = g_xLightColour.xyz * r2.zzz;
  r2.x = r2.x * r2.y + r2.w;
  r2.x = r2.x * r4.z;
  r2.yzw = r1.xyz * g_xLightColour.www + -g_xLightColour.xyz;
  r2.yzw = r1.www * r2.yzw + g_xLightColour.xyz;
  r2.xyz = r2.yzw * r2.xxx;
  o0.xyz = r3.xyz * r1.xyz + r2.xyz;
  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.xy = g_axCSMBlurKernel[1].xy * g_afCSMBlurKernelWeights.xx + r1.xz;
  r1.x = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r1.xy, r0.z).x;
  r1.y = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r2.xy, r0.z).x;
  r1.z = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r2.zw, 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.x = r0.x ? g_fCSMStrength : 0;
  r0.y = r1.y + r0.w;
  r0.y = r0.y + r1.z;
  r0.y = r0.y + r1.x;
  r0.x = r0.y * r0.x;
  o0.w = -r0.x * 0.25 + 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
//   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
//   float4 g_xLightColour;             // Offset:   16 Size:    16
//   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 [unused]
//
// }
//
// 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 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// g_xPointClamp                     sampler      NA          NA    1        1
// g_xTrilinearClamp                 sampler      NA          NA    3        1
// g_xShadowCmpSampler             sampler_c      NA          NA    8        1
// g_xMaterialsTexture               texture  float4          2d    0        1
// g_xNormalBuffer                   texture  float4          2d   24        1
// g_xAlbedoBuffer                   texture  float4          2d   25        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 s1, mode_default
dcl_sampler s3, mode_default
dcl_sampler s8, mode_comparison
dcl_resource_texture2d (float,float,float,float) t0
dcl_resource_texture2d (float,float,float,float) t24
dcl_resource_texture2d (float,float,float,float) t25
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 8
mad r0.xy, v0.xyxx, cb4[4].zwzz, l(-1.000000, -1.000000, 0.000000, 0.000000)
ftoi r1.xy, v0.xyxx
mov r1.zw, l(0,0,0,0)
ld r2.xyzw, r1.xyzw, t28.xyzw
add r0.z, -r2.x, cb0[25].w
div r0.z, cb0[25].z, r0.z
mul r2.xy, r0.zzzz, cb4[4].xyxx
mul r0.xy, r0.xyxx, r2.xyxx
mul r2.xyzw, r0.yyyy, cb0[13].xyzw
mad r2.xyzw, cb0[12].xyzw, r0.xxxx, r2.xyzw
mad r0.xyzw, cb0[14].xyzw, r0.zzzz, r2.xyzw
add r0.xyzw, r0.xyzw, cb0[15].xyzw
add r2.xyz, -r0.xyzx, cb0[24].xyzx
dp3 r1.z, r2.xyzx, r2.xyzx
rsq r1.z, r1.z
mul r3.xyz, r1.zzzz, r2.xyzx
mad r2.xyz, r2.xyzx, r1.zzzz, cb4[0].xyzx
ld r4.xyzw, r1.xyww, t24.xyzw
ld r1.xyzw, r1.xyww, t25.xyzw
dp2 r2.w, r4.xyxx, r4.xyxx
rsq r3.w, r2.w
mad r5.z, r2.w, l(2.000000), l(-1.000000)
mul r4.xy, r3.wwww, r4.xyxx
mad r2.w, -r5.z, r5.z, l(1.000000)
sqrt r2.w, r2.w
mul r5.xy, r2.wwww, r4.xyxx
dp3_sat r2.w, r5.xyzx, r3.xyzx
add r2.w, -r2.w, l(1.000000)
mul r3.x, r2.w, r2.w
mul r3.x, r3.x, r3.x
mul r2.w, r2.w, r3.x
mov r3.z, r1.w
mov r3.yw, l(0,0.994141,0,0.998047)
sample r6.xyzw, r3.wzww, t0.xyzw, s3
sample r7.xyzw, r3.yzyy, t0.xyzw, s1
add r1.w, r7.w, r7.w
add r3.y, r6.w, r6.w
mul r3.w, r6.x, l(256.000000)
mul r3.w, r3.w, r3.w
mul r2.w, r2.w, r3.y
dp3 r3.y, r5.xyzx, cb4[0].xyzx
min r3.y, r3.y, l(1.000000)
add r4.x, r3.y, l(0.500000)
max r4.x, r4.x, l(0.000000)
mul r2.w, r2.w, r4.x
dp3 r4.x, r2.xyzx, r2.xyzx
rsq r4.x, r4.x
mul r2.xyz, r2.xyzx, r4.xxxx
dp3 r2.x, r5.xyzx, r2.xyzx
max r2.x, r2.x, l(0.000001)
log r2.x, r2.x
mul r2.x, r2.x, r3.w
exp r2.x, r2.x
mul r2.x, r2.x, r3.w
mul r2.xw, r2.xxxw, l(0.031250, 0.000000, 0.000000, 2.500000)
max r2.y, r3.y, l(0.000000)
mad r3.x, r3.y, l(0.490234), l(0.492188)
sample r3.xyzw, r3.xzxx, t0.xyzw, s3
add r2.z, r3.x, r3.x
mul r3.xyz, r2.zzzz, cb4[1].xyzx
mad r2.x, r2.x, r2.y, r2.w
mul r2.x, r4.z, r2.x
mad r2.yzw, r1.xxyz, cb4[1].wwww, -cb4[1].xxyz
mad r2.yzw, r1.wwww, r2.yyzw, cb4[1].xxyz
mul r2.xyz, r2.xxxx, r2.yzwy
mad o0.xyz, r3.xyzx, r1.xyzx, r2.xyzx
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
mad r1.xy, cb4[3].xyxx, cb5[8].xxxx, r1.xzxx
sample_c r1.x, r1.xyxx, t26.xxxx, s8, r0.z
sample_c r1.y, r2.xyxx, t26.xxxx, s8, r0.z
sample_c r1.z, r2.zwzz, 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)
and r0.x, r0.x, cb4[5].x
add r0.y, r0.w, r1.y
add r0.y, r1.z, r0.y
add r0.y, r1.x, r0.y
mul r0.x, r0.x, r0.y
mad o0.w, -r0.x, l(0.250000), l(1.000000)
ret 
// Approximately 90 instruction slots used

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