Skip to content

Commit bc7416a

Browse files
authored
Use 4-byte LightmapSlabIndex for batching instead of 16-byte AssetId<Image> (#18326)
Less data accessed and compared gives better batching performance. # Objective - Use a smaller id to represent the lightmap in batch data to enable a faster implementation of draw streams. - Improve batching performance for 3D sorted render phases. ## Solution - 3D batching can use `LightmapSlabIndex` (a `NonMaxU32` which is 4 bytes) instead of the lightmap `AssetId<Image>` (an enum where the largest variant is a 16-byte UUID) to discern the ability to batch. ## Testing Tested main (yellow) vs this PR (red) on an M4 Max using the `many_cubes` example with `WGPU_SETTINGS_PRIO=webgl2` to avoid GPU-preprocessing, and modifying the materials in `many_cubes` to have `AlphaMode::Blend` so that they would rely on the less efficient sorted render phase batching. <img width="1500" alt="Screenshot_2025-03-15_at_12 17 21" src="https://github.com/user-attachments/assets/14709bd3-6d06-40fb-aa51-e1d2d606ebe3" /> A 44.75us or 7.5% reduction in median execution time of the batch and prepare sorted render phase system for the `Transparent3d` phase (handling 160k cubes). --- ## Migration Guide - Changed: `RenderLightmap::new()` no longer takes an `AssetId<Image>` argument for the asset id of the lightmap image.
1 parent b462f47 commit bc7416a

File tree

2 files changed

+3
-9
lines changed

2 files changed

+3
-9
lines changed

crates/bevy_pbr/src/lightmap/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ pub struct Lightmap {
116116
/// There is one of these per visible lightmapped mesh instance.
117117
#[derive(Debug)]
118118
pub(crate) struct RenderLightmap {
119-
/// The ID of the lightmap texture.
120-
pub(crate) image: AssetId<Image>,
121-
122119
/// The rectangle within the lightmap texture that the UVs are relative to.
123120
///
124121
/// The top left coordinate is the `min` part of the rect, and the bottom
@@ -245,7 +242,6 @@ fn extract_lightmaps(
245242
render_lightmaps.render_lightmaps.insert(
246243
entity.into(),
247244
RenderLightmap::new(
248-
lightmap.image.id(),
249245
lightmap.uv_rect,
250246
slab_index,
251247
slot_index,
@@ -305,14 +301,12 @@ impl RenderLightmap {
305301
/// Creates a new lightmap from a texture, a UV rect, and a slab and slot
306302
/// index pair.
307303
fn new(
308-
image: AssetId<Image>,
309304
uv_rect: Rect,
310305
slab_index: LightmapSlabIndex,
311306
slot_index: LightmapSlotIndex,
312307
bicubic_sampling: bool,
313308
) -> Self {
314309
Self {
315-
image,
316310
uv_rect,
317311
slab_index,
318312
slot_index,

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ impl GetBatchData for MeshPipeline {
19051905
type CompareData = (
19061906
MaterialBindGroupIndex,
19071907
AssetId<Mesh>,
1908-
Option<AssetId<Image>>,
1908+
Option<LightmapSlabIndex>,
19091909
);
19101910

19111911
type BufferData = MeshUniform;
@@ -1946,7 +1946,7 @@ impl GetBatchData for MeshPipeline {
19461946
mesh_instance.should_batch().then_some((
19471947
material_bind_group_index.group,
19481948
mesh_instance.mesh_asset_id,
1949-
maybe_lightmap.map(|lightmap| lightmap.image),
1949+
maybe_lightmap.map(|lightmap| lightmap.slab_index),
19501950
)),
19511951
))
19521952
}
@@ -1976,7 +1976,7 @@ impl GetFullBatchData for MeshPipeline {
19761976
mesh_instance.should_batch().then_some((
19771977
mesh_instance.material_bindings_index.group,
19781978
mesh_instance.mesh_asset_id,
1979-
maybe_lightmap.map(|lightmap| lightmap.image),
1979+
maybe_lightmap.map(|lightmap| lightmap.slab_index),
19801980
)),
19811981
))
19821982
}

0 commit comments

Comments
 (0)