
cbuffer _Globals : register(b0)
{
  float3 g_cameraPos : packoffset(c0);
  float2 g_invScreenSize : packoffset(c1);
  float4x4 g_invViewProjMatrix : packoffset(c2);
  float3 g_lightDir : packoffset(c6);
  float4 g_keyColor : packoffset(c7);
  float2 g_lightAngularRadius : packoffset(c8);
  float4 g_keyTranslucencyParams : packoffset(c9);
  float4 g_subParams : packoffset(c10);
  float4 g_envmapParams : packoffset(c11);
  float g_skyEnvmap8BitTexInvScale : packoffset(c12);
  float4 g_exposureMultipliers : packoffset(c13);
  float4 g_envmapsMipmapCount : packoffset(c14);
  int g_drawReference : packoffset(c15);
  float g_skyEnvmapMipmapCount : packoffset(c15.y);
}
SamplerState g_skyEnvmapSampler_s : register(s0);
SamplerState g_customSampler_s : register(s1);
SamplerState g_diffuseOcclusionSampler_s : register(s2);
SamplerState g_dynamicEnvmapSampler_s : register(s3);
TextureCube<float4> g_skyEnvmap : register(t0);
TextureCube<float4> g_customEnvmap : 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);
Texture2D<float> g_msaaClassifyTexture : register(t9);

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;
  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 = (int3)g_envmapsMipmapCount.zxy;
  r2.xyz = (int3)r2.xyz + int3(-1,-1,-1);
  r3.xyz = trunc(g_envmapsMipmapCount.zxy);
  r2.xyz = r2.xyz;
  r4.w = 1.000000000e+000;
  r5.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
  r2.w = 0.000000000e+000;
  while (true) {
    r3.w = (int)r2.w >= (int)r0.z;
    if (r3.w != 0) break;
    r6.xy = 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.xyz = g_gbufferTexture2.Load(r1.xyw,r2.w).xzw;
    r10.xyz = g_gbufferTexture3.Load(r1.xyw,r2.w).xyz;
    r4.z = g_depthTexture.Load(r1.xyz,r2.w).x;
    r3.w = 2.550000000e+002 * r9.z;
    r3.w = round(r3.w);
    r3.w = (int)r3.w;
    r6.zw = r3.ww == float2(4.000000e+000,3.000000e+000);
    if (r6.z != 0) {
      r9.zw = r7.xy * float2(4.000000e+000,4.000000e+000) + float2(-2.000000e+000,-2.000000e+000);
      r5.w = dot(r9.zw, r9.zw);
      r11.zw = -r5.ww * float2(2.500000e-001,5.000000e-001) + float2(1.000000e+000,1.000000e+000);
      r5.w = max(r11.z, 0.000000000e+000);
      r5.w = sqrt(r5.w);
      r11.xy = r9.zw * r5.ww;
      r5.w = dot(r11.xyw, r11.xyw);
      r5.w = rsqrt(r5.w);
      r11.xyz = r11.xyw * r5.www;
      r12.xyzw = r8.xyzw * r8.xyzw;
      r13.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);
      r9.z = dot(r7.xyz, r7.xyz);
      r9.z = rsqrt(r9.z);
      r11.xyz = r9.zzz * r7.xyz;
      r8.xyzw = r8.xyzw * r8.xyzw;
      r7.x = r3.w == 5;
      if (r7.x != 0) {
        r12.xyz = r8.xyz * r8.www;
        r13.xyz = r8.xyz;
      } else {
        r7.x = r3.w == 1;
        if (r7.x != 0) {
          r13.xyz = r8.www * r8.xyz;
        } else {
          r13.xyz = r8.www;
        }
        r12.xyz = r8.xyz;
      }
      r7.x = 0.000000000e+000 < r13.x;
      r5.w = r7.x ? r7.w : 0;
    }
    r7.xyz = g_exposureMultipliers.yyy * r10.xyz;
    r6.xy = r6.xy * float2(2.000000e+000,2.000000e+000) + float2(-1.000000e+000,-1.000000e+000);
    r4.xy = float2(1.000000e+000,-1.000000e+000) * r6.xy;
    r8.x = dot(r4.xyzw, g_invViewProjMatrix._m00_m10_m20_m30);
    r8.y = dot(r4.xyzw, g_invViewProjMatrix._m01_m11_m21_m31);
    r8.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 = r8.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);
r8.xyz = r26.xyz / r4.xxx;

    r4.xyz = r8.xyz * r4.xxx;


    r6.x = dot(r4.xyz, r4.xyz);
    r6.x = rsqrt(r6.x);
    r4.xyz = r6.xxx * r4.xyz;
    r6.x = dot(r4.xyz, r11.xyz);
    r6.x = r6.x + r6.x;
    r4.xyz = r11.xyz * -r6.xxx + r4.xyz;
    if (r6.w != 0) {
      r6.x = -r5.w * 1.500000000e+001 + r3.x;
      r6.x = 5.000000000e+000 + r6.x;
      r6.x = max(r6.x, 0.000000000e+000);
      r6.x = min(r2.x, r6.x);
      r6.xyw = g_dynamicEnvmap.SampleLevel(g_dynamicEnvmapSampler_s, r4.xyz, r6.x).xyz;
    } else {
      r7.w = log2(abs(r9.x));
      r7.w = g_envmapParams.z * r7.w;
      r7.w = exp2(r7.w);
      r3.w = r3.w != 4;
      if (r3.w != 0) {
        r8.xyzw = r9.yyyy == float4(0.000000e+000,3.922000e-003,7.843000e-003,1.176500e-002);
        r8.xyzw = r8.xyzw ? g_envmapParams.xxxx : 0;
        r9.xy = -r5.ww * float2(1.500000e+001,1.500000e+001) + r3.yz;
        r9.xy = float2(5.000000e+000,5.000000e+000) + r9.xy;
        r9.xy = max(r9.xy, float2(0.000000e+000,0.000000e+000));
        r9.xy = min(r2.yz, r9.xy);
        r11.xyzw = g_customEnvmap.SampleLevel(g_customSampler_s, r4.xyz, r9.y).xyzw;
        r3.w = dot(r8.xyzw, r11.xyzw);
        r4.xyz = g_skyEnvmap.SampleLevel(g_skyEnvmapSampler_s, r4.xyz, r9.x).xyz;
        r4.xyz = r4.xyz * r7.www;
        r4.xyz = g_skyEnvmap8BitTexInvScale * r4.xyz;
      } else {
        r4.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
        r3.w = 0.000000000e+000;
      }
      r8.xyz = r10.xyz * g_exposureMultipliers.yyy + g_envmapParams.yyy;
      r8.xyz = r8.xyz * r3.www;
      r3.w = 1.000000000e+000 + -r7.w;
      r6.xyw = r8.xyz * r3.www + r4.xyz;
    }
    if (r6.z == 0) {
      r6.xyw = r6.xyw * r13.xyz;
    }
    r4.xyz = r7.xyz * r12.xyz + r6.xyw;
    r5.xyz = r5.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 = r5.xyz * r0.www;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HLSL errors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(63,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(64,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(65,14-48): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(66,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(67,12-43): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(59,3-14): warning X3557: loop only executes for 0 iteration(s), forcing loop to unroll
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
