DebugDrawHistogramCS.hlsl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: flattenedThreadIdInGroup
  3. // CHECK: threadId
  4. // CHECK: bufferLoad
  5. // CHECK: barrier
  6. // CHECK: UMax
  7. // CHECK: getDimensions
  8. // CHECK: textureStore
  9. //
  10. // Copyright (c) Microsoft. All rights reserved.
  11. // This code is licensed under the MIT License (MIT).
  12. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  13. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  14. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  15. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  16. //
  17. // Developed by Minigraph
  18. //
  19. // Author: James Stanard
  20. //
  21. #include "PostEffectsRS.hlsli"
  22. ByteAddressBuffer Histogram : register( t0 );
  23. StructuredBuffer<float> Exposure : register( t1 );
  24. RWTexture2D<float3> ColorBuffer : register( u0 );
  25. groupshared uint gs_hist[256];
  26. [RootSignature(PostEffects_RootSig)]
  27. [numthreads( 256, 1, 1 )]
  28. void main( uint GI : SV_GroupIndex, uint3 DTid : SV_DispatchThreadID )
  29. {
  30. uint histValue = Histogram.Load(GI * 4);
  31. // Compute the maximum histogram value, but don't include the black pixel
  32. gs_hist[GI] = GI == 0 ? 0 : histValue;
  33. GroupMemoryBarrierWithGroupSync();
  34. gs_hist[GI] = max(gs_hist[GI], gs_hist[(GI + 128) % 256]);
  35. GroupMemoryBarrierWithGroupSync();
  36. gs_hist[GI] = max(gs_hist[GI], gs_hist[(GI + 64) % 256]);
  37. GroupMemoryBarrierWithGroupSync();
  38. gs_hist[GI] = max(gs_hist[GI], gs_hist[(GI + 32) % 256]);
  39. GroupMemoryBarrierWithGroupSync();
  40. gs_hist[GI] = max(gs_hist[GI], gs_hist[(GI + 16) % 256]);
  41. GroupMemoryBarrierWithGroupSync();
  42. gs_hist[GI] = max(gs_hist[GI], gs_hist[(GI + 8) % 256]);
  43. GroupMemoryBarrierWithGroupSync();
  44. gs_hist[GI] = max(gs_hist[GI], gs_hist[(GI + 4) % 256]);
  45. GroupMemoryBarrierWithGroupSync();
  46. gs_hist[GI] = max(gs_hist[GI], gs_hist[(GI + 2) % 256]);
  47. GroupMemoryBarrierWithGroupSync();
  48. gs_hist[GI] = max(gs_hist[GI], gs_hist[(GI + 1) % 256]);
  49. GroupMemoryBarrierWithGroupSync();
  50. uint maxHistValue = gs_hist[GI];
  51. uint2 BufferDim;
  52. ColorBuffer.GetDimensions(BufferDim.x, BufferDim.y);
  53. const uint2 RectCorner = uint2(BufferDim.x / 2 - 512, BufferDim.y - 256);
  54. const uint2 GroupCorner = RectCorner + DTid.xy * 4;
  55. uint height = 127 - DTid.y * 4;
  56. uint threshold = histValue * 128 / max(1, maxHistValue);
  57. float3 OutColor = (GI == (uint)Exposure[3]) ? float3(1.0, 1.0, 0.0) : float3(0.5, 0.5, 0.5);
  58. for (uint i = 0; i < 4; ++i)
  59. {
  60. float3 MaskedColor = (height - i) < threshold ? OutColor : float3(0, 0, 0);
  61. // 4-wide column with 2 pixels for the histogram bar and 2 for black spacing
  62. ColorBuffer[GroupCorner + uint2(0, i)] = MaskedColor;
  63. ColorBuffer[GroupCorner + uint2(1, i)] = MaskedColor;
  64. ColorBuffer[GroupCorner + uint2(2, i)] = float3(0, 0, 0);
  65. ColorBuffer[GroupCorner + uint2(3, i)] = float3(0, 0, 0);
  66. }
  67. }