// Outdoor physical lighting (uneven lighting in first level)

cbuffer cPSScene : register(b2)
{

  struct
  {
    float4x4 m_projectionView;
    float4x4 m_projection;
    float4x4 m_view;
    float4x4 m_shadowProjection;
    float4x4 m_shadowProjection2;
    float4 m_eyepos;
    float4 m_projectionParam;
    float4 m_viewportSize;
    float4 m_exposure;
    float4 m_fogParam[3];
    float4 m_fogColor;
    float4 m_cameraCenterOffset;
    float4 m_shadowMapResolutions;
  } g_psScene : packoffset(c0);

}

cbuffer cPSMaterial : register(b4)
{

  struct
  {
    float4 m_materials[8];
  } g_psMaterial : packoffset(c0);

}

cbuffer cPSSystem : register(b0)
{

  struct
  {
    float4 m_param;
    float4 m_renderInfo;
    float4 m_renderBuffer;
    float4 m_dominantLightDir;
  } g_psSystem : packoffset(c0);

}

SamplerState g_samplerPoint_Wrap_s : register(s8);
SamplerState g_samplerPoint_Clamp_s : register(s9);
SamplerState g_samplerLinear_Wrap_s : register(s10);
SamplerState g_samplerLinear_Clamp_s : register(s11);
Texture2D<float4> inFloorNormalMap : register(t0);
TextureCube<float4> inReflectionCubeMap : register(t1);
Texture2D<float4> inSpecular : register(t3);
Texture2D<float4> inNormal : register(t4);
Texture2D<float4> inAlbedo : register(t8);
Texture2D<float4> inLightDiffuse : register(t9);
Texture2D<float4> inLightSpecular : register(t10);
Texture2D<float4> inDepth : register(t11);
Texture2D<float4> inFogVolume : register(t12);
Texture2D<float4> inOcclusion : register(t13);


// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
Texture2D<float4> StereoParams : register(t125);


void main( 
  float4 v0 : SV_Position0,
  float4 v1 : TEXCOORD0,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2,r3,r4,r5,r6;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xy = float2(-0.00390625,-0.00390625) + v0.xy;
  r0.xy = g_psSystem.m_renderBuffer.zw * r0.xy;
  r0.z = inDepth.SampleLevel(g_samplerPoint_Clamp_s, r0.xy, 0).x;
  r0.z = -g_psScene.m_projectionParam.w + r0.z;
  r0.z = g_psScene.m_projectionParam.z / r0.z;
  r1.xy = v1.xy / v1.ww;
  r1.xy = -g_psScene.m_cameraCenterOffset.xy + r1.xy;
  r0.w = inDepth.Sample(g_samplerPoint_Clamp_s, r0.xy).x;
  r0.w = -g_psScene.m_projectionParam.w + r0.w;
  r2.z = g_psScene.m_projectionParam.z / r0.w;
  r1.xy = g_psScene.m_projectionParam.xy * r1.xy;
  r2.xy = r1.xy * r2.zz;
  
  // Fix physical lighting on ground:
  float4 stereo = StereoParams.Load(0);
  r2.x -= stereo.x * (r2.z - stereo.y) / g_psScene.m_projection._m00;
  
  r2.w = 1;
  r1.x = dot(r2.xyzw, g_psScene.m_view._m00_m10_m20_m30);
  r1.y = dot(r2.xyzw, g_psScene.m_view._m01_m11_m21_m31);
  r1.z = dot(r2.xyzw, g_psScene.m_view._m02_m12_m22_m32);
  r0.w = cmp(r2.z >= g_psMaterial.m_materials[2].z);
  if (r0.w != 0) {
    r0.w = max(g_psMaterial.m_materials[2].z, r2.z);
    r0.w = min(g_psMaterial.m_materials[2].w, r0.w);
    r0.w = -g_psMaterial.m_materials[2].z + r0.w;
    r0.w = -r0.w * g_psMaterial.m_materials[4].y + 1;
  } else {
    r1.w = max(g_psMaterial.m_materials[2].x, r2.z);
    r1.w = min(g_psMaterial.m_materials[2].y, r1.w);
    r1.w = -g_psMaterial.m_materials[2].x + r1.w;
    r0.w = g_psMaterial.m_materials[4].x * r1.w;
  }
  r2.xy = g_psMaterial.m_materials[7].zw * r1.xz;
  r2.xy = r2.xy * g_psMaterial.m_materials[1].ww + g_psMaterial.m_materials[7].xy;
  r3.xyz = inNormal.Sample(g_samplerPoint_Clamp_s, r0.xy).xyz;
  r2.zw = r3.xy * float2(2,2) + float2(-1,-1);
  r1.w = dot(r3.zz, r3.zz);
  r3.z = -1 + r1.w;
  r1.w = -r3.z * r3.z + 1;
  r4.xy = r2.zw * r1.ww;
  r2.z = dot(r2.zw, r2.zw);
  r1.w = r1.w * r2.z + 1.00000001e-007;
  r1.w = rsqrt(r1.w);
  r3.xy = r4.xy * r1.ww;
  r4.x = dot(r3.xyz, g_psScene.m_view._m00_m10_m20);
  r4.y = dot(r3.xyz, g_psScene.m_view._m01_m11_m21);
  r4.z = dot(r3.xyz, g_psScene.m_view._m02_m12_m22);
  r3.xyz = inLightSpecular.Sample(g_samplerPoint_Wrap_s, r0.xy).xyz;
  r5.xyz = inLightDiffuse.Sample(g_samplerPoint_Wrap_s, r0.xy).xyz;
  r6.xyz = inAlbedo.Sample(g_samplerPoint_Clamp_s, r0.xy).xyz;
  r1.w = inOcclusion.Sample(g_samplerLinear_Clamp_s, r0.xy).x;
  r2.z = 1 + r4.y;
  r2.z = max(0.25, r2.z);
  r2.z = min(1, r2.z);
  r2.w = cmp(r2.z >= 1);
  r3.w = -0.25 + r2.z;
  r3.w = -r3.w * 1.33329999 + 1;
  r2.w = r2.w ? -0 : -r3.w;
  r2.z = r2.z + r2.w;
  r1.xyz = -g_psScene.m_eyepos.xyz + r1.xyz;
  r2.w = dot(r1.xyz, r1.xyz);
  r2.w = rsqrt(r2.w);
  r1.xyz = r2.www * r1.xyz;
  r3.xyz = r6.xyz * r5.xyz + r3.xyz;
  r2.x = inFloorNormalMap.Sample(g_samplerLinear_Wrap_s, r2.xy).y;
  r2.y = saturate(r4.y);
  r2.w = dot(r1.xyz, r4.xyz);
  r3.w = r2.w + r2.w;
  r1.xyz = r4.xyz * -r3.www + r1.xyz;
  r4.xyzw = inReflectionCubeMap.SampleBias(g_samplerLinear_Clamp_s, r1.xyz, -2).xyzw;
  r1.x = r2.x * r2.y;
  r5.xyz = r4.www + -r4.xyz;
  r1.xyz = r1.xxx * r5.xyz + r4.xyz;
  r1.xyz = r1.xyz * r1.xyz;
  r0.x = inSpecular.Sample(g_samplerLinear_Clamp_s, r0.xy).y;
  r1.xyz = r1.xyz * g_psMaterial.m_materials[1].xxx + g_psMaterial.m_materials[1].yyy;
  r0.y = 1 + -abs(r2.w);
  r0.y = r0.y * r0.y + 0.0299999993;
  r1.xyz = r1.xyz * r0.yyy;
  r0.y = r2.z * r0.w;
  r0.x = r0.y * r0.x;
  r0.xyw = saturate(r1.xyz * r0.xxx);
  r0.xyw = r0.xyw * r1.www;
  r0.xyw = r3.xyz * r1.www + r0.xyw;
  r0.z = log2(r0.z);
  r0.z = g_psScene.m_fogParam[2].y * r0.z;
  r0.z = r0.z * 0.693147182 + g_psScene.m_fogParam[2].z;
  r0.z = max(0, r0.z);
  r0.z = min(g_psScene.m_fogParam[2].w, r0.z);
  r1.xz = float2(0.0146484403,0.0146484403) * v1.xx + float2(0.015625,0.015625);
  r1.yw = g_psScene.m_fogParam[1].xx * v1.yy + g_psScene.m_fogParam[1].yy;
  r2.x = 1 + r0.z;
  r2.x = max(0, r2.x);
  r2.x = min(g_psScene.m_fogParam[2].w, r2.x);
  r2.y = floor(r0.z);
  r2.w = floor(r2.x);
  r2.xy = float2(0.03125,0.03125) * r2.yw;
  r2.zw = frac(r2.xy);
  r3.xz = g_psScene.m_fogParam[1].zz * r2.zw;
  r3.yw = floor(r2.xy);
  r2.xz = float2(32,32);
  r2.yw = g_psScene.m_fogParam[1].ww;
  r1.xyzw = r2.xyzw * r3.xyzw + r1.xyzw;
  r0.z = frac(r0.z);
  r2.xyzw = inFogVolume.Sample(g_samplerLinear_Wrap_s, r1.xy).xyzw;
  r1.xyzw = inFogVolume.Sample(g_samplerLinear_Wrap_s, r1.zw).xyzw;
  r3.x = 1 + -r0.z;
  r1.xyzw = r1.xyzw * r0.zzzz;
  r1.xyzw = r3.xxxx * r2.xyzw + r1.xyzw;
  r0.xyz = r0.xyw * r1.www + r1.xyz;
  r0.w = dot(r0.xyz, float3(0.212500006,0.715399981,0.0720999986));
  o0.w = 0.03125 * r0.w;
  r1.xyz = cmp(g_psMaterial.m_materials[0].yyy >= r0.xyz);
  r2.xyz = r1.xyz ? 1.000000 : 0;
  r1.xyz = r1.xyz ? float3(0,0,0) : float3(1,1,1);
  r3.xyz = g_psMaterial.m_materials[0].zzz + r0.xyz;
  r3.xyz = -g_psMaterial.m_materials[0].yyy + r3.xyz;
  r3.xyz = g_psMaterial.m_materials[0].xxx * r3.xyz;
  r3.xyz = float3(-1,-1,-1) / r3.xyz;
  r3.xyz = g_psMaterial.m_materials[0].zzz + r3.xyz;
  r3.xyz = g_psMaterial.m_materials[0].yyy + r3.xyz;
  r1.xyz = r3.xyz * r1.xyz;
  o0.xyz = r2.xyz * r0.xyz + r1.xyz;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//
// Buffer Definitions: 
//
// cbuffer cPSScene
// {
//
//   struct PSScene
//   {
//       
//       float4x4 m_projectionView;     // Offset:    0
//       float4x4 m_projection;         // Offset:   64
//       float4x4 m_view;               // Offset:  128
//       float4x4 m_shadowProjection;   // Offset:  192
//       float4x4 m_shadowProjection2;  // Offset:  256
//       float4 m_eyepos;               // Offset:  320
//       float4 m_projectionParam;      // Offset:  336
//       float4 m_viewportSize;         // Offset:  352
//       float4 m_exposure;             // Offset:  368
//       float4 m_fogParam[3];          // Offset:  384
//       float4 m_fogColor;             // Offset:  432
//       float4 m_cameraCenterOffset;   // Offset:  448
//       float4 m_shadowMapResolutions; // Offset:  464
//
//   } g_psScene;                       // Offset:    0 Size:   480
//
// }
//
// cbuffer cPSMaterial
// {
//
//   struct PSMaterial
//   {
//       
//       float4 m_materials[8];         // Offset:    0
//
//   } g_psMaterial;                    // Offset:    0 Size:   128
//
// }
//
// cbuffer cPSSystem
// {
//
//   struct PSSystem
//   {
//       
//       float4 m_param;                // Offset:    0
//       float4 m_renderInfo;           // Offset:   16
//       float4 m_renderBuffer;         // Offset:   32
//       float4 m_dominantLightDir;     // Offset:   48
//
//   } g_psSystem;                      // Offset:    0 Size:    64
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// g_samplerPoint_Wrap               sampler      NA          NA    8        1
// g_samplerPoint_Clamp              sampler      NA          NA    9        1
// g_samplerLinear_Wrap              sampler      NA          NA   10        1
// g_samplerLinear_Clamp             sampler      NA          NA   11        1
// inFloorNormalMap                  texture  float4          2d    0        1
// inReflectionCubeMap               texture  float4        cube    1        1
// inSpecular                        texture  float4          2d    3        1
// inNormal                          texture  float4          2d    4        1
// inAlbedo                          texture  float4          2d    8        1
// inLightDiffuse                    texture  float4          2d    9        1
// inLightSpecular                   texture  float4          2d   10        1
// inDepth                           texture  float4          2d   11        1
// inFogVolume                       texture  float4          2d   12        1
// inOcclusion                       texture  float4          2d   13        1
// cPSSystem                         cbuffer      NA          NA    0        1
// cPSScene                          cbuffer      NA          NA    2        1
// cPSMaterial                       cbuffer      NA          NA    4        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float   xy  
// TEXCOORD                 0   xyzw        1     NONE   float   xy w
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
//
ps_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb2[29], immediateIndexed
dcl_constantbuffer cb4[8], immediateIndexed
dcl_constantbuffer cb0[3], immediateIndexed
dcl_sampler s8, mode_default
dcl_sampler s9, mode_default
dcl_sampler s10, mode_default
dcl_sampler s11, mode_default
dcl_resource_texture2d (float,float,float,float) t0
dcl_resource_texturecube (float,float,float,float) t1
dcl_resource_texture2d (float,float,float,float) t3
dcl_resource_texture2d (float,float,float,float) t4
dcl_resource_texture2d (float,float,float,float) t8
dcl_resource_texture2d (float,float,float,float) t9
dcl_resource_texture2d (float,float,float,float) t10
dcl_resource_texture2d (float,float,float,float) t11
dcl_resource_texture2d (float,float,float,float) t12
dcl_resource_texture2d (float,float,float,float) t13
dcl_input_ps_siv linear noperspective v0.xy, position
dcl_input_ps linear v1.xyw
dcl_output o0.xyzw
dcl_temps 7
add r0.xy, v0.xyxx, l(-0.003906, -0.003906, 0.000000, 0.000000)
mul r0.xy, r0.xyxx, cb0[2].zwzz
sample_l_indexable(texture2d)(float,float,float,float) r0.z, r0.xyxx, t11.yzxw, s9, l(0.000000)
add r0.z, r0.z, -cb2[21].w
div r0.z, cb2[21].z, r0.z
div r1.xy, v1.xyxx, v1.wwww
add r1.xy, r1.xyxx, -cb2[28].xyxx
sample_indexable(texture2d)(float,float,float,float) r0.w, r0.xyxx, t11.yzwx, s9
add r0.w, r0.w, -cb2[21].w
div r2.z, cb2[21].z, r0.w
mul r1.xy, r1.xyxx, cb2[21].xyxx
mul r2.xy, r2.zzzz, r1.xyxx
mov r2.w, l(1.000000)
dp4 r1.x, r2.xyzw, cb2[8].xyzw
dp4 r1.y, r2.xyzw, cb2[9].xyzw
dp4 r1.z, r2.xyzw, cb2[10].xyzw
ge r0.w, r2.z, cb4[2].z
if_nz r0.w
  max r0.w, r2.z, cb4[2].z
  min r0.w, r0.w, cb4[2].w
  add r0.w, r0.w, -cb4[2].z
  mad r0.w, -r0.w, cb4[4].y, l(1.000000)
else 
  max r1.w, r2.z, cb4[2].x
  min r1.w, r1.w, cb4[2].y
  add r1.w, r1.w, -cb4[2].x
  mul r0.w, r1.w, cb4[4].x
endif 
mul r2.xy, r1.xzxx, cb4[7].zwzz
mad r2.xy, r2.xyxx, cb4[1].wwww, cb4[7].xyxx
sample_indexable(texture2d)(float,float,float,float) r3.xyz, r0.xyxx, t4.xyzw, s9
mad r2.zw, r3.xxxy, l(0.000000, 0.000000, 2.000000, 2.000000), l(0.000000, 0.000000, -1.000000, -1.000000)
dp2 r1.w, r3.zzzz, r3.zzzz
add r3.z, r1.w, l(-1.000000)
mad r1.w, -r3.z, r3.z, l(1.000000)
mul r4.xy, r1.wwww, r2.zwzz
dp2 r2.z, r2.zwzz, r2.zwzz
mad r1.w, r1.w, r2.z, l(0.000000)
rsq r1.w, r1.w
mul r3.xy, r1.wwww, r4.xyxx
dp3 r4.x, r3.xyzx, cb2[8].xyzx
dp3 r4.y, r3.xyzx, cb2[9].xyzx
dp3 r4.z, r3.xyzx, cb2[10].xyzx
sample_indexable(texture2d)(float,float,float,float) r3.xyz, r0.xyxx, t10.xyzw, s8
sample_indexable(texture2d)(float,float,float,float) r5.xyz, r0.xyxx, t9.xyzw, s8
sample_indexable(texture2d)(float,float,float,float) r6.xyz, r0.xyxx, t8.xyzw, s9
sample_indexable(texture2d)(float,float,float,float) r1.w, r0.xyxx, t13.yzwx, s11
add r2.z, r4.y, l(1.000000)
max r2.z, r2.z, l(0.250000)
min r2.z, r2.z, l(1.000000)
ge r2.w, r2.z, l(1.000000)
add r3.w, r2.z, l(-0.250000)
mad r3.w, -r3.w, l(1.333300), l(1.000000)
movc r2.w, r2.w, l(-0.000000), -r3.w
add r2.z, r2.w, r2.z
add r1.xyz, r1.xyzx, -cb2[20].xyzx
dp3 r2.w, r1.xyzx, r1.xyzx
rsq r2.w, r2.w
mul r1.xyz, r1.xyzx, r2.wwww
mad r3.xyz, r6.xyzx, r5.xyzx, r3.xyzx
sample_indexable(texture2d)(float,float,float,float) r2.x, r2.xyxx, t0.yxzw, s10
mov_sat r2.y, r4.y
dp3 r2.w, r1.xyzx, r4.xyzx
add r3.w, r2.w, r2.w
mad r1.xyz, r4.xyzx, -r3.wwww, r1.xyzx
sample_b_indexable(texturecube)(float,float,float,float) r4.xyzw, r1.xyzx, t1.xyzw, s11, l(-2.000000)
mul r1.x, r2.y, r2.x
add r5.xyz, -r4.xyzx, r4.wwww
mad r1.xyz, r1.xxxx, r5.xyzx, r4.xyzx
mul r1.xyz, r1.xyzx, r1.xyzx
sample_indexable(texture2d)(float,float,float,float) r0.x, r0.xyxx, t3.yxzw, s11
mad r1.xyz, r1.xyzx, cb4[1].xxxx, cb4[1].yyyy
add r0.y, -|r2.w|, l(1.000000)
mad r0.y, r0.y, r0.y, l(0.030000)
mul r1.xyz, r0.yyyy, r1.xyzx
mul r0.y, r0.w, r2.z
mul r0.x, r0.x, r0.y
mul_sat r0.xyw, r0.xxxx, r1.xyxz
mul r0.xyw, r1.wwww, r0.xyxw
mad r0.xyw, r3.xyxz, r1.wwww, r0.xyxw
log r0.z, r0.z
mul r0.z, r0.z, cb2[26].y
mad r0.z, r0.z, l(0.693147), cb2[26].z
max r0.z, r0.z, l(0.000000)
min r0.z, r0.z, cb2[26].w
mad r1.xz, l(0.014648, 0.000000, 0.014648, 0.000000), v1.xxxx, l(0.015625, 0.000000, 0.015625, 0.000000)
mad r1.yw, cb2[25].xxxx, v1.yyyy, cb2[25].yyyy
add r2.x, r0.z, l(1.000000)
max r2.x, r2.x, l(0.000000)
min r2.x, r2.x, cb2[26].w
round_ni r2.y, r0.z
round_ni r2.w, r2.x
mul r2.xy, r2.ywyy, l(0.031250, 0.031250, 0.000000, 0.000000)
frc r2.zw, r2.xxxy
mul r3.xz, r2.zzwz, cb2[25].zzzz
round_ni r3.yw, r2.xxxy
mov r2.xz, l(32.000000,0,32.000000,0)
mov r2.yw, cb2[25].wwww
mad r1.xyzw, r2.xyzw, r3.xyzw, r1.xyzw
frc r0.z, r0.z
sample_indexable(texture2d)(float,float,float,float) r2.xyzw, r1.xyxx, t12.xyzw, s10
sample_indexable(texture2d)(float,float,float,float) r1.xyzw, r1.zwzz, t12.xyzw, s10
add r3.x, -r0.z, l(1.000000)
mul r1.xyzw, r0.zzzz, r1.xyzw
mad r1.xyzw, r3.xxxx, r2.xyzw, r1.xyzw
mad r0.xyz, r0.xywx, r1.wwww, r1.xyzx
dp3 r0.w, r0.xyzx, l(0.212500, 0.715400, 0.072100, 0.000000)
mul o0.w, r0.w, l(0.031250)
ge r1.xyz, cb4[0].yyyy, r0.xyzx
and r2.xyz, r1.xyzx, l(0x3f800000, 0x3f800000, 0x3f800000, 0)
movc r1.xyz, r1.xyzx, l(0,0,0,0), l(1.000000,1.000000,1.000000,0)
add r3.xyz, r0.xyzx, cb4[0].zzzz
add r3.xyz, r3.xyzx, -cb4[0].yyyy
mul r3.xyz, r3.xyzx, cb4[0].xxxx
div r3.xyz, l(-1.000000, -1.000000, -1.000000, 0.000000), r3.xyzx
add r3.xyz, r3.xyzx, cb4[0].zzzz
add r3.xyz, r3.xyzx, cb4[0].yyyy
mul r1.xyz, r1.xyzx, r3.xyzx
mad o0.xyz, r2.xyzx, r0.xyzx, r1.xyzx
ret 
// Approximately 120 instruction slots used

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
