Skip to content

Commit

Permalink
Implement non-indexed mesh rendering (#3415)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
nicopap committed Dec 23, 2021
1 parent 851b593 commit 035ec7b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
20 changes: 13 additions & 7 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy_ecs::{
use bevy_math::Mat4;
use bevy_reflect::TypeUuid;
use bevy_render::{
mesh::Mesh,
mesh::{GpuBufferInfo, Mesh},
render_asset::RenderAssets,
render_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
Expand Down Expand Up @@ -720,13 +720,19 @@ impl EntityRenderCommand for DrawMesh {
let mesh_handle = mesh_query.get(item).unwrap();
if let Some(gpu_mesh) = meshes.into_inner().get(mesh_handle) {
pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..));
if let Some(index_info) = &gpu_mesh.index_info {
pass.set_index_buffer(index_info.buffer.slice(..), 0, index_info.index_format);
pass.draw_indexed(0..index_info.count, 0, 0..1);
} else {
panic!("non-indexed drawing not supported yet")
match &gpu_mesh.buffer_info {
GpuBufferInfo::Indexed {
buffer,
index_format,
count,
} => {
pass.set_index_buffer(buffer.slice(..), 0, *index_format);
pass.draw_indexed(0..*count, 0, 0..1);
}
GpuBufferInfo::NonIndexed { vertex_count } => {
pass.draw(0..*vertex_count, 0..1);
}
}

RenderCommandResult::Success
} else {
RenderCommandResult::Failure
Expand Down
44 changes: 27 additions & 17 deletions crates/bevy_render/src/mesh/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,18 +591,23 @@ impl From<&Indices> for IndexFormat {
pub struct GpuMesh {
/// Contains all attribute data for each vertex.
pub vertex_buffer: Buffer,
pub index_info: Option<GpuIndexInfo>,
pub buffer_info: GpuBufferInfo,
pub has_tangents: bool,
pub primitive_topology: PrimitiveTopology,
}

/// The index info of a [`GpuMesh`].
/// The index/vertex buffer info of a [`GpuMesh`].
#[derive(Debug, Clone)]
pub struct GpuIndexInfo {
/// Contains all index data of a mesh.
pub buffer: Buffer,
pub count: u32,
pub index_format: IndexFormat,
pub enum GpuBufferInfo {
Indexed {
/// Contains all index data of a mesh.
buffer: Buffer,
count: u32,
index_format: IndexFormat,
},
NonIndexed {
vertex_count: u32,
},
}

impl RenderAsset for Mesh {
Expand All @@ -627,19 +632,24 @@ impl RenderAsset for Mesh {
contents: &vertex_buffer_data,
});

let index_info = mesh.get_index_buffer_bytes().map(|data| GpuIndexInfo {
buffer: render_device.create_buffer_with_data(&BufferInitDescriptor {
usage: BufferUsages::INDEX,
contents: data,
label: Some("Mesh Index Buffer"),
}),
count: mesh.indices().unwrap().len() as u32,
index_format: mesh.indices().unwrap().into(),
});
let buffer_info = mesh.get_index_buffer_bytes().map_or(
GpuBufferInfo::NonIndexed {
vertex_count: mesh.count_vertices() as u32,
},
|data| GpuBufferInfo::Indexed {
buffer: render_device.create_buffer_with_data(&BufferInitDescriptor {
usage: BufferUsages::INDEX,
contents: data,
label: Some("Mesh Index Buffer"),
}),
count: mesh.indices().unwrap().len() as u32,
index_format: mesh.indices().unwrap().into(),
},
);

Ok(GpuMesh {
vertex_buffer,
index_info,
buffer_info,
has_tangents: mesh.attributes.contains_key(Mesh::ATTRIBUTE_TANGENT),
primitive_topology: mesh.primitive_topology(),
})
Expand Down

0 comments on commit 035ec7b

Please sign in to comment.