// LIGHT BLOOM 

cbuffer CB_SHADER_COMMON_PARAM : register(b0)
{
  uint4 VolumeSize : packoffset(c0);
  float4 InvVolumeSize : packoffset(c1);
  float4 DepthRangeInfo : packoffset(c2);
  float4 LogDepthRangeInfo : packoffset(c3);
  float4 PreComputed : packoffset(c4);
  float4x4 ViewMatrix : packoffset(c5);
  float2 ViewTangent : packoffset(c9);
  float2 DepthComputeParam : packoffset(c9.z);
  float4 g_HeightFactor : packoffset(c10);
  float4 Global : packoffset(c11);
  float3 AmbientColor : packoffset(c12);
  float AmbientHeightAtten : packoffset(c12.w);
  float4 g_HeightFogParam : packoffset(c13);
  uint SampleCount : packoffset(c14);
  float DitherIntensity : packoffset(c14.y);
  float DirLightShadowDither : packoffset(c14.z);
  float CancelInScatteringRate : packoffset(c14.w);
  float3 DirLightDirection : packoffset(c15);
  float DirLightAngleInvCos : packoffset(c15.w);
  float3 DirLightDirectColor : packoffset(c16);
  float DirLightHeightAtten : packoffset(c16.w);
  float3 DirLightOppositeColor : packoffset(c17);
  uint DirLightShadowSampleCount : packoffset(c17.w);
  float4x4 mtxViewToShadowMap[4] : packoffset(c18);
  float4 vCascadeParam : packoffset(c34);
  float3 ExtinctionScale : packoffset(c35);
  float PreExposure : packoffset(c35.w);
  uint2 ScaledSceneSize : packoffset(c36);
  bool EnableAnalyticalFog : packoffset(c36.z);
  float4 aRandomVec[32] : packoffset(c37);
  float3 ShadowSubpixelOffset : packoffset(c69);
  float ReprojectionFeedback : packoffset(c69.w);
  float4x3 mtxNowToPrevView : packoffset(c70);
  float4 PrevLogDepthRangeInfo : packoffset(c73);
  float2 PrevViewTangent : packoffset(c74);
  float DirLightStartDist : packoffset(c75);
}

SamplerState SS_ClampPoint_s : register(s7);
SamplerState SS_ClampLinear_s : register(s9);
Texture3D<float4> g_InScatterTex : register(t0);
Texture2D<float4> g_SceneDepth : register(t1);


// 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,
  out float4 o1 : SV_TARGET1)
{
  float4 r0,r1,r2;
  uint4 bitmask, uiDest;
  float4 fDest;

  float depth = g_SceneDepth.Sample(SS_ClampPoint_s, v1.xy).x;
  
  r0.x = g_SceneDepth.Sample(SS_ClampPoint_s, v1.xy).x;
  r0.x = DepthComputeParam.x + -r0.x;
  r0.z = -DepthComputeParam.y / r0.x;
  r1.x = min(DepthRangeInfo.y, r0.z);
  r1.x = LogDepthRangeInfo.y + r1.x;
  r1.x = log2(r1.x);
  r1.x = -LogDepthRangeInfo.x + r1.x;
  r1.x = r1.x * LogDepthRangeInfo.w + PreComputed.y;
  r1.z = PreComputed.z * r1.x;
  r1.xy = v1.xy;
  
    float4 stereo = StereoParams.Load(0);
  r1.x -= stereo.x *(1.0 - depth * stereo.y) * 0.4;
  
  r1.x = g_InScatterTex.Sample(SS_ClampLinear_s, r1.xyz).w;
  r1.y = cmp(DepthRangeInfo.y < r0.z);
  r1.z = cmp(0 != EnableAnalyticalFog);
  r1.y = r1.z ? r1.y : 0;
  if (r1.y != 0) {
    r1.yz = v1.xy * float2(2,-2) + float2(-1,1);
    r1.yz = r1.yz * r0.zz;
    r0.xy = ViewTangent.xy * r1.yz;
    r1.yzw = DepthRangeInfo.yyy * r0.xyz;
    r2.xyz = r1.yzw / r0.zzz;
    r2.w = 1;
    r1.y = dot(r2.xyzw, g_HeightFactor.xyzw);
    r0.w = 1;
    r0.w = dot(r0.xyzw, g_HeightFactor.xyzw);
    r1.z = ViewMatrix._m11 * r0.y;
    r1.z = r0.x * ViewMatrix._m10 + r1.z;
    r1.z = r0.z * ViewMatrix._m12 + r1.z;
    r0.x = dot(r0.xyz, r0.xyz);
    r0.x = sqrt(r0.x);
    r0.y = r0.x / r1.z;
    r0.z = DepthRangeInfo.y / r0.z;
    r0.z = 1 + -r0.z;
    r0.x = r0.x * r0.z;
    r0.z = r0.w + -r1.y;
    r0.z = g_HeightFogParam.x * r0.z;
    r0.z = cmp(abs(r0.z) < 9.99999975e-005);
    if (r0.z != 0) {
      r0.z = cmp(0 < r1.y);
      r1.z = -g_HeightFogParam.x * r1.y;
      r1.z = 1.44269502 * r1.z;
      r1.z = exp2(r1.z);
      r1.w = g_HeightFogParam.x * r1.y;
      r1.w = 1.44269502 * r1.w;
      r1.w = exp2(r1.w);
      r1.w = 2 + -r1.w;
      r0.z = r0.z ? r1.z : r1.w;
      r0.z = r0.z * g_HeightFogParam.y + Global.x;
      r0.z = r0.x * r0.z;
    } else {
      r1.z = 1 / g_HeightFogParam.x;
      r1.w = max(r1.y, r0.w);
      r0.w = min(r1.y, r0.w);
      r1.y = max(0, r1.w);
      r2.x = max(0, r0.w);
      r1.y = -g_HeightFogParam.x * r1.y;
      r1.y = 1.44269502 * r1.y;
      r1.y = exp2(r1.y);
      r2.x = -g_HeightFogParam.x * r2.x;
      r2.x = 1.44269502 * r2.x;
      r2.x = exp2(r2.x);
      r1.y = -r2.x + r1.y;
      r1.w = min(0, r1.w);
      r0.w = min(0, r0.w);
      r2.x = r1.w + -r0.w;
      r2.x = r2.x + r2.x;
      r1.w = g_HeightFogParam.x * r1.w;
      r1.w = 1.44269502 * r1.w;
      r1.w = exp2(r1.w);
      r0.w = g_HeightFogParam.x * r0.w;
      r0.w = 1.44269502 * r0.w;
      r0.w = exp2(r0.w);
      r0.w = r1.w + -r0.w;
      r0.w = -r1.z * r0.w + r2.x;
      r0.w = -r1.z * r1.y + r0.w;
      r0.w = g_HeightFogParam.y * r0.w;
      r0.x = Global.x * r0.x;
      r0.z = r0.w * abs(r0.y) + r0.x;
    }
    r0.x = -1.44269502 * r0.z;
    r0.x = exp2(r0.x);
    r1.x = r1.x * r0.x;
  }
  r0.x = log2(r1.x);
  r0.xyz = ExtinctionScale.xyz * r0.xxx;
  r0.xyz = exp2(r0.xyz);
  o0.xyz = r0.xyz;
  o0.w = 1;
  o1.xyz = r0.xyz;
  o1.w = 1;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.30.9200.20546
//
//
// Buffer Definitions: 
//
// cbuffer CB_SHADER_COMMON_PARAM
// {
//
//   uint4 VolumeSize;                  // Offset:    0 Size:    16 [unused]
//   float4 InvVolumeSize;              // Offset:   16 Size:    16 [unused]
//   float4 DepthRangeInfo;             // Offset:   32 Size:    16
//   float4 LogDepthRangeInfo;          // Offset:   48 Size:    16
//   float4 PreComputed;                // Offset:   64 Size:    16
//   float4x4 ViewMatrix;               // Offset:   80 Size:    64
//   float2 ViewTangent;                // Offset:  144 Size:     8
//   float2 DepthComputeParam;          // Offset:  152 Size:     8
//   float4 g_HeightFactor;             // Offset:  160 Size:    16
//   float4 Global;                     // Offset:  176 Size:    16
//   float3 AmbientColor;               // Offset:  192 Size:    12 [unused]
//   float AmbientHeightAtten;          // Offset:  204 Size:     4 [unused]
//   float4 g_HeightFogParam;           // Offset:  208 Size:    16
//   uint SampleCount;                  // Offset:  224 Size:     4 [unused]
//   float DitherIntensity;             // Offset:  228 Size:     4 [unused]
//   float DirLightShadowDither;        // Offset:  232 Size:     4 [unused]
//   float CancelInScatteringRate;      // Offset:  236 Size:     4 [unused]
//   float3 DirLightDirection;          // Offset:  240 Size:    12 [unused]
//   float DirLightAngleInvCos;         // Offset:  252 Size:     4 [unused]
//   float3 DirLightDirectColor;        // Offset:  256 Size:    12 [unused]
//   float DirLightHeightAtten;         // Offset:  268 Size:     4 [unused]
//   float3 DirLightOppositeColor;      // Offset:  272 Size:    12 [unused]
//   uint DirLightShadowSampleCount;    // Offset:  284 Size:     4 [unused]
//   float4x4 mtxViewToShadowMap[4];    // Offset:  288 Size:   256 [unused]
//   float4 vCascadeParam;              // Offset:  544 Size:    16 [unused]
//   float3 ExtinctionScale;            // Offset:  560 Size:    12
//   float PreExposure;                 // Offset:  572 Size:     4 [unused]
//   uint2 ScaledSceneSize;             // Offset:  576 Size:     8 [unused]
//   bool EnableAnalyticalFog;          // Offset:  584 Size:     4
//   float4 aRandomVec[32];             // Offset:  592 Size:   512 [unused]
//   float3 ShadowSubpixelOffset;       // Offset: 1104 Size:    12 [unused]
//   float ReprojectionFeedback;        // Offset: 1116 Size:     4 [unused]
//   float4x3 mtxNowToPrevView;         // Offset: 1120 Size:    48 [unused]
//   float4 PrevLogDepthRangeInfo;      // Offset: 1168 Size:    16 [unused]
//   float2 PrevViewTangent;            // Offset: 1184 Size:     8 [unused]
//   float DirLightStartDist;           // Offset: 1200 Size:     4 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// SS_ClampPoint                     sampler      NA          NA    7        1
// SS_ClampLinear                    sampler      NA          NA    9        1
// g_InScatterTex                    texture  float4          3d    0        1
// g_SceneDepth                      texture  float4          2d    1        1
// CB_SHADER_COMMON_PARAM            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  
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_TARGET                0   xyzw        0   TARGET   float   xyzw
// SV_TARGET                1   xyzw        1   TARGET   float   xyzw
//
ps_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[37], immediateIndexed
dcl_sampler s7, mode_default
dcl_sampler s9, mode_default
dcl_resource_texture3d (float,float,float,float) t0
dcl_resource_texture2d (float,float,float,float) t1
dcl_input_ps linear v1.xy
dcl_output o0.xyzw
dcl_output o1.xyzw
dcl_temps 3
sample_indexable(texture2d)(float,float,float,float) r0.x, v1.xyxx, t1.xyzw, s7
add r0.x, -r0.x, cb0[9].z
div r0.z, -cb0[9].w, r0.x
min r1.x, r0.z, cb0[2].y
add r1.x, r1.x, cb0[3].y
log r1.x, r1.x
add r1.x, r1.x, -cb0[3].x
mad r1.x, r1.x, cb0[3].w, cb0[4].y
mul r1.z, r1.x, cb0[4].z
mov r1.xy, v1.xyxx
sample_indexable(texture3d)(float,float,float,float) r1.x, r1.xyzx, t0.wxyz, s9
lt r1.y, cb0[2].y, r0.z
ine r1.z, l(0), cb0[36].z
and r1.y, r1.z, r1.y
if_nz r1.y
  mad r1.yz, v1.xxyx, l(0.000000, 2.000000, -2.000000, 0.000000), l(0.000000, -1.000000, 1.000000, 0.000000)
  mul r1.yz, r0.zzzz, r1.yyzy
  mul r0.xy, r1.yzyy, cb0[9].xyxx
  mul r1.yzw, r0.xxyz, cb0[2].yyyy
  div r2.xyz, r1.yzwy, r0.zzzz
  mov r2.w, l(1.000000)
  dp4 r1.y, r2.xyzw, cb0[10].xyzw
  mov r0.w, l(1.000000)
  dp4 r0.w, r0.xyzw, cb0[10].xyzw
  mul r1.z, r0.y, cb0[6].y
  mad r1.z, r0.x, cb0[5].y, r1.z
  mad r1.z, r0.z, cb0[7].y, r1.z
  dp3 r0.x, r0.xyzx, r0.xyzx
  sqrt r0.x, r0.x
  div r0.y, r0.x, r1.z
  div r0.z, cb0[2].y, r0.z
  add r0.z, -r0.z, l(1.000000)
  mul r0.x, r0.z, r0.x
  add r0.z, -r1.y, r0.w
  mul r0.z, r0.z, cb0[13].x
  lt r0.z, |r0.z|, l(0.000100)
  if_nz r0.z
    lt r0.z, l(0.000000), r1.y
    mul r1.z, r1.y, -cb0[13].x
    mul r1.z, r1.z, l(1.442695)
    exp r1.z, r1.z
    mul r1.w, r1.y, cb0[13].x
    mul r1.w, r1.w, l(1.442695)
    exp r1.w, r1.w
    add r1.w, -r1.w, l(2.000000)
    movc r0.z, r0.z, r1.z, r1.w
    mad r0.z, r0.z, cb0[13].y, cb0[11].x
    mul r0.z, r0.z, r0.x
  else 
    div r1.z, l(1.000000, 1.000000, 1.000000, 1.000000), cb0[13].x
    max r1.w, r0.w, r1.y
    min r0.w, r0.w, r1.y
    max r1.y, r1.w, l(0.000000)
    max r2.x, r0.w, l(0.000000)
    mul r1.y, r1.y, -cb0[13].x
    mul r1.y, r1.y, l(1.442695)
    exp r1.y, r1.y
    mul r2.x, r2.x, -cb0[13].x
    mul r2.x, r2.x, l(1.442695)
    exp r2.x, r2.x
    add r1.y, r1.y, -r2.x
    min r1.w, r1.w, l(0.000000)
    min r0.w, r0.w, l(0.000000)
    add r2.x, -r0.w, r1.w
    add r2.x, r2.x, r2.x
    mul r1.w, r1.w, cb0[13].x
    mul r1.w, r1.w, l(1.442695)
    exp r1.w, r1.w
    mul r0.w, r0.w, cb0[13].x
    mul r0.w, r0.w, l(1.442695)
    exp r0.w, r0.w
    add r0.w, -r0.w, r1.w
    mad r0.w, -r1.z, r0.w, r2.x
    mad r0.w, -r1.z, r1.y, r0.w
    mul r0.w, r0.w, cb0[13].y
    mul r0.x, r0.x, cb0[11].x
    mad r0.z, r0.w, |r0.y|, r0.x
  endif 
  mul r0.x, r0.z, l(-1.442695)
  exp r0.x, r0.x
  mul r1.x, r0.x, r1.x
endif 
log r0.x, r1.x
mul r0.xyz, r0.xxxx, cb0[35].xyzx
exp r0.xyz, r0.xyzx
mov o0.xyz, r0.xyzx
mov o0.w, l(1.000000)
mov o1.xyz, r0.xyzx
mov o1.w, l(1.000000)
ret 
// Approximately 90 instruction slots used

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