Skip to content

Handle unallocated GPU memory for mesh extraction #20112

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mate-h
Copy link
Contributor

@mate-h mate-h commented Jul 13, 2025

Objective

Fix examples/asset/alter_mesh where the right-hand mesh disappeared and the cube flickered when swapping between cube and sphere at runtime.

Fixes #18967

Solution

Early-exit RenderMeshInstanceGpuBuilder::update when the mesh’s GPU slice is not yet allocated and queue the entity for re-extraction on the next frame.

Testing

Ran cargo run --example alter_mesh on macOS/Metal, toggling meshes (Space) and vertex scaling (Return) for several minutes; verified both meshes remain visible, no flicker occurs, and performance is unchanged.


Showcase

Screenshot 2025-07-13 at 10 19 06 AM

None => (0, 0),
None => {
// GPU memory for this mesh hasn't been allocated yet. Retry next frame.
meshes_to_reextract_next_frame.insert(entity);
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is the fix, we might need to do the same for the mesh_index_slice a few lines below this.

I'm unsure why it previously didn't return None in this case. I feel like this might not be the full fix. I'll need to investigate further.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, thinking about this more, no, we shouldn't do this for the index_slice because not having indices is a valid case

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a case where vertex_slice is ready, but not index_slice?

Copy link
Contributor

Choose a reason for hiding this comment

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

For non indexed meshes index_slice will never be ready. I'm not sure it's possible for an indexed mesh to not have the index_slice ready. AFAIK they are allocated together but I could be wrong.

@@ -1098,7 +1098,11 @@ impl RenderMeshInstanceGpuBuilder {
mesh_vertex_slice.range.start,
mesh_vertex_slice.range.end - mesh_vertex_slice.range.start,
),
None => (0, 0),
None => {
// GPU memory for this mesh hasn't been allocated yet. Retry next frame.
Copy link
Contributor

Choose a reason for hiding this comment

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

What cases will this occur?

We don't want to end up in an infinite retry loop for failed meshes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure there's a way to detect this here? With that said it's also possible this reextract thing should happen somewhere else and not here.

Copy link
Member

Choose a reason for hiding this comment

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

I really don't like that this is an Option: a Result would have a lot clearer semantics to me.

Copy link
Contributor

@IceSentry IceSentry 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 still a bit worried as to why the original code didn't consider this an invalid case and exit early, but this looks correct to me.

Also it feels a bit weird that this function is the one that modifies meshes_to_reextract_next_frame. I feel like it should be the job of the parent to decide what to do when a None is returned but that's out of scope for this PR and I don't know if this should change.

@janhohenheim janhohenheim added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Jul 13, 2025
@janhohenheim janhohenheim added the C-Examples An addition or correction to our examples label Jul 13, 2025
@janhohenheim janhohenheim added this to the 0.17 milestone Jul 13, 2025
@alice-i-cecile alice-i-cecile requested a review from superdump July 14, 2025 21:42
@alice-i-cecile
Copy link
Member

@superdump I'm not confident on this PR, so I'm going to skip it on my merge train. It feels like this is a good bug fix inside of messy code, and I don't have the rendering expertise to judge whether we should merge it as is or block it on a more comprehensive cleanup.

@mate-h
Copy link
Contributor Author

mate-h commented Jul 14, 2025

Based on the feedback I got so far it seems we should block the change for a more comprehensive cleanup, I support this decision to skip for the merge train now.

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Jul 14, 2025
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 C-Examples An addition or correction to our examples D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Mesh not showing(or updating) in alter_mesh example
5 participants