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)).

<details><summary>Example image</summary>
<p>


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

</p>
</details> 

## Solution

Move `vertex_count` field from `GpuBufferInfo::NonIndexed` to `GpuMesh`

## Migration Guide

`vertex_count` is now stored directly on `GpuMesh` instead of
`GpuBufferInfo::NonIndexed`.
This commit is contained in:
ira 2023-04-22 19:28:58 +02:00 committed by GitHub
parent 149c86b2ae
commit 6b774c0fda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 16 deletions

View File

@ -1177,8 +1177,8 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMesh {
pass.set_index_buffer(buffer.slice(..), 0, *index_format); pass.set_index_buffer(buffer.slice(..), 0, *index_format);
pass.draw_indexed(0..*count, 0, 0..1); pass.draw_indexed(0..*count, 0, 0..1);
} }
GpuBufferInfo::NonIndexed { vertex_count } => { GpuBufferInfo::NonIndexed => {
pass.draw(0..*vertex_count, 0..1); pass.draw(0..gpu_mesh.vertex_count, 0..1);
} }
} }
RenderCommandResult::Success RenderCommandResult::Success

View File

@ -820,6 +820,7 @@ impl From<&Indices> for IndexFormat {
pub struct GpuMesh { pub struct GpuMesh {
/// Contains all attribute data for each vertex. /// Contains all attribute data for each vertex.
pub vertex_buffer: Buffer, pub vertex_buffer: Buffer,
pub vertex_count: u32,
pub buffer_info: GpuBufferInfo, pub buffer_info: GpuBufferInfo,
pub primitive_topology: PrimitiveTopology, pub primitive_topology: PrimitiveTopology,
pub layout: MeshVertexBufferLayout, pub layout: MeshVertexBufferLayout,
@ -834,9 +835,7 @@ pub enum GpuBufferInfo {
count: u32, count: u32,
index_format: IndexFormat, index_format: IndexFormat,
}, },
NonIndexed { NonIndexed,
vertex_count: u32,
},
} }
impl RenderAsset for Mesh { impl RenderAsset for Mesh {
@ -861,11 +860,8 @@ impl RenderAsset for Mesh {
contents: &vertex_buffer_data, contents: &vertex_buffer_data,
}); });
let buffer_info = mesh.get_index_buffer_bytes().map_or( let buffer_info = if let Some(data) = mesh.get_index_buffer_bytes() {
GpuBufferInfo::NonIndexed { GpuBufferInfo::Indexed {
vertex_count: mesh.count_vertices() as u32,
},
|data| GpuBufferInfo::Indexed {
buffer: render_device.create_buffer_with_data(&BufferInitDescriptor { buffer: render_device.create_buffer_with_data(&BufferInitDescriptor {
usage: BufferUsages::INDEX, usage: BufferUsages::INDEX,
contents: data, contents: data,
@ -873,13 +869,16 @@ impl RenderAsset for Mesh {
}), }),
count: mesh.indices().unwrap().len() as u32, count: mesh.indices().unwrap().len() as u32,
index_format: mesh.indices().unwrap().into(), index_format: mesh.indices().unwrap().into(),
}, }
); } else {
GpuBufferInfo::NonIndexed
};
let mesh_vertex_buffer_layout = mesh.get_mesh_vertex_buffer_layout(); let mesh_vertex_buffer_layout = mesh.get_mesh_vertex_buffer_layout();
Ok(GpuMesh { Ok(GpuMesh {
vertex_buffer, vertex_buffer,
vertex_count: mesh.count_vertices() as u32,
buffer_info, buffer_info,
primitive_topology: mesh.primitive_topology(), primitive_topology: mesh.primitive_topology(),
layout: mesh_vertex_buffer_layout, layout: mesh_vertex_buffer_layout,

View File

@ -599,8 +599,8 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMesh2d {
pass.set_index_buffer(buffer.slice(..), 0, *index_format); pass.set_index_buffer(buffer.slice(..), 0, *index_format);
pass.draw_indexed(0..*count, 0, 0..1); pass.draw_indexed(0..*count, 0, 0..1);
} }
GpuBufferInfo::NonIndexed { vertex_count } => { GpuBufferInfo::NonIndexed => {
pass.draw(0..*vertex_count, 0..1); pass.draw(0..gpu_mesh.vertex_count, 0..1);
} }
} }
RenderCommandResult::Success RenderCommandResult::Success

View File

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