diff --git a/shaders/compaction.spv b/shaders/compaction.spv index f71f998..aed71d5 100644 Binary files a/shaders/compaction.spv and b/shaders/compaction.spv differ diff --git a/shaders/gi.spv b/shaders/gi.spv index 47cbafa..d281cac 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 85fc112..d9d8040 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 3c32e3c..b601257 100644 --- a/shaders/radix_sort.slang +++ b/shaders/radix_sort.slang @@ -210,17 +210,25 @@ void scatter_pass( uint32_t3 local_location: SV_GroupThreadID, uint32_t3 group_location: SV_GroupID, ) { - uint32_t batch_index = 0; - - 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); + // 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; + for (uint32_t index = start; + index < (group_location.x + 1) * WG_HISTO_ELEMENT; index += WG_THREAD_COUNT) { - 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); + if (index < end) + { + uint32_t key = key_buffer[index]; + uint32_t digit = extract_digit(key, params.pass_index, params.descending); + sort_tokens[local_index] = SortToken(digit, local_index); + } + else + { + sort_tokens[local_index] = SortToken(0xFFFF, local_index); + } } GroupMemoryBarrierWithGroupSync(); @@ -240,9 +248,9 @@ void scatter_pass( 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; + uint32_t bin_digit = (thread_tokens[i].digit >> radix_pass) & 1; thread_prefix[i] = sum; - sum += bin_digit; + sum += 1 - bin_digit; // Count number of zeroes } // Publish sum @@ -262,36 +270,56 @@ void scatter_pass( uint32_t total_zeroes = local_prefix[WG_THREAD_COUNT - 1]; // Scatter sort tokens + + // Exclusive zero rank of block 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 index = i + local_location.x * WE_HISTO_ELEMENTS; uint32_t scatter_index = select(bin_digit == 0, thread_prefix[i] + zero_rank, - total_zeroes + local_location.x - (thread_prefix[i] + zero_rank)); + total_zeroes + index - (thread_prefix[i] + zero_rank)); sort_tokens[scatter_index] = thread_tokens[i]; } GroupMemoryBarrierWithGroupSync(); } - // 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) + // Tokens are now sorted in the LDS + // Boundary detection + for ( + uint32_t i = local_location.x * WE_HISTO_ELEMENTS; + i < min((local_location.x + 1) * WE_HISTO_ELEMENTS, element_count); + i++) { - local_prefix[sort_tokens[local_location.x].digit] = local_location.x; + SortToken token = sort_tokens[i]; + uint32_t digit = token.digit; + if (i == 0 || sort_tokens[i - 1].digit != digit) + { + local_prefix[digit] = i; + } } + + // For each digit, we now know where it starts within the buffer + GroupMemoryBarrierWithGroupSync(); // Scatter keys - uint32_t offset = local_location.x * WE_HISTO_ELEMENTS; - for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++) + for ( + uint32_t i = local_location.x * WE_HISTO_ELEMENTS; + i < min((local_location.x + 1) * WE_HISTO_ELEMENTS, element_count); + i++) { - uint32_t digit = sort_tokens[offset + i].digit; - uint32_t index = sort_tokens[offset + i].index; + 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) + digit * params.histogram_count]; + uint32_t digit_offset = histograms[(params.histogram_count - 1) + digit * params.histogram_count]; - uint32_t destination_index = group_offset + digit_offset + (local_location.x - local_prefix[digit]); + uint32_t destination_index = group_offset + digit_offset + (i - local_prefix[digit]); + + // dest_key_buffer[i] = local_prefix[local_location.x]; + // dest_key_buffer[i] = destination_index; + // dest_key_buffer[i] = local_prefix[local_location.x]; 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 a2f3b14..b6d3529 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 400108a..788705b 100644 Binary files a/shaders/voxel.spv and b/shaders/voxel.spv differ diff --git a/src/main.rs b/src/main.rs index adf531d..e3baaaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -198,7 +198,11 @@ impl State let mut encoder = device.create_command_encoder(&Default::default()); println!("Making elements"); - let elt_count = (1 << 20) as u32; + //let elt_count = (1 << 20) as u32; + //let elt_count = 100_000; + //let elt_count = (21 * 4096) as u32; + //let elt_count = 3000 as u32; + let elt_count = 1_000_000; //let elt_count = 256 as u32; let org_big_value_buffer = (0..(elt_count as u32)).collect::>(); let org_big_key_buffer = org_big_value_buffer @@ -207,6 +211,22 @@ impl State .map(|_| random_range(1..256)) .collect::>(); + // let mut prefix = org_big_key_buffer.iter().fold([0; 256], |acc, x| { + // let mut acc = acc; + // acc[x >> 24] += 1; + // acc + // }); + // + // let mut sum = 0; + // for x in prefix.iter_mut() + // { + // let cur = *x; + // *x = sum; + // sum += cur; + // } + // + // println!("Counts : {:?}", prefix); + let sorted_values = org_big_key_buffer .iter() .zip(org_big_value_buffer.iter()) @@ -267,7 +287,7 @@ impl State .chunks(4) .map(|x| u32::from_ne_bytes([x[0], x[1], x[2], x[3]])) .collect::>(); - println!("{:?}", &data[0..256]); + println!("{:?}", &data[0..3000]); }, );