VoxelCache refactor
This commit is contained in:
@@ -326,6 +326,7 @@ impl RadixSorter
|
||||
keys: &Buffer,
|
||||
values: &Buffer,
|
||||
indirect: Option<&Buffer>,
|
||||
direct_length: Option<usize>,
|
||||
encoder: &mut CommandEncoder,
|
||||
sort_direction: RadixSortDirection,
|
||||
)
|
||||
@@ -383,7 +384,7 @@ impl RadixSorter
|
||||
}
|
||||
else
|
||||
{
|
||||
self.max_size as u32
|
||||
direct_length.unwrap_or(self.max_size) as u32
|
||||
},
|
||||
descending: 0,
|
||||
digit_stride: 0,
|
||||
|
||||
+36
-14
@@ -64,6 +64,7 @@ use crate::voxel_cache::VoxelCache;
|
||||
use crate::voxel_cache::data::ColorBytes;
|
||||
use crate::voxel_cache::data::LocationPoolElement;
|
||||
use crate::voxel_cache::data::StructurePoolElement;
|
||||
use crate::voxel_cache::layout::VoxelCacheLayout;
|
||||
use crate::voxel_cache::producer_interface::CacheProducerInterface;
|
||||
use crate::voxel_cache::producer_interface::CacheRequest;
|
||||
|
||||
@@ -188,6 +189,7 @@ impl State
|
||||
max_immediate_size: 112,
|
||||
max_storage_buffers_per_shader_stage: 16,
|
||||
max_binding_array_elements_per_shader_stage: 16,
|
||||
max_bind_groups: 8,
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
@@ -203,7 +205,7 @@ impl State
|
||||
//let elt_count = 100_000;
|
||||
//let elt_count = (21 * 4096) as u32;
|
||||
//let elt_count = 3000 as u32;
|
||||
let elt_count_real = 1_000_000;
|
||||
let elt_count_real = 500_000;
|
||||
let count_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("count_buffer"),
|
||||
usage: wgpu::BufferUsages::STORAGE,
|
||||
@@ -215,8 +217,8 @@ impl State
|
||||
let org_big_value_buffer = (0..(elt_count as u32)).collect::<Vec<_>>();
|
||||
let org_big_key_buffer = org_big_value_buffer
|
||||
.iter()
|
||||
//.map(|_| random::<u32>())
|
||||
.map(|_| random_range(1..256))
|
||||
.map(|_| random::<u32>())
|
||||
//.map(|_| random_range(1..256))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// let mut prefix = org_big_key_buffer.iter().fold([0; 256], |acc, x| {
|
||||
@@ -330,13 +332,13 @@ impl State
|
||||
|
||||
let egui_renderer = EguiRenderer::new(&device, surface_format, &window);
|
||||
|
||||
let attributes = vec![
|
||||
VoxelAttribute { word_count: 1 },
|
||||
VoxelAttribute { word_count: 3 },
|
||||
];
|
||||
let attributes = VoxelCacheLayout::default()
|
||||
.add_attribute(voxel_cache::layout::VoxelCacheAttribute { word_count: 1 })
|
||||
.add_attribute(voxel_cache::layout::VoxelCacheAttribute { word_count: 3 });
|
||||
|
||||
let mut voxel_cache =
|
||||
VoxelCache::<4>::new(100_000, device.clone(), queue.clone(), attributes.clone());
|
||||
let cache_interface = CacheProducerInterface::new(1024, &device, attributes.clone());
|
||||
VoxelCache::<4>::new(100_000, device.clone(), queue.clone(), &attributes);
|
||||
let cache_interface = CacheProducerInterface::new(1024, &device, &attributes);
|
||||
|
||||
let terrain_generator = TerrainGenerator::<4>::new(5, "vxls_height.tif", 0.2, "img.jpg");
|
||||
//let terrain_generator = SdfGenerator::<4>::new(5);
|
||||
@@ -357,6 +359,9 @@ impl State
|
||||
let terrain_width = terrain_generator.chunk_width;
|
||||
let terrain_height = terrain_generator.chunk_height;
|
||||
let terrain_alt = terrain_generator.chunk_alt;
|
||||
// let terrain_width = 10;
|
||||
// let terrain_height = 10;
|
||||
// let terrain_alt = 10;
|
||||
println!(
|
||||
"Chunk count : {}",
|
||||
terrain_width * terrain_height * terrain_width
|
||||
@@ -568,7 +573,7 @@ impl State
|
||||
label: Some("Voxel pipeline layout"),
|
||||
|
||||
bind_group_layouts: &[
|
||||
Some(&voxel_cache.bind_group_layout()),
|
||||
Some(voxel_cache.user_bind_group_layout()),
|
||||
Some(&ray_march_bind_group_layout),
|
||||
],
|
||||
immediate_size: size_of::<Immediates>() as u32,
|
||||
@@ -590,7 +595,7 @@ impl State
|
||||
label: Some("Gi pipeline layout"),
|
||||
|
||||
bind_group_layouts: &[
|
||||
Some(&voxel_cache.bind_group_layout()),
|
||||
Some(&voxel_cache.user_bind_group_layout()),
|
||||
Some(&gi_bind_group_layout),
|
||||
],
|
||||
immediate_size: size_of::<Immediates>() as u32,
|
||||
@@ -960,7 +965,7 @@ impl State
|
||||
});
|
||||
|
||||
renderpass.set_pipeline(&self.pipeline);
|
||||
renderpass.set_bind_group(0, Some(&self.voxel_cache.lock().bind_group()), &[]);
|
||||
renderpass.set_bind_group(0, &self.voxel_cache.lock().user_bind_group(), &[]);
|
||||
renderpass.set_bind_group(1, Some(&surface_bg), &[]);
|
||||
let imm = [Immediates {
|
||||
view_proj: self.camera.view_proj().inverse(),
|
||||
@@ -1122,7 +1127,7 @@ impl State
|
||||
DownloadBuffer::read_buffer(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
&self.voxel_cache.lock().usage_buffer.usage_buffer.slice(..),
|
||||
&self.voxel_cache.lock().usage_buffer.slice(..),
|
||||
move |slice| {
|
||||
let slice = slice.unwrap();
|
||||
let cache_slice: &[u32] = bytemuck::cast_slice(&slice);
|
||||
@@ -1136,6 +1141,23 @@ impl State
|
||||
},
|
||||
);
|
||||
|
||||
// DownloadBuffer::read_buffer(
|
||||
// &self.device,
|
||||
// &self.queue,
|
||||
// &self
|
||||
// .voxel_cache
|
||||
// .lock()
|
||||
// .structure_table
|
||||
// .request_buffer
|
||||
// .request_count_buffer()
|
||||
// .slice(..),
|
||||
// move |slice| {
|
||||
// let slice = slice.unwrap();
|
||||
// let cache_slice: &[u32] = bytemuck::cast_slice(&slice);
|
||||
// println!("structure_table count: {}", cache_slice[0]);
|
||||
// },
|
||||
// );
|
||||
|
||||
// ~~ Do cache managment
|
||||
if !self
|
||||
.camera
|
||||
@@ -1205,7 +1227,7 @@ impl State
|
||||
bytemuck::pod_collect_to_vec(&buffer.unwrap());
|
||||
|
||||
let generator = cloned_generator;
|
||||
let gen_test = SineGenerator::<4>::new(5);
|
||||
//let gen_test = SineGenerator::<4>::new(5);
|
||||
|
||||
let mut structure_nodes: Vec<StructurePoolElement<4>> = vec![];
|
||||
let mut color_nodes: Vec<[ColorBytes; 64]> = vec![];
|
||||
|
||||
+564
-477
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,4 @@
|
||||
use bytemuck::bytes_of;
|
||||
use wgpu::BindGroup;
|
||||
use wgpu::BindGroupLayout;
|
||||
use wgpu::Buffer;
|
||||
@@ -67,6 +68,17 @@ impl NonZeroBufferCompactor
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// Optional compacted layout
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 2,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -116,7 +128,7 @@ impl NonZeroBufferCompactor
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("compaction_pipeline_layout"),
|
||||
bind_group_layouts: &[Some(&count_buffer_bg_layout), Some(&compaction_bg_layout)],
|
||||
immediate_size: 0,
|
||||
immediate_size: size_of::<u32>() as u32, // Bool : Write to compacted buffer ?
|
||||
});
|
||||
|
||||
let compaction_shader_module = unsafe {
|
||||
@@ -201,6 +213,7 @@ impl NonZeroBufferCompactor
|
||||
encoder: &mut CommandEncoder,
|
||||
count_buffer: &Buffer,
|
||||
index_buffer: &Buffer,
|
||||
compact_buffer: Option<&Buffer>,
|
||||
)
|
||||
{
|
||||
let count_buffer_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
@@ -215,6 +228,10 @@ impl NonZeroBufferCompactor
|
||||
binding: 1,
|
||||
resource: index_buffer.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: compact_buffer.unwrap_or(&count_buffer).as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
@@ -235,6 +252,17 @@ impl NonZeroBufferCompactor
|
||||
|
||||
// We can now reapply the partial running sum on the subblocks
|
||||
compute_pass.set_pipeline(&self.scatter_pipeline);
|
||||
compute_pass.set_immediates(
|
||||
0,
|
||||
if compact_buffer.is_some()
|
||||
{
|
||||
bytes_of(&1)
|
||||
}
|
||||
else
|
||||
{
|
||||
bytes_of(&0)
|
||||
},
|
||||
);
|
||||
compute_pass.dispatch_workgroups(self.block_count as u32, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#[derive(Clone, PartialEq, Eq, Default)]
|
||||
pub struct VoxelCacheLayout
|
||||
{
|
||||
pub(super) attributes: Vec<VoxelCacheAttribute>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct VoxelCacheAttribute
|
||||
{
|
||||
pub word_count: u32,
|
||||
}
|
||||
|
||||
impl VoxelCacheLayout
|
||||
{
|
||||
pub fn add_attribute(mut self, attribute: VoxelCacheAttribute) -> Self
|
||||
{
|
||||
self.attributes.push(attribute);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn attributes(&self) -> &[VoxelCacheAttribute]
|
||||
{
|
||||
&self.attributes
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,8 @@ use crate::voxel_cache::VoxelAttribute;
|
||||
use crate::voxel_cache::data::ColorPoolElement;
|
||||
use crate::voxel_cache::data::LocationPoolElement;
|
||||
use crate::voxel_cache::data::StructurePoolElement;
|
||||
use crate::voxel_cache::layout::VoxelCacheAttribute;
|
||||
use crate::voxel_cache::layout::VoxelCacheLayout;
|
||||
|
||||
#[derive(Clone, Copy, Zeroable, Pod)]
|
||||
#[repr(C)]
|
||||
@@ -67,7 +69,7 @@ impl<const N: usize> CacheProducerInterface<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(size: usize, device: &Device, attributes: Vec<VoxelAttribute>) -> Self
|
||||
pub fn new(size: usize, device: &Device, layout: &VoxelCacheLayout) -> Self
|
||||
{
|
||||
let requests = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("cache_interface_request_buffer"),
|
||||
@@ -91,7 +93,8 @@ where
|
||||
});
|
||||
|
||||
// Create attributes
|
||||
let attributes_nodes = attributes
|
||||
let attributes_nodes = layout
|
||||
.attributes()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, x)| {
|
||||
@@ -119,7 +122,7 @@ where
|
||||
});
|
||||
|
||||
let request_side_bind_group_layout =
|
||||
Self::request_side_bind_group_layout(device, &attributes);
|
||||
Self::request_side_bind_group_layout(device, layout.attributes());
|
||||
let producer_side_bind_group_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("cache_interface_producer_side_bind_group_layout"),
|
||||
@@ -166,7 +169,7 @@ where
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: Some(NonZero::new(attributes.len() as u32).unwrap()),
|
||||
count: Some(NonZero::new(layout.attributes().len() as u32).unwrap()),
|
||||
},
|
||||
// Location nodes
|
||||
BindGroupLayoutEntry {
|
||||
@@ -309,7 +312,7 @@ where
|
||||
|
||||
pub fn request_side_bind_group_layout(
|
||||
device: &Device,
|
||||
attributes: &[VoxelAttribute],
|
||||
attributes: &[VoxelCacheAttribute],
|
||||
) -> BindGroupLayout
|
||||
{
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
|
||||
@@ -265,6 +265,7 @@ where
|
||||
encoder,
|
||||
&self.request_buffer,
|
||||
&self.index_buffer,
|
||||
None,
|
||||
);
|
||||
|
||||
self.buffer_compactor
|
||||
@@ -287,6 +288,7 @@ where
|
||||
&self.keys_buffer,
|
||||
&self.index_buffer,
|
||||
Some(&self.request_count),
|
||||
None,
|
||||
encoder,
|
||||
radix_sort::RadixSortDirection::Descending,
|
||||
);
|
||||
|
||||
@@ -183,6 +183,7 @@ impl UsageBuffer
|
||||
&self.key_buffer,
|
||||
&self.index_buffer,
|
||||
None,
|
||||
None,
|
||||
encoder,
|
||||
crate::gpu::radix_sort::RadixSortDirection::Ascending,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user