/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Data/Samples/ComputeShaderHistogram/Shaders/ComputeHistogram.ezShader
71 строка
2 KB
C-Core
Couple of follow up fixes after the bind group changes (#1607)
12 июл 2025, 18:29
Не верифицирован
12 июл 2025, 18:29
72585a0
Код
Авторство
О чём код?
[PLATFORMS] ALL [PERMUTATIONS] [COMPUTESHADER] #include <Shaders/Common/GlobalConstants.h> RWTexture2D<uint> HistogramOutput; Texture2D<float3> ScreenTexture; SamplerState PointSampler; // Turns out the "BRUTEFORCE" method is MUCH faster then my optimization attempt. #define BRUTEFORCE #define NUM_BINS 256 #define NUM_THREADS 32 groupshared uint GroupHistogram[3 * NUM_BINS]; [numthreads(NUM_THREADS, NUM_THREADS, 1)] void main(uint3 PixCoord : SV_DispatchThreadID, uint GroupIndex : SV_GroupIndex) { float2 pixCoordF = PixCoord.xy / ViewportSize.xy; float3 screenColor = ScreenTexture.SampleLevel(PointSampler, pixCoordF, 0.0f); uint3 screenColorDiscrete = uint3(screenColor * (NUM_BINS-1) + 0.5f); bool isOutsideOfScreen = PixCoord.x >= uint(ViewportSize.x) || PixCoord.y >= uint(ViewportSize.y); #ifdef BRUTEFORCE // Kill threads that are outside of the screen. if (isOutsideOfScreen) return; int previousValue; [unroll] for(int i=0; i<3; ++i) InterlockedAdd(HistogramOutput[int2(screenColorDiscrete[i], i)], 1, previousValue); #else // A thread can have "ownership" over a histogram cell. bool hasHistOwnership = (GroupIndex < NUM_BINS * 3); // Clear group shared memory. if(hasHistOwnership) GroupHistogram[GroupIndex] = 0; GroupMemoryBarrierWithGroupSync(); // Write values. int previousValue; if (!isOutsideOfScreen) { [unroll] for(int i=0; i<3; ++i) InterlockedAdd(GroupHistogram[i * NUM_BINS + screenColorDiscrete[i]], 1, previousValue); } GroupMemoryBarrierWithGroupSync(); // Now only write those values to global mem that need writing. [branch] if(hasHistOwnership) { uint value = GroupHistogram[GroupIndex]; [branch] if(value > 0) { uint2 histCoord = uint2(GroupIndex % NUM_BINS, GroupIndex / NUM_BINS); InterlockedAdd(HistogramOutput[histCoord], value, previousValue); } } #endif }