// Water VS4 DJRK
// VS2 changes to this VS when close
cbuffer cb2 : register(b2)
{
  float4 cb2[13];
}

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

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




// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
Texture2D<float4> StereoParams : register(t125);
#include <matrix.hlsl>

void main( 
  float4 v0 : POSITION0,
  float2 v1 : TEXCOORD0,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : COLOR0,
  out float4 o2 : TEXCOORD0,
  out float4 o3 : TEXCOORD1,
  out float4 o4 : TEXCOORD2,
  out float4 o5 : TEXCOORD3,
  out float4 o6 : TEXCOORD5,
out float4 o7: TEXCOORD6)
{
  float4 r0,r1,r2,r3, r10, r11;
  uint4 bitmask, uiDest;
  float4 fDest;
matrix vp = {cb2[8], cb2[9], cb2[10], cb2[11]};
matrix ivp = inverse(vp);

float4 stereo = StereoParams.Load(0);
float4 params = IniParams.Load(0);

  r0.xyz = v0.xyz;
  r0.w = 1;
  r1.z = dot(cb2[10].xyzw, r0.xyzw);
  r1.w = -70000 + r1.z;
  r1.w = max(0, r1.w);
  r1.w = 9.99999975e-005 * r1.w;
  r1.w = min(1, r1.w);
  o0.z = r1.w * 0.5 + r1.z;
  r1.x = dot(cb2[8].xyzw, r0.xyzw);
  r1.y = dot(cb2[9].xyzw, r0.xyzw);
  o0.xy = r1.xy;
  r1.x = dot(r1.xyz, r1.xyz);
  r1.x = sqrt(r1.x);
  r1.x = saturate(r1.x * cb1[0].y + -cb1[0].x);
  r1.x = log2(r1.x);
  r1.x = cb1[5].w * r1.x;
  r1.x = exp2(r1.x);
  r1.x = min(cb1[2].w, r1.x);
  r1.y = dot(cb2[11].xyzw, r0.xyzw);
  o0.w = r1.y;
  o4.z = r1.y;
  r1.yzw = cb1[2].xyz + -cb1[1].xyz;
  o1.xyz = r1.xxx * r1.yzw + cb1[1].xyz;
  o1.w = r1.x;
float4 tmp = mul(vp, r0);
tmp.x+=stereo.x*(tmp.w-stereo.y);
r10=mul(ivp, tmp);
  r1.z = dot(cb2[2].xyzw, r0.xyzw);
  r1.x = dot(cb2[0].xyzw, r0.xyzw);
  r1.y = dot(cb2[1].xyzw, r0.xyzw);
  
r11.z = dot(cb2[2].xyzw, r10.xyzw);
r11.x = dot(cb2[0].xyzw, r10.xyzw);
r11.y = dot(cb2[1].xyzw, r10.xyzw);
  // o6.xyzw = r0.xyzw;
o6.xyzw = r10.xyzw;
  
// World space fix
// float4 tmp = mul(vp, r0);
// tmp.x+=stereo.x*(tmp.w-stereo.y);
// o6=mul(ivp, tmp);

  r0.x = dot(r1.xyz, r1.xyz);
  // o2.xyz = r1.xyz;
o2.xyz = r11.xyz;
o7.xyz = r1.xyz;
  r0.yz = cb0[0].xy + r1.xy;
  o2.w = sqrt(r0.x);
o7.w = sqrt(r0.x);
  r0.yz = cb0[0].xy + r1.xy;
  o2.w = sqrt(r0.x);
  r1.zw = r0.yz / cb1[5].zz;
  r0.xyzw = r0.yzyz / cb1[5].xxyy;
  r1.xy = r0.zw;
  r2.xyz = float3(0.00100000005,0.00100000005,0.00100000005) * cb1[5].xyz;
  r3.zw = v1.xy / r2.zz;
  r2.xyzw = v1.xyxy / r2.xxyy;
  r3.xy = r2.zw;
  r0.z = cmp(0 != cb2[12].x);
  r1.xyzw = r0.zzzz ? r3.xyzw : r1.xyzw;
  r0.xy = r0.zz ? r2.xy : r0.xy;
  o3.xy = cb1[3].xy + r0.xy;
  o3.zw = cb1[3].zw + r1.xy;
  o4.xy = cb1[4].xy + r1.zw;
  o4.w = 0;
  o5.xy = v1.xy;
  o5.zw = float2(0,0);
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) D3D Shader Disassembler
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float   xyz 
// TEXCOORD                 0   xy          1     NONE   float   xy  
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_POSITION              0   xyzw        0      POS   float   xyzw
// COLOR                    0   xyzw        1     NONE   float   xyzw
// TEXCOORD                 0   xyzw        2     NONE   float   xyzw
// TEXCOORD                 1   xyzw        3     NONE   float   xyzw
// TEXCOORD                 2   xyzw        4     NONE   float   xyzw
// TEXCOORD                 3   xyzw        5     NONE   float   xyzw
// TEXCOORD                 5   xyzw        6     NONE   float   xyzw
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[1], immediateIndexed
dcl_constantbuffer cb1[6], immediateIndexed
dcl_constantbuffer cb2[13], immediateIndexed
dcl_input v0.xyz
dcl_input v1.xy
dcl_output_siv o0.xyzw, position
dcl_output o1.xyzw
dcl_output o2.xyzw
dcl_output o3.xyzw
dcl_output o4.xyzw
dcl_output o5.xyzw
dcl_output o6.xyzw
dcl_temps 4
mov r0.xyz, v0.xyzx
mov r0.w, l(1.000000)
dp4 r1.z, cb2[10].xyzw, r0.xyzw
add r1.w, r1.z, l(-70000.000000)
max r1.w, r1.w, l(0.000000)
mul r1.w, r1.w, l(0.000100)
min r1.w, r1.w, l(1.000000)
mad o0.z, r1.w, l(0.500000), r1.z
dp4 r1.x, cb2[8].xyzw, r0.xyzw
dp4 r1.y, cb2[9].xyzw, r0.xyzw
mov o0.xy, r1.xyxx
dp3 r1.x, r1.xyzx, r1.xyzx
sqrt r1.x, r1.x
mad_sat r1.x, r1.x, cb1[0].y, -cb1[0].x
log r1.x, r1.x
mul r1.x, r1.x, cb1[5].w
exp r1.x, r1.x
min r1.x, r1.x, cb1[2].w
dp4 r1.y, cb2[11].xyzw, r0.xyzw
mov o0.w, r1.y
mov o4.z, r1.y
add r1.yzw, -cb1[1].xxyz, cb1[2].xxyz
mad o1.xyz, r1.xxxx, r1.yzwy, cb1[1].xyzx
mov o1.w, r1.x
dp4 r1.z, cb2[2].xyzw, r0.xyzw
dp4 r1.x, cb2[0].xyzw, r0.xyzw
dp4 r1.y, cb2[1].xyzw, r0.xyzw
mov o6.xyzw, r0.xyzw
dp3 r0.x, r1.xyzx, r1.xyzx
mov o2.xyz, r1.xyzx
add r0.yz, r1.xxyx, cb0[0].xxyx
sqrt o2.w, r0.x
div r1.zw, r0.yyyz, cb1[5].zzzz
div r0.xyzw, r0.yzyz, cb1[5].xxyy
mov r1.xy, r0.zwzz
mul r2.xyz, cb1[5].xyzx, l(0.001000, 0.001000, 0.001000, 0.000000)
div r3.zw, v1.xxxy, r2.zzzz
div r2.xyzw, v1.xyxy, r2.xxyy
mov r3.xy, r2.zwzz
ne r0.z, l(0.000000, 0.000000, 0.000000, 0.000000), cb2[12].x
movc r1.xyzw, r0.zzzz, r3.xyzw, r1.xyzw
movc r0.xy, r0.zzzz, r2.xyxx, r0.xyxx
add o3.xy, r0.xyxx, cb1[3].xyxx
add o3.zw, r1.xxxy, cb1[3].zzzw
add o4.xy, r1.zwzz, cb1[4].xyxx
mov o4.w, l(0)
mov o5.xy, v1.xyxx
mov o5.zw, l(0,0,0,0)
ret 
// Approximately 0 instruction slots used

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 