-
Notifications
You must be signed in to change notification settings - Fork 67
Bitonic_Sort #943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CrabExtra
wants to merge
39
commits into
Devsh-Graphics-Programming:master
Choose a base branch
from
CrabExtra:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Bitonic_Sort #943
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
3a161ab
created a structured like fft
CrabExtra 7b4d222
config question
CrabExtra 432ae81
subgroupsort
CrabExtra 20cac8c
added bitonic_sort name space
CrabExtra 9afdf98
subgroup changes
CrabExtra 68b8c39
removed unused
CrabExtra 2f79d74
added last merge step as a function
CrabExtra b3ccdea
uncomplete workgroup fn
CrabExtra 42072f6
complete the logic for some pr questions
CrabExtra f352c49
Refactor bitonic sort for workgroup + Accessor support
CrabExtra e27c9ef
Update bitonic_sort.hlsl
CrabExtra cdb804c
VT implumentation
CrabExtra 729b9c7
Update bitonic_sort.hlsl
CrabExtra b6a8cbb
Update common.hlsl
CrabExtra 1636680
Update bitonic_sort.hlsl
CrabExtra 69e178a
Update common.hlsl
CrabExtra 8596ad6
Update bitonic_sort.hlsl
CrabExtra b9884a3
Update CMakeLists.txt
CrabExtra 6fa52bc
pair added
CrabExtra 8429b4b
comment outdated pair impl
CrabExtra 0f8c062
bitonic sort acessor added
CrabExtra c750811
Update common.hlsl
CrabExtra 17eebef
Update bitonic_sort.hlsl
CrabExtra b2f7d3a
Update bitonic_sort.hlsl
CrabExtra 57e5460
Update utility.hlsl
CrabExtra d14acf2
Delete include/nbl/builtin/hlsl/utility.hlsl
CrabExtra e6a51c2
Delete include/nbl/builtin/hlsl/pair.hlsl
CrabExtra a590338
Add files via upload
CrabExtra 7ca09ae
Update CMakeLists.txt
CrabExtra ef7edd5
Remove unused pair struct from memory_accessor.hlsl
CrabExtra d80f449
Merge remote-tracking branch 'upstream/master'
CrabExtra fa78d2b
refactor common.hlsl and prep for new implumentation
CrabExtra 9d56528
Merge branch 'Devsh-Graphics-Programming:master' into master
CrabExtra 6f2ee43
update common.hlsl
CrabExtra 2d6bf0b
Refactor LocalPasses template for flexibility
CrabExtra d46a866
Merge branch 'Devsh-Graphics-Programming:master' into master
CrabExtra 7ac604c
Refactor and optimize bitonic sort implementation
CrabExtra 589056f
Merge branch 'master' of https://github.com/CrabExtra/Nabla
CrabExtra a5e811b
some changes for debugging
CrabExtra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #ifndef _NBL_BUILTIN_HLSL_BITONIC_SORT_COMMON_INCLUDED_ | ||
| #define _NBL_BUILTIN_HLSL_BITONIC_SORT_COMMON_INCLUDED_ | ||
|
|
||
| #include "nbl/builtin/hlsl/cpp_compat.hlsl" | ||
| #include "nbl/builtin/hlsl/functional.hlsl" | ||
| #include "nbl/builtin/hlsl/subgroup/basic.hlsl" | ||
| #include "nbl/builtin/hlsl/glsl_compat/subgroup_shuffle.hlsl" | ||
|
|
||
| namespace nbl | ||
| { | ||
| namespace hlsl | ||
| { | ||
| namespace bitonic_sort | ||
| { | ||
|
|
||
| template<typename KeyType, typename ValueType, uint32_t SubgroupSizelog2, typename Comparator> | ||
| struct bitonic_sort_config | ||
| { | ||
| using key_t = KeyType; | ||
| using value_t = ValueType; | ||
| using comparator_t = Comparator; | ||
| static const uint32_t SubgroupSizeLog2 = SubgroupSizelog2; | ||
| static const uint32_t SubgroupSize = 1u << SubgroupSizeLog2; | ||
| }; | ||
|
|
||
| template<typename Config, class device_capabilities = void> | ||
| struct bitonic_sort; | ||
|
|
||
|
|
||
| template<typename sortable_t, uint32_t Log2N, typename Comparator> | ||
| struct LocalPasses | ||
| { | ||
| static const uint32_t N = 1u << Log2N; | ||
| void operator()(bool ascending, sortable_t data[N], NBL_CONST_REF_ARG(Comparator) comp); | ||
| }; | ||
|
|
||
| // Specialization for 2 elements (Log2N=1) | ||
| template<typename sortable_t, typename Comparator> | ||
| struct LocalPasses<sortable_t, 1, Comparator> | ||
| { | ||
| static const uint32_t N = 2; | ||
|
|
||
| void operator()(bool ascending, sortable_t data[N], NBL_CONST_REF_ARG(Comparator) comp) | ||
| { | ||
| // For ascending: swap if data[1] < data[0] (put smaller first) | ||
| // For descending: swap if data[0] < data[1] (put larger first) | ||
| const bool needSwap = ascending ? comp(data[1], data[0]) : comp(data[0], data[1]); | ||
|
|
||
| if (needSwap) | ||
| { | ||
| sortable_t temp = data[0]; | ||
| data[0] = data[1]; | ||
| data[1] = temp; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| } // namespace bitonic_sort | ||
| } // namespace hlsl | ||
| } // namespace nbl | ||
|
|
||
| #endif |
31 changes: 31 additions & 0 deletions
31
include/nbl/builtin/hlsl/concepts/accessors/bitonic_sort.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #ifndef _NBL_BUILTIN_HLSL_CONCEPTS_ACCESSORS_BITONIC_SORT_INCLUDED_ | ||
| #define _NBL_BUILTIN_HLSL_CONCEPTS_ACCESSORS_BITONIC_SORT_INCLUDED_ | ||
|
|
||
| #include "nbl/builtin/hlsl/concepts/accessors/generic_shared_data.hlsl" | ||
|
|
||
| namespace nbl | ||
| { | ||
| namespace hlsl | ||
| { | ||
| namespace workgroup | ||
| { | ||
| namespace bitonic_sort | ||
| { | ||
| // The SharedMemoryAccessor MUST provide the following methods: | ||
| // * void get(uint32_t index, NBL_REF_ARG(uint32_t) value); | ||
| // * void set(uint32_t index, in uint32_t value); | ||
| // * void workgroupExecutionAndMemoryBarrier(); | ||
| template<typename T, typename V = uint32_t, typename I = uint32_t> | ||
| NBL_BOOL_CONCEPT BitonicSortSharedMemoryAccessor = concepts::accessors::GenericSharedMemoryAccessor<T, V, I>; | ||
|
|
||
| // The Accessor MUST provide the following methods: | ||
| // * void get(uint32_t index, NBL_REF_ARG(pair<KeyType, ValueType>) value); | ||
| // * void set(uint32_t index, in pair<KeyType, ValueType> value); | ||
| template<typename T, typename KeyType, typename ValueType, typename I = uint32_t> | ||
| NBL_BOOL_CONCEPT BitonicSortAccessor = concepts::accessors::GenericDataAccessor<T, pair<KeyType, ValueType>, I>; | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #ifndef _NBL_BUILTIN_HLSL_SUBGROUP_BITONIC_SORT_INCLUDED_ | ||
| #define _NBL_BUILTIN_HLSL_SUBGROUP_BITONIC_SORT_INCLUDED_ | ||
|
|
||
| #include "nbl/builtin/hlsl/bitonic_sort/common.hlsl" | ||
| #include "nbl/builtin/hlsl/subgroup/basic.hlsl" | ||
| #include "nbl/builtin/hlsl/glsl_compat/subgroup_shuffle.hlsl" | ||
|
|
||
| namespace nbl | ||
| { | ||
| namespace hlsl | ||
| { | ||
| namespace subgroup | ||
| { | ||
| namespace bitonic_sort | ||
| { | ||
| using namespace nbl::hlsl::bitonic_sort; | ||
|
|
||
| template<typename KeyType, typename Comparator, class device_capabilities = void> | ||
| struct bitonic_sort_wgtype | ||
| { | ||
| using WGType = WorkgroupType<KeyType>; | ||
| using key_t = KeyType; | ||
| using comparator_t = Comparator; | ||
|
|
||
| static void mergeStage( | ||
| uint32_t stage, | ||
| bool bitonicAscending, | ||
| uint32_t invocationID, | ||
| NBL_REF_ARG(WGType) lo, | ||
| NBL_REF_ARG(WGType) hi) | ||
| { | ||
| comparator_t comp; | ||
|
|
||
| [unroll] | ||
| for (uint32_t pass = 0u; pass <= stage; ++pass) | ||
| { | ||
| uint32_t stride = 1u << (stage - pass); | ||
| uint32_t partner = stride >> 1; | ||
|
|
||
| if (partner == 0u) | ||
| { | ||
| bool swap = comp(hi.key, lo.key) == bitonicAscending; | ||
| WGType tmp = lo; | ||
| lo.key = swap ? hi.key : lo.key; | ||
| lo.workgroupRelativeIndex = swap ? hi.workgroupRelativeIndex : lo.workgroupRelativeIndex; | ||
| hi.key = swap ? tmp.key : hi.key; | ||
| hi.workgroupRelativeIndex = swap ? tmp.workgroupRelativeIndex : hi.workgroupRelativeIndex; | ||
| } | ||
| else | ||
| { | ||
| bool isUpper = (invocationID & partner) != 0u; | ||
|
|
||
| // Select which element to trade and shuffle members individually | ||
| key_t tradingKey = isUpper ? hi.key : lo.key; | ||
| uint32_t tradingIdx = isUpper ? hi.workgroupRelativeIndex : lo.workgroupRelativeIndex; | ||
|
|
||
| tradingKey = glsl::subgroupShuffleXor(tradingKey, partner); | ||
| tradingIdx = glsl::subgroupShuffleXor(tradingIdx, partner); | ||
|
|
||
| lo.key = isUpper ? lo.key : tradingKey; | ||
| lo.workgroupRelativeIndex = isUpper ? lo.workgroupRelativeIndex : tradingIdx; | ||
| hi.key = isUpper ? tradingKey : hi.key; | ||
| hi.workgroupRelativeIndex = isUpper ? tradingIdx : hi.workgroupRelativeIndex; | ||
|
|
||
| bool swap = comp(hi.key, lo.key) == bitonicAscending; | ||
| WGType tmp = lo; | ||
| lo.key = swap ? hi.key : lo.key; | ||
| lo.workgroupRelativeIndex = swap ? hi.workgroupRelativeIndex : lo.workgroupRelativeIndex; | ||
| hi.key = swap ? tmp.key : hi.key; | ||
| hi.workgroupRelativeIndex = swap ? tmp.workgroupRelativeIndex : hi.workgroupRelativeIndex; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void __call(bool ascending, NBL_REF_ARG(WGType) lo, NBL_REF_ARG(WGType) hi) | ||
| { | ||
| uint32_t id = glsl::gl_SubgroupInvocationID(); | ||
| uint32_t log2 = glsl::gl_SubgroupSizeLog2(); | ||
|
|
||
| [unroll] | ||
| for (uint32_t s = 0u; s <= log2; ++s) | ||
| { | ||
| bool dir = (s == log2) ? ascending : ((id & (1u << s)) != 0u); | ||
| mergeStage(s, dir, id, lo, hi); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace bitonic_sort | ||
| } // namespace subgroup | ||
| } // namespace hlsl | ||
| } // namespace nbl | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,70 @@ | ||
| // Copyright (C) 2024 - DevSH Graphics Programming Sp. z O.O. | ||
| // This file is part of the "Nabla Engine". | ||
| // For conditions of distribution and use, see copyright notice in nabla.h | ||
| #ifndef _NBL_BUILTIN_HLSL_UTILITY_INCLUDED_ | ||
| #define _NBL_BUILTIN_HLSL_UTILITY_INCLUDED_ | ||
|
|
||
|
|
||
| #include <nbl/builtin/hlsl/type_traits.hlsl> | ||
|
|
||
|
|
||
| // for now we only implement declval | ||
| namespace nbl | ||
| { | ||
| namespace hlsl | ||
| { | ||
| template<typename T> | ||
| const static bool always_true = true; | ||
| #ifndef __HLSL_VERSION | ||
|
|
||
| template<class T> | ||
| std::add_rvalue_reference_t<T> declval() noexcept | ||
| { | ||
| static_assert(false,"Actually calling declval is ill-formed."); | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| namespace experimental | ||
| { | ||
|
|
||
| template<class T> | ||
| T declval() {} | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
| // Copyright (C) 2024 - DevSH Graphics Programming Sp. z O.O. | ||
| // This file is part of the "Nabla Engine". | ||
| // For conditions of distribution and use, see copyright notice in nabla.h | ||
| #ifndef _NBL_BUILTIN_HLSL_UTILITY_INCLUDED_ | ||
| #define _NBL_BUILTIN_HLSL_UTILITY_INCLUDED_ | ||
|
|
||
|
|
||
| #include <nbl/builtin/hlsl/type_traits.hlsl> | ||
|
|
||
|
|
||
| namespace nbl | ||
| { | ||
| namespace hlsl | ||
| { | ||
|
|
||
| template<typename T1, typename T2> | ||
| struct pair | ||
| { | ||
| using first_type = T1; | ||
| using second_type = T2; | ||
|
|
||
| first_type first; | ||
| second_type second; | ||
| }; | ||
|
Comment on lines
+16
to
+24
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. want a |
||
|
|
||
| template<typename T1, typename T2> | ||
| pair<T1, T2> make_pair(T1 f, T2 s) | ||
| { | ||
| pair<T1, T2> p; | ||
| p.first = f; | ||
| p.second = s; | ||
| return p; | ||
| } | ||
|
|
||
| template<typename T1, typename T2> | ||
| void swap(NBL_REF_ARG(pair<T1, T2>) a, NBL_REF_ARG(pair<T1, T2>) b) | ||
| { | ||
| T1 temp_first = a.first; | ||
| T2 temp_second = a.second; | ||
| a.first = b.first; | ||
| a.second = b.second; | ||
| b.first = temp_first; | ||
| b.second = temp_second; | ||
| } | ||
|
|
||
| template<typename T> | ||
| const static bool always_true = true; | ||
| #ifndef __HLSL_VERSION | ||
|
|
||
| template<class T> | ||
| std::add_rvalue_reference_t<T> declval() noexcept | ||
| { | ||
| static_assert(false,"Actually calling declval is ill-formed."); | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| namespace experimental | ||
| { | ||
|
|
||
| template<class T> | ||
| T declval() {} | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bitonic_sort::BitonicSort...is a tautology, drop theBitonicSortprefix from the names