// hlsltool.py -I ../.. --fix-unity-reflection --only-autofixed --fxc /home/rshan/fxc.exe 02178a269539d21f-ps_replace.txt
// 3DMigoto: 02178a269539d21f |    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 91
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from Alloy Deferred Shading.shader.decompressed:
//   API d3d11
//   Shader model ps_4_0
//   undeciphered1: 97 5 0
//   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "UNITY_HDR_ON" }
//   undeciphered2: 1 5 4 5 0
//   undeciphered3: 0 0
//   ConstBuffer "$Globals" 272
//   Vector 144 [_LightDir]
//   Vector 176 [_LightColor]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 64 [_WorldSpaceCameraPos] 3
//   Vector 80 [_ProjectionParams]
//   Vector 112 [_ZBufferParams]
//   ConstBuffer "UnityShadows" 416
//   Vector 384 [_LightShadowData]
//   Vector 400 [unity_ShadowFadeCenterAndType]
//   ConstBuffer "UnityPerCamera2" 64
//   Matrix 0 [_CameraToWorld]
//   SetTexture 0 [_CameraDepthTexture] 2D 0
//   SetTexture 1 [_CameraGBufferTexture0] 2D 2
//   SetTexture 2 [_CameraGBufferTexture1] 2D 3
//   SetTexture 3 [_CameraGBufferTexture2] 2D 4
//   SetTexture 4 [_ShadowMapTexture] 2D 1
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityShadows" 2
//   BindCB "UnityPerCamera2" 3
//
// 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:10 2016
Texture2D<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 cb3 : register(b3)
{
  float4 cb3[4];
}

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

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

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




// 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;
  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 02178a269539d21f-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 = 0.100000001 * abs(cb0[11].w);
  r0.y = cb1[5].z / v2.z;
  r0.yzw = v2.xyz * r0.yyy;
  r1.xy = v1.xy / v1.ww;
  r2.xyzw = t0.Sample(s0_s, r1.xy).xyzw;
  r1.z = cb1[7].x * r2.x + cb1[7].y;
  r1.z = 1 / r1.z;
  r2.xyz = r1.zzz * r0.yzw;
  r3.xyz = cb3[1].xyz * r2.yyy;
  r2.xyw = cb3[0].xyz * r2.xxx + r3.xyz;
  r2.xyw = cb3[2].xyz * r2.zzz + r2.xyw;
  r2.xyw = cb3[3].xyz + r2.xyw;
  r3.xyz = _WorldSpaceCameraPos.xyz + -r2.xyw;
  r2.xyw = -cb2[25].xyz + r2.xyw;
  r0.y = dot(r2.xyw, r2.xyw);
  r0.y = sqrt(r0.y);
  r0.y = -r0.w * r1.z + r0.y;
  r0.y = cb2[25].w * r0.y + r2.z;
  r0.y = saturate(r0.y * cb2[24].z + cb2[24].w);
  r0.z = dot(r3.xyz, r3.xyz);
  r0.z = rsqrt(r0.z);
  r2.xyz = r3.xyz * r0.zzz;
  r3.xyzw = t3.Sample(s4_s, r1.xy).xyzw;
  r3.xyz = r3.xyz * float3(2,2,2) + float3(-1,-1,-1);
  r0.z = 1 + -r3.w;
  r0.z = 1 + -r0.z;
  r0.w = dot(r3.xyz, r3.xyz);
  r0.w = rsqrt(r0.w);
  r3.xyz = r3.xyz * r0.www;
  r0.w = dot(-r2.xyz, r3.xyz);
  r0.w = r0.w + r0.w;
  r4.xyz = r3.xyz * -r0.www + -r2.xyz;
  r0.w = dot(-cb0[9].xyz, r4.xyz);
  r4.xyz = r0.www * r4.xyz + cb0[9].xyz;
  r0.w = dot(r4.xyz, r4.xyz);
  r0.w = rsqrt(r0.w);
  r0.x = r0.x * r0.w;
  r0.x = min(1, r0.x);
  r4.xyz = r4.xyz * r0.xxx + -cb0[9].xyz;
  r0.x = dot(r4.xyz, r4.xyz);
  r0.x = rsqrt(r0.x);
  r5.xyz = r4.xyz * r0.xxx + r2.xyz;
  r4.xyz = r4.xyz * r0.xxx;
  r0.x = abs(cb0[11].w) * r0.x;
  r0.w = saturate(dot(r3.xyz, r2.xyz));
  r1.z = dot(r5.xyz, r5.xyz);
  r1.z = rsqrt(r1.z);
  r2.xyz = r5.xyz * r1.zzz;
  r1.z = saturate(dot(r3.xyz, r2.xyz));
  r1.w = saturate(dot(r4.xyz, r2.xyz));
  r2.x = saturate(dot(r3.xyz, -cb0[9].xyz));
  r1.z = r1.z * r1.z;
  r3.xyzw = t2.Sample(s3_s, r1.xy).xyzw;
  r4.xyzw = float4(1,1,1,1) + -r3.wxyz;
  r2.y = r4.x * 0.949999988 + 0.0500000007;
  r2.y = r2.y * r2.y;
  r2.z = r2.y * r2.y + -1;
  r1.z = r1.z * r2.z + 1;
  r1.z = r1.z * r1.z;
  r2.z = r2.y * r2.y;
  r1.z = r2.z / r1.z;
  r2.z = r1.w * r1.w;
  r2.w = 0.5 * r2.y;
  r3.w = r2.w * r2.w;
  r2.w = -r2.w * r2.w + 1;
  r2.w = r2.z * r2.w + r3.w;
  r2.z = dot(r2.zz, r4.xx);
  r2.z = -0.5 + r2.z;
  r2.w = 4 * r2.w;
  r1.z = r1.z / r2.w;
  r2.w = r1.w * -5.55472994 + -6.98316002;
  r1.w = r2.w * r1.w;
  r1.w = exp2(r1.w);
  r3.xyz = r1.www * r4.yzw + r3.xyz;
  r3.xyz = r3.xyz * r1.zzz;
  r0.x = r0.x * 0.0500000007 + r2.y;
  r0.x = min(1, r0.x);
  r0.x = r2.y / r0.x;
  r0.x = r0.x * r0.x;
  r1.z = cmp(0 < cb0[11].w);
  r1.z = r1.z ? 1.000000 : 0;
  r0.x = r1.z * r0.x;
  r4.xyzw = t1.Sample(s2_s, r1.xy).xyzw;
  r1.xyzw = t4.Sample(s1_s, r1.xy).xyzw;
  r0.y = saturate(r1.x + r0.y);
  r0.y = r0.y * r2.x;
  r1.xyz = cb0[11].xyz * r0.yyy;
  r0.x = r4.w * r0.x;
  r3.xyz = r0.xxx * r3.xyz;
  r0.x = r2.x * -5.55472994 + -6.98316002;
  r0.x = r0.x * r2.x;
  r0.x = exp2(r0.x);
  r0.x = r0.x * r2.z + 1;
  r0.y = r0.w * -5.55472994 + -6.98316002;
  r0.y = r0.y * r0.w;
  r0.y = exp2(r0.y);
  r0.y = r0.y * r2.z + 1;
  r0.x = r0.x * r0.y;
  r0.xyw = r4.xyz * r0.xxx;
  r0.xyz = r0.xyw * r0.zzz + r3.xyz;
  r0.xyz = r1.xyz * r0.xyz;
  o0.xyz = min(float3(100,100,100), r0.xyz);
  o0.w = 0;
  return;
}
