// PS to be used for zoom and color change in cockpit when sharpen is activated

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);
Texture2D<float4> StereoParams : register(t125);
// Texture2D<float4> t100 : register(t100);

void main( 
  float4 v0 : SV_Position0,
  float2 v1 : TEXCOORD0,
  float2 w1 : TEXCOORD1,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2,r3,r4,r5;
  uint4 bitmask, uiDest;
  float4 fDest;
  
// float4 filter;

float zoomRadius = IniParams.Load(0).x; // square zoom factor
float zoomBorder = IniParams.Load(0).y; // square black circle thickness
float zoomFactor = IniParams.Load(0).w; // zoom factor
float FovX = IniParams.Load(int2(6, 0)).x; // X Fov factor
float FovY = IniParams.Load(int2(6, 0)).y; // Y Fov factor
float colorFixAdd = IniParams.Load(int2(6, 0)).z; // color fix to add
float colorFixMul = IniParams.Load(int2(6, 0)).w; // color fix to mul

float ratio = FovX/FovY;

uint width;
uint height;
float2 tmp, coord;

tmp.xy = v1.xy;


 DiffTexture.GetDimensions(width, height);
 coord.xy = v1.xy - float2(0.5, 0.5);
 
// if ((coord.x*coord.x*ratio +coord.y*coord.y) < zoomRadius ){
	// tmp.x = v1.x/zoomFactor+0.5-0.5/zoomFactor;
	// tmp.y = v1.y/zoomFactor+0.5-0.5/zoomFactor;
	// }  
  
  r0.xy = cmp(float2(0.5,0.5) < Settings2.yx);
  if (r0.x != 0) {
    // r1.xyzw = DiffuseSamplerPoint.Sample(DiffuseSamplerPoint_s, v1.xy).xyzw;
	r1.xyzw = DiffuseSamplerPoint.Sample(DiffuseSamplerPoint_s, tmp.xy).xyzw;
    r1.xyzw = saturate(r1.xyzw);
  } else {
    // r1.xyzw = DiffTexture.SampleLevel(DiffuseSamplerLerp_s, v1.xy, 0).xyzw;
	r1.xyzw = DiffTexture.SampleLevel(DiffuseSamplerLerp_s, tmp.xy, 0).xyzw;
    r1.xyzw = saturate(r1.xyzw);
  }
  // r0.xz = -Settings2.zw + v1.xy;
  r0.xz = -Settings2.zw + tmp.xy;
  r2.xyzw = DiffTexture.SampleLevel(DiffuseSamplerLerp_s, r0.xz, 0).xyzw;
  r2.xyzw = saturate(r2.xyzw);
  // r0.xz = Settings2.zw + v1.xy;
  r0.xz = Settings2.zw + tmp.xy;
  r3.xyzw = DiffTexture.SampleLevel(DiffuseSamplerLerp_s, r0.xz, 0).xyzw;
  r3.xyzw = saturate(r3.xyzw);
  // r4.xyzw = Settings2.zwzw * float4(2,-2,-2,2) + v1.xyxy;
  r4.xyzw = Settings2.zwzw * float4(2,-2,-2,2) + tmp.xyxy;
  r5.xyzw = DiffuseSamplerPoint.Sample(DiffuseSamplerPoint_s, r4.xy).xyzw;
  r5.xyzw = saturate(r5.xyzw);
  r4.xyzw = DiffuseSamplerPoint.Sample(DiffuseSamplerPoint_s, r4.zw).xyzw;
  r4.xyzw = saturate(r4.xyzw);
  r2.xyzw = r3.xyzw + r2.xyzw;
  r2.xyzw = -r1.xyzw * float4(0.5,0.5,0.5,0.5) + r2.xyzw;
  r3.xyzw = r5.xyzw + r4.xyzw;
  r3.xyzw = float4(0.125,0.125,0.125,0.125) * r3.xyzw;
  r2.xyzw = r2.xyzw * float4(0.5,0.5,0.5,0.5) + r3.xyzw;
  r1.xyzw = saturate(r1.xyzw * float4(2,2,2,2) + -r2.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 Border
 // if (((coord.x*coord.x*ratio+coord.y*coord.y) < (zoomRadius+zoomBorder)) && ((coord.x*coord.x*ratio+coord.y*coord.y) > (zoomRadius-zoomBorder)) && zoomFactor >1) {
  if (((coord.x*coord.x*ratio+coord.y*coord.y) > zoomRadius/(zoomFactor*zoomFactor))  && zoomFactor >1) {
	o0.xyzw=float4(0,0,0,0);
	}
	
  o0.xyzw *= float4(colorFixMul,colorFixMul,colorFixMul,1.0);
  o0.xyzw += float4(colorFixAdd,colorFixAdd,colorFixAdd,0);
 
  // tmp.x=v0.x;
  // tmp.y=v0.y;
  // filter = t100.Load(int3(tmp.xy, 0)); 
  
  // if (filter.w == 1) o0.xyzw += float4(1,0,0,0);
  
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.2.57 on Sat Jun 03 15:27:32 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 6
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 
add r0.xz, v1.xxyx, -cb0[16].zzwz
sample_l r2.xyzw, r0.xzxx, t1.xyzw, s0, l(0.000000)
mov_sat r2.xyzw, r2.xyzw
add r0.xz, v1.xxyx, cb0[16].zzwz
sample_l r3.xyzw, r0.xzxx, t1.xyzw, s0, l(0.000000)
mov_sat r3.xyzw, r3.xyzw
mad r4.xyzw, cb0[16].zwzw, l(2.000000, -2.000000, -2.000000, 2.000000), v1.xyxy
sample r5.xyzw, r4.xyxx, t0.xyzw, s1
mov_sat r5.xyzw, r5.xyzw
sample r4.xyzw, r4.zwzz, t0.xyzw, s1
mov_sat r4.xyzw, r4.xyzw
add r2.xyzw, r2.xyzw, r3.xyzw
mad r2.xyzw, -r1.xyzw, l(0.500000, 0.500000, 0.500000, 0.500000), r2.xyzw
add r3.xyzw, r4.xyzw, r5.xyzw
mul r3.xyzw, r3.xyzw, l(0.125000, 0.125000, 0.125000, 0.125000)
mad r2.xyzw, r2.xyzw, l(0.500000, 0.500000, 0.500000, 0.500000), r3.xyzw
mad_sat r1.xyzw, r1.xyzw, l(2.000000, 2.000000, 2.000000, 2.000000), -r2.xyzw
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 39 instruction slots used

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