PPEyeAdaptHistogram.bsl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. Parameters =
  2. {
  3. Texture2D gSceneColorTex;
  4. RWTexture2D gOutputTex;
  5. };
  6. Blocks =
  7. {
  8. Block Input;
  9. };
  10. #define NUM_BUCKETS (THREADGROUP_SIZE_X * THREADGROUP_SIZE_Y)
  11. Technique =
  12. {
  13. Pass =
  14. {
  15. Compute =
  16. {
  17. cbuffer Input
  18. {
  19. // xy - offset, zw - size
  20. uint4 gPixelOffsetAndSize;
  21. // x - histogram scale, y - histogram offset
  22. float2 gHistogramParams;
  23. uint2 gThreadGroupCount;
  24. }
  25. Texture2D gSceneColorTex;
  26. RWTexture2D<float4> gOutputTex;
  27. // Keep elements in this order as it ensures coalesced memory operations for non-random ops
  28. groupshared float sharedData[NUM_BUCKETS][THREADGROUP_SIZE_X][THREADGROUP_SIZE_Y];
  29. float calcHistogramPos(float luminance)
  30. {
  31. return saturate(log2(luminance) * gHistogramParams.x + gHistogramParams.y);
  32. }
  33. [numthreads(THREADGROUP_SIZE_X, THREADGROUP_SIZE_Y, 1)]
  34. void main(
  35. uint3 groupId : SV_GroupID,
  36. uint3 groupThreadId : SV_GroupThreadID,
  37. uint3 dispatchThreadId : SV_DispatchThreadID,
  38. uint threadIndex : SV_GroupIndex)
  39. {
  40. // Clear everything
  41. for(uint i = 0; i < NUM_BUCKETS; i++)
  42. sharedData[i][groupThreadId.x][groupThreadId.y] = 0.0f;
  43. GroupMemoryBarrierWithGroupSync();
  44. // Sort all pixel luminance for the current thread into histogram buckets
  45. uint2 tileSize = uint2(LOOP_COUNT_X, LOOP_COUNT_Y);
  46. uint2 maxExtent = gPixelOffsetAndSize.xy + gPixelOffsetAndSize.zw;
  47. uint2 tileStart = dispatchThreadId.xy * tileSize + gPixelOffsetAndSize.xy;
  48. for(uint y = 0; y < LOOP_COUNT_Y; y++)
  49. {
  50. uint2 texelPos = tileStart + uint2(0, y);
  51. if(texelPos.y > maxExtent.y)
  52. break;
  53. for(uint x = 0; x < LOOP_COUNT_X; x++)
  54. {
  55. if(texelPos.x > maxExtent.x)
  56. break;
  57. float4 hdrColor = gSceneColorTex.Load(int3(texelPos, 0));
  58. float luminance = dot(hdrColor.rgb, float3(0.299f, 0.587f, 0.114f)); // TODO - Perhaps just use max() of all values?
  59. float histogramPos = calcHistogramPos(luminance);
  60. float bucket = histogramPos * (NUM_BUCKETS - 1) * 0.9999f;
  61. uint bucketAIdx = (uint)bucket;
  62. uint bucketBIdx = bucketAIdx + 1;
  63. float weightB = frac(bucket);
  64. float weightA = 1.0f - weightB;
  65. if(bucketAIdx != 0)
  66. sharedData[bucketAIdx][groupThreadId.x][groupThreadId.y] += weightA;
  67. sharedData[bucketBIdx][groupThreadId.x][groupThreadId.y] += weightB;
  68. texelPos.x++;
  69. }
  70. }
  71. GroupMemoryBarrierWithGroupSync();
  72. // Accumulate bucketed values from all threads in the group
  73. if(threadIndex < (NUM_BUCKETS / 4))
  74. {
  75. float4 sum = 0.0f;
  76. for(uint y = 0; y < THREADGROUP_SIZE_Y; y++)
  77. {
  78. for(uint x = 0; x < THREADGROUP_SIZE_X; x++)
  79. {
  80. sum += float4(
  81. sharedData[threadIndex * 4 + 0][x][y],
  82. sharedData[threadIndex * 4 + 1][x][y],
  83. sharedData[threadIndex * 4 + 2][x][y],
  84. sharedData[threadIndex * 4 + 3][x][y]
  85. );
  86. }
  87. }
  88. // Normalize and output histogram for the group (single line per group)
  89. float groupArea = THREADGROUP_SIZE_X * LOOP_COUNT_X * THREADGROUP_SIZE_Y * LOOP_COUNT_Y;
  90. gOutputTex[uint2(threadIndex, groupId.x + groupId.y * gThreadGroupCount.x)] = sum / groupArea;
  91. }
  92. }
  93. };
  94. };
  95. };