shitty radix

This commit is contained in:
2026-09-28 23:01:31 +02:00
parent e673c6dc0d
commit 3124978e3f
15 changed files with 1403 additions and 314 deletions
+158
View File
@@ -0,0 +1,158 @@
[[vk::binding(0, 0)]]
RWStructuredBuffer<uint32_t> count_buffer;
[[vk::binding(0, 1)]]
RWStructuredBuffer<uint32_t> reduced_buffer;
static const uint32_t THREAD_WIDTH = 256;
static const uint32_t DATA_WIDTH = THREAD_WIDTH * 2;
static const uint32_t DIGIT_COUNT = 8;
static const uint32_t VALUE_COUNT = 1 << DIGIT_COUNT;
groupshared uint32_t local_data[DATA_WIDTH];
[numthreads(256, 1, 1)]
[shader("compute")]
void reduce_pass(
uint32_t3 workgroup_id: SV_GroupID,
uint32_t3 local_thread_id: SV_GroupThreadID,
uint32_t3 global_thread_id: SV_DispatchThreadID)
{
// Perform sum in current block
let thread_index = global_thread_id.x;
let local_thread_index = local_thread_id.x;
let total = count_buffer.getCount();
// Load to groupshared
local_data[local_thread_index * 2] = select((thread_index * 2) < total, count_buffer[thread_index * 2], 0);
local_data[local_thread_index * 2 + 1] = select(((thread_index * 2) + 1) < total, count_buffer[thread_index * 2 + 1], 0);
GroupMemoryBarrierWithGroupSync();
// Belloch sum
var width : uint32_t = 2;
while (width <= DATA_WIDTH)
{
let dest_index = width * (local_thread_index + 1) - 1;
let get_index = dest_index - (width / 2);
// println!("{}, {}", get_index, dest_index);
if (dest_index < DATA_WIDTH)
{
local_data[dest_index] += local_data[get_index];
}
width *= 2;
GroupMemoryBarrierWithGroupSync();
}
// Write to reduced buffer
reduced_buffer[workgroup_id.x] = local_data[DATA_WIDTH - 1];
// reduced_buffer[workgroup_id.x] = local_data[DATA_WIDTH - 1];
GroupMemoryBarrierWithGroupSync();
local_data[DATA_WIDTH - 1] = 0;
GroupMemoryBarrierWithGroupSync();
while (width >= 2)
{
let dest_index = width * (local_thread_index + 1) - 1;
let get_index = dest_index - (width / 2);
// println!("{}, {}", get_index, dest_index);
if (dest_index < DATA_WIDTH)
{
let self_data = local_data[dest_index];
local_data[dest_index] += local_data[get_index];
local_data[get_index] = self_data;
}
width /= 2;
GroupMemoryBarrierWithGroupSync();
}
// Block now contains running local sum
// Dump back to sum buffer
if (2 * thread_index < total)
{
count_buffer[2 * thread_index] = local_data[2 * local_thread_index];
}
if ((2 * thread_index + 1) < total)
{
count_buffer[2 * thread_index + 1] = local_data[2 * local_thread_index + 1];
}
}
[numthreads(256, 1, 1)]
[shader("compute")]
void apply_pass(
uint32_t3 workgroup_id: SV_GroupID,
uint32_t3 local_thread_id: SV_GroupThreadID,
uint32_t3 global_thread_id: SV_DispatchThreadID)
{
let total = count_buffer.getCount();
let thread_index = global_thread_id.x;
let local_thread_index = local_thread_id.x;
// Gather
let reduced_value = reduced_buffer[workgroup_id.x];
// Apply sum back
if (thread_index * 2 < total)
{
count_buffer[thread_index * 2] += reduced_value;
}
if ((thread_index * 2 + 1) < total)
{
count_buffer[thread_index * 2 + 1] += reduced_value;
}
}
[numthreads(1, 1, 1)]
[shader("compute")]
void direct_pass(
uint32_t3 workgroup_id: SV_GroupID,
uint32_t3 local_thread_id: SV_GroupThreadID,
uint32_t3 global_thread_id: SV_DispatchThreadID)
{
let size = reduced_buffer.getCount();
// Perform exclusive sum
var running_sum : uint32_t = 0;
for (uint32_t i = 0; i < size; i++)
{
let value = reduced_buffer[i];
reduced_buffer[i] = running_sum;
running_sum += value;
}
}
struct IndirectArgs
{
uint32_t x;
uint32_t y;
uint32_t z;
}
[[vk::binding(0, 0)]]
RWStructuredBuffer<uint32_t> total_count;
[[vk::binding(0, 1)]]
RWStructuredBuffer<IndirectArgs> group_counts;
[numthreads(1, 1, 1)]
[shader("compute")]
void compute_invocation_count(
uint32_t3 workgroup_id: SV_GroupID,
uint32_t3 local_thread_id: SV_GroupThreadID,
uint32_t3 global_thread_id: SV_DispatchThreadID)
{
var prev_count = total_count[0];
for (var i = 0; i < group_counts.getCount(); i++)
{
prev_count = div_ceil(prev_count, THREAD_WIDTH);
group_counts[i].x = prev_count;
group_counts[i].y = 1;
group_counts[i].z = 1;
}
}
uint32_t div_ceil(uint32_t n, uint32_t q)
{
return (n / q) + select(n % q == 0, 0, 1);
}