// Lights MSAA 2x

cbuffer _cbSharedPerViewData : register(b1)
{

  struct
  {
    row_major float4x4 mProjection;
    row_major float4x4 mViewToViewport;
    row_major float4x4 mViewToWorld;
    float4 vViewRemap;
    float4 vViewDepthRemap;
    float4 vEyeVectorUL;
    float4 vEyeVectorLR;
    float4 v4ShadowRange;
    float4 vViewSpaceUpVector;
    float4 vGlobalLitFactor;
    float4 vViewportSize;
    float4 vEngineTime;
    float4 vShaderColor0;
    float4 vShaderColor1;
    float4 vShaderColor2;
    float4 vShaderColor3;
    float4 vShaderColor4;
    float4 vClipPlane;
  } cbSharedPerViewData : packoffset(c0);

}

cbuffer _cbSharedLightData : register(b2)
{

  struct
  {
    float4 vParams0;
    float4 vParams1;
    float4 vParams2;
    float4 vLightDiffuse;
    float4 vHalfLightBoxFadeNeg;
    float4 vHalfLightBoxFadePos;
    float4 vRcpLightBoxFadeNeg;
    float4 vRcpLightBoxFadePos;
    float4 vLightPosView;
    float4 vLightDirView;
    float4 vProjectorTilingOffset;
    row_major float4x4 mViewToShadowProjTrans;
    row_major float4x4 mViewToLightProjTrans;
    float4 vLightParams;
    float4 vRestrictLightBoxVectorX;
    float4 vRestrictLightBoxVectorY;
    float4 vRestrictLightBoxVectorZ;
    float4 vRestrictLightBoxPosition;
  } cbSharedLightData : packoffset(c0);

}

SamplerState samplerLightModel_s : register(s0);
SamplerState samplerShadowColor0BBlack_s : register(s1);
SamplerState samplerLightCubeProjectorMap0_s : register(s2);
Texture2D<float4> mapLightModel : register(t0);
Texture2D<float4> mapShadowColor0BBlack : register(t1);
TextureCube<float4> mapLightCubeProjectorMap0 : register(t2);
Texture2DMS<float4,2> mapDeferred0 : register(t3);
Texture2DMS<float4,2> mapDeferred1 : register(t4);
Texture2DMS<float4,2> mapDeferred2 : register(t5);
Texture2DMS<float4,2> mapDeferred3 : register(t6);


// 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,
  uint v3 : SV_SampleIndex0,
  out float4 o0 : SV_TARGET0)
{
  float4 r0,r1,r2,r3,r4,r5;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xy = (int2)v0.xy;
  r0.zw = float2(0,0);
  r1.xyz = mapDeferred1.Load(r0.xy, v3.x).xyz;
  r1.xyz = float3(-0.5,-0.5,-0.5) + r1.xyz;
  r1.xyz = r1.xyz + r1.xyz;
  r1.w = dot(r1.xyz, r1.xyz);
  r1.w = rsqrt(r1.w);
  r1.xyz = r1.xyz * r1.www;
  r1.w = 1 / v1.w;
  r2.xyz = v2.xyz * r1.www;
  r1.w = mapDeferred0.Load(r0.xy, v3.x).x;
  r1.w = cbSharedPerViewData.vViewRemap.z * r1.w + -cbSharedPerViewData.vViewRemap.y;
  r1.w = 1 / r1.w;
  r3.xyz = -r1.www * r2.xyz + cbSharedLightData.vLightPosView.xyz;
  r2.xyz = r1.www * r2.xyz;
  
    float4 stereo = StereoParams.Load(0);
  r2.x -= stereo.x * (r1.w  - stereo.y) / cbSharedPerViewData.mProjection._m00;
  
  r3.x -= stereo.x * (r1.w  - stereo.y) / -cbSharedPerViewData.mProjection._m00;
  
  r1.w = dot(r3.xyz, r3.xyz);
  r2.w = rsqrt(r1.w);
  r1.w = sqrt(r1.w);
  r3.xyz = r3.xyz * r2.www;
  r2.w = dot(r1.xyz, r3.xyz);
  r3.w = r2.w + r2.w;
  r1.xyz = -r3.www * r1.xyz + r3.xyz;
  r3.x = dot(r3.xyz, cbSharedLightData.vLightDirView.xyz);
  r3.x = saturate(cbSharedLightData.vParams0.y * r3.x + cbSharedLightData.vParams0.x);
  r3.y = dot(-r2.xyz, -r2.xyz);
  r3.y = rsqrt(r3.y);
  r3.yzw = r3.yyy * -r2.xyz;
  r1.x = saturate(dot(r3.yzw, -r1.xyz));
  r4.xyzw = mapDeferred2.Load(r0.xy, v3.x).xyzw;
  r0.xyz = mapDeferred3.Load(r0.xy, v3.x).xyw;
  r1.y = r4.w;
  r1.y = dot(r1.xy, float2(0.248046875,0.75390625));
  r1.x = saturate(r2.w);
  r2.w = saturate(-r2.w);
  r5.xyzw = mapLightModel.Sample(samplerLightModel_s, r1.xy).xyzw;
  r0.w = r5.w * 1024 + -1000;
  r0.y = r0.y * r0.w;
  r0.y = exp2(r0.y);
  r0.w = dot(r4.xyz, float3(0.447058499,0.882354021,0.170587495));
  r1.xyz = r4.xyz * float3(2,2,2) + -r0.www;
  r0.w = 1 + -cbSharedLightData.vLightDiffuse.w;
  r0.z = r0.w * r0.z;
  r0.z = r0.z * r2.w;
  r1.xyz = r0.zzz * r1.xyz;
  r0.xyz = r0.xxx * r0.yyy + r1.xyz;
  r0.xyz = cbSharedLightData.vLightParams.xxx * r0.xyz;
  r0.xyz = r5.xyz * r4.xyz + r0.xyz;
  r1.xyz = cbSharedLightData.mViewToLightProjTrans._m10_m12_m11 * r2.yyy;
  r1.xyz = r2.xxx * cbSharedLightData.mViewToLightProjTrans._m00_m02_m01 + r1.xyz;
  r1.xyz = r2.zzz * cbSharedLightData.mViewToLightProjTrans._m20_m22_m21 + r1.xyz;
  r1.xyz = cbSharedLightData.mViewToLightProjTrans._m30_m32_m31 + r1.xyz;
  r2.xyz = cbSharedLightData.vHalfLightBoxFadeNeg.xyz * r1.xyz;
  r1.xyz = mapLightCubeProjectorMap0.Sample(samplerLightCubeProjectorMap0_s, r1.xzy).xyz;
  r2.xy = r2.xy / r2.zz;
  r2.xy = r2.xy * float2(0.5,0.5) + float2(0.5,0.5);
  r2.xy = mapShadowColor0BBlack.SampleLevel(samplerShadowColor0BBlack_s, r2.xy, 0).xy;
  r0.w = -r1.w * cbSharedLightData.vLightPosView.w + r2.x;
  r2.x = saturate(-r2.x * r2.x + r2.y);
  r2.x = 0.00039999999 + r2.x;
  r2.x = min(1, r2.x);
  r2.x = rsqrt(r2.x);
  r2.yz = saturate(cbSharedLightData.vParams1.yw * r1.ww + cbSharedLightData.vParams1.xz);
  r1.w = r2.y * r2.z;
  r2.yzw = cbSharedLightData.vLightDiffuse.xyz * r1.www;
  r1.xyz = r2.yzw * r1.xyz;
  r0.w = cbSharedLightData.vLightParams.y * r0.w;
  r0.w = saturate(r0.w * r2.x + 1);
  r0.w = -1 + r0.w;
  r0.w = r3.x * r0.w + 1;
  r1.w = 1 + -r0.w;
  r0.w = cbSharedLightData.vLightDiffuse.w * r1.w + r0.w;
  r1.xyz = r1.xyz * r0.www;
  o0.xyz = r1.xyz * r0.xyz;
  o0.w = 0;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.27.952.3022
//
//   using 3Dmigoto v1.2.19 on Fri Jan 15 18:37:04 2016
//
//
// Buffer Definitions: 
//
// cbuffer _cbSharedPerViewData
// {
//
//   struct
//   {
//       
//       row_major float4x4 mProjection;// Offset:    0
//       row_major float4x4 mViewToViewport;// Offset:   64
//       row_major float4x4 mViewToWorld;// Offset:  128
//       float4 vViewRemap;             // Offset:  192
//       float4 vViewDepthRemap;        // Offset:  208
//       float4 vEyeVectorUL;           // Offset:  224
//       float4 vEyeVectorLR;           // Offset:  240
//       float4 v4ShadowRange;          // Offset:  256
//       float4 vViewSpaceUpVector;     // Offset:  272
//       float4 vGlobalLitFactor;       // Offset:  288
//       float4 vViewportSize;          // Offset:  304
//       float4 vEngineTime;            // Offset:  320
//       float4 vShaderColor0;          // Offset:  336
//       float4 vShaderColor1;          // Offset:  352
//       float4 vShaderColor2;          // Offset:  368
//       float4 vShaderColor3;          // Offset:  384
//       float4 vShaderColor4;          // Offset:  400
//       float4 vClipPlane;             // Offset:  416
//
//   } cbSharedPerViewData;             // Offset:    0 Size:   432
//
// }
//
// cbuffer _cbSharedLightData
// {
//
//   struct
//   {
//       
//       float4 vParams0;               // Offset:    0
//       float4 vParams1;               // Offset:   16
//       float4 vParams2;               // Offset:   32
//       float4 vLightDiffuse;          // Offset:   48
//       float4 vHalfLightBoxFadeNeg;   // Offset:   64
//       float4 vHalfLightBoxFadePos;   // Offset:   80
//       float4 vRcpLightBoxFadeNeg;    // Offset:   96
//       float4 vRcpLightBoxFadePos;    // Offset:  112
//       float4 vLightPosView;          // Offset:  128
//       float4 vLightDirView;          // Offset:  144
//       float4 vProjectorTilingOffset; // Offset:  160
//       row_major float4x4 mViewToShadowProjTrans;// Offset:  176
//       row_major float4x4 mViewToLightProjTrans;// Offset:  240
//       float4 vLightParams;           // Offset:  304
//       float4 vRestrictLightBoxVectorX;// Offset:  320
//       float4 vRestrictLightBoxVectorY;// Offset:  336
//       float4 vRestrictLightBoxVectorZ;// Offset:  352
//       float4 vRestrictLightBoxPosition;// Offset:  368
//
//   } cbSharedLightData;               // Offset:    0 Size:   384
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// samplerLightModel                 sampler      NA          NA    0        1
// samplerShadowColor0BBlack         sampler      NA          NA    1        1
// samplerLightCubeProjectorMap0     sampler      NA          NA    2        1
// mapLightModel                     texture  float4          2d    0        1
// mapShadowColor0BBlack             texture  float4          2d    1        1
// mapLightCubeProjectorMap0         texture  float4        cube    2        1
// mapDeferred0                      texture  float4       2dMS2    3        1
// mapDeferred1                      texture  float4       2dMS2    4        1
// mapDeferred2                      texture  float4       2dMS2    5        1
// mapDeferred3                      texture  float4       2dMS2    6        1
// _cbSharedPerViewData              cbuffer      NA          NA    1        1
// _cbSharedLightData                cbuffer      NA          NA    2        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_POSITION              0   xyzw        0      POS   float   xy  
// TEXCOORD                 0   xyzw        1     NONE   float   xyzw
// TEXCOORD                 1   xyzw        2     NONE   float   xyz 
// SV_SampleIndex           0   x           3   SAMPLE    uint   x   
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_TARGET                0   xyzw        0   TARGET   float   xyzw
//
// Pixel Shader runs at sample frequency
//
ps_4_1
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb1[13], immediateIndexed
dcl_constantbuffer cb2[20], immediateIndexed
dcl_sampler s0, mode_default
dcl_sampler s1, mode_default
dcl_sampler s2, mode_default
dcl_resource_texture2d (float,float,float,float) t0
dcl_resource_texture2d (float,float,float,float) t1
dcl_resource_texturecube (float,float,float,float) t2
dcl_resource_texture2dms(2) (float,float,float,float) t3
dcl_resource_texture2dms(2) (float,float,float,float) t4
dcl_resource_texture2dms(2) (float,float,float,float) t5
dcl_resource_texture2dms(2) (float,float,float,float) t6
dcl_input_ps_siv linear noperspective v0.xy, position
dcl_input_ps linear v1.w
dcl_input_ps linear v2.xyz
dcl_input_ps_sgv v3.x, sampleIndex
dcl_output o0.xyzw
dcl_temps 6
ftoi r0.xy, v0.xyxx
mov r0.zw, l(0,0,0,0)
ldms r1.xyz, r0.xyww, t4.xyzw, v3.x
add r1.xyz, r1.xyzx, l(-0.500000, -0.500000, -0.500000, 0.000000)
add r1.xyz, r1.xyzx, r1.xyzx
dp3 r1.w, r1.xyzx, r1.xyzx
rsq r1.w, r1.w
mul r1.xyz, r1.wwww, r1.xyzx
div r1.w, l(1.000000, 1.000000, 1.000000, 1.000000), v1.w
mul r2.xyz, r1.wwww, v2.xyzx
ldms r1.w, r0.xyww, t3.yzwx, v3.x
mad r1.w, cb1[12].z, r1.w, -cb1[12].y
div r1.w, l(1.000000, 1.000000, 1.000000, 1.000000), r1.w
mad r3.xyz, -r1.wwww, r2.xyzx, cb2[8].xyzx
mul r2.xyz, r2.xyzx, r1.wwww
dp3 r1.w, r3.xyzx, r3.xyzx
rsq r2.w, r1.w
sqrt r1.w, r1.w
mul r3.xyz, r2.wwww, r3.xyzx
dp3 r2.w, r1.xyzx, r3.xyzx
add r3.w, r2.w, r2.w
mad r1.xyz, -r3.wwww, r1.xyzx, r3.xyzx
dp3 r3.x, r3.xyzx, cb2[9].xyzx
mad_sat r3.x, cb2[0].y, r3.x, cb2[0].x
dp3 r3.y, -r2.xyzx, -r2.xyzx
rsq r3.y, r3.y
mul r3.yzw, -r2.xxyz, r3.yyyy
dp3_sat r1.x, r3.yzwy, -r1.xyzx
ldms r4.xyzw, r0.xyww, t5.xyzw, v3.x
ldms r0.xyz, r0.xyzw, t6.xywz, v3.x
mov r1.y, r4.w
dp2 r1.y, r1.xyxx, l(0.248046875, 0.753906250, 0.000000, 0.000000)
mov_sat r1.x, r2.w
mov_sat r2.w, -r2.w
sample r5.xyzw, r1.xyxx, t0.xyzw, s0
mad r0.w, r5.w, l(1024.000000), l(-1000.000000)
mul r0.y, r0.w, r0.y
exp r0.y, r0.y
dp3 r0.w, r4.xyzx, l(0.447058499, 0.882354, 0.170587495, 0.000000)
mad r1.xyz, r4.xyzx, l(2.000000, 2.000000, 2.000000, 0.000000), -r0.wwww
add r0.w, -cb2[3].w, l(1.000000)
mul r0.z, r0.z, r0.w
mul r0.z, r2.w, r0.z
mul r1.xyz, r1.xyzx, r0.zzzz
mad r0.xyz, r0.xxxx, r0.yyyy, r1.xyzx
mul r0.xyz, r0.xyzx, cb2[19].xxxx
mad r0.xyz, r5.xyzx, r4.xyzx, r0.xyzx
mul r1.xyz, r2.yyyy, cb2[16].xzyx
mad r1.xyz, r2.xxxx, cb2[15].xzyx, r1.xyzx
mad r1.xyz, r2.zzzz, cb2[17].xzyx, r1.xyzx
add r1.xyz, r1.xyzx, cb2[18].xzyx
mul r2.xyz, r1.xyzx, cb2[4].xyzx
sample r1.xyz, r1.xzyx, t2.xyzw, s2
div r2.xy, r2.xyxx, r2.zzzz
mad r2.xy, r2.xyxx, l(0.500000, 0.500000, 0.000000, 0.000000), l(0.500000, 0.500000, 0.000000, 0.000000)
sample_l r2.xy, r2.xyxx, t1.xyzw, s1, l(0.000000)
mad r0.w, -r1.w, cb2[8].w, r2.x
mad_sat r2.x, -r2.x, r2.x, r2.y
add r2.x, r2.x, l(0.000400)
min r2.x, r2.x, l(1.000000)
rsq r2.x, r2.x
mad_sat r2.yz, cb2[1].yywy, r1.wwww, cb2[1].xxzx
mul r1.w, r2.z, r2.y
mul r2.yzw, r1.wwww, cb2[3].xxyz
mul r1.xyz, r1.xyzx, r2.yzwy
mul r0.w, r0.w, cb2[19].y
mad_sat r0.w, r0.w, r2.x, l(1.000000)
add r0.w, r0.w, l(-1.000000)
mad r0.w, r3.x, r0.w, l(1.000000)
add r1.w, -r0.w, l(1.000000)
mad r0.w, cb2[3].w, r1.w, r0.w
mul r1.xyz, r0.wwww, r1.xyzx
mul o0.xyz, r0.xyzx, r1.xyzx
mov o0.w, l(0)
ret 
// Approximately 75 instruction slots used

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