// Unusual lighting pattern (PCF_5x5_FORCE_INV_PROJECTION_IN_PS)
// Found in mirror in Life Is Strange: Before the Storm
// If the lighting is still broken, check that o0 and ps-t0 are stereo
//
// 3DMigoto: c3947cf574931d30 |    Unity headers extracted from Hidden_Internal-ScreenSpaceShadows
//    Shader "Hidden/Internal-ScreenSpaceShadows" {
//      SubShader 4/4 {
//        Tags { "ShadowmapFilter"="PCF_5x5_FORCE_INV_PROJECTION_IN_PS" }
//        Pass 1/1 {
//          Tags { "ShadowmapFilter"="PCF_5x5_FORCE_INV_PROJECTION_IN_PS" }
//          ZWrite Off
//          Program "fp" {
//            SubProgram "d3d11" {
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Hidden_Internal-ScreenSpaceShadows:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 201608170 88 1 0
//   undeciphered1a: 8
//   Keywords { "SHADOWS_SPLIT_SPHERES" }
//   undeciphered2: 1 2 3 2 0 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 64
//   Vector 48 [_ShadowMapTexture_TexelSize]
//   ConstBuffer "UnityPerCameraRare" 352
//   Matrix 160 [unity_CameraInvProjection]
//   Matrix 288 [unity_CameraToWorld]
//   ConstBuffer "UnityShadows" 416
//   Vector 0 [unity_ShadowSplitSpheres0]
//   Vector 16 [unity_ShadowSplitSpheres1]
//   Vector 32 [unity_ShadowSplitSpheres2]
//   Vector 48 [unity_ShadowSplitSpheres3]
//   Vector 64 [unity_ShadowSplitSqRadii]
//   Vector 384 [_LightShadowData]
//   Vector 400 [unity_ShadowFadeCenterAndType]
//   Matrix 128 [unity_WorldToShadow0]
//   Matrix 192 [unity_WorldToShadow1]
//   Matrix 256 [unity_WorldToShadow2]
//   Matrix 320 [unity_WorldToShadow3]
//   SetTexture 0 [_CameraDepthTexture] 2D 0
//   SetTexture 1 [_ShadowMapTexture] 2D 1
//   BindCB "$Globals" 0
//   BindCB "UnityPerCameraRare" 1
//   BindCB "UnityShadows" 2
//
// Headers extracted with DarkStarSword's extract_unity55_shaders.py
// https://github.com/DarkStarSword/3d-fixes

// ---- Created with 3Dmigoto v1.2.61 on Wed Jan 03 05:20:31 2018
Texture2D<float4> t1 : register(t1);

Texture2D<float4> t0 : register(t0);

SamplerComparisonState s1_s : register(s1);

SamplerState s0_s : register(s0);

cbuffer cb2 : register(b2)
{
  float4 cb2[26];
}

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

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




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

#include "matrix.hlsl"

void main( 
  float4 v0 : TEXCOORD0,
  float4 v1 : TEXCOORD1,
  float4 v2 : TEXCOORD2,
  float3 v3 : TEXCOORD3,
  float4 v4 : SV_POSITION0,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2,r3,r4,r5,r6,r7;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xyzw = t0.Sample(s0_s, v0.xy).xyzw;
  r0.z = 1 + -r0.x;
  r0.xy = v0.zw;
  r0.xyz = r0.xyz * float3(2,2,2) + float3(-1,-1,-1);

// This shader doesn't do the usual thing to get the linear depth.
// Use the inverse projection matrix to find the linear depth:
float4 s = StereoParams.Load(0);
float4 tmp = mul(r0, MATRIX(cb1, 10));
float depth = -tmp.z / tmp.w;
r0.x -= s.x * (depth - s.y) / depth;

// unity_CameraInvProjection:
  r1.xyzw = cb1[11].xyzw * r0.yyyy;
  r1.xyzw = cb1[10].xyzw * r0.xxxx + r1.xyzw;
  r0.xyzw = cb1[12].xyzw * r0.zzzz + r1.xyzw;
  r0.xyzw = cb1[13].xyzw + r0.xyzw;
  r0.xyz = r0.xyz / r0.www;

// unity_CameraToWorld:
  r1.xyzw = cb1[19].xyzw * r0.yyyy;
  r1.xyzw = cb1[18].xyzw * r0.xxxx + r1.xyzw;
  r0.xyzw = cb1[20].xyzw * -r0.zzzz + r1.xyzw;
  r0.xyzw = cb1[21].xyzw + r0.xyzw;

  r1.xyz = -cb2[0].xyz + r0.xyz;
  r1.x = dot(r1.xyz, r1.xyz);
  r2.xyz = -cb2[1].xyz + r0.xyz;
  r1.y = dot(r2.xyz, r2.xyz);
  r2.xyz = -cb2[2].xyz + r0.xyz;
  r1.z = dot(r2.xyz, r2.xyz);
  r2.xyz = -cb2[3].xyz + r0.xyz;
  r1.w = dot(r2.xyz, r2.xyz);
  r1.xyzw = cmp(r1.xyzw < cb2[4].xyzw);
  r2.xyz = r1.xyz ? float3(-1,-1,-1) : float3(-0,-0,-0);
  r1.xyzw = r1.xyzw ? float4(1,1,1,1) : 0;
  r2.xyz = r1.yzw + r2.xyz;
  r1.yzw = max(float3(0,0,0), r2.xyz);
  r2.xyz = cb2[13].xyz * r0.yyy;
  r2.xyz = cb2[12].xyz * r0.xxx + r2.xyz;
  r2.xyz = cb2[14].xyz * r0.zzz + r2.xyz;
  r2.xyz = cb2[15].xyz * r0.www + r2.xyz;
  r2.xyz = r2.xyz * r1.yyy;
  r3.xyz = cb2[9].xyz * r0.yyy;
  r3.xyz = cb2[8].xyz * r0.xxx + r3.xyz;
  r3.xyz = cb2[10].xyz * r0.zzz + r3.xyz;
  r3.xyz = cb2[11].xyz * r0.www + r3.xyz;
  r2.xyz = r3.xyz * r1.xxx + r2.xyz;
  r1.x = dot(r1.xyzw, float4(1,1,1,1));
  r3.xyz = cb2[17].xyz * r0.yyy;
  r3.xyz = cb2[16].xyz * r0.xxx + r3.xyz;
  r3.xyz = cb2[18].xyz * r0.zzz + r3.xyz;
  r3.xyz = cb2[19].xyz * r0.www + r3.xyz;
  r2.xyz = r3.xyz * r1.zzz + r2.xyz;
  r3.xyz = cb2[21].xyz * r0.yyy;
  r3.xyz = cb2[20].xyz * r0.xxx + r3.xyz;
  r3.xyz = cb2[22].xyz * r0.zzz + r3.xyz;
  r3.xyz = cb2[23].xyz * r0.www + r3.xyz;
  r0.xyz = -cb2[25].xyz + r0.xyz;
  r0.x = dot(r0.xyz, r0.xyz);
  r0.x = sqrt(r0.x);
  r0.x = saturate(r0.x * cb2[24].z + cb2[24].w);
  r0.yzw = r3.xyz * r1.www + r2.xyz;
  r0.yz = r0.yz * cb0[3].zw + float2(0.5,0.5);
  r0.w = r0.w + -r1.x;
  r0.w = 1 + r0.w;
  r1.xy = frac(r0.yz);
  r0.yz = floor(r0.yz);
  r0.yz = float2(-0.5,-0.5) + r0.yz;
  r1.zw = -r1.xy * float2(3,3) + float2(4,4);
  r2.x = 3 * r1.x;
  r2.xz = r2.xx * float2(-1,1) + float2(4,1);
  r2.y = 7;
  r3.xyz = r2.xyz * r1.www;
  r4.xy = -r1.xy * float2(2,2) + float2(3,3);
  r1.zw = r4.xy / r1.zw;
  r4.xy = float2(-2,-2) + r1.zw;
  r5.z = r4.y;
  r1.zw = r1.xy * float2(3,3) + float2(1,1);
  r6.xy = r1.xy / r1.zw;
  r7.xyz = r2.xyz * r1.www;
  r1.zw = float2(7,7) * r2.xz;
  r1.xy = float2(3,3) + r1.xy;
  r5.xy = cb0[3].xy * r1.xy;
  r1.xy = float2(2,2) + r6.xy;
  r5.w = r1.y;
  r4.w = r1.x;
  r2.xz = cb0[3].yy;
  r2.y = 0.142857149;
  r2.xyz = r5.zyw * r2.xyz;
  r4.z = r5.x;
  r5.w = r2.x;
  r6.xz = cb0[3].xx;
  r6.y = 0.142857149;
  r5.xyz = r6.yxz * r4.zxw;
  r4.xyzw = r0.yzyz * cb0[3].xyxy + r5.ywxw;
  r1.xy = r0.yz * cb0[3].xy + r5.zw;
  r1.x = t1.SampleCmpLevelZero(s1_s, r1.xy, r0.w).x;
  r1.y = t1.SampleCmpLevelZero(s1_s, r4.xy, r0.w).x;
  r2.x = t1.SampleCmpLevelZero(s1_s, r4.zw, r0.w).x;
  r2.x = r3.y * r2.x;
  r1.y = r3.x * r1.y + r2.x;
  r1.x = r3.z * r1.x + r1.y;
  r2.w = r5.y;
  r3.xyzw = r0.yzyz * cb0[3].xyxy + r2.wywz;
  r5.yw = r2.yz;
  r1.y = t1.SampleCmpLevelZero(s1_s, r3.xy, r0.w).x;
  r2.x = t1.SampleCmpLevelZero(s1_s, r3.zw, r0.w).x;
  r1.x = r1.z * r1.y + r1.x;
  r3.xyzw = r0.yzyz * cb0[3].xyxy + r5.xyzy;
  r4.xyzw = r0.yzyz * cb0[3].xyxy + r5.xwzw;
  r0.y = t1.SampleCmpLevelZero(s1_s, r3.xy, r0.w).x;
  r0.z = t1.SampleCmpLevelZero(s1_s, r3.zw, r0.w).x;
  r0.y = r0.y * 49 + r1.x;
  r0.y = r1.w * r0.z + r0.y;
  r0.y = r7.x * r2.x + r0.y;
  r0.z = t1.SampleCmpLevelZero(s1_s, r4.xy, r0.w).x;
  r0.w = t1.SampleCmpLevelZero(s1_s, r4.zw, r0.w).x;
  r0.y = r7.y * r0.z + r0.y;
  r0.y = r7.z * r0.w + r0.y;
  r0.y = 0.0069444445 * r0.y;
  r0.z = 1 + -cb2[24].x;
  r0.y = r0.y * r0.z + cb2[24].x;
  o0.xyzw = r0.yyyy + r0.xxxx;
  return;
}
