memory profile

This commit is contained in:
2026-11-02 12:06:56 +01:00
parent 9b9f637842
commit 11d26ad16b
14 changed files with 463 additions and 92 deletions
+49
View File
@@ -0,0 +1,49 @@
use crate::gi_pipeline::sparse_set::GpuSparseSet;
use crate::voxel_cache::VoxelCache;
use crate::voxel_cache::{self};
mod sparse_set;
pub struct VoxelGiPipeline<const N: usize>
where
[(); N * N * N]:,
{
voxel_cache: VoxelCache<N>,
/// Keeps a set of the leaves present in the cache.
/// Kept in correspondancy with the leaf list attribute within the cache
leaf_list: GpuSparseSet,
leaf_list_attribute: usize,
}
impl<const N: usize> VoxelGiPipeline<N>
where
[(); N * N * N]:,
{
pub fn new(
device: &wgpu::Device,
cache_size: usize,
cache_layout: &mut voxel_cache::CacheLayout,
) -> Self
{
let leaf_list_attribute = cache_layout.add_attribute(voxel_cache::CacheAttribute {
label: Some("VoxelGiPipeline::leaf_list_pointer"),
word_count: 1,
init: voxel_cache::CacheAttributeInit::External,
});
cache_layout.add_attribute(voxel_cache::CacheAttribute {
label: Some("VoxelGiPipeline::radiance"),
word_count: 3,
init: voxel_cache::CacheAttributeInit::DontTouch,
});
let voxel_cache = VoxelCache::new(device, cache_size, cache_layout);
Self {
voxel_cache,
leaf_list: GpuSparseSet::new(device, cache_size),
leaf_list_attribute,
}
}
}
+57
View File
@@ -0,0 +1,57 @@
use wgpu::util::DeviceExt;
use crate::gpu::create_multi_buffers_bind_group;
use crate::gpu::create_multi_buffers_bind_group_layout;
/// To keep a set of voxel nodes in the cache
pub struct GpuSparseSet
{
/// Maximum size of the set
size: usize,
/// Contains pointers to nodes in the cache
pointers_buffer: wgpu::Buffer,
/// Amount of elements in the set
count_buffer: wgpu::Buffer,
bind_group: wgpu::BindGroup,
}
impl GpuSparseSet
{
pub fn new(device: &wgpu::Device, size: usize) -> Self
{
let pointers_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("GpuSparseSet::pointers_buffer"),
usage: wgpu::BufferUsages::STORAGE,
contents: bytemuck::cast_slice(&vec![0u32; size]),
});
let count_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("GpuSparseSet::count_buffer"),
usage: wgpu::BufferUsages::STORAGE,
contents: bytemuck::bytes_of(&0u32),
});
let bind_group_layout = Self::create_bind_group_layout(device);
let bind_group = create_multi_buffers_bind_group(
device,
&bind_group_layout,
&[&pointers_buffer, &count_buffer],
Some("GpuSparseSet::bind_group"),
);
Self {
size,
pointers_buffer,
count_buffer,
bind_group,
}
}
pub fn create_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout
{
create_multi_buffers_bind_group_layout(device, 2, Some("GpuSparseSet::bind_group_layout"))
}
}
+60
View File
@@ -2,3 +2,63 @@
pub mod buffer_compactor;
pub mod indirect_buffer;
pub mod radix_sort;
// Utilities
pub fn create_multi_buffers_bind_group_layout(
device: &wgpu::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,
})
}
pub fn create_multi_buffers_bind_group(
device: &wgpu::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,
})
}
pub fn create_multi_buffers_bg_bg_layout(
device: &wgpu::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)
}
+2
View File
@@ -12,6 +12,8 @@ use wgpu::Device;
use wgpu::QuerySet;
use wgpu::Queue;
pub mod memory_profiler;
macro_rules! begin_frame {
($encoder:ident) => {
let mut $encoder = $encoder.begin_frame(None);
+205
View File
@@ -0,0 +1,205 @@
use std::ops::Deref;
use wgpu::util::DeviceExt;
pub struct Buffer
{
pub label: String,
pub size: usize,
}
pub struct Group
{
pub label: &'static str,
size: Option<usize>,
pub buffers: Vec<Buffer>,
pub sub_groups: Vec<Group>,
}
impl Group
{
pub fn new(label: &'static str) -> Self
{
Self {
label,
size: None,
buffers: Vec::new(),
sub_groups: Vec::new(),
}
}
pub fn size(&self) -> usize
{
self.size.unwrap_or_else(|| {
self.buffers.iter().map(|b| b.size).sum::<usize>()
+ self.sub_groups.iter().map(Self::size).sum::<usize>()
})
}
pub fn size_mut(&mut self) -> usize
{
*self.size.get_or_insert_with(|| {
self.buffers.iter().map(|b| b.size).sum::<usize>()
+ self
.sub_groups
.iter_mut()
.map(Self::size_mut)
.sum::<usize>()
})
}
}
pub struct MemoryProfile
{
stack: Vec<Group>,
}
pub struct Device<'a>
{
device: wgpu::Device,
profile: &'a mut MemoryProfile,
}
impl<'a> Device<'a>
{
pub fn create_buffer(&mut self, desc: &wgpu::BufferDescriptor) -> wgpu::Buffer
{
let buffer = self.device.create_buffer(desc);
self.profile.add_buffer(Buffer {
label: desc.label.unwrap_or("Unnamed").into(),
size: desc.size as usize,
});
buffer
}
pub fn create_buffer_init(&mut self, desc: &wgpu::util::BufferInitDescriptor) -> wgpu::Buffer
{
let buffer = self.device.create_buffer_init(desc);
self.profile.add_buffer(Buffer {
label: desc.label.unwrap_or("Unnamed").into(),
size: desc.contents.len(),
});
buffer
}
pub fn group<'b>(&'b mut self, group_label: &'static str) -> Device<'b>
where
'a: 'b,
{
self.profile.open_group(group_label);
Device {
device: self.device.clone(),
profile: self.profile,
}
}
}
impl<'a> Drop for Device<'a>
{
fn drop(&mut self)
{
self.profile.close_group();
}
}
impl Deref for Device<'_>
{
type Target = wgpu::Device;
fn deref(&self) -> &Self::Target
{
&self.device
}
}
impl MemoryProfile
{
pub fn new(label: &'static str) -> Self
{
Self {
stack: vec![Group::new(label)],
}
}
pub fn begin(&mut self, device: &wgpu::Device) -> Device
{
Device {
device: device.clone(),
profile: self,
}
}
pub fn terminate(mut self) -> Group
{
self.stack.pop().unwrap()
}
fn open_group(&mut self, label: &'static str)
{
self.stack.push(Group::new(label));
}
fn close_group(&mut self)
{
let mut group = self.stack.pop().unwrap();
let _ = group.size_mut();
// Add it to parent if it exists
if let Some(parent) = self.stack.last_mut()
{
parent.sub_groups.push(group);
}
else
{
self.stack.push(group);
}
}
fn add_buffer(&mut self, buffer: Buffer)
{
self.stack.last_mut().unwrap().buffers.push(buffer);
}
}
use std::fmt;
impl fmt::Display for Group
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
self.fmt_with_indent(f, 0)
}
}
impl Group
{
fn fmt_with_indent(&self, f: &mut fmt::Formatter<'_>, indent: usize) -> fmt::Result
{
let prefix = " ".repeat(indent);
writeln!(
f,
"{}{} ({} bytes)",
prefix,
self.label,
self.size.unwrap_or_else(|| self.size())
)?;
// Print buffers
for buffer in &self.buffers
{
writeln!(
f,
"{} ├── {} ({} bytes)",
prefix, buffer.label, buffer.size
)?;
}
// Print subgroups
for (i, group) in self.sub_groups.iter().enumerate()
{
group.fmt_with_indent(f, indent + 1)?;
}
Ok(())
}
}
+2 -1
View File
@@ -204,6 +204,7 @@ impl State
max_storage_buffers_per_shader_stage: 32,
max_binding_array_elements_per_shader_stage: 16,
max_bind_groups: 8,
max_storage_buffer_binding_size: 1_000_000_000,
..Default::default()
},
..Default::default()
@@ -359,7 +360,7 @@ impl State
init: voxel_cache::CacheAttributeInit::External,
});
let voxel_cache = VoxelCache::<4>::new(&device, 100_000, &layout);
let voxel_cache = VoxelCache::<4>::new(&device, 200_000, &layout);
let request_buffer = CacheRequestBuffer::new(&device, 1024);
let insertion_data = CacheInsertionData::new(&device, 1024, &layout);
-9
View File
@@ -1,19 +1,10 @@
use std::f32::EPSILON;
use std::f32::INFINITY;
use std::fs::File;
use std::path::Path;
use glam::Vec3;
use itertools::Itertools;
use noise::Billow;
use noise::Cylinders;
use noise::Fbm;
use noise::NoiseFn;
use noise::Perlin;
use noise::RidgedMulti;
use noise::Simplex;
use noise::SuperSimplex;
use noise::Worley;
use rand::random;
use crate::sparse_tree::Color;
+26 -68
View File
@@ -3,13 +3,16 @@ use std::num::NonZero;
use wgpu::CommandEncoder;
use wgpu::Device;
use crate::gpu::create_multi_buffers_bg_bg_layout;
use crate::gpu::create_multi_buffers_bind_group_layout;
use crate::gpu::radix_sort::RadixSorter;
use crate::gpu_profiler;
use crate::gpu_profiler::ScopeEncoder;
use crate::gpu_profiler::memory_profiler;
use crate::gpu_profiler::memory_profiler::MemoryProfile;
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;
@@ -28,7 +31,9 @@ pub mod schema;
pub mod structure;
pub mod tables;
pub use layout::*;
pub use layout::CacheAttribute;
pub use layout::CacheAttributeInit;
pub use layout::CacheLayout;
/// Stores some ressources that can be reused throughout the modules of the cache
struct CacheRessources<const N: usize>
@@ -40,7 +45,7 @@ struct CacheRessources<const N: usize>
impl<const N: usize> CacheRessources<N>
{
pub fn new(device: &Device, page_count: usize) -> Self
pub fn new(device: &mut memory_profiler::Device, page_count: usize) -> Self
{
let scratch_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("CacheRessources::scratch_buffer"),
@@ -127,11 +132,20 @@ where
{
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 mut cache_memory_profile = MemoryProfile::new("VoxelCache");
let mut mem_device = cache_memory_profile.begin(device);
let structure = CacheStructure::new(&mut mem_device.group("CacheStructure"), page_count);
let attributes = CacheAttributes::from_cache_layout(
&mut mem_device.group("CacheAttributes"),
layout,
page_count,
);
let request_handler =
CacheRequestHandler::new(&mut mem_device.group("CacheRequestHandler"), page_count);
let evictor = CacheEvictor::new(&mut mem_device.group("CacheEvictor"), page_count, 0u32);
let common_ressources =
CacheRessources::new(&mut mem_device.group("CacheRessources"), page_count);
let write_requests_pass = WriteRequestsPass::new(device, page_count, layout);
let cache_insertion_pass = CacheInsertionPass::new(device, page_count, layout);
@@ -146,6 +160,10 @@ where
&evictor,
);
drop(mem_device);
let mut root_group = cache_memory_profile.terminate();
println!("{}", root_group);
Self {
page_count,
structure,
@@ -383,63 +401,3 @@ where
self.evictor.timestamp_pages_buffer()
}
}
// 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)
}
+12 -3
View File
@@ -2,6 +2,7 @@ use wgpu::BindGroupLayout;
use wgpu::Buffer;
use wgpu::BufferUsages;
use crate::gpu_profiler::memory_profiler;
use crate::voxel_cache::layout::CacheLayout;
/// Contains instanciated gpu buffers to hold data for cache attributes
@@ -15,14 +16,22 @@ pub struct CacheAttributes<const N: usize>
impl<const N: usize> CacheAttributes<N>
{
pub fn from_cache_layout(device: &wgpu::Device, layout: &CacheLayout, page_count: usize)
-> Self
pub fn from_cache_layout(
device: &mut memory_profiler::Device,
layout: &CacheLayout,
page_count: usize,
) -> Self
{
let attribute_buffers = layout
.attributes
.iter()
.map(|attribute| {
attribute.create_buffer(device, N * N * N, page_count, BufferUsages::STORAGE)
attribute.create_buffer_profiled(
device,
N * N * N,
page_count,
BufferUsages::STORAGE,
)
})
.collect::<Vec<_>>();
+7 -3
View File
@@ -6,12 +6,12 @@ use wgpu::ComputePipeline;
use wgpu::include_wgsl;
use wgpu::util::DeviceExt;
use crate::gpu::create_multi_buffers_bind_group;
use crate::gpu::radix_sort;
use crate::gpu_profiler::ScopeEncoder;
use crate::gpu_profiler::memory_profiler;
use crate::gpu_profiler::{self};
use crate::voxel_cache::CacheRessources;
use crate::voxel_cache::create_multi_buffers_bg_bg_layout;
use crate::voxel_cache::create_multi_buffers_bind_group;
use crate::voxel_cache::create_multi_buffers_bind_group_layout;
use crate::voxel_cache::structure::CacheStructure;
@@ -44,7 +44,11 @@ pub struct CacheEvictor
impl CacheEvictor
{
pub fn new(device: &wgpu::Device, page_count: usize, initial_timestamp: u32) -> Self
pub fn new(
device: &mut memory_profiler::Device,
page_count: usize,
initial_timestamp: u32,
) -> Self
{
let timestamp_pages = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("CacheUsage::timestamp_pages"),
+25
View File
@@ -1,5 +1,7 @@
use wgpu::BufferUsages;
use crate::gpu_profiler::memory_profiler;
/// Defines how a [`VoxelCache`]'s attribute is populated upon populating a page
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CacheAttributeInit
@@ -26,6 +28,29 @@ pub struct CacheAttribute
impl CacheAttribute
{
pub fn create_buffer_profiled(
&self,
device: &mut memory_profiler::Device,
child_count: usize,
page_count: usize,
usage: wgpu::BufferUsages,
) -> wgpu::Buffer
{
device.create_buffer(&wgpu::BufferDescriptor {
label: Some(
format!(
"VoxelCacheAttribute ({}) {} word(s)",
self.label.unwrap_or("no-name"),
self.word_count
)
.as_str(),
),
size: (self.word_count as usize * page_count * child_count * size_of::<u32>()) as u64,
usage,
mapped_at_creation: false,
})
}
/// Creates the wgpu buffer necessary to store this attribute in the cache
pub fn create_buffer(
&self,
+3 -2
View File
@@ -10,12 +10,13 @@ use wgpu::include_wgsl;
use wgpu::util::DeviceExt;
use crate::gpu::buffer_compactor::NonZeroBufferCompactor;
use crate::gpu::create_multi_buffers_bind_group;
use crate::gpu::radix_sort::{self};
use crate::gpu_profiler::ScopeEncoder;
use crate::gpu_profiler::memory_profiler;
use crate::gpu_profiler::{self};
use crate::voxel_cache::CacheRessources;
use crate::voxel_cache::create_multi_buffers_bg_bg_layout;
use crate::voxel_cache::create_multi_buffers_bind_group;
use crate::voxel_cache::create_multi_buffers_bind_group_layout;
use crate::voxel_cache::schema::RequestInformation;
use crate::voxel_cache::schema::RequestMeta;
@@ -65,7 +66,7 @@ pub struct CacheRequestBuffer
impl<const N: usize> CacheRequestHandler<N>
{
pub fn new(device: &wgpu::Device, page_count: usize) -> Self
pub fn new(device: &mut memory_profiler::Device, page_count: usize) -> Self
{
let request_counts_pages = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("CacheRequestHandler::request_counts_pages"),
+4 -4
View File
@@ -2,10 +2,10 @@ use wgpu::BindGroup;
use wgpu::BindGroupLayout;
use wgpu::Buffer;
use wgpu::BufferUsages;
use wgpu::util::DeviceExt;
use crate::voxel_cache::create_multi_buffers_bind_group;
use crate::voxel_cache::create_multi_buffers_bind_group_layout;
use crate::gpu::create_multi_buffers_bind_group;
use crate::gpu::create_multi_buffers_bind_group_layout;
use crate::gpu_profiler::memory_profiler;
use crate::voxel_cache::schema::DepthPage;
use crate::voxel_cache::schema::StructurePage;
@@ -30,7 +30,7 @@ where
pub const LOCATION_PAGES_BINDING: u32 = 1;
pub const DEPTH_PAGES_BINDING: u32 = 2;
pub fn new(device: &wgpu::Device, page_count: usize) -> Self
pub fn new(device: &mut memory_profiler::Device, page_count: usize) -> Self
{
let pointer_pages = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("CacheStructure::pointer_pages"),
+11 -2
View File
@@ -2,6 +2,7 @@ use wgpu::util::DeviceExt;
use crate::gpu::indirect_buffer::IndirectBuffer1D;
use crate::gpu_profiler::ScopeEncoder;
use crate::gpu_profiler::memory_profiler::MemoryProfile;
use crate::voxel_cache::CacheInvalidable;
use crate::voxel_cache::CacheRessources;
use crate::voxel_cache::create_multi_buffers_bg_bg_layout;
@@ -68,7 +69,15 @@ impl FixedStructureTable
contents: bytemuck::cast_slice(&vec![pointer; size]),
});
let request_handler = CacheRequestHandler::<1>::new(device, size);
let mut memory_profile = MemoryProfile::new("FixedStructureTable");
let mut mem_device = memory_profile.begin(device);
let request_handler =
CacheRequestHandler::<1>::new(&mut mem_device.group("CacheRequestHandler"), size);
let common_ressources =
CacheRessources::new(&mut mem_device.group("CacheRessources"), size);
drop(mem_device);
drop(memory_profile);
let (pointers_bind_group_layout, pointers_bind_group) = create_multi_buffers_bg_bg_layout(
device,
@@ -167,7 +176,7 @@ impl FixedStructureTable
size,
pointers,
pointers_bind_group,
common_ressources: CacheRessources::new(device, size),
common_ressources,
request_handler,
indirect_buffer: IndirectBuffer1D::new(device),
write_requests_pass,