
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);
  float3 g_hemisphereDir : packoffset(c9);
  float3 g_topColor : packoffset(c10);
  float3 g_bottomColor : packoffset(c11);
  float4 g_keyColor : packoffset(c12);
  float2 g_lightAngularRadius : packoffset(c13);
  float4 g_keyTranslucencyParams : packoffset(c14);
  float4 g_subParams : packoffset(c15);
  float4 g_envmapParams : packoffset(c16);
  float g_skyEnvmap8BitTexInvScale : packoffset(c17);
  float4 g_exposureMultipliers : packoffset(c18);
  float4 g_envmapsMipmapCount : packoffset(c19);
  int g_drawReference : packoffset(c20);
  float g_skyEnvmapMipmapCount : packoffset(c20.y);
}
SamplerState g_cloudShadowSampler_s : register(s0);
SamplerState g_skyEnvmapSampler_s : register(s1);
SamplerState g_diffuseOcclusionSampler_s : register(s2);
SamplerState g_dynamicEnvmapSampler_s : register(s3);
Texture2D<float4> g_cloudShadowTexture : register(t0);
TextureCube<float4> g_skyEnvmap : register(t1);
Texture2D<float4> g_diffuseOcclusionTexture : register(t2);
TextureCube<float4> g_dynamicEnvmap : 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,
  out float4 o1 : SV_Target1)
{
  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,r20,r21,r22;
  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.xyz = -g_bottomColor.xyz + g_topColor.xyz;
  r2.w = 1.000000000e+000 + -g_envmapParams.w;
  r3.xy = (int2)g_envmapsMipmapCount.zx;
  r3.xy = (int2)r3.xy + int2(-1,-1);
  r3.zw = trunc(g_envmapsMipmapCount.zx);
  r3.xy = r3.xy;
  r4.w = 1.000000000e+000;
  r5.y = 0.000000000e+000;
  r6.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
  r7.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
  r5.z = 0.000000000e+000;
  while (true) {
    r5.w = (int)r5.z >= (int)r0.z;
    if (r5.w != 0) break;
    r8.xy = icb[r5.z].xy * g_invScreenSize.xy + r0.xy;
    r9.xyzw = g_gbufferTexture0.Load(r1.xyw,r5.z).xyzw;
    r10.xyzw = g_gbufferTexture1.Load(r1.xyw,r5.z).xyzw;
    r8.zw = g_gbufferTexture2.Load(r1.xyw,r5.z).yw;
    r11.xyz = g_gbufferTexture3.Load(r1.xyw,r5.z).xyz;
    r4.z = g_depthTexture.Load(r1.xyw,r5.z).x;
    r5.w = 2.550000000e+002 * r8.w;
    r5.w = round(r5.w);
    r5.w = (int)r5.w;
    r12.xyz = r5.www == float3(4.000000e+000,3.000000e+000,2.000000e+000);
    if (r12.x != 0) {
      r13.xyzw = g_gbufferTexture4.Load(r1.xyw,r5.z).xyzw;
      r14.xyzw = r9.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);
      r6.w = dot(r14.xy, r14.xy);
      r15.zw = -r6.ww * float2(2.500000e-001,5.000000e-001) + float2(1.000000e+000,1.000000e+000);
      r6.w = max(r15.z, 0.000000000e+000);
      r6.w = sqrt(r6.w);
      r15.xy = r14.xy * r6.ww;
      r6.w = dot(r15.xyw, r15.xyw);
      r6.w = rsqrt(r6.w);
      r15.xyz = r15.xyw * r6.www;
      r6.w = dot(r14.zw, r14.zw);
      r16.zw = -r6.ww * float2(2.500000e-001,5.000000e-001) + float2(1.000000e+000,1.000000e+000);
      r6.w = max(r16.z, 0.000000000e+000);
      r6.w = sqrt(r6.w);
      r16.xy = r14.zw * r6.ww;
      r6.w = dot(r16.xyw, r16.xyw);
      r6.w = rsqrt(r6.w);
      r14.xyz = r16.xyw * r6.www;
      r6.w = dot(r13.xy, float2(6.528000e+004,2.550000e+002));
      r6.w = round(r6.w);
      r7.w = 3.125000000e-002 * r6.w;
      r7.w = trunc(r7.w);
      r16.z = r6.w * 3.125000000e-002 + -r7.w;
      r6.w = 1.562500000e-002 * r7.w;
      r16.x = trunc(r6.w);
      r16.y = r7.w * 1.562500000e-002 + -r16.x;
      r16.xyz = float3(3.225806e-002,1.015873e+000,1.032258e+000) * r16.xyz;
      r13.xy = float2(2.550000e+002,2.550000e+002) * r13.zw;
      r13.xy = round(r13.xy);
      r13.zw = float2(6.250000e-002,6.250000e-002) * r13.xy;
      r17.xy = trunc(r13.zw);
      r17.zw = r13.xy * float2(6.250000e-002,6.250000e-002) + -r17.xy;
      r13.xw = float2(6.666667e-002,1.066667e+000) * r17.yw;
      r13.yz = r17.xz * float2(8.000001e-001,1.280000e+001) + float2(1.000000e+000,1.000000e+000);
      r13.yz = exp2(r13.yz);
      r13.yz = r13.yz * float2(1.443000e+000,1.443000e+000) + float2(1.443000e+000,1.443000e+000);
      r17.xyzw = r10.xyzw * r10.xyzw;
      r18.xyz = r17.www;
      r6.w = 0.000000000e+000;
    } else {
      r9.xyz = r9.xyz * float3(2.000000e+000,2.000000e+000,2.000000e+000) + float3(-1.000000e+000,-1.000000e+000,-1.000000e+000);
      r7.w = dot(r9.xyz, r9.xyz);
      r7.w = rsqrt(r7.w);
      r15.xyz = r9.xyz * r7.www;
      r10.xyzw = r10.xyzw * r10.xyzw;
      r7.w = r5.w == 5;
      if (r7.w != 0) {
        r17.xyz = r10.xyz * r10.www;
        r18.xyz = r10.xyz;
      } else {
        r7.w = r5.w == 1;
        if (r7.w != 0) {
          r18.xyz = r10.www * r10.xyz;
        } else {
          r18.xyz = r10.www;
        }
        r17.xyz = r10.xyz;
      }
      r7.w = 0.000000000e+000 < r18.x;
      r6.w = r7.w ? r9.w : 0;
      r7.w = r6.w * 1.200000000e+001 + 1.000000000e+000;
      r7.w = exp2(r7.w);
      r5.x = r7.w * 1.442999959e+000 + 1.442999959e+000;
      r13.xw = float2(0.000000e+000,0.000000e+000);
      r16.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
      r14.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
      r13.yz = r5.xy;
    }
    r8.xy = r8.xy * float2(2.000000e+000,2.000000e+000) + float2(-1.000000e+000,-1.000000e+000);
    r4.xy = float2(1.000000e+000,-1.000000e+000) * r8.xy;
    r9.x = dot(r4.xyzw, g_invViewProjMatrix._m00_m10_m20_m30);
    r9.y = dot(r4.xyzw, g_invViewProjMatrix._m01_m11_m21_m31);
    r9.z = dot(r4.xyzw, g_invViewProjMatrix._m02_m12_m22_m32);
    r4.x = dot(r4.xyzw, g_invViewProjMatrix._m03_m13_m23_m33);
    r4.x = 1.000000e+000 / r4.x;


float4 r28, r26, r27;
r26.xyz = r9.xyz * r4.xxx;
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);
r9.xyz = r26.xyz / r4.xxx;

    r9.xzw = r9.xzy * r4.xxx;


    r4.y = dot(r9.xzw, r9.xzw);
    r4.z = rsqrt(r4.y);
    r10.xyz = r9.xwz * r4.zzz;
    r5.x = g_shadowAccumTexture.Load(r1.xyz,r5.z).x;
    r4.x = r9.y * r4.x + g_cameraPos.y;
    r8.xy = g_lightDir.xz * r4.xx;
    r8.xy = r8.xy * g_cloudOffsetAndScale.ww + r9.xz;
    r8.xy = r8.xy * g_cloudOffsetAndScale.zz + g_cloudOffsetAndScale.xy;
    r4.x = g_cloudShadowTexture.SampleLevel(g_cloudShadowSampler_s, r8.xy, 0.000000000e+000).x;
    r4.x = saturate(g_cloudCoverageExponentAndFade.x + r4.x);
    r4.x = log2(r4.x);
    r4.x = g_cloudCoverageExponentAndFade.y * r4.x;
    r4.x = exp2(r4.x);
    r4.y = sqrt(r4.y);
    r4.y = -g_cloudCoverageExponentAndFade.z + r4.y;
    r4.y = saturate(g_cloudCoverageExponentAndFade.w * r4.y);
    r4.y = r4.y * r4.y;
    r7.w = 1.000000000e+000 + -r4.x;
    r4.x = r4.y * r7.w + r4.x;
    r4.x = r5.x * r4.x;
    r4.y = dot(r15.xyz, g_lightDir.xyz);
    r5.x = saturate(r4.y);
    r19.xyz = g_keyColor.xyz * r5.xxx;
    r19.xyz = r19.xyz * r4.xxx;
    if (r12.x != 0) {
      r13.w = saturate(r13.w);
      r4.y = r13.w * 5.000000000e-001 + r4.y;
      r5.x = r13.w * 5.000000000e-001 + 1.000000000e+000;
      r4.y = saturate(r4.y / r5.x);
      r20.xyz = g_keyColor.xyz * r4.yyy;
      r20.xyz = r20.xyz * r4.xxx;
      r21.xyz = -r15.xyz * r13.xxx + r14.xyz;
      r4.y = dot(r21.xyz, r21.xyz);
      r4.y = rsqrt(r4.y);
      r21.xyz = r21.xyz * r4.yyy;
      r22.xyz = -r9.xwz * r4.zzz + g_lightDir.xyz;
      r4.y = dot(r22.xyz, r22.xyz);
      r4.y = rsqrt(r4.y);
      r22.xyz = r22.xyz * r4.yyy;
      r4.y = dot(r14.xyz, r22.xyz);
      r4.y = -r4.y * r4.y + 1.000000000e+000;
      r4.y = max(r4.y, 0.000000000e+000);
      r4.y = sqrt(r4.y);
      r4.y = log2(r4.y);
      r4.y = r13.y * r4.y;
      r4.y = exp2(r4.y);
      r5.x = dot(r21.xyz, r22.xyz);
      r5.x = -r5.x * r5.x + 1.000000000e+000;
      r5.x = max(r5.x, 0.000000000e+000);
      r5.x = sqrt(r5.x);
      r5.x = log2(r5.x);
      r5.x = r13.z * r5.x;
      r5.x = exp2(r5.x);
      r13.xzw = r5.xxx * r16.xyz;
      r13.xzw = r4.yyy * r18.xxx + r13.xzw;
      r13.xzw = float3(1.500000e-001,1.500000e-001,1.500000e-001) * r13.xzw;
      r13.xzw = r19.xyz * r13.xzw;
      r13.xzw = g_keyColor.www * r13.xzw;
      r19.xyz = r20.xyz;
    } else {
      r9.xyz = -r9.xwz * r4.zzz + g_lightDir.xyz;
      r4.y = dot(r9.xyz, r9.xyz);
      r4.y = rsqrt(r4.y);
      r9.xyz = r9.xyz * r4.yyy;
      r4.y = saturate(dot(r15.xyz, r9.xyz));
      r4.z = r13.y * 8.662509918e-002 + 8.750000000e-001;
      r4.y = r4.y * r13.y + -r13.y;
      r4.y = exp2(r4.y);
      r4.y = r4.z * r4.y;
      r9.xyz = r19.xyz * r4.yyy;
      r13.xzw = g_keyColor.www * r9.xyz;
    }
    r4.y = dot(r15.xyz, g_hemisphereDir.xyz);
    r4.y = r4.y * 5.000000000e-001 + 5.000000000e-001;
    r9.xyz = r4.yyy * r2.xyz + g_bottomColor.xyz;
    r9.xyz = r19.xyz + r9.xyz;
    r9.xyz = r11.xyz * g_exposureMultipliers.yyy + r9.xyz;
    r4.y = dot(r10.xyz, r15.xyz);
    r4.y = r4.y + r4.y;
    r11.xyz = r15.xyz * -r4.yyy + r10.xyz;
    if (r12.y != 0) {
      r4.y = -r6.w * 1.500000000e+001 + r3.z;
      r4.y = 5.000000000e+000 + r4.y;
      r4.y = max(r4.y, 0.000000000e+000);
      r4.y = min(r3.x, r4.y);
      r14.xyz = g_dynamicEnvmap.SampleLevel(g_dynamicEnvmapSampler_s, r11.xyz, r4.y).xyz;
      r13.xzw = r14.xyz + r13.xzw;
    } else {
      r4.y = r5.w != 4;
      if (r4.y != 0) {
        r4.y = r4.x * r2.w + g_envmapParams.w;
        r4.z = -r6.w * 1.500000000e+001 + r3.w;
        r4.z = 5.000000000e+000 + r4.z;
        r4.z = max(r4.z, 0.000000000e+000);
        r4.z = min(r3.y, r4.z);
        r11.xyz = g_skyEnvmap.SampleLevel(g_skyEnvmapSampler_s, r11.xyz, r4.z).xyz;
        r11.xyz = r11.xyz * r4.yyy;
        r13.xzw = r11.xyz * g_skyEnvmap8BitTexInvScale + r13.xzw;
      }
    }
    r4.y = (int)r5.w >= (int)6;
    if (r4.y != 0) {
      r4.y = saturate(-2.352941222e-002 + r8.w);
      r8.y = 1.000000000e+000 + -r4.y;
      r8.z = saturate(r8.z);
      r8.x = 1.000000000e+000 + -r8.z;
      r11.xyz = r15.xyz * g_keyTranslucencyParams.www + g_lightDir.xyz;
      r4.y = saturate(dot(-r10.xyz, -r11.xyz));
      r5.xw = g_keyTranslucencyParams.yy * r8.xy;
      r5.xw = float2(-1.442695e+000,-1.442695e+000) * r5.xw;
      r5.xw = exp2(r5.xw);
      r4.y = r4.y * g_keyTranslucencyParams.z + g_keyTranslucencyParams.x;
      r4.y = dot(r4.yy, r5.xw);
      r4.x = r4.y * r4.x;
      r9.xyz = r4.xxx * g_keyColor.xyz + r9.xyz;
    }
    if (r12.x == 0) {
      r13.xzw = r13.xzw * r18.xyz;
    }
    r4.x = r12.z ? 0.000000000e+000 : 1.000000000e+000;
    r4.xyz = r13.xzw * r4.xxx;
    r4.xyz = r9.xyz * r17.xyz + r4.xyz;
    r7.xyz = r7.xyz + r4.xyz;
    r6.xyz = r13.xzw + r6.xyz;
    r5.z = (int)r5.z + 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 = r7.xyz * r0.www;
  o1.xyz = r6.xyz * r0.www;
  o1.w = 1.000000000e+000;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HLSL errors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(75,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(76,16-50): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(77,13-47): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(78,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(79,12-43): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(85,18-52): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(164,12-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(71,3-14): warning X3557: loop only executes for 0 iteration(s), forcing loop to unroll
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
