bevy/crates
Patrick Walton bc34216929
Pack multiple vertex and index arrays together into growable buffers. (#14257)
This commit uses the [`offset-allocator`] crate to combine vertex and
index arrays from different meshes into single buffers. Since the
primary source of `wgpu` overhead is from validation and synchronization
when switching buffers, this significantly improves Bevy's rendering
performance on many scenes.

This patch is a more flexible version of #13218, which also used slabs.
Unlike #13218, which used slabs of a fixed size, this commit implements
slabs that start small and can grow. In addition to reducing memory
usage, supporting slab growth reduces the number of vertex and index
buffer switches that need to happen during rendering, leading to
improved performance. To prevent pathological fragmentation behavior,
slabs are capped to a maximum size, and mesh arrays that are too large
get their own dedicated slabs.

As an additional improvement over #13218, this commit allows the
application to customize all allocator heuristics. The
`MeshAllocatorSettings` resource contains values that adjust the minimum
and maximum slab sizes, the cutoff point at which meshes get their own
dedicated slabs, and the rate at which slabs grow. Hopefully-sensible
defaults have been chosen for each value.

Unfortunately, WebGL 2 doesn't support the *base vertex* feature, which
is necessary to pack vertex arrays from different meshes into the same
buffer. `wgpu` represents this restriction as the downlevel flag
`BASE_VERTEX`. This patch detects that bit and ensures that all vertex
buffers get dedicated slabs on that platform. Even on WebGL 2, though,
we can combine all *index* arrays into single buffers to reduce buffer
changes, and we do so.

The following measurements are on Bistro:

Overall frame time improves from 8.74 ms to 5.53 ms (1.58x speedup):
![Screenshot 2024-07-09
163521](https://github.com/bevyengine/bevy/assets/157897/5d83c824-c0ee-434c-bbaf-218ff7212c48)

Render system time improves from 6.57 ms to 3.54 ms (1.86x speedup):
![Screenshot 2024-07-09
163559](https://github.com/bevyengine/bevy/assets/157897/d94e2273-c3a0-496a-9f88-20d394129610)

Opaque pass time improves from 4.64 ms to 2.33 ms (1.99x speedup):
![Screenshot 2024-07-09
163536](https://github.com/bevyengine/bevy/assets/157897/e4ef6e48-d60e-44ae-9a71-b9a731c99d9a)

## Migration Guide

### Changed

* Vertex and index buffers for meshes may now be packed alongside other
buffers, for performance.
* `GpuMesh` has been renamed to `RenderMesh`, to reflect the fact that
it no longer directly stores handles to GPU objects.
* Because meshes no longer have their own vertex and index buffers, the
responsibility for the buffers has moved from `GpuMesh` (now called
`RenderMesh`) to the `MeshAllocator` resource. To access the vertex data
for a mesh, use `MeshAllocator::mesh_vertex_slice`. To access the index
data for a mesh, use `MeshAllocator::mesh_index_slice`.

[`offset-allocator`]: https://github.com/pcwalton/offset-allocator
2024-07-16 20:33:15 +00:00
..
bevy_a11y plugin_group! macro (adopted) (#14339) 2024-07-16 01:14:33 +00:00
bevy_animation Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_app plugin_group! macro (adopted) (#14339) 2024-07-16 01:14:33 +00:00
bevy_asset add debug logging to ascertain the base path the asset server is using (#13820) 2024-07-15 14:00:43 +00:00
bevy_audio Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_color Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_core Rename bevy_core::name::DebugName to bevy_core::name::NameOrEntity (#14211) 2024-07-15 15:21:41 +00:00
bevy_core_pipeline Fix error/typo in SMAA shader (#14338) 2024-07-15 23:40:39 +00:00
bevy_derive Specify test group names in github summary for compile fail tests (#14330) 2024-07-15 16:13:03 +00:00
bevy_dev_tools plugin_group! macro (adopted) (#14339) 2024-07-16 01:14:33 +00:00
bevy_diagnostic Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_dylib Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_dynamic_plugin Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_ecs Add insert_by_id and try_insert_by_id to EntityCommands (#14283) 2024-07-15 23:29:13 +00:00
bevy_encase_derive Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_gilrs Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_gizmos plugin_group! macro (adopted) (#14339) 2024-07-16 01:14:33 +00:00
bevy_gltf Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_hierarchy Minimal Bubbling Observers (#13991) 2024-07-15 13:39:41 +00:00
bevy_input Expose Winit's KeyEvent::repeat in KeyboardInput (#14161) 2024-07-15 14:52:33 +00:00
bevy_internal plugin_group! macro (adopted) (#14339) 2024-07-16 01:14:33 +00:00
bevy_log Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_macro_utils Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_math Added new method to Cone 3D primitive (#14325) 2024-07-16 12:59:26 +00:00
bevy_mikktspace Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_pbr Pack multiple vertex and index arrays together into growable buffers. (#14257) 2024-07-16 20:33:15 +00:00
bevy_picking Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_ptr Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_reflect bevy_reflect: Improve DynamicFunction ergonomics (#14201) 2024-07-16 13:01:52 +00:00
bevy_render Pack multiple vertex and index arrays together into growable buffers. (#14257) 2024-07-16 20:33:15 +00:00
bevy_scene Align Scene::write_to_world_with to match DynamicScene::write_to_world_with (#13855) 2024-07-15 14:04:09 +00:00
bevy_sprite Pack multiple vertex and index arrays together into growable buffers. (#14257) 2024-07-16 20:33:15 +00:00
bevy_state plugin_group! macro (adopted) (#14339) 2024-07-16 01:14:33 +00:00
bevy_tasks Make Tasks functional on WASM (#13889) 2024-07-16 01:15:03 +00:00
bevy_text Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_time Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_transform Clarify GlobalTransform::transform_point (#14292) 2024-07-15 15:59:29 +00:00
bevy_ui Clean up UiSystem system sets (#14228) 2024-07-15 15:27:38 +00:00
bevy_utils Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_window Remove unused default feature from bevy_window (#14313) 2024-07-15 16:49:00 +00:00
bevy_winit Remove need for EventLoopProxy to be NonSend (#14198) 2024-07-16 06:59:01 +00:00