// hlsltool.py -I ../112 --fix-unity-lighting-ps --only-autofixed --no-validate --ignore-other-errors bd751537715a6e17-ps_replace.txt
// 3DMigoto: bd751537715a6e17 |    Unity headers extracted from Internal-DeferredShading.shader
//    Shader "Hidden/Internal-DeferredShading" {
//      Properties {
//       _LightTexture0 ("", any) = "" { }
//       _LightTextureB0 ("", 2D) = "" { }
//       _ShadowMapTexture ("", any) = "" { }
//       _SrcBlend ("", Float) = 1
//       _DstBlend ("", Float) = 1
//      }
//      Fallback Off
//      SubShader 1/1 {
//        Pass 1/2 {
//          Tags { "SHADOWSUPPORT"="true" }
//          ZWrite Off
//          Blend [_SrcBlend] [_DstBlend]
//          GpuProgramID 36028
//          Program "fp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 57
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Internal-DeferredShading.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 201509030 96 5 0
//   Keywords { "SPOT" "SHADOWS_OFF" }
//   undeciphered2: 1 6 3 6 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 272
//   Vector 48 [unity_ColorSpaceLuminance]
//   Vector 128 [_LightPos]
//   Vector 144 [_LightColor]
//   Matrix 176 [_LightMatrix0]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   Vector 80 [_ProjectionParams]
//   Vector 112 [_ZBufferParams]
//   ConstBuffer "UnityPerCamera2" 64
//   Matrix 0 [_CameraToWorld]
//   SetTexture 0 [_CameraDepthTexture] 2D 0
//   SetTexture 1 [_LightTexture0] 2D 2
//   SetTexture 2 [_LightTextureB0] 2D 1
//   SetTexture 3 [_CameraGBufferTexture0] 2D 3
//   SetTexture 4 [_CameraGBufferTexture1] 2D 4
//   SetTexture 5 [_CameraGBufferTexture2] 2D 5
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityPerCamera2" 2
//
// 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:32:42 2017
Texture2D<float4> t5 : register(t5);

Texture2D<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 s5_s : register(s5);

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 cb2 : register(b2)
{
  float4 cb2[4];
}

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

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




// 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;
  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 bd751537715a6e17-ps_replace.txt
matrix _CameraToWorld = matrix(cb2[0], cb2[1], cb2[2], cb2[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(s0_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 bd751537715a6e17-ps_replace.txt
if (fov) {
  r0.x -= separation * (depth - convergence) * fov;
}

  r2.xyz = cb2[1].xyz * r0.yyy;
  r0.xyw = cb2[0].xyz * r0.xxx + r2.xyz;
  r0.xyz = cb2[2].xyz * r0.zzz + r0.xyw; // depth in r0.z
  r0.xyz = cb2[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.xyz = -_WorldSpaceCameraPos.xyz + r0.xyz;
  r0.w = dot(r2.xyz, r2.xyz);
  r0.w = rsqrt(r0.w);
  r2.xyz = r2.xyz * r0.www;
  r3.xyzw = t5.Sample(s5_s, r1.xy).xyzw;
  r3.xyz = r3.xyz * float3(2,2,2) + float3(-1,-1,-1);
  r0.w = dot(r3.xyz, r3.xyz);
  r0.w = rsqrt(r0.w);
  r3.xyz = r3.xyz * r0.www;
  r0.w = dot(r3.xyz, -r2.xyz);
  r1.z = 9.99999975e-006 + -r0.w;
  r0.w = cmp(r0.w < 0);
  r4.xyz = -r2.xyz * r1.zzz + r3.xyz;
  r3.xyz = r0.www ? r4.xyz : r3.xyz;
  r4.xyz = cb0[8].xyz + -r0.xyz;
  r0.w = dot(r4.xyz, r4.xyz);
  r1.z = rsqrt(r0.w);
  r0.w = cb0[8].w * r0.w;
  r5.xyzw = t2.Sample(s1_s, r0.ww).xyzw;
  r5.yzw = r4.xyz * r1.zzz + -r2.xyz;
  r0.w = dot(r3.xyz, -r2.xyz);
  r0.w = max(0, r0.w);
  r2.xyz = r4.xyz * r1.zzz;
  r1.z = dot(r5.yzw, r5.yzw);
  r1.z = max(0.00100000005, r1.z);
  r1.z = rsqrt(r1.z);
  r4.xyz = r5.yzw * r1.zzz;
  r1.z = dot(r3.xyz, r4.xyz);
  r1.w = dot(r3.xyz, r2.xyz);
  r2.x = dot(r2.xyz, r4.xyz);
  r2.x = max(0, r2.x);
  r1.zw = max(float2(0,0), r1.zw);
  r1.z = r1.z * r1.z;
  r3.xyzw = t4.Sample(s4_s, r1.xy).xyzw;
  r4.xyzw = t3.Sample(s3_s, r1.xy).xyzw;
  r6.xyzw = float4(1,1,1,1) + -r3.wxyz;
  r1.x = r6.x * r6.x;
  r1.y = r1.x * r1.x + -1;
  r1.y = r1.z * r1.y + 1;
  r1.z = 3.14159274 * r1.y;
  r1.y = r1.z * r1.y + 1.00000001e-007;
  r1.z = r1.x * r1.x;
  r1.y = r1.z / r1.y;
  r1.z = -r6.x * r6.x + 1;
  r2.y = r0.w * r1.z + r1.x;
  r1.x = r1.w * r1.z + r1.x;
  r1.x = r1.x * r0.w;
  r0.w = 1 + -r0.w;
  r1.x = r1.w * r2.y + r1.x;
  r1.x = 9.99999975e-006 + r1.x;
  r1.x = 2 / r1.x;
  r1.x = r1.x * r1.y;
  r1.x = 0.785398185 * r1.x;
  r1.y = max(9.99999975e-005, r1.x);
  r1.y = sqrt(r1.y);
  r1.z = cmp(cb0[3].w == 0.000000);
  r1.x = r1.z ? r1.y : r1.x;
  r1.x = r1.x * r1.w;
  r1.x = max(0, r1.x);
  r2.yzw = cb0[12].xyw * r0.yyy;
  r2.yzw = cb0[11].xyw * r0.xxx + r2.yzw;
  r0.xyz = cb0[13].xyw * r0.zzz + r2.yzw;
  r0.xyz = cb0[14].xyw + r0.xyz;
  r0.xy = r0.xy / r0.zz;
  r0.z = cmp(r0.z < 0);
  r0.z = r0.z ? 1.000000 : 0;
  r7.xyzw = t1.SampleBias(s2_s, r0.xy, -8).xyzw;
  r0.x = r7.w * r0.z;
  r0.x = r0.x * r5.x;
  r0.xyz = cb0[9].xyz * r0.xxx;
  r1.xyz = r1.xxx * r0.xyz;
  r2.y = 1 + -r2.x;
  r2.xz = r2.xy * r2.xy;
  r2.x = dot(r2.xx, r6.xx);
  r2.x = -0.5 + r2.x;
  r2.z = r2.z * r2.z;
  r2.y = r2.z * r2.y;
  r2.yzw = r6.yzw * r2.yyy + r3.xyz;
  r1.xyz = r2.yzw * r1.xyz;
  r2.y = r0.w * r0.w;
  r2.y = r2.y * r2.y;
  r0.w = r2.y * r0.w;
  r0.w = r2.x * r0.w + 1;
  r2.y = 1 + -r1.w;
  r2.z = r2.y * r2.y;
  r2.z = r2.z * r2.z;
  r2.y = r2.z * r2.y;
  r2.x = r2.x * r2.y + 1;
  r0.w = r2.x * r0.w;
  r0.w = r0.w * r1.w;
  r0.xyz = r0.xyz * r0.www;
  r0.xyz = r4.xyz * r0.xyz + r1.xyz;
  r0.w = 1;
  o0.xyzw = exp2(-r0.xyzw);
  return;
}
