//TAA. Temporal antialiasing.
// ---- Created with 3Dmigoto v1.3.15 on Sat Mar 09 00:35:15 2019

cbuffer SceneInfo : register(b0)
{
  row_major float4x4 viewProjMat : packoffset(c0);
  row_major float3x4 transposeViewMat : packoffset(c4);
  row_major float3x4 transposeViewInvMat : packoffset(c7);
  float4 projElement[2] : packoffset(c10);
  float4 projInvElements[2] : packoffset(c12);
  row_major float4x4 viewProjInvMat : packoffset(c14);
  row_major float4x4 prevViewProjMat : packoffset(c18);
  float3 ZToLinear : packoffset(c22);
  float subdivisionLevel : packoffset(c22.w);
  float2 screenSize : packoffset(c23);
  float2 screenInverseSize : packoffset(c23.z);
  float2 cullingHelper : packoffset(c24);
  float cameraNearPlane : packoffset(c24.z);
  float cameraFarPlane : packoffset(c24.w);
  float4 viewFrustum[6] : packoffset(c25);
  float4 clipplane : packoffset(c31);
}

cbuffer Tonemap : register(b1)
{
  float exposureAdjustment : packoffset(c0);
  float tonemapRange : packoffset(c0.y);
  float sharpness : packoffset(c0.z);
  float preTonemapRange : packoffset(c0.w);
  int useAutoExposure : packoffset(c1);
  float echoBlend : packoffset(c1.y);
  float AABlend : packoffset(c1.z);
  float AASubPixel : packoffset(c1.w);
  float ResponsiveAARate : packoffset(c2);
}

SamplerState PointClamp_s : register(s0);
SamplerState BilinearClamp_s : register(s1);
Texture2D<float> ReadonlyDepth : register(t0);
Texture2D<float4> HDRImage : register(t1);
Texture2D<float4> PrevHDRImage : register(t2);
Texture2D<float4> Velocity : register(t3);


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


void main(
  float4 v0 : SV_Position0,
  float4 v1 : Kerare0,
  float v2 : Exposure0,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2,r3,r4,r5,r6,r7,r8;
  uint4 bitmask, uiDest;
  float4 fDest;

  float4 stereo = StereoParams.Load(0);
  float4 iniparams = IniParams.Load(0);
  float4 iniparams11 = IniParams.Load(int2(11,0));
  
  r0.xy = screenInverseSize.xy * v0.xy;
  r0.zw = screenInverseSize.xy * projElement[0].zw;
  
  
  
  r0.zw = -r0.zw * float2(0.5,0.5) + r0.xy;
  
  float depth = r0.z;
  
  r1.xyz = HDRImage.SampleLevel(PointClamp_s, r0.zw, 0).xyz;
  
  
  r1.xyz = max(float3(0,0,0), r1.xyz);
  r1.xyz = min(float3(65536,65536,65536), r1.xyz);
  
  //r1.xyz=(r1.x+r1.y+r1.z)/3;
  
  
  r1.xyz = v2.xxx * r1.xyz;
  
  
  
  r0.z = max(r1.x, r1.y);
  
  //float depth = 1/(r0.z*viewProjMat._m02-viewProjMat._m03);
  
  r0.z = max(r0.z, r1.z);
  
  
  
  r0.w = (int)r0.z & 0x7f800000;
  r0.w = cmp((int)r0.w == 0x7f800000);
  if (r0.w != 0) {
    r2.xyz = float3(1,1,1);
  }
  if (r0.w == 0) {
    r3.xy = (uint2)v0.xy;
    r4.xyzw = (int4)r3.xyxy + int4(-1,0,0,-1);
    r5.xy = r4.zw;
    r5.zw = float2(0,0);
    r0.w = ReadonlyDepth.Load(r5.xyz).x;
    r0.w = 1 + -r0.w;
    r4.zw = float2(0,0);
    r1.w = ReadonlyDepth.Load(r4.xyz).x;
    r1.w = 1 + -r1.w;
    r3.zw = float2(0,0);
    r2.w = ReadonlyDepth.Load(r3.xyz).x;
    r2.w = 1 + -r2.w;
    r4.xyzw = (int4)r3.xyxy + int4(0,1,1,0);
    r5.xy = r4.zw;
    r5.zw = float2(0,0);
    r3.z = ReadonlyDepth.Load(r5.xyz).x;
    r3.z = 1 + -r3.z;
    r4.zw = float2(0,0);
    r3.w = ReadonlyDepth.Load(r4.xyz).x;
    r3.w = 1 + -r3.w;
    r0.w = (int)r0.w & -16;
    r0.w = (int)r0.w + 4;
    bitmask.w = ((~(-1 << 4)) << 0) & 0xffffffff;  r1.w = (((uint)1 << 0) & bitmask.w) | ((uint)r1.w & ~bitmask.w);
    r2.w = (int)r2.w & -16;
    r2.w = (int)r2.w + 5;
    r3.zw = (int2)r3.zw & int2(-16,-16);
    r3.zw = (int2)r3.zw + int2(9,6);
    r0.w = min(r1.w, r0.w);
    r0.w = min(r0.w, r2.w);
    r0.w = min(r0.w, r3.z);
    r0.w = min(r0.w, r3.w);
    r1.w = (int)r0.w & 3;
    if (2 == 0) r0.w = 0; else if (2+2 < 32) {     r0.w = (uint)r0.w << (32-(2 + 2)); r0.w = (uint)r0.w >> (32-2);    } else r0.w = (uint)r0.w >> 2;
    r0.w = (int)r0.w + (int)r3.x;
    r4.x = (int)r0.w + -1;
    r0.w = (int)r1.w + (int)r3.y;
    r4.y = (int)r0.w + -1;
    r4.zw = float2(0,0);
    r3.xy = Velocity.Load(r4.xyz).yz;
	//r3.x+=stereo.x*(1+stereo.y*depth*-0.015*iniparams.x);
	
	r3.x+=stereo.x;
	
	//r3.x+=stereo.x*(depth-stereo.y)*iniparams.x/depth;
    r0.xy = r3.xy * float2(0.5,-0.5) + r0.xy;
    r0.xyw = PrevHDRImage.SampleLevel(BilinearClamp_s, r0.xy, 0).xyz;
    r0.xyw = v2.xxx * r0.xyw;
    r0.z = r0.z * preTonemapRange + 1;
    r1.xyz = r1.xyz / r0.zzz;
    r0.z = max(r0.x, r0.y);
    r0.z = max(r0.z, r0.w);
    r0.z = r0.z * preTonemapRange + 1;
    r0.xyz = r0.xyw / r0.zzz;
    r3.xw = float2(0,0);
    r3.yz = -screenInverseSize.yx;
    r3.xyzw = v0.xyxy * screenInverseSize.xyxy + r3.xyzw;
    r4.xyz = HDRImage.SampleLevel(BilinearClamp_s, r3.xy, 0).xyz;
    r3.xyz = HDRImage.SampleLevel(BilinearClamp_s, r3.zw, 0).xyz;
    r5.xw = screenInverseSize.xy;
    r5.yz = float2(0,0);
    r5.xyzw = v0.xyxy * screenInverseSize.xyxy + r5.xyzw;
    r6.xyz = HDRImage.SampleLevel(BilinearClamp_s, r5.xy, 0).xyz;
    r5.xyz = HDRImage.SampleLevel(BilinearClamp_s, r5.zw, 0).xyz;
    r4.xyz = v2.xxx * r4.xyz;
    r3.xyz = v2.xxx * r3.xyz;
    r6.xyz = v2.xxx * r6.xyz;
    r5.xyz = v2.xxx * r5.xyz;
    r0.w = max(r4.x, r4.y);
    r0.w = max(r0.w, r4.z);
    r0.w = r0.w * preTonemapRange + 1;
    r4.xyz = r4.xyz / r0.www;
    r0.w = max(r3.x, r3.y);
    r0.w = max(r0.w, r3.z);
    r0.w = r0.w * preTonemapRange + 1;
    r3.xyz = r3.xyz / r0.www;
    r0.w = max(r6.x, r6.y);
    r0.w = max(r0.w, r6.z);
    r0.w = r0.w * preTonemapRange + 1;
    r6.xyz = r6.xyz / r0.www;
    r0.w = max(r5.x, r5.y);
    r0.w = max(r0.w, r5.z);
    r0.w = r0.w * preTonemapRange + 1;
    r5.xyz = r5.xyz / r0.www;
    r7.xyz = max(r4.xyz, r1.xyz);
    r8.xyz = max(r6.xyz, r3.xyz);
    r8.xyz = max(r8.xyz, r5.xyz);
    r7.xyz = max(r8.xyz, r7.xyz);
    r4.xyz = min(r4.xyz, r1.xyz);
    r3.xyz = min(r6.xyz, r3.xyz);
    r3.xyz = min(r3.xyz, r5.xyz);
    r3.xyz = min(r4.xyz, r3.xyz);
    r4.xyz = r7.xyz + r3.xyz;
    r5.xyz = r7.xyz + -r3.xyz;
    r5.xyz = float3(0.5,0.5,0.5) * r5.xyz;
    r6.xyz = -r4.xyz * float3(0.5,0.5,0.5) + r0.xyz;
    r5.xyz = r6.xyz / r5.xyz;
    r0.w = max(abs(r5.x), abs(r5.y));
    r0.w = max(r0.w, abs(r5.z));
    r1.w = cmp(1 < r0.w);
    if (r1.w != 0) {
      r5.xyz = r6.xyz / r0.www;
      r0.xyz = r4.xyz * float3(0.5,0.5,0.5) + r5.xyz;
    }
    r0.w = max(r7.x, r7.y);
    r0.w = max(r0.w, r7.z);
    r1.w = max(r3.x, r3.y);
    r1.w = max(r1.w, r3.z);
    r2.w = -r1.w + r0.w;
    r0.w = r1.w + r0.w;
    r0.w = r2.w / r0.w;
	
	float aamult = 1.0;
	if ((stereo.x!=0 && iniparams11.x==0) || (stereo.x==0 && iniparams11.x==1))
	{
	  aamult = 0;
	}
	
    r1.w = AABlend * r0.w * aamult;
    r2.w = -AABlend * r0.w * aamult + AABlend * aamult;
    r0.w = saturate(r0.w * r2.w + r1.w);
    r0.xyz = r0.xyz + -r1.xyz;
    r2.xyz = r0.www * r0.xyz + r1.xyz;
  }
  r0.x = max(r2.x, r2.y);
  r0.x = max(r0.x, r2.z);
  r0.x = -r0.x * preTonemapRange + 1;
  r0.x = max(9.99999975e-005, r0.x);
  r0.xyz = r2.xyz / r0.xxx;
  r0.xyz = r0.xyz / v2.xxx;
  o0.xyz = max(float3(0,0,0), r0.xyz);
  o0.w = 1;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v1.3.15 on Sat Mar 09 00:35:15 2019
//
//
// Buffer Definitions:
//
// cbuffer SceneInfo
// {
//
//   row_major float4x4 viewProjMat;    // Offset:    0 Size:    64 [unused]
//   row_major float3x4 transposeViewMat;// Offset:   64 Size:    48 [unused]
//   row_major float3x4 transposeViewInvMat;// Offset:  112 Size:    48 [unused]
//   float4 projElement[2];             // Offset:  160 Size:    32
//   float4 projInvElements[2];         // Offset:  192 Size:    32 [unused]
//   row_major float4x4 viewProjInvMat; // Offset:  224 Size:    64 [unused]
//   row_major float4x4 prevViewProjMat;// Offset:  288 Size:    64 [unused]
//   float3 ZToLinear;                  // Offset:  352 Size:    12 [unused]
//   float subdivisionLevel;            // Offset:  364 Size:     4 [unused]
//   float2 screenSize;                 // Offset:  368 Size:     8 [unused]
//   float2 screenInverseSize;          // Offset:  376 Size:     8
//   float2 cullingHelper;              // Offset:  384 Size:     8 [unused]
//   float cameraNearPlane;             // Offset:  392 Size:     4 [unused]
//   float cameraFarPlane;              // Offset:  396 Size:     4 [unused]
//   float4 viewFrustum[6];             // Offset:  400 Size:    96 [unused]
//   float4 clipplane;                  // Offset:  496 Size:    16 [unused]
//
// }
//
// cbuffer Tonemap
// {
//
//   float exposureAdjustment;          // Offset:    0 Size:     4 [unused]
//   float tonemapRange;                // Offset:    4 Size:     4 [unused]
//   float sharpness;                   // Offset:    8 Size:     4 [unused]
//   float preTonemapRange;             // Offset:   12 Size:     4
//   int useAutoExposure;               // Offset:   16 Size:     4 [unused]
//   float echoBlend;                   // Offset:   20 Size:     4 [unused]
//   float AABlend;                     // Offset:   24 Size:     4
//   float AASubPixel;                  // Offset:   28 Size:     4 [unused]
//   float ResponsiveAARate;            // Offset:   32 Size:     4 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// PointClamp                        sampler      NA          NA    0        1
// BilinearClamp                     sampler      NA          NA    1        1
// ReadonlyDepth                     texture   float          2d    0        1
// HDRImage                          texture  float4          2d    1        1
// PrevHDRImage                      texture  float4          2d    2        1
// Velocity                          texture  float4          2d    3        1
// SceneInfo                         cbuffer      NA          NA    0        1
// Tonemap                           cbuffer      NA          NA    1        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float   xy
// Kerare                   0   xyzw        1     NONE   float
// Exposure                 0   x           2     NONE   float   x
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
//
ps_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[24], immediateIndexed
dcl_constantbuffer cb1[2], 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_resource_texture2d (float,float,float,float) t2
dcl_resource_texture2d (float,float,float,float) t3
dcl_input_ps_siv linear noperspective v0.xy, position
dcl_input_ps linear v2.x
dcl_output o0.xyzw
dcl_temps 9
mul r0.xy, v0.xyxx, cb0[23].zwzz
mul r0.zw, cb0[10].zzzw, cb0[23].zzzw
mad r0.zw, -r0.zzzw, l(0.000000, 0.000000, 0.500000, 0.500000), r0.xxxy
sample_l_indexable(texture2d)(float,float,float,float) r1.xyz, r0.zwzz, t1.xyzw, s0, l(0.000000)
max r1.xyz, r1.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
min r1.xyz, r1.xyzx, l(65536.000000, 65536.000000, 65536.000000, 0.000000)
mul r1.xyz, r1.xyzx, v2.xxxx
max r0.z, r1.y, r1.x
max r0.z, r1.z, r0.z
and r0.w, r0.z, l(0x7f800000)
ieq r0.w, r0.w, l(0x7f800000)
if_nz r0.w
  mov r2.xyz, l(1.000000,1.000000,1.000000,0)
endif
if_z r0.w
  ftou r3.xy, v0.xyxx
  iadd r4.xyzw, r3.xyxy, l(-1, 0, 0, -1)
  mov r5.xy, r4.zwzz
  mov r5.zw, l(0,0,0,0)
  ld_indexable(texture2d)(float,float,float,float) r0.w, r5.xyzw, t0.yzwx
  add r0.w, -r0.w, l(1.000000)
  mov r4.zw, l(0,0,0,0)
  ld_indexable(texture2d)(float,float,float,float) r1.w, r4.xyzw, t0.yzwx
  add r1.w, -r1.w, l(1.000000)
  mov r3.zw, l(0,0,0,0)
  ld_indexable(texture2d)(float,float,float,float) r2.w, r3.xyzw, t0.yzwx
  add r2.w, -r2.w, l(1.000000)
  iadd r4.xyzw, r3.xyxy, l(0, 1, 1, 0)
  mov r5.xy, r4.zwzz
  mov r5.zw, l(0,0,0,0)
  ld_indexable(texture2d)(float,float,float,float) r3.z, r5.xyzw, t0.yzxw
  add r3.z, -r3.z, l(1.000000)
  mov r4.zw, l(0,0,0,0)
  ld_indexable(texture2d)(float,float,float,float) r3.w, r4.xyzw, t0.yzwx
  add r3.w, -r3.w, l(1.000000)
  and r0.w, r0.w, l(-16)
  iadd r0.w, r0.w, l(4)
  bfi r1.w, l(4), l(0), l(1), r1.w
  and r2.w, r2.w, l(-16)
  iadd r2.w, r2.w, l(5)
  and r3.zw, r3.zzzw, l(0, 0, -16, -16)
  iadd r3.zw, r3.zzzw, l(0, 0, 9, 6)
  min r0.w, r0.w, r1.w
  min r0.w, r2.w, r0.w
  min r0.w, r3.z, r0.w
  min r0.w, r3.w, r0.w
  and r1.w, r0.w, l(3)
  ubfe r0.w, l(2), l(2), r0.w
  iadd r0.w, r0.w, r3.x
  iadd r4.x, r0.w, l(-1)
  iadd r0.w, r1.w, r3.y
  iadd r4.y, r0.w, l(-1)
  mov r4.zw, l(0,0,0,0)
  ld_indexable(texture2d)(float,float,float,float) r3.xy, r4.xyzw, t3.yzxw
  mad r0.xy, r3.xyxx, l(0.500000, -0.500000, 0.000000, 0.000000), r0.xyxx
  sample_l_indexable(texture2d)(float,float,float,float) r0.xyw, r0.xyxx, t2.xywz, s1, l(0.000000)
  mul r0.xyw, r0.xyxw, v2.xxxx
  mad r0.z, r0.z, cb1[0].w, l(1.000000)
  div r1.xyz, r1.xyzx, r0.zzzz
  max r0.z, r0.y, r0.x
  max r0.z, r0.w, r0.z
  mad r0.z, r0.z, cb1[0].w, l(1.000000)
  div r0.xyz, r0.xywx, r0.zzzz
  mov r3.xw, l(0,0,0,0)
  mov r3.yz, -cb0[23].wwzw
  mad r3.xyzw, v0.xyxy, cb0[23].zwzw, r3.xyzw
  sample_l_indexable(texture2d)(float,float,float,float) r4.xyz, r3.xyxx, t1.xyzw, s1, l(0.000000)
  sample_l_indexable(texture2d)(float,float,float,float) r3.xyz, r3.zwzz, t1.xyzw, s1, l(0.000000)
  mov r5.xw, cb0[23].zzzw
  mov r5.yz, l(0,0,0,0)
  mad r5.xyzw, v0.xyxy, cb0[23].zwzw, r5.xyzw
  sample_l_indexable(texture2d)(float,float,float,float) r6.xyz, r5.xyxx, t1.xyzw, s1, l(0.000000)
  sample_l_indexable(texture2d)(float,float,float,float) r5.xyz, r5.zwzz, t1.xyzw, s1, l(0.000000)
  mul r4.xyz, r4.xyzx, v2.xxxx
  mul r3.xyz, r3.xyzx, v2.xxxx
  mul r6.xyz, r6.xyzx, v2.xxxx
  mul r5.xyz, r5.xyzx, v2.xxxx
  max r0.w, r4.y, r4.x
  max r0.w, r4.z, r0.w
  mad r0.w, r0.w, cb1[0].w, l(1.000000)
  div r4.xyz, r4.xyzx, r0.wwww
  max r0.w, r3.y, r3.x
  max r0.w, r3.z, r0.w
  mad r0.w, r0.w, cb1[0].w, l(1.000000)
  div r3.xyz, r3.xyzx, r0.wwww
  max r0.w, r6.y, r6.x
  max r0.w, r6.z, r0.w
  mad r0.w, r0.w, cb1[0].w, l(1.000000)
  div r6.xyz, r6.xyzx, r0.wwww
  max r0.w, r5.y, r5.x
  max r0.w, r5.z, r0.w
  mad r0.w, r0.w, cb1[0].w, l(1.000000)
  div r5.xyz, r5.xyzx, r0.wwww
  max r7.xyz, r1.xyzx, r4.xyzx
  max r8.xyz, r3.xyzx, r6.xyzx
  max r8.xyz, r5.xyzx, r8.xyzx
  max r7.xyz, r7.xyzx, r8.xyzx
  min r4.xyz, r1.xyzx, r4.xyzx
  min r3.xyz, r3.xyzx, r6.xyzx
  min r3.xyz, r5.xyzx, r3.xyzx
  min r3.xyz, r3.xyzx, r4.xyzx
  add r4.xyz, r3.xyzx, r7.xyzx
  add r5.xyz, -r3.xyzx, r7.xyzx
  mul r5.xyz, r5.xyzx, l(0.500000, 0.500000, 0.500000, 0.000000)
  mad r6.xyz, -r4.xyzx, l(0.500000, 0.500000, 0.500000, 0.000000), r0.xyzx
  div r5.xyz, r6.xyzx, r5.xyzx
  max r0.w, |r5.y|, |r5.x|
  max r0.w, |r5.z|, r0.w
  lt r1.w, l(1.000000), r0.w
  if_nz r1.w
    div r5.xyz, r6.xyzx, r0.wwww
    mad r0.xyz, r4.xyzx, l(0.500000, 0.500000, 0.500000, 0.000000), r5.xyzx
  endif
  max r0.w, r7.y, r7.x
  max r0.w, r7.z, r0.w
  max r1.w, r3.y, r3.x
  max r1.w, r3.z, r1.w
  add r2.w, r0.w, -r1.w
  add r0.w, r0.w, r1.w
  div r0.w, r2.w, r0.w
  mul r1.w, r0.w, cb1[1].z
  mad r2.w, -cb1[1].z, r0.w, cb1[1].z
  mad_sat r0.w, r0.w, r2.w, r1.w
  add r0.xyz, -r1.xyzx, r0.xyzx
  mad r2.xyz, r0.wwww, r0.xyzx, r1.xyzx
endif
max r0.x, r2.y, r2.x
max r0.x, r2.z, r0.x
mad r0.x, -r0.x, cb1[0].w, l(1.000000)
max r0.x, r0.x, l(0.000100)
div r0.xyz, r2.xyzx, r0.xxxx
div r0.xyz, r0.xyzx, v2.xxxx
max o0.xyz, r0.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
mov o0.w, l(1.000000)
ret
// Approximately 135 instruction slots used

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