diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-08-25 20:04:17 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-08-25 20:04:17 -0700 |
| commit | adb07dada5fe61d4165b00eba1906cb354d949fa (patch) | |
| tree | 50770eb021c536a1748daf46db6d46e94b0dc50d /src | |
| parent | da01c6e887d6865f0ad75c8dfe68bbad702afb99 (diff) | |
make all buffer sizes dynamic
Diffstat (limited to 'src')
| -rw-r--r-- | src/renderer/mod.rs | 102 |
1 files changed, 70 insertions, 32 deletions
diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 133c132..364d000 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -20,13 +20,6 @@ use crate::{ // TODO derive from shared chunk config const CHUNK_SLOTS: usize = 11 * 200; -const ENTITY_SLOTS: usize = 64; -const CELL_SLOTS: usize = CHUNK_SLOTS + ENTITY_SLOTS; -const PARTICLE_SLOTS: usize = 10_000; -// TODO make this dynamic -const VFX_LINE_SLOTS: usize = 10_000; - -const INITIAL_DEBUG_VERTEX_CAPACITY: usize = 4096; #[repr(C)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] @@ -87,6 +80,42 @@ fn create_debug_vertex_buffer(device: &wgpu::Device, capacity: usize) -> wgpu::B }) } +fn create_instance_buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Instance buffer"), + size: (capacity * size_of::<RendererInstance>()) as u64, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }) +} + +fn create_cell_buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Cell buffer"), + size: (capacity * CELLS_IN_CHUNK) as u64, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }) +} + +fn create_particle_buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Particle buffer"), + size: (capacity * size_of::<RendererParticle>()) as u64, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }) +} + +fn create_vfx_line_buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some("VFX line buffer"), + size: (capacity * size_of::<RendererVfxLine>()) as u64, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }) +} + pub struct RendererState { window: Arc<Window>, surface: wgpu::Surface<'static>, @@ -109,16 +138,19 @@ pub struct RendererState { instance_buffer: wgpu::Buffer, cell_buffer: wgpu::Buffer, instance_bind_group: wgpu::BindGroup, + entity_capacity: usize, // particles particle_pipeline: wgpu::RenderPipeline, particle_buffer: wgpu::Buffer, particle_bind_group: wgpu::BindGroup, + particle_capacity: usize, // vfx vfx_line_pipeline: wgpu::RenderPipeline, vfx_line_buffer: wgpu::Buffer, vfx_line_bind_group: wgpu::BindGroup, + vfx_line_capacity: usize, // physics debug lines debug_pipeline: wgpu::RenderPipeline, @@ -356,19 +388,10 @@ impl RendererState { mapped_at_creation: false, }); - let instance_buffer = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Instance buffer"), - size: (CELL_SLOTS * size_of::<RendererInstance>()) as u64, - usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); + let entity_capacity = 2048; + let instance_buffer = create_instance_buffer(&device, CHUNK_SLOTS + entity_capacity); - let cell_buffer = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Cell buffer"), - size: (CELL_SLOTS * CELLS_IN_CHUNK) as u64, - usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); + let cell_buffer = create_cell_buffer(&device, (entity_capacity + CHUNK_SLOTS)); let instance_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { label: None, @@ -480,12 +503,8 @@ impl RendererState { cache: None, }); - let particle_buffer = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Particle buffer"), - size: (PARTICLE_SLOTS * size_of::<RendererParticle>()) as u64, - usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); + let particle_capacity = 2048; + let particle_buffer = create_particle_buffer(&device, particle_capacity); let particle_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { label: None, @@ -594,12 +613,8 @@ impl RendererState { cache: None, }); - let vfx_line_buffer = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("VFX line buffer"), - size: (VFX_LINE_SLOTS * size_of::<RendererVfxLine>()) as u64, - usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); + let vfx_line_capacity = 512; + let vfx_line_buffer = create_vfx_line_buffer(&device, vfx_line_capacity); let vfx_line_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { label: None, @@ -698,7 +713,7 @@ impl RendererState { cache: None, }); - let debug_vertex_capacity = INITIAL_DEBUG_VERTEX_CAPACITY; + let debug_vertex_capacity = 4096; let debug_vertex_buffer = create_debug_vertex_buffer(&device, debug_vertex_capacity); RendererState { @@ -719,14 +734,17 @@ impl RendererState { instance_buffer, cell_buffer, instance_bind_group, + entity_capacity, particle_pipeline, particle_buffer, particle_bind_group, + particle_capacity, vfx_line_pipeline, vfx_line_buffer, vfx_line_bind_group, + vfx_line_capacity, debug_pipeline, debug_bind_group, @@ -919,11 +937,13 @@ impl RendererState { } } + let mut entities = 0; for (id, slot) in &self.renderer_rb_entities { if let Some(entity) = sim.entities.get(id) && let Some(cells) = &entity.data.cells && let Some((pos, (cos, sin))) = entity.data._transform(&sim.rb_manager) { + entities += 1; instances.push(RendererInstance { centre: pos.to_array(), cos_sin: [cos, sin], @@ -935,6 +955,14 @@ impl RendererState { } } + if entities > self.entity_capacity { + self.entity_capacity = entities.next_power_of_two(); + self.instance_buffer = + create_instance_buffer(&self.device, self.entity_capacity + CHUNK_SLOTS); + self.cell_buffer = + create_cell_buffer(&self.device, (CHUNK_SLOTS + self.entity_capacity) as usize); + } + self.queue .write_buffer(&self.instance_buffer, 0, bytemuck::cast_slice(&instances)); } @@ -955,6 +983,11 @@ impl RendererState { }) .collect(); + if particles.len() > self.particle_capacity { + self.particle_capacity = particles.len().next_power_of_two(); + self.particle_buffer = create_particle_buffer(&self.device, self.particle_capacity); + } + self.queue .write_buffer(&self.particle_buffer, 0, bytemuck::cast_slice(&particles)); } @@ -977,6 +1010,11 @@ impl RendererState { }) .collect(); + if vfx_lines.len() > self.vfx_line_capacity { + self.vfx_line_capacity = vfx_lines.len().next_power_of_two(); + self.vfx_line_buffer = create_vfx_line_buffer(&self.device, self.vfx_line_capacity); + } + self.queue .write_buffer(&self.vfx_line_buffer, 0, bytemuck::cast_slice(&vfx_lines)); } |
