// 3DMigoto: 99341a34a001a3d1 | Matched 52 variants of 1 shaders: Hidden/Internal-DeferredShading
//
//                   Unity headers extracted from Internal-DeferredShading.shader
//                   Shader "Hidden/Internal-DeferredShading" {
//                     Properties {
//                      _LightTexture0 ("", any) = "" { }
//                      _LightTextureB0 ("", 2D) = "" { }
//                      _ShadowMapTexture ("", any) = "" { }
//                      _SrcBlend ("", Float) = 1.000000
//                      _DstBlend ("", Float) = 1.000000
//                     }
//                     Fallback Off
//                     SubShader 1/1 {
//                       Pass 1/2 {
//                         Tags { "SHADOWSUPPORT"="true" }
//                         ZWrite Off
//                         Blend [_SrcBlend] [_DstBlend]
//                         GpuProgramID 31668
//                         Program "vp" {
//                           SubProgram "d3d11 " {
// 0000004000001:              GpuProgramIndex 1
// 0000080000020:              GpuProgramIndex 11
// 0000100000040:              GpuProgramIndex 13
// 0000200000080:              GpuProgramIndex 15
// 0000400000100:              GpuProgramIndex 17
// 0000800000200:              GpuProgramIndex 19
// 0001000000400:              GpuProgramIndex 21
// 0002000000800:              GpuProgramIndex 23
// 0004000001000:              GpuProgramIndex 25
// 0008000002000:              GpuProgramIndex 27
// 0010000004000:              GpuProgramIndex 29
// 0000008000002:              GpuProgramIndex 3
// 0020000008000:              GpuProgramIndex 31
// 0040000010000:              GpuProgramIndex 33
// 0080000020000:              GpuProgramIndex 35
// 0100000040000:              GpuProgramIndex 37
// 0200000080000:              GpuProgramIndex 39
// 0400000100000:              GpuProgramIndex 41
// 0800000200000:              GpuProgramIndex 43
// 1000000400000:              GpuProgramIndex 45
// 2000000800000:              GpuProgramIndex 47
// 4000001000000:              GpuProgramIndex 49
// 0000010000004:              GpuProgramIndex 5
// 8000002000000:              GpuProgramIndex 51
// 0000020000008:              GpuProgramIndex 7
// 0000040000010:              GpuProgramIndex 9
//                           }
//                         }
//                       }
//                     }
//                   }
//
//                Unity 5.3 headers extracted from Internal-DeferredShading.shader.decompressed:
//                  API d3d11
//                  Shader model vs_4_0
//                  undeciphered1: 201510240 14 0 0
// 0200000080000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
// 0000100000040:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" }
// 0010000004000:   Keywords { "DIRECTIONAL" "UNITY_HDR_ON" }
// 0000008000002:   Keywords { "DIRECTIONAL" }
// 0400000100000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
// 0000200000080:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" }
// 0080000020000:   Keywords { "DIRECTIONAL_COOKIE" "UNITY_HDR_ON" }
// 0000040000010:   Keywords { "DIRECTIONAL_COOKIE" }
// 4000001000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 0002000000800:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 0800000200000:   Keywords { "POINT" "SHADOWS_CUBE" "UNITY_HDR_ON" }
// 0000400000100:   Keywords { "POINT" "SHADOWS_CUBE" }
// 0008000002000:   Keywords { "POINT" "UNITY_HDR_ON" }
// 0000004000001:   Keywords { "POINT" }
// 8000002000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 0004000001000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 1000000400000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "UNITY_HDR_ON" }
// 0000800000200:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" }
// 0040000010000:   Keywords { "POINT_COOKIE" "UNITY_HDR_ON" }
// 0000020000008:   Keywords { "POINT_COOKIE" }
// 2000000800000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 0001000000400:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" }
// 0100000040000:   Keywords { "SPOT" "SHADOWS_DEPTH" "UNITY_HDR_ON" }
// 0000080000020:   Keywords { "SPOT" "SHADOWS_DEPTH" }
// 0020000008000:   Keywords { "SPOT" "UNITY_HDR_ON" }
// 0000010000004:   Keywords { "SPOT" }
//                  undeciphered2: 1 0 3 0 0 0
//                  undeciphered3: 3 2 0 0 1 2
// dffefff7ffbff:   ConstBuffer "$Globals" 288
// 2001000800400:   ConstBuffer "$Globals" 352
//                  Float 112 [_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:57 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[8];
}


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