BloomExtractAndDownsampleHdrCS.hlsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: sampleLevel
  3. // CHECK: FMax
  4. // CHECK: Saturate
  5. //
  6. // Copyright (c) Microsoft. All rights reserved.
  7. // This code is licensed under the MIT License (MIT).
  8. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  9. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  10. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  11. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  12. //
  13. // Developed by Minigraph
  14. //
  15. // Author: James Stanard
  16. //
  17. // The CS for extracting bright pixels and downsampling them to an unblurred bloom buffer.
  18. #include "ShaderUtility.hlsli"
  19. #include "PostEffectsRS.hlsli"
  20. SamplerState BiLinearClamp : register( s0 );
  21. Texture2D<float3> SourceTex : register( t0 );
  22. StructuredBuffer<float> Exposure : register( t1 );
  23. RWTexture2D<float3> BloomResult : register( u0 );
  24. RWTexture2D<uint> LumaResult : register( u1 );
  25. cbuffer cb0
  26. {
  27. float2 g_inverseOutputSize;
  28. float g_bloomThreshold;
  29. }
  30. [RootSignature(PostEffects_RootSig)]
  31. [numthreads( 8, 8, 1 )]
  32. void main( uint3 DTid : SV_DispatchThreadID )
  33. {
  34. // We need the scale factor and the size of one pixel so that our four samples are right in the middle
  35. // of the quadrant they are covering.
  36. float2 uv = (DTid.xy + 0.5) * g_inverseOutputSize;
  37. float2 offset = g_inverseOutputSize * 0.25;
  38. // Use 4 bilinear samples to guarantee we don't undersample when downsizing by more than 2x
  39. float3 color1 = SourceTex.SampleLevel( BiLinearClamp, uv + float2(-offset.x, -offset.y), 0 );
  40. float3 color2 = SourceTex.SampleLevel( BiLinearClamp, uv + float2( offset.x, -offset.y), 0 );
  41. float3 color3 = SourceTex.SampleLevel( BiLinearClamp, uv + float2(-offset.x, offset.y), 0 );
  42. float3 color4 = SourceTex.SampleLevel( BiLinearClamp, uv + float2( offset.x, offset.y), 0 );
  43. float luma1 = RGBToLuminance(color1);
  44. float luma2 = RGBToLuminance(color2);
  45. float luma3 = RGBToLuminance(color3);
  46. float luma4 = RGBToLuminance(color4);
  47. const float kSmallEpsilon = 0.0001;
  48. float ScaledThreshold = g_bloomThreshold * Exposure[1]; // BloomThreshold / Exposure
  49. // We perform a brightness filter pass, where lone bright pixels will contribute less.
  50. color1 *= max(kSmallEpsilon, luma1 - ScaledThreshold) / (luma1 + kSmallEpsilon);
  51. color2 *= max(kSmallEpsilon, luma2 - ScaledThreshold) / (luma2 + kSmallEpsilon);
  52. color3 *= max(kSmallEpsilon, luma3 - ScaledThreshold) / (luma3 + kSmallEpsilon);
  53. color4 *= max(kSmallEpsilon, luma4 - ScaledThreshold) / (luma4 + kSmallEpsilon);
  54. // The shimmer filter helps remove stray bright pixels from the bloom buffer by inversely weighting
  55. // them by their luminance. The overall effect is to shrink bright pixel regions around the border.
  56. // Lone pixels are likely to dissolve completely. This effect can be tuned by adjusting the shimmer
  57. // filter inverse strength. The bigger it is, the less a pixel's luminance will matter.
  58. const float kShimmerFilterInverseStrength = 1.0f;
  59. float weight1 = 1.0f / (luma1 + kShimmerFilterInverseStrength);
  60. float weight2 = 1.0f / (luma2 + kShimmerFilterInverseStrength);
  61. float weight3 = 1.0f / (luma3 + kShimmerFilterInverseStrength);
  62. float weight4 = 1.0f / (luma4 + kShimmerFilterInverseStrength);
  63. float weightSum = weight1 + weight2 + weight3 + weight4;
  64. BloomResult[DTid.xy] = (color1 * weight1 + color2 * weight2 + color3 * weight3 + color4 * weight4) / weightSum;
  65. float luma = (luma1 + luma2 + luma3 + luma4) * 0.25;
  66. // Prevent divide by 0 and put only pure black pixels in Histogram[0]
  67. if (luma == 0.0)
  68. {
  69. LumaResult[DTid.xy] = 0;
  70. }
  71. else
  72. {
  73. const float MinLog = Exposure[4];
  74. const float RcpLogRange = Exposure[7];
  75. float logLuma = saturate((log2(luma) - MinLog) * RcpLogRange); // Rescale to [0.0, 1.0]
  76. LumaResult[DTid.xy] = logLuma * 254.0 + 1.0; // Rescale to [1, 255]
  77. }
  78. }