Skip to content

Commit

Permalink
Implement non-indexed mesh rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
nicopap committed Dec 22, 2021
1 parent a3c53e6 commit 11257db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
16 changes: 9 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::{Mesh, GpuBufferInfo},
render_asset::RenderAssets,
render_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
Expand Down Expand Up @@ -711,13 +711,15 @@ 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: None,
}),
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: None,
}),
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 11257db

Please sign in to comment.