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

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_xNextCSM : register(t27);
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;
  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_axShadowViewProj[1]._m10_m11_m12 * r0.yyy;
  r2.xyz = r0.xxx * g_axShadowViewProj[1]._m00_m01_m02 + r2.xyz;
  r2.xyz = r0.zzz * g_axShadowViewProj[1]._m20_m21_m22 + r2.xyz;
  r2.xyz = r0.www * g_axShadowViewProj[1]._m30_m31_m32 + r2.xyz;
  r3.xy = r2.xy * float2(0.5,0.5) + float2(0.5,0.5);
  r3.z = 1 + -r3.y;
  r1.z = g_xNextCSM.SampleCmp(g_xShadowCmpSampler_s, r3.xz, r2.z).x;
  r4.xyzw = g_axCSMBlurKernel[0].xyzw * g_afCSMBlurKernelWeights.yyyy + r3.xzxz;
  r2.xy = g_axCSMBlurKernel[1].xy * g_afCSMBlurKernelWeights.yy + r3.xz;
  r2.x = g_xNextCSM.SampleCmp(g_xShadowCmpSampler_s, r2.xy, r2.z).x;
  r2.y = g_xNextCSM.SampleCmp(g_xShadowCmpSampler_s, r4.xy, r2.z).x;
  r2.z = g_xNextCSM.SampleCmp(g_xShadowCmpSampler_s, r4.zw, r2.z).x;
  r1.z = r2.y + r1.z;
  r1.z = r1.z + r2.z;
  r1.z = r1.z + r2.x;
  r2.xyz = g_axShadowViewProj[0]._m10_m11_m12 * r0.yyy;
  r2.xyz = r0.xxx * g_axShadowViewProj[0]._m00_m01_m02 + r2.xyz;
  r2.xyz = r0.zzz * g_axShadowViewProj[0]._m20_m21_m22 + r2.xyz;
  r2.xyz = r0.www * g_axShadowViewProj[0]._m30_m31_m32 + r2.xyz;
  r0.xyz = g_xCamPos.xyz + -r0.xyz;
  r3.yz = r2.xy * float2(0.5,0.5) + float2(0.5,0.5);
  r3.xw = float2(1,1) + -r3.zz;
  r0.w = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r3.yw, r2.z).x;
  r4.xyzw = g_axCSMBlurKernel[0].xyzw * g_afCSMBlurKernelWeights.xxxx + r3.ywyw;
  r2.x = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r4.xy, r2.z).x;
  r2.y = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r4.zw, r2.z).x;
  r0.w = r2.x + r0.w;
  r0.w = r0.w + r2.y;
  r2.xy = g_axCSMBlurKernel[1].xy * g_afCSMBlurKernelWeights.xx + r3.yw;
  r2.x = g_xCSM.SampleCmp(g_xShadowCmpSampler_s, r2.xy, r2.z).x;
  r0.w = r2.x + r0.w;
  r0.w = 0.25 * r0.w;
  r1.z = r1.z * 0.25 + -r0.w;
  r2.xy = float2(1,1) + -r3.yw;
  r2.z = min(r3.y, r3.x);
  r2.x = min(r2.x, r2.y);
  r2.x = min(r2.z, r2.x);
  r2.x = r2.x / g_fBlendArea;
  r2.x = 1 + -r2.x;
  r0.w = r2.x * r1.z + r0.w;
  o0.w = -r0.w * g_fCSMStrength + 1;
  r0.w = dot(r0.xyz, r0.xyz);
  r0.w = rsqrt(r0.w);
  r2.xyz = r0.xyz * r0.www;
  r0.xyz = r0.xyz * r0.www + g_xLightDir.xyz;
  r3.xyzw = g_xNormalBuffer.Load(r1.xyw).xyzw;
  r1.xyzw = g_xAlbedoBuffer.Load(r1.xyw).xyzw;
  r0.w = dot(r3.xy, r3.xy);
  r2.w = rsqrt(r0.w);
  r4.z = r0.w * 2 + -1;
  r3.xy = r3.xy * r2.ww;
  r0.w = -r4.z * r4.z + 1;
  r0.w = sqrt(r0.w);
  r4.xy = r3.xy * r0.ww;
  r0.w = saturate(dot(r4.xyz, r2.xyz));
  r0.w = 1 + -r0.w;
  r2.x = r0.w * r0.w;
  r2.x = r2.x * r2.x;
  r0.w = r2.x * r0.w;
  r2.z = r1.w;
  r2.yw = float2(0.994140625,0.998046875);
  r5.xyzw = g_xMaterialsTexture.Sample(g_xTrilinearClamp_s, r2.wz).xyzw;
  r6.xyzw = g_xMaterialsTexture.Sample(g_xPointClamp_s, r2.yz).xyzw;
  r1.w = r6.w + r6.w;
  r2.y = r5.w + r5.w;
  r2.w = 256 * r5.x;
  r2.w = r2.w * r2.w;
  r0.w = r2.y * r0.w;
  r2.y = dot(r4.xyz, g_xLightDir.xyz);
  r2.y = min(1, r2.y);
  r3.x = 0.5 + r2.y;
  r3.x = max(0, r3.x);
  r0.w = r3.x * r0.w;
  r3.x = dot(r0.xyz, r0.xyz);
  r3.x = rsqrt(r3.x);
  r0.xyz = r3.xxx * r0.xyz;
  r0.x = dot(r4.xyz, r0.xyz);
  r0.x = max(9.99999997e-007, r0.x);
  r0.x = log2(r0.x);
  r0.x = r2.w * r0.x;
  r0.x = exp2(r0.x);
  r0.x = r2.w * r0.x;
  r0.xw = float2(0.03125,2.5) * r0.xw;
  r0.y = max(0, r2.y);
  r2.x = r2.y * 0.490234375 + 0.4921875;
  r2.xyzw = g_xMaterialsTexture.Sample(g_xTrilinearClamp_s, r2.xz).xyzw;
  r0.z = r2.x + r2.x;
  r2.xyz = g_xLightColour.xyz * r0.zzz;
  r0.x = r0.x * r0.y + r0.w;
  r0.x = r0.x * r3.z;
  r0.yzw = r1.xyz * g_xLightColour.www + -g_xLightColour.xyz;
  r0.yzw = r1.www * r0.yzw + g_xLightColour.xyz;
  r0.xyz = r0.yzw * r0.xxx;
  o0.xyz = r2.xyz * r1.xyz + r0.xyz;
  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
//   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_xNextCSM                        texture  float4          2d   27        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) t27
dcl_resource_texture2d (float,float,float,float) t28
dcl_input_ps_siv linear noperspective v0.xy, position
dcl_output o0.xyzw
dcl_temps 7
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
mul r2.xyz, r0.yyyy, cb5[5].xyzx
mad r2.xyz, r0.xxxx, cb5[4].xyzx, r2.xyzx
mad r2.xyz, r0.zzzz, cb5[6].xyzx, r2.xyzx
mad r2.xyz, r0.wwww, cb5[7].xyzx, r2.xyzx
mad r3.xy, r2.xyxx, l(0.500000, 0.500000, 0.000000, 0.000000), l(0.500000, 0.500000, 0.000000, 0.000000)
add r3.z, -r3.y, l(1.000000)
sample_c r1.z, r3.xzxx, t27.xxxx, s8, r2.z
mad r4.xyzw, cb4[2].xyzw, cb5[8].yyyy, r3.xzxz
mad r2.xy, cb4[3].xyxx, cb5[8].yyyy, r3.xzxx
sample_c r2.x, r2.xyxx, t27.xxxx, s8, r2.z
sample_c r2.y, r4.xyxx, t27.xxxx, s8, r2.z
sample_c r2.z, r4.zwzz, t27.xxxx, s8, r2.z
add r1.z, r1.z, r2.y
add r1.z, r2.z, r1.z
add r1.z, r2.x, r1.z
mul r2.xyz, r0.yyyy, cb5[1].xyzx
mad r2.xyz, r0.xxxx, cb5[0].xyzx, r2.xyzx
mad r2.xyz, r0.zzzz, cb5[2].xyzx, r2.xyzx
mad r2.xyz, r0.wwww, cb5[3].xyzx, r2.xyzx
add r0.xyz, -r0.xyzx, cb0[24].xyzx
mad r3.yz, r2.xxyx, l(0.000000, 0.500000, 0.500000, 0.000000), l(0.000000, 0.500000, 0.500000, 0.000000)
add r3.xw, -r3.zzzz, l(1.000000, 0.000000, 0.000000, 1.000000)
sample_c r0.w, r3.ywyy, t26.xxxx, s8, r2.z
mad r4.xyzw, cb4[2].xyzw, cb5[8].xxxx, r3.ywyw
sample_c r2.x, r4.xyxx, t26.xxxx, s8, r2.z
sample_c r2.y, r4.zwzz, t26.xxxx, s8, r2.z
add r0.w, r0.w, r2.x
add r0.w, r2.y, r0.w
mad r2.xy, cb4[3].xyxx, cb5[8].xxxx, r3.ywyy
sample_c r2.x, r2.xyxx, t26.xxxx, s8, r2.z
add r0.w, r0.w, r2.x
mul r0.w, r0.w, l(0.250000)
mad r1.z, r1.z, l(0.250000), -r0.w
add r2.xy, -r3.ywyy, l(1.000000, 1.000000, 0.000000, 0.000000)
min r2.z, r3.x, r3.y
min r2.x, r2.y, r2.x
min r2.x, r2.x, r2.z
div r2.x, r2.x, cb5[8].z
add r2.x, -r2.x, l(1.000000)
mad r0.w, r2.x, r1.z, r0.w
mad o0.w, -r0.w, cb4[5].x, l(1.000000)
dp3 r0.w, r0.xyzx, r0.xyzx
rsq r0.w, r0.w
mul r2.xyz, r0.wwww, r0.xyzx
mad r0.xyz, r0.xyzx, r0.wwww, cb4[0].xyzx
ld r3.xyzw, r1.xyww, t24.xyzw
ld r1.xyzw, r1.xyww, t25.xyzw
dp2 r0.w, r3.xyxx, r3.xyxx
rsq r2.w, r0.w
mad r4.z, r0.w, l(2.000000), l(-1.000000)
mul r3.xy, r2.wwww, r3.xyxx
mad r0.w, -r4.z, r4.z, l(1.000000)
sqrt r0.w, r0.w
mul r4.xy, r0.wwww, r3.xyxx
dp3_sat r0.w, r4.xyzx, r2.xyzx
add r0.w, -r0.w, l(1.000000)
mul r2.x, r0.w, r0.w
mul r2.x, r2.x, r2.x
mul r0.w, r0.w, r2.x
mov r2.z, r1.w
mov r2.yw, l(0,0.994141,0,0.998047)
sample r5.xyzw, r2.wzww, t0.xyzw, s3
sample r6.xyzw, r2.yzyy, t0.xyzw, s1
add r1.w, r6.w, r6.w
add r2.y, r5.w, r5.w
mul r2.w, r5.x, l(256.000000)
mul r2.w, r2.w, r2.w
mul r0.w, r0.w, r2.y
dp3 r2.y, r4.xyzx, cb4[0].xyzx
min r2.y, r2.y, l(1.000000)
add r3.x, r2.y, l(0.500000)
max r3.x, r3.x, l(0.000000)
mul r0.w, r0.w, r3.x
dp3 r3.x, r0.xyzx, r0.xyzx
rsq r3.x, r3.x
mul r0.xyz, r0.xyzx, r3.xxxx
dp3 r0.x, r4.xyzx, r0.xyzx
max r0.x, r0.x, l(0.000001)
log r0.x, r0.x
mul r0.x, r0.x, r2.w
exp r0.x, r0.x
mul r0.x, r0.x, r2.w
mul r0.xw, r0.xxxw, l(0.031250, 0.000000, 0.000000, 2.500000)
max r0.y, r2.y, l(0.000000)
mad r2.x, r2.y, l(0.490234), l(0.492188)
sample r2.xyzw, r2.xzxx, t0.xyzw, s3
add r0.z, r2.x, r2.x
mul r2.xyz, r0.zzzz, cb4[1].xyzx
mad r0.x, r0.x, r0.y, r0.w
mul r0.x, r3.z, r0.x
mad r0.yzw, r1.xxyz, cb4[1].wwww, -cb4[1].xxyz
mad r0.yzw, r1.wwww, r0.yyzw, cb4[1].xxyz
mul r0.xyz, r0.xxxx, r0.yzwy
mad o0.xyz, r2.xyzx, r1.xyzx, r0.xyzx
ret 
// Approximately 107 instruction slots used

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