// hlsltool.py -I ../.. --fix-unity-reflection --only-autofixed --fxc /home/Ryan/fxc.exe f38d2f39dc60359b-ps_replace.txt
// 3DMigoto: f38d2f39dc60359b |    Unity headers extracted from Reflect-BumpSpec.shader
//    Shader "Legacy Shaders/Reflective/Bumped 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
//       _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
//       _MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" { }
//       _Cube ("Reflection Cubemap", CUBE) = "" { }
//       _BumpMap ("Normalmap", 2D) = "bump" { }
//      }
//      Fallback "Legacy Shaders/Reflective/Bumped Diffuse"
//      SubShader 1/1 {
//        LOD 400
//        Tags { "RenderType"="Opaque" }
//        Pass 5/5 {
//          Name "DEFERRED"
//          Tags { "LIGHTMODE"="Deferred" "RenderType"="Opaque" }
//          GpuProgramID 318506
//          Program "fp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 89
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Reflect-BumpSpec.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 32 3 0
//   Keywords { "LIGHTMAP_OFF" "DIRLIGHTMAP_OFF" "DYNAMICLIGHTMAP_OFF" "HDR_LIGHT_PREPASS_OFF" }
//   undeciphered2: 1 3 3 3 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 224
//   Vector 48 [unity_ColorSpaceLuminance]
//   Vector 112 [_SpecColor]
//   Vector 128 [_Color]
//   Vector 144 [_ReflectColor]
//   Float 160 [_Shininess]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   ConstBuffer "UnityLighting" 720
//   Vector 608 [unity_SHAr]
//   Vector 624 [unity_SHAg]
//   Vector 640 [unity_SHAb]
//   SetTexture 0 [_MainTex] 2D 0
//   SetTexture 1 [_BumpMap] 2D 1
//   SetTexture 2 [_Cube] CUBE 2
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityLighting" 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.30 on Thu Aug 18 03:40:25 2016
TextureCube<float4> t2 : register(t2);

Texture2D<float4> t1 : register(t1);

Texture2D<float4> t0 : register(t0);

SamplerState s2_s : register(s2);

SamplerState s1_s : register(s1);

SamplerState s0_s : register(s0);

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

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

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




// 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,
  float4 v1 : TEXCOORD0,
  float4 v2 : TEXCOORD1,
  float4 v3 : TEXCOORD2,
  float4 v4 : TEXCOORD3,
  float4 v5 : TEXCOORD4,
  float3 v6 : TEXCOORD5,
  out float4 o0 : SV_Target0,
  out float4 o1 : SV_Target1,
  out float4 o2 : SV_Target2,
  out float4 o3 : SV_Target3)
{
  float4 r0,r1,r2,r3;
  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 f38d2f39dc60359b-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;

  o0.w = 1;
  r0.xyzw = t0.Sample(s0_s, v1.xy).xyzw;
  r0.xyz = cb0[8].xyz * r0.xyz;
  o0.xyz = r0.xyz;
  o1.xyz = cb0[7].xyz;
  o1.w = cb0[10].x;
  r1.xyzw = t1.Sample(s1_s, v1.zw).xyzw;
  r1.xy = r1.wy * float2(2,2) + float2(-1,-1);
  r1.w = dot(r1.xy, r1.xy);
  r1.w = min(1, r1.w);
  r1.w = 1 + -r1.w;
  r1.z = sqrt(r1.w);
  r2.x = dot(v2.xyz, r1.xyz);
  r2.y = dot(v3.xyz, r1.xyz);
  r2.z = dot(v4.xyz, r1.xyz);
  r2.w = 1;
  o2.xyzw = r2.xyzw * float4(0.5,0.5,0.5,1) + float4(0.5,0.5,0.5,0);
  r1.x = dot(cb2[38].xyzw, r2.xyzw);
  r1.y = dot(cb2[39].xyzw, r2.xyzw);
  r1.z = dot(cb2[40].xyzw, r2.xyzw);
  r1.xyz = v6.xyz + r1.xyz;
  r1.xyz = max(float3(0,0,0), r1.xyz);
  r3.xyz = log2(r1.xyz);
  r3.xyz = float3(0.416666657,0.416666657,0.416666657) * r3.xyz;
  r3.xyz = exp2(r3.xyz);
  r3.xyz = r3.xyz * float3(1.05499995,1.05499995,1.05499995) + float3(-0.0549999997,-0.0549999997,-0.0549999997);
  r3.xyz = max(float3(0,0,0), r3.xyz);
  r1.w = cmp(cb0[3].w == 0.000000);
  r1.xyz = r1.www ? r3.xyz : r1.xyz;
  r0.xyz = r1.xyz * r0.xyz;
  r1.x = v2.w;
  r1.y = v3.w;
  r1.z = v4.w;
  r1.xyz = _WorldSpaceCameraPos.xyz + -r1.xyz;
  r1.w = dot(r1.xyz, r1.xyz);
  r1.w = rsqrt(r1.w);
  r1.xyz = r1.xyz * r1.www;
  r1.w = dot(-r1.xyz, r2.xyz);
  r1.w = r1.w + r1.w;
  r1.xyz = r2.xyz * -r1.www + -r1.xyz;
  r1.xyzw = t2.Sample(s2_s, r1.xyz).xyzw;
  r1.xyz = r1.xyz * r0.www;
  r0.xyz = r1.xyz * cb0[9].xyz + r0.xyz;
  o3.xyz = exp2(-r0.xyz);
  o3.w = 1;
  return;
}
