415 lines
13 KiB
Rust
415 lines
13 KiB
Rust
use std::num::NonZero;
|
|
|
|
use wgpu::CommandEncoder;
|
|
use wgpu::Device;
|
|
|
|
use crate::gpu::radix_sort::RadixSorter;
|
|
use crate::gpu_profiler;
|
|
use crate::gpu_profiler::ScopeEncoder;
|
|
use crate::voxel_cache::attributes::CacheAttributes;
|
|
use crate::voxel_cache::eviction::CacheEvictor;
|
|
use crate::voxel_cache::insertion::CacheInsertionData;
|
|
use crate::voxel_cache::layout::CacheLayout;
|
|
use crate::voxel_cache::pipelines::CacheInsertionPass;
|
|
use crate::voxel_cache::pipelines::CacheInvalidationPass;
|
|
use crate::voxel_cache::pipelines::WritePointersPass;
|
|
use crate::voxel_cache::pipelines::WriteRequestsPass;
|
|
use crate::voxel_cache::requests::CacheRequestBuffer;
|
|
use crate::voxel_cache::requests::CacheRequestHandler;
|
|
use crate::voxel_cache::structure::CacheStructure;
|
|
|
|
pub mod attributes;
|
|
pub mod eviction;
|
|
pub mod insertion;
|
|
pub mod layout;
|
|
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>
|
|
{
|
|
scratch_buffer: wgpu::Buffer,
|
|
radix_sorter: RadixSorter,
|
|
}
|
|
|
|
impl<const N: usize> CacheRessources<N>
|
|
{
|
|
pub fn new(device: &Device, page_count: usize) -> Self
|
|
{
|
|
let scratch_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
|
label: Some("CacheRessources::scratch_buffer"),
|
|
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
|
size: (size_of::<u32>() * page_count * N * N * N) as u64,
|
|
mapped_at_creation: false,
|
|
});
|
|
|
|
let radix_sorter = RadixSorter::new(device, page_count * N * N * N);
|
|
|
|
Self {
|
|
scratch_buffer,
|
|
radix_sorter,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct VoxelCache<const N: usize>
|
|
where
|
|
[(); N * N * N]:,
|
|
{
|
|
page_count: usize,
|
|
structure: CacheStructure<N>,
|
|
attributes: CacheAttributes<N>,
|
|
request_handler: CacheRequestHandler<N>,
|
|
evictor: CacheEvictor,
|
|
|
|
common_ressources: CacheRessources<N>,
|
|
|
|
// Operations
|
|
write_requests_pass: WriteRequestsPass<N>,
|
|
cache_insertion_pass: CacheInsertionPass<N>,
|
|
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 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]:,
|
|
{
|
|
ExternalRequest(&'a CacheInsertionData<N>, &'a mut dyn CacheInvalidable),
|
|
InternalRequest(&'a CacheInsertionData<N>, &'a CacheRequestBuffer),
|
|
}
|
|
|
|
impl<const N: usize> VoxelCache<N>
|
|
where
|
|
[(); N * N * N]:,
|
|
{
|
|
pub fn new(device: &Device, page_count: usize, layout: &CacheLayout) -> Self
|
|
{
|
|
let structure = CacheStructure::new(device, page_count);
|
|
let attributes = CacheAttributes::from_cache_layout(device, layout, page_count);
|
|
let request_handler = CacheRequestHandler::new(device, page_count);
|
|
let evictor = CacheEvictor::new(device, page_count, 0u32);
|
|
let common_ressources = CacheRessources::new(device, page_count);
|
|
|
|
let write_requests_pass = WriteRequestsPass::new(device, page_count, layout);
|
|
let cache_insertion_pass = CacheInsertionPass::new(device, page_count, layout);
|
|
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,
|
|
attributes,
|
|
request_handler,
|
|
evictor,
|
|
common_ressources,
|
|
|
|
write_requests_pass,
|
|
cache_insertion_pass,
|
|
cache_invalidation_pass,
|
|
write_pointers_pass,
|
|
|
|
raytrace_bind_group_layout,
|
|
raytrace_bind_group,
|
|
|
|
frame_timestamp: 0,
|
|
}
|
|
}
|
|
|
|
pub fn gather_requests(
|
|
&self,
|
|
device: &Device,
|
|
encoder: &mut ScopeEncoder<CommandEncoder>,
|
|
request_buffer: &CacheRequestBuffer,
|
|
)
|
|
{
|
|
gpu_profiler::begin_scope!(encoder, "gather_requests");
|
|
|
|
self.request_handler
|
|
.sort_requests(device, &self.common_ressources, &mut encoder);
|
|
|
|
self.write_requests_pass.encode(
|
|
&mut encoder,
|
|
device,
|
|
&self.structure,
|
|
&self.request_handler,
|
|
request_buffer,
|
|
);
|
|
|
|
self.request_handler.reset(&mut encoder);
|
|
}
|
|
|
|
/// 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,
|
|
device: &Device,
|
|
encoder: &mut ScopeEncoder<CommandEncoder>,
|
|
insertions: &mut [&mut VoxelCacheInsertion<N>],
|
|
)
|
|
{
|
|
gpu_profiler::begin_scope!(encoder, "insertion");
|
|
let new_timestamp = self.frame_timestamp + 1;
|
|
|
|
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,
|
|
&self.structure,
|
|
&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
|
|
}
|
|
}
|
|
|
|
// Utilities
|
|
fn create_multi_buffers_bind_group_layout(
|
|
device: &Device,
|
|
count: u32,
|
|
label: Option<&str>,
|
|
) -> wgpu::BindGroupLayout
|
|
{
|
|
let entries = (0..count)
|
|
.map(|i| wgpu::BindGroupLayoutEntry {
|
|
binding: i,
|
|
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,
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
label,
|
|
entries: &entries,
|
|
})
|
|
}
|
|
|
|
fn create_multi_buffers_bind_group(
|
|
device: &Device,
|
|
layout: &wgpu::BindGroupLayout,
|
|
buffers: &[&wgpu::Buffer],
|
|
label: Option<&str>,
|
|
) -> wgpu::BindGroup
|
|
{
|
|
let entries = buffers
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(i, buffer)| wgpu::BindGroupEntry {
|
|
binding: i as u32,
|
|
resource: buffer.as_entire_binding(),
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
label,
|
|
layout,
|
|
entries: &entries,
|
|
})
|
|
}
|
|
|
|
fn create_multi_buffers_bg_bg_layout(
|
|
device: &Device,
|
|
buffers: &[&wgpu::Buffer],
|
|
label: Option<&str>,
|
|
) -> (wgpu::BindGroupLayout, wgpu::BindGroup)
|
|
{
|
|
let layout = create_multi_buffers_bind_group_layout(device, buffers.len() as u32, label);
|
|
let bg = create_multi_buffers_bind_group(device, &layout, buffers, label);
|
|
(layout, bg)
|
|
}
|