// Bloom around lights - move to infinity
// ---- Created with 3Dmigoto v1.2.31 on Wed Mar 02 09:01:38 2016

cbuffer CViewportShaderParameterProvider : register(b0)
{
  float4x4 InvProjectionMatrix : packoffset(c0);
  float4x4 InvViewMatrix : packoffset(c4);
  float4x4 ProjectionMatrix : packoffset(c8);
  float4x4 ViewMatrix : packoffset(c12);
  float4x4 ViewProjectionMatrix : packoffset(c16);
  float4x4 ViewRotProjectionMatrix : packoffset(c20);
  float4x4 ViewRotProjectionMatrix_Previous : packoffset(c24);
  float4 AmbientSHR : packoffset(c28);
  float4 AmbientSHG : packoffset(c29);
  float4 AmbientSHB : packoffset(c30);
  float4 CameraDistances : packoffset(c31);
  float4 CameraNearPlaneSize : packoffset(c32);
  float4 DepthScale : packoffset(c33);
  float4 DepthTextureScaleOffset : packoffset(c34);
  float4 FogParams : packoffset(c35);
  float4 FogSHB : packoffset(c36);
  float4 FogSHG : packoffset(c37);
  float4 FogSHR : packoffset(c38);
  float4 HeightFogParams : packoffset(c39);
  float4 ViewportSize : packoffset(c40);
  float3 ViewPoint : packoffset(c41);
  float3 CameraPosition : packoffset(c42);
  float3 CameraPosition_Previous : packoffset(c43);
  float3 CameraPositionFractions : packoffset(c44);
  float3 CameraPositionFractions_Previous : packoffset(c45);
  float3 CameraRight : packoffset(c46);
  float3 CameraUp : packoffset(c47);
  float3 CameraDirection : packoffset(c48);
  float3 EnvironmentMapColorOffset : packoffset(c49);
  float3 EnvironmentMapColorScale : packoffset(c50);
  float3 AmbientOcclusionRanges : packoffset(c51);
  float3 DepthTextureRcpSize : packoffset(c52);
  float2 MotionVectorOffsets : packoffset(c53);
  float2 DepthRangeCompression : packoffset(c53.z);
  float ShadowProjDepthMinValue : packoffset(c54);
  float DistanceScale : packoffset(c54.y);
  float WorldSpaceZOffset : packoffset(c54.z);
  float WorldSpaceZOffset_Previous : packoffset(c54.w);
  float CameraFOVDeg : packoffset(c55);
  float EffectsEmissiveEVBias : packoffset(c55.y);
  float ShadowCSMLastSliceIndex : packoffset(c55.z);
  float UseNormalization : packoffset(c55.w);
  float FogUndergroundColorScale : packoffset(c56);
}

cbuffer CSceneTerrainSkyOcclusionAtlasPrivateData : register(b1)
{
  float4 SectorOcclusion_OffsetScale : packoffset(c0);
  float3 SectorOcclusion_DistanceFade : packoffset(c1);
  float2 SectorOcclusion_HeightOffsetScale : packoffset(c2);
}

cbuffer CAmbientProvider : register(b2)
{
  float4 CascadeDistanceScales[5] : packoffset(c0);
  float4 CascadeNearFarSnappedOffsetCamera[5] : packoffset(c5);
  float4 CascadeNearFarRcpUVSize[5] : packoffset(c10);
  float4 UndergroundSHR : packoffset(c15);
  float4 UndergroundSHG : packoffset(c16);
  float4 UndergroundSHB : packoffset(c17);
  float InvUndergroundBlendEndHeight : packoffset(c18);
  float IndirectLightingScale : packoffset(c18.y);
}

cbuffer CLightEffectParameterProvider : register(b3)
{
  float4 ScreenParams : packoffset(c0);
  float4 VisibilityParams : packoffset(c1);
  float EmissiveEV : packoffset(c2);
}

SamplerState SectorHemiMapSamplerStateLinear_s : register(s0);
Texture2D<float4> SectorOcclusion_HeightTexture__TexObj__ : register(t0);


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


void main( 
  float4 v0 : position0,
  float4 v1 : color0,
  float2 v2 : texcoord0,
  out float4 o0 : SV_Position0,
  out float4 o1 : TEXCOORD0,
  out float4 o2 : TEXCOORD1,
  out float4 o3 : TEXCOORD2,
  out float2 o4 : TEXCOORD3)
{
  float4 r0,r1,r2,r3;
  uint4 bitmask, uiDest;
  float4 fDest;

  float4 stereo = StereoParams.Load(0);

// This shader draws bloom around several lights simultaneously, so the depth
// cannot be in a constant buffer. Frame analysis shows that it is not in the
// vertex buffers (z is always 1.0), and the only other possibility is a shader
// resource slot, but there is nothing suitable.
// Move bloom to infinity:
v0.x += stereo.x;

  o1.xyz = EmissiveEV * v1.xyz;
  r0.xyz = v0.xyz;
  r0.w = 1;
  r1.x = dot(r0.xyzw, InvProjectionMatrix._m00_m10_m20_m30);
  r1.y = dot(r0.xyzw, InvProjectionMatrix._m01_m11_m21_m31);
  r1.z = dot(r0.xyzw, InvProjectionMatrix._m02_m12_m22_m32);
  r1.w = dot(r0.xyzw, InvProjectionMatrix._m03_m13_m23_m33);
  r0.x = dot(r1.xyzw, InvViewMatrix._m00_m10_m20_m30);
  r0.y = dot(r1.xyzw, InvViewMatrix._m01_m11_m21_m31);
  r0.z = dot(r1.xyzw, InvViewMatrix._m02_m12_m22_m32);
  r0.w = dot(r1.xyzw, InvViewMatrix._m03_m13_m23_m33);
  r0.xyz = r0.xyz / r0.www;
  r1.xyz = CameraPosition.xyz + -r0.xyz;
  r0.w = dot(r1.xyz, r1.xyz);
  r0.w = sqrt(r0.w);
  r1.xyz = r1.xyz / r0.www;
  r0.w = -2000 + r0.w;
  r0.w = saturate(0.00033333333 * r0.w);
  r0.w = 250 * r0.w;
  r0.xyz = r1.xyz * r0.www + r0.xyz;
  r1.xyz = -CameraPosition.xyz + r0.xyz;
  r0.w = dot(r1.xyz, r1.xyz);
  r0.w = rsqrt(r0.w);
  r1.xyz = r1.xyz * r0.www;
  r0.xyz = r1.xyz * WorldSpaceZOffset + r0.xyz;
  r1.xy = cmp(float2(0,0) < HeightFogParams.zw);
  r0.w = r1.y ? r1.x : 0;
  if (r0.w != 0) {
    o2.xyz = float3(0,0,0);
  } else {
    r1.xyz = -CameraPosition.yzx + r0.yzx;
    r0.w = dot(r1.xyz, r1.xyz);
    r0.w = rsqrt(r0.w);
    r1.yzw = r1.xyz * r0.www;
    r1.x = 1;
    r2.x = dot(FogSHR.xyzw, r1.xyzw);
    r2.y = dot(FogSHG.xyzw, r1.xyzw);
    r2.z = dot(FogSHB.xyzw, r1.xyzw);
    r2.xyz = max(float3(0,0,0), r2.xyz);
    r3.xy = r0.yx * SectorOcclusion_OffsetScale.yx + SectorOcclusion_OffsetScale.wz;
    r0.w = SectorOcclusion_HeightTexture__TexObj__.SampleLevel(SectorHemiMapSamplerStateLinear_s, r3.xy, 0).y;
    r0.w = r0.w * SectorOcclusion_HeightOffsetScale.x + SectorOcclusion_HeightOffsetScale.y;
    r0.w = r0.z + -r0.w;
    r0.w = saturate(InvUndergroundBlendEndHeight * -r0.w);
    r0.w = sqrt(r0.w);
    r3.xy = -CameraPosition.xy + r0.xy;
    r2.w = dot(r3.xy, r3.xy);
    r2.w = SectorOcclusion_DistanceFade.x * r2.w + SectorOcclusion_DistanceFade.y;
    r2.w = saturate(3 * r2.w);
    r0.w = r2.w * r0.w;
    r3.x = dot(UndergroundSHR.xyzw, r1.xyzw);
    r3.y = dot(UndergroundSHG.xyzw, r1.xyzw);
    r3.z = dot(UndergroundSHB.xyzw, r1.xyzw);
    r1.xyz = max(float3(0,0,0), r3.xyz);
    r1.xyz = r1.xyz * FogUndergroundColorScale + -r2.xyz;
    o2.xyz = r0.www * r1.xyz + r2.xyz;
  }
  o0.xyzw = v0.xyzw;
  o1.w = v1.w;
  o3.xyz = r0.xyz;
  o4.xy = v2.xy;
  return;
}
