// hlsltool.py -I ../.. --fix-unity-reflection --only-autofixed --fxc /home/rshan/fxc.exe 82c7913122647cf9-ps_replace.txt
// 3DMigoto: 82c7913122647cf9 |    Unity headers extracted from Alloy Deferred Shading.shader
//    Shader "Hidden/Alloy/Deferred Shading" {
//      Properties {
//       _LightTexture0 ("", any) = "" { }
//       _LightTextureB0 ("", 2D) = "" { }
//       _ShadowMapTexture ("", any) = "" { }
//       _SrcBlend ("", Float) = 1
//       _DstBlend ("", Float) = 1
//      }
//      Fallback Off
//      SubShader 1/1 {
//        Pass 1/2 {
//          Tags { "SHADOWSUPPORT"="true" }
//          ZWrite Off
//          Blend [_SrcBlend] [_DstBlend]
//          GpuProgramID 2174
//          Program "fp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 103
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Alloy Deferred Shading.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 172 8 2
//   Keywords { "POINT_COOKIE" "SHADOWS_CUBE" "SHADOWS_SOFT" "UNITY_HDR_ON" }
//   undeciphered2: 1 6 5 6 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 272
//   Vector 160 [_LightPos]
//   Vector 176 [_LightColor]
//   Matrix 208 [_LightMatrix0]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   Vector 80 [_ProjectionParams]
//   Vector 112 [_ZBufferParams]
//   ConstBuffer "UnityLighting" 720
//   Vector 16 [_LightPositionRange]
//   ConstBuffer "UnityShadows" 416
//   Vector 384 [_LightShadowData]
//   ConstBuffer "UnityPerCamera2" 64
//   Matrix 0 [_CameraToWorld]
//   SetTexture 0 [_CameraDepthTexture] 2D 0
//   SetTexture 1 [_CameraGBufferTexture0] 2D 3
//   SetTexture 2 [_CameraGBufferTexture1] 2D 4
//   SetTexture 3 [_CameraGBufferTexture2] 2D 5
//   SetTexture 4 [_ShadowMapTexture] CUBE 2
//   SetTexture 5 [_LightTexture0] CUBE 1
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityLighting" 2
//   BindCB "UnityShadows" 3
//   BindCB "UnityPerCamera2" 4
//
// 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:11 2016
TextureCube<float4> t5 : register(t5);

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 s5_s : register(s5);

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 cb4 : register(b4)
{
  float4 cb4[4];
}

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

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

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

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




// 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)
{
  float4 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11;
  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 82c7913122647cf9-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 = cb1[5].z / v2.z;
  r0.xyz = v2.xyz * r0.xxx;
  r1.xy = v1.xy / v1.ww;
  r2.xyzw = t0.Sample(s0_s, r1.xy).xyzw;
  r0.w = cb1[7].x * r2.x + cb1[7].y;
  r0.w = 1 / r0.w;
  r0.xyz = r0.xyz * r0.www;
  r2.xyz = cb4[1].xyz * r0.yyy;
  r0.xyw = cb4[0].xyz * r0.xxx + r2.xyz;
  r0.xyz = cb4[2].xyz * r0.zzz + r0.xyw;
  r0.xyz = cb4[3].xyz + r0.xyz;
  r2.xyzw = t1.Sample(s3_s, r1.xy).xyzw;
  r3.xyzw = t2.Sample(s4_s, r1.xy).xyzw;
  r1.xyzw = t3.Sample(s5_s, r1.xy).xyzw;
  r4.xyzw = float4(1,1,1,1) + -r3.wxyz;
  r1.xyz = r1.xyz * float3(2,2,2) + float3(-1,-1,-1);
  r0.w = dot(r1.xyz, r1.xyz);
  r0.w = rsqrt(r0.w);
  r1.xyz = r1.xyz * r0.www;
  r5.xyz = _WorldSpaceCameraPos.xyz + -r0.xyz;
  r0.w = dot(r5.xyz, r5.xyz);
  r0.w = rsqrt(r0.w);
  r5.xyz = r5.xyz * r0.www;
  r0.w = 1 + -r1.w;
  r1.w = r4.x * 0.949999988 + 0.0500000007;
  r1.w = r1.w * r1.w;
  r3.w = dot(-r5.xyz, r1.xyz);
  r3.w = r3.w + r3.w;
  r6.xyz = r1.xyz * -r3.www + -r5.xyz;
  r3.w = saturate(dot(r1.xyz, r5.xyz));
  r7.xyz = cb0[10].xyz + -r0.xyz;
  r5.w = rsqrt(cb0[10].w);
  r6.w = dot(-r7.xyz, -r7.xyz);
  r6.w = sqrt(r6.w);
  r6.w = cb2[1].w * r6.w;
  r6.w = 0.970000029 * r6.w;
  r8.xyz = float3(0.0078125,0.0078125,0.0078125) + -r7.xyz;
  r8.xyzw = t4.Sample(s2_s, r8.xyz).xyzw;
  r8.yzw = float3(-0.0078125,-0.0078125,0.0078125) + -r7.xyz;
  r9.xyzw = t4.Sample(s2_s, r8.yzw).xyzw;
  r8.yzw = float3(-0.0078125,0.0078125,-0.0078125) + -r7.xyz;
  r10.xyzw = t4.Sample(s2_s, r8.yzw).xyzw;
  r8.yzw = float3(0.0078125,-0.0078125,-0.0078125) + -r7.xyz;
  r11.xyzw = t4.Sample(s2_s, r8.yzw).yzwx;
  r11.x = r8.x;
  r11.y = r9.x;
  r11.z = r10.x;
  r8.xyzw = cmp(r11.xyzw < r6.wwww);
  r8.xyzw = r8.xyzw ? cb3[24].xxxx : float4(1,1,1,1);
  r6.w = dot(r8.xyzw, float4(0.25,0.25,0.25,0.25));
  r8.xyz = cb0[14].xyz * r0.yyy;
  r8.xyz = cb0[13].xyz * r0.xxx + r8.xyz;
  r0.xyz = cb0[15].xyz * r0.zzz + r8.xyz;
  r0.xyz = cb0[16].xyz + r0.xyz;
  r8.xyzw = t5.SampleBias(s1_s, r0.xyz, -8).xyzw;
  r0.xyz = r8.xyz * r8.www;
  r0.xyz = cb0[11].xyz * r0.xyz;
  r7.w = cmp(0 < cb0[11].w);
  r7.w = r7.w ? 1.000000 : 0;
  r8.x = cmp(cb0[11].w >= 1);
  if (r8.x != 0) {
    r8.x = cb0[13].y;
    r8.y = cb0[14].y;
    r8.z = cb0[15].y;
    r8.w = dot(r8.xyz, r8.xyz);
    r8.w = rsqrt(r8.w);
    r8.xyz = r8.xyz * r8.www;
    r8.w = frac(abs(cb0[11].w));
    r8.w = r8.w * r5.w;
    r9.x = floor(abs(cb0[11].w));
    r9.x = 0.00100000005 * r9.x;
    r9.x = r9.x * r5.w;
    r9.yzw = r9.xxx * r8.xyz;
    r10.xyz = r8.xyz * r9.xxx + r7.xyz;
    r9.yzw = float3(-2,-2,-2) * r9.yzw;
    r10.w = dot(r6.xyz, r10.xyz);
    r11.x = dot(r6.xyz, r9.yzw);
    r11.y = dot(r10.xyz, r9.yzw);
    r10.w = r10.w * r11.x + -r11.y;
    r11.y = dot(r9.yzw, r9.yzw);
    r11.x = -r11.x * r11.x + r11.y;
    r10.w = saturate(r10.w / r11.x);
    r11.x = dot(r7.xyz, r8.xyz);
    r11.x = max(r11.x, -r9.x);
    r11.x = min(r11.x, r9.x);
    r8.xyz = -r11.xxx * r8.xyz + r7.xyz;
    r9.yzw = r9.yzw * r10.www + r10.xyz;
    r10.x = dot(r7.xyz, r7.xyz);
    r10.x = rsqrt(r10.x);
    r9.x = 0.25 * r9.x;
    r10.y = dot(r9.yzw, r6.xyz);
    r10.yzw = r10.yyy * r6.xyz + -r9.yzw;
    r11.x = dot(r10.yzw, r10.yzw);
    r11.x = rsqrt(r11.x);
    r11.x = r11.x * r8.w;
    r11.x = min(1, r11.x);
    r9.yzw = r10.yzw * r11.xxx + r9.yzw;
    r10.y = dot(r9.yzw, r9.yzw);
    r10.y = rsqrt(r10.y);
    r8.w = r10.y * r8.w;
    r8.w = r8.w * 0.5 + r1.w;
    r8.w = min(1, r8.w);
    r8.w = r1.w / r8.w;
    r11.xyz = r10.yyy * r9.yzw;
    r8.w = r8.w * r8.w;
    r8.w = r8.w * r7.w;
    r9.yzw = r9.yzw * r10.yyy + r5.xyz;
    r10.y = dot(r9.yzw, r9.yzw);
    r10.y = rsqrt(r10.y);
    r9.yzw = r10.yyy * r9.yzw;
    r11.x = saturate(dot(r11.xyz, r9.yzw));
    r11.y = saturate(dot(r1.xyz, r9.yzw));
    r9.y = dot(r8.xyz, r8.xyz);
    r9.z = rsqrt(r9.y);
    r8.xyz = r9.zzz * r8.xyz;
    r11.z = saturate(dot(r1.xyz, r8.xyz));
    r8.x = 1 + r9.y;
    r8.xyz = r0.xyz / r8.xxx;
    r9.x = r9.x * r9.z + 1;
    r8.xyz = r8.xyz / r9.xxx;
  } else {
    r9.x = abs(cb0[11].w) * r5.w;
    r9.y = dot(r7.xyz, r6.xyz);
    r6.xyz = r9.yyy * r6.xyz + -r7.xyz;
    r9.y = dot(r6.xyz, r6.xyz);
    r9.y = rsqrt(r9.y);
    r9.y = r9.x * r9.y;
    r9.y = min(1, r9.y);
    r6.xyz = r6.xyz * r9.yyy + r7.xyz;
    r9.y = dot(r6.xyz, r6.xyz);
    r9.y = rsqrt(r9.y);
    r9.x = r9.x * r9.y;
    r9.x = r9.x * 0.5 + r1.w;
    r9.x = min(1, r9.x);
    r9.x = r1.w / r9.x;
    r10.yzw = r9.yyy * r6.xyz;
    r9.x = r9.x * r9.x;
    r8.w = r9.x * r7.w;
    r5.xyz = r6.xyz * r9.yyy + r5.xyz;
    r6.x = dot(r5.xyz, r5.xyz);
    r6.x = rsqrt(r6.x);
    r5.xyz = r6.xxx * r5.xyz;
    r11.x = saturate(dot(r10.yzw, r5.xyz));
    r11.y = saturate(dot(r1.xyz, r5.xyz));
    r5.x = dot(r7.xyz, r7.xyz);
    r10.x = rsqrt(r5.x);
    r6.xyz = r10.xxx * r7.xyz;
    r11.z = saturate(dot(r1.xyz, r6.xyz));
    r1.x = 1 + r5.x;
    r8.xyz = r0.xyz / r1.xxx;
  }
  r0.x = r10.x * r5.w;
  r0.x = 1 / r0.x;
  r0.x = r0.x * r0.x;
  r0.x = -r0.x * r0.x + 1;
  r0.x = max(0, r0.x);
  r0.x = r0.x * r0.x;
  r0.xyz = r8.xyz * r0.xxx;
  r1.x = r11.z * r6.w;
  r0.xyz = r1.xxx * r0.xyz;
  r1.xy = r11.zx * float2(-5.55472994,-5.55472994) + float2(-6.98316002,-6.98316002);
  r1.xy = r1.xy * r11.zx;
  r1.xy = exp2(r1.xy);
  r1.z = r3.w * -5.55472994 + -6.98316002;
  r1.z = r1.z * r3.w;
  r1.z = exp2(r1.z);
  r5.xy = r11.xy * r11.xy;
  r3.w = dot(r5.xx, r4.xx);
  r3.w = -0.5 + r3.w;
  r1.x = r1.x * r3.w + 1;
  r1.z = r1.z * r3.w + 1;
  r1.x = r1.x * r1.z;
  r2.xyz = r2.xyz * r1.xxx;
  r0.w = 1 + -r0.w;
  r1.x = r8.w * r2.w;
  r3.xyz = r1.yyy * r4.yzw + r3.xyz;
  r1.z = r1.w * r1.w + -1;
  r1.z = r5.y * r1.z + 1;
  r1.yz = r1.wz * r1.wz;
  r1.y = r1.y / r1.z;
  r1.z = 0.5 * r1.w;
  r1.w = r1.z * r1.z;
  r1.z = -r1.z * r1.z + 1;
  r1.z = r5.x * r1.z + r1.w;
  r1.z = 4 * r1.z;
  r1.y = r1.y / r1.z;
  r1.yzw = r3.xyz * r1.yyy;
  r1.xyz = r1.xxx * r1.yzw;
  r1.xyz = r2.xyz * r0.www + r1.xyz;
  r0.xyz = r1.xyz * r0.xyz;
  o0.xyz = min(float3(100,100,100), r0.xyz);
  o0.w = 0;
  return;
}
