Skip to content

Reextract a mesh on the next frame if its material couldn't be prepared on the frame we first encountered it.#17963

Merged
superdump merged 1 commit intobevyengine:mainfrom
pcwalton:reextract-materials-if-not-allocated
Feb 22, 2025
Merged

Reextract a mesh on the next frame if its material couldn't be prepared on the frame we first encountered it.#17963
superdump merged 1 commit intobevyengine:mainfrom
pcwalton:reextract-materials-if-not-allocated

Conversation

@pcwalton
Copy link
Copy Markdown
Contributor

We might not be able to prepare a material on the first frame we encounter a mesh using it for various reasons, including that the material hasn't been loaded yet or that preparing the material is exceeding the per-frame cap on number of bytes to load. When this happens, we currently try to find the material in the MaterialBindGroupAllocator, fail, and then fall back to group 0, slot 0, the default MaterialBindGroupId, which is obviously incorrect. Worse, we then fail to dirty the mesh and reextract it when we do finish preparing the material, so the mesh will continue to be rendered with an incorrect material.

This patch fixes both problems. In collect_meshes_for_gpu_building, if we fail to find a mesh's material in the MeshBindGroupAllocator, then we detect that case, bail out, and add it to a list, MeshesToReextractNextFrame. On subsequent frames, we process all the meshes in MeshesToReextractNextFrame as though they were changed. This ensures both that we don't render a mesh if its material hasn't been loaded and that we start rendering the mesh once its material does load.

This was first noticed in the intermittent Pixel Eagle failures in the testbed_3d patch in #17898, although the problem has actually existed for some time. I believe it just so happened that the changes to the allocator in that PR caused the problem to appear more commonly than it did before.

on the frame we first encountered it.

We might not be able to prepare a material on the first frame we
encounter a mesh using it for various reasons, including that the
material hasn't been loaded yet or that preparing the material is
exceeding the per-frame cap on number of bytes to load. When this
happens, we currently try to find the material in the
`MaterialBindGroupAllocator`, fail, and then fall back to group 0, slot
0, the default `MaterialBindGroupId`, which is obviously incorrect.
Worse, we then fail to dirty the mesh and reextract it when we *do*
finish preparing the material, so the mesh will continue to be rendered
with an incorrect material.

This patch fixes both problems. In `collect_meshes_for_gpu_building`, if
we fail to find a mesh's material in the `MeshBindGroupAllocator`, then
we detect that case, bail out, and add it to a list,
`MeshesToReextractNextFrame`. On subsequent frames, we process all the
meshes in `MeshesToReextractNextFrame` as though they were changed. This
ensures both that we don't render a mesh if its material hasn't been
loaded and that we start rendering the mesh once its material does load.

This was first noticed in the intermittent Pixel Eagle failures in the
`testbed_3d` patch in bevyengine#17898, although the problem has actually existed
for some time. I believe it just so happened that the changes to the
allocator in that PR caused the problem to appear more commonly than it
did before.
@pcwalton pcwalton added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen P-Regression Functionality that used to work but no longer does. Add a test for this! S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 21, 2025
@rparrett
Copy link
Copy Markdown
Contributor

I documented some of the breakage that appeared after #17898 over here: #17970

I am not sure if there's a separate issue or if the fix here is incomplete.

@hukasu
Copy link
Copy Markdown
Contributor

hukasu commented Feb 21, 2025

on deferred_rendering example, the right cube (below the light) got fixed, but there is still a different issue on Forward + Prepass

Copy link
Copy Markdown
Contributor

@atlv24 atlv24 left a comment

Choose a reason for hiding this comment

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

Appreciate the minor cleanup refactors and type extraction.

@superdump superdump added this pull request to the merge queue Feb 22, 2025
Merged via the queue into bevyengine:main with commit 465306b Feb 22, 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 P-Regression Functionality that used to work but no longer does. Add a test for this! S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants