
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,4> g_depthTexture : register(t1);
Texture2DMS<float4,4> g_gbufferTexture0 : register(t2);
Texture2DMS<float4,4> g_gbufferTexture1 : register(t3);
Texture2DMS<float4,4> g_gbufferTexture2 : register(t4);
Texture2D<float> g_msaaClassifyTexture : register(t5);

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,
  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;
  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(4.000000e+000,2.500000e-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.w = 1.000000000e+000;
  r4.xyz = float3(0.000000e+000,0.000000e+000,0.000000e+000);
  r3.xyzw = float4(0.000000e+000,0.000000e+000,0.000000e+000,0.000000e+000);
  while (true) {
    r4.w = (int)r3.w >= (int)r1.x;
    if (r4.w != 0) break;
    r5.xy = icb[r3.w].xy * g_invScreenSize.xy;
    r5.xy = v0.xy * g_invScreenSize.xy + r5.xy;
    r6.xyzw = g_gbufferTexture0.Load(r0.xyw,r3.w).xyzw;
    r7.xyzw = g_gbufferTexture1.Load(r0.xyw,r3.w).xyzw;
    r4.w = g_gbufferTexture2.Load(r0.xyw,r3.w).w;
    r2.z = g_depthTexture.Load(r0.xyz,r3.w).x;
    r6.xyz = r6.xyz * float3(2.000000e+000,2.000000e+000,2.000000e+000) + float3(-1.000000e+000,-1.000000e+000,-1.000000e+000);
    r5.z = dot(r6.xyz, r6.xyz);
    r5.z = rsqrt(r5.z);
    r6.xyz = r6.xyz * r5.zzz;
    r4.w = 2.550000000e+002 * r4.w;
    r4.w = round(r4.w);
    r4.w = (int)r4.w;
    r7.xyzw = r7.xyzw * r7.xyzw;
    r5.zw = r4.ww == float2(5.000000e+000,2.000000e+000);
    if (r5.z == 0) {
      r4.w = r4.w == 1;
      if (r4.w != 0) {
        r7.xyz = r7.www * r7.xyz;
      } else {
        r7.xyz = r7.www;
      }
    }
    r4.w = 0.000000000e+000 < r7.x;
    r4.w = r4.w ? r6.w : 0;
    r5.xy = r5.xy * float2(2.000000e+000,2.000000e+000) + float2(-1.000000e+000,-1.000000e+000);
    r2.xy = float2(1.000000e+000,-1.000000e+000) * r5.xy;
    r5.x = dot(r2.xyzw, g_invViewProjMatrix._m00_m10_m20_m30);
    r5.y = dot(r2.xyzw, g_invViewProjMatrix._m01_m11_m21_m31);
    r5.z = dot(r2.xyzw, g_invViewProjMatrix._m02_m12_m22_m32);
    r2.x = dot(r2.xyzw, g_invViewProjMatrix._m03_m13_m23_m33);
    r2.x = 1.000000e+000 / r2.x;


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

    r2.xyz = r5.xyz * r2.xxx;


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

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