fix crash, ui core multi-draw call

This commit is contained in:
griffi-gh 2023-11-22 20:26:44 +01:00
parent d852c48e4a
commit 547759d6b2
2 changed files with 35 additions and 20 deletions

View file

@ -30,11 +30,11 @@ impl From<UiVertex> for Vertex {
implement_vertex!(Vertex, position, color); implement_vertex!(Vertex, position, color);
pub struct GliumUiRenderer { pub struct GliumUiRenderer {
pub program: glium::Program, program: glium::Program,
pub vertex_buffer: glium::VertexBuffer<Vertex>, vertex_buffer: glium::VertexBuffer<Vertex>,
pub index_buffer: glium::IndexBuffer<u32>, index_buffer: glium::IndexBuffer<u32>,
pub vertex_count: usize, vertex_count: usize,
pub index_count: usize, index_count: usize,
} }
impl GliumUiRenderer { impl GliumUiRenderer {
@ -73,18 +73,26 @@ impl GliumUiRenderer {
fn write_buffer_data(&mut self, vtx: &[Vertex], idx: &[u32]) { fn write_buffer_data(&mut self, vtx: &[Vertex], idx: &[u32]) {
log::debug!("uploading {} vertices and {} indices", vtx.len(), idx.len()); 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.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.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) { pub fn update(&mut self, plan: &UiDrawPlan) {
let data_vtx = &plan.vertices.iter().copied().map(Vertex::from).collect::<Vec<_>>(); assert!(plan.calls.len() == 1, "multiple draw calls not supported yet");
let data_idx = &plan.indices; let data_vtx = &plan.calls[0].vertices.iter().copied().map(Vertex::from).collect::<Vec<_>>();
let data_idx = &plan.calls[0].indices;
self.write_buffer_data(data_vtx, data_idx); self.write_buffer_data(data_vtx, data_idx);
} }

View file

@ -34,20 +34,25 @@ pub struct UiVertex {
} }
#[derive(Default)] #[derive(Default)]
pub struct UiDrawPlan { pub struct UiDrawCall {
pub vertices: Vec<UiVertex>, pub vertices: Vec<UiVertex>,
pub indices: Vec<u32>, pub indices: Vec<u32>,
} }
#[derive(Default)]
pub struct UiDrawPlan {
pub calls: Vec<UiDrawCall>
}
impl UiDrawPlan { impl UiDrawPlan {
pub fn build(calls: &UiDrawCommands) -> Self { pub fn build(calls: &UiDrawCommands) -> Self {
let mut plan = Self::default(); let mut call = UiDrawCall::default();
for call in &calls.commands { for command in &calls.commands {
match call { match command {
UiDrawCommand::Rectangle { position, size, color } => { UiDrawCommand::Rectangle { position, size, color } => {
let idx = plan.vertices.len() as u32; let idx = call.vertices.len() as u32;
plan.indices.extend([idx, idx + 1, idx + 2, idx, idx + 2, idx + 3]); call.indices.extend([idx, idx + 1, idx + 2, idx, idx + 2, idx + 3]);
plan.vertices.extend([ call.vertices.extend([
UiVertex { UiVertex {
position: *position, position: *position,
color: *color, color: *color,
@ -68,6 +73,8 @@ impl UiDrawPlan {
} }
} }
} }
plan Self {
calls: vec![call]
}
} }
} }