PPEyeAdaptHistogram.bsl 3.1 KB

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