// Indoor lighting (AA) - FIXED

cbuffer static_globals : register(b0)
{
  row_major float3x4 m_V : packoffset(c0);
  row_major float4x4 m_P : packoffset(c3);
  row_major float4x4 m_VP : packoffset(c7);
  float4 timers : packoffset(c11);
  float4 fog_plane : packoffset(c12);
  float4 fog_params : packoffset(c13);
  float4 fog_color : packoffset(c14);
  float4 L_ambient : packoffset(c15);
  float3 L_sun_color : packoffset(c16);
  float3 L_sun_dir_w : packoffset(c17);
  float4 L_hemi_color : packoffset(c18);
  float3 eye_position : packoffset(c19);
  float4 pos_decompression_params : packoffset(c20);
  float4 pos_decompression_params2 : packoffset(c21);
}

cbuffer dynamic_light : register(b1)
{
  float4 Ldynamic_color : packoffset(c0);
  float4 Ldynamic_pos : packoffset(c1);
  float4 Ldynamic_dir : packoffset(c2);
}

SamplerState smp_material_s : register(s0);
Texture2DMS<float4,4> s_position : register(t0);
Texture3D<float4> s_material : register(t1);


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


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

  r0.xy = v1.xy * pos_decompression_params.zw + -pos_decompression_params.xy;
  r1.xy = (int2)v1.xy;
  r1.zw = float2(0,0);
  r1.xyzw = s_position.Load(r1.xy, 0).xyzw;
  r0.xy = r1.zz * r0.xy;

float4 stereo = StereoParams.Load(0);
float4 params = IniParams.Load(0);
r0.x -= stereo.x * (r1.z - stereo.y)/m_P._m00;
  r0.z = r1.z;
  r2.xyz = -Ldynamic_pos.xyz + r0.xyz;
  r0.w = dot(r2.xyz, r2.xyz);
  r1.z = rsqrt(r0.w);
  r0.w = saturate(-r0.w * Ldynamic_pos.w + 1);
  r2.xyz = r2.xyz * r1.zzz;
  r1.z = dot(r0.xyz, r0.xyz);
  r1.z = rsqrt(r1.z);
  r0.xyz = -r0.xyz * r1.zzz + -r2.xyz;
  r1.z = dot(r0.xyz, r0.xyz);
  r1.z = rsqrt(r1.z);
  r0.xyz = r1.zzz * r0.xyz;
  r3.x = abs(r1.y) * 2 + -1;
  r1.z = -r3.x * r3.x + 1;
  r1.z = -r1.x * r1.x + r1.z;
  r1.z = sqrt(abs(r1.z));
  r1.y = cmp(r1.y < 0);
  r1.y = r1.y ? -1 : 1;
  r3.y = r1.y * r1.z;
  r3.z = r1.x;
  r0.y = dot(r0.xyz, r3.xyz);
  r0.x = dot(-r2.xyz, r3.xyz);
  r1.x = (int)r1.w & 0x80000000;
  if (4 == 0) r1.y = 0; else if (4+21 < 32) {   r1.y = (int)r1.w << (32-(4 + 21)); r1.y = (uint)r1.y >> (32-4);  } else r1.y = (uint)r1.w >> 21;
  r1.x = r1.x ? 16 : 0;
  bitmask.x = ((~(-1 << 4)) << 0) & 0xffffffff;
  r1.x = (((uint)r1.y << 0) & bitmask.x) | ((uint)r1.x & ~bitmask.x);
  r1.x = (uint)r1.x;
  r0.z = 0.0430107526 * r1.x;
  r0.xy = s_material.Sample(smp_material_s, r0.xyz).xy;
  r0.xyzw = r0.wwww * r0.xxxy;
  o0.xyzw = Ldynamic_color.xyzw * r0.xyzw;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.27.952.3022
//
//   using 3Dmigoto v1.2.29 on Wed Apr 13 13:46:20 2016
//
//
// Buffer Definitions: 
//
// cbuffer static_globals
// {
//
//   row_major float3x4 m_V;            // Offset:    0 Size:    48 [unused]
//   row_major float4x4 m_P;            // Offset:   48 Size:    64 [unused]
//   row_major float4x4 m_VP;           // Offset:  112 Size:    64 [unused]
//   float4 timers;                     // Offset:  176 Size:    16 [unused]
//   float4 fog_plane;                  // Offset:  192 Size:    16 [unused]
//   float4 fog_params;                 // Offset:  208 Size:    16 [unused]
//   float4 fog_color;                  // Offset:  224 Size:    16 [unused]
//   float4 L_ambient;                  // Offset:  240 Size:    16 [unused]
//   float3 L_sun_color;                // Offset:  256 Size:    12 [unused]
//   float3 L_sun_dir_w;                // Offset:  272 Size:    12 [unused]
//   float4 L_hemi_color;               // Offset:  288 Size:    16 [unused]
//   float3 eye_position;               // Offset:  304 Size:    12 [unused]
//   float4 pos_decompression_params;   // Offset:  320 Size:    16
//   float4 pos_decompression_params2;  // Offset:  336 Size:    16 [unused]
//
// }
//
// cbuffer dynamic_light
// {
//
//   float4 Ldynamic_color;             // Offset:    0 Size:    16
//   float4 Ldynamic_pos;               // Offset:   16 Size:    16
//   float4 Ldynamic_dir;               // Offset:   32 Size:    16 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// smp_material                      sampler      NA          NA    0        1
// s_position                        texture  float4       2dMS4    0        1
// s_material                        texture  float4          3d    1        1
// static_globals                    cbuffer      NA          NA    0        1
// dynamic_light                     cbuffer      NA          NA    1        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD                 0   xyzw        0     NONE   float       
// SV_Position              0   xyzw        1      POS   float   xy  
//
//
// 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 cb0[21], immediateIndexed
dcl_constantbuffer cb1[2], immediateIndexed
dcl_sampler s0, mode_default
dcl_resource_texture2dms(4) (float,float,float,float) t0
dcl_resource_texture3d (float,float,float,float) t1
dcl_input_ps_siv linear noperspective v1.xy, position
dcl_output o0.xyzw
dcl_temps 4
mad r0.xy, v1.xyxx, cb0[20].zwzz, -cb0[20].xyxx
ftoi r1.xy, v1.xyxx
mov r1.zw, l(0,0,0,0)
ldms_indexable(texture2dms)(float,float,float,float) r1.xyzw, r1.xyzw, t0.xyzw, l(0)
mul r0.xy, r0.xyxx, r1.zzzz
mov r0.z, r1.z
add r2.xyz, r0.xyzx, -cb1[1].xyzx
dp3 r0.w, r2.xyzx, r2.xyzx
rsq r1.z, r0.w
mad_sat r0.w, -r0.w, cb1[1].w, l(1.000000)
mul r2.xyz, r1.zzzz, r2.xyzx
dp3 r1.z, r0.xyzx, r0.xyzx
rsq r1.z, r1.z
mad r0.xyz, -r0.xyzx, r1.zzzz, -r2.xyzx
dp3 r1.z, r0.xyzx, r0.xyzx
rsq r1.z, r1.z
mul r0.xyz, r0.xyzx, r1.zzzz
mad r3.x, |r1.y|, l(2.000000), l(-1.000000)
mad r1.z, -r3.x, r3.x, l(1.000000)
mad r1.z, -r1.x, r1.x, r1.z
sqrt r1.z, |r1.z|
lt r1.y, r1.y, l(0.000000)
movc r1.y, r1.y, l(-1.000000), l(1.000000)
mul r3.y, r1.z, r1.y
mov r3.z, r1.x
dp3 r0.y, r0.xyzx, r3.xyzx
dp3 r0.x, -r2.xyzx, r3.xyzx
and r1.x, r1.w, l(0x80000000)
ubfe r1.y, l(4), l(21), r1.w
movc r1.x, r1.x, l(16), l(0)
bfi r1.x, l(4), l(0), r1.y, r1.x
utof r1.x, r1.x
mul r0.z, r1.x, l(0.0430107526)
sample_indexable(texture3d)(float,float,float,float) r0.xy, r0.xyzx, t1.xyzw, s0
mul r0.xyzw, r0.xxxy, r0.wwww
mul o0.xyzw, r0.xyzw, cb1[0].xyzw
ret 
// Approximately 37 instruction slots used

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