2
0

AsyncComputeLuminanceReduce.azsl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/Features/SrgSemantics.azsli>
  9. ShaderResourceGroup TexturesSrg : SRG_PerObject
  10. {
  11. RWTexture2D<float> m_outputTexture;
  12. Texture2D<float> m_inputTexture;
  13. };
  14. #define ThreadGroupSize 8
  15. const static uint NumThreads = ThreadGroupSize * ThreadGroupSize;
  16. groupshared static float4 s_threadGroupSharedMem[NumThreads];
  17. [numthreads(ThreadGroupSize, ThreadGroupSize, 1)]
  18. void MainCS(uint3 groupID : SV_GroupID, uint3 groupThreadID : SV_GroupThreadID, uint groupIndex : SV_GroupIndex)
  19. {
  20. const uint threadIdx = groupIndex;
  21. const uint2 sampleIdx = (groupID.xy * ThreadGroupSize + groupThreadID.xy) * 2;
  22. float4 sampleValue = 0.0f;
  23. sampleValue.x = TexturesSrg::m_inputTexture[sampleIdx + uint2(0, 0)];
  24. sampleValue.y = TexturesSrg::m_inputTexture[sampleIdx + uint2(1, 0)];
  25. sampleValue.z = TexturesSrg::m_inputTexture[sampleIdx + uint2(0, 1)];
  26. sampleValue.w = TexturesSrg::m_inputTexture[sampleIdx + uint2(1, 1)];
  27. s_threadGroupSharedMem[threadIdx] = sampleValue;
  28. GroupMemoryBarrierWithGroupSync();
  29. // Parallel reduce
  30. for(uint i = NumThreads/2; i > 0; i /= 2)
  31. {
  32. if(threadIdx < i)
  33. {
  34. s_threadGroupSharedMem[threadIdx] += s_threadGroupSharedMem[threadIdx + i];
  35. }
  36. GroupMemoryBarrierWithGroupSync();
  37. }
  38. if(threadIdx == 0)
  39. {
  40. const float4 accum = s_threadGroupSharedMem[0];
  41. const float totalSamples = NumThreads * 4;
  42. TexturesSrg::m_outputTexture[groupID.xy] = (accum.x + accum.y + accum.z + accum.w) / totalSamples;
  43. }
  44. }