Skip to content

Fix: Support RenderLayers filtering for Lights (Adoption of #23265)#23925

Open
kfc35 wants to merge 16 commits intobevyengine:mainfrom
kfc35:23264_adoption
Open

Fix: Support RenderLayers filtering for Lights (Adoption of #23265)#23925
kfc35 wants to merge 16 commits intobevyengine:mainfrom
kfc35:23264_adoption

Conversation

@kfc35
Copy link
Copy Markdown
Contributor

@kfc35 kfc35 commented Apr 22, 2026

Objective

Solution

Copied and Pasted from original PR:

  • 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).

Additional Note from Me: Updating to main was mostly painless. All the conflicts were in crates/bevy_pbr/src/render/light.rs, and I resolved to the best of my ability and understanding since mucking around in that same file for another rendering issue.

I’m not as well versed in the rendering side of things, so if you have a comment, you might have to explain it like I’m five. Hopefully there isn’t much to fix up… but if there is, lean towards being very specific for my sake please 🙏

Testing

from original author: Tested and confirmed working with: cargo run --example light_render_layers

from me: I double checked the example and it works the same as it did before I merged main and resolved conflicts.

Showcase

Video of `light_render_layers ` example
Screen.Recording.2026-04-23.at.5.27.27.PM.mov

@kfc35 kfc35 added C-Bug An unexpected or incorrect behavior 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 Apr 22, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Rendering Apr 22, 2026
@kfc35 kfc35 added this to the 0.19 milestone Apr 22, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-23925

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

1 similar comment
@github-actions
Copy link
Copy Markdown
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-23925

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

@kfc35
Copy link
Copy Markdown
Contributor Author

kfc35 commented Apr 22, 2026

RE PixelEagle: it was an expected change and c71c870 changes it back to normal.

@github-actions
Copy link
Copy Markdown
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-23925

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

@alice-i-cecile alice-i-cecile added the D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes label Apr 22, 2026
@alice-i-cecile alice-i-cecile added the X-Uncontroversial This work is generally agreed upon label Apr 22, 2026
@pauliusuza
Copy link
Copy Markdown

Looks great, thanks @kfc35 for taking this on

Comment thread crates/bevy_pbr/src/render/light.rs Outdated
Comment thread crates/bevy_pbr/src/render/light.rs Outdated
Comment thread crates/bevy_pbr/src/render/light.rs Outdated
Copy link
Copy Markdown
Member

@tychedelia tychedelia left a comment

Choose a reason for hiding this comment

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

I'm not sure we should land this without also landing per-camera shadow maps as it may be confusing to users trying to make use of this feature.

pub local_from_world_transpose_b: f32,
pub flags: u32,
/// Packed render layer mask used for per-mesh light filtering in shaders.
pub render_layers: u32,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Am confused why we don't need padding here

/// If the mesh has no morph targets, this is `u32::MAX`.
pub morph_descriptor_index: u32,
/// Padding to preserve 16-byte alignment for POD casts.
pub pad: [u32; 3],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

these can usually be gated with #[cfg(feature = "webgl2")] and SIXTEEN_BYTE_ALIGNMENT in teh shader.

@kfc35 kfc35 added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 23, 2026
@kfc35 kfc35 added S-Blocked This cannot move forward until something else changes and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Apr 24, 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 D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes P-Regression Functionality that used to work but no longer does. Add a test for this! S-Blocked This cannot move forward until something else changes X-Uncontroversial This work is generally agreed upon

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

Regression: RenderLayers do not work for Lights

4 participants