Radix sort working !

This commit is contained in:
2026-10-03 00:02:47 +02:00
parent efd07fd53b
commit d9df73abab
8 changed files with 266 additions and 161 deletions
+31 -7
View File
@@ -87,7 +87,9 @@ void block_sum(
sum_buffer[2 * thread_index + 1] = local_data[2 * local_thread_index + 1];
}
[numthreads(1, 1, 1)]
groupshared uint32_t local_prefix[256];
[numthreads(256, 1, 1)]
[shader("compute")]
void linear_reduced_sum(
uint32_t3 workgroup_id: SV_GroupID,
@@ -96,13 +98,35 @@ void linear_reduced_sum(
{
let size = reduced_buffer.getCount();
// Perform exclusive sum
var running_sum : uint32_t = 0;
for (uint32_t i = 0; i < size; i++)
uint32_t running_sum = 0;
for (uint32_t slice = 0; slice < size; slice += 256)
{
let value = reduced_buffer[i];
reduced_buffer[i] = running_sum;
running_sum += value;
let index = slice + local_thread_id.x;
if (index < size)
{
local_prefix[local_thread_id.x] = reduced_buffer[index];
}
else
{
local_prefix[local_thread_id.x] = 0;
}
GroupMemoryBarrierWithGroupSync();
for (uint32_t offset = 1; offset < 256; offset *= 2)
{
uint32_t offset_value = select(local_thread_id.x >= offset, local_prefix[local_thread_id.x - offset], 0);
GroupMemoryBarrierWithGroupSync();
local_prefix[local_thread_id.x] += offset_value;
GroupMemoryBarrierWithGroupSync();
}
if (index < size)
{
reduced_buffer[index] = select(local_thread_id.x == 0, 0, local_prefix[local_thread_id.x - 1]) + running_sum;
}
running_sum += local_prefix[255];
GroupMemoryBarrierWithGroupSync();
}
}
Binary file not shown.
+32 -7
View File
@@ -38,6 +38,11 @@ RWStructuredBuffer<uint32_t> dest_key_buffer;
[vk::binding(1, 2)]
RWStructuredBuffer<uint32_t> dest_value_buffer;
[vk::binding(0, 3)]
RWStructuredBuffer<uint32_t> indirect_element_count;
[vk::binding(1, 3)]
RWStructuredBuffer<IndirectArgs> indirect_workgroup_count;
[vk::push_constant]
uniform PassParams params;
@@ -68,7 +73,7 @@ void histogram_pass(
// This coalesces read
for (uint32_t index = group_location.x * WG_HISTO_ELEMENT + local_location.x;
index < min(key_buffer.getCount(), (group_location.x + 1) * WG_HISTO_ELEMENT);
index < min(indirect_element_count[0], (group_location.x + 1) * WG_HISTO_ELEMENT);
index += WG_THREAD_COUNT)
{
uint32_t key = key_buffer[index];
@@ -106,7 +111,8 @@ void prefix_sum_pass(
uint32_t running_prefix = 0;
uint32_t start_offset = group_location.x * params.histogram_count;
uint32_t end_offset = start_offset + params.histogram_count;
// uint32_t end_offset = start_offset + params.histogram_count;
uint32_t end_offset = start_offset + indirect_workgroup_count[0].x + 1;
for (
uint32_t slice_offset = start_offset;
@@ -160,7 +166,7 @@ void total_prefix_sum_pass(
}
// Load last histogram into lds
local_prefix[local_location.x] = histograms[(params.histogram_count - 1) + params.histogram_count * local_location.x];
local_prefix[local_location.x] = histograms[indirect_workgroup_count[0].x + params.histogram_count * local_location.x];
GroupMemoryBarrierWithGroupSync();
// Perform hillis steele
for (uint32_t offset = 1; offset < WG_THREAD_COUNT; offset *= 2)
@@ -171,7 +177,7 @@ void total_prefix_sum_pass(
GroupMemoryBarrierWithGroupSync();
}
histograms[(params.histogram_count - 1) + params.histogram_count * local_location.x] =
histograms[indirect_workgroup_count[0].x + params.histogram_count * local_location.x] =
select(local_location.x == 0, 0, local_prefix[local_location.x - 1]);
}
@@ -212,8 +218,8 @@ void scatter_pass(
{
// uint32_t batch_index = 0;
uint32_t start = group_location.x * WG_HISTO_ELEMENT + local_location.x;
uint32_t end = min(key_buffer.getCount(), (group_location.x + 1) * WG_HISTO_ELEMENT);
uint32_t element_count = key_buffer.getCount() - group_location.x * WG_HISTO_ELEMENT;
uint32_t end = min(indirect_element_count[0], (group_location.x + 1) * WG_HISTO_ELEMENT);
uint32_t element_count = indirect_element_count[0] - group_location.x * WG_HISTO_ELEMENT;
for (uint32_t index = start;
index < (group_location.x + 1) * WG_HISTO_ELEMENT;
index += WG_THREAD_COUNT)
@@ -313,7 +319,7 @@ void scatter_pass(
uint32_t digit = sort_tokens[i].digit;
uint32_t index = sort_tokens[i].index + group_location.x * WG_HISTO_ELEMENT;
uint32_t group_offset = histograms[group_location.x + digit * params.histogram_count];
uint32_t digit_offset = histograms[(params.histogram_count - 1) + digit * params.histogram_count];
uint32_t digit_offset = histograms[indirect_workgroup_count[0].x + digit * params.histogram_count];
uint32_t destination_index = group_offset + digit_offset + (i - local_prefix[digit]);
@@ -326,3 +332,22 @@ void scatter_pass(
}
}
[shader("compute")]
[numthreads(1, 1, 1)]
void compute_counts_pass(
uint32_t3 location: SV_DispatchThreadID,
uint32_t3 local_location: SV_GroupThreadID,
uint32_t3 group_location: SV_GroupID, )
{
uint32_t element_count = indirect_element_count[0];
if (params.pass_index != 0)
{
element_count = params.pass_index;
indirect_element_count[0] = params.pass_index;
}
indirect_workgroup_count[0].x = div_ceil(element_count, WG_HISTO_ELEMENT);
indirect_workgroup_count[0].y = 1;
indirect_workgroup_count[0].z = 1;
}
Binary file not shown.