From 2722ccccdb1f91cb266a1325d1f723c2719eb9a0 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Wed, 22 Dec 2021 17:54:29 +0100 Subject: [PATCH] Remove indices of immediate mod line meshes With bevyengine/bevy#3415 it is now possible to upload meshes to GPU without `indices` set. Before, it would panic. Removing `indices` enables us to simply remove the `ImmLinesStorage::fill_indices` code, since the ordering is exactly as the GPU would interpret the mesh otherwise. --- Cargo.toml | 2 +- examples/3d.rs | 2 +- src/lib.rs | 31 +++++++------------------------ 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b229d82..49abc23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ exclude = ["demo.gif", "demo_2.png", "demo_2.webm"] bevy = { version = "0.5", default-features = false, features = [ "render" ] } [patch.crates-io] -bevy = { git = "https://github.com/bevyengine/bevy", rev = "a3c53e689d62a2fd403bf0236b5638e569bd5600" } +bevy = { git = "https://github.com/nicopap/bevy", branch = "implement-non-indexed-drawing" } [features] example_deps = [ diff --git a/examples/3d.rs b/examples/3d.rs index 77be96f..b75d748 100644 --- a/examples/3d.rs +++ b/examples/3d.rs @@ -23,7 +23,7 @@ fn setup(mut commands: Commands, mut lines: DebugLines) { Vec3::new(-1.0, 1.0, 1.0), 100.0, Color::CYAN, - Color::ORANGE_RED, + Color::MIDNIGHT_BLUE, ); } diff --git a/src/lib.rs b/src/lib.rs index 946804d..a8a8d60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,8 +73,9 @@ pub const MAX_LINES: usize = MAX_POINTS / 2; pub const MAX_POINTS: usize = MAX_POINTS_PER_MESH * DEBUG_LINES_MESH_COUNT; fn spawn_debug_lines_mesh(meshes: &mut Assets, retain: DebugLinesMesh) -> impl Bundle { + let is_immediate = matches!(retain, DebugLinesMesh::Immediate(_)); ( - meshes.add(debug_lines_mesh()), + meshes.add(debug_lines_mesh(is_immediate)), Transform::default(), GlobalTransform::default(), Visibility::default(), @@ -111,7 +112,7 @@ fn update_debug_lines_mesh( } /// Initialize [`DebugLinesMesh`]'s [`Mesh`]. -fn debug_lines_mesh() -> Mesh { +fn debug_lines_mesh(is_immediate: bool) -> Mesh { let mut mesh = Mesh::new(PrimitiveTopology::LineList); mesh.set_attribute( Mesh::ATTRIBUTE_POSITION, @@ -121,7 +122,9 @@ fn debug_lines_mesh() -> Mesh { Mesh::ATTRIBUTE_COLOR, VertexAttributeValues::Float32x4(Vec::with_capacity(256)), ); - mesh.set_indices(Some(Indices::U16(Vec::with_capacity(256)))); + if !is_immediate { + mesh.set_indices(Some(Indices::U16(Vec::with_capacity(256)))); + } mesh } @@ -194,23 +197,6 @@ impl ImmLinesStorage { } } - fn fill_indices(&self, buffer: &mut Vec, mesh: usize) { - buffer.clear(); - let start = mesh << 16; - let end = (mesh + 1) << 16; - let buffer_size = self.positions.len(); - let indices = if start < buffer_size && buffer_size >= end { - // Because MAX_POINTS_PER_MESH == 2^16 - 0xFFFF_u16 - } else if start < buffer_size && buffer_size < end { - (buffer_size - start) as u16 - } else { - 0 - }; - // Because MAX_POINTS_PER_MESH == 2^16, this needs to be inclusive - buffer.extend(0..=indices); - } - fn mark_expired(&mut self) { self.positions.clear(); self.colors.clear(); @@ -232,9 +218,6 @@ impl ImmLinesStorage { fn fill_attributes(&self, mesh: &mut Mesh, mesh_index: usize) { use VertexAttributeValues::{Float32x3, Float32x4}; - if let Some(Indices::U16(indices)) = mesh.indices_mut() { - self.fill_indices(indices, mesh_index); - } if let Some(Float32x3(vbuffer)) = mesh.attribute_mut(Mesh::ATTRIBUTE_POSITION) { self.fill_vertexes(vbuffer, mesh_index); } @@ -458,7 +441,7 @@ impl SpecializedPipeline for DebugLinePipeline { descriptor.fragment.as_mut().unwrap().shader = self.shader.clone_weak(); descriptor.primitive.topology = PrimitiveTopology::LineList; descriptor.primitive.cull_mode = None; - // TODO: set this to None to remove depth check + // TODO: set this to a large value to remove depth check descriptor.depth_stencil.as_mut().unwrap().bias.slope_scale = 1.0; descriptor }