// Lights

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 cPSLight : register(b3)
{

  struct
  {
    float4 m_lightDir0;
    float4 m_lightColor0;
    float4 m_lightColor1;
    float4 m_lightSourcePos;
    float4 m_lightSourceColor;
    float4 m_lightSourceDir;
    float4 m_lightSourceParam1;
    float4 m_lightSourceParam2;
    float4 m_lightSourceParam3;
    float4 m_lightPos0;
    float4 m_lightParam2;
  } g_psLight : packoffset(c0);

}

cbuffer cPSMaterial : register(b4)
{

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

}

SamplerState g_samplerPoint_Wrap_s : register(s8);
SamplerState g_samplerPoint_Clamp_s : register(s9);
SamplerState g_samplerLinear_Wrap_s : register(s10);
Texture2D<float4> g_NormalTexture : register(t0);
Texture2D<float4> g_SpecularTexture : register(t1);
Texture2D<float4> g_DepthTexture : register(t2);
Texture2D<float4> g_MaskTexture : register(t4);
Texture2D<float4> g_MaterialTexture : register(t10);


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


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

  r0.xy = v2.xy / v2.ww;
  r1.xyz = g_NormalTexture.Sample(g_samplerPoint_Wrap_s, r0.xy).xyz;
  r0.z = dot(r1.zz, r1.zz);
  r1.xy = r1.xy * float2(2,2) + float2(-1,-1);
  r2.z = -1 + r0.z;
  r0.z = -r2.z * r2.z + 1;
  r0.w = dot(r1.xy, r1.xy);
  r1.xy = r1.xy * r0.zz;
  r0.z = r0.z * r0.w + 1.00000001e-007;
  r0.z = rsqrt(r0.z);
  r2.xy = r1.xy * r0.zz;
  r0.zw = v1.xy / v1.ww;
  r0.zw = -g_psScene.m_cameraCenterOffset.xy + r0.zw;
  r0.zw = g_psScene.m_projectionParam.xy * r0.zw;
  r1.x = g_DepthTexture.Sample(g_samplerPoint_Wrap_s, r0.xy).x;
  r1.yw = g_SpecularTexture.Sample(g_samplerPoint_Wrap_s, r0.xy).zw;
  r0.x = -g_psScene.m_projectionParam.w + r1.x;
  r3.z = g_psScene.m_projectionParam.z / r0.x;
  r3.xy = r3.zz * r0.zw;
  
  float4 stereo = StereoParams.Load(0);
  r3.x -= stereo.x * (r3.z - stereo.y) / g_psScene.m_projection._m00;
  
  r0.xyz = g_psLight.m_lightSourcePos.xyz + -r3.xyz;
  r0.w = dot(r0.xyz, r0.xyz);
  r1.x = rsqrt(r0.w);
  r0.xyz = r1.xxx * r0.xyz;
  r1.x = dot(r0.xyz, r2.xyz);
  r0.x = dot(g_psLight.m_lightSourceDir.xyz, -r0.xyz);
  r0.x = -g_psLight.m_lightSourceParam2.x + r0.x;
  r0.x = saturate(g_psLight.m_lightSourceParam2.y * r0.x);
  r0.x = log2(r0.x);
  r0.x = g_psLight.m_lightSourceParam2.z * r0.x;
  r0.x = exp2(r0.x);
  r0.y = -r1.x * r1.x + 1;
  r0.y = r0.y * r1.w + r1.x;
  r1.z = 0.25;
  r0.z = g_MaterialTexture.Sample(g_samplerPoint_Clamp_s, r1.yz).w;
  r0.z = r1.w * r0.z;
  r0.y = saturate(max(r0.y, r0.z));
  r0.z = 1 / r0.w;
  r0.z = -g_psLight.m_lightSourceParam1.w * r0.w + r0.z;
  r0.z = min(g_psMaterial.m_materials[7].w, r0.z);
  r0.z = max(0, r0.z);
  r1.xyz = g_psLight.m_lightSourceColor.xyz * r0.zzz;
  r0.xzw = r1.xyz * r0.xxx;
  r0.xzw = g_psScene.m_exposure.zzz * r0.xzw;
  r0.xyz = r0.yyy * r0.xzw;
  r0.xyz = g_psLight.m_lightSourceParam3.yyy * r0.xyz;
  r3.w = 1;
  r1.x = dot(r3.xyzw, g_psScene.m_shadowProjection2._m00_m10_m20_m30);
  r1.y = dot(r3.xyzw, g_psScene.m_shadowProjection2._m01_m11_m21_m31);
  r0.w = dot(r3.xyzw, g_psScene.m_shadowProjection2._m03_m13_m23_m33);
  r1.xy = r1.xy / r0.ww;
  r1.xy = r1.xy * float2(0.5,0.5) + float2(0.5,0.5);
  r1.z = 1 + -r1.y;
  r1.xyz = g_MaskTexture.SampleLevel(g_samplerLinear_Wrap_s, r1.xz, 0).xyz;
  o0.xyz = r1.xyz * r0.xyz;
  o0.w = 1;
  o1.xyzw = float4(0,0,0,1);
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.2.25 on Wed Jan 20 23:45:22 2016
//
//
// 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 cPSLight
// {
//
//   struct PSLight
//   {
//       
//       float4 m_lightDir0;            // Offset:    0
//       float4 m_lightColor0;          // Offset:   16
//       float4 m_lightColor1;          // Offset:   32
//       float4 m_lightSourcePos;       // Offset:   48
//       float4 m_lightSourceColor;     // Offset:   64
//       float4 m_lightSourceDir;       // Offset:   80
//       float4 m_lightSourceParam1;    // Offset:   96
//       float4 m_lightSourceParam2;    // Offset:  112
//       float4 m_lightSourceParam3;    // Offset:  128
//       float4 m_lightPos0;            // Offset:  144
//       float4 m_lightParam2;          // Offset:  160
//
//   } g_psLight;                       // Offset:    0 Size:   176
//
// }
//
// cbuffer cPSMaterial
// {
//
//   struct PSMaterial
//   {
//       
//       float4 m_materials[8];         // Offset:    0
//
//   } g_psMaterial;                    // Offset:    0 Size:   128
//
// }
//
//
// 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_NormalTexture                   texture  float4          2d    0        1
// g_SpecularTexture                 texture  float4          2d    1        1
// g_DepthTexture                    texture  float4          2d    2        1
// g_MaskTexture                     texture  float4          2d    4        1
// g_MaterialTexture                 texture  float4          2d   10        1
// cPSScene                          cbuffer      NA          NA    2        1
// cPSLight                          cbuffer      NA          NA    3        1
// cPSMaterial                       cbuffer      NA          NA    4        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float       
// TEXCOORD                 0   xyzw        1     NONE   float   xy w
// TEXCOORD                 1   xyzw        2     NONE   float   xy w
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
// SV_Target                1   xyzw        1   TARGET   float   xyzw
//
ps_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb2[29], immediateIndexed
dcl_constantbuffer cb3[9], immediateIndexed
dcl_constantbuffer cb4[8], immediateIndexed
dcl_sampler s8, mode_default
dcl_sampler s9, mode_default
dcl_sampler s10, mode_default
dcl_resource_texture2d (float,float,float,float) t0
dcl_resource_texture2d (float,float,float,float) t1
dcl_resource_texture2d (float,float,float,float) t2
dcl_resource_texture2d (float,float,float,float) t4
dcl_resource_texture2d (float,float,float,float) t10
dcl_input_ps linear v1.xyw
dcl_input_ps linear v2.xyw
dcl_output o0.xyzw
dcl_output o1.xyzw
dcl_temps 4
div r0.xy, v2.xyxx, v2.wwww
sample_indexable(texture2d)(float,float,float,float) r1.xyz, r0.xyxx, t0.xyzw, s8
dp2 r0.z, r1.zzzz, r1.zzzz
mad r1.xy, r1.xyxx, l(2.000000, 2.000000, 0.000000, 0.000000), l(-1.000000, -1.000000, 0.000000, 0.000000)
add r2.z, r0.z, l(-1.000000)
mad r0.z, -r2.z, r2.z, l(1.000000)
dp2 r0.w, r1.xyxx, r1.xyxx
mul r1.xy, r0.zzzz, r1.xyxx
mad r0.z, r0.z, r0.w, l(1.000000012E-007)
rsq r0.z, r0.z
mul r2.xy, r0.zzzz, r1.xyxx
div r0.zw, v1.xxxy, v1.wwww
add r0.zw, r0.zzzw, -cb2[28].xxxy
mul r0.zw, r0.zzzw, cb2[21].xxxy
sample_indexable(texture2d)(float,float,float,float) r1.x, r0.xyxx, t2.xyzw, s8
sample_indexable(texture2d)(float,float,float,float) r1.yw, r0.xyxx, t1.yzxw, s8
add r0.x, r1.x, -cb2[21].w
div r3.z, cb2[21].z, r0.x
mul r3.xy, r0.zwzz, r3.zzzz
add r0.xyz, -r3.xyzx, cb3[3].xyzx
dp3 r0.w, r0.xyzx, r0.xyzx
rsq r1.x, r0.w
mul r0.xyz, r0.xyzx, r1.xxxx
dp3 r1.x, r0.xyzx, r2.xyzx
dp3 r0.x, cb3[5].xyzx, -r0.xyzx
add r0.x, r0.x, -cb3[7].x
mul_sat r0.x, r0.x, cb3[7].y
log r0.x, r0.x
mul r0.x, r0.x, cb3[7].z
exp r0.x, r0.x
mad r0.y, -r1.x, r1.x, l(1.000000)
mad r0.y, r0.y, r1.w, r1.x
mov r1.z, l(0.250000)
sample_indexable(texture2d)(float,float,float,float) r0.z, r1.yzyy, t10.xywz, s9
mul r0.z, r0.z, r1.w
max_sat r0.y, r0.z, r0.y
div r0.z, l(1.000000, 1.000000, 1.000000, 1.000000), r0.w
mad r0.z, -cb3[6].w, r0.w, r0.z
min r0.z, r0.z, cb4[7].w
max r0.z, r0.z, l(0.000000)
mul r1.xyz, r0.zzzz, cb3[4].xyzx
mul r0.xzw, r0.xxxx, r1.xxyz
mul r0.xzw, r0.xxzw, cb2[23].zzzz
mul r0.xyz, r0.xzwx, r0.yyyy
mul r0.xyz, r0.xyzx, cb3[8].yyyy
mov r3.w, l(1.000000)
dp4 r1.x, r3.xyzw, cb2[16].xyzw
dp4 r1.y, r3.xyzw, cb2[17].xyzw
dp4 r0.w, r3.xyzw, cb2[19].xyzw
div r1.xy, r1.xyxx, r0.wwww
mad r1.xy, r1.xyxx, l(0.500000, 0.500000, 0.000000, 0.000000), l(0.500000, 0.500000, 0.000000, 0.000000)
add r1.z, -r1.y, l(1.000000)
sample_l_indexable(texture2d)(float,float,float,float) r1.xyz, r1.xzxx, t4.xyzw, s10, l(0.000000)
mul o0.xyz, r0.xyzx, r1.xyzx
mov o0.w, l(1.000000)
mov o1.xyzw, l(0,0,0,1.000000)
ret 
// Approximately 57 instruction slots used

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