feature: add a producer_interface struct to cleanly represent the $ interface
This commit is contained in:
+70
-212
@@ -1,12 +1,13 @@
|
||||
use bytemuck::Zeroable;
|
||||
use wgpu::{BindGroup, BindGroupLayout, Buffer, BufferUsages, ComputePipeline, Device, Queue, ShaderStages, CommandEncoder};
|
||||
use wgpu::{BindGroup, BindGroupLayout, Buffer, BufferUsages, CommandEncoder, ComputePipeline, Device, Queue, ShaderStages};
|
||||
|
||||
use crate::voxel_cache::{data::{CacheNodeRequest, CacheResponse, ColorPoolElement, LocationPoolElement, StructurePoolElement}, request_buffer::RequestBuffer, structure_table::StructureTable, usage_buffer::UsageBuffer};
|
||||
use crate::voxel_cache::{data::{CacheNodeRequest, CacheResponse, ColorPoolElement, LocationPoolElement, StructurePoolElement}, producer_interface::CacheProducerInterface, request_buffer::RequestBuffer, structure_table::StructureTable, usage_buffer::UsageBuffer};
|
||||
|
||||
|
||||
pub mod request_buffer;
|
||||
pub mod structure_table;
|
||||
pub mod usage_buffer;
|
||||
pub mod producer_interface;
|
||||
|
||||
pub mod data;
|
||||
|
||||
@@ -28,12 +29,9 @@ pub struct VoxelCache<const N: usize>
|
||||
voxel_cache_render_bind_group_layout: BindGroupLayout,
|
||||
|
||||
// Writing requests
|
||||
request_write_count_buffer: Buffer,
|
||||
request_write_bindgroup_layout_user: BindGroupLayout,
|
||||
request_write_pipeline: ComputePipeline,
|
||||
|
||||
// User response -> caching
|
||||
caching_user_bindgroup_layout: BindGroupLayout,
|
||||
caching_pipeline: ComputePipeline,
|
||||
|
||||
// Invalidation
|
||||
@@ -87,12 +85,7 @@ where
|
||||
let request_buffer = RequestBuffer::new(cache_size, device.clone());
|
||||
let usage_buffer = UsageBuffer::new(cache_size, device.clone());
|
||||
|
||||
let request_write_count_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("total_request_count_buffer"),
|
||||
size: size_of::<u32>() as u64,
|
||||
usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let request_interface_bind_group_layout = CacheProducerInterface::<N>::request_side_bind_group_layout(&device);
|
||||
|
||||
// Rendering bing group layouts
|
||||
let voxel_cache_render_bind_group_layout =
|
||||
@@ -263,17 +256,6 @@ where
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// total request count
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 7,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -316,11 +298,6 @@ where
|
||||
binding: 6,
|
||||
resource: request_buffer.request_count_buffer().as_entire_binding(),
|
||||
},
|
||||
// Total request count
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 7,
|
||||
resource: request_write_count_buffer.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -396,39 +373,19 @@ where
|
||||
@group(0) @binding(4) var<storage, read> lru_list: array<u32>;
|
||||
@group(0) @binding(5) var<storage, read_write> usage_buffer: array<u32>;
|
||||
@group(0) @binding(6) var<storage, read> pools_request_count: u32;
|
||||
@group(0) @binding(7) var<storage, read_write> total_request_count: u32;
|
||||
|
||||
@group(1) @binding(0) var<storage, read_write> structure_table_pointers: array<u32>;
|
||||
@group(1) @binding(1) var<storage, read_write> structure_table_sorted_requests: array<SortedRequestsElement>;
|
||||
@group(1) @binding(2) var<storage, read_write> structure_table_request_count: u32;
|
||||
");
|
||||
|
||||
|
||||
let write_requests_bindgroup_layout_user =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("write_requests_bindgroup_layout_user"),
|
||||
entries: &[
|
||||
// User target
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let write_requests_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("write_requests_pipeline_layout"),
|
||||
bind_group_layouts: &[
|
||||
Some(&voxel_cache_bind_group_layout),
|
||||
Some(&structure_table_bind_group_layout),
|
||||
Some(&write_requests_bindgroup_layout_user),
|
||||
Some(&request_interface_bind_group_layout),
|
||||
],
|
||||
immediate_size: 0,
|
||||
});
|
||||
@@ -444,18 +401,26 @@ where
|
||||
format!("
|
||||
{wgsl_bindings}
|
||||
|
||||
struct CacheNodeRequest
|
||||
struct CacheInterfaceRequest
|
||||
{{
|
||||
// Requested ressource
|
||||
structure_id: u32,
|
||||
structure_locator: u32,
|
||||
|
||||
// Write back info
|
||||
node_index: u32,
|
||||
child_index: u32,
|
||||
locator: u32,
|
||||
child_index: u32
|
||||
}}
|
||||
|
||||
@group(2) @binding(0) var<storage, read_write> requests: array<CacheNodeRequest>;
|
||||
struct CacheInterfaceRequestWb
|
||||
{{
|
||||
node_index: u32,
|
||||
child_index: u32
|
||||
}}
|
||||
|
||||
@group(2) @binding(0) var<storage, read_write> requests: array<CacheInterfaceRequest>;
|
||||
@group(2) @binding(1) var<storage, read_write> requests_wb: array<CacheInterfaceRequestWb>;
|
||||
@group(2) @binding(2) var<storage, read_write> request_count: u32;
|
||||
|
||||
@group(2) @binding(3) var<storage, read> structure_nodes: array<StructurePoolElement>;
|
||||
@group(2) @binding(4) var<storage, read> color_nodes: array<ColorPoolElement>;
|
||||
@group(2) @binding(5) var<storage, read> locations: array<LocationPoolElement>;
|
||||
|
||||
@compute
|
||||
@workgroup_size(64)
|
||||
@@ -467,38 +432,45 @@ where
|
||||
// and cache domain
|
||||
var index = global_invocation_id.x;
|
||||
let max_requests_count = min(arrayLength(&requests), arrayLength(&lru_list));
|
||||
total_request_count = min(max_requests_count, pools_request_count + structure_table_request_count);
|
||||
if(index >= total_request_count)
|
||||
request_count = min(max_requests_count, pools_request_count + structure_table_request_count);
|
||||
if(index >= request_count)
|
||||
{{
|
||||
return;
|
||||
}}
|
||||
|
||||
if(index < structure_table_request_count)
|
||||
{{
|
||||
var request: CacheNodeRequest;
|
||||
var request: CacheInterfaceRequest;
|
||||
// Index in structure table IS structure id
|
||||
request.structure_id = structure_table_sorted_requests[index].node;
|
||||
request.structure_locator = 0; // Root request -> locator 0
|
||||
request.node_index = request.structure_id;
|
||||
request.locator = 0; // Root request -> locator 0
|
||||
request.child_index = 0xFFFFFFFF; // child index u32::MAX ->
|
||||
|
||||
var request_wb: CacheInterfaceRequestWb;
|
||||
request_wb.node_index = request.structure_id;
|
||||
request_wb.child_index = 0xFFFFFFFF; // child index u32::MAX ->
|
||||
// Structure table request
|
||||
// Write request
|
||||
requests[index] = request;
|
||||
requests_wb[index] = request_wb;
|
||||
return;
|
||||
}}
|
||||
|
||||
let pool_index = index - structure_table_request_count;
|
||||
var request: CacheNodeRequest;
|
||||
|
||||
// Request location comes from location pool
|
||||
var request: CacheInterfaceRequest;
|
||||
request.structure_id = location_pool[sorted_requests[pool_index].node].structure_id;
|
||||
request.structure_locator = location_pool[sorted_requests[pool_index].node].structure_locator;
|
||||
|
||||
request.node_index = sorted_requests[pool_index].node;
|
||||
request.locator = location_pool[sorted_requests[pool_index].node].structure_locator;
|
||||
request.child_index = sorted_requests[pool_index].child;
|
||||
|
||||
var request_wb: CacheInterfaceRequestWb;
|
||||
request_wb.node_index = sorted_requests[pool_index].node;
|
||||
request_wb.child_index = sorted_requests[pool_index].child;
|
||||
|
||||
// Write request
|
||||
requests[index] = request;
|
||||
requests_wb[index] = request_wb;
|
||||
|
||||
}}
|
||||
"
|
||||
@@ -515,72 +487,13 @@ where
|
||||
// Pipeline that takes user fulling ~some~ requests
|
||||
// and writes them to the cache based on the eviction list
|
||||
|
||||
// Caching pipeline binds
|
||||
// - Cache data
|
||||
//
|
||||
// - User response :
|
||||
// - response structure nodes
|
||||
// - response color nodes
|
||||
// - response locations
|
||||
// - response destinations
|
||||
let caching_user_bindgroup_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("caching_user_bindgroup_layout"),
|
||||
entries: &[
|
||||
// structure nodes
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// response color nodes
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// response locations
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 2,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// response destinations
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 3,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let caching_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("caching_pipelien_layout"),
|
||||
bind_group_layouts: &[
|
||||
Some(&voxel_cache_bind_group_layout),
|
||||
Some(&structure_table_bind_group_layout),
|
||||
Some(&caching_user_bindgroup_layout),
|
||||
Some(&request_interface_bind_group_layout),
|
||||
],
|
||||
immediate_size: size_of::<CachingPipelineImmediates>() as u32, // Current frame timestamp
|
||||
});
|
||||
@@ -611,10 +524,26 @@ where
|
||||
|
||||
var<immediate> parameters: CachingPipelineImmediates;
|
||||
|
||||
@group(2) @binding(0) var<storage, read> structure_nodes: array<StructurePoolElement>;
|
||||
@group(2) @binding(1) var<storage, read> color_nodes: array<ColorPoolElement>;
|
||||
@group(2) @binding(2) var<storage, read> locations: array<LocationPoolElement>;
|
||||
@group(2) @binding(3) var<storage, read> destinations: array<DestinationElement>;
|
||||
struct CacheInterfaceRequest
|
||||
{{
|
||||
structure_id: u32,
|
||||
locator: u32,
|
||||
child_index: u32
|
||||
}}
|
||||
|
||||
struct CacheInterfaceRequestWb
|
||||
{{
|
||||
node_index: u32,
|
||||
child_index: u32
|
||||
}}
|
||||
|
||||
@group(2) @binding(0) var<storage, read_write> requests: array<CacheInterfaceRequest>;
|
||||
@group(2) @binding(1) var<storage, read_write> requests_wb: array<CacheInterfaceRequestWb>;
|
||||
@group(2) @binding(2) var<storage, read_write> request_count: u32;
|
||||
|
||||
@group(2) @binding(3) var<storage, read_write> structure_nodes: array<StructurePoolElement>;
|
||||
@group(2) @binding(4) var<storage, read_write> color_nodes: array<ColorPoolElement>;
|
||||
@group(2) @binding(5) var<storage, read_write> locations: array<LocationPoolElement>;
|
||||
|
||||
@compute
|
||||
@workgroup_size(64)
|
||||
@@ -653,12 +582,12 @@ where
|
||||
|
||||
// Point parent to new page
|
||||
let new_pointer = (1 << 31) | (1 << 30) | overwritten_element;
|
||||
if(destinations[index].child == 0xFFFFFFFF)
|
||||
if(requests_wb[index].child_index == 0xFFFFFFFF)
|
||||
{{
|
||||
structure_table_pointers[destinations[index].node] = new_pointer;
|
||||
}}else if usage_buffer[destinations[index].node] != parameters.frame_timestamp + 1
|
||||
structure_table_pointers[requests_wb[index].node_index] = new_pointer;
|
||||
}}else if usage_buffer[requests_wb[index].node_index] != parameters.frame_timestamp + 1
|
||||
{{
|
||||
structure_pool[destinations[index].node].pointers[destinations[index].child] = new_pointer;
|
||||
structure_pool[requests_wb[index].node_index].pointers[requests_wb[index].child_index] = new_pointer;
|
||||
}}
|
||||
}}
|
||||
|
||||
@@ -769,12 +698,9 @@ where
|
||||
voxel_cache_render_bind_group_layout,
|
||||
|
||||
// Write requests stage
|
||||
request_write_bindgroup_layout_user: write_requests_bindgroup_layout_user,
|
||||
request_write_count_buffer,
|
||||
request_write_pipeline: write_requests,
|
||||
|
||||
// Caching operation
|
||||
caching_user_bindgroup_layout,
|
||||
caching_pipeline,
|
||||
|
||||
// Invalidation
|
||||
@@ -860,7 +786,7 @@ where
|
||||
})
|
||||
}
|
||||
|
||||
pub fn cache_post_render(&mut self, encoder: &mut CommandEncoder, request_target: Buffer)
|
||||
pub fn cache_post_render(&mut self, encoder: &mut CommandEncoder, request_interface: &CacheProducerInterface<N>)
|
||||
{
|
||||
// Sort requests, reset request buffers to count requests
|
||||
self.usage_buffer.sort_usage(encoder);
|
||||
@@ -871,18 +797,6 @@ where
|
||||
.sort_requests(encoder);
|
||||
|
||||
// Sorted requests are now in the group
|
||||
let user_target_bindgroup = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("write_requests_user_bindgroup"),
|
||||
layout: &self.request_write_bindgroup_layout_user,
|
||||
entries: &[
|
||||
// user target
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: request_target.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let mut write_requests_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("write_requests_pass"),
|
||||
timestamp_writes: None,
|
||||
@@ -890,11 +804,11 @@ where
|
||||
|
||||
write_requests_pass.set_bind_group(0, Some(&self.voxel_cache_bind_group), &[]);
|
||||
write_requests_pass.set_bind_group(1, Some(&self.structure_table_bind_group()), &[]);
|
||||
write_requests_pass.set_bind_group(2, Some(&user_target_bindgroup), &[]);
|
||||
write_requests_pass.set_bind_group(2, Some(&request_interface.request_side_bind_group), &[]);
|
||||
write_requests_pass.set_pipeline(&self.request_write_pipeline);
|
||||
|
||||
// Compute necessary shader invocations
|
||||
let shader_invocation_count = request_target.size() / size_of::<CacheNodeRequest>() as u64;
|
||||
let shader_invocation_count = request_interface.size;
|
||||
let workgroup_invocation_count = shader_invocation_count.div_ceil(64);
|
||||
write_requests_pass.dispatch_workgroups(workgroup_invocation_count as u32, 1, 1);
|
||||
drop(write_requests_pass);
|
||||
@@ -910,7 +824,7 @@ where
|
||||
self.usage_buffer.timestamp()
|
||||
}
|
||||
|
||||
pub fn cache_insert(&mut self, encoder: &mut CommandEncoder, insertion: &CacheResponse)
|
||||
pub fn cache_insert(&mut self, encoder: &mut CommandEncoder, cache_interface: &CacheProducerInterface<N>)
|
||||
{
|
||||
// Each of the entry of the cache insertion, is matched with the usage list to evict
|
||||
let mut cache_insertion_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
@@ -918,35 +832,6 @@ where
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
let user_caching_bindgroup = self.device.create_bind_group(&wgpu::BindGroupDescriptor
|
||||
{
|
||||
label: Some("user_caching_bind_group"),
|
||||
layout: &self.caching_user_bindgroup_layout,
|
||||
entries: &
|
||||
[
|
||||
wgpu::BindGroupEntry
|
||||
{
|
||||
binding: 0,
|
||||
resource: insertion.structure_nodes.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry
|
||||
{
|
||||
binding: 1,
|
||||
resource: insertion.color_nodes.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry
|
||||
{
|
||||
binding: 2,
|
||||
resource: insertion.locations.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry
|
||||
{
|
||||
binding: 3,
|
||||
resource: insertion.parents.as_entire_binding(),
|
||||
}
|
||||
],
|
||||
});
|
||||
|
||||
// Copy insertions to evicted lru
|
||||
// Point parents to children
|
||||
let structure_table_bind_group = self.structure_table_bind_group();
|
||||
@@ -956,7 +841,7 @@ where
|
||||
cache_insertion_pass.set_pipeline(&self.caching_pipeline);
|
||||
cache_insertion_pass.set_bind_group(0, Some(&self.voxel_cache_bind_group), &[]);
|
||||
cache_insertion_pass.set_bind_group(1, Some(&structure_table_bind_group), &[]);
|
||||
cache_insertion_pass.set_bind_group(2, Some(&user_caching_bindgroup), &[]);
|
||||
cache_insertion_pass.set_bind_group(2, Some(&cache_interface.request_side_bind_group), &[]);
|
||||
cache_insertion_pass.set_immediates(
|
||||
0,
|
||||
bytemuck::bytes_of(&CachingPipelineImmediates {
|
||||
@@ -966,8 +851,7 @@ where
|
||||
);
|
||||
|
||||
// Compute dispatch amounts
|
||||
let shader_invocation_count =
|
||||
insertion.structure_nodes.size() as usize / size_of::<StructurePoolElement<N>>();
|
||||
let shader_invocation_count = cache_interface.size;
|
||||
let workgroup_invocations = shader_invocation_count.div_ceil(64);
|
||||
|
||||
cache_insertion_pass.dispatch_workgroups(workgroup_invocations as u32, 1, 1);
|
||||
@@ -990,7 +874,7 @@ where
|
||||
// ~~~ Write new pointers ~~~
|
||||
{
|
||||
cache_insertion_pass.set_pipeline(&self.caching_pipeline);
|
||||
cache_insertion_pass.set_bind_group(2, Some(&user_caching_bindgroup), &[]);
|
||||
cache_insertion_pass.set_bind_group(2, Some(&cache_interface.request_side_bind_group), &[]);
|
||||
cache_insertion_pass.set_immediates(
|
||||
0,
|
||||
bytemuck::bytes_of(&CachingPipelineImmediates {
|
||||
@@ -1000,8 +884,7 @@ where
|
||||
);
|
||||
|
||||
// Compute dispatch amounts
|
||||
let shader_invocation_count =
|
||||
insertion.structure_nodes.size() as usize / size_of::<StructurePoolElement<N>>();
|
||||
let shader_invocation_count = cache_interface.size;
|
||||
let workgroup_invocations = shader_invocation_count.div_ceil(64);
|
||||
|
||||
cache_insertion_pass.dispatch_workgroups(workgroup_invocations as u32, 1, 1);
|
||||
@@ -1014,29 +897,4 @@ where
|
||||
self.usage_buffer.next_frame();
|
||||
}
|
||||
|
||||
pub fn total_request_count(&self) -> u32
|
||||
{
|
||||
let (tx, rx) = std::sync::mpsc::sync_channel(1);
|
||||
wgpu::util::DownloadBuffer::read_buffer(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
&self.request_write_count_buffer.slice(0..),
|
||||
move |download_buffer| {
|
||||
let vec = download_buffer.unwrap().to_vec();
|
||||
let value = u32::from_ne_bytes(std::array::from_fn(|i| vec[i]));
|
||||
let _ = tx.try_send(value);
|
||||
},
|
||||
);
|
||||
|
||||
loop
|
||||
{
|
||||
if let Ok(value) = rx.try_recv()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
self.device.poll(wgpu::wgt::PollType::Poll).unwrap();
|
||||
}
|
||||
|
||||
//panic!("Could not retrieve total request count.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user