PPEyeAdaptHistogram.bsl 3.3 KB

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