Open pcwalton opened 1 week ago
The generated examples/README.md
is out of sync with the example metadata in Cargo.toml
or the example readme template. Please run cargo run -p build-templated-pages -- update examples
to update it, and commit the file change.
This patch adds the infrastructure necessary for Bevy to support bindless resources, by adding a new
#[bindless]
attribute toAsBindGroup
.Classically, only a single texture (or sampler, or buffer) can be attached to each shader binding. This means that switching materials requires breaking a batch and issuing a new drawcall, even if the mesh is otherwise identical. This adds significant overhead not only in the driver but also in
wgpu
, as switching bind groups increases the amount of validation work thatwgpu
must do.Bindless resources are the typical solution to this problem. Instead of switching bindings between each texture, the renderer instead supplies a large array of all textures in the scene up front, and the material contains an index into that array. This pattern is repeated for buffers and samplers as well. The renderer now no longer needs to switch binding descriptor sets while drawing the scene.
Unfortunately, as things currently stand, this approach won't quite work for Bevy. Two aspects of
wgpu
conspire to make this ideal approach unacceptably slow:In the DX12 backend, all binding arrays (bindless resources) must have a constant size declared in the shader, and all textures in an array must be bound to actual textures. Changing the size requires a recompile.
Changing even one texture incurs revalidation of all textures, a process that takes time that's linear in the total size of the binding array.
This means that declaring a large array of textures big enough to encompass the entire scene is presently unacceptably slow. For example, if you declare 4096 textures, then
wgpu
will have to revalidate all 4096 textures if even a single one changes. This process can take multiple frames.To work around this problem, this PR groups bindless resources into small slabs and maintains a free list for each. The size of each slab for the bindless arrays associated with a material is specified via the
#[bindless(N)]
attribute. For instance, consider the following declaration:The
#[bindless(N)]
attribute specifies that, if bindless arrays are supported on the current platform, each resource becomes a binding array of N instances of that resource. So, forMyMaterial
above, thecolor
attribute is exposed to the shader asbinding_array<vec4<f32>, 16>
, thediffuse
texture is exposed to the shader asbinding_array<texture_2d<f32>, 16>
, and thediffuse
sampler is exposed to the shader asbinding_array<sampler, 16>
. Inside the material's vertex and fragment shaders, the applicable index is available via thematerial_bind_group_slot
field of theMesh
structure. So, for instance, you can access the current color like so:Note that portable shader code can't guarantee that the current platform supports bindless textures. Indeed, bindless mode is only available in Vulkan and DX12. The
BINDLESS
shader definition is available for your use to determine whether you're on a bindless platform or not. Thus a portable version of the shader above would look like:Importantly, this PR doesn't update
StandardMaterial
to be bindless. So, for example,scene_viewer
will currently not run any faster. I intend to updateStandardMaterial
to use bindless mode in a follow-up patch.A new example,
shaders/shader_material_bindless
, has been added to demonstrate how to use this new feature.Here's a Tracy profile of
submit_graph_commands
of this patch and an additional patch (not submitted yet) that makesStandardMaterial
use bindless. Red is those patches; yellow ismain
. The scene was Bistro Exterior with a hack that forces all textures to opaque. You can see a 1.47x mean speedup.Migration Guide
RenderAssets::prepare_asset
now takes anAssetId
parameter.wgpu
material bind group IDs, as part of the bindless change. Use the newMaterialBindGroupAllocator
to map from bind group index to bind group ID.