
cbuffer _Globals : register(b0)
{
  float3 g_cameraPos : packoffset(c0);
  float2 g_invScreenSize : packoffset(c1);
  float4x4 g_invViewProjMatrix : packoffset(c2);
  float4x4 g_viewToShadowMatrix : packoffset(c6);
  float4 g_shadowmapHalfSizeAndInvHalfSize : packoffset(c10);
  float4 g_shadowmapPoissonScale : packoffset(c11);
  float4 g_shadowmapNoiseTilingFactor : packoffset(c12);
  float g_shadowPenumbraSize : packoffset(c13);
  float4 g_pcssFilterParameters0 : packoffset(c14);
  float4 g_pcssFilterParameters1 : packoffset(c15);
  float4x4 g_shadowmapViewMatrix : packoffset(c16);
  float4 g_shadowmapScales : packoffset(c20);
  float3 g_lightDir : packoffset(c21);
  float4x4 g_shadowmapTransform : packoffset(c22);
  uint4 g_shadowmapDimensions : packoffset(c26);
}
SamplerState g_shadowmapSamplerNoCompare_s : register(s1);
SamplerComparisonState g_shadowmapSampler_s : register(s0);
Texture2D<float4> g_shadowmapTexture : register(t0);
Texture2D<float4> g_shadowmapTextureNoCompare : register(t1);
Texture2D<float2> g_softShadowJitterTexture : register(t2);
Texture2D<float4> g_depthTexture : register(t3);
Texture2D<float4> g_gbufferTexture0 : register(t4);
Texture2D<float4> g_gbufferTexture2 : register(t5);

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,r8;
  uint4 bitmask;

//Work out Inverse
//...Variables
float4 a1, a2, a3, a4;
float4 b1, b2, b3, b4;
float det;
//...Original Matrix
a1 = g_invViewProjMatrix._m00_m10_m20_m30;
a2 = g_invViewProjMatrix._m01_m11_m21_m31;
a3 = g_invViewProjMatrix._m02_m12_m22_m32;
a4 = g_invViewProjMatrix._m03_m13_m23_m33;
//...Determinant
det  = a1.x*(a2.y*(a3.z*a4.w - a3.w*a4.z) + a2.z*(a3.w*a4.y - a3.y*a4.w) + a2.w*(a3.y*a4.z - a3.z*a4.y));
det += a1.y*(a2.x*(a3.w*a4.z - a3.z*a4.w) + a2.z*(a3.x*a4.w - a3.w*a4.z) + a2.w*(a3.z*a4.x - a3.x*a4.z));
det += a1.z*(a2.x*(a3.y*a4.w - a3.w*a4.y) + a2.y*(a3.w*a4.x - a3.x*a4.w) + a2.w*(a3.x*a4.y - a3.y*a4.x));
det += a1.w*(a2.x*(a3.z*a4.y - a3.y*a4.z) + a2.y*(a3.x*a4.z - a3.z*a4.x) + a2.z*(a3.y*a4.x - a3.x*a4.y));
//...Inverse Matrix Elements
b1.x = a2.y*(a3.z*a4.w - a3.w*a4.z) + a2.z*(a3.w*a4.y - a3.y*a4.w) + a2.w*(a3.y*a4.z - a3.z*a4.y);
b1.y = a1.y*(a3.w*a4.z - a3.z*a4.w) + a1.z*(a3.y*a4.w - a3.w*a4.y) + a1.w*(a3.z*a4.y - a3.y*a4.z);
b1.z = a1.y*(a2.z*a4.w - a2.w*a4.z) + a1.z*(a2.w*a4.y - a2.y*a4.w) + a1.w*(a2.y*a4.z - a2.z*a4.y);
b1.w = a1.y*(a2.w*a3.z - a2.z*a3.w) + a1.z*(a2.y*a3.w - a2.w*a3.y) + a1.w*(a2.z*a3.y - a2.y*a3.z);
b2.x = a2.x*(a3.w*a4.z - a3.z*a4.w) + a2.z*(a3.x*a4.w - a3.w*a4.x) + a2.w*(a3.z*a4.x - a3.x*a4.z);
b2.y = a1.x*(a3.z*a4.w - a3.w*a4.z) + a1.z*(a3.w*a4.x - a3.x*a4.w) + a1.w*(a3.x*a4.z - a3.z*a4.x);
b2.z = a1.x*(a2.w*a4.z - a2.z*a4.w) + a1.z*(a2.x*a4.w - a2.w*a4.x) + a1.w*(a2.z*a4.x - a2.x*a4.z);
b2.w = a1.x*(a2.z*a3.w - a2.w*a3.z) + a1.z*(a2.w*a3.x - a2.x*a3.w) + a1.w*(a2.x*a3.z - a2.z*a3.x);
b3.x = a2.x*(a3.y*a4.w - a3.w*a4.y) + a2.y*(a3.w*a4.x - a3.x*a4.w) + a2.w*(a3.x*a4.y - a3.y*a4.x);
b3.y = a1.x*(a3.w*a4.y - a3.y*a4.w) + a1.y*(a3.x*a4.w - a3.w*a4.x) + a1.w*(a3.y*a4.x - a3.x*a4.y);
b3.z = a1.x*(a2.y*a4.w - a2.w*a4.y) + a1.y*(a2.w*a4.x - a2.x*a4.w) + a1.w*(a2.x*a4.y - a2.y*a4.x);
b3.w = a1.x*(a2.w*a3.y - a2.y*a3.w) + a1.y*(a2.x*a3.w - a2.w*a3.x) + a1.w*(a2.y*a3.x - a2.x*a3.y);
b4.x = a2.x*(a3.z*a4.y - a3.y*a4.z) + a2.y*(a3.x*a4.z - a3.z*a4.x) + a2.z*(a3.y*a4.x - a3.x*a4.y);
b4.y = a1.x*(a3.y*a4.z - a3.z*a4.y) + a1.y*(a3.z*a4.x - a3.x*a4.z) + a1.z*(a3.x*a4.y - a3.y*a4.x);
b4.z = a1.x*(a2.z*a4.y - a2.y*a4.z) + a1.y*(a2.x*a4.z - a2.z*a4.x) + a1.z*(a2.y*a4.x - a2.x*a4.y);
b4.w = a1.x*(a2.y*a3.z - a2.z*a3.y) + a1.y*(a2.z*a3.x - a2.x*a3.z) + a1.z*(a2.x*a3.y - a2.y*a3.x);
b1.xyzw /= det;
b2.xyzw /= det;
b3.xyzw /= det;
b4.xyzw /= det;
//End Inverse


  r0.xy = (int2)v0.xy;
  r1.xy = g_invScreenSize.xy * v0.xy;
  r0.zw = float2(0.000000e+000,0.000000e+000);
  r2.z = g_depthTexture.Load(r0.xyw).x;
  r1.xy = r1.xy * float2(2.000000e+000,2.000000e+000) + float2(-1.000000e+000,-1.000000e+000);
  r2.xy = float2(1.000000e+000,-1.000000e+000) * r1.xy;
  r2.w = 1.000000000e+000;
  r1.x = dot(r2.xyzw, g_invViewProjMatrix._m00_m10_m20_m30);
  r1.y = dot(r2.xyzw, g_invViewProjMatrix._m01_m11_m21_m31);
  r1.z = dot(r2.xyzw, g_invViewProjMatrix._m02_m12_m22_m32);
  r1.w = dot(r2.xyzw, g_invViewProjMatrix._m03_m13_m23_m33);
  r1.w = 1.000000e+000 / r1.w;


float4 r28, r26, r27;
r26.xyz = r1.xyz * r1.www;
r26.w = 1.0;
r28.x = dot(r26.xyzw, b1.xyzw);
r28.y = dot(r26.xyzw, b2.xyzw);
r28.z = dot(r26.xyzw, b3.xyzw);
r28.w = dot(r26.xyzw, b4.xyzw);
float4 stereo = StereoParams.Load(0);
r28.x -= stereo.x * (r28.w - stereo.y); 
r26.x = dot(r28.xyzw, a1.xyzw);
r26.y = dot(r28.xyzw, a2.xyzw);
r26.z = dot(r28.xyzw, a3.xyzw);
r26.w = dot(r28.xyzw, a4.xyzw);
r1.xyz = r26.xyz / r1.www;

  r1.xyz = r1.xyz * r1.www;


  r1.w = 1.000000000e+000;
  r2.x = dot(r1.xyzw, g_shadowmapTransform._m00_m10_m20_m30);
  r2.y = dot(r1.xyzw, g_shadowmapTransform._m01_m11_m21_m31);
  r1.x = dot(r1.xyzw, g_shadowmapTransform._m02_m12_m22_m32);
  r1.yzw = g_gbufferTexture0.Load(r0.xyw).xyz;
  r0.x = g_gbufferTexture2.Load(r0.xyz).w;
  r0.x = 2.550000000e+002 * r0.x;
  r0.x = round(r0.x);
  r0.x = (int)r0.x;
  r0.x = r0.x == 4;
  if (r0.x != 0) {
    r0.xy = r1.yz * float2(4.000000e+000,4.000000e+000) + float2(-2.000000e+000,-2.000000e+000);
    r0.z = dot(r0.xy, r0.xy);
    r3.zw = -r0.zz * float2(2.500000e-001,5.000000e-001) + float2(1.000000e+000,1.000000e+000);
    r0.z = max(r3.z, 0.000000000e+000);
    r0.z = sqrt(r0.z);
    r3.xy = r0.xy * r0.zz;
    r0.x = dot(r3.xyw, r3.xyw);
    r0.x = rsqrt(r0.x);
    r0.xyz = r3.xyw * r0.xxx;
  } else {
    r1.yzw = r1.yzw * float3(2.000000e+000,2.000000e+000,2.000000e+000) + float3(-1.000000e+000,-1.000000e+000,-1.000000e+000);
    r0.w = dot(r1.yzw, r1.yzw);
    r0.w = rsqrt(r0.w);
    r0.xyz = r1.yzw * r0.www;
  }
  r3.x = dot(r0.xyz, g_shadowmapViewMatrix._m00_m10_m20);
  r3.y = dot(r0.xyz, g_shadowmapViewMatrix._m01_m11_m21);
  r3.z = dot(r0.xyz, g_shadowmapViewMatrix._m02_m12_m22);
  r0.xyz = g_shadowmapScales.xyz * r3.xyz;
  r0.xy = -r0.xy / r0.zz;
  r0.z = g_shadowmapDimensions.x;
  r0.z = 1.000000e+000 / r0.z;
  r1.yz = trunc(v0.yx);
  r1.yz = r1.zy * float2(7.200000e+002,1.024000e+003) + r1.yz;
  r1.yz = (uint2)r1.yz;
  r1.yz = (int2)r1.yz ^ int2(0,0);
  r1.yz = (int2)r1.yz * int2(0,0);
  r2.zw = (uint2)r1.yz >> int2(16,16);
  r1.yz = (int2)r1.yz ^ (int2)r2.zw;
  r1.yz = (int2)r1.yz * int2(0,0);
  r2.zw = (uint2)r1.yz >> int2(16,16);
  r1.yz = (int2)r1.yz ^ (int2)r2.zw;
  r1.yz = (int2)r1.yz * int2(0,0);
  r1.yz = r1.yz;
  r0.w = g_shadowPenumbraSize * r0.z;
  r1.w = g_shadowmapTextureNoCompare.Sample(g_shadowmapSamplerNoCompare_s, r2.xy).x;
  r2.z = -9.999999747e-006 + r1.x;
  r2.z = r1.w < r2.z;
  r2.z = r2.z ? 1.000000 : 0;
  r1.w = r2.z * r1.w;
  r3.xzw = float3(0.000000e+000,0.000000e+000,0.000000e+000);
  r4.x = r1.w;
  r4.y = r2.z;
  r3.y = 0.000000000e+000;
  while (true) {
    r2.w = (int)r3.y >= (int)16;
    if (r2.w != 0) break;
    r4.zw = g_softShadowJitterTexture.Load(r3.xyz).xy;
    r4.zw = r1.yz * float2(2.328306e-010,2.328306e-010) + r4.zw;
    r4.zw = frac(r4.zw);
    r2.w = sqrt(r4.z);
    r4.z = 6.283185482e+000 * r4.w;
    sincos(r4.z, r5.x, r6.x);
    r5.x = r5.x * r2.w;
    r5.y = r6.x * r2.w;
    r4.zw = r5.xy * r0.ww;
    r5.xy = r5.xy * r0.ww + r2.xy;
    r2.w = g_shadowmapTextureNoCompare.Sample(g_shadowmapSamplerNoCompare_s, r5.xy).x;
    r4.z = dot(r0.xy, r4.zw);
    r4.z = r4.z + r1.x;
    r4.z = -9.999999747e-006 + r4.z;
    r4.z = r2.w < r4.z;
    r4.z = r4.z ? 1.000000 : 0;
    r4.y = r4.y + r4.z;
    r4.x = r4.z * r2.w + r4.x;
    r3.y = (int)r3.y + 1;
  }
  r0.w = r4.y >= 1.000000000e+000;
  if (r0.w != 0) {
    r0.w = r4.x / r4.y;
    r3.xyz = (int3)g_pcssFilterParameters0.xyw;
    r1.w = r0.z + r0.z;
    r0.w = r1.x + -r0.w;
    r0.w = g_pcssFilterParameters1.x * r0.w;
    r0.w = g_shadowPenumbraSize * r0.w;
    r0.z = saturate(r0.w * r0.z);
    r0.z = max(r1.w, r0.z);
    r4.xzw = float3(0.000000e+000,0.000000e+000,0.000000e+000);
    r0.w = 0.000000000e+000;
    r4.y = 0.000000000e+000;
    while (true) {
      r1.w = (int)r4.y >= (int)r3.x;
      if (r1.w != 0) break;
      r2.zw = g_softShadowJitterTexture.Load(r4.xyz).xy;
      r2.zw = r1.yz * float2(2.328306e-010,2.328306e-010) + r2.zw;
      r2.zw = frac(r2.zw);
      r1.w = sqrt(r2.z);
      r2.z = 6.283185482e+000 * r2.w;
      sincos(r2.z, r5.x, r6.x);
      r5.x = r5.x * r1.w;
      r5.y = r6.x * r1.w;
      r2.zw = r5.xy * r0.zz;
      r1.w = dot(r0.xy, r2.zw);
      r1.w = r1.x + r1.w;
      r2.zw = r5.xy * r0.zz + r2.xy;
      r1.w = g_shadowmapTexture.GatherCmp(g_shadowmapSampler_s, r2.zw, r1.w).x;
      r1.w = dot(r1.wwww, float4(2.500000e-001,2.500000e-001,2.500000e-001,2.500000e-001));
      r0.w = r1.w + r0.w;
      r4.y = (int)r4.y + 1;
    }
    r1.w = r4.y;
    r1.w = r0.w / r1.w;
    r5.xzw = float3(0.000000e+000,0.000000e+000,0.000000e+000);
    r6.x = r0.w;
    r6.y = r4.y;
    r2.z = r1.w;
    r2.w = 1.000000000e+000;
    while (true) {
      r3.x = g_pcssFilterParameters0.z < r2.w;
      r3.w = (int)r6.y < (int)r3.y;
      r3.x = r3.w ? r3.x : 0;
      if (r3.x == 0) break;
      r3.x = (int)r3.z + (int)r6.y;
      r3.w = r6.x;
      r5.y = r6.y;
      while (true) {
        r4.x = (int)r5.y >= (int)r3.x;
        if (r4.x != 0) break;
        r4.xz = g_softShadowJitterTexture.Load(r5.xyz).xy;
        r4.xz = r1.yz * float2(2.328306e-010,2.328306e-010) + r4.xz;
        r4.xz = frac(r4.xz);
        r4.x = sqrt(r4.x);
        r4.z = 6.283185482e+000 * r4.z;
        sincos(r4.z, r7.x, r8.x);
        r7.x = r7.x * r4.x;
        r7.y = r8.x * r4.x;
        r4.xz = r7.xy * r0.zz;
        r4.x = dot(r0.xy, r4.xz);
        r4.x = r4.x + r1.x;
        r4.zw = r7.xy * r0.zz + r2.xy;
        r4.x = g_shadowmapTexture.GatherCmp(g_shadowmapSampler_s, r4.zw, r4.x).x;
        r4.x = dot(r4.xxxx, float4(2.500000e-001,2.500000e-001,2.500000e-001,2.500000e-001));
        r3.w = r4.x + r3.w;
        r5.y = (int)r5.y + 1;
      }
      r6.x = r3.w;
      r6.y = r5.y;
      r3.x = r6.y;
      r3.x = r6.x / r3.x;
      r4.x = r3.x + -r2.z;
      r4.z = r3.x + r2.z;
      r4.z = 5.000000000e-001 * r4.z;
      r2.w = r4.x / r4.z;
      r2.z = r3.x;
      r2.w = abs(r2.w);
    }
  } else {
    r2.z = 1.000000000e+000;
  }
  o0.xyzw = r2.zzzz;
  return;
}
