From 08fa6a81a58c8202ed2ec15c1522e31482284cbb Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 10 Jan 2025 16:28:52 +0100 Subject: expand ui buffers on demand --- client/src/ui.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 13 deletions(-) (limited to 'client/src/ui.rs') diff --git a/client/src/ui.rs b/client/src/ui.rs index 2bf8ed5..8147ad9 100644 --- a/client/src/ui.rs +++ b/client/src/ui.rs @@ -50,14 +50,16 @@ pub struct UiRenderer { pipeline: RenderPipeline, bind_group_layout: BindGroupLayout, textures: RwLock>, - surfaces: HashMap, + surfaces: RwLock>, } pub struct UiSurface { pub transform: Affine3A, pub content: Arc, index: Buffer, + index_capacity: usize, vertex: Buffer, + vertex_capacity: usize, } impl UiRenderer { @@ -140,7 +142,7 @@ impl UiRenderer { queue, bind_group_layout, textures: HashMap::new().into(), - surfaces: HashMap::new(), + surfaces: HashMap::new().into(), } } @@ -149,25 +151,31 @@ impl UiRenderer { transform: Affine3A, content: impl Fn(&Context) + Send + Sync + 'static, ) { + let index_capacity = 1024; + let vertex_capacity = 1024; let index = self.device.create_buffer(&BufferDescriptor { label: None, - size: size_of::() as u64 * 1024 * 1024, + size: (size_of::() * index_capacity) as u64, usage: BufferUsages::INDEX | BufferUsages::COPY_DST, mapped_at_creation: false, }); let vertex = self.device.create_buffer(&BufferDescriptor { label: None, - size: size_of::() as u64 * 1024 * 1024, + size: (size_of::() * vertex_capacity) as u64, usage: BufferUsages::VERTEX | BufferUsages::COPY_DST, mapped_at_creation: false, }); - self.surfaces - .insert(ViewportId::from_hash_of(random::()), UiSurface { + self.surfaces.write().unwrap().insert( + ViewportId::from_hash_of(random::()), + UiSurface { transform, content: Arc::new(content), index, vertex, - }); + index_capacity, + vertex_capacity, + }, + ); } pub fn apply_texture_delta(&self, texid: TextureId, delta: ImageDelta) { @@ -266,7 +274,8 @@ impl UiRenderer { depth: &TextureView, projection: Mat4, ) { - if self.surfaces.is_empty() { + let mut surfaces = self.surfaces.write().unwrap(); + if surfaces.is_empty() { return; } @@ -294,9 +303,8 @@ impl UiRenderer { rpass.set_pipeline(&self.pipeline); let mut raw_input = egui::RawInput::default(); - raw_input.viewport_id = self.surfaces.keys().next().copied().unwrap(); - raw_input.viewports = self - .surfaces + raw_input.viewport_id = surfaces.keys().next().copied().unwrap(); + raw_input.viewports = surfaces .keys() .map(|k| { (*k, ViewportInfo { @@ -306,7 +314,7 @@ impl UiRenderer { }) .collect(); - for (_viewport_id, surf) in &self.surfaces { + for (_viewport_id, surf) in surfaces.iter_mut() { let full_output = self.ctx.run(raw_input.clone(), |ctx| (surf.content)(ctx)); for (texid, delta) in full_output.textures_delta.set { @@ -330,7 +338,34 @@ impl UiRenderer { return; } - // TODO realloc buffers and retry if overflowing + while index_count > surf.index_capacity { + info!( + "index buffer overflow. expanding {} -> {}", + surf.index_capacity, + surf.index_capacity * 2 + ); + surf.index_capacity *= 2; + surf.index = self.device.create_buffer(&BufferDescriptor { + label: None, + size: (size_of::() * surf.index_capacity) as u64, + usage: BufferUsages::INDEX | BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + } + while vertex_count > surf.vertex_capacity { + info!( + "vertex buffer overflow. expanding {} -> {}", + surf.vertex_capacity, + surf.vertex_capacity * 2 + ); + surf.vertex_capacity *= 2; + surf.vertex = self.device.create_buffer(&BufferDescriptor { + label: None, + size: (size_of::() * surf.vertex_capacity) as u64, + usage: BufferUsages::VERTEX | BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + } let mut mapped_index = self .queue -- cgit v1.2.3-70-g09d2