WorkDrainCompute.hlsl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #define TEX_SIZE_X 4096u
  6. #define TEX_SIZE_Y 4096u
  7. #define TILE_SIZE_X 32u
  8. #define TILE_SIZE_Y 32u
  9. #define TILE_COUNT_X (TEX_SIZE_X / TILE_SIZE_X)
  10. #define TILE_COUNT_Y (TEX_SIZE_Y / TILE_SIZE_Y)
  11. #define TILE_COUNT (TILE_COUNT_X * TILE_COUNT_Y)
  12. Texture2D g_inputTex : register(t0);
  13. RWStructuredBuffer<float4> g_tileMaxColors : register(u0);
  14. RWStructuredBuffer<float4> g_result : register(u1);
  15. #if defined(FIRST)
  16. groupshared float4 g_tileMax[TILE_SIZE_X * TILE_SIZE_Y];
  17. [numthreads(TILE_SIZE_X, TILE_SIZE_Y, 1)] void main(uint2 svDispatchThreadId : SV_DISPATCHTHREADID, uint svGroupIndex : SV_GROUPINDEX,
  18. uint2 svGroupId : SV_GROUPID)
  19. {
  20. g_tileMax[svGroupIndex] = g_inputTex[svDispatchThreadId];
  21. GroupMemoryBarrierWithGroupSync();
  22. [loop] for(uint s = TILE_SIZE_X * TILE_SIZE_Y / 2u; s > 0u; s >>= 1u)
  23. {
  24. if(svGroupIndex < s)
  25. {
  26. g_tileMax[svGroupIndex] = max(g_tileMax[svGroupIndex], g_tileMax[svGroupIndex + s]);
  27. }
  28. GroupMemoryBarrierWithGroupSync();
  29. }
  30. const uint tileIdx = svGroupId.y * TILE_COUNT_X + svGroupId.x;
  31. g_tileMaxColors[tileIdx] = g_tileMax[0];
  32. }
  33. #else
  34. groupshared float4 g_maxColor[64];
  35. [numthreads(64, 1, 1)] void main(uint svGroupIndex : SV_GROUPINDEX)
  36. {
  37. const uint tilesPerThread = TILE_COUNT / 64;
  38. const uint start = svGroupIndex * tilesPerThread;
  39. const uint end = start + tilesPerThread;
  40. float4 localMax = 0.0;
  41. for(uint tileIdx = start; tileIdx < end; ++tileIdx)
  42. {
  43. localMax = max(localMax, g_tileMaxColors[tileIdx]);
  44. }
  45. g_maxColor[svGroupIndex] = localMax;
  46. GroupMemoryBarrierWithGroupSync();
  47. [loop] for(uint s = 64 / 2u; s > 0u; s >>= 1u)
  48. {
  49. if(svGroupIndex < s)
  50. {
  51. g_maxColor[svGroupIndex] = max(g_maxColor[svGroupIndex], g_maxColor[svGroupIndex + s]);
  52. }
  53. GroupMemoryBarrierWithGroupSync();
  54. }
  55. g_result[0] = g_maxColor[0];
  56. }
  57. #endif