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

Fix underflow panic in InitTriInfo #14893

Merged
merged 3 commits into from
Aug 25, 2024
Merged

Conversation

kumikaya
Copy link
Contributor

@kumikaya kumikaya commented Aug 23, 2024

Objective

Solution

  • Change the place where a panic occurs from t < iNrTrianglesIn - 1 to t + 1 < iNrTrianglesIn.

Testing

  • After the fix, the following code runs successfully without any panic.
use bevy::prelude::Mesh;
use bevy_render::{
    mesh::{Indices, PrimitiveTopology},
    render_asset::RenderAssetUsages,
};

const POSITIONS: &[[f32; 3]] = &[[0.0, 1.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0]];

const NORMALS: &[[f32; 3]] = &[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]];

const INDICES: &[u32] = &[0, 1, 2];

const UVS: &[[f32; 2]] = &[[0.0, 1.0], [0.0, 0.0], [0.0, 1.0]];

fn main() {
    let mut mesh = Mesh::new(
        PrimitiveTopology::TriangleList,
        RenderAssetUsages::default(),
    );

    mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, POSITIONS.to_vec());
    mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, UVS.to_vec());
    mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, NORMALS.to_vec());
    mesh.insert_indices(Indices::U32(INDICES.to_vec()));
    mesh.generate_tangents().ok();

}

Migration Guide

  • No breaking changes introduced.

Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@janhohenheim
Copy link
Member

Note that this will add a merge conflict to #9050

@janhohenheim janhohenheim added C-Bug An unexpected or incorrect behavior S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 23, 2024
@alice-i-cecile alice-i-cecile added the A-Rendering Drawing game state to the screen label Aug 23, 2024
@alice-i-cecile
Copy link
Member

@ethereumdegen your review here would be appreciated.

Copy link
Member

@janhohenheim janhohenheim left a comment

Choose a reason for hiding this comment

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

I'm honestly not convinced that the code in question should run smoothly. That mesh seems whacky and bound to cause problems down the line. @ethereumdegen's offending mesh was similarly malformed. Do we at least print a warn! in this case?

@ethereumdegen
Copy link
Contributor

I'm honestly not convinced that the code in question should run smoothly. That mesh seems whacky and bound to cause problems down the line. @ethereumdegen's offending mesh was similarly malformed. Do we at least print a warn! in this case?

It doesnt have to run smoothly it can throw an error from the result

But it shouldnt crash the entire program

@janhohenheim janhohenheim added the D-Unsafe Touches with unsafe code in some way label Aug 23, 2024
@janhohenheim
Copy link
Member

janhohenheim commented Aug 23, 2024

Mesh::new doesn't return a result. Am I missing something?
Agreed that ideally we want to return an error somewhere instead of panicking, but it seems to me like this PR instead changes the behavior to instead silently pretend everything is alright.

@ethereumdegen
Copy link
Contributor

ethereumdegen commented Aug 23, 2024 via email

@janhohenheim
Copy link
Member

janhohenheim commented Aug 23, 2024

@ethereumdegen Ooooooh I see, I didn't scroll down enough on the example code, haha. That makes sense then :)
Then my issue becomes that the code posted above seems to not return an error on generate_tangents. I didn't run it, so I might be misunderstanding the behavior @kumikaya

@kumikaya
Copy link
Contributor Author

Maybe it would be more appropriate to change the place where a panic occurs from t < iNrTrianglesIn - 1 to t + 1 < iNrTrianglesIn.

@kumikaya kumikaya changed the title Change parameter type of InitTriInfo to prevent underflow panic Fix underflow panic in InitTriInfo Aug 23, 2024
Copy link
Member

@janhohenheim janhohenheim left a comment

Choose a reason for hiding this comment

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

@kumikaya I would prefer it if there was no panic at all, and instead generate_tangents returned an error in this case.
Also, no need to force push your commits to clean the history; the PR get squashed anyways and by leaving the history intact you make it easier for future readers :)

@kumikaya
Copy link
Contributor Author

@janhohenheim Yeah, it would be better to check if iNrTrianglesIn is zero and return false after iNrTrianglesIn = iTotTris - iDegenTriangles, but I'm not quite sure at which point it would be best to return; thanks for your advice.

@janhohenheim janhohenheim added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 23, 2024
@janhohenheim
Copy link
Member

@kumikaya feel free to ping me when the code you posted in the PR description results in generate_tangents returning an Err variant :)

@kumikaya
Copy link
Contributor Author

kumikaya commented Aug 24, 2024

@janhohenheim I've updated the code in the PR,genTangSpace returns false immediately when it detects that the number of valid triangles is 0. This causes generate_tangents, which indirectly calls genTangSpace, to return an Err variant.

Copy link
Member

@janhohenheim janhohenheim left a comment

Choose a reason for hiding this comment

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

Looks good to me then :)
Thanks for fixing the crash!

@janhohenheim janhohenheim removed the S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged label Aug 24, 2024
@janhohenheim janhohenheim added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Aug 24, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Aug 25, 2024
Merged via the queue into bevyengine:main with commit 9a2eb87 Aug 25, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior D-Unsafe Touches with unsafe code in some way S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

GenerateTangents in 0.14.1 can panic even though it is supposed to return Result
4 participants