From 11257db828b9f72eb2056d73ce998fc699592ad7 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Wed, 22 Dec 2021 14:04:29 +0100 Subject: [PATCH] Implement non-indexed mesh rendering --- crates/bevy_pbr/src/render/mesh.rs | 16 +++++---- crates/bevy_render/src/mesh/mesh/mod.rs | 44 +++++++++++++++---------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 42e4bb344dd573..f7abe24060cd19 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -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}, @@ -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 diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index c12d1a688dd911..ef6366ef3bc4c7 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -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, + 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 { @@ -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(), })