// 3DMigoto: 4a87a6742ed6d7a9 | Matched 222 variants of 3 shaders: Hidden/Internal-PrePassLighting, Hidden/LISDeferredReflections, Hidden/LISDeferredShading
//
// 0000000fff8000000000000000000000000000000000000000000000:    Unity headers extracted from Hidden_Internal-PrePassLighting
// 00000000004000000000000000000000000000000000000000000000:    Unity headers extracted from Hidden_LISDeferredReflections
// 3ffffff0003fffffffffffffffffffffffffffffffffffffffffffff:    Unity headers extracted from Hidden_LISDeferredShading
// 0000000fff8000000000000000000000000000000000000000000000:    Shader "Hidden/Internal-PrePassLighting" {
// 00000000004000000000000000000000000000000000000000000000:    Shader "Hidden/LISDeferredReflections" {
// 3ffffff0003fffffffffffffffffffffffffffffffffffffffffffff:    Shader "Hidden/LISDeferredShading" {
//                                                                SubShader 1/1 {
// 3ffffff0007fffffffffffffffffffffffffffffffffffffffffffff:        Pass 1/2 {
// 0000000fff8000000000000000000000000000000000000000000000: 1:     Pass 1/2 {
// 0000000fff8000000000000000000000000000000000000000000000: 2:     Pass 2/2 {
// 3fffffffffbfffffffffffffffffffffffffffffffffffffffffffff:          Tags { "SHADOWSUPPORT"="true" }
//                                                                    ZWrite Off
//                                                                    Program "vp" {
//                                                                      SubProgram "d3d11" {
//                                                                      }
//                                                                    }
//                                                                  }
//                                                                }
//                                                              }
//
// 0000000fff8000000000000000000000000000000000000000000000: Unity 5.3 headers extracted from Hidden_Internal-PrePassLighting:
// 00000000004000000000000000000000000000000000000000000000: Unity 5.3 headers extracted from Hidden_LISDeferredReflections:
// 3ffffff0003fffffffffffffffffffffffffffffffffffffffffffff: Unity 5.3 headers extracted from Hidden_LISDeferredShading:
//                                                             API d3d11
//                                                             Shader model vs_4_0
//                                                             undeciphered1: 201608170 14 0 0
//                                                             undeciphered1a: 2
// 00800000000080000020000008000002000000800000200000080000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
// 00000400200000040000010000004000001000000400000100000040:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" }
// 00040000000004000001000000400000100000040000010000004000:   Keywords { "DIRECTIONAL" "UNITY_HDR_ON" }
// 00000020010000002000000800000200000080000020000008000002:   Keywords { "DIRECTIONAL" }
// 01000000000100000040000010000004000001000000400000100000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
// 00000800400000080000020000008000002000000800000200000080:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" }
// 00200000000020000008000002000000800000200000080000020000:   Keywords { "DIRECTIONAL_COOKIE" "UNITY_HDR_ON" }
// 00000100080000010000004000001000000400000100000040000010:   Keywords { "DIRECTIONAL_COOKIE" }
// 10000000001000000400000100000040000010000004000001000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 00008004000000800000200000080000020000008000002000000800:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 02000000000200000080000020000008000002000000800000200000:   Keywords { "POINT" "SHADOWS_CUBE" "UNITY_HDR_ON" }
// 00001000800000100000040000010000004000001000000400000100:   Keywords { "POINT" "SHADOWS_CUBE" }
// 00020000000002000000800000200000080000020000008000002000:   Keywords { "POINT" "UNITY_HDR_ON" }
// 00000010008000001000000400000100000040000010000004000001:   Keywords { "POINT" }
// 20000000002000000800000200000080000020000008000002000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 00010008000001000000400000100000040000010000004000001000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 04000000000400000100000040000010000004000001000000400000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "UNITY_HDR_ON" }
// 00002001000000200000080000020000008000002000000800000200:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" }
// 00100000000010000004000001000000400000100000040000010000:   Keywords { "POINT_COOKIE" "UNITY_HDR_ON" }
// 00000080040000008000002000000800000200000080000020000008:   Keywords { "POINT_COOKIE" }
// 08000000000800000200000080000020000008000002000000800000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 00004002000000400000100000040000010000004000001000000400:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" }
// 00400000000040000010000004000001000000400000100000040000:   Keywords { "SPOT" "SHADOWS_DEPTH" "UNITY_HDR_ON" }
// 00000200100000020000008000002000000800000200000080000020:   Keywords { "SPOT" "SHADOWS_DEPTH" }
// 00080000000008000002000000800000200000080000020000008000:   Keywords { "SPOT" "UNITY_HDR_ON" }
// 00000040020000004000001000000400000100000040000010000004:   Keywords { "SPOT" }
//                                                             undeciphered2: 1 0 3 0 0 0
//                                                             undeciphered3: 3 2 0 0 1 2
// 0000000dff8000000000000000000000000000000000000000000000:   ConstBuffer "$Globals" 192
// 00000002000000000000000000000000000000000000000000000000:   ConstBuffer "$Globals" 256
// 37ffbff00077ffbffdffefff7ffbffdffefff7ffbffdffefff7ffbff:   ConstBuffer "$Globals" 272
// 08004000000800400200100080040020010008004002001000800400:   ConstBuffer "$Globals" 336
//                                                             Float 32 [_LightAsQuad]
//                                                             ConstBuffer "UnityPerCamera" 144
//                                                             Vector 80 [_ProjectionParams]
//                                                             ConstBuffer "UnityPerDraw" 352
//                                                             Matrix 0 [glstate_matrix_mvp]
//                                                             Matrix 64 [glstate_matrix_modelview0]
//                                                             BindCB "$Globals" 0
//                                                             BindCB "UnityPerCamera" 1
//                                                             BindCB "UnityPerDraw" 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 04:41:41 2018

// Replace definition with UnityPerDraw from Unity Cg source code:
// Note that matrix order is transposed between Cg and HLSL, so add row_major
// keywords to each float4x4 entry
cbuffer cb2 : register(b2)
{
  // float4 cb2[8];

  row_major float4x4 glstate_matrix_mvp;
  row_major float4x4 glstate_matrix_modelview0;
  row_major 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

  row_major uniform float4x4 _Object2World;
  row_major uniform float4x4 _World2Object;
  uniform float4 unity_LODFade; // x is the fade value ranging within [0,1]. y is x quantized into 16 levels
}

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

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


// Copied from the directional lighting shader using 3DMigoto
cbuffer UnityPerCameraRare : register(b13)
{
  uniform float4 unity_CameraWorldClipPlanes[6];

  // Projection matrices of the camera. Note that this might be different from projection matrix
  // that is set right now, e.g. while rendering shadows the matrices below are still the projection
  // of original camera.
  row_major uniform float4x4 unity_CameraProjection;
  row_major uniform float4x4 unity_CameraInvProjection;
}


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


void main( 
  float4 v0 : POSITION0,
  float3 v1 : NORMAL0,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : TEXCOORD0,
  out float3 o2 : TEXCOORD1,
  out float fov : TEXCOORD2) // New output to pass FOV to pixel shaders
{
  float4 r0,r1;
  uint4 bitmask, uiDest;
  float4 fDest;

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

  r0.xyzw = UNITY_MATRIX_MVP[1].xyzw * v0.yyyy;
  r0.xyzw = UNITY_MATRIX_MVP[0].xyzw * v0.xxxx + r0.xyzw;
  r0.xyzw = UNITY_MATRIX_MVP[2].xyzw * v0.zzzz + r0.xyzw;
  r0.xyzw = UNITY_MATRIX_MVP[3].xyzw + r0.xyzw;
  o0.xyzw = r0.xyzw;

bool full_screen = (r0.w == 1);
float depth = r0.w;

if (!full_screen) {
	r0.x += separation * (depth - convergence);
}

  r0.y = cb1[5].x * r0.y;
  r1.xzw = float3(0.5,0.5,0.5) * r0.xwy;
  o1.zw = r0.zw;
  o1.xy = r1.xw + r1.zz;
  r0.xyz = UNITY_MATRIX_MV[1].xyz * v0.yyy;
  r0.xyz = UNITY_MATRIX_MV[0].xyz * v0.xxx + r0.xyz;
  r0.xyz = UNITY_MATRIX_MV[2].xyz * v0.zzz + r0.xyz;
  r0.xyz = UNITY_MATRIX_MV[3].xyz + r0.xyz;

if (!full_screen) {
	// Looks like UNITY_MATRIX_IT_MV is 0, so we can't just do this:
	//    float4x4 projection = mul(UNITY_MATRIX_IT_MV, UNITY_MATRIX_MVP);
	//    fov = 1 / projection[0].x;

	// Instead, we have to calculate this the long way:

	// I already have an optimised version of this calculation in assembler for
	// Unity 4 (the committed version is for column-major order, but the optimised
	// row-major version was only slightly longer). I'm guessing the HLSL
	// determinant() function won't be optimised because it can't make assumptions
	// about the matrix (and that is a *large* amount of multiplications that could
	// be eliminated), so we can probably do better, but for now this is clearer:
	float det = 1 / determinant(UNITY_MATRIX_MV);

	float3 mvi;
	mvi.x = ((UNITY_MATRIX_MV._m11 * UNITY_MATRIX_MV._m22) - (UNITY_MATRIX_MV._m21 * UNITY_MATRIX_MV._m12)) * det;
	mvi.y = ((UNITY_MATRIX_MV._m21 * UNITY_MATRIX_MV._m02) - (UNITY_MATRIX_MV._m01 * UNITY_MATRIX_MV._m22)) * det;
	mvi.z = ((UNITY_MATRIX_MV._m01 * UNITY_MATRIX_MV._m12) - (UNITY_MATRIX_MV._m11 * UNITY_MATRIX_MV._m02)) * det;

	fov = 1 / dot(mvi, UNITY_MATRIX_MVP._m00_m10_m20);

	r0.x += separation * (depth - convergence) * fov;
} else {
	// Can't determine the FOV from the values passed to this shader, so
	// use the FOV copied from directional lighting shader with 3DMigoto.
	// We could probably get away with using only this and scrapping the
	// above code for games that always have directional lighting enabled.

	fov = unity_CameraInvProjection[0].x;
}

  r1.xyz = float3(-1,-1,1) * r0.xyz;
  r0.xyz = -r0.xyz * float3(-1,-1,1) + v1.xyz;
  o2.xyz = cb0[2].xxx * r0.xyz + r1.xyz;
  return;
}
