Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute vertex_count for indexed meshes on GpuMesh #8460

Merged
merged 1 commit into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vertex count would be stored twice (once in GpuMesh.vertex_count field, once in GpuMesh.buffer_info.Indexed.count) With this solution.

Why not define vertex_count() as a method on GpuMesh? This way you inspect the content of buffer_info and return the value of either NonIndex.vertex_count or Indexed.count.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The count stored there is the size of the index buffer, not the amount of vertices in the mesh

Copy link
Contributor

@JMS55 JMS55 Apr 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing confused me at first. Imo, we should rename Indexed.count to Indexed.index_count or something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size would be appropriate then

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