// hlsltool.py -I ../.. --fix-unity-reflection --only-autofixed --fxc /home/rshan/fxc.exe f6ec7e7291b756e9-ps_replace.txt
// 3DMigoto: f6ec7e7291b756e9 |    Unity headers extracted from Alloy Deferred Reflections.shader
//    Shader "Hidden/Alloy/Deferred Reflections" {
//      Properties {
//       _SrcBlend ("", Float) = 1
//       _DstBlend ("", Float) = 1
//      }
//      Fallback Off
//      SubShader 1/1 {
//        Pass 1/2 {
//          ZWrite Off
//          Blend [_SrcBlend] [_DstBlend]
//          GpuProgramID 10211
//          Program "fp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 3
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Alloy Deferred Reflections.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 64 4 1
//   undeciphered2: 1 5 3 5 0
//   undeciphered3: 0 0
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   Vector 80 [_ProjectionParams]
//   Vector 112 [_ZBufferParams]
//   ConstBuffer "UnityReflectionProbes" 128
//   Vector 0 [unity_SpecCube0_BoxMax]
//   Vector 16 [unity_SpecCube0_BoxMin]
//   Vector 32 [unity_SpecCube0_ProbePosition]
//   Vector 48 [unity_SpecCube0_HDR]
//   Vector 96 [unity_SpecCube1_ProbePosition]
//   ConstBuffer "UnityPerCamera2" 64
//   Matrix 0 [_CameraToWorld]
//   SetTexture 0 [_CameraDepthTexture] 2D 1
//   SetTexture 1 [_CameraGBufferTexture0] 2D 2
//   SetTexture 2 [_CameraGBufferTexture1] 2D 3
//   SetTexture 3 [_CameraGBufferTexture2] 2D 4
//   SetTexture 4 [unity_SpecCube0] CUBE 0
//   BindCB "UnityPerCamera" 0
//   BindCB "UnityReflectionProbes" 1
//   BindCB "UnityPerCamera2" 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:14:08 2016
TextureCube<float4> t4 : register(t4);

Texture2D<float4> t3 : register(t3);

Texture2D<float4> t2 : register(t2);

Texture2D<float4> t1 : register(t1);

Texture2D<float4> t0 : register(t0);

SamplerState s4_s : register(s4);

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[4];
}

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

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




// 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,
  float3 v2 : TEXCOORD1,
  out float4 o0 : SV_Target0,
float fov : TEXCOORD2)
{
  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 f6ec7e7291b756e9-ps_replace.txt
float4 _WorldSpaceCameraPos = cb0[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 = cb0[5].z / v2.z;
  r0.xyz = v2.xyz * r0.xxx;
  r1.xy = v1.xy / v1.ww;
  r2.xyzw = t0.Sample(s1_s, r1.xy).xyzw;
  r0.w = cb0[7].x * r2.x + cb0[7].y;
  r0.w = 1 / r0.w;
  r0.xyz = r0.xyz * r0.www;
r0.x-=separation*(r0.z-convergence)*fov;
  r2.xyz = cb2[1].xyz * r0.yyy;
  r0.xyw = cb2[0].xyz * r0.xxx + r2.xyz;
  r0.xyz = cb2[2].xyz * r0.zzz + r0.xyw;
  r0.xyz = cb2[3].xyz + r0.xyz;
  r2.xyzw = t1.Sample(s2_s, r1.xy).xyzw;
  r3.xyzw = t2.Sample(s3_s, r1.xy).xyzw;
  r1.xyzw = t3.Sample(s4_s, r1.xy).xyzw;
  r0.w = 1 + -r3.w;
  r1.xyz = r1.xyz * float3(2,2,2) + float3(-1,-1,-1);
  r1.w = dot(r1.xyz, r1.xyz);
  r1.w = rsqrt(r1.w);
  r1.xyz = r1.xyz * r1.www;
  r2.xyz = _WorldSpaceCameraPos.xyz + -r0.xyz;
  r1.w = dot(r2.xyz, r2.xyz);
  r1.w = rsqrt(r1.w);
  r2.xyz = r2.xyz * r1.www;
  r1.w = dot(-r2.xyz, r1.xyz);
  r1.w = r1.w + r1.w;
  r4.xyz = r1.xyz * -r1.www + -r2.xyz;
  r1.x = saturate(dot(r1.xyz, r2.xyz));
  r1.y = cmp(0 < cb1[2].w);
  if (r1.y != 0) {
    r1.yzw = -cb1[6].www + cb1[1].xyz;
    r2.xyz = cb1[6].www + cb1[0].xyz;
    r3.w = dot(r4.xyz, r4.xyz);
    r3.w = rsqrt(r3.w);
    r5.xyz = r4.xyz * r3.www;
    r2.xyz = r2.xyz + -r0.xyz;
    r2.xyz = r2.xyz / r5.xyz;
    r1.yzw = r1.yzw + -r0.xyz;
    r1.yzw = r1.yzw / r5.xyz;
    r6.xyz = cmp(float3(0,0,0) < r5.xyz);
    r1.yzw = r6.xyz ? r2.xyz : r1.yzw;
    r1.y = min(r1.y, r1.z);
    r1.y = min(r1.y, r1.w);
    r2.xyz = -cb1[2].xyz + r0.xyz;
    r4.xyz = r5.xyz * r1.yyy + r2.xyz;
  }
  r1.y = -r0.w * 0.699999988 + 1.70000005;
  r1.y = r1.y * r0.w;
  r1.y = 6 * r1.y;
  r4.xyzw = t4.SampleLevel(s0_s, r4.xyz, r1.y).xyzw;
  r1.y = log2(r4.w);
  r1.y = cb1[3].y * r1.y;
  r1.y = exp2(r1.y);
  r1.y = cb1[3].x * r1.y;
  r1.yzw = r1.yyy * r4.xyz;
  r1.yzw = r1.yzw * r2.www;
  r2.xyzw = r0.wwww * float4(-1,-0.0274999999,-0.572000027,0.0219999999) + float4(1,0.0425000004,1.03999996,-0.0399999991);
  r0.w = r2.x * r2.x;
  r1.x = -9.27999973 * r1.x;
  r1.x = exp2(r1.x);
  r0.w = min(r1.x, r0.w);
  r0.w = r0.w * r2.x + r2.y;
  r2.xy = r0.ww * float2(-1.03999996,1.03999996) + r2.zw;
  r2.xyz = r3.xyz * r2.xxx + r2.yyy;
  o0.xyz = r2.xyz * r1.yzw;
  r1.xyz = -cb1[0].xyz + r0.xyz;
  r0.xyz = cb1[1].xyz + -r0.xyz;
  r0.xyz = max(r1.xyz, r0.xyz);
  r0.xyz = max(float3(0,0,0), r0.xyz);
  r0.x = dot(r0.xyz, r0.xyz);
  r0.x = sqrt(r0.x);
  r0.x = r0.x / cb1[6].w;
  o0.w = saturate(1 + -r0.x);
  return;
}
