bevy/crates/bevy_pbr/src/render
Patrick Walton a8f15bd95e
Introduce two-level bins for multidrawable meshes. (#16898)
Currently, our batchable binned items are stored in a hash table that
maps bin key, which includes the batch set key, to a list of entities.
Multidraw is handled by sorting the bin keys and accumulating adjacent
bins that can be multidrawn together (i.e. have the same batch set key)
into multidraw commands during `batch_and_prepare_binned_render_phase`.

This is reasonably efficient right now, but it will complicate future
work to retain indirect draw parameters from frame to frame. Consider
what must happen when we have retained indirect draw parameters and the
application adds a bin (i.e. a new mesh) that shares a batch set key
with some pre-existing meshes. (That is, the new mesh can be multidrawn
with the pre-existing meshes.) To be maximally efficient, our goal in
that scenario will be to update *only* the indirect draw parameters for
the batch set (i.e. multidraw command) containing the mesh that was
added, while leaving the others alone. That means that we have to
quickly locate all the bins that belong to the batch set being modified.

In the existing code, we would have to sort the list of bin keys so that
bins that can be multidrawn together become adjacent to one another in
the list. Then we would have to do a binary search through the sorted
list to find the location of the bin that was just added. Next, we would
have to widen our search to adjacent indexes that contain the same batch
set, doing expensive comparisons against the batch set key every time.
Finally, we would reallocate the indirect draw parameters and update the
stored pointers to the indirect draw parameters that the bins store.

By contrast, it'd be dramatically simpler if we simply changed the way
bins are stored to first map from batch set key (i.e. multidraw command)
to the bins (i.e. meshes) within that batch set key, and then from each
individual bin to the mesh instances. That way, the scenario above in
which we add a new mesh will be simpler to handle. First, we will look
up the batch set key corresponding to that mesh in the outer map to find
an inner map corresponding to the single multidraw command that will
draw that batch set. We will know how many meshes the multidraw command
is going to draw by the size of that inner map. Then we simply need to
reallocate the indirect draw parameters and update the pointers to those
parameters within the bins as necessary. There will be no need to do any
binary search or expensive batch set key comparison: only a single hash
lookup and an iteration over the inner map to update the pointers.

This patch implements the above technique. Because we don't have
retained bins yet, this PR provides no performance benefits. However, it
opens the door to maximally efficient updates when only a small number
of meshes change from frame to frame.

The main churn that this patch causes is that the *batch set key* (which
uniquely specifies a multidraw command) and *bin key* (which uniquely
specifies a mesh *within* that multidraw command) are now separate,
instead of the batch set key being embedded *within* the bin key.

In order to isolate potential regressions, I think that at least #16890,
#16836, and #16825 should land before this PR does.

## Migration Guide

* The *batch set key* is now separate from the *bin key* in
`BinnedPhaseItem`. The batch set key is used to collect multidrawable
meshes together. If you aren't using the multidraw feature, you can
safely set the batch set key to `()`.
2025-01-06 18:34:40 +00:00
..
clustered_forward.wgsl Cluster light probes using conservative spherical bounds. (#13746) 2024-12-05 13:07:10 +00:00
fog.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
fog.wgsl
forward_io.wgsl
gpu_preprocess.rs Remove tracing re-export from bevy_utils (#17161) 2025-01-05 23:06:34 +00:00
light.rs Introduce two-level bins for multidrawable meshes. (#16898) 2025-01-06 18:34:40 +00:00
mesh_bindings.rs Fix several regressions from recent rendering changes. (#16890) 2024-12-22 23:03:06 +00:00
mesh_bindings.wgsl Fix meshlet shaders for bindless mode. (#16825) 2024-12-24 02:39:18 +00:00
mesh_functions.wgsl Fix meshlet shaders for bindless mode. (#16825) 2024-12-24 02:39:18 +00:00
mesh_preprocess_types.wgsl Refactor batch_and_prepare_binned_render_phase in preparation for bin retention. (#16922) 2024-12-30 20:11:31 +00:00
mesh_preprocess.wgsl Refactor batch_and_prepare_binned_render_phase in preparation for bin retention. (#16922) 2024-12-30 20:11:31 +00:00
mesh_types.wgsl Implement bindless lightmaps. (#16653) 2024-12-16 23:37:06 +00:00
mesh_view_bindings.rs Remove tracing re-export from bevy_utils (#17161) 2025-01-05 23:06:34 +00:00
mesh_view_bindings.wgsl Fix WGSL formatting inconsistency on mesh_view_binding (#16202) 2024-11-04 15:32:38 +00:00
mesh_view_types.wgsl Introduce support for mixed lighting by allowing lights to opt out of contributing diffuse light to lightmapped objects. (#16761) 2024-12-16 23:48:33 +00:00
mesh.rs Remove tracing re-export from bevy_utils (#17161) 2025-01-05 23:06:34 +00:00
mesh.wgsl Batch skinned meshes on platforms where storage buffers are available. (#16599) 2024-12-10 17:50:03 +00:00
mod.rs Batch skinned meshes on platforms where storage buffers are available. (#16599) 2024-12-10 17:50:03 +00:00
morph.rs Type safe retained render world (#15756) 2024-10-10 18:47:04 +00:00
morph.wgsl
parallax_mapping.wgsl Fix meshlet shaders for bindless mode. (#16825) 2024-12-24 02:39:18 +00:00
pbr_ambient.wgsl
pbr_bindings.wgsl Make StandardMaterial bindless. (#16644) 2024-12-10 17:48:56 +00:00
pbr_fragment.wgsl Fix non-meshlet shaders for non-bindless mode (#16966) 2024-12-26 18:00:21 +00:00
pbr_functions.wgsl Fix several regressions from recent rendering changes. (#16890) 2024-12-22 23:03:06 +00:00
pbr_lighting.wgsl Introduce support for mixed lighting by allowing lights to opt out of contributing diffuse light to lightmapped objects. (#16761) 2024-12-16 23:48:33 +00:00
pbr_prepass_functions.wgsl Implement bindless lightmaps. (#16653) 2024-12-16 23:37:06 +00:00
pbr_prepass.wgsl Update the prepass for the bindless lightmap changes. (#16855) 2024-12-17 19:21:12 +00:00
pbr_transmission.wgsl Update typos (#17126) 2025-01-03 17:44:26 +00:00
pbr_types.wgsl Revert "Make FOG_ENABLED a shader_def instead of material flag (#13783)" (#13803) 2024-06-10 23:25:16 +00:00
pbr.wgsl Fix OIT shaders error with DX12 backend (#15782) 2024-10-10 14:17:09 +00:00
rgb9e5.wgsl
shadow_sampling.wgsl Place percentage-closer soft shadows behind a feature gate to save on samplers. (#16068) 2024-10-24 21:16:00 +00:00
shadows.wgsl Implement percentage-closer soft shadows (PCSS). (#13497) 2024-09-18 18:07:17 +00:00
skin.rs Batch skinned meshes on platforms where storage buffers are available. (#16599) 2024-12-10 17:50:03 +00:00
skinning.wgsl Batch skinned meshes on platforms where storage buffers are available. (#16599) 2024-12-10 17:50:03 +00:00
utils.wgsl Per-meshlet compressed vertex data (#15643) 2024-10-08 18:42:55 +00:00
view_transformations.wgsl Normalise matrix naming (#13489) 2024-06-03 16:56:53 +00:00
wireframe.wgsl