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
+1 -1
View File
@@ -1,4 +1,4 @@
all: clean voxel.spv compaction.spv gi.spv
all: clean voxel.spv compaction.spv gi.spv radix_sort.spv radix_reduce.spv
clean:
rm -f *.spv
Binary file not shown.
BIN
View File
Binary file not shown.
+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);
}
Binary file not shown.
+275
View File
@@ -0,0 +1,275 @@
const static uint32_t digit_count = 4;
const static uint32_t value_count = 1 << digit_count;
struct Histogram
{
uint32_t[value_count] histogram;
}
struct IndirectArgs
{
uint32_t x;
uint32_t y;
uint32_t z;
}
struct PassParams
{
uint32_t pass_index;
uint32_t descending;
uint32_t digit_stride;
}
[vk::binding(0, 0)]
RWStructuredBuffer<uint32_t> histograms;
[vk::binding(0, 1)]
RWStructuredBuffer<uint32_t> key_buffer;
[vk::binding(1, 1)]
RWStructuredBuffer<uint32_t> value_buffer;
// Double buffering
[vk::binding(0, 2)]
RWStructuredBuffer<uint32_t> dest_key_buffer;
[vk::binding(1, 2)]
RWStructuredBuffer<uint32_t> dest_value_buffer;
[vk::binding(0, 3)]
RWStructuredBuffer<uint32_t> sort_count;
[vk::binding(1, 3)]
RWStructuredBuffer<IndirectArgs> block_count;
[vk::push_constant]
uniform PassParams params;
uint32_t get_digit(uint32_t value, uint32_t digit_id, uint32_t descending)
{
let mask = ~(0xFFFFFFFF << digit_count);
let digit = (value >> (digit_id * digit_count)) & mask;
return select(descending != 0, digit ^ mask, digit);
}
static const uint32_t split_size = 16;
static const uint32_t thread_count = 256;
static const uint32_t local_histo_count = thread_count / split_size;
struct LocalHistogram
{
Atomic<uint32_t>[value_count] histogram;
}
groupshared LocalHistogram[local_histo_count] local_histograms;
uint32_t histogram_index(uint32_t digit, uint32_t histo_index)
{
return digit * params.digit_stride + histo_index;
}
[shader("compute")]
[numthreads(256, 1, 1)]
void histogram_pass(
uint32_t3 location: SV_DispatchThreadID,
uint32_t3 local_location: SV_GroupThreadID,
uint32_t3 group_location: SV_GroupID, )
{
let element_count = sort_count[0];
// Reset histogram
let split_index = local_location.x / split_size;
for (uint32_t i = 0; i < value_count; i++)
{
local_histograms[split_index].histogram[i].store(0);
}
if (location.x >= element_count)
{
return;
}
GroupMemoryBarrierWithGroupSync(); // Sync histo reset
var value = key_buffer[location.x];
value = get_digit(value, params.pass_index, params.descending);
local_histograms[split_index].histogram[value].add(1);
GroupMemoryBarrierWithGroupSync(); // Sync histo build
// Aggregate all histograms and send to local memory
// Binary tree sum
/*
var amount = 2;
for (uint32_t d = local_histo_count / 2; d > 0; d >>= 1)
{
if (local_location.x < d)
{
let a = location.x * amount;
let b = a + amount - 1;
for (var v = 0; v < value_count; v++)
{
local_histograms[a * value_count + v].add(local_histograms[b * value_count + v].load());
}
}
GroupMemoryBarrierWithGroupSync();
amount >>= 1;
}
*/
// thread 0 sends to local memory
if (local_location.x == 0)
{
for (uint32_t v = 0; v < value_count; v++)
{
var sum = 0;
for (uint32_t i = 0; i < local_histo_count; i++)
{
sum += local_histograms[i].histogram[v].load();
}
histograms[histogram_index(v, group_location.x)] = sum;
}
}
}
uint32_t div_ceil(uint32_t n, uint32_t q)
{
return (n / q) + select(n % q == 0, 0, 1);
}
groupshared uint32_t[16][16] block_sums;
void scan_digit(uint32_t lid_digit, uint32_t lid_histo)
{
let thread_block_size = div_ceil(block_count[0].x, 16);
var running_sum = 0;
for (uint32_t i = lid_histo * thread_block_size; i < min(thread_block_size * (lid_histo + 1), block_count[0].x); i++)
{
let current = histograms[histogram_index(lid_digit, i)];
histograms[histogram_index(lid_digit, i)] = running_sum;
running_sum += current;
}
if (lid_histo == 15)
{
histograms[histogram_index(lid_digit, block_count[0].x)] = running_sum;
}
block_sums[lid_digit][lid_histo] = running_sum;
}
void apply_digit(uint32_t lid_digit, uint32_t lid_histo)
{
let thread_block_size = div_ceil(block_count[0].x + 1, 16);
var running_sum = 0;
for (uint32_t i = lid_histo * thread_block_size; i < min(thread_block_size * (lid_histo + 1), block_count[0].x + 1); i++)
{
histograms[histogram_index(lid_digit, i)] += block_sums[lid_digit][lid_histo];
}
}
[shader("compute")]
[numthreads(16, 16, 1)]
void running_sum_pass(
uint32_t3 location: SV_DispatchThreadID,
uint32_t3 local_location: SV_GroupThreadID,
uint32_t3 group_location: SV_GroupID, )
{
scan_digit(local_location.x, local_location.y);
AllMemoryBarrierWithGroupSync();
if (local_location.y == 0)
{
var running_sum = 0;
for (uint32_t i = 0; i < 16; i++)
{
let current = block_sums[local_location.x][i];
block_sums[local_location.x][i] = running_sum;
running_sum += current;
}
}
AllMemoryBarrierWithGroupSync();
apply_digit(local_location.x, local_location.y);
AllMemoryBarrierWithGroupSync();
if (local_location.x == 0 && local_location.y == 0)
{
var running_sum = 0;
for (uint32_t i = 0; i < value_count; i++)
{
let current = histograms[histogram_index(i, block_count[0].x)];
histograms[histogram_index(i, block_count[0].x)] = running_sum;
running_sum += current;
}
}
}
groupshared uint32_t[256] keys;
groupshared uint32_t[256] values;
groupshared uint32_t[256] digit;
[shader("compute")]
[numthreads(256, 1, 1)]
void scatter_pass(
uint32_t3 location: SV_DispatchThreadID,
uint32_t3 local_location: SV_GroupThreadID,
uint32_t3 group_location: SV_GroupID, )
{
let element_count = sort_count[0];
// Load kv
if (location.x >= element_count)
{
return;
}
// Gather
keys[local_location.x] = key_buffer[location.x];
values[local_location.x] = value_buffer[location.x];
// Compute digit
digit[local_location.x] = get_digit(keys[local_location.x], params.pass_index, params.descending);
GroupMemoryBarrierWithGroupSync();
// Compute local offset
var local_offset = 0;
var local_digit = digit[local_location.x];
for (uint32_t i = 0; i < local_location.x; i++)
{
if (digit[i] == local_digit)
{
local_offset += 1;
}
}
// Scatter
// Compute global offset
let digit_offset = local_digit * (block_count[0].x + 1);
let global_loc = histograms[histogram_index(local_digit, block_count[0].x)] + histograms[histogram_index(local_digit, group_location.x)] + local_offset;
dest_key_buffer[global_loc] = keys[local_location.x];
dest_value_buffer[global_loc] = values[local_location.x];
}
[shader("compute")]
[numthreads(1, 1, 1)]
void indirect_count_pass(
uint32_t3 location: SV_DispatchThreadID,
uint32_t3 local_location: SV_GroupThreadID,
uint32_t3 group_location: SV_GroupID, )
{
let indirect = params.pass_index;
if (indirect == 0)
{
sort_count[0] = key_buffer.getCount();
}
let div_ceil = (sort_count[0] / 256) + select(sort_count[0] % 256 == 0, 0, 1);
block_count[0].x = div_ceil;
block_count[0].y = 1;
block_count[0].z = 1;
}
Binary file not shown.
BIN
View File
Binary file not shown.