//Black Shroud
// ---- Created with 3Dmigoto v1.2.56 on Wed May 24 18:33:06 2017

cbuffer _Globals : register(b0)
{
  float4 VPtoRTScaleBias : packoffset(c0);
  float4 ShroudTextureInfo : packoffset(c1);
  float3 RegionTextureInfo : packoffset(c2);
  float Region : packoffset(c2.w);
  float2 WorldPositionOffset : packoffset(c3);
}

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 PointWrapSampler_s : register(s0);
SamplerState LinearClampSampler_s : register(s1);
SamplerState PointClampSampler_s : register(s2);
Texture2D<float4> Base : register(t0);
Texture2D<float4> LinearDepth : register(t1);
Texture2D<float4> Base3 : register(t2);
Texture2D<float4> Base6 : register(t3);


// 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,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1;
  uint4 bitmask, uiDest;
  float4 fDest;
  
  r0.xyz = Base.Sample(LinearClampSampler_s, v1.xy).xyz;
  r0.w = LinearDepth.Sample(PointWrapSampler_s, v1.xy).x;
  
  r1.x = cmp(r0.w < 1);
  if (r1.x != 0) {
	  
float4 s = StereoParams.Load(0);
float4 r20, r21, r25;
r20.xyz = r0.www * v2.xyz + global_ViewPos.xyz;
r20.w = 1.0;
r25 = mul(global_ViewProjection, r20);  
r25.x -= s.x * (r25.w - s.y); 
r20 = mul(inverse(global_ViewProjection), r25);

	// r1.xy = r0.ww * v2.xz + global_ViewPos.xz;
r1.xy = r20.xz;
	
    r0.w = cmp(0 < ShroudTextureInfo.w);
    if (r0.w != 0) {
      r1.zw = -WorldPositionOffset.xy + r1.xy;
      r1.zw = r1.zw / ShroudTextureInfo.zz;
      r1.zw = r1.zw / ShroudTextureInfo.xy;
      r0.w = Base3.SampleLevel(LinearClampSampler_s, r1.zw, 0).x;

      r0.xyz = r0.xyz * r0.www;
    }
    r1.xy = -WorldPositionOffset.xy + r1.xy;  //Must be fixed
    r1.xy = r1.xy / RegionTextureInfo.zz;
    r1.xy = r1.xy / RegionTextureInfo.xy;
    r0.w = Base6.SampleLevel(PointClampSampler_s, r1.xy, 0).x;

    r0.w = r0.w * 255 + -Region;
    r0.w = min(1, abs(r0.w));
    r0.w = 1 + -r0.w;
    r1.xyz = r0.xyz * r0.www;
  } else {
    r0.w = 1 + -ShroudTextureInfo.w;
    r1.xyz = r0.xyz * r0.www;
  }
  o0.xyz = r1.xyz;
  o0.w = 1;
  return;
}      