
cbuffer _Globals : register(b0)
{
  float3 g_cameraPos : packoffset(c0);
  float2 g_invScreenSize : packoffset(c1);
  float4x4 g_invViewProjMatrix : packoffset(c2);
  float4 g_cloudOffsetAndScale : packoffset(c6);
  float4 g_cloudCoverageExponentAndFade : packoffset(c7);
  float3 g_lightDir : packoffset(c8);
  float4 g_keyColor : packoffset(c9);
  float2 g_lightAngularRadius : packoffset(c10);
  float4 g_keyTranslucencyParams : packoffset(c11);
  float4 g_subParams : packoffset(c12);
  float4 g_envmapParams : packoffset(c13);
  float g_skyEnvmap8BitTexInvScale : packoffset(c14);
  float4 g_exposureMultipliers : packoffset(c15);
  float4 g_envmapsMipmapCount : packoffset(c16);
  int g_drawReference : packoffset(c17);
  float g_skyEnvmapMipmapCount : packoffset(c17.y);
}
SamplerState g_cloudShadowSampler_s : register(s0);
SamplerState g_skyEnvmapSampler_s : register(s1);
SamplerState g_customSampler_s : register(s2);
SamplerState g_diffuseOcclusionSampler_s : register(s3);
Texture2D<float4> g_cloudShadowTexture : register(t0);
TextureCube<float4> g_skyEnvmap : register(t1);
TextureCube<float4> g_customEnvmap : register(t2);
Texture2D<float4> g_diffuseOcclusionTexture : register(t3);
Texture2DMS<float4,4> g_depthTexture : register(t4);
Texture2DMS<float4,4> g_gbufferTexture0 : register(t5);
Texture2DMS<float4,4> g_gbufferTexture1 : register(t6);
Texture2DMS<float4,4> g_gbufferTexture2 : register(t7);
Texture2DMS<float4,4> g_gbufferTexture3 : register(t8);
Texture2DMS<float4,4> g_gbufferTexture4 : register(t9);
Texture2DMS<float4> g_shadowAccumTexture : register(t10);
Texture2D<float> g_msaaClassifyTexture : register(t11);

Texture2D<float4> StereoParams : register(t125);

void main(
  float4 v0 : SV_Position0,
  out float4 o0 : SV_Target0)
{
  const float4 icb[] = { { -0.125000, -0.375000, 0, 0},
                              { 0.375000, -0.125000, 0, 0},
                              { -0.375000, 0.125000, 0, 0},
                              { 0.125000, 0.375000, 0, 0} };
  float4 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19;
  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 = g_invScreenSize.xy * v0.xy;
  r1.xy = (int2)v0.xy;
  r1.zw = float2(0.000000e+000,0.000000e+000);
  r0.z = g_msaaClassifyTexture.Load(r1.xyw).x;
  r0.z = 0.000000000e+000 < r0.z;
  r0.zw = r0.zz ? float2(5.605194e-045,2.500000e-001) : float2(1.401298e-045,1.000000e+000);
  r2.x = 1.000000000e+000 + -g_envmapParams.w;
  r2.yz = (int2)g_envmapsMipmapCount.xy;
  r2.yz = (int2)r2.yz + int2(-1,-1);
  r3.xy = trunc(g_envmapsMipmapCount.xy);
  r2.yz = r2.yz;
  r4.w = 1.000000000e+000;
  r5.y = 0.000000000e+000;
  r6.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
  r2.w = 0.000000000e+000;
  while (true) {
    r3.z = (int)r2.w >= (int)r0.z;
    if (r3.z != 0) break;
    r3.zw = icb[r2.w].xy * g_invScreenSize.xy + r0.xy;
    r7.xyzw = g_gbufferTexture0.Load(r1.xyw,r2.w).xyzw;
    r8.xyzw = g_gbufferTexture1.Load(r1.xyw,r2.w).xyzw;
    r9.xyzw = g_gbufferTexture2.Load(r1.xyw,r2.w).yxzw;
    r10.xyz = g_gbufferTexture3.Load(r1.xyw,r2.w).xyz;
    r4.z = g_depthTexture.Load(r1.xyw,r2.w).x;
    r5.z = 2.550000000e+002 * r9.w;
    r5.z = round(r5.z);
    r5.z = (int)r5.z;
    r11.xy = r5.zz == float2(4.000000e+000,2.000000e+000);
    if (r11.x != 0) {
      r12.xyzw = g_gbufferTexture4.Load(r1.xyw,r2.w).xyzw;
      r13.xyzw = r7.xyzw * float4(4.000000e+000,4.000000e+000,4.000000e+000,4.000000e+000) + float4(-2.000000e+000,-2.000000e+000,-2.000000e+000,-2.000000e+000);
      r5.w = dot(r13.xy, r13.xy);
      r14.zw = -r5.ww * float2(2.500000e-001,5.000000e-001) + float2(1.000000e+000,1.000000e+000);
      r5.w = max(r14.z, 0.000000000e+000);
      r5.w = sqrt(r5.w);
      r14.xy = r13.xy * r5.ww;
      r5.w = dot(r14.xyw, r14.xyw);
      r5.w = rsqrt(r5.w);
      r14.xyz = r14.xyw * r5.www;
      r5.w = dot(r13.zw, r13.zw);
      r15.zw = -r5.ww * float2(2.500000e-001,5.000000e-001) + float2(1.000000e+000,1.000000e+000);
      r5.w = max(r15.z, 0.000000000e+000);
      r5.w = sqrt(r5.w);
      r15.xy = r13.zw * r5.ww;
      r5.w = dot(r15.xyw, r15.xyw);
      r5.w = rsqrt(r5.w);
      r13.xyz = r15.xyw * r5.www;
      r5.w = dot(r12.xy, float2(6.528000e+004,2.550000e+002));
      r5.w = round(r5.w);
      r6.w = 3.125000000e-002 * r5.w;
      r6.w = trunc(r6.w);
      r15.z = r5.w * 3.125000000e-002 + -r6.w;
      r5.w = 1.562500000e-002 * r6.w;
      r15.x = trunc(r5.w);
      r15.y = r6.w * 1.562500000e-002 + -r15.x;
      r15.xyz = float3(3.225806e-002,1.015873e+000,1.032258e+000) * r15.xyz;
      r11.zw = float2(2.550000e+002,2.550000e+002) * r12.zw;
      r11.zw = round(r11.zw);
      r12.xy = float2(6.250000e-002,6.250000e-002) * r11.zw;
      r12.xy = trunc(r12.xy);
      r12.zw = r11.zw * float2(6.250000e-002,6.250000e-002) + -r12.xy;
      r16.xw = float2(6.666667e-002,1.066667e+000) * r12.yw;
      r11.zw = r12.xz * float2(8.000001e-001,1.280000e+001) + float2(1.000000e+000,1.000000e+000);
      r11.zw = exp2(r11.zw);
      r11.zw = r11.zw * float2(1.443000e+000,1.443000e+000) + float2(1.443000e+000,1.443000e+000);
      r12.xyzw = r8.xyzw * r8.xyzw;
      r17.xyz = r12.www;
      r5.w = 0.000000000e+000;
    } else {
      r7.xyz = r7.xyz * float3(2.000000e+000,2.000000e+000,2.000000e+000) + float3(-1.000000e+000,-1.000000e+000,-1.000000e+000);
      r6.w = dot(r7.xyz, r7.xyz);
      r6.w = rsqrt(r6.w);
      r14.xyz = r7.xyz * r6.www;
      r8.xyzw = r8.xyzw * r8.xyzw;
      r6.w = r5.z == 5;
      if (r6.w != 0) {
        r12.xyz = r8.xyz * r8.www;
        r17.xyz = r8.xyz;
      } else {
        r6.w = r5.z == 1;
        if (r6.w != 0) {
          r17.xyz = r8.www * r8.xyz;
        } else {
          r17.xyz = r8.www;
        }
        r12.xyz = r8.xyz;
      }
      r6.w = 0.000000000e+000 < r17.x;
      r5.w = r6.w ? r7.w : 0;
      r6.w = r5.w * 1.200000000e+001 + 1.000000000e+000;
      r6.w = exp2(r6.w);
      r5.x = r6.w * 1.442999959e+000 + 1.442999959e+000;
      r11.zw = r5.xy;
      r15.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
      r13.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
      r16.xw = float2(0.000000e+000,0.000000e+000);
    }
    r3.zw = r3.zw * float2(2.000000e+000,2.000000e+000) + float2(-1.000000e+000,-1.000000e+000);
    r4.xy = float2(1.000000e+000,-1.000000e+000) * r3.zw;
    r7.x = dot(r4.xyzw, g_invViewProjMatrix._m00_m10_m20_m30);
    r7.y = dot(r4.xyzw, g_invViewProjMatrix._m01_m11_m21_m31);
    r7.z = dot(r4.xyzw, g_invViewProjMatrix._m02_m12_m22_m32);
    r3.z = dot(r4.xyzw, g_invViewProjMatrix._m03_m13_m23_m33);
    r3.z = 1.000000e+000 / r3.z;


float4 r28, r26, r27;
r26.xyz = r7.xyz * r3.zzz;
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);
r7.xyz = r26.xyz / r3.zzz;

    r4.xyz = r7.xzy * r3.zzz;


    r3.w = dot(r4.xyz, r4.xyz);
    r5.x = rsqrt(r3.w);
    r7.xzw = r5.xxx * r4.xzy;
    r6.w = log2(abs(r9.y));
    r6.w = g_envmapParams.z * r6.w;
    r6.w = exp2(r6.w);
    r8.x = g_shadowAccumTexture.Load(r1.xyz,r2.w).x;
    r3.z = r7.y * r3.z + g_cameraPos.y;
    r8.yz = g_lightDir.xz * r3.zz;
    r8.yz = r8.yz * g_cloudOffsetAndScale.ww + r4.xy;
    r8.yz = r8.yz * g_cloudOffsetAndScale.zz + g_cloudOffsetAndScale.xy;
    r3.z = g_cloudShadowTexture.SampleLevel(g_cloudShadowSampler_s, r8.yz, 0.000000000e+000).x;
    r3.z = saturate(g_cloudCoverageExponentAndFade.x + r3.z);
    r3.z = log2(r3.z);
    r3.z = g_cloudCoverageExponentAndFade.y * r3.z;
    r3.z = exp2(r3.z);
    r3.w = sqrt(r3.w);
    r3.w = -g_cloudCoverageExponentAndFade.z + r3.w;
    r3.w = saturate(g_cloudCoverageExponentAndFade.w * r3.w);
    r3.w = r3.w * r3.w;
    r7.y = 1.000000000e+000 + -r3.z;
    r3.z = r3.w * r7.y + r3.z;
    r3.z = r8.x * r3.z;
    r3.w = dot(r14.xyz, g_lightDir.xyz);
    r7.y = saturate(r3.w);
    r8.xyz = g_keyColor.xyz * r7.yyy;
    r8.xyz = r8.xyz * r3.zzz;
    if (r11.x != 0) {
      r16.w = saturate(r16.w);
      r7.y = r16.w * 5.000000000e-001 + r3.w;
      r8.w = r16.w * 5.000000000e-001 + 1.000000000e+000;
      r7.y = saturate(r7.y / r8.w);
      r16.yzw = g_keyColor.xyz * r7.yyy;
      r16.yzw = r16.yzw * r3.zzz;
      r18.xyz = -r14.xyz * r16.xxx + r13.xyz;
      r7.y = dot(r18.xyz, r18.xyz);
      r7.y = rsqrt(r7.y);
      r18.xyz = r18.xyz * r7.yyy;
      r19.xyz = -r4.xzy * r5.xxx + g_lightDir.xyz;
      r7.y = dot(r19.xyz, r19.xyz);
      r7.y = rsqrt(r7.y);
      r19.xyz = r19.xyz * r7.yyy;
      r7.y = dot(r13.xyz, r19.xyz);
      r7.y = -r7.y * r7.y + 1.000000000e+000;
      r7.y = max(r7.y, 0.000000000e+000);
      r7.y = sqrt(r7.y);
      r7.y = log2(r7.y);
      r7.y = r11.z * r7.y;
      r7.y = exp2(r7.y);
      r8.w = dot(r18.xyz, r19.xyz);
      r8.w = -r8.w * r8.w + 1.000000000e+000;
      r8.w = max(r8.w, 0.000000000e+000);
      r8.w = sqrt(r8.w);
      r8.w = log2(r8.w);
      r8.w = r11.w * r8.w;
      r8.w = exp2(r8.w);
      r13.xyz = r8.www * r15.xyz;
      r13.xyz = r7.yyy * r17.xxx + r13.xyz;
      r13.xyz = float3(1.500000e-001,1.500000e-001,1.500000e-001) * r13.xyz;
      r13.xyz = r13.xyz * r8.xyz;
      r13.xyz = g_keyColor.www * r13.xyz;
      r8.xyz = r16.yzw;
    } else {
      r4.xyz = -r4.xzy * r5.xxx + g_lightDir.xyz;
      r5.x = dot(r4.xyz, r4.xyz);
      r5.x = rsqrt(r5.x);
      r4.xyz = r5.xxx * r4.xyz;
      r4.x = saturate(dot(r14.xyz, r4.xyz));
      r4.y = r11.z * 8.662509918e-002 + 8.750000000e-001;
      r4.x = r4.x * r11.z + -r11.z;
      r4.x = exp2(r4.x);
      r4.x = r4.y * r4.x;
      r4.xyz = r8.xyz * r4.xxx;
      r13.xyz = g_keyColor.www * r4.xyz;
    }
    r4.xyz = r10.xyz * g_exposureMultipliers.yyy + r8.xyz;
    r5.x = r5.z != 4;
    if (r5.x != 0) {
      r5.x = dot(r7.xzw, r14.xyz);
      r5.x = r5.x + r5.x;
      r8.xyz = r14.xyz * -r5.xxx + r7.xzw;
      r15.xyzw = r9.zzzz == float4(0.000000e+000,3.922000e-003,7.843000e-003,1.176500e-002);
      r15.xyzw = r15.xyzw ? g_envmapParams.xxxx : 0;
      r5.x = r3.z * r2.x + g_envmapParams.w;
      r5.x = r5.x * r6.w;
      r9.yz = -r5.ww * float2(1.500000e+001,1.500000e+001) + r3.xy;
      r9.yz = float2(5.000000e+000,5.000000e+000) + r9.yz;
      r9.yz = max(r9.yz, float2(0.000000e+000,0.000000e+000));
      r9.yz = min(r2.yz, r9.yz);
      r16.xyzw = g_customEnvmap.SampleLevel(g_customSampler_s, r8.xyz, r9.z).xyzw;
      r5.w = dot(r15.xyzw, r16.xyzw);
      r8.xyz = g_skyEnvmap.SampleLevel(g_skyEnvmapSampler_s, r8.xyz, r9.y).xyz;
      r8.xyz = r8.xyz * r5.xxx;
      r13.xyz = r8.xyz * g_skyEnvmap8BitTexInvScale + r13.xyz;
    } else {
      r5.w = 0.000000000e+000;
    }
    r8.xyz = r10.xyz * g_exposureMultipliers.yyy + g_envmapParams.yyy;
    r8.xyz = r8.xyz * r5.www;
    r5.x = 1.000000000e+000 + -r6.w;
    r8.xyz = r8.xyz * r5.xxx + r13.xyz;
    if (r11.y != 0) {
      r5.x = r3.w * 1.149889946e+000 + -2.145639896e+000;
      r5.x = r3.w * r5.x + 8.416090012e-001;
      r3.w = saturate(r3.w * r5.x + 1.541409940e-001);
      r10.xyz = g_subParams.xyz * r3.www;
      r4.xyz = r10.xyz * r3.zzz + r4.xyz;
    }
    r3.w = (int)r5.z >= (int)6;
    if (r3.w != 0) {
      r3.w = saturate(-2.352941222e-002 + r9.w);
      r10.y = 1.000000000e+000 + -r3.w;
      r9.x = saturate(r9.x);
      r10.x = 1.000000000e+000 + -r9.x;
      r5.xzw = r14.xyz * g_keyTranslucencyParams.www + g_lightDir.xyz;
      r3.w = saturate(dot(-r7.xzw, -r5.xzw));
      r5.xz = g_keyTranslucencyParams.yy * r10.xy;
      r5.xz = float2(-1.442695e+000,-1.442695e+000) * r5.xz;
      r5.xz = exp2(r5.xz);
      r3.w = r3.w * g_keyTranslucencyParams.z + g_keyTranslucencyParams.x;
      r3.w = dot(r3.ww, r5.xz);
      r3.z = r3.w * r3.z;
      r4.xyz = r3.zzz * g_keyColor.xyz + r4.xyz;
    }
    if (r11.x == 0) {
      r8.xyz = r8.xyz * r17.xyz;
    }
    r4.xyz = r4.xyz * r12.xyz + r8.xyz;
    r6.xyz = r6.xyz + r4.xyz;
    r2.w = (int)r2.w + 1;
  }
  r1.xyzw = g_diffuseOcclusionTexture.Gather(g_diffuseOcclusionSampler_s, r0.xy).xyzw;
  r0.x = min(r1.y, r1.x);
  r0.x = min(r1.z, r0.x);
  o0.w = min(r1.w, r0.x);
  o0.xyz = r6.xyz * r0.www;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HLSL errors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(69,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(70,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(71,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(72,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(73,12-43): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(79,18-52): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(161,12-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(65,3-14): warning X3557: loop only executes for 0 iteration(s), forcing loop to unroll
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
