// 3DMigoto: 4c9936f7f1d28f6e | Matched 40 variants of 3 shaders: Hidden/Internal-DeferredReflections, Hidden/Internal-DeferredShading, Hidden/Internal-PrePassLighting
//
// 8000000000:    Unity headers extracted from Hidden_Internal-DeferredReflections
// 7fffffe000:    Unity headers extracted from Hidden_Internal-DeferredShading
// 0000001fff:    Unity headers extracted from Hidden_Internal-PrePassLighting
// 8000000000:    Shader "Hidden/Internal-DeferredReflections" {
// 7fffffe000:    Shader "Hidden/Internal-DeferredShading" {
// 0000001fff:    Shader "Hidden/Internal-PrePassLighting" {
//                  SubShader 1/1 {
// ffffffe000:        Pass 1/2 {
// 0000001fff: 1:     Pass 1/2 {
// 0000001fff: 2:     Pass 2/2 {
// 7fffffffff:          Tags { "SHADOWSUPPORT"="true" }
//                      ZWrite Off
//                      Program "vp" {
//                        SubProgram "d3d11" {
//                        }
//                      }
//                    }
//                  }
//                }
//
// 8000000000: Unity 5.3 headers extracted from Hidden_Internal-DeferredReflections:
// 7fffffe000: Unity 5.3 headers extracted from Hidden_Internal-DeferredShading:
// 0000001fff: Unity 5.3 headers extracted from Hidden_Internal-PrePassLighting:
//               API d3d11
//               Shader model vs_4_0
//               undeciphered1: 201609010 18 0 0
//               undeciphered1a: 3
// 0200000000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
// 0000100080:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" }
// 0008000000:   Keywords { "DIRECTIONAL" "UNITY_HDR_ON" }
// 0000004002:   Keywords { "DIRECTIONAL" }
// 0400000000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
// 0000200100:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" }
// 0040000000:   Keywords { "DIRECTIONAL_COOKIE" "UNITY_HDR_ON" }
// 0000020010:   Keywords { "DIRECTIONAL_COOKIE" }
// 1000000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 0000800400:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 0800000000:   Keywords { "POINT" "SHADOWS_CUBE" "UNITY_HDR_ON" }
// 0000400200:   Keywords { "POINT" "SHADOWS_CUBE" }
// 0004000000:   Keywords { "POINT" "UNITY_HDR_ON" }
// 0000002001:   Keywords { "POINT" }
// 4000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 0002001000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 2000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "UNITY_HDR_ON" }
// 0001000800:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" }
// 0020000000:   Keywords { "POINT_COOKIE" "UNITY_HDR_ON" }
// 0000010008:   Keywords { "POINT_COOKIE" }
// 0100000000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 0000080040:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" }
// 0080000000:   Keywords { "SPOT" "SHADOWS_DEPTH" "UNITY_HDR_ON" }
// 0000040020:   Keywords { "SPOT" "SHADOWS_DEPTH" }
// 0010000000:   Keywords { "SPOT" "UNITY_HDR_ON" }
// 0000008004:   Keywords { "SPOT" }
//               undeciphered2: 1 0 4 0 0 0
//               undeciphered3: 3 2 0 0 1 2
// 0000001fbf:   ConstBuffer "$Globals" 192
// fefff7e000:   ConstBuffer "$Globals" 208
// 0000000040:   ConstBuffer "$Globals" 256
// 0100080000:   ConstBuffer "$Globals" 272
//               Float 32 [_LightAsQuad]
//               ConstBuffer "UnityPerCamera" 144
//               Vector 80 [_ProjectionParams]
//               ConstBuffer "UnityPerDraw" 160
//               Matrix 0 [unity_ObjectToWorld]
//               ConstBuffer "UnityPerFrame" 384
//               Matrix 144 [unity_MatrixV]
//               Matrix 272 [unity_MatrixVP]
//               BindCB "$Globals" 0
//               BindCB "UnityPerCamera" 1
//               BindCB "UnityPerDraw" 2
//               BindCB "UnityPerFrame" 3
//
// Headers extracted with DarkStarSword's extract_unity55_shaders.py
// https://github.com/DarkStarSword/3d-fixes

// ---- Created with 3Dmigoto v1.2.65 on Thu Jan 25 16:19:14 2018

// Replace definition with UnityPerFrame from Unity Cg source code:
// Note that matrix order is transposed between Cg and HLSL, so add row_major
// keywords to each float4x4 entry
// NEW IN 5.6
#define fixed4 float4
cbuffer cb3 : register(b3)
{
  // float4 cb3[21];

    fixed4 glstate_lightmodel_ambient;
    fixed4 unity_AmbientSky;
    fixed4 unity_AmbientEquator;
    fixed4 unity_AmbientGround;
    fixed4 unity_IndirectSpecColor;

#if !defined(USING_STEREO_MATRICES)
    row_major float4x4 glstate_matrix_projection; // cb3[ 5: 8]
    row_major float4x4 unity_MatrixV;             // cb3[ 9:12]
    row_major float4x4 unity_MatrixInvV;          // cb3[13:16]
    row_major float4x4 unity_MatrixVP;            // cb3[17:20]
    float4 unity_StereoScaleOffset;
    int unity_StereoEyeIndex;
#endif

    fixed4 unity_ShadowColor;
}

// 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
// CHANGED IN 5.6
cbuffer cb2 : register(b2)
{
  // float4 cb2[4];

  row_major float4x4 unity_ObjectToWorld;
  row_major float4x4 unity_WorldToObject;
  float4 unity_LODFade; // x is the fade value ranging within [0,1]. y is x quantized into 16 levels
  float4 unity_WorldTransformParams; // w is usually 1.0, or -1.0 for odd-negative scale transforms
}

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,r2;
  uint4 bitmask, uiDest;
  float4 fDest;

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

  r0.xyzw = unity_ObjectToWorld[1].xyzw * v0.yyyy;
  r0.xyzw = unity_ObjectToWorld[0].xyzw * v0.xxxx + r0.xyzw;
  r0.xyzw = unity_ObjectToWorld[2].xyzw * v0.zzzz + r0.xyzw;
  r0.xyzw = unity_ObjectToWorld[3].xyzw + r0.xyzw;
  r1.xyzw = unity_MatrixVP[1].xyzw * r0.yyyy;
  r1.xyzw = unity_MatrixVP[0].xyzw * r0.xxxx + r1.xyzw;
  r1.xyzw = unity_MatrixVP[2].xyzw * r0.zzzz + r1.xyzw;
  r1.xyzw = unity_MatrixVP[3].xyzw * r0.wwww + r1.xyzw;
  o0.xyzw = r1.xyzw;

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

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

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

if (!full_screen) {
	// Looks like unity_MatrixInvV is 0, so we can't just do this:
	//    float4x4 projection = mul(unity_MatrixInvV, unity_MatrixVP);
	//    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_MatrixV);

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

	fov = 1 / dot(mvi, unity_MatrixVP._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;
}
