// hlsltool.py -I ../.. --fix-unity-reflection --only-autofixed --fxc /home/Ryan/fxc.exe 9e4dd6ce02c7deb8-ps_replace.txt
// 3DMigoto: 9e4dd6ce02c7deb8 |    Unity headers extracted from Normal-Glossy.shader
//    Shader "Legacy Shaders/Specular" {
//      Properties {
//       _Color ("Main Color", Color) = (1,1,1,1)
//       _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
//       _Shininess ("Shininess", Range(0.01,1)) = 0.078125
//       _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" { }
//      }
//      Fallback "Legacy Shaders/VertexLit"
//      SubShader 1/1 {
//        LOD 300
//        Tags { "RenderType"="Opaque" }
//        Pass 1/5 {
//          Name "FORWARD"
//          Tags { "LIGHTMODE"="ForwardBase" "SHADOWSUPPORT"="true" "RenderType"="Opaque" }
//          GpuProgramID 8614
//          Program "fp" {
//            SubProgram "d3d11_9x " {
//              GpuProgramIndex 32
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Normal-Glossy.shader.decompressed:
//   API d3d11_9x
//   Shader model ps_4_0_level_9_1
//   undeciphered1: 24 1 0
//   Keywords { "DIRECTIONAL" "SHADOWS_OFF" "LIGHTMAP_OFF" "DIRLIGHTMAP_OFF" "DYNAMICLIGHTMAP_OFF" "FOG_LINEAR" }
//   undeciphered2: 1 1 4 1 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 176
//   Vector 96 [_LightColor0]
//   Vector 112 [_SpecColor]
//   Vector 128 [_Color]
//   Float 144 [_Shininess]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   ConstBuffer "UnityLighting" 720
//   Vector 0 [_WorldSpaceLightPos0]
//   ConstBuffer "UnityFog" 32
//   Vector 0 [unity_FogColor]
//   SetTexture 0 [_MainTex] 2D 0
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityLighting" 2
//   BindCB "UnityFog" 3
//
// 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.30 on Thu Aug 18 03:38:51 2016
Texture2D<float4> t0 : register(t0);

SamplerState s0_s : register(s0);

cbuffer cb3 : register(b3)
{
  float4 cb3[1];
}

cbuffer cb2 : register(b2)
{
  float4 cb2[1];
}

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

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




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



cbuffer UnityPerDraw : register(b11) {
	float4x4 glstate_matrix_mvp;
	float4x4 glstate_matrix_modelview0;
	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

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


#include <matrix.hlsl>

void main( 
  float4 v0 : SV_POSITION0,
  float2 v1 : TEXCOORD0,
  float w1 : TEXCOORD5,
  float4 v2 : TEXCOORD1,
  float4 v3 : TEXCOORD2,
  float3 v4 : TEXCOORD3,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2;
  uint4 bitmask, uiDest;
  float4 fDest;

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

// Unity reflection/specular fix inserted with DarkStarSword's hlsltool.py:
// hlsltool.py -I ../.. --fix-unity-reflection --only-autofixed --fxc /home/Ryan/fxc.exe 9e4dd6ce02c7deb8-ps_replace.txt
float4 _WorldSpaceCameraPos = cb1[4];
float4 clip_space_adj = float4(-separation * convergence, 0, 0, 0);
float4 local_space_adj = mul(inverse(glstate_matrix_mvp), clip_space_adj);
float4 world_space_adj = mul(_Object2World, local_space_adj);
_WorldSpaceCameraPos.xyz -= world_space_adj.xyz;

  r0.xyz = _WorldSpaceCameraPos.xyz + -v3.xyz;
  r0.w = dot(r0.xyz, r0.xyz);
  r0.w = rsqrt(r0.w);
  r0.xyz = r0.xyz * r0.www + cb2[0].xyz;
  r0.w = dot(r0.xyz, r0.xyz);
  r0.w = rsqrt(r0.w);
  r0.xyz = r0.xyz * r0.www;
  r0.x = dot(v2.xyz, r0.xyz);
  r0.x = max(0, r0.x);
  r0.x = log2(r0.x);
  r0.y = 128 * cb0[9].x;
  r0.x = r0.y * r0.x;
  r0.x = exp2(r0.x);
  r1.xyzw = t0.Sample(s0_s, v1.xy).xyzw;
  r0.x = r1.w * r0.x;
  r0.yzw = cb0[8].xyz * r1.xyz;
  r1.xyz = cb0[7].xyz * cb0[6].xyz;
  r1.xyz = r1.xyz * r0.xxx;
  r0.x = dot(v2.xyz, cb2[0].xyz);
  r0.x = max(0, r0.x);
  r2.xyz = cb0[6].xyz * r0.yzw;
  r1.xyz = r2.xyz * r0.xxx + r1.xyz;
  r0.xyz = r0.yzw * v4.xyz + r1.xyz;
  r0.xyz = -cb3[0].xyz + r0.xyz;
  r0.w = saturate(w1.x);
  o0.xyz = r0.www * r0.xyz + cb3[0].xyz;
  o0.w = 1;
  return;
}
