// hlsltool.py -I ../.. --fix-unity-lighting-ps=TEXCOORD4 --only-autofixed --fxc /home/rshan/fxc.exe 996f9165c668dab7-ps_replace.txt
// 3DMigoto: 996f9165c668dab7 |    Unity headers extracted from Internal-PrePassCollectShadows.shader
//    Shader "Hidden/Internal-PrePassCollectShadows" {
//      Properties {
//       _ShadowMapTexture ("", any) = "" { }
//      }
//      Fallback Off
//      SubShader 2/2 {
//        Tags { "ShadowmapFilter"="PCF_5x5" }
//        Pass 1/1 {
//          Tags { "ShadowmapFilter"="PCF_5x5" }
//          ZTest Always
//          ZWrite Off
//          Cull Off
//          GpuProgramID 128839
//          Program "fp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 93
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Internal-PrePassCollectShadows.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 56 1 0
//   Keywords { "SHADOWS_NATIVE" "SHADOWS_SINGLE_CASCADE" }
//   undeciphered2: 1 2 4 2 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 128
//   Vector 112 [_ShadowMapTexture_TexelSize]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 112 [_ZBufferParams]
//   Vector 128 [unity_OrthoParams]
//   ConstBuffer "UnityShadows" 416
//   Vector 384 [_LightShadowData]
//   Matrix 128 [unity_World2Shadow0]
//   Matrix 192 [unity_World2Shadow1]
//   Matrix 256 [unity_World2Shadow2]
//   Matrix 320 [unity_World2Shadow3]
//   ConstBuffer "UnityPerCamera2" 64
//   Matrix 0 [_CameraToWorld]
//   SetTexture 0 [_CameraDepthTexture] 2D 0
//   SetTexture 1 [_ShadowMapTexture] 2D 1
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityShadows" 2
//   BindCB "UnityPerCamera2" 3
//
// Headers extracted with DarkStarSword's extract_unity53_shaders.py
// https://raw.githubusercontent.com/DarkStarSword/3d-fixes/master/extract_unity53_shaders.py

// ---- Created with 3Dmigoto v1.2.30 on Wed Oct 26 06:16:05 2016
Texture2D<float4> t1 : register(t1);

Texture2D<float4> t0 : register(t0);

SamplerComparisonState s1_s : register(s1);

SamplerState s0_s : register(s0);

cbuffer cb3 : register(b3)
{
  float4 cb3[4];
}

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

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

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




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



cbuffer UnityPerDraw : register(b11) {
	float4x4 glstate_matrix_mvp;
	float4x4 glstate_matrix_modelview0;
	float4x4 glstate_matrix_invtrans_modelview0;
	#define UNITY_MATRIX_MVP glstate_matrix_mvp
	#define UNITY_MATRIX_MV glstate_matrix_modelview0
	#define UNITY_MATRIX_IT_MV glstate_matrix_invtrans_modelview0

	uniform float4x4 _Object2World;
	uniform float4x4 _World2Object;
	uniform float4 unity_LODFade; // x is the fade value ranging within [0,1]. y is x quantized into 16 levels
	uniform float4 unity_WorldTransformParams; // w is usually 1.0, or -1.0 for odd-negative scale transforms
}


#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,
  float fov : TEXCOORD4)
{
  float4 r0,r1,r2,r3,r4,r5,r6,r7;
  uint4 bitmask, uiDest;
  float4 fDest;

float4 stereo = StereoParams.Load(0);
float separation = stereo.x, convergence = stereo.y, eye = stereo.z;

  r0.xz = cb0[7].yy;
  r0.y = 0.142857149;
  r1.xyzw = t0.Sample(s0_s, v0.xy).xyzw;
  r0.w = cb1[7].x * r1.x + cb1[7].y;
  r0.w = 1 / r0.w;
  r1.y = r1.x + -r0.w;
  r0.w = cb1[8].w * r1.y + r0.w;
  r1.yzw = v3.xyz + -v2.xyz;
  r1.xyz = r1.xxx * r1.yzw + v2.xyz;
  r1.xyz = -v1.xyz * r0.www + r1.xyz;
  r2.xyz = v1.xyz * r0.www;
  r1.xyz = cb1[8].www * r1.xyz + r2.xyz;
float depth = r1.z;

// Unity light/shadow fix (pixel shader stage) inserted with DarkStarSword's hlsltool.py:
// hlsltool.py -I ../.. --fix-unity-lighting-ps=TEXCOORD4 --only-autofixed --fxc /home/rshan/fxc.exe 996f9165c668dab7-ps_replace.txt
if (fov) {
  r1.x -= separation * (depth - convergence) * fov;
}

  r2.xyzw = cb3[1].xyzw * r1.yyyy;
  r2.xyzw = cb3[0].xyzw * r1.xxxx + r2.xyzw;
  r2.xyzw = cb3[2].xyzw * r1.zzzz + r2.xyzw; // depth in r1.z
  r0.w = saturate(r1.z * cb2[24].z + cb2[24].w);
  r1.xyzw = cb3[3].xyzw + r2.xyzw;

// Fallback adjustment if the FOV was not passed from the VS:
if (!fov) {
  float4 clip_space_adj = float4(separation * (depth - convergence), 0, 0, 0);
  float4 local_space_adj = mul(inverse(glstate_matrix_mvp), clip_space_adj);
  float4 world_space_adj = mul(_Object2World, local_space_adj);
  r1.xyz -= world_space_adj.xyz;
}

  r2.xyz = cb2[9].xyz * r1.yyy;
  r2.xyz = cb2[8].xyz * r1.xxx + r2.xyz;
  r1.xyz = cb2[10].xyz * r1.zzz + r2.xyz;
  r1.xyz = cb2[11].xyz * r1.www + r1.xyz;
  r1.xy = r1.xy * cb0[7].zw + float2(0.5,0.5);
  r2.xy = frac(r1.xy);
  r1.xy = floor(r1.xy);
  r1.xy = float2(-0.5,-0.5) + r1.xy;
  r2.zw = -r2.xy * float2(2,2) + float2(3,3);
  r3.xy = -r2.xy * float2(3,3) + float2(4,4);
  r2.zw = r2.zw / r3.xy;
  r4.xy = float2(-2,-2) + r2.zw;
  r5.z = r4.y;
  r2.zw = r2.xy * float2(3,3) + float2(1,1);
  r3.xz = r2.xy / r2.zw;
  r5.xw = float2(2,2) + r3.xz;
  r4.w = r5.x;
  r2.yz = float2(3,3) + r2.xy;
  r1.w = 3 * r2.x;
  r6.xz = r1.ww * float2(-1,1) + float2(4,1);
  r5.xy = cb0[7].xy * r2.yz;
  r7.xyz = r5.zyw * r0.xyz;
  r4.z = r5.x;
  r5.w = r7.x;
  r0.xz = cb0[7].xx;
  r0.y = 0.142857149;
  r5.xyz = r4.zxw * r0.yxz;
  r4.xyzw = r1.xyxy * cb0[7].xyxy + r5.ywxw;
  r0.xy = r1.xy * cb0[7].xy + r5.zw;
  r0.x = t1.SampleCmpLevelZero(s1_s, r0.xy, r1.z).x;
  r0.y = t1.SampleCmpLevelZero(s1_s, r4.xy, r1.z).x;
  r0.z = t1.SampleCmpLevelZero(s1_s, r4.zw, r1.z).x;
  r6.y = 7;
  r2.xyz = r6.xyz * r3.yyy;
  r3.xyz = r6.xyz * r2.www;
  r4.xy = float2(7,7) * r6.xz;
  r0.z = r2.y * r0.z;
  r0.y = r2.x * r0.y + r0.z;
  r0.x = r2.z * r0.x + r0.y;
  r7.w = r5.y;
  r2.xyzw = r1.xyxy * cb0[7].xyxy + r7.wywz;
  r5.yw = r7.yz;
  r0.y = t1.SampleCmpLevelZero(s1_s, r2.xy, r1.z).x;
  r0.z = t1.SampleCmpLevelZero(s1_s, r2.zw, r1.z).x;
  r0.x = r4.x * r0.y + r0.x;
  r2.xyzw = r1.xyxy * cb0[7].xyxy + r5.xyzy;
  r5.xyzw = r1.xyxy * cb0[7].xyxy + r5.xwzw;
  r0.y = t1.SampleCmpLevelZero(s1_s, r2.xy, r1.z).x;
  r1.x = t1.SampleCmpLevelZero(s1_s, r2.zw, r1.z).x;
  r0.x = r0.y * 49 + r0.x;
  r0.x = r4.y * r1.x + r0.x;
  r0.x = r3.x * r0.z + r0.x;
  r0.y = t1.SampleCmpLevelZero(s1_s, r5.xy, r1.z).x;
  r0.z = t1.SampleCmpLevelZero(s1_s, r5.zw, r1.z).x;
  r0.x = r3.y * r0.y + r0.x;
  r0.x = r3.z * r0.z + r0.x;
  r0.x = 0.0069444445 * r0.x;
  r0.y = 1 + -cb2[24].x;
  r0.x = r0.x * r0.y + cb2[24].x;
  o0.xyzw = r0.xxxx + r0.wwww;
  return;
}
