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

Option to enable deterministic rendering #11248

Merged
merged 2 commits into from
Jan 9, 2024

Conversation

stepancheg
Copy link
Contributor

@stepancheg stepancheg commented Jan 7, 2024

Objective

Issue #10243: rendering multiple triangles in the same place results in flickering.

Solution

Considered these alternatives:

  • depth_bias may not work, because of high number of entities, so creating a material per entity is practically not possible
  • rendering at slightly different positions does not work, because when camera is far, float rounding causes the same issues (edit: assuming we have to use the same depth_bias)
  • considered implementing deterministic operation like query.par_iter().flat_map(...).collect() to be used in check_visibility system (which would solve the issue since query is deterministic), and could not figure out how to make it as cheap as current approach with thread-local collectors (QueryParIter::map_collect and similar operations #11249)

So adding an option to sort entities after check_visibility system run.

Should not be too bad, because after visibility check, only a handful entities remain.

This is probably not the only source of non-determinism in Bevy, but this is one I could find so far. At least it fixes the repro example.

Changelog

  • DeterministicRenderingConfig option to enable deterministic rendering

Test

image

@robtfm
Copy link
Contributor

robtfm commented Jan 7, 2024

  • rendering at slightly different positions does not work, because when camera is far, float rounding causes the same issues

My understanding is that the bias applies in depth buffer increments, so a bias of 1 should always result in an overwrite regardless of distance, but I haven’t tested it.

Either way making an option to stablesort is totally reasonable. The caveat regarding entity id reuse - making explicit/deliberate ordering difficult - still applies, but I guess a random stable order can still be better than a random unstable order to avoid flickering.

Cargo.toml Outdated Show resolved Hide resolved
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Seems like a reasonable compromise for users who prefer this behavior. Flickering can be much worse than z-fighting, and "fix your models" is not always an option. The branch doesn't bother me: this isn't inside the hot loop.

Some small suggestions on how to explain this feature to users, but otherwise this LGTM. If there are better solutions, this is easy to amend or extend later.

@stepancheg
Copy link
Contributor Author

stepancheg commented Jan 8, 2024

Applied suggestions, although Z-fighting might not be precisely correct here.

This PR does not fix z-fighting, it only fixes flickering when Z-fighting already occurs, but Z-fighting stays.

@stepancheg stepancheg force-pushed the deterministic branch 3 times, most recently from 873f21a to cfc756e Compare January 8, 2024 02:14
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Good, that's much clearer. And yeah, that's a sensible distinction: we're still z-fighting, but at least it's not constantly unstable.

Copy link
Contributor

@rdrpenguin04 rdrpenguin04 left a comment

Choose a reason for hiding this comment

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

Either this should be fixed if it's wrong, or there should be a comment to affirm that it's right. I'm assuming you chose sort_unstable for a reason.

crates/bevy_render/src/view/visibility/mod.rs Show resolved Hide resolved
@stepancheg
Copy link
Contributor Author

Either this should be fixed if it's wrong, or there should be a comment to affirm that it's right.

AFAIU this is by design. I've added plenty of comments explaining current behavior. If you have suggestions how to rephrase the comments to make it more clear yet keeping comment concise, you are welcome.

Copy link
Contributor

@robtfm robtfm left a comment

Choose a reason for hiding this comment

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

minor comment issue notwithstanding, lgtm and works as expected

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Jan 8, 2024
Copy link
Contributor

@rdrpenguin04 rdrpenguin04 left a comment

Choose a reason for hiding this comment

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

LGTM

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 8, 2024
@alice-i-cecile alice-i-cecile removed this pull request from the merge queue due to a manual request Jan 8, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 9, 2024
Merged via the queue into bevyengine:main with commit 06bf928 Jan 9, 2024
26 checks passed
@stepancheg stepancheg deleted the deterministic branch January 9, 2024 02:31
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-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants