Voxel cache rewrite
This commit is contained in:
+208
-29
@@ -1,3 +1,5 @@
|
||||
use std::num::NonZero;
|
||||
|
||||
use wgpu::CommandEncoder;
|
||||
use wgpu::Device;
|
||||
|
||||
@@ -24,6 +26,9 @@ pub mod pipelines;
|
||||
pub mod requests;
|
||||
pub mod schema;
|
||||
pub mod structure;
|
||||
pub mod tables;
|
||||
|
||||
pub use layout::*;
|
||||
|
||||
/// Stores some ressources that can be reused throughout the modules of the cache
|
||||
struct CacheRessources<const N: usize>
|
||||
@@ -38,7 +43,7 @@ impl<const N: usize> CacheRessources<N>
|
||||
{
|
||||
let scratch_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheRessources::scratch_buffer"),
|
||||
usage: wgpu::BufferUsages::STORAGE,
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
size: (size_of::<u32>() * page_count * N * N * N) as u64,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
@@ -70,14 +75,29 @@ where
|
||||
cache_invalidation_pass: CacheInvalidationPass<N>,
|
||||
write_pointers_pass: WritePointersPass<N>,
|
||||
|
||||
// Raytracing
|
||||
raytrace_bind_group_layout: wgpu::BindGroupLayout,
|
||||
raytrace_bind_group: wgpu::BindGroup,
|
||||
|
||||
frame_timestamp: u32,
|
||||
}
|
||||
|
||||
pub struct VoxelCacheWriter<'cache, const N: usize>
|
||||
pub trait CacheInvalidable
|
||||
{
|
||||
fn invalidate(
|
||||
&mut self,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
cache_evictor: &CacheEvictor,
|
||||
invalid_timestamp: u32,
|
||||
);
|
||||
}
|
||||
|
||||
pub enum VoxelCacheInsertion<'a, const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
cache: &'cache mut VoxelCache<N>,
|
||||
ExternalRequest(&'a CacheInsertionData<N>, &'a mut dyn CacheInvalidable),
|
||||
InternalRequest(&'a CacheInsertionData<N>, &'a CacheRequestBuffer),
|
||||
}
|
||||
|
||||
impl<const N: usize> VoxelCache<N>
|
||||
@@ -97,6 +117,14 @@ where
|
||||
let cache_invalidation_pass = CacheInvalidationPass::new(device, page_count, layout);
|
||||
let write_pointers_pass = WritePointersPass::new(device, page_count, layout);
|
||||
|
||||
let (raytrace_bind_group_layout, raytrace_bind_group) = Self::create_raytrace_bind_group(
|
||||
device,
|
||||
&structure,
|
||||
&attributes,
|
||||
&request_handler,
|
||||
&evictor,
|
||||
);
|
||||
|
||||
Self {
|
||||
page_count,
|
||||
structure,
|
||||
@@ -110,6 +138,9 @@ where
|
||||
cache_invalidation_pass,
|
||||
write_pointers_pass,
|
||||
|
||||
raytrace_bind_group_layout,
|
||||
raytrace_bind_group,
|
||||
|
||||
frame_timestamp: 0,
|
||||
}
|
||||
}
|
||||
@@ -125,8 +156,6 @@ where
|
||||
|
||||
self.request_handler
|
||||
.sort_requests(device, &self.common_ressources, &mut encoder);
|
||||
self.evictor
|
||||
.prepare_eviction_list(device, &self.common_ressources, &mut encoder);
|
||||
|
||||
self.write_requests_pass.encode(
|
||||
&mut encoder,
|
||||
@@ -135,42 +164,192 @@ where
|
||||
&self.request_handler,
|
||||
request_buffer,
|
||||
);
|
||||
|
||||
self.request_handler.reset(&mut encoder);
|
||||
}
|
||||
|
||||
pub fn write_invalidate<F: FnMut(&VoxelCacheWriter<N>)>(
|
||||
/// Inserts new pages into the cache. Two insertion modes are available:
|
||||
///
|
||||
/// External: Inserts brand new pages, regardless of the misses in the cache. This can be used
|
||||
/// to bring external requests into the cache
|
||||
///
|
||||
/// Internal: This is the response to requests emited inside the cache (gotten through the
|
||||
/// gather requests method)
|
||||
pub fn insert(
|
||||
&mut self,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
mut write_ops: F,
|
||||
)
|
||||
{
|
||||
let writer = VoxelCacheWriter { cache: self };
|
||||
write_ops(&writer);
|
||||
|
||||
self.cache_invalidation_pass.encode(
|
||||
encoder,
|
||||
&self.structure,
|
||||
&self.evictor,
|
||||
self.frame_timestamp,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn write_pointers(
|
||||
&self,
|
||||
device: &Device,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
request_buffer: &CacheRequestBuffer,
|
||||
insertion: &CacheInsertionData<N>,
|
||||
insertions: &mut [&mut VoxelCacheInsertion<N>],
|
||||
)
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "insert_pages");
|
||||
gpu_profiler::begin_scope!(encoder, "insertion");
|
||||
let new_timestamp = self.frame_timestamp + 1;
|
||||
|
||||
self.write_pointers_pass.encode(
|
||||
self.evictor
|
||||
.prepare_eviction_list(device, &self.common_ressources, &mut encoder);
|
||||
|
||||
for insertion in insertions.iter()
|
||||
{
|
||||
match insertion
|
||||
{
|
||||
VoxelCacheInsertion::ExternalRequest(cache_insertion_data, _)
|
||||
| VoxelCacheInsertion::InternalRequest(cache_insertion_data, _) =>
|
||||
{
|
||||
self.cache_insertion_pass.encode(
|
||||
device,
|
||||
&mut encoder,
|
||||
&self.attributes,
|
||||
&self.structure,
|
||||
cache_insertion_data,
|
||||
&self.evictor,
|
||||
new_timestamp,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate external structures pointing inside the cache
|
||||
for insertion in insertions.iter_mut()
|
||||
{
|
||||
if let VoxelCacheInsertion::ExternalRequest(_, invalidable) = insertion
|
||||
{
|
||||
invalidable.invalidate(&mut encoder, &self.evictor, new_timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate cache pages
|
||||
self.cache_invalidation_pass.encode(
|
||||
&mut encoder,
|
||||
device,
|
||||
&self.structure,
|
||||
request_buffer,
|
||||
&self.evictor,
|
||||
new_timestamp,
|
||||
);
|
||||
|
||||
// Write internal requests pointers
|
||||
for insertion in insertions.iter_mut()
|
||||
{
|
||||
if let VoxelCacheInsertion::InternalRequest(insertion, request) = insertion
|
||||
{
|
||||
self.write_pointers_pass.encode(
|
||||
&mut encoder,
|
||||
device,
|
||||
&self.structure,
|
||||
request,
|
||||
insertion,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Proceed to new frame
|
||||
self.frame_timestamp = new_timestamp;
|
||||
}
|
||||
|
||||
pub fn current_timestamp(&self) -> u32
|
||||
{
|
||||
self.frame_timestamp
|
||||
}
|
||||
|
||||
fn create_raytrace_bind_group(
|
||||
device: &wgpu::Device,
|
||||
structure: &CacheStructure<N>,
|
||||
attributes: &CacheAttributes<N>,
|
||||
request_handler: &CacheRequestHandler<N>,
|
||||
evictor: &CacheEvictor,
|
||||
) -> (wgpu::BindGroupLayout, wgpu::BindGroup)
|
||||
{
|
||||
let attribute_count = attributes.attribute_buffers().len();
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("VoxelCache::raytrace_bind_group_layout"),
|
||||
entries: &[
|
||||
// Structure pointers
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// Attributes
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: Some(
|
||||
NonZero::new(attribute_count as u32)
|
||||
.expect("Null amount of cache attributes."),
|
||||
),
|
||||
},
|
||||
// Request pages
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 2,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// Usage pages
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 3,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let attributes_bindings = attributes
|
||||
.attribute_buffers()
|
||||
.iter()
|
||||
.map(|buffer| buffer.as_entire_buffer_binding())
|
||||
.collect::<Vec<_>>();
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("VoxelCache::raytrace_bind_group"),
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: structure.pointer_pages().as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::BufferArray(&attributes_bindings),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: request_handler.request_pages_buffer().as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 3,
|
||||
resource: evictor.timestamp_pages_buffer().as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
(bind_group_layout, bind_group)
|
||||
}
|
||||
|
||||
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout
|
||||
{
|
||||
&self.raytrace_bind_group_layout
|
||||
}
|
||||
|
||||
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup
|
||||
{
|
||||
&self.raytrace_bind_group
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user