Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement bindless lightmaps. #16653

Merged
merged 12 commits into from
Dec 17, 2024
Merged

Conversation

pcwalton
Copy link
Contributor

@pcwalton pcwalton commented Dec 5, 2024

This commit allows Bevy to bind 16 lightmaps at a time, if the current platform supports bindless textures. Naturally, if bindless textures aren't supported, Bevy falls back to binding only a single lightmap at a time. As lightmaps are usually heavily atlased, I doubt many scenes will use more than 16 lightmap textures.

This has little performance impact now, but it's desirable for us to reap the benefits of multidraw and bindless textures on scenes that use lightmaps. Otherwise, we might have to break batches in order to switch those lightmaps.

Additionally, this PR slightly reduces the cost of binning because it makes the lightmap index in Opaque3dBinKey 32 bits instead of an AssetId.

Migration Guide

  • The Opaque3dBinKey::lightmap_image field is now Opaque3dBinKey::lightmap_slab, which is a lightweight identifier for an entire binding array of lightmaps.

This commit allows Bevy to bind 16 lightmaps at a time, if the current
platform supports bindless textures. Naturally, if bindless textures
aren't supported, Bevys fall back to binding only a single lightmap at a
time. As lightmaps are usually heavily atlased, I doubt many scenes will
use more than 16 lightmap textures.

This has little performance impact now, but it's desirable for us to
reap the benefits of multidraw and bindless textures on scenes that use
lightmaps. Otherwise, we might have to break batches in order to switch
lightmaps.

Additionally, this PR slightly reduces the cost of binning because it
makes the lightmap index in `Opaque3dBinKey` 32 bits instead of an
`AssetId`.
@pcwalton pcwalton requested review from kristoff3r and JMS55 December 5, 2024 02:32
@pcwalton pcwalton added A-Rendering Drawing game state to the screen S-Needs-Review Needs reviewer attention (from anyone!) to move forward C-Performance A change motivated by improving speed, memory usage or compile times labels Dec 5, 2024
Copy link
Contributor

@JMS55 JMS55 left a comment

Choose a reason for hiding this comment

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

For binding_arrays_are_usable() we don't require PARTIALLY_BOUND_BINDING_ARRAY, but I don't see any padding of slabs with dummy textures to ensure the binding array is full.

@pcwalton
Copy link
Contributor Author

pcwalton commented Dec 5, 2024

Dang it, I even made a note to remember to do that and then forgot anyway :)

I now pad out the lightmap slabs to their maximum size.

@pcwalton pcwalton requested a review from JMS55 December 5, 2024 20:42
@pcwalton pcwalton requested a review from IceSentry December 6, 2024 06:48
@pcwalton pcwalton requested a review from JMS55 December 10, 2024 01:15
@pcwalton pcwalton requested a review from Elabajaba December 10, 2024 08:24
@pcwalton
Copy link
Contributor Author

pcwalton commented Dec 11, 2024

The latest merge ran this PR out of space in the MeshInputUniform and MeshUniform. I fixed it by packing the material bind group slot and lightmap slot into a single 32-bit integer. Since the maximum size of each is 16 by default, this gives us plenty of space.

Copy link
Contributor

Your PR increases Bevy Minimum Supported Rust Version. Please update the rust-version field in the root Cargo.toml file.

Copy link
Contributor

@bushrat011899 bushrat011899 left a comment

Choose a reason for hiding this comment

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

The Rust code looks fine to me (no comment on the WGSL as I'm unfamiliar with the language). I tried running this branch locally with the testbed_3d example on Windows 10 with an i5-1240P iGPU and encountered a crash which I'm not experiencing on main.

Tested using $env:WGPU_BACKEND="dx12"; cargo run --example testbed_3d and can render the first two scenes fine, but the 3rd scene (gas mask) crashes with the following log:

     Running `target\debug\examples\testbed_3d.exe`
2024-12-12T01:05:31.383756Z  INFO bevy_diagnostic::system_information_diagnostics_plugin::internal: SystemInfo { os: "Windows 10 Pro", kernel: "19045", cpu: "12th Gen Intel(R) Core(TM) i5-1240P", core_count: "12", memory: "31.7 GiB" }
2024-12-12T01:05:31.453262Z  INFO bevy_render::renderer: AdapterInfo { name: "Intel(R) Iris(R) Xe Graphics", vendor: 32902, device: 18086, device_type: IntegratedGpu, driver: "32.0.101.5972", driver_info: "", backend: Dx12 }
2024-12-12T01:05:32.052131Z  INFO bevy_winit::system: Creating new window "App" (0v1#4294967296)
2024-12-12T01:05:37.295522Z  INFO testbed_3d: Switching scene
2024-12-12T01:05:41.643572Z  INFO testbed_3d: Switching scene
thread 'Compute Task Pool (6)' panicked at crates\bevy_pbr\src\render\mesh.rs:989:49:
index out of bounds: the len is 1 but the index is 2
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_pbr::render::mesh::collect_meshes_for_gpu_building`!

This PR does modify the file listed as the source of the panic, so it may be caused by this PR. I tried switching back and forth between different branches to confirm. Please reach out to me if there's something I can do to help diagnose!

@pcwalton
Copy link
Contributor Author

@bushrat011899 That sounds like something that was fixed on main recently. I can't reproduce after merging with main. Could you try again with the newest version of this PR?

@bushrat011899
Copy link
Contributor

Can confirm this is no longer an issue, I'm able to cycle all scenes in testbed_3d

@pcwalton pcwalton added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 13, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Dec 16, 2024
Merged via the queue into bevyengine:main with commit 35826be Dec 17, 2024
29 checks passed
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-Performance A change motivated by improving speed, memory usage or compile times S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants