allocate 1x1 shadow map textures when no shadow-casting lights - #25820
Conversation
alice-i-cecile
left a comment
There was a problem hiding this comment.
Sure, the logic makes sense here, and I can see the value for 2D or UI-only scenes in projects that still want to pull in bevy_pbr. Or cases where you're rolling your own shadows / rendering pipeline.
Is there a noticeable hitch for reallocation when toggling shadows on? I think it'll probably be dwarfed by asset loading in most realistic cases but it's something I worry a bit about.
|
I tested on native (Windows Vulkan), WebGPU (Chrome desktop), and WebGPU (Chrome) on a mid-range Android device (Redmi Note 14). In all cases, toggling shadows on/off caused no perceptible hitch. The reallocation is fast enough to be invisible even on mobile hardware. Also toggling shadow casting at runtime is not a common pattern in production apps, it's mostly a concern for tooling and debug workflows, where a one-frame hitch (even if exists) is entirely acceptable. |
Objective
point_light_shadow_map_textureanddirectional_light_shadow_map_textureare always allocated at their full configured size (default: 1024×1024×6 = 24 MB and 2048×2048 = 16 MB) even when no shadow-casting lights are present in the scene.Solution
When no shadow-casting lights are present, allocate a 1×1 texture instead of a full-size one. The binding remains valid (no shader changes required), and
TextureCachewill reallocate at the correct size as soon as a shadow-casting light appears.Testing
Tested with a scene containing no lights, then toggling point and directional lights with shadows on/off at runtime. The GPU memory inspector confirms: