//P5S portal haloing PS 1.
// ---- Created with 3Dmigoto v1.3.16 on Thu Mar 03 23:50:07 2022

cbuffer _Globals : register(b0)
{
  float4 vEye : packoffset(c35);
  row_major float4x4 mW2P : packoffset(c40);
  float vATest : packoffset(c0);
  float2 vSatFade : packoffset(c1) = {0,1};
  bool bSatFade : packoffset(c2) = true;
  float4 vSPort : packoffset(c44);
  float envscale : packoffset(c36);
}

cbuffer cb13 : register(b13)
{
  float4 cb13[10];
}

SamplerState __smpsStage0_s : register(s0);
SamplerState __smpsRTT_s : register(s3);
SamplerState __smpsEnv_s : register(s4);
Texture2D<float4> sStage0 : register(t0);
Texture2D<float4> sRTT : register(t3);
TextureCube<float4> sEnv : register(t4);


// 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,
  float4 v3 : TEXCOORD2,
  float4 v4 : TEXCOORD3,
  float4 v5 : TEXCOORD4,
  float2 v6 : TEXCOORD5,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2;
  uint4 bitmask, uiDest;
  float4 fDest;
  
  float4 stereo = StereoParams.Load(0);
  
  r0.xyzw = sStage0.Sample(__smpsStage0_s, v6.xy).xyzw;
  r1.x = -vATest + r0.w;
  r1.x = cmp(r1.x < 0);
  if (r1.x != 0) discard;
  r0.xyz = r0.xyz * float3(2,2,2) + float3(-1,-1,-1);
  o0.w = v2.w * r0.w;
  r0.w = dot(r0.xyz, r0.xyz);
  r0.w = rsqrt(r0.w);
  r0.xyz = r0.xyz * r0.www;
  r1.xyz = v4.xyz * r0.yyy;
  r0.xyw = r0.xxx * v5.xyz + r1.xyz;
  r0.xyz = r0.zzz * v3.xyz + r0.xyw;
  r1.xyz = vEye.xyz + -v1.xyz;
  
  //Do not use. It affects Osaka jail castle.
  //if (cb13[0].x==0.0 && cb13[0].y==0.0 && cb13[0].z==0.0 && cb13[0].w==0.0) {
  //  float fov = cb13[1].x*cb13[1].x+cb13[2].x*cb13[2].x+cb13[3].x*cb13[3].x;
  //  r1.x-=stereo.x*stereo.y*cb13[1].x/fov;
  //  r1.y-=stereo.x*stereo.y*cb13[2].x/fov;
  //  r1.z-=stereo.x*stereo.y*cb13[3].x/fov;
  //} else {
  //  float fov = cb13[0].x*cb13[0].x+cb13[1].x*cb13[1].x+cb13[2].x*cb13[2].x;
  //  r1.x-=stereo.x*stereo.y*cb13[0].x/fov;
  //  r1.y-=stereo.x*stereo.y*cb13[1].x/fov;
  //  r1.z-=stereo.x*stereo.y*cb13[2].x/fov;
  //}
  
  r0.w = dot(r1.xyz, r1.xyz);
  r0.w = rsqrt(r0.w);
  r2.xyz = r1.xyz * r0.www;
  r0.w = dot(r0.xyz, -r2.xyz);
  r0.w = saturate(1 + r0.w);
  r1.w = r0.w * r0.w;
  r1.w = r1.w * r1.w;
  r0.w = r1.w * r0.w;
  r1.w = 1 + -v4.w;
  r0.w = r0.w * r1.w + v4.w;
  r1.w = dot(-r1.xyz, r0.xyz);
  r2.x = r1.w + r1.w;
  r2.yzw = r1.www * r0.xyz + r1.xyz;
  r0.xyz = -r2.xxx * r0.xyz + -r1.xyz;
  r1.xyz = v3.www * r2.yzw + -r1.xyz;
  r1.xyz = v1.xyz + r1.xyz;
  r2.xyzw = sEnv.Sample(__smpsEnv_s, r0.xyz).xyzw;
  r0.x = envscale * r2.w;
  r0.xyz = r2.xyz * r0.xxx;
  r0.xyz = r0.xyz * r0.www;
  r2.xyz = mW2P._m10_m11_m13 * r1.yyy;
  r1.xyw = r1.xxx * mW2P._m00_m01_m03 + r2.xyz;
  r1.xyz = r1.zzz * mW2P._m20_m21_m23 + r1.xyw;
  r1.xyz = mW2P._m30_m31_m33 + r1.xyz;
  r2.x = dot(vSPort.xy, r1.xz);
  r2.y = dot(vSPort.zw, r1.yz);
  
  r2.x+=stereo.x*(r1.z-stereo.y*2)*0.5;
  
  r1.xy = r2.xy / r1.zz;
  r1.xyz = sRTT.Sample(__smpsRTT_s, r1.xy).xyz;
  r0.xyz = r1.xyz * v2.xyz + r0.xyz;
  r0.w = max(r0.y, r0.z);
  r0.w = max(r0.x, r0.w);
  r1.xyz = r0.www * vSatFade.yyy + -r0.xyz;
  r1.xyz = vSatFade.xxx * r1.xyz + r0.xyz;
  o0.xyz = bSatFade ? r1.xyz : r0.xyz;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 6.3.9600.16384
//
//   using 3Dmigoto v1.3.16 on Thu Mar 03 23:50:07 2022
//
//
// Buffer Definitions:
//
// cbuffer $Globals
// {
//
//   float4 vEye;                       // Offset:  560 Size:    16
//   row_major float4x4 mW2P;           // Offset:  640 Size:    64
//   float vATest;                      // Offset:    0 Size:     4
//   float2 vSatFade;                   // Offset:   16 Size:     8
//      = 0x00000000 0x3f800000
//   bool bSatFade;                     // Offset:   32 Size:     4
//      = 0xffffffff
//   float4 vSPort;                     // Offset:  704 Size:    16
//   float envscale;                    // Offset:  576 Size:     4
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// __smpsStage0                      sampler      NA          NA    0        1
// __smpsRTT                         sampler      NA          NA    3        1
// __smpsEnv                         sampler      NA          NA    4        1
// sStage0                           texture  float4          2d    0        1
// sRTT                              texture  float4          2d    3        1
// sEnv                              texture  float4        cube    4        1
// $Globals                          cbuffer      NA          NA    0        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float
// TEXCOORD                 0   xyzw        1     NONE   float   xyz
// TEXCOORD                 1   xyzw        2     NONE   float   xyzw
// TEXCOORD                 2   xyzw        3     NONE   float   xyzw
// TEXCOORD                 3   xyzw        4     NONE   float   xyzw
// TEXCOORD                 4   xyz         5     NONE   float   xyz
// TEXCOORD                 5   xy          6     NONE   float   xy
//
//
// 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[45], immediateIndexed
dcl_sampler s0, 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) t3
dcl_resource_texturecube (float,float,float,float) t4
dcl_input_ps linear v1.xyz
dcl_input_ps linear v2.xyzw
dcl_input_ps linear v3.xyzw
dcl_input_ps linear v4.xyzw
dcl_input_ps linear v5.xyz
dcl_input_ps linear v6.xy
dcl_output o0.xyzw
dcl_temps 3
sample_indexable(texture2d)(float,float,float,float) r0.xyzw, v6.xyxx, t0.xyzw, s0
add r1.x, r0.w, -cb0[0].x
lt r1.x, r1.x, l(0.000000)
discard_nz r1.x
mad r0.xyz, r0.xyzx, l(2.000000, 2.000000, 2.000000, 0.000000), l(-1.000000, -1.000000, -1.000000, 0.000000)
mul o0.w, r0.w, v2.w
dp3 r0.w, r0.xyzx, r0.xyzx
rsq r0.w, r0.w
mul r0.xyz, r0.wwww, r0.xyzx
mul r1.xyz, r0.yyyy, v4.xyzx
mad r0.xyw, r0.xxxx, v5.xyxz, r1.xyxz
mad r0.xyz, r0.zzzz, v3.xyzx, r0.xywx
add r1.xyz, -v1.xyzx, cb0[35].xyzx
dp3 r0.w, r1.xyzx, r1.xyzx
rsq r0.w, r0.w
mul r2.xyz, r0.wwww, r1.xyzx
dp3 r0.w, r0.xyzx, -r2.xyzx
add_sat r0.w, r0.w, l(1.000000)
mul r1.w, r0.w, r0.w
mul r1.w, r1.w, r1.w
mul r0.w, r0.w, r1.w
add r1.w, -v4.w, l(1.000000)
mad r0.w, r0.w, r1.w, v4.w
dp3 r1.w, -r1.xyzx, r0.xyzx
add r2.x, r1.w, r1.w
mad r2.yzw, r1.wwww, r0.xxyz, r1.xxyz
mad r0.xyz, -r2.xxxx, r0.xyzx, -r1.xyzx
mad r1.xyz, v3.wwww, r2.yzwy, -r1.xyzx
add r1.xyz, r1.xyzx, v1.xyzx
sample_indexable(texturecube)(float,float,float,float) r2.xyzw, r0.xyzx, t4.xyzw, s4
mul r0.x, r2.w, cb0[36].x
mul r0.xyz, r0.xxxx, r2.xyzx
mul r0.xyz, r0.wwww, r0.xyzx
mul r2.xyz, r1.yyyy, cb0[41].xywx
mad r1.xyw, r1.xxxx, cb0[40].xyxw, r2.xyxz
mad r1.xyz, r1.zzzz, cb0[42].xywx, r1.xywx
add r1.xyz, r1.xyzx, cb0[43].xywx
dp2 r2.x, cb0[44].xyxx, r1.xzxx
dp2 r2.y, cb0[44].zwzz, r1.yzyy
div r1.xy, r2.xyxx, r1.zzzz
sample_indexable(texture2d)(float,float,float,float) r1.xyz, r1.xyxx, t3.xyzw, s3
mad r0.xyz, r1.xyzx, v2.xyzx, r0.xyzx
max r0.w, r0.z, r0.y
max r0.w, r0.w, r0.x
mad r1.xyz, r0.wwww, cb0[1].yyyy, -r0.xyzx
mad r1.xyz, cb0[1].xxxx, r1.xyzx, r0.xyzx
movc o0.xyz, cb0[2].xxxx, r1.xyzx, r0.xyzx
ret
// Approximately 48 instruction slots used

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