// ScreenSpaceReflections.usf + ScreenSpaceRayCast.usf
// ---- Created with 3Dmigoto v1.2.39 on Sun May 22 23:33:46 2016
Texture2D<float4> t4 : register(t4);

Texture2D<float4> t3 : register(t3);

Texture2D<float4> t2 : register(t2);

Texture2D<float4> t1 : register(t1);

Texture2D<float4> t0 : register(t0);

SamplerState s4_s : register(s4);

SamplerState s3_s : register(s3);

SamplerState s2_s : register(s2);

SamplerState s1_s : register(s1);

SamplerState s0_s : register(s0);

cbuffer cb1 : register(b1)
{
  float4 cb1[75];
}

cbuffer cb0 : register(b0)
{
  float4 cb0[19];
}




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


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

float4 stereo = StereoParams.Load(0);
float p = IniParams.Load(0).x;

  r0.xy = t1.SampleLevel(s1_s, v0.xy, 0).zw;
  r0.z = cmp(0.5 < r0.y);
  r0.z = r0.z ? -0.5 : -0;
  r0.y = r0.y + r0.z;
  r0.y = 14 * r0.y;
  r0.y = (uint)r0.y;
  r0.z = saturate(r0.x * cb0[18].y + 2);
  r0.w = cmp(r0.z == 0.000000);
  r0.y = cmp((int)r0.y == 0);
  r0.y = (int)r0.y | (int)r0.w;
  if (r0.y != 0) {
    o0.xyzw = float4(0,0,0,0);
    return;
  }
  r0.yw = v0.zw * cb0[16].xy + cb0[16].zw;
  r0.yw = float2(0.5,0.5) + r0.yw;
  r0.yw = (int2)r0.yw;
  r1.xyz = t0.SampleLevel(s0_s, v0.xy, 0).xyz;
  r1.xyz = r1.xyz * float3(2,2,2) + float3(-1,-1,-1);
  r1.w = dot(r1.xyz, r1.xyz);
  r1.w = rsqrt(r1.w);
  r1.xyz = r1.xyz * r1.www;

// const float SceneDepth = CalcSceneDepth(UV);
  r1.w = t2.SampleLevel(s2_s, v0.xy, 0).x;
  r1.w = r1.w * cb1[40].z + -cb1[40].w;
  r1.w = 1 / r1.w;

// const float3 PositionTranslatedWorld = mul( float4( ScreenPos * SceneDepth, SceneDepth, 1 ), View.ScreenToTranslatedWorld ).xyz;
  r2.xy = v0.zw * r1.ww;

r2.x -= stereo.x * (r1.w - stereo.y);

  r2.yzw = cb1[33].xyz * r2.yyy;
  r2.xyz = r2.xxx * cb1[32].xyz + r2.yzw;
  r2.xyz = r1.www * cb1[34].xyz + r2.xyz;
  r3.xyz = cb1[45].xyz + -r2.xyz;

// const float3 V = normalize(View.TranslatedViewOrigin.xyz - PositionTranslatedWorld);
  r2.w = dot(r3.xyz, r3.xyz);
  r2.w = rsqrt(r2.w);
  r3.xyz = r3.xyz * r2.www;

  r2.w = cmp(1 < cb1[74].y);
  r3.w = 1551 * cb1[74].x;
  r3.w = (uint)r3.w;
  r4.x = asint(cb1[54].y) & 7;
  r4.x = (int)r4.x * 1551;
  r2.w = r2.w ? r3.w : r4.x;
  r4.xy = (int2)r0.yw & int2(3,3);
  bitmask.y = ((~(-1 << 2)) << 2) & 0xffffffff;
  r0.y = (((uint)r0.y << 2) & bitmask.y) | ((uint)0 & ~bitmask.y);
  bitmask.w = ((~(-1 << 2)) << 2) & 0xffffffff;
  r0.w = (((uint)r0.w << 2) & bitmask.w) | ((uint)0 & ~bitmask.w);
  r0.yw = (int2)r0.yw ^ (int2)r4.xy;
  r4.xy = (int2)r0.yw & int2(3,3);
  bitmask.y = ((~(-1 << 2)) << 1) & 0xffffffff;
  r0.y = (((uint)r0.y << 1) & bitmask.y) | ((uint)0 & ~bitmask.y);
  bitmask.w = ((~(-1 << 2)) << 1) & 0xffffffff;
  r0.w = (((uint)r0.w << 1) & bitmask.w) | ((uint)0 & ~bitmask.w);
  r0.yw = (int2)r0.yw ^ (int2)r4.xy;
  r0.w = (uint)r0.w << 1;
  r0.yw = (int2)r0.yw & int2(5,10);
  r0.y = (int)r0.y + (int)r0.w;
  bitmask.x = ((~(-1 << 2)) << 2) & 0xffffffff;
  r4.x = (((uint)r0.y << 2) & bitmask.x) | ((uint)0 & ~bitmask.x);
  bitmask.y = ((~(-1 << 2)) << 3) & 0xffffffff;
  r4.y = (((uint)r0.y << 3) & bitmask.y) | ((uint)0 & ~bitmask.y);
  r0.y = (uint)r0.y >> 2;
  bitmask.y = ((~(-1 << 2)) << 0) & 0xffffffff;
  r0.y = (((uint)r0.y << 0) & bitmask.y) | ((uint)r4.x & ~bitmask.y);
  bitmask.w = ((~(-1 << 2)) << 1) & 0xffffffff;
  r0.w = (((uint)r0.y << 1) & bitmask.w) | ((uint)r4.y & ~bitmask.w);
  r0.y = (uint)r0.y >> 1;
  r0.yw = (int2)r0.yw & int2(5,10);
  r0.y = (int)r0.w + (int)r0.y;
  r0.w = (uint)r2.w << 2;
  r0.w = (int)r0.w & 0xcccccccc;
  r2.w = (uint)r2.w >> 2;
  r2.w = (int)r2.w & 0x33333333;
  r0.w = (int)r0.w + (int)r2.w;
  r2.w = (uint)r0.w << 1;
  r2.w = (int)r2.w & 0xaaaaaaaa;
  r0.w = (uint)r0.w >> 1;
  r0.w = (int)r0.w & 0x55555555;
  r0.w = (int)r2.w + (int)r0.w;
  r0.y = (int)r0.y + (int)r0.w;
  r0.y = (int)r0.y & 15;
  r0.y = (uint)r0.y;
  r0.w = dot(-r3.xyz, r1.xyz);
  r0.w = r0.w + r0.w;
  r1.xyz = r1.xyz * -r0.www + -r3.xyz;

// float4 RayStartClip	= mul( float4( PositionTranslatedWorld, 1 ), View.TranslatedWorldToClip );
  r3.xyzw = cb1[1].xyzw * r2.yyyy;
  r3.xyzw = r2.xxxx * cb1[0].xyzw + r3.xyzw;
  r3.xyzw = r2.zzzz * cb1[2].xyzw + r3.xyzw;
  r3.xyzw = cb1[3].xyzw + r3.xyzw;

// float4 RayEndClip	= mul( float4( PositionTranslatedWorld + R * SceneDepth, 1 ), View.TranslatedWorldToClip );
  r1.xyz = r1.xyz * r1.www + r2.xyz;
  r2.xyzw = cb1[1].xyzw * r1.yyyy;
  r2.xyzw = r1.xxxx * cb1[0].xyzw + r2.xyzw;
  r1.xyzw = r1.zzzz * cb1[2].xyzw + r2.xyzw;
  r1.xyzw = cb1[3].xyzw + r1.xyzw;

// Correct ray start + end position:
r3.x += stereo.x * (r3.w - stereo.y);
r1.x += stereo.x * (r1.w - stereo.y);

// float3 RayStartScreen = RayStartClip.xyz / RayStartClip.w;
  r2.xyz = r3.zxy / r3.www;

// float3 RayEndScreen = RayEndClip.xyz / RayEndClip.w;
  r1.xyz = r1.zxy / r1.www;

// float3 RayStepScreen = ( RayEndScreen - RayStartScreen ) / length( RayEndScreen.xy - RayStartScreen.xy );
  r1.xyz = r1.xyz + -r2.xyz;
  r0.w = dot(r1.yz, r1.yz);
  r0.w = sqrt(r0.w);
  r1.xyz = r1.xyz / r0.www;

  r2.yz = r2.yz * float2(0.5,-0.5) + float2(0.5,0.5);

// RayStepScreen *= 0.75;
  r1.xyz = float3(1.5,0.75,-0.75) * r1.xyz;

  r0.w = 0.117647059 * abs(r1.x);
  r3.xyzw = r0.yyyy * float4(0.0666666701,0.0666666701,0.0666666701,0.0666666701) + float4(0.5,1.5,2.5,3.5);
  r3.xyzw = float4(0.0588235296,0.0588235296,0.0588235296,0.0588235296) * r3.xyzw;
  r4.xyz = float3(0,0,1);
  r5.xyzw = r3.xyzw;
  r6.x = 0;
  r0.y = 0;
  r1.w = 0;
  while (true) {
    r2.w = cmp((int)r1.w >= 16);
    if (r2.w != 0) break;
    r7.xyzw = r1.yzyz * r5.xxyy + r2.yzyz;
    r8.xyzw = r1.yzyz * r5.zzww + r2.yzyz;
    r9.xyzw = r1.xxxx * r5.xyzw + r2.xxxx;
    r10.x = t4.SampleLevel(s4_s, r7.xy, r0.y).x;
    r10.y = t4.SampleLevel(s4_s, r7.zw, r0.y).x;
    r10.z = t4.SampleLevel(s4_s, r8.xy, r0.y).x;
    r10.w = t4.SampleLevel(s4_s, r8.zw, r0.y).x;
    r7.xyzw = -r10.xyzw + r9.xyzw;
    r8.xyzw = -abs(r1.xxxx) * float4(0.117647059,0.117647059,0.117647059,0.117647059) + -r7.xyzw;
    r8.xyzw = cmp(abs(r8.xyzw) < r0.wwww);
    r9.xy = (int2)r8.zw | (int2)r8.xy;
    r2.w = (int)r9.y | (int)r9.x;
    if (r2.w != 0) {
      r6.yzw = r7.xyz;
      r9.xyzw = r6.xyzw + -r7.xyzw;
      r9.xyzw = saturate(r6.xyzw / r9.xyzw);
      r9.xyzw = float4(-1,-1,-1,-1) + r9.xyzw;
      r9.xyzw = r9.xyzw * float4(0.0588235296,0.0588235296,0.0588235296,0.0588235296) + r5.xyzw;
      r8.xyzw = r8.xyzw ? r9.xyzw : float4(1,1,1,1);
      r6.yz = min(r8.xy, r8.zw);
      r4.z = min(r6.y, r6.z);
      r6.yz = r1.yz * r4.zz + r2.yz;
      r6.yz = r6.yz * float2(2,-2) + float2(-1,1);
      r4.xy = r6.yz * cb1[41].xy + cb1[41].wz;
      break;
    }
    r0.y = r0.y + r0.x;
    r5.xyzw = float4(0.235294119,0.235294119,0.235294119,0.235294119) + r5.xyzw;
    r1.w = (int)r1.w + 4;
    r6.x = r7.w;
    r4.xyz = float3(0,0,1);
  }
  r0.x = cmp(r4.z < 1);
  if (r0.x != 0) {
    r1.xyz = t3.SampleLevel(s3_s, r4.xy, 0).xyz;
    r0.xy = -cb1[41].wz + r4.xy;
    r0.xy = r0.xy / cb1[41].xy;
    r0.xy = saturate(abs(r0.xy) * float2(5,5) + float2(-4,-4));
    r0.x = dot(r0.xy, r0.xy);
    r0.x = 1 + -r0.x;
    r0.x = max(0, r0.x);
    r1.w = 1;
    r1.xyzw = r1.xyzw * r0.xxxx;
    r0.xyw = min(float3(0,0,0), -r1.xyz);
    r1.xyz = -r0.xyw;
    r0.x = -r4.z * 4 + 4;
    r0.x = min(1, r0.x);
    r1.xyzw = r1.xyzw * r0.xxxx;
  } else {
    r1.xyzw = float4(0,0,0,0);
  }
  r0.xyzw = r1.xyzw * r0.zzzz;
  o0.xyzw = cb0[18].xxxx * r0.xyzw;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) D3D Shader Disassembler
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD                 0   xyzw        0     NONE   float   xyzw
//
//
// 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[19], immediateIndexed
dcl_constantbuffer cb1[75], immediateIndexed
dcl_sampler s0, mode_default
dcl_sampler s1, mode_default
dcl_sampler s2, mode_default
dcl_sampler s3, mode_default
dcl_sampler s4, 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) t3
dcl_resource_texture2d (float,float,float,float) t4
dcl_input_ps linear v0.xyzw
dcl_output o0.xyzw
dcl_temps 11
sample_l_indexable(texture2d)(float,float,float,float) r0.xy, v0.xyxx, t1.zwxy, s1, l(0.000000)
lt r0.z, l(0.500000), r0.y
movc r0.z, r0.z, l(-0.500000), l(-0.000000)
add r0.y, r0.z, r0.y
mul r0.y, r0.y, l(14.000000)
ftou r0.y, r0.y
mad_sat r0.z, r0.x, cb0[18].y, l(2.000000)
eq r0.w, r0.z, l(0.000000)
ieq r0.y, r0.y, l(0)
or r0.y, r0.y, r0.w
if_nz r0.y
  mov o0.xyzw, l(0,0,0,0)
  ret 
endif 
mad r0.yw, v0.zzzw, cb0[16].xxxy, cb0[16].zzzw
add r0.yw, r0.yyyw, l(0.000000, 0.500000, 0.000000, 0.500000)
ftoi r0.yw, r0.yyyw
sample_l_indexable(texture2d)(float,float,float,float) r1.xyz, v0.xyxx, t0.xyzw, s0, l(0.000000)
mad r1.xyz, r1.xyzx, l(2.000000, 2.000000, 2.000000, 0.000000), l(-1.000000, -1.000000, -1.000000, 0.000000)
dp3 r1.w, r1.xyzx, r1.xyzx
rsq r1.w, r1.w
mul r1.xyz, r1.wwww, r1.xyzx
sample_l_indexable(texture2d)(float,float,float,float) r1.w, v0.xyxx, t2.yzwx, s2, l(0.000000)
mad r1.w, r1.w, cb1[40].z, -cb1[40].w
div r1.w, l(1.000000, 1.000000, 1.000000, 1.000000), r1.w
mul r2.xy, r1.wwww, v0.zwzz
mul r2.yzw, r2.yyyy, cb1[33].xxyz
mad r2.xyz, r2.xxxx, cb1[32].xyzx, r2.yzwy
mad r2.xyz, r1.wwww, cb1[34].xyzx, r2.xyzx
add r3.xyz, -r2.xyzx, cb1[45].xyzx
dp3 r2.w, r3.xyzx, r3.xyzx
rsq r2.w, r2.w
mul r3.xyz, r2.wwww, r3.xyzx
lt r2.w, l(1.000000), cb1[74].y
mul r3.w, cb1[74].x, l(1551.000000)
ftou r3.w, r3.w
and r4.x, cb1[54].y, l(7)
imul null, r4.x, r4.x, l(1551)
movc r2.w, r2.w, r3.w, r4.x
and r4.xy, r0.ywyy, l(3, 3, 0, 0)
bfi r0.yw, l(0, 2, 0, 2), l(0, 2, 0, 2), r0.yyyw, l(0, 0, 0, 0)
xor r0.yw, r0.yyyw, r4.xxxy
and r4.xy, r0.ywyy, l(3, 3, 0, 0)
bfi r0.yw, l(0, 2, 0, 2), l(0, 1, 0, 1), r0.yyyw, l(0, 0, 0, 0)
xor r0.yw, r0.yyyw, r4.xxxy
ishl r0.w, r0.w, l(1)
and r0.yw, r0.yyyw, l(0, 5, 0, 10)
iadd r0.y, r0.y, r0.w
bfi r4.xy, l(2, 2, 0, 0), l(2, 3, 0, 0), r0.yyyy, l(0, 0, 0, 0)
ushr r0.y, r0.y, l(2)
bfi r0.yw, l(0, 2, 0, 2), l(0, 0, 0, 1), r0.yyyy, r4.xxxy
ushr r0.y, r0.y, l(1)
and r0.yw, r0.yyyw, l(0, 5, 0, 10)
iadd r0.y, r0.w, r0.y
ishl r0.w, r2.w, l(2)
and r0.w, r0.w, l(0xcccccccc)
ushr r2.w, r2.w, l(2)
and r2.w, r2.w, l(0x33333333)
iadd r0.w, r0.w, r2.w
ishl r2.w, r0.w, l(1)
and r2.w, r2.w, l(0xaaaaaaaa)
ushr r0.w, r0.w, l(1)
and r0.w, r0.w, l(0x55555555)
iadd r0.w, r2.w, r0.w
iadd r0.y, r0.y, r0.w
and r0.y, r0.y, l(15)
utof r0.y, r0.y
dp3 r0.w, -r3.xyzx, r1.xyzx
add r0.w, r0.w, r0.w
mad r1.xyz, r1.xyzx, -r0.wwww, -r3.xyzx
mul r3.xyzw, r2.yyyy, cb1[1].xyzw
mad r3.xyzw, r2.xxxx, cb1[0].xyzw, r3.xyzw
mad r3.xyzw, r2.zzzz, cb1[2].xyzw, r3.xyzw
add r3.xyzw, r3.xyzw, cb1[3].xyzw
mad r1.xyz, r1.xyzx, r1.wwww, r2.xyzx
mul r2.xyzw, r1.yyyy, cb1[1].xyzw
mad r2.xyzw, r1.xxxx, cb1[0].xyzw, r2.xyzw
mad r1.xyzw, r1.zzzz, cb1[2].xyzw, r2.xyzw
add r1.xyzw, r1.xyzw, cb1[3].xyzw
div r2.xyz, r3.zxyz, r3.wwww
div r1.xyz, r1.zxyz, r1.wwww
add r1.xyz, -r2.xyzx, r1.xyzx
dp2 r0.w, r1.yzyy, r1.yzyy
sqrt r0.w, r0.w
div r1.xyz, r1.xyzx, r0.wwww
mad r2.yz, r2.yyzy, l(0.000000, 0.500000, -0.500000, 0.000000), l(0.000000, 0.500000, 0.500000, 0.000000)
mul r1.xyz, r1.xyzx, l(1.500000, 0.750000, -0.750000, 0.000000)
mul r0.w, |r1.x|, l(0.117647)
mad r3.xyzw, r0.yyyy, l(0.066667, 0.066667, 0.066667, 0.066667), l(0.500000, 1.500000, 2.500000, 3.500000)
mul r3.xyzw, r3.xyzw, l(0.058824, 0.058824, 0.058824, 0.058824)
mov r4.xyz, l(0,0,1.000000,0)
mov r5.xyzw, r3.xyzw
mov r6.x, l(0)
mov r0.y, l(0)
mov r1.w, l(0)
loop 
  ige r2.w, r1.w, l(16)
  breakc_nz r2.w
  mad r7.xyzw, r1.yzyz, r5.xxyy, r2.yzyz
  mad r8.xyzw, r1.yzyz, r5.zzww, r2.yzyz
  mad r9.xyzw, r1.xxxx, r5.xyzw, r2.xxxx
  sample_l_indexable(texture2d)(float,float,float,float) r10.x, r7.xyxx, t4.xyzw, s4, r0.y
  sample_l_indexable(texture2d)(float,float,float,float) r10.y, r7.zwzz, t4.yxzw, s4, r0.y
  sample_l_indexable(texture2d)(float,float,float,float) r10.z, r8.xyxx, t4.yzxw, s4, r0.y
  sample_l_indexable(texture2d)(float,float,float,float) r10.w, r8.zwzz, t4.yzwx, s4, r0.y
  add r7.xyzw, r9.xyzw, -r10.xyzw
  mad r8.xyzw, -|r1.xxxx|, l(0.117647, 0.117647, 0.117647, 0.117647), -r7.xyzw
  lt r8.xyzw, |r8.xyzw|, r0.wwww
  or r9.xy, r8.zwzz, r8.xyxx
  or r2.w, r9.y, r9.x
  if_nz r2.w
    mov r6.yzw, r7.xxyz
    add r9.xyzw, -r7.xyzw, r6.xyzw
    div_sat r9.xyzw, r6.xyzw, r9.xyzw
    add r9.xyzw, r9.xyzw, l(-1.000000, -1.000000, -1.000000, -1.000000)
    mad r9.xyzw, r9.xyzw, l(0.058824, 0.058824, 0.058824, 0.058824), r5.xyzw
    movc r8.xyzw, r8.xyzw, r9.xyzw, l(1.000000,1.000000,1.000000,1.000000)
    min r6.yz, r8.zzwz, r8.xxyx
    min r4.z, r6.z, r6.y
    mad r6.yz, r1.yyzy, r4.zzzz, r2.yyzy
    mad r6.yz, r6.yyzy, l(0.000000, 2.000000, -2.000000, 0.000000), l(0.000000, -1.000000, 1.000000, 0.000000)
    mad r4.xy, r6.yzyy, cb1[41].xyxx, cb1[41].wzww
    break 
  endif 
  add r0.y, r0.x, r0.y
  add r5.xyzw, r5.xyzw, l(0.235294, 0.235294, 0.235294, 0.235294)
  iadd r1.w, r1.w, l(4)
  mov r6.x, r7.w
  mov r4.xyz, l(0,0,1.000000,0)
endloop 
lt r0.x, r4.z, l(1.000000)
if_nz r0.x
  sample_l_indexable(texture2d)(float,float,float,float) r1.xyz, r4.xyxx, t3.xyzw, s3, l(0.000000)
  add r0.xy, r4.xyxx, -cb1[41].wzww
  div r0.xy, r0.xyxx, cb1[41].xyxx
  mad_sat r0.xy, |r0.xyxx|, l(5.000000, 5.000000, 0.000000, 0.000000), l(-4.000000, -4.000000, 0.000000, 0.000000)
  dp2 r0.x, r0.xyxx, r0.xyxx
  add r0.x, -r0.x, l(1.000000)
  max r0.x, r0.x, l(0.000000)
  mov r1.w, l(1.000000)
  mul r1.xyzw, r0.xxxx, r1.xyzw
  min r0.xyw, -r1.xyxz, l(0.000000, 0.000000, 0.000000, 0.000000)
  mov r1.xyz, -r0.xywx
  mad r0.x, -r4.z, l(4.000000), l(4.000000)
  min r0.x, r0.x, l(1.000000)
  mul r1.xyzw, r0.xxxx, r1.xyzw
else 
  mov r1.xyzw, l(0,0,0,0)
endif 
mul r0.xyzw, r0.zzzz, r1.xyzw
mul o0.xyzw, r0.xyzw, cb0[18].xxxx
ret 
// Approximately 0 instruction slots used

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