bevy/crates/bevy_render/src/deterministic.rs
Stepan Koltsov 06bf928927
Option to enable deterministic rendering (#11248)
# 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 (#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

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/28969/c735bce1-3a71-44cd-8677-c19f6c0ee6bd">

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2024-01-09 00:46:01 +00:00

15 lines
597 B
Rust

use bevy_ecs::system::Resource;
/// Configure deterministic rendering to fix flickering due to z-fighting.
#[derive(Resource, Default)]
pub struct DeterministicRenderingConfig {
/// Sort visible entities by id before rendering to avoid flickering.
///
/// Render is parallel by default, and if there's z-fighting, it may cause flickering.
/// Default fix for the issue is to set `depth_bias` per material.
/// When it is not possible, entities sorting can be used.
///
/// This option costs performance and disabled by default.
pub stable_sort_z_fighting: bool,
}