diff --git a/Content/Shaders/BitonicSort.flax b/Content/Shaders/BitonicSort.flax index 1d5b8a581..c9dd81dc9 100644 --- a/Content/Shaders/BitonicSort.flax +++ b/Content/Shaders/BitonicSort.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f46a61cf8d5183230176e661a51208bfeece16cc7238655f406288ff448af64e -size 6721 +oid sha256:0c780dc1881ef96dece237bde3e87fca3c9e4e542d89ebde7c9308f00f81c6a9 +size 6808 diff --git a/Source/Engine/Renderer/Utils/BitonicSort.cpp b/Source/Engine/Renderer/Utils/BitonicSort.cpp index 89313a95d..dc67e5326 100644 --- a/Source/Engine/Renderer/Utils/BitonicSort.cpp +++ b/Source/Engine/Renderer/Utils/BitonicSort.cpp @@ -6,10 +6,22 @@ #define INDIRECT_ARGS_STRIDE 12 -BitonicSort::BitonicSort() - : _dispatchArgsBuffer(nullptr) +// The sorting keys buffer item structure template. Matches the shader type. +struct Item { -} + float Key; + uint32 Value; +}; + +PACK_STRUCT(struct Data { + Item NullItem; + uint32 CounterOffset; + uint32 MaxIterations; + uint32 LoopK; + float KeySign; + uint32 LoopJ; + float Dummy0; + }); String BitonicSort::ToString() const { diff --git a/Source/Engine/Renderer/Utils/BitonicSort.h b/Source/Engine/Renderer/Utils/BitonicSort.h index 053cee6ba..caef88078 100644 --- a/Source/Engine/Renderer/Utils/BitonicSort.h +++ b/Source/Engine/Renderer/Utils/BitonicSort.h @@ -7,36 +7,15 @@ /// /// Bitonic Sort implementation using GPU compute shaders. -/// It has a complexity of O(n*(log n)^2), which is inferior to most -/// traditional sorting algorithms, but because GPUs have so many threads, -/// and because each thread can be utilized, the algorithm can fully load -/// the GPU, taking advantage of its high ALU and bandwidth capabilities. +/// It has a complexity of O(n*(log n)^2), which is inferior to most traditional sorting algorithms, but because GPUs have so many threads, +/// and because each thread can be utilized, the algorithm can fully load the GPU, taking advantage of its high ALU and bandwidth capabilities. /// class BitonicSort : public RendererPass { -public: - - // The sorting keys buffer item structure template. Matches the shader type. - struct Item - { - float Key; - uint32 Value; - }; - private: - PACK_STRUCT(struct Data { - Item NullItem; - uint32 CounterOffset; - uint32 MaxIterations; - uint32 LoopK; - float KeySign; - uint32 LoopJ; - float Dummy0; - }); - AssetReference _shader; - GPUBuffer* _dispatchArgsBuffer; + GPUBuffer* _dispatchArgsBuffer = nullptr; GPUConstantBuffer* _cb; GPUShaderProgramCS* _indirectArgsCS; GPUShaderProgramCS* _preSortCS; @@ -44,13 +23,6 @@ private: GPUShaderProgramCS* _outerSortCS; GPUShaderProgramCS* _copyIndicesCS; -public: - - /// - /// Initializes a new instance of the class. - /// - BitonicSort(); - public: /// diff --git a/Source/Shaders/BitonicSort.shader b/Source/Shaders/BitonicSort.shader index 8a53802d0..8d2172b77 100644 --- a/Source/Shaders/BitonicSort.shader +++ b/Source/Shaders/BitonicSort.shader @@ -10,7 +10,8 @@ struct Item }; META_CB_BEGIN(0, Data) -Item NullItem; +float NullItemKey; +uint NullItemValue; uint CounterOffset; uint MaxIterations; uint LoopK; @@ -95,10 +96,17 @@ groupshared Item SortData[2048]; void LoadItem(uint element, uint count) { // Unused elements must sort to the end + Item item; if (element < count) - SortData[element & 2047] = SortBuffer[element]; + { + item = SortBuffer[element]; + } else - SortData[element & 2047] = NullItem; + { + item.Key = NullItemKey; + item.Value = NullItemValue; + } + SortData[element & 2047] = item; } void StoreItem(uint element, uint count)