Skip to content

Fix: support RenderLayer filtering for lights#23265

Open
pauliusuza wants to merge 3 commits intobevyengine:mainfrom
pauliusuza:main
Open

Fix: support RenderLayer filtering for lights#23265
pauliusuza wants to merge 3 commits intobevyengine:mainfrom
pauliusuza:main

Conversation

@pauliusuza
Copy link
Copy Markdown

Objective

Prior art:

Solution

  • Added per-light packed render-layer masks (stored in unused high bits of existing light flags) and used them during shading.
  • Added a packed render-layer mask to mesh uniforms/input uniforms, propagated through GPU preprocess and meshlet resolve.
  • Forward PBR lighting now filters point/spot/directional lights by mesh_layers ∩ light_layers at fragment time.
  • Shadow specialization/queue now also filters by light-vs-mesh layers (not just camera-vs-mesh), so wrong-layer meshes no longer cast into a light’s shadow pass.

With this implementation, per-mesh/per-light filtering supports layers 0..=25 (26 layers total).

  • These 26 bits are packed into light flags starting at bit 6.
  • If a mesh/light uses layers outside that packed range, it falls back to permissive behavior for those cases (preserves previous behavior instead of breaking rendering).

Testing

Tested and confirmed working with:
cargo run --example light_render_layers


@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 8, 2026

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@kfc35 kfc35 added A-Rendering Drawing game state to the screen P-Regression Functionality that used to work but no longer does. Add a test for this! S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 8, 2026
@github-project-automation github-project-automation bot moved this to Needs SME Triage in Rendering Mar 8, 2026
.get(*light_entity)
.expect("Failed to get spot light visible entities");
(visible_entities, &light.render_layers)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, all of this will collide with #23107

's,
(&'static RenderVisibleMeshEntities, &'static ExtractedPointLight),
With<ExtractedPointLight>,
>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will collide with #23107. It's not a blocker though.

/// Keep this in sync with the light-side packing in `light.rs` and the shader
/// constants in `mesh_view_types.wgsl`.
const SHADER_RENDER_LAYER_MASK_BITS: u32 = 26;
const SHADER_RENDER_LAYER_MASK: u32 = (1 << SHADER_RENDER_LAYER_MASK_BITS) - 1;
Copy link
Copy Markdown
Contributor

@pcwalton pcwalton Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally get why you wanted to save memory here, but I think I'd just add a field instead. As you wrote it, we have no room to add flags; if we wanted to add flags we'd have to add a field anyway or risk breaking apps. And 26 is kind of an arbitrary number; 32 would be less weird.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The light flags field is a u32, and the lower 6 bits are already used for light feature flags (bit 0..bit 5), so render-layer bits must start at shift 6, leaving exactly 26 bits for layer membership.

@alice-i-cecile alice-i-cecile added this to the 0.19 milestone Mar 9, 2026
@alice-i-cecile alice-i-cecile added the C-Bug An unexpected or incorrect behavior label Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior P-Regression Functionality that used to work but no longer does. Add a test for this! S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

Regression: RenderLayers do not work for Lights

4 participants