// Main Lights VS - 2017.4
// by DSS
// 3DMigoto: 65640cd671a89091 | Matched 156 variants of 3 shaders: Hidden/Internal-DeferredShadingCustom, Hidden/Internal-PrePassLightingCustom, Hidden/Internal-PrePassLightingOLD
//
// 00000000000000000003fffffffffffffffffff:    Unity headers extracted from Hidden_Internal-DeferredShadingCustom
// 0000000001fffffffffc0000000000000000000:    Unity headers extracted from Hidden_Internal-PrePassLightingCustom
// fffffffffe00000000000000000000000000000:    Unity headers extracted from Hidden_Internal-PrePassLightingOLD
// 00000000000000000003fffffffffffffffffff:    Shader "Hidden/Internal-DeferredShadingCustom" {
// 0000000001fffffffffc0000000000000000000:    Shader "Hidden/Internal-PrePassLightingCustom" {
// fffffffffe00000000000000000000000000000:    Shader "Hidden/Internal-PrePassLightingOLD" {
//                                               SubShader 1/1 {
// 00000000000000000003fffffffffffffffffff:        Pass 1/2 {
// fffffffffffffffffffc0000000000000000000: 1:     Pass 1/3 {
// fffffffffffffffffffc0000000000000000000: 2:     Pass 2/3 {
// fffffffffffffffffffc0000000000000000000: 4:     Pass 3/3 {
//                                                   Tags { "SHADOWSUPPORT"="true" }
//                                                   ZWrite Off
//                                                   Program "vp" {
//                                                     SubProgram "d3d11" {
//                                                     }
//                                                   }
//                                                 }
//                                               }
//                                             }
//
// 00000000000000000003fffffffffffffffffff: Unity 5.3 headers extracted from Hidden_Internal-DeferredShadingCustom:
// 0000000001fffffffffc0000000000000000000: Unity 5.3 headers extracted from Hidden_Internal-PrePassLightingCustom:
// fffffffffe00000000000000000000000000000: Unity 5.3 headers extracted from Hidden_Internal-PrePassLightingOLD:
//                                            API d3d11
//                                            Shader model vs_4_0
//                                            undeciphered1: 201609010 34 0 0
//                                            undeciphered1a: 2
// 000000000000000000000000004000000000000:   Keywords { "DIRECTIONAL" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000100000000020000000000000000000800:   Keywords { "DIRECTIONAL" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000010000000000000000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000400000000080000000000000000002000000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000004000000000000000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000100000000020000000000000000000800000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "SHADOWS_SHADOWMASK" }
// 000000000000000000000001000000000000000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
// 000040000000008000000000000000000200000:   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" }
// 000000000000000000000000000200000000000:   Keywords { "DIRECTIONAL" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000008000000001000000000000000000040:   Keywords { "DIRECTIONAL" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000000010000000000:   Keywords { "DIRECTIONAL" "UNITY_HDR_ON" }
// 000000000400000000080000000000000000002:   Keywords { "DIRECTIONAL" }
// 000000000000000000000000020000000000000:   Keywords { "DIRECTIONAL_COOKIE" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000800000000100000000000000000004000:   Keywords { "DIRECTIONAL_COOKIE" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000020000000000000000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000800000000100000000000000000004000000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000008000000000000000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000200000000040000000000000000001000000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" "SHADOWS_SHADOWMASK" }
// 000000000000000000000002000000000000000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
// 000080000000010000000000000000000400000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SCREEN" }
// 000000000000000000000000001000000000000:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000040000000008000000000000000000200:   Keywords { "DIRECTIONAL_COOKIE" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000000080000000000:   Keywords { "DIRECTIONAL_COOKIE" "UNITY_HDR_ON" }
// 000000002000000000400000000000000000010:   Keywords { "DIRECTIONAL_COOKIE" }
// 000000000000000000000000002000000000000:   Keywords { "POINT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000080000000010000000000000000000400:   Keywords { "POINT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000400000000000000000:   Keywords { "POINT" "SHADOWS_CUBE" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 010000000002000000000000000000080000000:   Keywords { "POINT" "SHADOWS_CUBE" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000100000000000000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 004000000000800000000000000000020000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SHADOWMASK" }
// 000000000000000000000800000000000000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 020000000004000000000000000000100000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000200000000000000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 008000000001000000000000000000040000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" "SHADOWS_SHADOWMASK" }
// 000000000000000000000080000000000000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 002000000000400000000000000000010000000:   Keywords { "POINT" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 000000000000000000000040000000000000000:   Keywords { "POINT" "SHADOWS_CUBE" "UNITY_HDR_ON" }
// 001000000000200000000000000000008000000:   Keywords { "POINT" "SHADOWS_CUBE" }
// 000000000000000000000000000100000000000:   Keywords { "POINT" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000004000000000800000000000000000020:   Keywords { "POINT" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000000008000000000:   Keywords { "POINT" "UNITY_HDR_ON" }
// 000000000200000000040000000000000000001:   Keywords { "POINT" }
// 000000000000000000000000010000000000000:   Keywords { "POINT_COOKIE" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000400000000080000000000000000002000:   Keywords { "POINT_COOKIE" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000010000000000000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 400000000080000000000000000002000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000004000000000000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 100000000020000000000000000000800000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SHADOWMASK" }
// 000000000000000000020000000000000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 800000000100000000000000000004000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000008000000000000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 200000000040000000000000000001000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "SHADOWS_SHADOWMASK" }
// 000000000000000000002000000000000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 080000000010000000000000000000400000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" }
// 000000000000000000001000000000000000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "UNITY_HDR_ON" }
// 040000000008000000000000000000200000000:   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" }
// 000000000000000000000000000800000000000:   Keywords { "POINT_COOKIE" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000020000000004000000000000000000100:   Keywords { "POINT_COOKIE" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000000040000000000:   Keywords { "POINT_COOKIE" "UNITY_HDR_ON" }
// 000000001000000000200000000000000000008:   Keywords { "POINT_COOKIE" }
// 000000000000000000000000008000000000000:   Keywords { "SPOT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000200000000040000000000000000001000:   Keywords { "SPOT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000400000000000000:   Keywords { "SPOT" "SHADOWS_DEPTH" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000010000000002000000000000000000080000:   Keywords { "SPOT" "SHADOWS_DEPTH" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000100000000000000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000004000000000800000000000000000020000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000800000000000000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000020000000004000000000000000000100000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" "LIGHTMAP_SHADOW_MIXING" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000200000000000000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000008000000001000000000000000000040000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000080000000000000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" "UNITY_HDR_ON" }
// 000002000000000400000000000000000010000:   Keywords { "SPOT" "SHADOWS_DEPTH" "SHADOWS_SOFT" }
// 000000000000000000000000040000000000000:   Keywords { "SPOT" "SHADOWS_DEPTH" "UNITY_HDR_ON" }
// 000001000000000200000000000000000008000:   Keywords { "SPOT" "SHADOWS_DEPTH" }
// 000000000000000000000000000400000000000:   Keywords { "SPOT" "SHADOWS_SHADOWMASK" "UNITY_HDR_ON" }
// 000000010000000002000000000000000000080:   Keywords { "SPOT" "SHADOWS_SHADOWMASK" }
// 000000000000000000000000000020000000000:   Keywords { "SPOT" "UNITY_HDR_ON" }
// 000000000800000000100000000000000000004:   Keywords { "SPOT" }
//                                            undeciphered2: 1 0 4 0 0 0
//                                            undeciphered3: 3 2 0 0 1 2
// 0000000001ffffabfffc0000000000000000000:   ConstBuffer "$Globals" 176
// ffffd5fffe00000000000000000000000000000:   ConstBuffer "$Globals" 192
// 000000000000005400000000000000000000000:   ConstBuffer "$Globals" 240
// 00002a000000000000000000000000000000000:   ConstBuffer "$Globals" 256
// 00000000000000000003ffff57fffffffeaffff:   ConstBuffer "$Globals" 544
// 000000000000000000000000a80000000150000:   ConstBuffer "$Globals" 608
//                                            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:56:17 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;
  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;
  r0.xyzw = unity_MatrixVP[3].xyzw * r0.wwww + r1.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_MatrixV[1].xyz * unity_ObjectToWorld[1].yyy;
  r0.xyz = unity_MatrixV[0].xyz * unity_ObjectToWorld[1].xxx + r0.xyz;
  r0.xyz = unity_MatrixV[2].xyz * unity_ObjectToWorld[1].zzz + r0.xyz;
  r0.xyz = unity_MatrixV[3].xyz * unity_ObjectToWorld[1].www + r0.xyz;
  r0.xyz = v0.yyy * r0.xyz;
  r1.xyz = unity_MatrixV[1].xyz * unity_ObjectToWorld[0].yyy;
  r1.xyz = unity_MatrixV[0].xyz * unity_ObjectToWorld[0].xxx + r1.xyz;
  r1.xyz = unity_MatrixV[2].xyz * unity_ObjectToWorld[0].zzz + r1.xyz;
  r1.xyz = unity_MatrixV[3].xyz * unity_ObjectToWorld[0].www + r1.xyz;
  r0.xyz = r1.xyz * v0.xxx + r0.xyz;
  r1.xyz = unity_MatrixV[1].xyz * unity_ObjectToWorld[2].yyy;
  r1.xyz = unity_MatrixV[0].xyz * unity_ObjectToWorld[2].xxx + r1.xyz;
  r1.xyz = unity_MatrixV[2].xyz * unity_ObjectToWorld[2].zzz + r1.xyz;
  r1.xyz = unity_MatrixV[3].xyz * unity_ObjectToWorld[2].www + r1.xyz;
  r0.xyz = r1.xyz * v0.zzz + r0.xyz;
  r1.xyz = unity_MatrixV[1].xyz * unity_ObjectToWorld[3].yyy;
  r1.xyz = unity_MatrixV[0].xyz * unity_ObjectToWorld[3].xxx + r1.xyz;
  r1.xyz = unity_MatrixV[2].xyz * unity_ObjectToWorld[3].zzz + r1.xyz;
  r1.xyz = unity_MatrixV[3].xyz * unity_ObjectToWorld[3].www + r1.xyz;
  r0.xyz = r1.xyz * v0.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;
}
