// hlsltool.py -I ../112 --auto-fix-vertex-halo --fix-unity-reflection --only-autofixed --no-validate --ignore-other-errors 632a37ce8f0480cc-vs_replace.txt
// 3DMigoto: 632a37ce8f0480cc | Matched 2 variants of 1 shaders: PlayWay Water/Depth/Water Depth
//
//       Unity headers extracted from Depth.shader
//       Shader "PlayWay Water/Depth/Water Depth" {
//         Properties {
//          _SubtractiveMask ("", 2D) = "black" { }
//          _AdditiveMask ("", 2D) = "black" { }
//          _DisplacedHeightMaps ("", 2D) = "black" { }
//          _WaterTileSize ("Heightmap Tile Size", Vector) = (180,18,600,1800)
//          _SurfaceOffset ("", Vector) = (0,0,0,0)
//          _WaterId ("", Vector) = (128,256,0,0)
//         }
//         SubShader 2/5 {
//           Tags { "CustomType"="Water" }
//           Pass 1/1 {
//             Tags { "CustomType"="Water" }
//             ZTest Less
//             Cull Off
//             GpuProgramID 104108
//             Program "vp" {
//               SubProgram "d3d11 " {
// 1:              GpuProgramIndex 155
// 2:              GpuProgramIndex 159
//               }
//             }
//           }
//         }
//       }
//
//    Unity 5.3 headers extracted from Depth.shader.decompressed:
//      API d3d11
//      Shader model vs_4_0
//      undeciphered1: 201509030 49 0 1
// 2:   Keywords { "_WAVES_FFT" "_WAVES_GERSTNER" "_WAVES_ALIGN" "_BOUNDED_WATER" }
// 1:   Keywords { "_WAVES_FFT" "_WAVES_GERSTNER" "_WAVES_ALIGN" }
//      undeciphered2: 1 4 4 4 0
//      undeciphered3: 1 1 0 0
//      ConstBuffer "$Globals" 1296
//      Float 128 [_DisplacementsScale]
//      Float 160 [_DetailFadeFactor]
//      Vector 176 [_WaterTileSize]
//      Vector 224 [_SurfaceOffset]
//      Vector 256 [_GrAmp0]
//      Vector 272 [_GrAmp1]
//      Vector 288 [_GrAmp2]
//      Vector 304 [_GrAmp3]
//      Vector 320 [_GrAmp4]
//      Vector 336 [_GrFrq0]
//      Vector 352 [_GrFrq1]
//      Vector 368 [_GrFrq2]
//      Vector 384 [_GrFrq3]
//      Vector 400 [_GrFrq4]
//      Vector 416 [_GrOff0]
//      Vector 432 [_GrOff1]
//      Vector 448 [_GrOff2]
//      Vector 464 [_GrOff3]
//      Vector 480 [_GrOff4]
//      Vector 496 [_GrAB0]
//      Vector 512 [_GrAB1]
//      Vector 528 [_GrAB2]
//      Vector 544 [_GrAB3]
//      Vector 560 [_GrAB4]
//      Vector 576 [_GrCD0]
//      Vector 592 [_GrCD1]
//      Vector 608 [_GrCD2]
//      Vector 624 [_GrCD3]
//      Vector 640 [_GrCD4]
//      ConstBuffer "UnityPerCamera" 144
//      Vector 64 [_WorldSpaceCameraPos] 3
//      Vector 80 [_ProjectionParams]
//      ConstBuffer "UnityPerDraw" 352
//      Matrix 192 [_Object2World]
//      ConstBuffer "UnityPerFrame" 256
//      Matrix 144 [unity_MatrixVP]
//      SetTexture 0 [_GlobalDisplacementMap] 2D 0
//      SetTexture 1 [_GlobalDisplacementMap1] 2D 1
//      SetTexture 2 [_GlobalDisplacementMap2] 2D 2
//      SetTexture 3 [_DisplacedHeightMaps] 2D 3
//      BindCB "$Globals" 0
//      BindCB "UnityPerCamera" 1
//      BindCB "UnityPerDraw" 2
//      BindCB "UnityPerFrame" 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.38 on Thu Feb 09 12:28:29 2017
Texture2D<float4> t3 : register(t3);

Texture2D<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 cb3 : register(b3)
{
  float4 cb3[13];
}

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

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

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




// 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 : POSITION0,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : TEXCOORD0)
{
  float4 r0,r1,r2,r3,r4,r5,r6,r7;
  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 ../112 --auto-fix-vertex-halo --fix-unity-reflection --only-autofixed --no-validate --ignore-other-errors 632a37ce8f0480cc-vs_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.xyzw = cb2[13].xyzw * v0.yyyy;
  r0.xyzw = cb2[12].xyzw * v0.xxxx + r0.xyzw;
  r0.xyzw = cb2[14].xyzw * v0.zzzz + r0.xyzw;
  r0.xyzw = cb2[15].xyzw * v0.wwww + r0.xyzw;
  r1.xyzw = cb0[14].xzxz + r0.xzxz;
  r2.xyzw = float4(0,0,0,0);
  while (true) {
    r3.x = cmp((int)r2.w >= 5);
    if (r3.x != 0) break;
    r3.x = dot(cb0[r2.w+31].xy, -r1.zw);
    r3.y = dot(cb0[r2.w+31].zw, -r1.zw);
    r3.z = dot(cb0[r2.w+36].xy, -r1.zw);
    r3.w = dot(cb0[r2.w+36].zw, -r1.zw);
    r3.xyzw = cb0[r2.w+21].xyzw * r3.xyzw + cb0[r2.w+26].xyzw;
    sincos(r3.xyzw, r3.xyzw, r4.xyzw);
    r3.x = dot(r3.xyzw, cb0[r2.w+16].xyzw);
    r2.y = r3.x + r2.y;
    r3.xyzw = cb0[r2.w+31].xyzw * cb0[r2.w+16].xxyy;
    r5.xyzw = cb0[r2.w+36].xzyw * cb0[r2.w+16].zwzw;
    r6.xy = r3.xz;
    r6.zw = r5.xy;
    r3.x = dot(r4.xyzw, r6.xyzw);
    r2.x = r3.x + r2.x;
    r5.xy = r3.yw;
    r3.x = dot(r4.xyzw, r5.xyzw);
    r2.z = r3.x + r2.z;
    r2.w = (int)r2.w + 1;
  }
  r2.xz = -cb0[8].xx * r2.xz;
  r3.xyzw = r1.zwzw / cb0[11].xxyy;
  r4.xy = r1.zw / cb0[11].zz;
  r5.xyz = _WorldSpaceCameraPos.xyz + -r0.xyz;
  r2.w = dot(r5.xyz, r5.xyz);
  r2.w = sqrt(r2.w);
  r5.xyz = -cb0[11].xyz + r2.www;
  r6.xyz = cb0[11].xyz * cb0[10].xxx;
  r5.xyz = saturate(r5.xyz / r6.xyz);
  r5.xyz = float3(1,1,1) + -r5.xyz;
  r6.xyz = float3(1,1,1) / r5.xyz;
  r6.xyz = float3(-1,-1,-1) + r6.xyz;
  r7.xyzw = t0.SampleLevel(s0_s, r3.xy, r6.x).xyzw;
  r3.xyzw = t1.SampleLevel(s1_s, r3.zw, r6.y).xyzw;
  r3.xy = r3.xz * r5.yy;
  r3.xy = r7.xz * r5.xx + r3.xy;
  r4.xyzw = t2.SampleLevel(s2_s, r4.xy, r6.z).xyzw;
  r3.xz = r4.xz * r5.zz + r3.xy;
  r1.xyzw = r3.xzxz + r1.xyzw;
  r4.xyzw = r1.zwzw / cb0[11].xxyy;
  r1.xyzw = r1.xyzw / cb0[11].zzww;
  r7.xyzw = t3.SampleLevel(s3_s, r4.xy, r6.x).xyzw;
  r4.xyzw = t3.SampleLevel(s3_s, r4.zw, r6.y).xyzw;
  r6.xyzw = t3.SampleLevel(s3_s, r1.xy, r6.z).xyzw;
  r1.xyzw = t3.SampleLevel(s3_s, r1.zw, 0).xyzw;
  r1.x = r7.x;
  r1.y = r4.y;
  r1.z = r6.z;
  r5.w = 1;
  r3.y = dot(r1.xyzw, r5.xyzw);
  r1.xyz = r3.xyz + r2.xyz;
  r0.xyz = r1.xyz * cb0[14].www + r0.xyz;
  r1.xyzw = cb3[10].xyzw * r0.yyyy;
  r1.xyzw = cb3[9].xyzw * r0.xxxx + r1.xyzw;
  r1.xyzw = cb3[11].xyzw * r0.zzzz + r1.xyzw;
  r0.xyzw = cb3[12].xyzw * r0.wwww + r1.xyzw;

o0.xyzw = r0.xyzw; // Relocated to here with hlsltool.py

// Automatic vertex shader halo fix inserted with DarkStarSword's hlsltool.py:
// hlsltool.py -I ../112 --auto-fix-vertex-halo --fix-unity-reflection --only-autofixed --no-validate --ignore-other-errors 632a37ce8f0480cc-vs_replace.txt
if (r0.w != 1.0) { r0.x += separation * (r0.w - convergence); }

  r1.xz = float2(0.5,0.5) * r0.xw;
  r1.y = cb1[5].x * r0.y;
  r1.w = 0.5 * r1.y;
  o1.xy = r1.xw + r1.zz;

  // o0.xyzw = r0.xyzw; // Relocated from here with hlsltool.py

  o1.zw = r0.zw;
  return;
}
