// ---- Created with 3Dmigoto v1.2.56 on Fri Sep 15 00:17:58 2017

cbuffer _Globals : register(b0)
{
  float4 VPtoRTScaleBias : packoffset(c0);
  float4 LightProperties : packoffset(c1);
  float4 shadowFarPlanes : packoffset(c2);
  float4 SliceDistances : packoffset(c3);
  row_major float4x4 ViewMatrix[5] : packoffset(c4);
  row_major float4x4 ProjectionMatrix[4] : packoffset(c24);
}

cbuffer PerView : register(b12)
{
  row_major float4x4 global_View : packoffset(c0);
  row_major float4x4 global_Projection : packoffset(c4);
  row_major float4x4 global_ViewProjection : packoffset(c8);
  float4 global_ViewPos : packoffset(c12);
  float4 global_ViewInfo : packoffset(c13);
  float4 global_ScaleAndBias : packoffset(c14);
}

SamplerState LinearSampler_s : register(s0);
Texture2D<float4> LinearDepth : register(t0);
Texture2D<float4> Shadow : register(t1);


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

void main( 
  float4 v0 : SV_Position0,
  float2 v1 : TexCoord0,
  float3 v2 : TexCoord1,
  float3 v3 : TexCoord2,
  out float4 o0 : SV_Target0)
{
  const float4 icb[] = { { 1.000000, 0, 0, 0},
                              { 0, 1.000000, 0, 0},
                              { 0, 0, 1.000000, 0},
                              { 0, 0, 0, 1.000000} };
  float4 r0,r1,r2,r3;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xy = (int2)v0.xy;
  r0.zw = float2(0,0);
  r0.z = LinearDepth.Load(r0.xyz).x;
  r0.w = global_ViewInfo.x * r0.z;
  r1.xyz = cmp(SliceDistances.xyz < r0.www);
  r2.xyzw = cmp(SliceDistances.xyzw >= r0.wwww);
  r1.xyz = r1.xyz ? r2.yzw : 0;
  r0.w = r1.x ? 1 : 4;
  r0.w = r2.x ? 0 : r0.w;
  r0.w = r1.z ? 3 : r0.w;
  r0.w = r1.y ? 2 : r0.w;
  r1.x = cmp((uint)r0.w < 4);
  r2.xy = -VPtoRTScaleBias.zw * global_ViewInfo.zw + v0.xy;
  r2.z = global_ViewInfo.w + -r2.y;
  r1.yz = r2.xz / global_ViewInfo.zw;
  r0.xy = r1.yz * float2(2,2) + float2(-1,-1);
  r1.yzw = v2.xyz * r0.xyz;
  r2.xyz = global_View._m10_m11_m12 * r1.zzz;
  r2.xyz = r1.yyy * global_View._m00_m01_m02 + r2.xyz;
  r1.yzw = r1.www * global_View._m20_m21_m22 + r2.xyz;
  r2.xyz = global_ViewPos.xyz + r1.yzw;
  r0.x = (uint)r0.w << 2;
  r2.w = 1;
  
//CORRECT WORLD COORD
float4 s = StereoParams.Load(0);
float4 r20, r21;
r21 = mul(global_ViewProjection, r2);  
r21.x -= s.x * (r21.w - s.y); 
r2 = mul(inverse(global_ViewProjection), r21);
  
  r3.x = dot(ProjectionMatrix[r0.x/4]._m00_m01_m02_m03, r2.xyzw);
  r3.y = dot(ProjectionMatrix[r0.x/4]._m10_m11_m12_m13, r2.xyzw);
  r0.y = dot(ProjectionMatrix[r0.x/4]._m30_m31_m32_m33, r2.xyzw);
  r1.yz = r3.xy / r0.yy;
  r1.yz = Shadow.Sample(LinearSampler_s, r1.yz).xy;
  if (r1.x != 0) {
    r0.x = dot(ViewMatrix[r0.x/4]._m20_m21_m22_m23, r2.xyzw);
    r0.y = dot(shadowFarPlanes.xyzw, icb[r0.w+0].xyzw);
    r0.x = saturate(r0.x / r0.y);
    r0.y = cmp(r1.y >= r0.x);
    r0.y = r0.y ? 1.000000 : 0;
    r1.x = -r1.y * r1.y + r1.z;
    r1.x = max(1.00000001e-007, r1.x);
    r0.x = r0.x + -r1.y;
    r0.x = r0.x * r0.x + r1.x;
    r0.x = r1.x / r0.x;
    r0.x = -0.400000006 + r0.x;
    r0.x = saturate(1.66666663 * r0.x);
    r0.x = max(r0.y, r0.x);
    r0.y = cmp((int)r0.w == 3);
    r0.z = global_ViewInfo.x * r0.z + -SliceDistances.z;
    r0.w = SliceDistances.w + -SliceDistances.z;
    r0.z = saturate(r0.z / r0.w);
    r0.w = 1 + -LightProperties.z;
    r0.z = saturate(r0.z + -r0.w);
    r0.w = 1 / LightProperties.z;
    r0.z = saturate(r0.z * r0.w);
    r0.w = 1 + -r0.x;
    r0.z = r0.z * r0.w + r0.x;
    r0.x = r0.y ? r0.z : r0.x;
  } else {
    r0.x = 1;
  }
  o0.xyzw = r0.xxxx;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 6.3.9600.16384
//
//
// Buffer Definitions: 
//
// cbuffer $Globals
// {
//
//   float4 VPtoRTScaleBias;            // Offset:    0 Size:    16
//   float4 LightProperties;            // Offset:   16 Size:    16
//   float4 shadowFarPlanes;            // Offset:   32 Size:    16
//   float4 SliceDistances;             // Offset:   48 Size:    16
//   row_major float4x4 ViewMatrix[5];  // Offset:   64 Size:   320
//   row_major float4x4 ProjectionMatrix[4];// Offset:  384 Size:   256
//
// }
//
// cbuffer PerView
// {
//
//   row_major float4x4 global_View;    // Offset:    0 Size:    64
//   row_major float4x4 global_Projection;// Offset:   64 Size:    64 [unused]
//   row_major float4x4 global_ViewProjection;// Offset:  128 Size:    64 [unused]
//   float4 global_ViewPos;             // Offset:  192 Size:    16
//   float4 global_ViewInfo;            // Offset:  208 Size:    16
//   float4 global_ScaleAndBias;        // Offset:  224 Size:    16 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// LinearSampler                     sampler      NA          NA    0        1
// LinearDepth                       texture  float4          2d    0        1
// Shadow                            texture  float4          2d    1        1
// $Globals                          cbuffer      NA          NA    0        1
// PerView                           cbuffer      NA          NA   12        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float   xy  
// TexCoord                 0   xy          1     NONE   float       
// TexCoord                 1   xyz         2     NONE   float   xyz 
// TexCoord                 2   xyz         3     NONE   float       
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
//
ps_5_0
dcl_globalFlags refactoringAllowed
dcl_immediateConstantBuffer { { 1.000000, 0, 0, 0},
                              { 0, 1.000000, 0, 0},
                              { 0, 0, 1.000000, 0},
                              { 0, 0, 0, 1.000000} }
dcl_constantbuffer cb0[40], dynamicIndexed
dcl_constantbuffer cb12[14], immediateIndexed
dcl_sampler s0, mode_default
dcl_resource_texture2d (float,float,float,float) t0
dcl_resource_texture2d (float,float,float,float) t1
dcl_input_ps_siv linear noperspective v0.xy, position
dcl_input_ps linear v2.xyz
dcl_output o0.xyzw
dcl_temps 4
ftoi r0.xy, v0.xyxx
mov r0.zw, l(0,0,0,0)
ld_indexable(texture2d)(float,float,float,float) r0.z, r0.xyzw, t0.yzxw
mul r0.w, r0.z, cb12[13].x
lt r1.xyz, cb0[3].xyzx, r0.wwww
ge r2.xyzw, cb0[3].xyzw, r0.wwww
and r1.xyz, r1.xyzx, r2.yzwy
movc r0.w, r1.x, l(1), l(4)
movc r0.w, r2.x, l(0), r0.w
movc r0.w, r1.z, l(3), r0.w
movc r0.w, r1.y, l(2), r0.w
ult r1.x, r0.w, l(4)
mad r2.xy, -cb0[0].zwzz, cb12[13].zwzz, v0.xyxx
add r2.z, -r2.y, cb12[13].w
div r1.yz, r2.xxzx, cb12[13].zzwz
mad r0.xy, r1.yzyy, l(2.000000, 2.000000, 0.000000, 0.000000), l(-1.000000, -1.000000, 0.000000, 0.000000)
mul r1.yzw, r0.xxyz, v2.xxyz
mul r2.xyz, r1.zzzz, cb12[1].xyzx
mad r2.xyz, r1.yyyy, cb12[0].xyzx, r2.xyzx
mad r1.yzw, r1.wwww, cb12[2].xxyz, r2.xxyz
add r2.xyz, r1.yzwy, cb12[12].xyzx
ishl r0.x, r0.w, l(2)
mov r2.w, l(1.000000)
dp4 r3.x, cb0[r0.x + 24].xyzw, r2.xyzw
dp4 r3.y, cb0[r0.x + 25].xyzw, r2.xyzw
dp4 r0.y, cb0[r0.x + 27].xyzw, r2.xyzw
div r1.yz, r3.xxyx, r0.yyyy
sample_indexable(texture2d)(float,float,float,float) r1.yz, r1.yzyy, t1.zxyw, s0
if_nz r1.x
  dp4 r0.x, cb0[r0.x + 6].xyzw, r2.xyzw
  dp4 r0.y, cb0[2].xyzw, icb[r0.w + 0].xyzw
  div_sat r0.x, r0.x, r0.y
  ge r0.y, r1.y, r0.x
  and r0.y, r0.y, l(0x3f800000)
  mad r1.x, -r1.y, r1.y, r1.z
  max r1.x, r1.x, l(0.000000)
  add r0.x, -r1.y, r0.x
  mad r0.x, r0.x, r0.x, r1.x
  div r0.x, r1.x, r0.x
  add r0.x, r0.x, l(-0.400000)
  mul_sat r0.x, r0.x, l(1.666667)
  max r0.x, r0.x, r0.y
  ieq r0.y, r0.w, l(3)
  mad r0.z, cb12[13].x, r0.z, -cb0[3].z
  add r0.w, -cb0[3].z, cb0[3].w
  div_sat r0.z, r0.z, r0.w
  add r0.w, -cb0[1].z, l(1.000000)
  add_sat r0.z, -r0.w, r0.z
  div r0.w, l(1.000000, 1.000000, 1.000000, 1.000000), cb0[1].z
  mul_sat r0.z, r0.w, r0.z
  add r0.w, -r0.x, l(1.000000)
  mad r0.z, r0.z, r0.w, r0.x
  movc r0.x, r0.y, r0.z, r0.x
else 
  mov r0.x, l(1.000000)
endif 
mov o0.xyzw, r0.xxxx
ret 
// Approximately 58 instruction slots used

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