bevy/crates/bevy_pbr/src/render
James O'Brien 4f1d9a6315
Reorder render sets, refactor bevy_sprite to take advantage (#9236)
This is a continuation of this PR: #8062 

# Objective

- Reorder render schedule sets to allow data preparation when phase item
order is known to support improved batching
- Part of the batching/instancing etc plan from here:
https://github.com/bevyengine/bevy/issues/89#issuecomment-1379249074
- The original idea came from @inodentry and proved to be a good one.
Thanks!
- Refactor `bevy_sprite` and `bevy_ui` to take advantage of the new
ordering

## Solution
- Move `Prepare` and `PrepareFlush` after `PhaseSortFlush` 
- Add a `PrepareAssets` set that runs in parallel with other systems and
sets in the render schedule.
  - Put prepare_assets systems in the `PrepareAssets` set
- If explicit dependencies are needed on Mesh or Material RenderAssets
then depend on the appropriate system.
- Add `ManageViews` and `ManageViewsFlush` sets between
`ExtractCommands` and Queue
- Move `queue_mesh*_bind_group` to the Prepare stage
  - Rename them to `prepare_`
- Put systems that prepare resources (buffers, textures, etc.) into a
`PrepareResources` set inside `Prepare`
- Put the `prepare_..._bind_group` systems into a `PrepareBindGroup` set
after `PrepareResources`
- Move `prepare_lights` to the `ManageViews` set
  - `prepare_lights` creates views and this must happen before `Queue`
  - This system needs refactoring to stop handling all responsibilities
- Gather lights, sort, and create shadow map views. Store sorted light
entities in a resource

- Remove `BatchedPhaseItem`
- Replace `batch_range` with `batch_size` representing how many items to
skip after rendering the item or to skip the item entirely if
`batch_size` is 0.
- `queue_sprites` has been split into `queue_sprites` for queueing phase
items and `prepare_sprites` for batching after the `PhaseSort`
  - `PhaseItem`s are still inserted in `queue_sprites`
- After sorting adjacent compatible sprite phase items are accumulated
into `SpriteBatch` components on the first entity of each batch,
containing a range of vertex indices. The associated `PhaseItem`'s
`batch_size` is updated appropriately.
- `SpriteBatch` items are then drawn skipping over the other items in
the batch based on the value in `batch_size`
- A very similar refactor was performed on `bevy_ui`
---

## Changelog

Changed:
- Reordered and reworked render app schedule sets. The main change is
that data is extracted, queued, sorted, and then prepared when the order
of data is known.
- Refactor `bevy_sprite` and `bevy_ui` to take advantage of the
reordering.

## Migration Guide
- Assets such as materials and meshes should now be created in
`PrepareAssets` e.g. `prepare_assets<Mesh>`
- Queueing entities to `RenderPhase`s continues to be done in `Queue`
e.g. `queue_sprites`
- Preparing resources (textures, buffers, etc.) should now be done in
`PrepareResources`, e.g. `prepare_prepass_textures`,
`prepare_mesh_uniforms`
- Prepare bind groups should now be done in `PrepareBindGroups` e.g.
`prepare_mesh_bind_group`
- Any batching or instancing can now be done in `Prepare` where the
order of the phase items is known e.g. `prepare_sprites`

 
## Next Steps
- Introduce some generic mechanism to ensure items that can be batched
are grouped in the phase item order, currently you could easily have
`[sprite at z 0, mesh at z 0, sprite at z 0]` preventing batching.
 - Investigate improved orderings for building the MeshUniform buffer
 - Implementing batching across the rest of bevy

---------

Co-authored-by: Robert Swain <robert.swain@gmail.com>
Co-authored-by: robtfm <50659922+robtfm@users.noreply.github.com>
2023-08-27 14:33:49 +00:00
..
clustered_forward.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
fog.rs Reorder render sets, refactor bevy_sprite to take advantage (#9236) 2023-08-27 14:33:49 +00:00
fog.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
light.rs Reorder render sets, refactor bevy_sprite to take advantage (#9236) 2023-08-27 14:33:49 +00:00
mesh.rs Reorder render sets, refactor bevy_sprite to take advantage (#9236) 2023-08-27 14:33:49 +00:00
mesh.wgsl Reduce the size of MeshUniform to improve performance (#9416) 2023-08-15 06:00:23 +00:00
mesh_bindings.rs Use GpuArrayBuffer for MeshUniform (#9254) 2023-07-30 13:17:08 +00:00
mesh_bindings.wgsl Use GpuArrayBuffer for MeshUniform (#9254) 2023-07-30 13:17:08 +00:00
mesh_functions.wgsl Reduce the size of MeshUniform to improve performance (#9416) 2023-08-15 06:00:23 +00:00
mesh_types.wgsl Reduce the size of MeshUniform to improve performance (#9416) 2023-08-15 06:00:23 +00:00
mesh_vertex_output.wgsl Use GpuArrayBuffer for MeshUniform (#9254) 2023-07-30 13:17:08 +00:00
mesh_view_bindings.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
mesh_view_types.wgsl EnvironmentMapLight support for WebGL2 (#7737) 2023-02-20 00:02:40 +00:00
mod.rs Add morph targets (#8158) 2023-06-22 20:00:01 +00:00
morph.rs Fix typos throughout the project (#9090) 2023-07-10 00:11:51 +00:00
morph.wgsl Fix typos throughout the project (#9090) 2023-07-10 00:11:51 +00:00
parallax_mapping.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
pbr.wgsl Use GpuArrayBuffer for MeshUniform (#9254) 2023-07-30 13:17:08 +00:00
pbr_ambient.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
pbr_bindings.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
pbr_functions.wgsl Use GpuArrayBuffer for MeshUniform (#9254) 2023-07-30 13:17:08 +00:00
pbr_lighting.wgsl Fix typos throughout the project (#9090) 2023-07-10 00:11:51 +00:00
pbr_prepass.wgsl fix prepass normal_mapping (#8978) 2023-06-29 00:28:34 +00:00
pbr_types.wgsl Add parallax mapping to bevy PBR (#5928) 2023-04-15 10:25:14 +00:00
shadows.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
skinning.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
utils.wgsl Screen Space Ambient Occlusion (SSAO) MVP (#7402) 2023-06-18 21:05:55 +00:00
wireframe.wgsl fix wireframe after MeshUniform size reduction (#9505) 2023-08-21 07:53:50 +00:00