// 3DMigoto: 94a6d6474c5424ae | Matched 14 variants of 2 shaders: Hidden/Internal-DeferredReflections, Hidden/Internal-PrePassLighting
//
// 0001:    Unity headers extracted from Internal-DeferredReflections.shader
// 3ffe:    Unity headers extracted from Internal-PrePassLighting.shader
// 0001:    Shader "Hidden/Internal-DeferredReflections" {
// 3ffe:    Shader "Hidden/Internal-PrePassLighting" {
//            Properties {
// 3ffe:       _LightTexture0 ("", any) = "" { }
// 0001:       _SrcBlend ("", Float) = 1.000000
// 0001:       _DstBlend ("", Float) = 1.000000
// 3ffe:       _LightTextureB0 ("", 2D) = "" { }
// 3ffe:       _ShadowMapTexture ("", any) = "" { }
//            }
//            Fallback Off
//            SubShader 1/1 {
// 0001:        Pass 1/2 {
// 3ffe:        Pass 1/3 {
// 3ffe:          Tags { "SHADOWSUPPORT"="true" }
//                ZWrite Off
// 3ffe:          Blend DstColor Zero
// 0001:          Blend [_SrcBlend] [_DstBlend]
// 3ffe:          GpuProgramID 35985
// 0001:          GpuProgramID 54546
//                Program "vp" {
//                  SubProgram "d3d11 " {
// 0003:              GpuProgramIndex 1
// 0040:              GpuProgramIndex 11
// 0080:              GpuProgramIndex 13
// 0100:              GpuProgramIndex 15
// 0200:              GpuProgramIndex 17
// 0400:              GpuProgramIndex 19
// 0800:              GpuProgramIndex 21
// 1000:              GpuProgramIndex 23
// 2000:              GpuProgramIndex 25
// 0004:              GpuProgramIndex 3
// 0008:              GpuProgramIndex 5
// 0010:              GpuProgramIndex 7
// 0020:              GpuProgramIndex 9
//                  }
//                }
//              }
//            }
//          }
//
// 0001: Unity 5.3 headers extracted from Internal-DeferredReflections.shader.decompressed:
// 3ffe: Unity 5.3 headers extracted from Internal-PrePassLighting.shader.decompressed:
//         API d3d11
//         Shader model vs_4_0
//         undeciphered1: 201510240 14 0 0
// 0080:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" }
// 0004:   Keywords { "DIRECTIONAL" }
// 0100:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" }
// 0020:   Keywords { "DIRECTIONAL_COOKIE" }
// 1000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 0200:   Keywords { "POINT" "SHADOWS_CUBE" }
// 0002:   Keywords { "POINT" }
// 2000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 0400:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" }
// 0010:   Keywords { "POINT_COOKIE" }
// 0800:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" }
// 0040:   Keywords { "SPOT" "SHADOWS_DEPTH" }
// 0008:   Keywords { "SPOT" }
//         undeciphered2: 1 0 3 0 0 0
//         undeciphered3: 3 2 0 0 1 2
// 37fe:   ConstBuffer "$Globals" 256
// 0001:   ConstBuffer "$Globals" 272
// 0800:   ConstBuffer "$Globals" 320
//         Float 96 [_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_unity53_shaders.py
// https://raw.githubusercontent.com/DarkStarSword/3d-fixes/master/extract_unity53_shaders.py

// ---- Created with 3Dmigoto v1.2.42 on Sun Nov 06 16:57:51 2016

// 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[7];
}


// 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[6].xxx * r0.xyz + r1.xyz;
  return;
}
