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

[Merged by Bors] - Validate vertex attribute format on insert #5259

Closed
wants to merge 4 commits into from
Closed
Changes from 3 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
38 changes: 30 additions & 8 deletions crates/bevy_render/src/mesh/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy_derive::EnumVariantMeta;
use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem};
use bevy_math::*;
use bevy_reflect::TypeUuid;
use bevy_utils::Hashed;
use bevy_utils::{tracing::error, Hashed};
use std::{collections::BTreeMap, hash::Hash, iter::FusedIterator};
use thiserror::Error;
use wgpu::{
Expand Down Expand Up @@ -101,19 +101,28 @@ impl Mesh {

/// Sets the data for a vertex attribute (position, normal etc.). The name will
/// often be one of the associated constants such as [`Mesh::ATTRIBUTE_POSITION`].
///
/// # Panics
/// Panics when the format of the values is not the expected attribute format
IceSentry marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn insert_attribute(
&mut self,
attribute: MeshVertexAttribute,
values: impl Into<VertexAttributeValues>,
) {
self.attributes.insert(
attribute.id,
MeshAttributeData {
attribute,
values: values.into(),
},
);
let values: VertexAttributeValues = values.into();

let values_format = VertexFormat::from(&values);
Copy link
Contributor

Choose a reason for hiding this comment

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

Kinda weird that this is a From impl instead of a .format() getter, and funnily enough this is going to be the only place it is used.
Ignore this comment, that's for another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair, but why not add it to this PR? It would make that code clearer and it seems directly related.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I just didn't want to push it onto you, could've worded that differently.
Feel free to :)

if values_format != attribute.format {
error!(
"Invalid attribute format for {}. Given format is {:?} but expected {:?}",
attribute.name, values_format, attribute.format
);
panic!("Failed to insert attribute");
}

self.attributes
.insert(attribute.id, MeshAttributeData { attribute, values });
}

/// Removes the data for a vertex attribute
Expand Down Expand Up @@ -994,3 +1003,16 @@ fn generate_tangents_for_mesh(mesh: &Mesh) -> Result<Vec<[f32; 4]>, GenerateTang

Ok(mikktspace_mesh.tangents)
}

#[cfg(test)]
mod tests {
use super::Mesh;
use wgpu::PrimitiveTopology;

#[test]
#[should_panic]
fn panic_invalid_format() {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, vec![[0.0, 0.0, 0.0]]);
}
}