

cbuffer cbSSAOParam : register(b0)
{
  float4 g_vDepthComputeParam : packoffset(c0);
  float4 g_vAOScaleOffset : packoffset(c1);
  float4 g_vBlendScale : packoffset(c2);
}

cbuffer cbSampleOffset : register(b1)
{
  float4 g_avSampleOffsets[16] : packoffset(c0);
}

cbuffer cbSSAOParam2 : register(b2)
{
  float4x4 g_mInvProj : packoffset(c0);
  float4 g_vScreenSize : packoffset(c4);
  float4x4 g_mView : packoffset(c5);
  float4 g_vAOScaleOffset2 : packoffset(c9);
  float4 g_vSampleRangeParam : packoffset(c10);
  float4x4 g_mCurToPrevScreen : packoffset(c11);
  float4 g_vPrevFrameStrengh : packoffset(c15);
  float4 g_vMiscParam : packoffset(c16);
  float4 g_vDepthRangeParam : packoffset(c17);
}

SamplerState SS_WrapPoint_s : register(s0);
SamplerState SS_ClampPoint_s : register(s2);
SamplerState SS_ClampLinear_s : register(s3);
SamplerState SS_WhiteBorderPoint_s : register(s4);
Texture2D<float4> g_DepthTex : register(t0);
Texture2D<float4> g_NormalTex : register(t1);
Texture2D<float4> g_RandomRotationTex : register(t2);
Texture2D<float4> g_PrevAOTex : register(t3);


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


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

  r0.z = g_DepthTex.SampleLevel(SS_WhiteBorderPoint_s, v1.xy, 0).x;
  r0.xy = v1.xy * float2(2,-2) + float2(-1,1);
  r0.w = 1;
  r1.x = dot(r0.xyzw, g_mInvProj._m00_m10_m20_m30);
  r1.y = dot(r0.xyzw, g_mInvProj._m01_m11_m21_m31);
  r1.z = dot(r0.xyzw, g_mInvProj._m02_m12_m22_m32);
  r1.w = dot(r0.xyzw, g_mInvProj._m03_m13_m23_m33);
  r1.xyz = r1.xyz / r1.www;
  r1.w = cmp(g_vDepthRangeParam.x >= r0.z);
  if (r1.w != 0) {
    r2.x = g_vAOScaleOffset2.x + g_vAOScaleOffset2.y;
  } else {
    r1.w = 1 / r1.z;
    r3.xyz = g_NormalTex.Sample(SS_ClampPoint_s, v1.xy).xyz;
    r3.xyz = r3.xyz * float3(2,2,2) + float3(-1,-1,-1);
    r3.w = dot(r3.xyz, r3.xyz);
    r3.w = rsqrt(r3.w);
    r3.xyz = r3.xyz * r3.www;
    r4.x = dot(r3.xyz, g_mView._m00_m10_m20);
    r4.y = dot(r3.xyz, g_mView._m01_m11_m21);
    r4.z = dot(r3.xyz, g_mView._m02_m12_m22);
    r3.xy = g_vScreenSize.xy * v1.xy;
    r3.xy = float2(0.015625,0.015625) * r3.xy;
    r3.xy = g_RandomRotationTex.SampleLevel(SS_WrapPoint_s, r3.xy, 0).xy;
    r3.xy = float2(-0.5,-0.5) + r3.xy;
    r3.xy = r3.xy + r3.xy;
    r3.w = g_vSampleRangeParam.x / r1.z;
    r3.w = max(g_vSampleRangeParam.y, r3.w);
    r5.y = min(g_vSampleRangeParam.z, r3.w);
    r5.x = g_vMiscParam.y * r5.y;
    r5.xy = g_vScreenSize.zw * r5.xy;
    r3.z = -r3.x;
    r3.xyzw = r5.xyxy * r3.yzxy;
    r5.w = 1;
    r6.w = 1;
    r4.w = 0;
    r7.x = 0;
    while (true) {
      r7.y = cmp((int)r7.x >= 8);
      if (r7.y != 0) break;
      r8.xyzw = g_avSampleOffsets[r7.x].xxyy * r3.xyzw;
      r7.yz = r8.xy + r8.zw;
      r8.xy = v1.xy + r7.yz;
      r5.z = g_DepthTex.SampleLevel(SS_WhiteBorderPoint_s, r8.xy, 0).x;
      r5.xy = r8.xy * float2(2,-2) + float2(-1,1);
      r8.x = dot(r5.xyzw, g_mInvProj._m00_m10_m20_m30);
      r8.y = dot(r5.xyzw, g_mInvProj._m01_m11_m21_m31);
      r8.z = dot(r5.xyzw, g_mInvProj._m02_m12_m22_m32);
      r5.x = dot(r5.xyzw, g_mInvProj._m03_m13_m23_m33);
      r8.xyz = r8.xyz / r5.xxx;
      r5.xy = v1.xy + -r7.yz;
      r6.z = g_DepthTex.SampleLevel(SS_WhiteBorderPoint_s, r5.xy, 0).x;
      r6.xy = r5.xy * float2(2,-2) + float2(-1,1);
      r5.x = dot(r6.xyzw, g_mInvProj._m00_m10_m20_m30);
      r5.y = dot(r6.xyzw, g_mInvProj._m01_m11_m21_m31);
      r5.z = dot(r6.xyzw, g_mInvProj._m02_m12_m22_m32);
      r6.x = dot(r6.xyzw, g_mInvProj._m03_m13_m23_m33);
      r5.xyz = r5.xyz / r6.xxx;
      r6.xyz = r8.xyz + -r1.xyz;
      r7.y = dot(r6.xyz, r6.xyz);
      r7.y = rsqrt(r7.y);
      r6.xyz = r7.yyy * r6.xyz;
      r7.yzw = r5.xyz + -r1.xyz;
      r5.x = dot(r7.yzw, r7.yzw);
      r5.x = rsqrt(r5.x);
      r7.yzw = r7.yzw * r5.xxx;
      r8.w = r5.z;
      r5.xy = r1.zz * float2(0.899999976,0.899999976) + -r8.zw;
      r5.xy = r5.xy * r1.ww;
      r5.xy = saturate(float2(10,10) * r5.xy);
      r5.z = dot(r6.xyz, r4.xyz);
      r5.z = saturate(1 + -r5.z);
      r6.x = dot(r7.yzw, r4.xyz);
      r6.x = saturate(1 + -r6.x);
      r6.y = 1 + -r5.z;
      r5.x = r5.x * r6.y + r5.z;
      r5.z = 1 + -r6.x;
      r5.y = r5.y * r5.z + r6.x;
      r5.x = r5.x + r5.y;
      r5.x = 0.5 * r5.x;
      r5.x = r5.x * r5.x;
      r4.w = g_avSampleOffsets[r7.x].z * r5.x + r4.w;
      r7.x = (int)r7.x + 1;
    }
    r1.x = saturate(r1.z * g_vDepthRangeParam.y + g_vDepthRangeParam.z);
    r1.y = 1 + -r4.w;
    r1.x = r1.x * r1.y + r4.w;
    r1.x = r1.x * g_vAOScaleOffset2.x + g_vAOScaleOffset2.y;
    r0.xy = v1.xy;
    r3.x = dot(r0.xyzw, g_mCurToPrevScreen._m00_m10_m20_m30);
    r3.y = dot(r0.xyzw, g_mCurToPrevScreen._m01_m11_m21_m31);
    r3.z = dot(r0.xyzw, g_mCurToPrevScreen._m02_m12_m22_m32);
    r0.x = dot(r0.xyzw, g_mCurToPrevScreen._m03_m13_m23_m33);
    r0.xyz = r3.xyz / r0.xxx;
    r0.z = g_vDepthComputeParam.x + -r0.z;
    r0.z = -g_vDepthComputeParam.y / r0.z;
    r0.z = g_vDepthComputeParam.z * r0.z;
    r0.w = g_PrevAOTex.SampleLevel(SS_ClampLinear_s, r0.xy, 0).x;
    r3.xyw = g_PrevAOTex.SampleLevel(SS_ClampPoint_s, r0.xy, 0).yzw;
    r0.x = dot(r3.xyw, float3(1.53787005e-005,0.00392156886,1));
    r0.x = -r0.x * g_vDepthComputeParam.z + r0.z;
    r0.y = g_vPrevFrameStrengh.z * r0.z;
    r0.x = saturate(abs(r0.x) / r0.y);
    r0.x = -r0.x * g_vPrevFrameStrengh.y + 1;
    r0.y = cmp(0 >= r3.z);
    r0.x = g_vPrevFrameStrengh.x * r0.x;
    r0.x = r0.y ? 0 : r0.x;
    r0.y = r0.w + -r1.x;
    r2.x = r0.x * r0.y + r1.x;
  }
  r0.x = saturate(r1.z / g_vDepthComputeParam.z);
  r0.xyz = float3(65025,255,1) * r0.xxx;
  r0.xy = frac(r0.xy);
  r2.yz = -r0.xy * float2(0.00392156886,0.00392156886) + r0.yz;
  r2.w = r0.x;
  o0.xyzw = r2.xwyz;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.30.9200.20546
//
//
// Buffer Definitions: 
//
// cbuffer cbSSAOParam
// {
//
//   float4 g_vDepthComputeParam;       // Offset:    0 Size:    16
//   float4 g_vAOScaleOffset;           // Offset:   16 Size:    16 [unused]
//   float4 g_vBlendScale;              // Offset:   32 Size:    16 [unused]
//
// }
//
// cbuffer cbSampleOffset
// {
//
//   float4 g_avSampleOffsets[16];      // Offset:    0 Size:   256
//
// }
//
// cbuffer cbSSAOParam2
// {
//
//   float4x4 g_mInvProj;               // Offset:    0 Size:    64
//   float4 g_vScreenSize;              // Offset:   64 Size:    16
//   float4x4 g_mView;                  // Offset:   80 Size:    64
//   float4 g_vAOScaleOffset2;          // Offset:  144 Size:    16
//   float4 g_vSampleRangeParam;        // Offset:  160 Size:    16
//   float4x4 g_mCurToPrevScreen;       // Offset:  176 Size:    64
//   float4 g_vPrevFrameStrengh;        // Offset:  240 Size:    16
//   float4 g_vMiscParam;               // Offset:  256 Size:    16
//   float4 g_vDepthRangeParam;         // Offset:  272 Size:    16
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// SS_WrapPoint                      sampler      NA          NA    0        1
// SS_ClampPoint                     sampler      NA          NA    2        1
// SS_ClampLinear                    sampler      NA          NA    3        1
// SS_WhiteBorderPoint               sampler      NA          NA    4        1
// g_DepthTex                        texture  float4          2d    0        1
// g_NormalTex                       texture  float4          2d    1        1
// g_RandomRotationTex               texture  float4          2d    2        1
// g_PrevAOTex                       texture  float4          2d    3        1
// cbSSAOParam                       cbuffer      NA          NA    0        1
// cbSampleOffset                    cbuffer      NA          NA    1        1
// cbSSAOParam2                      cbuffer      NA          NA    2        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float       
// TEXCOORD                 0   xy          1     NONE   float   xy  
//
//
// 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[1], immediateIndexed
dcl_constantbuffer cb1[9], dynamicIndexed
dcl_constantbuffer cb2[18], immediateIndexed
dcl_sampler s0, mode_default
dcl_sampler s2, mode_default
dcl_sampler s3, mode_default
dcl_sampler s4, 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 linear v1.xy
dcl_output o0.xyzw
dcl_temps 9
sample_l_indexable(texture2d)(float,float,float,float) r0.z, v1.xyxx, t0.yzxw, s4, l(0.000000)
mad r0.xy, v1.xyxx, l(2.000000, -2.000000, 0.000000, 0.000000), l(-1.000000, 1.000000, 0.000000, 0.000000)
mov r0.w, l(1.000000)
dp4 r1.x, r0.xyzw, cb2[0].xyzw
dp4 r1.y, r0.xyzw, cb2[1].xyzw
dp4 r1.z, r0.xyzw, cb2[2].xyzw
dp4 r1.w, r0.xyzw, cb2[3].xyzw
div r1.xyz, r1.xyzx, r1.wwww
ge r1.w, cb2[17].x, r0.z
if_nz r1.w
  add r2.x, cb2[9].y, cb2[9].x
else 
  div r1.w, l(1.000000, 1.000000, 1.000000, 1.000000), r1.z
  sample_indexable(texture2d)(float,float,float,float) r3.xyz, v1.xyxx, t1.xyzw, s2
  mad r3.xyz, r3.xyzx, l(2.000000, 2.000000, 2.000000, 0.000000), l(-1.000000, -1.000000, -1.000000, 0.000000)
  dp3 r3.w, r3.xyzx, r3.xyzx
  rsq r3.w, r3.w
  mul r3.xyz, r3.wwww, r3.xyzx
  dp3 r4.x, r3.xyzx, cb2[5].xyzx
  dp3 r4.y, r3.xyzx, cb2[6].xyzx
  dp3 r4.z, r3.xyzx, cb2[7].xyzx
  mul r3.xy, v1.xyxx, cb2[4].xyxx
  mul r3.xy, r3.xyxx, l(0.015625, 0.015625, 0.000000, 0.000000)
  sample_l_indexable(texture2d)(float,float,float,float) r3.xy, r3.xyxx, t2.xyzw, s0, l(0.000000)
  add r3.xy, r3.xyxx, l(-0.500000, -0.500000, 0.000000, 0.000000)
  add r3.xy, r3.xyxx, r3.xyxx
  div r3.w, cb2[10].x, r1.z
  max r3.w, r3.w, cb2[10].y
  min r5.y, r3.w, cb2[10].z
  mul r5.x, r5.y, cb2[16].y
  mul r5.xy, r5.xyxx, cb2[4].zwzz
  mov r3.z, -r3.x
  mul r3.xyzw, r3.yzxy, r5.xyxy
  mov r5.w, l(1.000000)
  mov r6.w, l(1.000000)
  mov r4.w, l(0)
  mov r7.x, l(0)
  loop 
    ige r7.y, r7.x, l(8)
    breakc_nz r7.y
    mul r8.xyzw, r3.xyzw, cb1[r7.x + 0].xxyy
    add r7.yz, r8.zzwz, r8.xxyx
    add r8.xy, r7.yzyy, v1.xyxx
    sample_l_indexable(texture2d)(float,float,float,float) r5.z, r8.xyxx, t0.yzxw, s4, l(0.000000)
    mad r5.xy, r8.xyxx, l(2.000000, -2.000000, 0.000000, 0.000000), l(-1.000000, 1.000000, 0.000000, 0.000000)
    dp4 r8.x, r5.xyzw, cb2[0].xyzw
    dp4 r8.y, r5.xyzw, cb2[1].xyzw
    dp4 r8.z, r5.xyzw, cb2[2].xyzw
    dp4 r5.x, r5.xyzw, cb2[3].xyzw
    div r8.xyz, r8.xyzx, r5.xxxx
    add r5.xy, -r7.yzyy, v1.xyxx
    sample_l_indexable(texture2d)(float,float,float,float) r6.z, r5.xyxx, t0.yzxw, s4, l(0.000000)
    mad r6.xy, r5.xyxx, l(2.000000, -2.000000, 0.000000, 0.000000), l(-1.000000, 1.000000, 0.000000, 0.000000)
    dp4 r5.x, r6.xyzw, cb2[0].xyzw
    dp4 r5.y, r6.xyzw, cb2[1].xyzw
    dp4 r5.z, r6.xyzw, cb2[2].xyzw
    dp4 r6.x, r6.xyzw, cb2[3].xyzw
    div r5.xyz, r5.xyzx, r6.xxxx
    add r6.xyz, -r1.xyzx, r8.xyzx
    dp3 r7.y, r6.xyzx, r6.xyzx
    rsq r7.y, r7.y
    mul r6.xyz, r6.xyzx, r7.yyyy
    add r7.yzw, -r1.xxyz, r5.xxyz
    dp3 r5.x, r7.yzwy, r7.yzwy
    rsq r5.x, r5.x
    mul r7.yzw, r5.xxxx, r7.yyzw
    mov r8.w, r5.z
    mad r5.xy, r1.zzzz, l(0.900000, 0.900000, 0.000000, 0.000000), -r8.zwzz
    mul r5.xy, r1.wwww, r5.xyxx
    mul_sat r5.xy, r5.xyxx, l(10.000000, 10.000000, 0.000000, 0.000000)
    dp3 r5.z, r6.xyzx, r4.xyzx
    add_sat r5.z, -r5.z, l(1.000000)
    dp3 r6.x, r7.yzwy, r4.xyzx
    add_sat r6.x, -r6.x, l(1.000000)
    add r6.y, -r5.z, l(1.000000)
    mad r5.x, r5.x, r6.y, r5.z
    add r5.z, -r6.x, l(1.000000)
    mad r5.y, r5.y, r5.z, r6.x
    add r5.x, r5.y, r5.x
    mul r5.x, r5.x, l(0.500000)
    mul r5.x, r5.x, r5.x
    mad r4.w, cb1[r7.x + 0].z, r5.x, r4.w
    iadd r7.x, r7.x, l(1)
  endloop 
  mad_sat r1.x, r1.z, cb2[17].y, cb2[17].z
  add r1.y, -r4.w, l(1.000000)
  mad r1.x, r1.x, r1.y, r4.w
  mad r1.x, r1.x, cb2[9].x, cb2[9].y
  mov r0.xy, v1.xyxx
  dp4 r3.x, r0.xyzw, cb2[11].xyzw
  dp4 r3.y, r0.xyzw, cb2[12].xyzw
  dp4 r3.z, r0.xyzw, cb2[13].xyzw
  dp4 r0.x, r0.xyzw, cb2[14].xyzw
  div r0.xyz, r3.xyzx, r0.xxxx
  add r0.z, -r0.z, cb0[0].x
  div r0.z, -cb0[0].y, r0.z
  mul r0.z, r0.z, cb0[0].z
  sample_l_indexable(texture2d)(float,float,float,float) r0.w, r0.xyxx, t3.yzwx, s3, l(0.000000)
  sample_l_indexable(texture2d)(float,float,float,float) r3.xyw, r0.xyxx, t3.yzxw, s2, l(0.000000)
  dp3 r0.x, r3.xywx, l(0.000015, 0.003922, 1.000000, 0.000000)
  mad r0.x, -r0.x, cb0[0].z, r0.z
  mul r0.y, r0.z, cb2[15].z
  div_sat r0.x, |r0.x|, r0.y
  mad r0.x, -r0.x, cb2[15].y, l(1.000000)
  ge r0.y, l(0.000000), r3.z
  mul r0.x, r0.x, cb2[15].x
  movc r0.x, r0.y, l(0), r0.x
  add r0.y, -r1.x, r0.w
  mad r2.x, r0.x, r0.y, r1.x
endif 
div_sat r0.x, r1.z, cb0[0].z
mul r0.xyz, r0.xxxx, l(65025.000000, 255.000000, 1.000000, 0.000000)
frc r0.xy, r0.xyxx
mad r2.yz, -r0.xxyx, l(0.000000, 0.003922, 0.003922, 0.000000), r0.yyzy
mov r2.w, r0.x
mov o0.xyzw, r2.xwyz
ret 
// Approximately 117 instruction slots used

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