// sky (horizon)
cbuffer _Globals : register(b0)
{
  float4x4 viewProjInverse : packoffset(c0);
}

// copied from Ground VS shader using 3DMIGOTO to find horizon roll angle
// only gView,gViewProj,gCloudShadowsProj are available
cbuffer cPerView : register(b13)
{
  float4x4 gView : packoffset(c0);
  float4x4 gViewInv : packoffset(c4);
  float4x4 gProj : packoffset(c8);
  float4x4 gProjInv : packoffset(c12);
  float4x4 gViewProj : packoffset(c16);
  float4x4 gViewProjInv : packoffset(c20);
  float4x4 gNormViewProj : packoffset(c24);
  float4x4 gCloudShadowsProj : packoffset(c28);
  float4x4 gClipCockpit : packoffset(c32);
  float4 gSunDirV : packoffset(c36);
  float3 gOrigin : packoffset(c37);
  float gCameraHeightAbs : packoffset(c37.w);
  float3 gCameraPos : packoffset(c38);
  float gFogCameraHeightNorm : packoffset(c38.w);
  float4 gNearFarFovZoom : packoffset(c39);
}


Texture2D<float4> StereoParams : register(t126);
Texture1D<float4> IniParams : register(t115);

void main( 
  uint v0 : SV_VertexID0,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : TEXCOORD0,
  out float3 o2 : TEXCOORD1)
{
  const float4 icb[] = { { -1.000000, -1.000000, 0, 0},
                              { 1.000000, -1.000000, 0, 0},
                              { -1.000000, 1.000000, 0, 0},
                              { 1.000000, 1.000000, 0, 0} };

  float4 r0;
  uint4 bitmask, uiDest;
  float4 fDest;
float4 stereo = StereoParams.Load(0); 
float4 iniParams = IniParams.Load(0);
float tex_filter = iniParams.z;
float x2 = IniParams.Load(int2(2, 0)).x;
float4 test, resu,fixLeft, fixRight;
  
  o0.zw = float2(0,1);
  o1.zw = float2(0,1);
  r0.x = v0.x;
// w = 1 for o0 
  o0.xy = icb[r0.x+0].xy;
  o1.xy = icb[r0.x+0].xy * float2(0.5,0.5) + float2(0.5,0.5);
  r0.xy = icb[r0.x+0].xy;  
//additional correction to remove thin dark blue line and white band
// resu.y > 0 => right part of the horizon is lower than left part => r0.x must be reduced
  test=float4(1,0,0,0);
  resu=mul(gView, test);  
  fixLeft=0;
  fixRight=0;
  if (resu.y > 0) {
	// fixRight=x2*resu.y;
	fixRight=resu.y*x2*3;
	fixLeft=resu.y*x2;
  }
  if (resu.y < 0) {
	// fixLeft=-resu.y*x2;
	fixRight=-resu.y*x2;
	fixLeft=-resu.y*x2*2;
  }
//reduce (but not suppress) the horizon problem
// r0.x = r0.x - stereo.x * (1.0 - stereo.y + resu.y*x2); 
if (stereo.x > 0) {
// right eye for right side upper than left side:  r0.x = r0.x -stereo.x*(1.0 - stereo.y );
// right eye for right side lower than left side:  r0.x = r0.x -stereo.x*(1.0 - stereo.y +0.5);
r0.x = r0.x -stereo.x*(1.0 - stereo.y + fixRight);
}
if (stereo.x < 0) {
// left eye for right side upper than left side:  r0.x = r0.x -stereo.x*(1.0 - stereo.y + 0.5);
// left eye for right side lower than left side:  r0.x = r0.x -stereo.x*(1.0 - stereo.y );
r0.x = r0.x -stereo.x*(1.0 - stereo.y + fixLeft);
}
  r0.z = 1.0;
  o2.x = dot(r0.xyz, viewProjInverse._m00_m10_m30);
  o2.y = dot(r0.xyz, viewProjInverse._m01_m11_m31);
  o2.z = dot(r0.xyz, viewProjInverse._m02_m12_m32);
  
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.30.9200.20789
//
//   using 3Dmigoto v1.2.1 on Sat Oct 17 13:50:40 2015
//
//
// Buffer Definitions: 
//
// cbuffer $Globals
// {
//
//   float4x4 viewProjInverse;          // Offset:    0 Size:    64
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// $Globals                          cbuffer      NA          NA    0        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_VertexID              0   x           0   VERTID    uint   x   
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_POSITION              0   xyzw        0      POS   float   xyzw
// TEXCOORD                 0   xy          1     NONE   float   xy  
// TEXCOORD                 1   xyz         2     NONE   float   xyz 
//
vs_4_0
dcl_immediateConstantBuffer { { -1.000000, -1.000000, 0, 0},
                              { 1.000000, -1.000000, 0, 0},
                              { -1.000000, 1.000000, 0, 0},
                              { 1.000000, 1.000000, 0, 0} }
dcl_constantbuffer cb0[3], immediateIndexed
dcl_input_sgv v0.x, vertex_id
dcl_output_siv o0.xyzw, position
dcl_output o1.xy
dcl_output o2.xyz
dcl_temps 1
mov o0.zw, l(0,0,0,1.000000)
mov r0.x, v0.x
mov o0.xy, icb[r0.x + 0].xyxx
mad o1.xy, icb[r0.x + 0].xyxx, l(0.500000, 0.500000, 0.000000, 0.000000), l(0.500000, 0.500000, 0.000000, 0.000000)
mov r0.xy, icb[r0.x + 0].xyxx
mov r0.z, l(1.000000)
dp3 o2.x, r0.xyzx, cb0[0].xywx
dp3 o2.y, r0.xyzx, cb0[1].xywx
dp3 o2.z, r0.xyzx, cb0[2].xywx
ret 
// Approximately 10 instruction slots used

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