Adding attribute system to the cache
This commit is contained in:
+69
-35
@@ -1,3 +1,5 @@
|
||||
use std::num::NonZero;
|
||||
|
||||
use bytemuck::Zeroable;
|
||||
use wgpu::{BindGroup, BindGroupLayout, Buffer, BufferUsages, CommandEncoder, ComputePipeline, Device, Queue, ShaderStages};
|
||||
|
||||
@@ -18,8 +20,8 @@ pub struct VoxelCache<const N: usize>
|
||||
device: Device,
|
||||
queue: Queue,
|
||||
structure_pool: Buffer,
|
||||
color_pool: Buffer,
|
||||
location_pool: Buffer,
|
||||
attributes_pools: Vec<Buffer>,
|
||||
|
||||
pub structure_table: StructureTable,
|
||||
|
||||
@@ -41,6 +43,12 @@ pub struct VoxelCache<const N: usize>
|
||||
pub usage_buffer: UsageBuffer,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct VoxelAttribute
|
||||
{
|
||||
pub word_count: u32
|
||||
}
|
||||
|
||||
|
||||
#[derive(Zeroable, bytemuck::Pod, Clone, Copy)]
|
||||
#[repr(C)]
|
||||
@@ -60,7 +68,7 @@ impl<const N: usize> VoxelCache<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(cache_size: usize, device: Device, queue: Queue) -> Self
|
||||
pub fn new(cache_size: usize, device: Device, queue: Queue, attributes: Vec<VoxelAttribute>) -> Self
|
||||
{
|
||||
let structure_pool = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Structure pool"),
|
||||
@@ -76,17 +84,23 @@ where
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let color_pool = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Color pool"),
|
||||
size: (size_of::<ColorPoolElement<N>>() * cache_size) as u64,
|
||||
usage: BufferUsages::STORAGE,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
// Create attribttues
|
||||
let attributes_pools =
|
||||
attributes.iter().enumerate()
|
||||
.map(|(i, x)|
|
||||
device.create_buffer(&wgpu::BufferDescriptor
|
||||
{
|
||||
label: Some(format!("VoxelCache<{N}>::attributes[{i}]").as_str()),
|
||||
size: ((x.word_count as usize) * N * N * N * cache_size * size_of::<u32>()) as u64,
|
||||
usage: BufferUsages::STORAGE,
|
||||
mapped_at_creation: false,
|
||||
})
|
||||
).collect::<Vec<_>>();
|
||||
|
||||
let request_buffer = RequestBuffer::new(cache_size, device.clone());
|
||||
let usage_buffer = UsageBuffer::new(cache_size, device.clone());
|
||||
|
||||
let request_interface_bind_group_layout = CacheProducerInterface::<N>::request_side_bind_group_layout(&device);
|
||||
let request_interface_bind_group_layout = CacheProducerInterface::<N>::request_side_bind_group_layout(&device, &attributes);
|
||||
|
||||
// Rendering bing group layouts
|
||||
let voxel_cache_render_bind_group_layout =
|
||||
@@ -104,7 +118,7 @@ where
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// Color pool
|
||||
// Attributes pool
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
@@ -113,7 +127,7 @@ where
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
count: Some(NonZero::new(attributes.len() as u32).unwrap()),
|
||||
},
|
||||
// Location pool
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
@@ -191,7 +205,7 @@ where
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// Color pool
|
||||
// Attributes pool
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
@@ -200,7 +214,7 @@ where
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
count: Some(NonZero::new(attributes.len() as u32).unwrap()),
|
||||
},
|
||||
// Location pool
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
@@ -260,6 +274,7 @@ where
|
||||
],
|
||||
});
|
||||
|
||||
let attributes_bindings = attributes_pools.iter().map(|x| x.as_entire_buffer_binding()).collect::<Vec<wgpu::BufferBinding<'_>>>();
|
||||
let voxel_cache_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("voxel_cache_bind_group"),
|
||||
layout: &voxel_cache_bind_group_layout,
|
||||
@@ -269,10 +284,12 @@ where
|
||||
binding: 0,
|
||||
resource: structure_pool.as_entire_binding(),
|
||||
},
|
||||
// Color pool
|
||||
// Attributes pool
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: color_pool.as_entire_binding(),
|
||||
resource: wgpu::BindingResource::BufferArray(
|
||||
attributes_bindings.as_slice()
|
||||
),
|
||||
},
|
||||
// Location pool
|
||||
wgpu::BindGroupEntry {
|
||||
@@ -343,8 +360,11 @@ where
|
||||
});
|
||||
|
||||
let children_count = N*N*N;
|
||||
let attribute_count = attributes.len();
|
||||
let wgsl_bindings =
|
||||
format!("
|
||||
enable wgpu_binding_array;
|
||||
|
||||
struct StructurePoolElement
|
||||
{{
|
||||
occupancy_low: atomic<u32>,
|
||||
@@ -369,8 +389,13 @@ where
|
||||
child: u32
|
||||
}}
|
||||
|
||||
struct AttributeArray
|
||||
{{
|
||||
values: array<u32>,
|
||||
}}
|
||||
|
||||
@group(0) @binding(0) var<storage, read_write> structure_pool: array<StructurePoolElement>;
|
||||
@group(0) @binding(1) var<storage, read_write> color_pool: array<ColorPoolElement>;
|
||||
@group(0) @binding(1) var<storage, read_write> attributes_pools: binding_array<AttributeArray, {attribute_count}>;
|
||||
@group(0) @binding(2) var<storage, read_write> location_pool: array<LocationPoolElement>;
|
||||
@group(0) @binding(3) var<storage, read> sorted_requests: array<u32>;
|
||||
@group(0) @binding(4) var<storage, read> lru_list: array<u32>;
|
||||
@@ -495,7 +520,7 @@ where
|
||||
|
||||
let caching_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("caching_pipelien_layout"),
|
||||
label: Some("caching_pipeline_layout"),
|
||||
bind_group_layouts: &[
|
||||
Some(&voxel_cache_bind_group_layout),
|
||||
Some(&structure_table_bind_group_layout),
|
||||
@@ -504,6 +529,23 @@ where
|
||||
immediate_size: size_of::<CachingPipelineImmediates>() as u32, // Current frame timestamp
|
||||
});
|
||||
|
||||
// Generate attributes copy loops
|
||||
let mut copy_loops = String::new();
|
||||
for (attribute_index, attribute) in attributes.iter().enumerate()
|
||||
{
|
||||
let node_size = attribute.word_count * children_count as u32;
|
||||
let word_count = attribute.word_count;
|
||||
copy_loops += format!("
|
||||
for(var i = 0u; i < {children_count}; i++){{
|
||||
for(var j = 0u; j < {word_count}; j++){{
|
||||
let offset = i * {word_count} + j;
|
||||
attributes_pools[{attribute_index}].values[overwritten_element * {node_size} + offset]
|
||||
= attributes_nodes[{attribute_index}].values[index * {node_size} + offset];
|
||||
}}
|
||||
}}
|
||||
").as_str();
|
||||
}
|
||||
|
||||
let children_count = N * N * N;
|
||||
let caching_pipeline =
|
||||
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
@@ -513,6 +555,7 @@ where
|
||||
label: Some("caching_pipeline_shader_module"),
|
||||
source: wgpu::ShaderSource::Wgsl(
|
||||
format!("
|
||||
{wgsl_bindings}
|
||||
|
||||
struct DestinationElement
|
||||
{{
|
||||
@@ -526,7 +569,6 @@ where
|
||||
write_pointers: u32
|
||||
}}
|
||||
|
||||
{wgsl_bindings}
|
||||
|
||||
var<immediate> parameters: CachingPipelineImmediates;
|
||||
|
||||
@@ -548,7 +590,7 @@ where
|
||||
@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(4) var<storage, read_write> attributes_nodes: binding_array<AttributeArray, {attribute_count}>;
|
||||
@group(2) @binding(5) var<storage, read_write> locations: array<LocationPoolElement>;
|
||||
|
||||
@compute
|
||||
@@ -571,18 +613,18 @@ where
|
||||
{{
|
||||
// Phase 1
|
||||
// Copy into cache page
|
||||
var occupancy_high = u32(0);
|
||||
var occupancy_high = u32(0);
|
||||
var occupancy_low = u32(0);
|
||||
for(var i = 0; i < {children_count}; i++)
|
||||
{{
|
||||
structure_pool[overwritten_element].pointers[i] = select(u32(0), u32(1)<<31, structure_nodes[index].pointers[i] != 0);
|
||||
structure_pool[overwritten_element].pointers[i] =
|
||||
select(u32(0), u32(1)<<31, (structure_nodes[index].pointers[i] >> 31) != 0);
|
||||
|
||||
let turn_on_bit =
|
||||
select(
|
||||
u32(0),
|
||||
u32(u32(1) << u32(i % ({children_count} / 2))),
|
||||
((color_nodes[index].colors[i] >> 24) != 0) ||
|
||||
structure_nodes[index].pointers[i] != 0
|
||||
((structure_nodes[index].pointers[i] >> 30) & 1) != 0
|
||||
);
|
||||
|
||||
if(i > {children_count} / 2)
|
||||
@@ -596,7 +638,7 @@ where
|
||||
atomicStore(&structure_pool[overwritten_element].occupancy_low, occupancy_low);
|
||||
atomicStore(&structure_pool[overwritten_element].occupancy_high, occupancy_high);
|
||||
|
||||
color_pool[overwritten_element] = color_nodes[index];
|
||||
{copy_loops}
|
||||
location_pool[overwritten_element] = locations[index];
|
||||
|
||||
// Mark dirty/correct timestamp
|
||||
@@ -616,15 +658,6 @@ where
|
||||
}}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;
|
||||
|
||||
let turn_on_bit = u32(1 << (requests_wb[index].child_index % ({children_count} / 2)));
|
||||
if(requests_wb[index].child_index > {children_count} / 2)
|
||||
{{
|
||||
//atomicOr(&structure_pool[requests_wb[index].node_index].occupancy_high, turn_on_bit);
|
||||
}}else
|
||||
{{
|
||||
//atomicOr(&structure_pool[requests_wb[index].node_index].occupancy_low, turn_on_bit);
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
|
||||
@@ -725,7 +758,7 @@ where
|
||||
queue,
|
||||
device,
|
||||
structure_pool,
|
||||
color_pool,
|
||||
attributes_pools,
|
||||
location_pool,
|
||||
request_buffer,
|
||||
usage_buffer,
|
||||
@@ -752,6 +785,7 @@ where
|
||||
|
||||
pub fn bind_group(&self) -> BindGroup
|
||||
{
|
||||
let attributes_bindings = self.attributes_pools.iter().map(|x| x.as_entire_buffer_binding()).collect::<Vec<wgpu::BufferBinding<'_>>>();
|
||||
self.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("voxel_cache_render_bind_group"),
|
||||
layout: &self.voxel_cache_render_bind_group_layout,
|
||||
@@ -764,7 +798,7 @@ where
|
||||
// Color pool
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: self.color_pool.as_entire_binding(),
|
||||
resource: wgpu::BindingResource::BufferArray(attributes_bindings.as_slice()),
|
||||
},
|
||||
// Location pool
|
||||
wgpu::BindGroupEntry {
|
||||
|
||||
Reference in New Issue
Block a user