bevy/crates/bevy_render/src
James Liu 7e6dd3f03e Allow unbatched render phases to use unstable sorts (#5049)
# Objective

Partially addresses #4291.

Speed up the sort phase for unbatched render phases.

## Solution
Split out one of the optimizations in #4899 and allow implementors of `PhaseItem` to change what kind of sort is used when sorting the items in the phase. This currently includes Stable, Unstable, and Unsorted. Each of these corresponds to `Vec::sort_by_key`, `Vec::sort_unstable_by_key`, and no sorting at all. The default is `Unstable`. The last one can be used as a default if users introduce a preliminary depth prepass.

## Performance
This will not impact the performance of any batched phases, as it is still using a stable sort. 2D's only phase is unchanged. All 3D phases are unbatched currently, and will benefit from this change.

On `many_cubes`, where the primary phase is opaque, this change sees a speed up from 907.02us -> 477.62us, a 47.35% reduction.

![image](https://user-images.githubusercontent.com/3137680/174471253-22424874-30d5-4db5-b5b4-65fb2c612a9c.png)

## Future Work
There were prior discussions to add support for faster radix sorts in #4291, which in theory should be a `O(n)` instead of a `O(nlog(n))` time. [`voracious`](https://crates.io/crates/voracious_radix_sort) has been proposed, but it seems to be optimize for use cases with more than 30,000 items, which may be atypical for most systems.

Another optimization included in #4899 is to reduce the size of a few of the IDs commonly used in `PhaseItem` implementations to shrink the types to make swapping/sorting faster. Both `CachedPipelineId` and `DrawFunctionId` could be reduced to `u32` instead of `usize`.

Ideally, this should automatically change to use stable sorts when `BatchedPhaseItem` is implemented on the same phase item type, but this requires specialization, which may not land in stable Rust for a short while.

---

## Changelog
Added: `PhaseItem::sort`

## Migration Guide
RenderPhases now default to a unstable sort (via `slice::sort_unstable_by_key`). This can typically improve sort phase performance, but may produce incorrect batching results when implementing `BatchedPhaseItem`. To revert to the older stable sort, manually implement `PhaseItem::sort` to implement a stable sort (i.e. via `slice::sort_by_key`).

Co-authored-by: Federico Rinaldi <gisquerin@gmail.com>
Co-authored-by: Robert Swain <robert.swain@gmail.com>
Co-authored-by: colepoirier <colepoirier@gmail.com>
2022-06-23 10:52:49 +00:00
..
camera bevy_reflect: put serialize into external ReflectSerialize type (#4782) 2022-06-20 17:18:58 +00:00
color bevy_reflect: put serialize into external ReflectSerialize type (#4782) 2022-06-20 17:18:58 +00:00
mesh bevy_render: Add attributes and attributes_mut methods to Mesh. (#3927) 2022-06-15 06:29:52 +00:00
primitives Faster assign lights to clusters (#4345) 2022-04-15 02:53:20 +00:00
render_graph Camera Driven Rendering (#4745) 2022-06-02 00:12:17 +00:00
render_phase Allow unbatched render phases to use unstable sorts (#5049) 2022-06-23 10:52:49 +00:00
render_resource Fix wasm examples (#4967) 2022-06-11 20:10:13 +00:00
renderer diagnostics: meaningful error when graph node has wrong number of inputs (#4924) 2022-06-06 15:47:52 +00:00
texture bevy_render: Fix KTX2 UASTC format mapping (#4569) 2022-06-17 00:14:02 +00:00
view Change check_visibility to use thread-local queues instead of a channel (#4663) 2022-06-21 18:10:27 +00:00
extract_component.rs ExtractResourcePlugin (#3745) 2022-05-30 18:36:03 +00:00
extract_resource.rs ExtractResourcePlugin (#3745) 2022-05-30 18:36:03 +00:00
lib.rs Change default Image FilterMode to Linear (#4465) 2022-06-11 09:13:37 +00:00
render_asset.rs Clippy improvements (#4665) 2022-05-31 01:38:07 +00:00
settings.rs bevy_render: Use RenderDevice to get limits/features and expose AdapterInfo (#3931) 2022-02-16 21:17:37 +00:00