diff --git a/shaders/compaction.spv b/shaders/compaction.spv index aed71d5..f71f998 100644 Binary files a/shaders/compaction.spv and b/shaders/compaction.spv differ diff --git a/shaders/gi.spv b/shaders/gi.spv index d281cac..47cbafa 100644 Binary files a/shaders/gi.spv and b/shaders/gi.spv differ diff --git a/shaders/radix_reduce.spv b/shaders/radix_reduce.spv index d9d8040..85fc112 100644 Binary files a/shaders/radix_reduce.spv and b/shaders/radix_reduce.spv differ diff --git a/shaders/radix_sort.slang b/shaders/radix_sort.slang index 33634ed..3c32e3c 100644 --- a/shaders/radix_sort.slang +++ b/shaders/radix_sort.slang @@ -5,7 +5,7 @@ const static uint32_t value_count = 1 << digit_count; static const uint32_t WG_THREAD_COUNT = 256; // Elements managed per thread for histo build -static const uint32_t WE_HISTO_ELEMENTS = 1; +static const uint32_t WE_HISTO_ELEMENTS = 16; // Elements managed per workgroup for histo build static const uint32_t WG_HISTO_ELEMENT = WG_THREAD_COUNT * WE_HISTO_ELEMENTS; @@ -175,10 +175,33 @@ void total_prefix_sum_pass( select(local_location.x == 0, 0, local_prefix[local_location.x - 1]); } -groupshared uint32_t keys[WG_THREAD_COUNT]; -groupshared uint32_t values[WG_THREAD_COUNT]; -groupshared uint32_t sub_radix[WG_THREAD_COUNT]; -groupshared uint32_t digits[WG_THREAD_COUNT]; +struct SortToken +{ + uint32_t tk; + + __init(uint32_t digit, uint32_t index) + { + tk = (digit << 16) | (index & 0xFFFF); + } + + property uint32_t digit + { + get { + return tk >> 16; + } + } + + property uint32_t index + { + get { + return tk & 0xFFFF; + } + } +} + +groupshared SortToken sort_tokens[WG_HISTO_ELEMENT]; + +// groupshared uint32_t local_prefix[WG_THREAD_COUNT]; [shader("compute")] [numthreads(WG_THREAD_COUNT, 1, 1)] @@ -189,43 +212,89 @@ void scatter_pass( { uint32_t batch_index = 0; - // Load up a batch of values - keys[local_location.x] = key_buffer[batch_index * WG_THREAD_COUNT + local_location.x]; - values[local_location.x] = value_buffer[batch_index * WG_THREAD_COUNT + local_location.x]; - digits[local_location.x] = extract_digit(keys[local_location.x], params.pass_index, params.descending); - - // Split radix sort on the batch - for (uint32_t split_index = 0; split_index < digit_count; split_index++) + 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 += WG_THREAD_COUNT) { - // Set subradix value - sub_radix[local_location.x] = select(((digits[local_location.x] >> split_index) & 1) != 0, 1 << 16, 1); + uint32_t key = key_buffer[index]; + uint32_t digit = extract_digit(key, params.pass_index, params.descending); + // local_histogram[digit].add(1); + uint32_t local_index = index - group_location.x * WG_HISTO_ELEMENT; + sort_tokens[local_index] = SortToken(digit, local_index); + } + + GroupMemoryBarrierWithGroupSync(); + + // Split sort + SortToken thread_tokens[WE_HISTO_ELEMENTS]; + uint32_t thread_prefix[WE_HISTO_ELEMENTS]; + for (uint32_t radix_pass = 0; radix_pass < 8; radix_pass++) + { + // Load tokens + for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++) + { + thread_tokens[i] = sort_tokens[i + WE_HISTO_ELEMENTS * local_location.x]; + } + + // Compute local prefix + uint32_t sum = 0; + for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++) + { + uint32_t bin_digit = ((thread_tokens[i].digit >> radix_pass) & 1) ^ 1; + thread_prefix[i] = sum; + sum += bin_digit; + } + + // Publish sum + local_prefix[local_location.x] = sum; + GroupMemoryBarrierWithGroupSync(); + // Perform hillis steele for (uint32_t offset = 1; offset < WG_THREAD_COUNT; offset *= 2) { - uint32_t offset_value = select(local_location.x >= offset, sub_radix[local_location.x - offset], 0); + uint32_t offset_value = select(local_location.x >= offset, local_prefix[local_location.x - offset], 0); GroupMemoryBarrierWithGroupSync(); - sub_radix[local_location.x] += offset_value; + local_prefix[local_location.x] += offset_value; GroupMemoryBarrierWithGroupSync(); } - // Now radix is ready, scatter split - uint32_t key = keys[local_location.x]; - uint32_t value = values[local_location.x]; - uint32_t digit = digits[local_location.x]; - uint32_t prefix = sub_radix[local_location.x]; - uint32_t destination = select(((digit >> split_index) & 1) != 0, prefix >> 16, prefix & 0xFFFF); - - GroupMemoryBarrierWithGroupSync(); - - // Scatter - keys[destination] = key; - values[destination] = value; - digits[destination] = digit; + uint32_t total_zeroes = local_prefix[WG_THREAD_COUNT - 1]; + // Scatter sort tokens + uint32_t zero_rank = select(local_location.x == 0, 0, local_prefix[local_location.x - 1]); + for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++) + { + uint32_t bin_digit = (thread_tokens[i].digit >> radix_pass) & 1; + uint32_t scatter_index = select(bin_digit == 0, + thread_prefix[i] + zero_rank, + total_zeroes + local_location.x - (thread_prefix[i] + zero_rank)); + sort_tokens[scatter_index] = thread_tokens[i]; + } GroupMemoryBarrierWithGroupSync(); } - // Now values are sorted according to digit + // Scatter to main memory + // Compute local offset by boundary detection + if (local_location.x == 0 || sort_tokens[local_location.x].digit != sort_tokens[local_location.x - 1].digit) + { + local_prefix[sort_tokens[local_location.x].digit] = local_location.x; + } + GroupMemoryBarrierWithGroupSync(); + + // Scatter keys + uint32_t offset = local_location.x * WE_HISTO_ELEMENTS; + for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++) + { + uint32_t digit = sort_tokens[offset + i].digit; + uint32_t index = sort_tokens[offset + i].index; + uint32_t group_offset = histograms[group_location.x + digit * params.histogram_count]; + uint32_t digit_offset = histograms[(params.histogram_count) + digit * params.histogram_count]; + + uint32_t destination_index = group_offset + digit_offset + (local_location.x - local_prefix[digit]); + + dest_key_buffer[destination_index] = key_buffer[index]; + dest_value_buffer[destination_index] = value_buffer[index]; + } } diff --git a/shaders/radix_sort.spv b/shaders/radix_sort.spv index 271d306..a2f3b14 100644 Binary files a/shaders/radix_sort.spv and b/shaders/radix_sort.spv differ diff --git a/shaders/voxel.spv b/shaders/voxel.spv index 788705b..400108a 100644 Binary files a/shaders/voxel.spv and b/shaders/voxel.spv differ diff --git a/src/gpu/radix_sort.rs b/src/gpu/radix_sort.rs index edf17fd..7c5702f 100644 --- a/src/gpu/radix_sort.rs +++ b/src/gpu/radix_sort.rs @@ -72,7 +72,7 @@ impl RadixSorter const WG_THREAD_COUNT: usize = 256; // Elements managed per thread for histo build - const WE_HISTO_ELEMENTS: usize = 1; + const WE_HISTO_ELEMENTS: usize = 16; // Elements managed per workgroup for histo build const WG_HISTO_ELEMENTS: usize = Self::WG_THREAD_COUNT * Self::WE_HISTO_ELEMENTS; @@ -351,11 +351,7 @@ impl RadixSorter //compute_pass.dispatch_workgroups_indirect(&self., 0); compute_pass.set_pipeline(&self.scatter_pass); compute_pass.set_immediates(0, bytes); - compute_pass.dispatch_workgroups( - self.max_size.div_ceil(Self::WG_THREAD_COUNT) as u32, - 1, - 1, - ); + compute_pass.dispatch_workgroups(self.histo_block_count as u32, 1, 1); } // Even number of passes => data is in the user buffer