cool profiling
This commit is contained in:
+148
-41
@@ -1,9 +1,10 @@
|
||||
use std::num::NonZero;
|
||||
|
||||
use bytemuck::Zeroable;
|
||||
use tiff::encoder;
|
||||
use wgpu::{BindGroup, BindGroupLayout, Buffer, BufferUsages, CommandEncoder, ComputePipeline, Device, Queue, ShaderStages, naga::back::spv::Capability::Shader, util::DeviceExt};
|
||||
|
||||
use crate::{gpu::radix_sort::{self, RadixSorter}, voxel_cache::{data::{CacheNodeRequest, CacheResponse, ColorPoolElement, LocationPoolElement, StructurePoolElement}, indirect_buffer::NonZeroBufferCompactor, layout::VoxelCacheLayout, producer_interface::CacheProducerInterface, request_buffer::RequestBuffer, structure_table::StructureTable, usage_buffer::UsageBuffer}};
|
||||
use crate::{gpu::radix_sort::{self, RadixSorter}, gpu_profiler::{self, ProfilingEncoder, ScopeEncoder, begin_scope}, voxel_cache::{data::{CacheNodeRequest, CacheResponse, ColorPoolElement, LocationPoolElement, StructurePoolElement}, indirect_buffer::NonZeroBufferCompactor, layout::VoxelCacheLayout, producer_interface::CacheProducerInterface, request_buffer::RequestBuffer, structure_table::StructureTable, usage_buffer::UsageBuffer}};
|
||||
|
||||
|
||||
pub mod request_buffer;
|
||||
@@ -26,6 +27,9 @@ pub struct VoxelCache<const N: usize>
|
||||
|
||||
// Contains location information per node for end application
|
||||
location_pool: Buffer,
|
||||
|
||||
// Contains the depth of node from its structure table indirection
|
||||
depth_pool: Buffer,
|
||||
|
||||
// User attributes
|
||||
attributes_pools: Vec<Buffer>,
|
||||
@@ -62,6 +66,7 @@ pub struct VoxelCache<const N: usize>
|
||||
caching_pass: ComputePipeline,
|
||||
invalidation_pass: ComputePipeline,
|
||||
reset_pass: ComputePipeline,
|
||||
indirect_usage_copy_pass: ComputePipeline,
|
||||
buffer_compactor: NonZeroBufferCompactor,
|
||||
sorter: RadixSorter,
|
||||
|
||||
@@ -113,6 +118,12 @@ where
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let depth_pool = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("VoxelCache::depth_pool"),
|
||||
usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
|
||||
contents: bytemuck::cast_slice(vec![u32::MAX; cache_size].as_slice()), // Fill empty nodes with max depth, so they end up first
|
||||
});
|
||||
|
||||
// Create attribttues
|
||||
let attributes_pools =
|
||||
layout.attributes().iter().enumerate()
|
||||
@@ -212,7 +223,7 @@ where
|
||||
|
||||
let (structure_bind_group_layout, structure_bind_group) = create_multi_buffers_bg_bg_layout(
|
||||
&device,
|
||||
&[&structure_pool, &location_pool],
|
||||
&[&structure_pool, &location_pool, &depth_pool],
|
||||
Some("VoxelCache::structure_bind_group"));
|
||||
|
||||
let (management_bind_group_layout, management_bind_group) = create_multi_buffers_bg_bg_layout(
|
||||
@@ -264,6 +275,7 @@ where
|
||||
// Structure bind group
|
||||
@group(0) @binding(0) var<storage, read_write> structure_pool: array<StructurePoolElement>;
|
||||
@group(0) @binding(1) var<storage, read_write> location_pool: array<LocationPoolElement>;
|
||||
@group(0) @binding(2) var<storage, read_write> depth_pool: array<u32>;
|
||||
|
||||
// Management bind group
|
||||
@group(1) @binding(0) var<storage, read_write> lru_list: array<u32>;
|
||||
@@ -271,6 +283,7 @@ where
|
||||
@group(1) @binding(2) var<storage, read_write> usage_buffer: array<u32>;
|
||||
@group(1) @binding(3) var<storage, read_write> request_buffer: array<u32>;
|
||||
@group(1) @binding(4) var<storage, read_write> cache_request_count: u32;
|
||||
@group(1) @binding(5) var<storage, read_write> cache_scratch_buffer: array<u32>;
|
||||
|
||||
// Attributes bind group
|
||||
@group(2) @binding(0) var<storage, read_write> attributes_pools: binding_array<AttributeArray, {attribute_count}>;
|
||||
@@ -568,12 +581,12 @@ where
|
||||
if(requests_wb[index].child_index == 0xFFFFFFFF)
|
||||
{{
|
||||
structure_table_pointers[requests_wb[index].node_index] = new_pointer;
|
||||
//depth_pool[overwritten_element] = 0;
|
||||
depth_pool[overwritten_element] = 0;
|
||||
}}
|
||||
else if(usage_buffer[requests_wb[index].node_index] != parameters.frame_timestamp + 1)
|
||||
{{
|
||||
structure_pool[requests_wb[index].node_index].pointers[requests_wb[index].child_index] = new_pointer;
|
||||
//depth_pool[overwritten_element] = depth_pool[requests_wb[index].node_index] + 1;
|
||||
depth_pool[overwritten_element] = depth_pool[requests_wb[index].node_index] + 1;
|
||||
}}
|
||||
}}
|
||||
|
||||
@@ -719,6 +732,41 @@ where
|
||||
cache: None,
|
||||
});
|
||||
|
||||
// ~~~ Indirect usage copy pipeline ~~~
|
||||
// Copies usage according to indicies already sorted by depth in the lru list
|
||||
// So they can be further sorted by usage
|
||||
let indirect_usage_copy_pass =
|
||||
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("VoxelCache::indirect_usage_copy_pass"),
|
||||
layout: Some(&invalidation_pipeline_layout),
|
||||
module: &device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("VoxelCache::indirect_usage_copy_pass_shader_module"),
|
||||
source: wgpu::ShaderSource::Wgsl(
|
||||
format!("
|
||||
{wgsl_cache_bindings}
|
||||
|
||||
@compute
|
||||
@workgroup_size(64)
|
||||
fn main(
|
||||
@builtin(global_invocation_id) global_invocation_id: vec3<u32>
|
||||
)
|
||||
{{
|
||||
var index = global_invocation_id.x;
|
||||
let total_pool = arrayLength(&lru_list);
|
||||
|
||||
var sorted_index = lru_list[index];
|
||||
var usage = usage_buffer[sorted_index];
|
||||
cache_scratch_buffer[index] = usage;
|
||||
}}
|
||||
")
|
||||
.into(),
|
||||
),
|
||||
}),
|
||||
entry_point: Some("main"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
// ~~ Structure table
|
||||
let structure_table = StructureTable::new(device.clone(), queue.clone());
|
||||
|
||||
@@ -793,6 +841,7 @@ where
|
||||
structure_pool,
|
||||
attributes_pools,
|
||||
location_pool,
|
||||
depth_pool,
|
||||
request_buffer,
|
||||
sorted_request_buffer,
|
||||
request_count_buffer,
|
||||
@@ -810,6 +859,7 @@ where
|
||||
caching_pass,
|
||||
invalidation_pass,
|
||||
reset_pass,
|
||||
indirect_usage_copy_pass,
|
||||
buffer_compactor,
|
||||
sorter,
|
||||
|
||||
@@ -876,55 +926,112 @@ where
|
||||
], Some("VoxelCache::structure_table_bind_group"))
|
||||
}
|
||||
|
||||
pub fn cache_post_render(&mut self, encoder: &mut CommandEncoder, request_interface: &CacheProducerInterface<N>)
|
||||
pub fn cache_post_render(&mut self, encoder: &mut ScopeEncoder, request_interface: &CacheProducerInterface<N>)
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "cache_post_render");
|
||||
// Sort request buffer
|
||||
{
|
||||
self.buffer_compactor
|
||||
.compact_buffer(
|
||||
&self.device,
|
||||
encoder,
|
||||
&self.request_buffer,
|
||||
&self.sorted_request_buffer,
|
||||
Some(&self.cache_scratch_buffer)
|
||||
);
|
||||
self.buffer_compactor.copy_count_to_buffer(encoder, &self.request_count_buffer);
|
||||
gpu_profiler::begin_scope!(encoder, "request_sort");
|
||||
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "compaction");
|
||||
self.buffer_compactor
|
||||
.compact_buffer(
|
||||
&self.device,
|
||||
&mut encoder,
|
||||
&self.request_buffer,
|
||||
&self.sorted_request_buffer,
|
||||
Some(&self.cache_scratch_buffer)
|
||||
);
|
||||
self.buffer_compactor.copy_count_to_buffer(&mut encoder, &self.request_count_buffer);
|
||||
}
|
||||
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "radix_sort");
|
||||
self.sorter
|
||||
.sort(
|
||||
&self.device,
|
||||
&self.cache_scratch_buffer,
|
||||
&self.sorted_request_buffer,
|
||||
Some(&self.request_count_buffer),
|
||||
None,
|
||||
&mut encoder,
|
||||
radix_sort::RadixSortDirection::Descending
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Usage sorting
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "usage_sorting");
|
||||
// Copy depth to scratch to preserve during sort
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&self.depth_pool,
|
||||
0,
|
||||
&self.cache_scratch_buffer,
|
||||
0, (self.size * size_of::<u32>()) as u64);
|
||||
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "depth_radix");
|
||||
// First sort by depth
|
||||
// Deeper nodes are first now
|
||||
self.sorter
|
||||
.sort(
|
||||
&self.device,
|
||||
&self.cache_scratch_buffer,
|
||||
&self.sorted_request_buffer,
|
||||
Some(&self.request_count_buffer),
|
||||
None,
|
||||
encoder,
|
||||
&self.lru_list,
|
||||
None,
|
||||
Some(self.size),
|
||||
&mut encoder,
|
||||
radix_sort::RadixSortDirection::Descending
|
||||
);
|
||||
); // Lowest (oldest) first
|
||||
}
|
||||
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "indirect_copy");
|
||||
let mut indirect_usage_copy_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("VoxelCache::indirect_usage_copy_pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
indirect_usage_copy_pass.set_bind_group(0, Some(&self.structure_bind_group), &[]);
|
||||
indirect_usage_copy_pass.set_bind_group(1, Some(&self.management_bind_group), &[]);
|
||||
indirect_usage_copy_pass.set_bind_group(2, Some(&self.attributes_bind_group), &[]);
|
||||
indirect_usage_copy_pass.set_bind_group(3, Some(&self.structure_table_bind_group()), &[]);
|
||||
|
||||
indirect_usage_copy_pass.set_pipeline(&self.indirect_usage_copy_pass);
|
||||
indirect_usage_copy_pass.dispatch_workgroups(self.size.div_ceil(64) as u32, 1, 1);
|
||||
|
||||
drop(indirect_usage_copy_pass);
|
||||
}
|
||||
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "usage_radix");
|
||||
// Sort usage
|
||||
self.sorter
|
||||
.sort(
|
||||
&self.device,
|
||||
&self.cache_scratch_buffer,
|
||||
&self.lru_list,
|
||||
None,
|
||||
Some(self.size),
|
||||
&mut encoder,
|
||||
radix_sort::RadixSortDirection::Ascending
|
||||
); // Lowest (oldest) first
|
||||
}
|
||||
}
|
||||
|
||||
// Copy usage to scratch to preserve during sort
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&self.usage_buffer,
|
||||
0,
|
||||
&self.cache_scratch_buffer,
|
||||
0, (self.size * size_of::<u32>()) as u64);
|
||||
|
||||
// Sort usage
|
||||
self.sorter
|
||||
.sort(
|
||||
&self.device,
|
||||
&self.cache_scratch_buffer,
|
||||
&self.lru_list,
|
||||
None,
|
||||
Some(self.size),
|
||||
encoder,
|
||||
radix_sort::RadixSortDirection::Ascending
|
||||
); // Lowest (oldest) first
|
||||
|
||||
self.structure_table
|
||||
.request_buffer_mut()
|
||||
.sort_requests(encoder);
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "structure_table");
|
||||
self.structure_table
|
||||
.request_buffer_mut()
|
||||
.sort_requests(&mut encoder);
|
||||
}
|
||||
|
||||
// Sorted requests are now in the group
|
||||
gpu_profiler::begin_scope!(encoder, "request_pass");
|
||||
let mut request_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("VoxelCache::write_requests_pass"),
|
||||
timestamp_writes: None,
|
||||
@@ -946,7 +1053,7 @@ where
|
||||
|
||||
self.structure_table
|
||||
.request_buffer_mut()
|
||||
.reset_requests(encoder);
|
||||
.reset_requests(&mut encoder);
|
||||
}
|
||||
|
||||
pub fn current_timestamp(&self) -> u32
|
||||
|
||||
Reference in New Issue
Block a user