From 6b774c0fdacb4ea4c606115bf2ccaf2fe6700fc6 Mon Sep 17 00:00:00 2001 From: ira Date: Sat, 22 Apr 2023 19:28:58 +0200 Subject: [PATCH] Compute `vertex_count` for indexed meshes on `GpuMesh` (#8460) # Objective Compute the `vertex_count` for indexed meshes as well as non-indexed meshes. I will need this in a future PR based on #8427 that adds a gizmo component that draws the normals of a mesh when attached to an entity ([branch](https://github.com/devil-ira/bevy/compare/instanced-line-rendering...devil-ira:bevy:instanced-line-rendering-normals)).
Example image

![image](https://user-images.githubusercontent.com/29694403/233789526-cb5feb47-0aa7-4e69-90a2-e31ec24aadff.png)

## Solution Move `vertex_count` field from `GpuBufferInfo::NonIndexed` to `GpuMesh` ## Migration Guide `vertex_count` is now stored directly on `GpuMesh` instead of `GpuBufferInfo::NonIndexed`. --- crates/bevy_pbr/src/render/mesh.rs | 4 ++-- crates/bevy_render/src/mesh/mesh/mod.rs | 19 +++++++++---------- crates/bevy_sprite/src/mesh2d/mesh.rs | 4 ++-- examples/shader/shader_instancing.rs | 4 ++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 72bc2a5a6e..61f0eadba9 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -1177,8 +1177,8 @@ impl RenderCommand

for DrawMesh { 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); + GpuBufferInfo::NonIndexed => { + pass.draw(0..gpu_mesh.vertex_count, 0..1); } } RenderCommandResult::Success diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 3afe4c7bcd..7a5fcbaf17 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -820,6 +820,7 @@ impl From<&Indices> for IndexFormat { pub struct GpuMesh { /// Contains all attribute data for each vertex. pub vertex_buffer: Buffer, + pub vertex_count: u32, pub buffer_info: GpuBufferInfo, pub primitive_topology: PrimitiveTopology, pub layout: MeshVertexBufferLayout, @@ -834,9 +835,7 @@ pub enum GpuBufferInfo { count: u32, index_format: IndexFormat, }, - NonIndexed { - vertex_count: u32, - }, + NonIndexed, } impl RenderAsset for Mesh { @@ -861,11 +860,8 @@ impl RenderAsset for Mesh { contents: &vertex_buffer_data, }); - let buffer_info = mesh.get_index_buffer_bytes().map_or( - GpuBufferInfo::NonIndexed { - vertex_count: mesh.count_vertices() as u32, - }, - |data| GpuBufferInfo::Indexed { + let buffer_info = if let Some(data) = mesh.get_index_buffer_bytes() { + GpuBufferInfo::Indexed { buffer: render_device.create_buffer_with_data(&BufferInitDescriptor { usage: BufferUsages::INDEX, contents: data, @@ -873,13 +869,16 @@ impl RenderAsset for Mesh { }), count: mesh.indices().unwrap().len() as u32, index_format: mesh.indices().unwrap().into(), - }, - ); + } + } else { + GpuBufferInfo::NonIndexed + }; let mesh_vertex_buffer_layout = mesh.get_mesh_vertex_buffer_layout(); Ok(GpuMesh { vertex_buffer, + vertex_count: mesh.count_vertices() as u32, buffer_info, primitive_topology: mesh.primitive_topology(), layout: mesh_vertex_buffer_layout, diff --git a/crates/bevy_sprite/src/mesh2d/mesh.rs b/crates/bevy_sprite/src/mesh2d/mesh.rs index 070898df55..d7b63d7d4c 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh.rs +++ b/crates/bevy_sprite/src/mesh2d/mesh.rs @@ -599,8 +599,8 @@ impl RenderCommand

for DrawMesh2d { 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); + GpuBufferInfo::NonIndexed => { + pass.draw(0..gpu_mesh.vertex_count, 0..1); } } RenderCommandResult::Success diff --git a/examples/shader/shader_instancing.rs b/examples/shader/shader_instancing.rs index dd533e32af..b9b53ea1ea 100644 --- a/examples/shader/shader_instancing.rs +++ b/examples/shader/shader_instancing.rs @@ -253,8 +253,8 @@ impl RenderCommand

for DrawMeshInstanced { pass.set_index_buffer(buffer.slice(..), 0, *index_format); pass.draw_indexed(0..*count, 0, 0..instance_buffer.length as u32); } - GpuBufferInfo::NonIndexed { vertex_count } => { - pass.draw(0..*vertex_count, 0..instance_buffer.length as u32); + GpuBufferInfo::NonIndexed => { + pass.draw(0..gpu_mesh.vertex_count, 0..instance_buffer.length as u32); } } RenderCommandResult::Success