// hlsltool.py -I ../112 --fix-unity-lighting-ps --only-autofixed --no-validate --ignore-other-errors 63dbf5f0a45108e1-ps_replace.txt
// 3DMigoto: 63dbf5f0a45108e1 |    Unity headers extracted from Internal-DeferredReflections.shader
//    Shader "Hidden/Internal-DeferredReflections" {
//      Properties {
//       _SrcBlend ("", Float) = 1
//       _DstBlend ("", Float) = 1
//      }
//      Fallback Off
//      SubShader 1/1 {
//        Pass 1/2 {
//          ZWrite Off
//          Blend [_SrcBlend] [_DstBlend]
//          GpuProgramID 34194
//          Program "fp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 3
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Internal-DeferredReflections.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 201509030 79 4 1
//   undeciphered2: 1 5 4 5 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 272
//   Vector 48 [unity_ColorSpaceLuminance]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   Vector 80 [_ProjectionParams]
//   Vector 112 [_ZBufferParams]
//   ConstBuffer "UnityReflectionProbes" 128
//   Vector 0 [unity_SpecCube0_BoxMax]
//   Vector 16 [unity_SpecCube0_BoxMin]
//   Vector 32 [unity_SpecCube0_ProbePosition]
//   Vector 48 [unity_SpecCube0_HDR]
//   Vector 96 [unity_SpecCube1_ProbePosition]
//   ConstBuffer "UnityPerCamera2" 64
//   Matrix 0 [_CameraToWorld]
//   SetTexture 0 [_CameraDepthTexture] 2D 1
//   SetTexture 1 [_CameraGBufferTexture0] 2D 2
//   SetTexture 2 [_CameraGBufferTexture1] 2D 3
//   SetTexture 3 [_CameraGBufferTexture2] 2D 4
//   SetTexture 4 [unity_SpecCube0] CUBE 0
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityReflectionProbes" 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.38 on Thu Feb 09 12:28:31 2017
TextureCube<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 cb3 : register(b3)
{
  float4 cb3[4];
}

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

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

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




// 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 : SV_POSITION0,
  float4 v1 : TEXCOORD0,
  float3 v2 : TEXCOORD1,
  out float4 o0 : SV_Target0,
  float fov : TEXCOORD3)
{
  float4 r0,r1,r2,r3,r4,r5,r6,r7,r8;
  uint4 bitmask, uiDest;
  float4 fDest;

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

// Unity reflection/specular fix inserted with DarkStarSword's hlsltool.py:
// hlsltool.py -I ../112 --fix-unity-lighting-ps --only-autofixed --no-validate --ignore-other-errors 63dbf5f0a45108e1-ps_replace.txt
matrix _CameraToWorld = matrix(cb3[0], cb3[1], cb3[2], cb3[3]);
float4 _WorldSpaceCameraPos = cb1[4];
if (fov) {
  _WorldSpaceCameraPos.xyz -= mul(float4(-separation * convergence * fov, 0, 0, 0), _CameraToWorld).xyz;
} else {
  float4 clip_space_adj = float4(-separation * 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);
  _WorldSpaceCameraPos.xyz -= world_space_adj.xyz;
}

  r0.x = cb1[5].z / v2.z;
  r0.xyz = v2.xyz * r0.xxx;
  r1.xy = v1.xy / v1.ww;
  r2.xyzw = t0.Sample(s1_s, r1.xy).xyzw;
  r0.w = cb1[7].x * r2.x + cb1[7].y;
  r0.w = 1 / r0.w;
  r0.xyz = r0.xyz * r0.www;
float depth = r0.z;

// Unity light/shadow fix (pixel shader stage) inserted with DarkStarSword's hlsltool.py:
// hlsltool.py -I ../112 --fix-unity-lighting-ps --only-autofixed --no-validate --ignore-other-errors 63dbf5f0a45108e1-ps_replace.txt
if (fov) {
  r0.x -= separation * (depth - convergence) * fov;
}

  r2.xyz = cb3[1].xyz * r0.yyy;
  r0.xyw = cb3[0].xyz * r0.xxx + r2.xyz;
  r0.xyz = cb3[2].xyz * r0.zzz + r0.xyw; // depth in r0.z
  r0.xyz = cb3[3].xyz + r0.xyz;

// 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);
  r0.xyz -= world_space_adj.xyz;
}

  r2.xyzw = t1.Sample(s2_s, r1.xy).xyzw;
  r3.xyzw = t2.Sample(s3_s, r1.xy).xyzw;
  r1.xyzw = t3.Sample(s4_s, r1.xy).xyzw;
  r1.xyz = r1.xyz * float3(2,2,2) + float3(-1,-1,-1);
  r0.w = dot(r1.xyz, r1.xyz);
  r0.w = rsqrt(r0.w);
  r1.xyz = r1.xyz * r0.www;
  r2.xyz = -_WorldSpaceCameraPos.xyz + r0.xyz;
  r0.w = dot(r2.xyz, r2.xyz);
  r0.w = rsqrt(r0.w);
  r2.xyz = r2.xyz * r0.www;
  r0.w = max(r3.x, r3.y);
  r0.w = max(r0.w, r3.z);
  r0.w = 1 + -r0.w;
  r1.w = dot(r2.xyz, r1.xyz);
  r1.w = r1.w + r1.w;
  r4.xyz = r1.xyz * -r1.www + r2.xyz;
  r1.w = cmp(0 < cb2[2].w);
  if (r1.w != 0) {
    r5.xyz = -cb2[6].www + cb2[1].xyz;
    r6.xyz = cb2[6].www + cb2[0].xyz;
    r1.w = dot(r4.xyz, r4.xyz);
    r1.w = rsqrt(r1.w);
    r7.xyz = r4.xyz * r1.www;
    r6.xyz = r6.xyz + -r0.xyz;
    r6.xyz = r6.xyz / r7.xyz;
    r5.xyz = r5.xyz + -r0.xyz;
    r5.xyz = r5.xyz / r7.xyz;
    r8.xyz = cmp(float3(0,0,0) < r7.xyz);
    r5.xyz = r8.xyz ? r6.xyz : r5.xyz;
    r1.w = min(r5.x, r5.y);
    r1.w = min(r1.w, r5.z);
    r5.xyz = -cb2[2].xyz + r0.xyz;
    r4.xyz = r7.xyz * r1.www + r5.xyz;
  }
  r1.w = 1 + -r3.w;
  r4.w = -r1.w * 0.699999988 + 1.70000005;
  r4.w = r4.w * r1.w;
  r4.w = 6 * r4.w;
  r4.xyzw = t4.SampleLevel(s0_s, r4.xyz, r4.w).xyzw;
  r4.w = log2(r4.w);
  r4.w = cb2[3].y * r4.w;
  r4.w = exp2(r4.w);
  r4.w = cb2[3].x * r4.w;
  r4.xyz = r4.www * r4.xyz;
  r4.xyz = r4.xyz * r2.www;
  r2.w = dot(r1.xyz, -r2.xyz);
  r4.w = cmp(r2.w < 0);
  r2.w = 9.99999975e-006 + -r2.w;
  r5.xyz = -r2.xyz * r2.www + r1.xyz;
  r1.xyz = r4.www ? r5.xyz : r1.xyz;
  r1.x = dot(r1.xyz, -r2.xyz);
  r1.x = max(0, r1.x);
  r1.y = r1.w * r1.w;
  r1.z = cmp(cb0[3].w == 0.000000);
  r1.w = r1.w * r1.y;
  r1.w = -r1.w * 0.280000001 + 1;
  r1.y = r1.y * r1.y + 1;
  r1.y = 1 / r1.y;
  r1.y = r1.z ? r1.w : r1.y;
  r0.w = r3.w + -r0.w;
  r0.w = saturate(1 + r0.w);
  r1.yzw = r1.yyy * r4.xyz;
  r1.x = 1 + -r1.x;
  r2.x = r1.x * r1.x;
  r2.x = r2.x * r2.x;
  r1.x = r2.x * r1.x;
  r2.xyz = r0.www + -r3.xyz;
  r2.xyz = r1.xxx * r2.xyz + r3.xyz;
  o0.xyz = r2.xyz * r1.yzw;
  r1.xyz = -cb2[0].xyz + r0.xyz;
  r0.xyz = cb2[1].xyz + -r0.xyz;
  r0.xyz = max(r1.xyz, r0.xyz);
  r0.xyz = max(float3(0,0,0), r0.xyz);
  r0.x = dot(r0.xyz, r0.xyz);
  r0.x = sqrt(r0.x);
  r0.x = r0.x / cb2[6].w;
  o0.w = saturate(1 + -r0.x);
  return;
}
 