
cbuffer _Globals : register(b0)
{
  float g_skyGradientBlendFactor : packoffset(c0);
  float g_panoramicBlendRotation : packoffset(c0.y);
  float4 g_sunColorAndSize : packoffset(c1);
  float4 g_sunDirAndSkyGradientScale : packoffset(c2);
  float g_panoramicRotation : packoffset(c3);
  float4 g_panoramicUVRanges : packoffset(c4);
  float g_panoramicTileFactor : packoffset(c5);
  float4 g_cloudLayerSunColor : packoffset(c6);
  float4 g_cloudLayerLightingParams0 : packoffset(c7);
  float4 g_cloudLayerLightingParams1 : packoffset(c8);
  float4 g_cloudLayer0Color : packoffset(c9);
  float4 g_cloudLayerParams : packoffset(c10);
  float4 g_cloudLayer0UVTransform0 : packoffset(c11);
  float4 g_cloudLayer0UVTransform1 : packoffset(c12);
  float4 g_cloudLayer1UVTransform0 : packoffset(c13);
  float4 g_cloudLayer1UVTransform1 : packoffset(c14);
  float4 g_cloudLayer1Color : packoffset(c15);
  float4 g_fogParams : packoffset(c16);
  float2 g_forwardScatteringParams : packoffset(c17);
  float4 g_forwardScatteringColorPresence : packoffset(c18);
  float4 g_fogCoefficients : packoffset(c19);
  float4 g_fogColorCoefficients : packoffset(c20);
  float4 g_fogColor : packoffset(c21);
  float g_fogStartDistance : packoffset(c22);
  float4 g_heightFogCoefficients : packoffset(c23);
  float4x4 g_invViewProjMatrix : packoffset(c24);
  float3 g_camPos : packoffset(c28);
  float2 g_screenSizeInv : packoffset(c29);
  float g_hdrExposureMultiplier : packoffset(c29.z);
  float g_drawEnvmap : packoffset(c29.w);
  float g_drawThetaPhiEnvmap : packoffset(c30);
  float4 g_alphaSelector : packoffset(c31);
  float g_hdriRotationAngle : packoffset(c32);
  float g_hdriCosRotation : packoffset(c32.y);
  float g_hdriSinRotation : packoffset(c32.z);
}
SamplerState g_skyGradientSampler_s : register(s0);
SamplerState g_acosLUTSampler_s : register(s1);
SamplerState g_skyGradientBlendSampler_s : register(s2);
SamplerState g_panoramicSampler_s : register(s3);
SamplerState g_panoramicAlphaSampler_s : register(s4);
SamplerState g_cloudLayer0Sampler_s : register(s5);
SamplerState g_cloudLayer1Sampler_s : register(s6);
SamplerState g_cloudLayerMaskSampler_s : register(s7);
SamplerState g_panoramicBlendSampler_s : register(s8);
SamplerState g_panoramicAlphaBlendSampler_s : register(s9);
Texture2D<float4> g_skyGradientTexture : register(t0);
Texture2D<float4> g_acosLUTTexture : register(t1);
Texture2D<float4> g_skyGradientBlendTexture : register(t2);
Texture2D<float3> g_panoramicTexture : register(t3);
Texture2D<float> g_panoramicAlphaTexture : register(t4);
Texture2D<float> g_cloudLayer0Texture : register(t5);
Texture2D<float> g_cloudLayer1Texture : register(t6);
Texture2D<float> g_cloudLayerMaskTexture : register(t7);
Texture2D<float3> g_panoramicBlendTexture : register(t8);
Texture2D<float> g_panoramicAlphaBlendTexture : register(t9);
Texture2DMS<float4,4> g_depthTexture : register(t10);
Texture2D<float> g_msaaClassifyTexture : register(t11);

Texture2D<float4> StereoParams : register(t125);

void main(
  float4 v0 : SV_Position0,
  float3 v1 : TEXCOORD0,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17;
  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.x = dot(v1.xyz, v1.xyz);
  r0.x = rsqrt(r0.x);
  r0.xyz = v1.xyz * r0.xxx;
  r1.xy = (int2)v0.xy;
  r2.xy = g_screenSizeInv.xy * v0.xy;
  r0.w = abs(r0.z) < abs(r0.x);
  if (r0.w != 0) {
    r0.w = r0.z;
    r2.z = r0.x;
    r2.w = 1.000000000e+000;
  } else {
    r0.w = r0.x;
    r2.z = r0.z;
    r2.w = 3.000000000e+000;
  }
  r3.x = dot(r0.xz, float2(1.000000e+000,-1.000000e+000));
  r3.x = 0.000000000e+000 < r3.x;
  if (r3.x != 0) {
    r2.w = 4.000000000e+000 + r2.w;
    r3.x = -1.000000000e+000;
  } else {
    r3.x = 1.000000000e+000;
  }
  r0.w = r0.w / abs(r2.z);
  r2.z = r0.w * r0.w;
  r2.z = r2.z * 2.000000030e-001 + 8.000000119e-001;
  r0.w = r0.w / r2.z;
  r0.w = r0.w * r3.x + r2.w;
  r0.w = r0.w * 1.250000000e-001 + 1.250000000e-001;
  r0.w = frac(r0.w);
  r3.x = 1.000000000e+000 + -r0.w;
  r0.w = r0.y * 4.990234375e-001 + 5.009765625e-001;
  r4.x = frac(r0.w);
  r4.yw = float2(5.000000e-001,1.000000e+000);
  r0.w = g_acosLUTTexture.SampleLevel(g_acosLUTSampler_s, r4.xy, 0.000000000e+000).x;
  r3.y = -r0.w;
  r4.z = g_panoramicRotation;
  r2.zw = r4.zw + r3.xy;
  r4.xyzw = g_skyGradientTexture.SampleLevel(g_skyGradientSampler_s, r2.zw, 0.000000000e+000).xyzw;
  r5.x = g_panoramicBlendRotation;
  r5.y = 1.000000000e+000;
  r2.zw = r5.xy + r3.xy;
  r5.xyzw = g_skyGradientBlendTexture.SampleLevel(g_skyGradientBlendSampler_s, r2.zw, 0.000000000e+000).xyzw;
  r3.z = 1.000000000e+000 + r3.y;
  r3.zw = saturate(r3.xz * g_panoramicUVRanges.xy + g_panoramicUVRanges.zw);
  r0.w = g_panoramicRotation + r3.z;
  r3.y = g_panoramicTileFactor * r0.w;
  r6.xyz = g_panoramicTexture.SampleLevel(g_panoramicSampler_s, r3.yw, 0.000000000e+000).xyz;
  r0.w = g_panoramicAlphaTexture.SampleLevel(g_panoramicAlphaSampler_s, r3.yw, 0.000000000e+000).x;
  r7.z = g_cloudLayerMaskTexture.SampleLevel(g_cloudLayerMaskSampler_s, r3.yw, 0.000000000e+000).x;
  r2.z = g_panoramicBlendRotation + r3.z;
  r3.x = g_panoramicTileFactor * r2.z;
  r8.xyz = g_panoramicBlendTexture.SampleLevel(g_panoramicBlendSampler_s, r3.xw, 0.000000000e+000).xyz;
  r2.z = g_panoramicAlphaBlendTexture.SampleLevel(g_panoramicAlphaBlendSampler_s, r3.xw, 0.000000000e+000).x;
  r2.w = dot(g_sunDirAndSkyGradientScale.xyz, r0.xyz);
  r3.x = 0.000000000e+000 >= r0.y;
  if (r3.x != 0) {
    r3.xyzw = float4(1.274200e+007,1.274200e+007,1.274200e+007,1.274200e+007) + g_cloudLayerParams.xxyy;
    r3.xyzw = -g_cloudLayerParams.xxyy * r3.xyzw;
    r6.w = 6.371000000e+006 * r0.y;
    r3.xyzw = r6.wwww * r6.wwww + -r3.xyzw;
    r3.xyzw = sqrt(r3.xyzw);
    r3.xyzw = r0.yyyy * float4(6.371000e+006,6.371000e+006,6.371000e+006,6.371000e+006) + r3.xyzw;
    r3.xyzw = -abs(-r3.zwxy) * r0.xzxz;
    r0.xy = r3.zw;
    r0.z = 1.000000000e+000;
    r9.x = dot(r0.xyz, g_cloudLayer0UVTransform0.xyz);
    r9.y = dot(r0.xyz, g_cloudLayer0UVTransform1.xyz);
    r3.z = 1.000000000e+000;
    r0.x = dot(r3.xyz, g_cloudLayer1UVTransform0.xyz);
    r0.y = dot(r3.xyz, g_cloudLayer1UVTransform1.xyz);
  } else {
    r9.xy = float2(0.000000e+000,0.000000e+000);
    r0.xy = float2(0.000000e+000,0.000000e+000);
  }
  r0.z = g_cloudLayer0Texture.Sample(g_cloudLayer0Sampler_s, r9.xy).x;
  r0.x = g_cloudLayer1Texture.Sample(g_cloudLayer1Sampler_s, r0.xy).x;
  r0.xy = saturate(g_cloudLayerParams.wz * r0.xz);
  r0.z = saturate(r2.w * 5.000000000e-001 + 5.000000000e-001);
  r0.z = log2(r0.z);
  r3.xy = g_cloudLayerLightingParams0.zw * r0.zz;
  r3.xy = exp2(r3.xy);
  r3.xy = g_cloudLayerLightingParams0.xy * r3.xy;
  r3.xy = exp2(r3.xy);
  r3.xy = float2(-1.000000e+000,-1.000000e+000) + r3.xy;
  r3.xzw = r3.xxx * g_cloudLayerSunColor.xyz + g_cloudLayerLightingParams1.xxx;
  r9.xyz = r3.yyy * g_cloudLayerSunColor.xyz + g_cloudLayerLightingParams1.yyy;
  r0.z = 1.000000000e+000 + -g_sunColorAndSize.w;
  r3.y = -r0.z * r0.z + 1.000000000e+000;
  r3.y = max(r3.y, 0.000000000e+000);
  r3.y = r3.y * r3.y;
  r6.w = r0.z * r0.z + 1.000000000e+000;
  r0.z = r0.z + r0.z;
  r0.z = saturate(-r0.z * r2.w + r6.w);
  r0.z = log2(r0.z);
  r0.z = 1.500000000e+000 * r0.z;
  r0.z = exp2(r0.z);
  r0.z = 1.256637096e+001 * r0.z;
  r0.z = r3.y / r0.z;
  r0.z = min(r0.z, 1.000000000e+000);
  r10.xyz = g_sunColorAndSize.xyz * r0.zzz;
  r11.xyzw = -r5.xyzw + r4.xyzw;
  r11.xyzw = g_skyGradientBlendFactor * r11.xyzw + r5.xyzw;
  r1.zw = float2(0.000000e+000,0.000000e+000);
  r0.z = g_msaaClassifyTexture.Load(r1.xyw).x;
  r0.z = 0.000000000e+000 < r0.z;
  r2.w = 1.000000000e+000 + -r0.y;
  r3.y = 1.000000000e+000 + -r0.x;
  r7.y = r3.y * r2.w;
  r7.w = r7.y * r7.z;
  r7.x = 1.000000000e+000;
  r2.w = dot(r7.xyzw, g_alphaSelector.xyzw);
  r2.w = 1.000000000e+000 + -r2.w;
  r7.xyw = r0.zzz ? float3(4.000000e+000,4.000000e+000,2.500000e-001) : float3(1.000000e+000,1.000000e+000,1.000000e+000);
  r2.xy = r2.xy * float2(2.000000e+000,2.000000e+000) + float2(-1.000000e+000,-1.000000e+000);
  r12.xy = g_fogParams.zw / r11.ww;
  r13.xy = float2(1.000000e+000,-1.000000e+000) * r2.xy;
  r13.w = 1.000000000e+000;
  r14.w = 1.000000000e+000;
  r15.w = 1.000000000e+000;
  r16.xyzw = float4(0.000000e+000,0.000000e+000,0.000000e+000,0.000000e+000);
  r0.z = 0.000000000e+000;
  r2.x = 0.000000000e+000;
  r2.y = r7.y;
  r3.y = 0.000000000e+000;
  while (true) {
    r4.w = (int)r3.y >= (int)r7.x;
    if (r4.w != 0) break;
    r13.z = g_depthTexture.Load(r1.xyz,r3.y).x;
    r4.w = r13.z == 1.000000;
    r5.w = r4.w ? -r2.w : -1.000000000e+000;
    r2.y = r5.w + r2.y;
    if (r4.w != 0) {
      r2.x = r2.x + r7.w;
    } else {
      r0.z = 1.000000000e+000 + r0.z;
      r17.x = dot(r13.xyzw, g_invViewProjMatrix._m00_m10_m20_m30);
      r17.y = dot(r13.xyzw, g_invViewProjMatrix._m01_m11_m21_m31);
      r17.z = dot(r13.xyzw, g_invViewProjMatrix._m02_m12_m22_m32);
      r4.w = dot(r13.xyzw, g_invViewProjMatrix._m03_m13_m23_m33);
      r17.xyz = r17.xyz / r4.www;

float4 r28, r26, r27;
r26.xyz = r17.xyz;
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);
r17.xyz = r26.xyz;


      r17.xzw = g_camPos.xyz + -r17.xyz;
      r4.w = dot(r17.xzw, r17.xzw);
      r4.w = sqrt(r4.w);
      r15.xz = saturate(r4.ww * r12.xy + g_fogParams.xy);
      r12.zw = r15.xz * r15.xz;
      r17.xz = r15.xz * r12.zw;
      r14.x = r17.x;
      r14.y = r12.z;
      r14.z = r15.x;
      r5.w = saturate(dot(r14.xyzw, g_fogCoefficients.xyzw));
      r15.x = r17.z;
      r15.y = r12.w;
      r6.w = saturate(dot(r15.xyzw, g_fogColorCoefficients.xyzw));
      r8.w = -g_camPos.y + r17.y;
      r9.w = abs(r8.w) < 9.999999776e-003;
      r8.w = r9.w ? 9.999999776e-003 : r8.w;
      r9.w = r8.w * g_heightFogCoefficients.x + g_heightFogCoefficients.y;
      r9.w = exp2(r9.w);
      r9.w = 1.000000000e+000 + r9.w;
      r9.w = g_heightFogCoefficients.z / r9.w;
      r9.w = -g_heightFogCoefficients.w + r9.w;
      r8.w = r9.w / r8.w;
      r4.w = -g_fogStartDistance + r4.w;
      r4.w = max(r4.w, 0.000000000e+000);
      r4.w = r8.w * r4.w;
      r4.w = exp2(-abs(-r4.w));
      r4.w = 1.000000000e+000 + -r4.w;
      r5.w = max(r4.w, r5.w);
      r4.w = max(r4.w, r6.w);
      r14.xyz = g_fogColor.xyz * r4.www;
      r15.xyz = r11.xyz * g_sunDirAndSkyGradientScale.www + -r14.xyz;
      r17.xyz = r5.www * r15.xyz + r14.xyz;
      r6.w = 1.000000000e+000 + -r5.w;
      r17.w = r6.w * r4.w + r5.w;
      r16.xyzw = r17.xyzw + r16.xyzw;
    }
    r3.y = (int)r3.y + 1;
  }
  r1.x = 1.000000000e+000 < r0.z;
  if (r1.x != 0) {
    r16.xyzw = r16.xyzw / r0.zzzz;
  }
  r1.w = r2.y * r7.w;
  r6.xyz = r6.xyz + -r4.xyz;
  r4.xyz = r0.www * r6.xyz + r4.xyz;
  r6.xyz = r8.xyz + -r5.xyz;
  r2.yzw = r2.zzz * r6.xyz + r5.xyz;
  r4.xyz = r4.xyz + -r2.yzw;
  r2.yzw = g_skyGradientBlendFactor * r4.xyz + r2.yzw;
  r2.yzw = r2.yzw * g_sunDirAndSkyGradientScale.www + r10.xyz;
  r0.xy = r0.xy * r7.zz;
  r3.xyz = r3.xzw * g_cloudLayer0Color.xyz + -r2.yzw;
  r0.yzw = r0.yyy * r3.xyz + r2.yzw;
  r2.yzw = r9.xyz * g_cloudLayer1Color.xyz + -r0.yzw;
  r1.xyz = r0.xxx * r2.yzw + r0.yzw;
  r0.xyzw = r1.xyzw + -r16.xyzw;
  r0.xyzw = r2.xxxx * r0.xyzw + r16.xyzw;
  o0.xyz = g_hdrExposureMultiplier * r0.xyz;
  o0.w = r0.w;
  return;
}
