Skip to content

Commit abf1bc3

Browse files
committed
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.
1 parent 95ee329 commit abf1bc3

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exclude = ["demo.gif", "demo_2.png", "demo_2.webm"]
1616
bevy = { version = "0.5", default-features = false, features = [ "render" ] }
1717

1818
[patch.crates-io]
19-
bevy = { git = "https://github.com/bevyengine/bevy", rev = "a3c53e689d62a2fd403bf0236b5638e569bd5600" }
19+
bevy = { git = "https://github.com/bevyengine/bevy", rev = "aeba9faf0451c4340cc3041d31519dd909598c0b" }
2020

2121
[features]
2222
example_deps = [

examples/3d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn setup(mut commands: Commands, mut lines: DebugLines) {
2323
Vec3::new(-1.0, 1.0, 1.0),
2424
100.0,
2525
Color::CYAN,
26-
Color::ORANGE_RED,
26+
Color::MIDNIGHT_BLUE,
2727
);
2828
}
2929

src/lib.rs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ pub const MAX_LINES: usize = MAX_POINTS / 2;
7373
pub const MAX_POINTS: usize = MAX_POINTS_PER_MESH * DEBUG_LINES_MESH_COUNT;
7474

7575
fn spawn_debug_lines_mesh(meshes: &mut Assets<Mesh>, retain: DebugLinesMesh) -> impl Bundle {
76+
let is_immediate = matches!(retain, DebugLinesMesh::Immediate(_));
7677
(
77-
meshes.add(debug_lines_mesh()),
78+
meshes.add(debug_lines_mesh(is_immediate)),
7879
Transform::default(),
7980
GlobalTransform::default(),
8081
Visibility::default(),
@@ -111,7 +112,7 @@ fn update_debug_lines_mesh(
111112
}
112113

113114
/// Initialize [`DebugLinesMesh`]'s [`Mesh`].
114-
fn debug_lines_mesh() -> Mesh {
115+
fn debug_lines_mesh(is_immediate: bool) -> Mesh {
115116
let mut mesh = Mesh::new(PrimitiveTopology::LineList);
116117
mesh.set_attribute(
117118
Mesh::ATTRIBUTE_POSITION,
@@ -121,22 +122,24 @@ fn debug_lines_mesh() -> Mesh {
121122
Mesh::ATTRIBUTE_COLOR,
122123
VertexAttributeValues::Float32x4(Vec::with_capacity(256)),
123124
);
124-
mesh.set_indices(Some(Indices::U16(Vec::with_capacity(256))));
125+
if !is_immediate {
126+
mesh.set_indices(Some(Indices::U16(Vec::with_capacity(256))));
127+
}
125128
mesh
126129
}
127130

128131
/// Move the DebugLinesMesh marker Component to the render context.
129-
fn extract_debug_lines(mut commands: Commands, query: Query<(Entity, &DebugLinesMesh)>) {
130-
for (entity, mesh_info) in query.iter() {
131-
commands.get_or_spawn(entity).insert(mesh_info.clone());
132+
fn extract_debug_lines(mut commands: Commands, query: Query<Entity, With<DebugLinesMesh>>) {
133+
for entity in query.iter() {
134+
commands.get_or_spawn(entity).insert(RenderDebugLinesMesh);
132135
}
133136
}
134137

135138
/// Marker Component for the [`Entity`] associated with the meshes rendered with the
136139
/// debuglines.wgsl shader.
137140
///
138141
/// Stores the index of the mesh for the logic of `ImmLinesStorage` and `RetLinesStorage`
139-
#[derive(Component, Debug, Clone)]
142+
#[derive(Component)]
140143
enum DebugLinesMesh {
141144
/// Meshes for duration=0.0 lines
142145
Immediate(usize),
@@ -149,6 +152,9 @@ impl Default for DebugLinesMesh {
149152
}
150153
}
151154

155+
#[derive(Component)]
156+
struct RenderDebugLinesMesh;
157+
152158
// NOTE: consider this: we could just hold a Handle<Mesh> to the DebugLinesMesh
153159
// and modify it in-place, so that there is no need to update the mesh every
154160
// frame on top of keeping track of all those buffers in `LinesStorage`.
@@ -194,23 +200,6 @@ impl ImmLinesStorage {
194200
}
195201
}
196202

197-
fn fill_indices(&self, buffer: &mut Vec<u16>, mesh: usize) {
198-
buffer.clear();
199-
let start = mesh << 16;
200-
let end = (mesh + 1) << 16;
201-
let buffer_size = self.positions.len();
202-
let indices = if start < buffer_size && buffer_size >= end {
203-
// Because MAX_POINTS_PER_MESH == 2^16
204-
0xFFFF_u16
205-
} else if start < buffer_size && buffer_size < end {
206-
(buffer_size - start) as u16
207-
} else {
208-
0
209-
};
210-
// Because MAX_POINTS_PER_MESH == 2^16, this needs to be inclusive
211-
buffer.extend(0..=indices);
212-
}
213-
214203
fn mark_expired(&mut self) {
215204
self.positions.clear();
216205
self.colors.clear();
@@ -232,9 +221,6 @@ impl ImmLinesStorage {
232221

233222
fn fill_attributes(&self, mesh: &mut Mesh, mesh_index: usize) {
234223
use VertexAttributeValues::{Float32x3, Float32x4};
235-
if let Some(Indices::U16(indices)) = mesh.indices_mut() {
236-
self.fill_indices(indices, mesh_index);
237-
}
238224
if let Some(Float32x3(vbuffer)) = mesh.attribute_mut(Mesh::ATTRIBUTE_POSITION) {
239225
self.fill_vertexes(vbuffer, mesh_index);
240226
}
@@ -458,7 +444,7 @@ impl SpecializedPipeline for DebugLinePipeline {
458444
descriptor.fragment.as_mut().unwrap().shader = self.shader.clone_weak();
459445
descriptor.primitive.topology = PrimitiveTopology::LineList;
460446
descriptor.primitive.cull_mode = None;
461-
// TODO: set this to None to remove depth check
447+
// TODO: set this to a large value to remove depth check
462448
descriptor.depth_stencil.as_mut().unwrap().bias.slope_scale = 1.0;
463449
descriptor
464450
}
@@ -470,7 +456,7 @@ fn queue_debug_lines(
470456
mut pipeline_cache: ResMut<RenderPipelineCache>,
471457
mut specialized_pipelines: ResMut<SpecializedPipelines<DebugLinePipeline>>,
472458
msaa: Res<Msaa>,
473-
material_meshes: Query<(Entity, &MeshUniform), With<DebugLinesMesh>>,
459+
material_meshes: Query<(Entity, &MeshUniform), With<RenderDebugLinesMesh>>,
474460
mut views: Query<(&ExtractedView, &mut RenderPhase<Opaque3d>)>,
475461
) {
476462
let draw_custom = opaque_3d_draw_functions

0 commit comments

Comments
 (0)