Skip to content

Commit

Permalink
Compute vertex_count for indexed meshes on GpuMesh (#8460)
Browse files Browse the repository at this point in the history
# 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](irate-devil/bevy@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`.
  • Loading branch information
irate-devil committed Apr 22, 2023
1 parent 149c86b commit 6b774c0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,8 +1177,8 @@ impl<P: PhaseItem> RenderCommand<P> 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
Expand Down
19 changes: 9 additions & 10 deletions crates/bevy_render/src/mesh/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -834,9 +835,7 @@ pub enum GpuBufferInfo {
count: u32,
index_format: IndexFormat,
},
NonIndexed {
vertex_count: u32,
},
NonIndexed,
}

impl RenderAsset for Mesh {
Expand All @@ -861,25 +860,25 @@ 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,
label: Some("Mesh Index Buffer"),
}),
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,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,8 @@ impl<P: PhaseItem> RenderCommand<P> 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
Expand Down
4 changes: 2 additions & 2 deletions examples/shader/shader_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ impl<P: PhaseItem> RenderCommand<P> 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
Expand Down

0 comments on commit 6b774c0

Please sign in to comment.