// Ocean - fix clipping at side of screen

cbuffer cVSScene : register(b2)
{

  struct
  {
    float4x4 m_projectionView;
    float4x4 m_projection;
    float4x4 m_view;
    float4x4 m_shadowProjection;
    float4x4 m_shadowProjection2;
    float4 m_eyepos;
    float4 m_projectionParam;
    float4 m_viewportSize;
    float4 m_exposure;
    float4 m_fogParam[3];
    float4 m_fogColor;
    float4 m_cameraCenterOffset;
    float4 m_shadowMapResolutions;
  } g_vsScene : packoffset(c0);

}

cbuffer cVSObject : register(b5)
{

  struct
  {
    float4x4 m_viewWorld;
    float4x4 m_world;
    float4 m_useWeightCount;
    float4 m_localParam[4];
  } g_vsObject : packoffset(c0);

}

cbuffer cVSMaterial : register(b4)
{

  struct
  {
    float4 m_materials[8];
  } g_vsMaterial : packoffset(c0);

}



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


void main( 
  float4 v0 : POSITION0,
  float4 v1 : COLOR0,
  float4 v2 : TEXCOORD0,
  float4 v3 : TEXCOORD1,
  float4 v4 : TEXCOORD2,
  out float4 o0 : SV_Position0,
  out float3 o1 : TEXCOORD0,
  out float p1 : TEXCOORD1,
  out float4 o2 : TEXCOORD2,
  out float4 o3 : TEXCOORD3,
  out float4 o4 : TEXCOORD4,
  out float4 o5 : TEXCOORD6,
  out float4 o6 : TEXCOORD7)
{
  float4 r0,r1,r2,r3,r4,r5;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.x = -g_vsScene.m_view._m30;
  r0.y = -g_vsScene.m_view._m31;
  r0.z = -g_vsScene.m_view._m32;
  r1.x = g_vsScene.m_view._m00;
  r1.y = g_vsScene.m_view._m01;
  r1.z = g_vsScene.m_view._m02;
  r2.x = dot(r1.xyz, r0.xyz);
  r3.x = g_vsScene.m_view._m10;
  r3.y = g_vsScene.m_view._m11;
  r3.z = g_vsScene.m_view._m12;
  r2.y = dot(r3.xyz, r0.xyz);
  r4.x = g_vsScene.m_view._m20;
  r4.y = g_vsScene.m_view._m21;
  r4.z = g_vsScene.m_view._m22;
  r2.w = dot(r4.xyz, r0.xyz);
  r2.z = -g_vsMaterial.m_materials[6].y;
  r0.x = dot(float2(1,1), r2.yz);
  r5.xy = g_vsScene.m_projectionParam.xy * v0.xy;
  
  // Fix clipping at sides of screen (keeps texture aligned). This formula works
  // by stretching the coordinate horizontally as opposed to moving it to a
  // different depth. We need to do this as the ocean is made up of many vertices
  // and we need to move them away from the center of the screen proportionally.
  float4 stereo = StereoParams.Load(0);
  r5.x *= 1 + abs(stereo.x);
  
  r5.z = 1;
  r0.y = dot(r5.xyz, r3.xyz);
  r0.z = cmp(r0.y >= 0);
  if (r0.z != 0) {
    r5.xy = -r3.xy * r0.yy + r5.xy;
  }
  r0.y = max(9.99999975e-005, -r0.y);
  r0.x = r0.x / r0.y;
  r5.z = 1;
  r0.xyz = r5.xyz * r0.xxx;
  r1.x = dot(r1.xyz, r0.xyz);
  r1.y = dot(r3.xyz, r0.xyz);
  r1.z = dot(r4.xyz, r0.xyz);
  o1.xyz = r2.xyw + r1.xyz;
  r1.x = dot(r1.xz, r1.xz);
  o5.x = sqrt(r1.x);
  r1.x = dot(g_vsScene.m_view._m02_m22, g_vsScene.m_view._m02_m22);
  r1.x = rsqrt(r1.x);
  o6.xy = g_vsScene.m_view._m02_m22 * r1.xx;
  r0.w = 1;
  o0.x = dot(r0.xyzw, g_vsScene.m_projection._m00_m10_m20_m30);
  o0.y = dot(r0.xyzw, g_vsScene.m_projection._m01_m11_m21_m31);
  o0.z = dot(r0.xyzw, g_vsScene.m_projection._m02_m12_m22_m32);
  r0.x = dot(r0.xyzw, g_vsScene.m_projection._m03_m13_m23_m33);
  o0.w = r0.x;
  o2.xyzw = v3.xyzw;
  o3.xyzw = v4.xyzw;
  o4.xyzw = v1.xyzw;
  o5.y = g_vsMaterial.m_materials[6].z;
  o5.z = g_vsObject.m_world._m01;
  o5.w = g_vsObject.m_world._m02;
  o6.zw = float2(0,0);
  p1.x = r0.x;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//
// Buffer Definitions: 
//
// cbuffer cVSScene
// {
//
//   struct VSScene
//   {
//       
//       float4x4 m_projectionView;     // Offset:    0
//       float4x4 m_projection;         // Offset:   64
//       float4x4 m_view;               // Offset:  128
//       float4x4 m_shadowProjection;   // Offset:  192
//       float4x4 m_shadowProjection2;  // Offset:  256
//       float4 m_eyepos;               // Offset:  320
//       float4 m_projectionParam;      // Offset:  336
//       float4 m_viewportSize;         // Offset:  352
//       float4 m_exposure;             // Offset:  368
//       float4 m_fogParam[3];          // Offset:  384
//       float4 m_fogColor;             // Offset:  432
//       float4 m_cameraCenterOffset;   // Offset:  448
//       float4 m_shadowMapResolutions; // Offset:  464
//
//   } g_vsScene;                       // Offset:    0 Size:   480
//
// }
//
// cbuffer cVSObject
// {
//
//   struct VSObject
//   {
//       
//       float4x4 m_viewWorld;          // Offset:    0
//       float4x4 m_world;              // Offset:   64
//       float4 m_useWeightCount;       // Offset:  128
//       float4 m_localParam[4];        // Offset:  144
//
//   } g_vsObject;                      // Offset:    0 Size:   208
//
// }
//
// cbuffer cVSMaterial
// {
//
//   struct VSMaterial
//   {
//       
//       float4 m_materials[8];         // Offset:    0
//
//   } g_vsMaterial;                    // Offset:    0 Size:   128
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// cVSScene                          cbuffer      NA          NA    2        1
// cVSMaterial                       cbuffer      NA          NA    4        1
// cVSObject                         cbuffer      NA          NA    5        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float   xy  
// COLOR                    0   xyzw        1     NONE   float   xyzw
// TEXCOORD                 0   xy          2     NONE   float       
// TEXCOORD                 1   xyzw        3     NONE   float   xyzw
// TEXCOORD                 2   xyzw        4     NONE   float   xyzw
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float   xyzw
// TEXCOORD                 0   xyz         1     NONE   float   xyz 
// TEXCOORD                 1      w        1     NONE   float      w
// TEXCOORD                 2   xyzw        2     NONE   float   xyzw
// TEXCOORD                 3   xyzw        3     NONE   float   xyzw
// TEXCOORD                 4   xyzw        4     NONE   float   xyzw
// TEXCOORD                 6   xyzw        5     NONE   float   xyzw
// TEXCOORD                 7   xyzw        6     NONE   float   xyzw
//
vs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb2[22], immediateIndexed
dcl_constantbuffer cb5[7], immediateIndexed
dcl_constantbuffer cb4[7], immediateIndexed
dcl_input v0.xy
dcl_input v1.xyzw
dcl_input v3.xyzw
dcl_input v4.xyzw
dcl_output_siv o0.xyzw, position
dcl_output o1.xyz
dcl_output o1.w
dcl_output o2.xyzw
dcl_output o3.xyzw
dcl_output o4.xyzw
dcl_output o5.xyzw
dcl_output o6.xyzw
dcl_temps 6
mov r0.x, -cb2[8].w
mov r0.y, -cb2[9].w
mov r0.z, -cb2[10].w
mov r1.x, cb2[8].x
mov r1.y, cb2[9].x
mov r1.z, cb2[10].x
dp3 r2.x, r1.xyzx, r0.xyzx
mov r3.x, cb2[8].y
mov r3.y, cb2[9].y
mov r3.z, cb2[10].y
dp3 r2.y, r3.xyzx, r0.xyzx
mov r4.x, cb2[8].z
mov r4.y, cb2[9].z
mov r4.z, cb2[10].z
dp3 r2.w, r4.xyzx, r0.xyzx
mov r2.z, -cb4[6].y
dp2 r0.x, l(1.000000, 1.000000, 0.000000, 0.000000), r2.yzyy
mul r5.xy, v0.xyxx, cb2[21].xyxx
mov r5.z, l(1.000000)
dp3 r0.y, r5.xyzx, r3.xyzx
ge r0.z, r0.y, l(0.000000)
if_nz r0.z
  mad r5.xy, -r3.xyxx, r0.yyyy, r5.xyxx
endif 
max r0.y, -r0.y, l(0.000100)
div r0.x, r0.x, r0.y
mov r5.z, l(1.000000)
mul r0.xyz, r0.xxxx, r5.xyzx
dp3 r1.x, r1.xyzx, r0.xyzx
dp3 r1.y, r3.xyzx, r0.xyzx
dp3 r1.z, r4.xyzx, r0.xyzx
add o1.xyz, r1.xyzx, r2.xywx
dp2 r1.x, r1.xzxx, r1.xzxx
sqrt o5.x, r1.x
dp2 r1.x, cb2[10].xzxx, cb2[10].xzxx
rsq r1.x, r1.x
mul o6.xy, r1.xxxx, cb2[10].xzxx
mov r0.w, l(1.000000)
dp4 o0.x, r0.xyzw, cb2[4].xyzw
dp4 o0.y, r0.xyzw, cb2[5].xyzw
dp4 o0.z, r0.xyzw, cb2[6].xyzw
dp4 r0.x, r0.xyzw, cb2[7].xyzw
mov o0.w, r0.x
mov o2.xyzw, v3.xyzw
mov o3.xyzw, v4.xyzw
mov o4.xyzw, v1.xyzw
mov o5.y, cb4[6].z
mov o5.z, cb5[5].x
mov o5.w, cb5[6].x
mov o6.zw, l(0,0,0,0)
mov o1.w, r0.x
ret 
// Approximately 52 instruction slots used

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