// hlsltool.py -I ../.. --fix-unity-reflection --only-autofixed --fxc /home/Ryan/fxc.exe 149a1933ddc2b11b-ps_replace.txt
// 3DMigoto: 149a1933ddc2b11b |    Unity headers extracted from Normal-BumpSpec.shader
//    Shader "Legacy Shaders/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.03,1)) = 0.078125
//       _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" { }
//       _BumpMap ("Normalmap", 2D) = "bump" { }
//      }
//      Fallback "Legacy Shaders/Specular"
//      SubShader 2/2 {
//        LOD 400
//        Tags { "RenderType"="Opaque" }
//        Pass 1/5 {
//          Name "FORWARD"
//          Tags { "LIGHTMODE"="ForwardBase" "SHADOWSUPPORT"="true" "RenderType"="Opaque" }
//          GpuProgramID 441920
//          Program "fp" {
//            SubProgram "d3d11_9x " {
//              GpuProgramIndex 121
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Normal-BumpSpec.shader.decompressed:
//   API d3d11_9x
//   Shader model ps_4_0_level_9_1
//   undeciphered1: 31 3 0
//   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "LIGHTMAP_OFF" "DIRLIGHTMAP_OFF" "DYNAMICLIGHTMAP_OFF" }
//   undeciphered2: 1 3 3 3 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 192
//   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]
//   SetTexture 0 [_MainTex] 2D 1
//   SetTexture 1 [_BumpMap] 2D 2
//   SetTexture 2 [_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 Thu Aug 18 03:38:48 2016
Texture2D<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[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,
  float4 v1 : TEXCOORD0,
  float4 v2 : TEXCOORD1,
  float4 v3 : TEXCOORD2,
  float4 v4 : TEXCOORD3,
  float4 v5 : TEXCOORD4,
  float4 v6 : TEXCOORD5,
  out float4 o0 : SV_Target0)
{
  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 149a1933ddc2b11b-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.x = v2.w;
  r0.y = v3.w;
  r0.z = v4.w;
  r0.xyz = _WorldSpaceCameraPos.xyz + -r0.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;
  r1.xyzw = t1.Sample(s2_s, v1.zw).xyzw;
  r1.xy = r1.wy * float2(2,2) + float2(-1,-1);
  r0.w = dot(r1.xy, r1.xy);
  r0.w = min(1, r0.w);
  r0.w = 1 + -r0.w;
  r1.z = sqrt(r0.w);
  r2.x = dot(v2.xyz, r1.xyz);
  r2.y = dot(v3.xyz, r1.xyz);
  r2.z = dot(v4.xyz, r1.xyz);
  r0.x = dot(r2.xyz, r0.xyz);
  r0.y = dot(r2.xyz, cb2[0].xyz);
  r0.xy = max(float2(0,0), r0.xy);
  r0.x = log2(r0.x);
  r0.z = 128 * cb0[9].x;
  r0.x = r0.z * r0.x;
  r0.x = exp2(r0.x);
  r1.xyzw = t0.Sample(s1_s, v1.xy).xyzw;
  r0.x = r1.w * r0.x;
  r1.xyz = cb0[8].xyz * r1.xyz;
  r0.zw = v6.xy / v6.ww;
  r2.xyzw = t2.Sample(s0_s, r0.zw).xyzw;
  r2.xyz = cb0[6].xyz * r2.xxx;
  r3.xyz = cb0[7].xyz * r2.xyz;
  r2.xyz = r2.xyz * r1.xyz;
  r0.xzw = r3.xyz * r0.xxx;
  r0.xyz = r2.xyz * r0.yyy + r0.xzw;
  o0.xyz = r1.xyz * v5.xyz + r0.xyz;
  o0.w = 1;
  return;
}
