Skip to content

Fix potential NaNs in bevy_pbr (one observed, four theoretical) - #26473

Open
Runi-c wants to merge 1 commit into
bevyengine:mainfrom
Runi-c:fix-nans
Open

Runi-c wants to merge 1 commit into
bevyengine:mainfrom
Runi-c:fix-nans

Conversation

@Runi-c

@Runi-c Runi-c commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Objective

image

I've only observed this on web, but I've seen it in at least 4 previous jam entries from Bevy Jams 7 and 8. Rarely, at certain camera angles, with the PBR lit surface shader, pixels will become NaN and then get exploded by the bloom shader.

I had Claude diagnose the issue. The problem traces to an unprotected pow(1.0 - VdotH, 5.0) in the Schlick approximation when calculating fresnel. The BRDF's NdotV which gets passed here can technically be greater than 1.0 due to float rounding. pow on negative values is undefined, and may produce a NaN which propagates to the bloom shader, creating the effect seen above.

Solution

Similar NaNs can theoretically happen anywhere an input is calculated and assumed to be strictly 0..1, and thus never saturated or capped, and then passed to functions with restricted domains which might produce NaNs, for example pow, sqrt, acos, log, etc. I had Claude look for likely problematic sites and checked them against Filament's source code before choosing which fixes to add in this PR.

Two approaches:

  • For the Schlick approximation, follow in the footsteps of Filament, Unreal, and Unity and use a pow5 helper instead of pow(x, 5.0). The helper just does x2 * x2 * x, which can't NaN the way pow does.
  • For the other potential sites, use either saturate or max(0.0, ..) as appropriate or conventional.

Note that this is not a comprehensive pass and there may be other problematic areas.

Testing

I confirmed the Schlick NaN fix by running the same game as in the screenshot above on web and confirming the flashes no longer appear. I'm not sure how to test the other potential NaN fixes.

AI Disclosure

I used Claude Opus 5.5 to diagnose this issue and implemented and confirmed the fix myself. I then had Claude search for similar code paths that could NaN in similar ways, and as best I could I researched how Filament and Unreal treat similar cases and followed either their convention or Bevy's convention per call site.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward AI-Assisted Discloses the use of AI in any way labels Nov 10, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Rendering Nov 10, 2026
@jgayfer jgayfer added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Nov 19, 2026
@alice-i-cecile

Copy link
Copy Markdown
Member

Let me know when merge conflicts are resolved and we'll get this in :)

@alice-i-cecile alice-i-cecile added the S-Merge-Conflicts Merge conflicts :( Add this label on top of other S- labels. label Nov 19, 2026
@Runi-c

Runi-c commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@alice-i-cecile I fixed the merge conflicts, which came from a fix for the same white flashes issue that motivated this PR: #26335

This PR still changes it to the pow5 helper (which as stated I found in every other reference, as it's likely faster as well as being safe), but now it's kinda just one micro-optimization plus four theoretical fixes for unconfirmed issues. Dunno if worth, but probably won't hurt!

This branch has not been deployed

No deployments
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 AI-Assisted Discloses the use of AI in any way C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Merge-Conflicts Merge conflicts :( Add this label on top of other S- labels. S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

4 participants