// hlsltool.py -I ../112 --fix-unity-lighting-ps --only-autofixed --no-validate --ignore-other-errors d86b43f6db87105c-ps_replace.txt
// 3DMigoto: d86b43f6db87105c |    Unity headers extracted from Internal-PrePassLighting.shader
//    Shader "Hidden/Internal-PrePassLighting" {
//      Properties {
//       _LightTexture0 ("", any) = "" { }
//       _LightTextureB0 ("", 2D) = "" { }
//       _ShadowMapTexture ("", any) = "" { }
//      }
//      Fallback Off
//      SubShader 1/1 {
//        Pass 1/3 {
//          Tags { "SHADOWSUPPORT"="true" }
//          ZWrite Off
//          Blend DstColor Zero
//          GpuProgramID 57115
//          Program "fp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 49
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Internal-PrePassLighting.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 201509030 64 7 0
//   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" }
//   undeciphered2: 1 4 5 4 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 240
//   Vector 48 [unity_ColorSpaceLuminance]
//   Vector 128 [_LightPos]
//   Vector 144 [_LightColor]
//   Vector 160 [unity_LightmapFade]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   Vector 80 [_ProjectionParams]
//   Vector 112 [_ZBufferParams]
//   ConstBuffer "UnityLighting" 720
//   Vector 16 [_LightPositionRange]
//   ConstBuffer "UnityShadows" 416
//   Vector 384 [_LightShadowData]
//   Vector 400 [unity_ShadowFadeCenterAndType]
//   ConstBuffer "UnityPerCamera2" 64
//   Matrix 0 [_CameraToWorld]
//   SetTexture 0 [_CameraDepthTexture] 2D 0
//   SetTexture 1 [_LightTextureB0] 2D 1
//   SetTexture 2 [_ShadowMapTexture] CUBE 2
//   SetTexture 3 [_CameraNormalsTexture] 2D 3
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityLighting" 2
//   BindCB "UnityShadows" 3
//   BindCB "UnityPerCamera2" 4
//
// 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:33:58 2017
Texture2D<float4> t3 : register(t3);

TextureCube<float4> t2 : register(t2);

Texture2D<float4> t1 : register(t1);

Texture2D<float4> t0 : register(t0);

SamplerState s3_s : register(s3);

SamplerState s2_s : register(s2);

SamplerState s1_s : register(s1);

SamplerState s0_s : register(s0);

cbuffer cb4 : register(b4)
{
  float4 cb4[4];
}

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

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

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

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




// 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;
  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 d86b43f6db87105c-ps_replace.txt
matrix _CameraToWorld = matrix(cb4[0], cb4[1], cb4[2], cb4[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.xyz = cb0[9].xyz * cb0[3].xyz;
  r0.xz = r0.xx + r0.yz;
  r0.y = r0.y * r0.z;
  r0.x = cb0[9].z * cb0[3].z + r0.x;
  r0.y = sqrt(r0.y);
  r0.y = dot(cb0[3].ww, r0.yy);
  r0.x = r0.x + r0.y;
  r0.yz = v1.xy / v1.ww;
  r1.xyzw = t0.Sample(s0_s, r0.yz).xyzw;
  r2.xyzw = t3.Sample(s3_s, r0.yz).xyzw;
  r0.y = cb1[7].x * r1.x + cb1[7].y;
  r0.y = 1 / r0.y;
  r0.z = cb1[5].z / v2.z;
  r1.xyz = v2.xyz * r0.zzz;
  r1.xyw = r1.xyz * r0.yyy;
float depth = r1.w;

// 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 d86b43f6db87105c-ps_replace.txt
if (fov) {
  r1.x -= separation * (depth - convergence) * fov;
}

  r3.xyz = cb4[1].xyz * r1.yyy;
  r3.xyz = cb4[0].xyz * r1.xxx + r3.xyz;
  r3.xyz = cb4[2].xyz * r1.www + r3.xyz; // depth in r1.w
  r3.xyz = cb4[3].xyz + r3.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);
  r3.xyz -= world_space_adj.xyz;
}

  r4.xyz = -cb0[8].xyz + r3.xyz;
  r5.xyz = float3(0.0078125,0.0078125,0.0078125) + r4.xyz;
  r5.xyzw = t2.Sample(s2_s, r5.xyz).xyzw;
  r6.xyz = float3(-0.0078125,-0.0078125,0.0078125) + r4.xyz;
  r6.xyzw = t2.Sample(s2_s, r6.xyz).xyzw;
  r5.y = r6.x;
  r6.xyz = float3(-0.0078125,0.0078125,-0.0078125) + r4.xyz;
  r6.xyzw = t2.Sample(s2_s, r6.xyz).xyzw;
  r5.z = r6.x;
  r6.xyz = float3(0.0078125,-0.0078125,-0.0078125) + r4.xyz;
  r6.xyzw = t2.Sample(s2_s, r6.xyz).xyzw;
  r5.w = r6.x;
  r0.z = dot(r4.xyz, r4.xyz);
  r0.w = sqrt(r0.z);
  r0.w = cb2[1].w * r0.w;
  r0.w = 0.970000029 * r0.w;
  r5.xyzw = cmp(r5.xyzw < r0.wwww);
  r5.xyzw = r5.xyzw ? cb3[24].xxxx : float4(1,1,1,1);
  r0.w = dot(r5.xyzw, float4(0.25,0.25,0.25,0.25));
  r1.x = cb0[8].w * r0.z;
  r0.z = rsqrt(r0.z);
  r4.xyz = r4.xyz * r0.zzz;
  r5.xyzw = t1.Sample(s1_s, r1.xx).xyzw;
  r0.z = r5.x * r0.w;
  r0.w = saturate(r0.z);
  r5.xyz = -_WorldSpaceCameraPos.xyz + r3.xyz;
  r3.xyz = -cb3[25].xyz + r3.xyz;
  r1.x = dot(r3.xyz, r3.xyz);
  r1.x = sqrt(r1.x);
  r0.y = -r1.z * r0.y + r1.x;
  r0.y = cb3[25].w * r0.y + r1.w;
  r0.y = r0.y * cb0[10].z + cb0[10].w;
  r0.y = saturate(1 + -r0.y);
  r1.x = dot(r5.xyz, r5.xyz);
  r1.x = rsqrt(r1.x);
  r1.xyz = -r5.xyz * r1.xxx + -r4.xyz;
  r1.w = dot(r1.xyz, r1.xyz);
  r1.w = rsqrt(r1.w);
  r1.xyz = r1.xyz * r1.www;
  r2.xyz = r2.xyz * float3(2,2,2) + float3(-1,-1,-1);
  r1.w = 128 * r2.w;
  r2.w = dot(r2.xyz, r2.xyz);
  r2.w = rsqrt(r2.w);
  r2.xyz = r2.xyz * r2.www;
  r1.x = dot(r1.xyz, r2.xyz);
  r1.y = dot(-r4.xyz, r2.xyz);
  r1.y = max(0, r1.y);
  r0.z = r1.y * r0.z;
  r2.xyz = cb0[9].xyz * r0.zzz;
  r0.z = max(0, r1.x);
  r0.z = log2(r0.z);
  r0.z = r1.w * r0.z;
  r0.z = exp2(r0.z);
  r0.z = r0.z * r0.w;
  r2.w = r0.z * r0.x;
  r0.xyzw = r2.xyzw * r0.yyyy;
  o0.xyzw = exp2(-r0.xyzw);
  return;
}
