// PS to be used for zoom and color change in cockpit

cbuffer _Globals : register(b0)
{
  float4 lightCount : packoffset(c0);
  float4 ambient : packoffset(c1);
  float4 lightInfo[4] : packoffset(c2);
  float4 g_vAmbientSettings : packoffset(c6) = {0,0,0,0};
  float4 g_vScreenDepthRect : packoffset(c7);
  float4 g_vDitherSettings : packoffset(c8);
  float4 g_ColorK : packoffset(c9) = {0.270000011,0.670000017,0.0599999987,0};
  float4 LevelMin : packoffset(c10) = {0.100000001,0.100000001,0.100000001,0};
  float4 LevelMax : packoffset(c11) = {0.800000012,0.800000012,0.800000012,0};
  float4 MaxAdd : packoffset(c12) = {0.100000001,0.100000001,0.100000001,0};
  float4 MinSub : packoffset(c13) = {0.0199999996,0.0199999996,0.0199999996,0};
  float4 Color : packoffset(c14);
  float4 Settings : packoffset(c15);
  float4 Settings2 : packoffset(c16);
  float4 Settings3 : packoffset(c17);
  float4 Settings4 : packoffset(c18);
  float4 Transform : packoffset(c19);
  float4 g_vDepthCoordsClamp : packoffset(c20);
  float4 CamPos : packoffset(c21);
  float4 g_vCenterOffs : packoffset(c22);
  float4 g_vHFrustrum : packoffset(c23);
  float4 g_vVFrustrum : packoffset(c24);
  float4 g_avNoise[8] : packoffset(c25);
  float4x4 g_mView : packoffset(c33);
  float4 g_PixelsDir[8] : packoffset(c37);
}


SamplerState DiffuseSamplerLerp_s : register(s0);
SamplerState DiffuseSamplerPoint_s : register(s1);
Texture2D<float4> DiffuseSamplerPoint : register(t0);
Texture2D<float4> DiffTexture : register(t1);

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

cbuffer _Globals : register(b12)
{
  float4 g_vCamPos1 : packoffset(c37);
}

cbuffer _Globals : register(b13)
{
  float4 g_vCamPos2 : packoffset(c37);
}

// debug filter
// Texture2D<float4> t16 : register(t121);
// end debug filter

void main( 
  float4 v0 : SV_Position0,
  float2 v1 : TEXCOORD0,
  float2 w1 : TEXCOORD1,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1;
  uint4 bitmask, uiDest;
  float4 fDest;
float zoomRadius = IniParams.Load(0).x; // square zoom factor
float zoomFactor = IniParams.Load(0).w; // zoom factor
float fovXshift = IniParams.Load(int2(6, 0)).x; // global shift (IPD modifier like effect)
float zoomShift = IniParams.Load(int2(6, 0)).y;  
float maskXshift = IniParams.Load(int2(7, 0)).x; // shift for mask
float maskGshift = IniParams.Load(int2(7, 0)).y; // Global up and left shift for mask
float colorFixAdd = IniParams.Load(int2(6, 0)).z; // color fix to add
float colorFixMul = IniParams.Load(int2(6, 0)).w; // color fix to mul


// xShift = 0.05 for zoom=2
// xShift = 0.025 for zoom=4
// float xShift=ZoomXshift+fovXshift;

float2 tmp, coord;
tmp.xy = v1.xy;

// debug filter
// uint width;
// uint height;
// float4 filter;
	 // if ((g_vCamPos2.x + g_vCamPos2.y + g_vCamPos2.z) != (g_vCamPos1.x + g_vCamPos1.y + g_vCamPos1.z)) {
		/* Right Eye */
		// fovXshift += zoomShift;
		// }
		// else  {
		/* Left eye */
		// fovXshift = -fovXshift;
		// fovXshift -= zoomShift;
		// }
// end debug filter

  r0.xy = cmp(float2(0.5,0.5) < Settings2.yx);
  if (r0.x != 0) {
    r1.xyzw = DiffuseSamplerPoint.Sample(DiffuseSamplerPoint_s, tmp.xy).xyzw;
    r1.xyzw = saturate(r1.xyzw);
  } else {
    r1.xyzw = DiffTexture.SampleLevel(DiffuseSamplerLerp_s, tmp.xy, 0).xyzw;
    r1.xyzw = saturate(r1.xyzw);
  }
  r0.x = dot(r1.xyzw, g_ColorK.xyzw);
  r1.xyzw = r1.xyzw + -r0.xxxx;
  r1.xyzw = r1.xyzw * Settings.wwww + r0.xxxx;
  if (r0.y != 0) {
    r0.xyz = float3(1,1,1) + -r1.xyz;
    r0.xyz = -r1.xyz * r0.xyz + float3(1,1,1);
    r1.xyz = r1.xyz / r0.xyz;
  }
  r0.xyz = log2(r1.xyz);
  r0.xyz = Settings.xyz * r0.xyz;
  r0.xyz = exp2(r0.xyz);
  o0.xyz = min(float3(1,1,1), r0.xyz);
  o0.w = r1.w;
  
// Zoom mask
  if (zoomFactor >1) {
	 if ((g_vCamPos2.x + g_vCamPos2.y + g_vCamPos2.z) != (g_vCamPos1.x + g_vCamPos1.y + g_vCamPos1.z)) {
		// Right Eye
		fovXshift -= maskXshift;
		}
		else  {
		// Left eye
		fovXshift = -fovXshift;
		fovXshift += maskXshift;
		}
		coord.xy = v1.xy + float2(maskGshift,maskGshift) - float2(0.5, 0.5)+float2(fovXshift,0);
		if ((coord.x*coord.x+coord.y*coord.y) > zoomRadius) o0.xyzw=float4(0,0,0,0);
	}
	
  o0.xyzw *= float4(colorFixMul,colorFixMul,colorFixMul,1.0);
  o0.xyzw += float4(colorFixAdd,colorFixAdd,colorFixAdd,0);
  
  // if (g_vCamPos1.x  == 0) o0.xyz+=float3(0,0,1); //oeil D
  // if (g_vCamPos.z < 0) o0.xyz+=float3(0,1,0); // oeil G
  
  
// debug filter  
  // t16.GetDimensions(width, height);  
	// tmp.x = v0.x/2- fovXshift*width/2;;
	// tmp.y = v0.y/2;
  // filter = t16.Load(int3(tmp.xy, 0)); 
   // if (filter.x == 1 && filter.y == 1 && filter.z == 1 && filter.w == 1) o0.x+=1.0; 
// end debug filter

  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.2.57 on Wed Apr 19 22:56:06 2017
//
//
// Buffer Definitions: 
//
// cbuffer $Globals
// {
//
//   float4 lightCount;                 // Offset:    0 Size:    16 [unused]
//   float4 ambient;                    // Offset:   16 Size:    16 [unused]
//   float4 lightInfo[4];               // Offset:   32 Size:    64 [unused]
//   float4 g_vAmbientSettings;         // Offset:   96 Size:    16 [unused]
//      = 0x00000000 0x00000000 0x00000000 0x00000000 
//   float4 g_vScreenDepthRect;         // Offset:  112 Size:    16 [unused]
//   float4 g_vDitherSettings;          // Offset:  128 Size:    16 [unused]
//   float4 g_ColorK;                   // Offset:  144 Size:    16
//      = 0x3e8a3d71 0x3f2b851f 0x3d75c28f 0x00000000 
//   float4 LevelMin;                   // Offset:  160 Size:    16 [unused]
//      = 0x3dcccccd 0x3dcccccd 0x3dcccccd 0x00000000 
//   float4 LevelMax;                   // Offset:  176 Size:    16 [unused]
//      = 0x3f4ccccd 0x3f4ccccd 0x3f4ccccd 0x00000000 
//   float4 MaxAdd;                     // Offset:  192 Size:    16 [unused]
//      = 0x3dcccccd 0x3dcccccd 0x3dcccccd 0x00000000 
//   float4 MinSub;                     // Offset:  208 Size:    16 [unused]
//      = 0x3ca3d70a 0x3ca3d70a 0x3ca3d70a 0x00000000 
//   float4 Color;                      // Offset:  224 Size:    16 [unused]
//   float4 Settings;                   // Offset:  240 Size:    16
//   float4 Settings2;                  // Offset:  256 Size:    16
//   float4 Settings3;                  // Offset:  272 Size:    16 [unused]
//   float4 Settings4;                  // Offset:  288 Size:    16 [unused]
//   float4 Transform;                  // Offset:  304 Size:    16 [unused]
//   float4 g_vDepthCoordsClamp;        // Offset:  320 Size:    16 [unused]
//   float4 CamPos;                     // Offset:  336 Size:    16 [unused]
//   float4 g_vCenterOffs;              // Offset:  352 Size:    16 [unused]
//   float4 g_vHFrustrum;               // Offset:  368 Size:    16 [unused]
//   float4 g_vVFrustrum;               // Offset:  384 Size:    16 [unused]
//   float4 g_avNoise[8];               // Offset:  400 Size:   128 [unused]
//   float4x4 g_mView;                  // Offset:  528 Size:    64 [unused]
//   float4 g_PixelsDir[8];             // Offset:  592 Size:   128 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// DiffuseSamplerLerp                sampler      NA          NA    0        1
// DiffuseSamplerPoint               sampler      NA          NA    1        1
// DiffuseSamplerPoint               texture  float4          2d    0        1
// DiffTexture                       texture  float4          2d    1        1
// $Globals                          cbuffer      NA          NA    0        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float       
// TEXCOORD                 0   xy          1     NONE   float   xy  
// TEXCOORD                 1     zw        1     NONE   float       
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
//
ps_4_0
dcl_constantbuffer cb0[17], immediateIndexed
dcl_sampler s0, mode_default
dcl_sampler s1, mode_default
dcl_resource_texture2d (float,float,float,float) t0
dcl_resource_texture2d (float,float,float,float) t1
dcl_input_ps linear v1.xy
dcl_output o0.xyzw
dcl_temps 2
lt r0.xy, l(0.500000, 0.500000, 0.000000, 0.000000), cb0[16].yxyy
if_nz r0.x
  sample r1.xyzw, v1.xyxx, t0.xyzw, s1
  mov_sat r1.xyzw, r1.xyzw
else 
  sample_l r1.xyzw, v1.xyxx, t1.xyzw, s0, l(0.000000)
  mov_sat r1.xyzw, r1.xyzw
endif 
dp4 r0.x, r1.xyzw, cb0[9].xyzw
add r1.xyzw, -r0.xxxx, r1.xyzw
mad r1.xyzw, r1.xyzw, cb0[15].wwww, r0.xxxx
if_nz r0.y
  add r0.xyz, -r1.xyzx, l(1.000000, 1.000000, 1.000000, 0.000000)
  mad r0.xyz, -r1.xyzx, r0.xyzx, l(1.000000, 1.000000, 1.000000, 0.000000)
  div r1.xyz, r1.xyzx, r0.xyzx
endif 
log r0.xyz, r1.xyzx
mul r0.xyz, r0.xyzx, cb0[15].xyzx
exp r0.xyz, r0.xyzx
min o0.xyz, r0.xyzx, l(1.000000, 1.000000, 1.000000, 0.000000)
mov o0.w, r1.w
ret 
// Approximately 22 instruction slots used

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