working buffer compactor

This commit is contained in:
2026-09-17 20:52:09 +02:00
parent 7c78085b61
commit f34550e692
5 changed files with 326 additions and 26 deletions
+23 -11
View File
@@ -1,13 +1,13 @@
[[vk::binding(0, 0)]]
RWStructuredBuffer<uint32_t> count_buffer;
[[vk::binding(1, 0)]]
RWStructuredBuffer<uint32_t> index_buffer;
[[vk::binding(0, 1)]]
RWStructuredBuffer<uint32_t> reduced_buffer;
[[vk::binding(1, 1)]]
RWStructuredBuffer<uint32_t> sum_buffer;
[[vk::binding(2, 1)]]
RWStructuredBuffer<uint32_t> compaction_buffer;
groupshared uint32_t local_data[256 * 2];
static uint32_t THREAD_WIDTH = 256;
@@ -50,7 +50,7 @@ void block_sum(
var width : uint32_t = 2;
while (width <= DATA_WIDTH)
{
let dest_index = width * (thread_index + 1) - 1;
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)
@@ -61,10 +61,14 @@ void block_sum(
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];
local_data[DATA_WIDTH - 1] = 0;
while (width >= 2)
{
let dest_index = width * (thread_index + 1) - 1;
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)
@@ -81,9 +85,6 @@ void block_sum(
// Dump back to sum buffer
sum_buffer[2 * thread_index] = local_data[2 * local_thread_index];
sum_buffer[2 * thread_index + 1] = local_data[2 * local_thread_index + 1];
// Write to reduced buffer
reduced_buffer[workgroup_id.x] = local_data[DATA_WIDTH - 1];
}
[numthreads(1, 1, 1)]
@@ -107,7 +108,7 @@ void linear_reduced_sum(
[numthreads(256, 1, 1)]
[shader("compute")]
void uniform_add(
void scatter(
uint32_t3 workgroup_id: SV_GroupID,
uint32_t3 local_thread_id: SV_GroupThreadID,
uint32_t3 global_thread_id: SV_DispatchThreadID)
@@ -116,9 +117,20 @@ void uniform_add(
let local_thread_index = local_thread_id.x;
// Gather
let reduced_value = reduced_buffer[global_thread_id.x];
let reduced_value = reduced_buffer[workgroup_id.x];
// Apply
sum_buffer[thread_index * 2] += reduced_value;
sum_buffer[thread_index * 2 + 1] += reduced_value;
let sum1 = sum_buffer[thread_index * 2] + reduced_value;
let sum2 = sum_buffer[thread_index * 2 + 1] + reduced_value;
// Scatter index to compact index buffer if predicate is true
if (count_buffer[thread_index * 2] != 0)
{
index_buffer[sum1] = thread_index * 2;
}
if (count_buffer[thread_index * 2 + 1] != 0)
{
index_buffer[sum2] = thread_index * 2 + 1;
}
}