From 547759d6b2554a39721b591b29bafcd7e62b2761 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Wed, 22 Nov 2023 20:26:44 +0100 Subject: [PATCH] fix crash, ui core multi-draw call --- kubi-ui/src/backend/glium.rs | 32 ++++++++++++++++++++------------ kubi-ui/src/draw.rs | 23 +++++++++++++++-------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/kubi-ui/src/backend/glium.rs b/kubi-ui/src/backend/glium.rs index 72041ba..d1ac903 100644 --- a/kubi-ui/src/backend/glium.rs +++ b/kubi-ui/src/backend/glium.rs @@ -30,11 +30,11 @@ impl From for Vertex { implement_vertex!(Vertex, position, color); pub struct GliumUiRenderer { - pub program: glium::Program, - pub vertex_buffer: glium::VertexBuffer, - pub index_buffer: glium::IndexBuffer, - pub vertex_count: usize, - pub index_count: usize, + program: glium::Program, + vertex_buffer: glium::VertexBuffer, + index_buffer: glium::IndexBuffer, + vertex_count: usize, + index_count: usize, } impl GliumUiRenderer { @@ -73,18 +73,26 @@ impl GliumUiRenderer { fn write_buffer_data(&mut self, vtx: &[Vertex], idx: &[u32]) { log::debug!("uploading {} vertices and {} indices", vtx.len(), idx.len()); - self.ensure_buffer_size(vtx.len(), idx.len()); - self.vertex_buffer.invalidate(); - self.vertex_buffer.slice_mut(0..vtx.len()).unwrap().write(vtx); + self.vertex_count = vtx.len(); - self.index_buffer.invalidate(); - self.index_buffer.slice_mut(0..idx.len()).unwrap().write(idx); self.index_count = idx.len(); + + self.vertex_buffer.invalidate(); + self.index_buffer.invalidate(); + + if self.vertex_count == 0 || self.index_count == 0 { + return + } + + self.ensure_buffer_size(vtx.len(), idx.len()); + self.vertex_buffer.slice_mut(0..vtx.len()).unwrap().write(vtx); + self.index_buffer.slice_mut(0..idx.len()).unwrap().write(idx); } pub fn update(&mut self, plan: &UiDrawPlan) { - let data_vtx = &plan.vertices.iter().copied().map(Vertex::from).collect::>(); - let data_idx = &plan.indices; + assert!(plan.calls.len() == 1, "multiple draw calls not supported yet"); + let data_vtx = &plan.calls[0].vertices.iter().copied().map(Vertex::from).collect::>(); + let data_idx = &plan.calls[0].indices; self.write_buffer_data(data_vtx, data_idx); } diff --git a/kubi-ui/src/draw.rs b/kubi-ui/src/draw.rs index c03b129..bc44cd2 100644 --- a/kubi-ui/src/draw.rs +++ b/kubi-ui/src/draw.rs @@ -34,20 +34,25 @@ pub struct UiVertex { } #[derive(Default)] -pub struct UiDrawPlan { +pub struct UiDrawCall { pub vertices: Vec, pub indices: Vec, } +#[derive(Default)] +pub struct UiDrawPlan { + pub calls: Vec +} + impl UiDrawPlan { pub fn build(calls: &UiDrawCommands) -> Self { - let mut plan = Self::default(); - for call in &calls.commands { - match call { + let mut call = UiDrawCall::default(); + for command in &calls.commands { + match command { UiDrawCommand::Rectangle { position, size, color } => { - let idx = plan.vertices.len() as u32; - plan.indices.extend([idx, idx + 1, idx + 2, idx, idx + 2, idx + 3]); - plan.vertices.extend([ + let idx = call.vertices.len() as u32; + call.indices.extend([idx, idx + 1, idx + 2, idx, idx + 2, idx + 3]); + call.vertices.extend([ UiVertex { position: *position, color: *color, @@ -68,6 +73,8 @@ impl UiDrawPlan { } } } - plan + Self { + calls: vec![call] + } } }