Skip to content
Open
Show file tree
Hide file tree
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 Oct 2, 2025
7b4d222
config question
CrabExtra Oct 2, 2025
432ae81
subgroupsort
CrabExtra Oct 5, 2025
20cac8c
added bitonic_sort name space
CrabExtra Oct 5, 2025
9afdf98
subgroup changes
CrabExtra Oct 7, 2025
68b8c39
removed unused
CrabExtra Oct 7, 2025
2f79d74
added last merge step as a function
CrabExtra Oct 19, 2025
b3ccdea
uncomplete workgroup fn
CrabExtra Oct 19, 2025
42072f6
complete the logic for some pr questions
CrabExtra Oct 19, 2025
f352c49
Refactor bitonic sort for workgroup + Accessor support
CrabExtra Oct 22, 2025
e27c9ef
Update bitonic_sort.hlsl
CrabExtra Oct 22, 2025
cdb804c
VT implumentation
CrabExtra Oct 27, 2025
729b9c7
Update bitonic_sort.hlsl
CrabExtra Oct 29, 2025
b6a8cbb
Update common.hlsl
CrabExtra Oct 29, 2025
1636680
Update bitonic_sort.hlsl
CrabExtra Oct 29, 2025
69e178a
Update common.hlsl
CrabExtra Nov 2, 2025
8596ad6
Update bitonic_sort.hlsl
CrabExtra Nov 2, 2025
b9884a3
Update CMakeLists.txt
CrabExtra Nov 2, 2025
6fa52bc
pair added
CrabExtra Nov 2, 2025
8429b4b
comment outdated pair impl
CrabExtra Nov 2, 2025
0f8c062
bitonic sort acessor added
CrabExtra Nov 2, 2025
c750811
Update common.hlsl
CrabExtra Nov 5, 2025
17eebef
Update bitonic_sort.hlsl
CrabExtra Nov 5, 2025
b2f7d3a
Update bitonic_sort.hlsl
CrabExtra Nov 5, 2025
57e5460
Update utility.hlsl
CrabExtra Nov 5, 2025
d14acf2
Delete include/nbl/builtin/hlsl/utility.hlsl
CrabExtra Nov 5, 2025
e6a51c2
Delete include/nbl/builtin/hlsl/pair.hlsl
CrabExtra Nov 5, 2025
a590338
Add files via upload
CrabExtra Nov 5, 2025
7ca09ae
Update CMakeLists.txt
CrabExtra Nov 5, 2025
ef7edd5
Remove unused pair struct from memory_accessor.hlsl
CrabExtra Nov 5, 2025
d80f449
Merge remote-tracking branch 'upstream/master'
CrabExtra Nov 25, 2025
fa78d2b
refactor common.hlsl and prep for new implumentation
CrabExtra Dec 1, 2025
9d56528
Merge branch 'Devsh-Graphics-Programming:master' into master
CrabExtra Dec 1, 2025
6f2ee43
update common.hlsl
CrabExtra Dec 1, 2025
2d6bf0b
Refactor LocalPasses template for flexibility
CrabExtra Dec 5, 2025
d46a866
Merge branch 'Devsh-Graphics-Programming:master' into master
CrabExtra Dec 5, 2025
7ac604c
Refactor and optimize bitonic sort implementation
CrabExtra Dec 9, 2025
589056f
Merge branch 'master' of https://github.com/CrabExtra/Nabla
CrabExtra Dec 9, 2025
a5e811b
some changes for debugging
CrabExtra Dec 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions include/nbl/builtin/hlsl/bitonic_sort/common.hlsl
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 include/nbl/builtin/hlsl/concepts/accessors/bitonic_sort.hlsl
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>;
Comment on lines +14 to +25

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 the BitonicSort prefix from the names


}
}
}
}
#endif
14 changes: 1 addition & 13 deletions include/nbl/builtin/hlsl/memory_accessor.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ namespace nbl
{
namespace hlsl
{

// TODO: flesh out and move to `nbl/builtin/hlsl/utility.hlsl`
template<typename T1, typename T2>
struct pair
{
using first_type = T1;
using second_type = T2;

first_type first;
second_type second;
};

namespace accessor_adaptors
{
namespace impl
Expand Down Expand Up @@ -227,4 +215,4 @@ struct Offset : impl::OffsetBase<IndexType,_Offset>
}
}
}
#endif
#endif
94 changes: 94 additions & 0 deletions include/nbl/builtin/hlsl/subgroup/bitonic_sort.hlsl
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
110 changes: 70 additions & 40 deletions include/nbl/builtin/hlsl/utility.hlsl
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

want a get and a get_helper struct in this header, plus a particla spec for pair in this header


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
Loading
Loading