Skip to content

Commit

Permalink
Remove indices of immediate mod line meshes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicopap committed Dec 22, 2021
1 parent 95ee329 commit 2722ccc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion examples/3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down
31 changes: 7 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mesh>, 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(),
Expand Down Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -194,23 +197,6 @@ impl ImmLinesStorage {
}
}

fn fill_indices(&self, buffer: &mut Vec<u16>, 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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 2722ccc

Please sign in to comment.