summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-01-10 16:28:52 +0100
committermetamuffin <metamuffin@disroot.org>2025-01-10 16:28:52 +0100
commit08fa6a81a58c8202ed2ec15c1522e31482284cbb (patch)
tree834c34f2e2282541215ccdaa99d426a948114d3c
parent8e1cc17bc59e3410315e42a7fe8e54f4afaae9ab (diff)
downloadweareserver-08fa6a81a58c8202ed2ec15c1522e31482284cbb.tar
weareserver-08fa6a81a58c8202ed2ec15c1522e31482284cbb.tar.bz2
weareserver-08fa6a81a58c8202ed2ec15c1522e31482284cbb.tar.zst
expand ui buffers on demand
-rw-r--r--client/src/ui.rs61
1 files changed, 48 insertions, 13 deletions
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<HashMap<TextureId, (BindGroup, Texture, [u32; 2])>>,
- surfaces: HashMap<ViewportId, UiSurface>,
+ surfaces: RwLock<HashMap<ViewportId, UiSurface>>,
}
pub struct UiSurface {
pub transform: Affine3A,
pub content: Arc<dyn Fn(&Context) + Send + Sync + 'static>,
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::<f32>() as u64 * 1024 * 1024,
+ size: (size_of::<f32>() * 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::<Vertex>() as u64 * 1024 * 1024,
+ size: (size_of::<Vertex>() * vertex_capacity) as u64,
usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
mapped_at_creation: false,
});
- self.surfaces
- .insert(ViewportId::from_hash_of(random::<u128>()), UiSurface {
+ self.surfaces.write().unwrap().insert(
+ ViewportId::from_hash_of(random::<u128>()),
+ 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::<f32>() * 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::<f32>() * surf.vertex_capacity) as u64,
+ usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ }
let mut mapped_index = self
.queue