
cbuffer _Globals : register(b0)
{
  float2 g_invScreenSize : packoffset(c0);
  float4x4 g_invViewProjMatrix : packoffset(c1);
  float g_indirectCubeMapMipCount : packoffset(c5);
  float4 g_exposureMultipliers : packoffset(c6);
  float4x4 g_cubeMapLocalToBBox : packoffset(c7);
  float3 g_cubeMapExtents : packoffset(c11);
  float3 g_cubeMapFadeDistInverse : packoffset(c12);
  float g_cubeMapScale : packoffset(c12.w);
}
SamplerState g_indirectCubeMapSampler_s : register(s0);
TextureCube<float4> g_indirectCubeMap : register(t0);
Texture2DMS<float4,2> g_depthTexture : register(t1);
Texture2DMS<float4,2> g_gbufferTexture0 : register(t2);
Texture2DMS<float4,2> g_gbufferTexture1 : register(t3);
Texture2DMS<float4,2> g_gbufferTexture2 : register(t4);
Texture2DMS<float4,2> g_gbufferTexture4 : register(t5);
Texture2D<float> g_msaaClassifyTexture : register(t6);

Texture2D<float4> StereoParams : register(t125);

void main(
  float4 v0 : SV_Position0,
  float4 v1 : TEXCOORD0,
  float4 v2 : TEXCOORD1,
  float4 v3 : TEXCOORD2,
  out float4 o0 : SV_Target0)
{
  const float4 icb[] = { { 0.250000, 0, 0, 0},
                              { -0.250000, 0, 0, 0} };
  float4 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10;
  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;
  r0.zw = float2(0.000000e+000,0.000000e+000);
  r1.x = g_msaaClassifyTexture.Load(r0.xyw).x;
  r1.x = 0.000000000e+000 < r1.x;
  r1.xy = r1.xx ? float2(2.000000e+000,5.000000e-001) : float2(1.000000e+000,1.000000e+000);
  r1.z = g_indirectCubeMapMipCount;
  r1.w = trunc(g_indirectCubeMapMipCount);
  r1.z = (int)r1.z + -1;
  r1.z = r1.z;
  r2.x = 5.000000000e+000 + r1.w;
  r2.x = max(r2.x, 0.000000000e+000);
  r2.x = min(r1.z, r2.x);
  r3.w = 1.000000000e+000;
  r2.yzw = float3(0.000000e+000,0.000000e+000,0.000000e+000);
  r4.x = 0.000000000e+000;
  while (true) {
    r4.y = (int)r4.x >= (int)r1.x;
    if (r4.y != 0) break;
    r4.yz = icb[r4.x].xx * g_invScreenSize.xy;
    r4.yz = v0.xy * g_invScreenSize.xy + r4.yz;
    r5.xyzw = g_gbufferTexture0.Load(r0.xyw,r4.x).xyzw;
    r6.xyzw = g_gbufferTexture1.Load(r0.xyw,r4.x).xyzw;
    r4.w = g_gbufferTexture2.Load(r0.xyw,r4.x).w;
    r3.z = g_depthTexture.Load(r0.xyw,r4.x).x;
    r4.w = 2.550000000e+002 * r4.w;
    r4.w = round(r4.w);
    r4.w = (int)r4.w;
    r7.x = r4.w == 4;
    if (r7.x != 0) {
      r7.yz = g_gbufferTexture4.Load(r0.xyz,r4.x).xy;
      r8.xy = r5.xy * float2(4.000000e+000,4.000000e+000) + float2(-2.000000e+000,-2.000000e+000);
      r7.w = dot(r8.xy, r8.xy);
      r9.zw = -r7.ww * float2(2.500000e-001,5.000000e-001) + float2(1.000000e+000,1.000000e+000);
      r7.w = max(r9.z, 0.000000000e+000);
      r7.w = sqrt(r7.w);
      r9.xy = r8.xy * r7.ww;
      r7.w = dot(r9.xyw, r9.xyw);
      r7.w = rsqrt(r7.w);
      r8.xyz = r9.xyw * r7.www;
      r7.y = dot(r7.yz, float2(6.528000e+004,2.550000e+002));
      r7.y = round(r7.y);
      r7.z = 3.125000000e-002 * r7.y;
      r7.z = trunc(r7.z);
      r9.z = r7.y * 3.125000000e-002 + -r7.z;
      r7.y = 1.562500000e-002 * r7.z;
      r9.x = trunc(r7.y);
      r9.y = r7.z * 1.562500000e-002 + -r9.x;
      r7.yzw = float3(3.225806e-002,1.015873e+000,1.032258e+000) * r9.xyz;
      r9.xyz = r6.www * r6.www;
      r8.w = 0.000000000e+000;
    } else {
      r5.xyz = r5.xyz * float3(2.000000e+000,2.000000e+000,2.000000e+000) + float3(-1.000000e+000,-1.000000e+000,-1.000000e+000);
      r10.x = dot(r5.xyz, r5.xyz);
      r10.x = rsqrt(r10.x);
      r8.xyz = r10.xxx * r5.xyz;
      r9.xyzw = r6.xyzw * r6.xyzw;
      r5.x = r4.w == 5;
      if (r5.x == 0) {
        r4.w = r4.w == 1;
        if (r4.w != 0) {
          r9.xyz = r9.www * r9.xyz;
        } else {
          r9.xyz = r9.www;
        }
      }
      r4.w = 0.000000000e+000 < r9.x;
      r8.w = r4.w ? r5.w : 0;
      r7.yzw = float3(0.000000e+000,0.000000e+000,0.000000e+000);
    }
    r4.yz = r4.yz * float2(2.000000e+000,2.000000e+000) + float2(-1.000000e+000,-1.000000e+000);
    r3.xy = float2(1.000000e+000,-1.000000e+000) * r4.yz;
    r5.x = dot(r3.xyzw, g_invViewProjMatrix._m00_m10_m20_m30);
    r5.y = dot(r3.xyzw, g_invViewProjMatrix._m01_m11_m21_m31);
    r5.z = dot(r3.xyzw, g_invViewProjMatrix._m02_m12_m22_m32);
    r3.x = dot(r3.xyzw, g_invViewProjMatrix._m03_m13_m23_m33);
    r3.x = 1.000000e+000 / r3.x;


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

    r3.xyz = r5.xyz * r3.xxx;


    r4.yzw = g_cubeMapLocalToBBox._m01_m11_m21 * r3.yyy;
    r4.yzw = g_cubeMapLocalToBBox._m00_m10_m20 * r3.xxx + r4.yzw;
    r4.yzw = g_cubeMapLocalToBBox._m02_m12_m22 * r3.zzz + r4.yzw;
    r4.yzw = g_cubeMapLocalToBBox._m03_m13_m23 + r4.yzw;
    r5.xyz = -g_cubeMapExtents.xyz + abs(r4.yzw);
    r5.xyz = saturate(-r5.xyz * g_cubeMapFadeDistInverse.xyz + float3(1.000000e+000,1.000000e+000,1.000000e+000));
    r5.x = min(r5.y, r5.x);
    r5.x = min(r5.z, r5.x);
    r5.y = dot(r3.xyz, r3.xyz);
    r5.y = rsqrt(r5.y);
    r3.xyz = r5.yyy * r3.xyz;
    r5.y = dot(r3.xyz, r8.xyz);
    r5.y = r5.y + r5.y;
    r3.xyz = r8.xyz * -r5.yyy + r3.xyz;
    r5.yz = g_cubeMapExtents.xz + -r4.yw;
    r6.xy = -g_cubeMapExtents.xz + -r4.yw;
    r6.zw = float2(0.000000e+000,0.000000e+000) < r3.xz;
    r5.yz = r6.zw ? r5.yz : r6.xy;
    r5.yz = r5.yz / r3.xz;
    r5.y = min(r5.z, r5.y);
    r3.xyz = r3.xyz * r5.yyy + r4.yzw;
    r4.y = -r8.w * 1.500000000e+001 + r1.w;
    r4.y = 5.000000000e+000 + r4.y;
    r4.y = max(r4.y, 0.000000000e+000);
    r4.y = min(r1.z, r4.y);
    r4.yzw = g_indirectCubeMap.SampleLevel(g_indirectCubeMapSampler_s, r3.xyz, r4.y).xyz;
    r4.yzw = r4.yzw * r9.xyz;
    r4.yzw = r4.yzw * r5.xxx;
    r4.yzw = g_cubeMapScale * r4.yzw;
    if (r7.x != 0) {
      r3.xyz = g_indirectCubeMap.SampleLevel(g_indirectCubeMapSampler_s, r3.xyz, r2.x).xyz;
      r3.xyz = r3.xyz * r7.yzw;
      r3.xyz = r3.xyz * r5.xxx;
      r3.xyz = r3.xyz * g_cubeMapScale + r4.yzw;
      r4.yzw = float3(1.500000e-001,1.500000e-001,1.500000e-001) * r3.xyz;
    }
    r2.yzw = r4.yzw + r2.yzw;
    r4.x = (int)r4.x + 1;
  }
  o0.xyz = r2.yzw * r1.yyy;
  o0.w = 0.000000000e+000;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HLSL errors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(55,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(56,15-49): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(57,12-46): warning X3206: 'Load': implicit truncation of vector type
E:\Program Files (x86)\Origin Games\Dragon Age Inquisition\wrapper1349(58,12-43): 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
