Skip to content

Commit 035ec7b

Browse files
committed
Implement non-indexed mesh rendering (#3415)
# Objective Instead of panicking when the `indices` field of a mesh is `None`, actually manage it. This is just a question of keeping track of the vertex buffer size. ## Notes * Relying on this change to improve performance on [bevy_debug_lines using the new renderer](Toqozz/bevy_debug_lines#10) * I'm still new to rendering, my only expertise with wgpu is the learn-wgpu tutorial, likely I'm overlooking something.
1 parent 851b593 commit 035ec7b

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bevy_ecs::{
1111
use bevy_math::Mat4;
1212
use bevy_reflect::TypeUuid;
1313
use bevy_render::{
14-
mesh::Mesh,
14+
mesh::{GpuBufferInfo, Mesh},
1515
render_asset::RenderAssets,
1616
render_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
1717
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
@@ -720,13 +720,19 @@ impl EntityRenderCommand for DrawMesh {
720720
let mesh_handle = mesh_query.get(item).unwrap();
721721
if let Some(gpu_mesh) = meshes.into_inner().get(mesh_handle) {
722722
pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..));
723-
if let Some(index_info) = &gpu_mesh.index_info {
724-
pass.set_index_buffer(index_info.buffer.slice(..), 0, index_info.index_format);
725-
pass.draw_indexed(0..index_info.count, 0, 0..1);
726-
} else {
727-
panic!("non-indexed drawing not supported yet")
723+
match &gpu_mesh.buffer_info {
724+
GpuBufferInfo::Indexed {
725+
buffer,
726+
index_format,
727+
count,
728+
} => {
729+
pass.set_index_buffer(buffer.slice(..), 0, *index_format);
730+
pass.draw_indexed(0..*count, 0, 0..1);
731+
}
732+
GpuBufferInfo::NonIndexed { vertex_count } => {
733+
pass.draw(0..*vertex_count, 0..1);
734+
}
728735
}
729-
730736
RenderCommandResult::Success
731737
} else {
732738
RenderCommandResult::Failure

crates/bevy_render/src/mesh/mesh/mod.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -591,18 +591,23 @@ impl From<&Indices> for IndexFormat {
591591
pub struct GpuMesh {
592592
/// Contains all attribute data for each vertex.
593593
pub vertex_buffer: Buffer,
594-
pub index_info: Option<GpuIndexInfo>,
594+
pub buffer_info: GpuBufferInfo,
595595
pub has_tangents: bool,
596596
pub primitive_topology: PrimitiveTopology,
597597
}
598598

599-
/// The index info of a [`GpuMesh`].
599+
/// The index/vertex buffer info of a [`GpuMesh`].
600600
#[derive(Debug, Clone)]
601-
pub struct GpuIndexInfo {
602-
/// Contains all index data of a mesh.
603-
pub buffer: Buffer,
604-
pub count: u32,
605-
pub index_format: IndexFormat,
601+
pub enum GpuBufferInfo {
602+
Indexed {
603+
/// Contains all index data of a mesh.
604+
buffer: Buffer,
605+
count: u32,
606+
index_format: IndexFormat,
607+
},
608+
NonIndexed {
609+
vertex_count: u32,
610+
},
606611
}
607612

608613
impl RenderAsset for Mesh {
@@ -627,19 +632,24 @@ impl RenderAsset for Mesh {
627632
contents: &vertex_buffer_data,
628633
});
629634

630-
let index_info = mesh.get_index_buffer_bytes().map(|data| GpuIndexInfo {
631-
buffer: render_device.create_buffer_with_data(&BufferInitDescriptor {
632-
usage: BufferUsages::INDEX,
633-
contents: data,
634-
label: Some("Mesh Index Buffer"),
635-
}),
636-
count: mesh.indices().unwrap().len() as u32,
637-
index_format: mesh.indices().unwrap().into(),
638-
});
635+
let buffer_info = mesh.get_index_buffer_bytes().map_or(
636+
GpuBufferInfo::NonIndexed {
637+
vertex_count: mesh.count_vertices() as u32,
638+
},
639+
|data| GpuBufferInfo::Indexed {
640+
buffer: render_device.create_buffer_with_data(&BufferInitDescriptor {
641+
usage: BufferUsages::INDEX,
642+
contents: data,
643+
label: Some("Mesh Index Buffer"),
644+
}),
645+
count: mesh.indices().unwrap().len() as u32,
646+
index_format: mesh.indices().unwrap().into(),
647+
},
648+
);
639649

640650
Ok(GpuMesh {
641651
vertex_buffer,
642-
index_info,
652+
buffer_info,
643653
has_tangents: mesh.attributes.contains_key(Mesh::ATTRIBUTE_TANGENT),
644654
primitive_topology: mesh.primitive_topology(),
645655
})

0 commit comments

Comments
 (0)