// hlsltool.py -I ../.. --fix-unity-reflection --only-autofixed --fxc /home/rshan/fxc.exe bfcdb528e0e6bb25-ps_replace.txt
// 3DMigoto: bfcdb528e0e6bb25 |    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 1/5 {
//          Name "FORWARD"
//          Tags { "LIGHTMODE"="ForwardBase" "SHADOWSUPPORT"="true" "RenderType"="Opaque" }
//          GpuProgramID 55912
//          Program "fp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 11
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Reflect-BumpSpec.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 49 4 0
//   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "LIGHTMAP_OFF" "DIRLIGHTMAP_OFF" "DYNAMICLIGHTMAP_OFF" }
//   undeciphered2: 1 4 3 4 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 208
//   Vector 48 [unity_ColorSpaceLuminance]
//   Vector 96 [_LightColor0]
//   Vector 112 [_SpecColor]
//   Vector 128 [_Color]
//   Vector 144 [_ReflectColor]
//   Float 160 [_Shininess]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   ConstBuffer "UnityLighting" 720
//   Vector 0 [_WorldSpaceLightPos0]
//   Vector 608 [unity_SHAr]
//   Vector 624 [unity_SHAg]
//   Vector 640 [unity_SHAb]
//   SetTexture 0 [_MainTex] 2D 1
//   SetTexture 1 [_BumpMap] 2D 2
//   SetTexture 2 [_Cube] CUBE 3
//   SetTexture 3 [_ShadowMapTexture] 2D 0
//   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 Wed Oct 26 06:15:32 2016
Texture2D<float4> t3 : register(t3);

TextureCube<float4> t2 : register(t2);

Texture2D<float4> t1 : register(t1);

Texture2D<float4> t0 : register(t0);

SamplerState s3_s : register(s3);

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,
  float4 v6 : TEXCOORD5,
  float4 v7 : TEXCOORD7,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2,r3,r4,r5,r6;
  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/rshan/fxc.exe bfcdb528e0e6bb25-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.xy = v6.xy / v6.ww;
  r0.xyzw = t3.Sample(s0_s, r0.xy).xyzw;
  r0.xyz = cb0[6].xyz * r0.xxx;
  r1.xyzw = t0.Sample(s1_s, v1.xy).xyzw;
  r1.xyz = cb0[8].xyz * r1.xyz;
  r2.xyz = r1.xyz * r0.xyz;
  r0.xyz = cb0[7].xyz * r0.xyz;
  r0.w = 128 * cb0[10].x;
  r3.x = v2.w;
  r3.y = v3.w;
  r3.z = v4.w;
  r3.xyz = _WorldSpaceCameraPos.xyz + -r3.xyz;
  r2.w = dot(r3.xyz, r3.xyz);
  r2.w = rsqrt(r2.w);
  r4.xyz = r3.xyz * r2.www + cb2[0].xyz;
  r3.xyz = r3.xyz * r2.www;
  r2.w = dot(r4.xyz, r4.xyz);
  r2.w = rsqrt(r2.w);
  r4.xyz = r4.xyz * r2.www;
  r5.xyzw = t1.Sample(s2_s, v1.zw).xyzw;
  r5.xy = r5.wy * float2(2,2) + float2(-1,-1);
  r2.w = dot(r5.xy, r5.xy);
  r2.w = min(1, r2.w);
  r2.w = 1 + -r2.w;
  r5.z = sqrt(r2.w);
  r6.x = dot(v2.xyz, r5.xyz);
  r6.y = dot(v3.xyz, r5.xyz);
  r6.z = dot(v4.xyz, r5.xyz);
  r2.w = dot(r6.xyz, r4.xyz);
  r2.w = max(0, r2.w);
  r2.w = log2(r2.w);
  r0.w = r2.w * r0.w;
  r0.w = exp2(r0.w);
  r0.w = r0.w * r1.w;
  r0.xyz = r0.xyz * r0.www;
  r0.w = dot(r6.xyz, cb2[0].xyz);
  r0.w = max(0, r0.w);
  r0.xyz = r2.xyz * r0.www + r0.xyz;
  r6.w = 1;
  r2.x = dot(cb2[38].xyzw, r6.xyzw);
  r2.y = dot(cb2[39].xyzw, r6.xyzw);
  r2.z = dot(cb2[40].xyzw, r6.xyzw);
  r2.xyz = v5.xyz + r2.xyz;
  r2.xyz = max(float3(0,0,0), r2.xyz);
  r4.xyz = log2(r2.xyz);
  r4.xyz = float3(0.416666657,0.416666657,0.416666657) * r4.xyz;
  r4.xyz = exp2(r4.xyz);
  r4.xyz = r4.xyz * float3(1.05499995,1.05499995,1.05499995) + float3(-0.0549999997,-0.0549999997,-0.0549999997);
  r4.xyz = max(float3(0,0,0), r4.xyz);
  r0.w = cmp(cb0[3].w == 0.000000);
  r2.xyz = r0.www ? r4.xyz : r2.xyz;
  r0.xyz = r1.xyz * r2.xyz + r0.xyz;
  r0.w = dot(-r3.xyz, r6.xyz);
  r0.w = r0.w + r0.w;
  r1.xyz = r6.xyz * -r0.www + -r3.xyz;
  r2.xyzw = t2.Sample(s3_s, r1.xyz).xyzw;
  r1.xyz = r2.xyz * r1.www;
  o0.xyz = r1.xyz * cb0[9].xyz + r0.xyz;
  o0.w = 1;
  return;
}
