This patch adds the infrastructure necessary for Bevy to support
*bindless resources*, by adding a new `#[bindless]` attribute to
`AsBindGroup`.
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 that `wgpu` 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:
1. 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.
2. 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:
```rust
#[derive(AsBindGroup)]
#[bindless(16)]
struct MyMaterial {
#[buffer(0)]
color: Vec4,
#[texture(1)]
#[sampler(2)]
diffuse: Handle<Image>,
}
```
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, for `MyMaterial` above, the `color`
attribute is exposed to the shader as `binding_array<vec4<f32>, 16>`,
the `diffuse` texture is exposed to the shader as
`binding_array<texture_2d<f32>, 16>`, and the `diffuse` sampler is
exposed to the shader as `binding_array<sampler, 16>`. Inside the
material's vertex and fragment shaders, the applicable index is
available via the `material_bind_group_slot` field of the `Mesh`
structure. So, for instance, you can access the current color like so:
```wgsl
// `uniform` binding arrays are a non-sequitur, so `uniform` is automatically promoted
// to `storage` in bindless mode.
@group(2) @binding(0) var<storage> material_color: binding_array<Color, 4>;
...
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
let color = material_color[mesh[in.instance_index].material_bind_group_slot];
...
}
```
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:
```wgsl
#ifdef BINDLESS
@group(2) @binding(0) var<storage> material_color: binding_array<Color, 4>;
#else // BINDLESS
@group(2) @binding(0) var<uniform> material_color: Color;
#endif // BINDLESS
...
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
#ifdef BINDLESS
let color = material_color[mesh[in.instance_index].material_bind_group_slot];
#else // BINDLESS
let color = material_color;
#endif // BINDLESS
...
}
```
Importantly, this PR *doesn't* update `StandardMaterial` to be bindless.
So, for example, `scene_viewer` will currently not run any faster. I
intend to update `StandardMaterial` 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 makes `StandardMaterial` use
bindless. Red is those patches; yellow is `main`. 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 an `AssetId` parameter.
* Bin keys now have Bevy-specific material bind group indices instead of
`wgpu` material bind group IDs, as part of the bindless change. Use the
new `MaterialBindGroupAllocator` to map from bind group index to bind
group ID.
211 lines
6.6 KiB
Rust
211 lines
6.6 KiB
Rust
use bevy_hierarchy::Children;
|
|
use bevy_math::Vec3;
|
|
pub use bevy_mesh::*;
|
|
use morph::{MeshMorphWeights, MorphWeights};
|
|
pub mod allocator;
|
|
mod components;
|
|
use crate::{
|
|
primitives::Aabb,
|
|
render_asset::{PrepareAssetError, RenderAsset, RenderAssetPlugin, RenderAssets},
|
|
render_resource::TextureView,
|
|
texture::GpuImage,
|
|
RenderApp,
|
|
};
|
|
use allocator::MeshAllocatorPlugin;
|
|
use bevy_app::{App, Plugin, PostUpdate};
|
|
use bevy_asset::{AssetApp, AssetId, RenderAssetUsages};
|
|
use bevy_ecs::{
|
|
entity::Entity,
|
|
query::{Changed, With},
|
|
system::Query,
|
|
};
|
|
use bevy_ecs::{
|
|
query::Without,
|
|
system::{
|
|
lifetimeless::{SRes, SResMut},
|
|
SystemParamItem,
|
|
},
|
|
};
|
|
pub use components::{Mesh2d, Mesh3d};
|
|
use wgpu::IndexFormat;
|
|
|
|
/// Adds the [`Mesh`] as an asset and makes sure that they are extracted and prepared for the GPU.
|
|
pub struct MeshPlugin;
|
|
|
|
impl Plugin for MeshPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_asset::<Mesh>()
|
|
.init_asset::<skinning::SkinnedMeshInverseBindposes>()
|
|
.register_asset_reflect::<Mesh>()
|
|
.register_type::<Mesh3d>()
|
|
.register_type::<skinning::SkinnedMesh>()
|
|
.register_type::<Vec<Entity>>()
|
|
// 'Mesh' must be prepared after 'Image' as meshes rely on the morph target image being ready
|
|
.add_plugins(RenderAssetPlugin::<RenderMesh, GpuImage>::default())
|
|
.add_plugins(MeshAllocatorPlugin);
|
|
|
|
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
|
|
return;
|
|
};
|
|
|
|
render_app.init_resource::<MeshVertexBufferLayouts>();
|
|
}
|
|
}
|
|
|
|
/// [Inherit weights](inherit_weights) from glTF mesh parent entity to direct
|
|
/// bevy mesh child entities (ie: glTF primitive).
|
|
pub struct MorphPlugin;
|
|
impl Plugin for MorphPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.register_type::<MorphWeights>()
|
|
.register_type::<MeshMorphWeights>()
|
|
.add_systems(PostUpdate, inherit_weights);
|
|
}
|
|
}
|
|
|
|
/// Bevy meshes are gltf primitives, [`MorphWeights`] on the bevy node entity
|
|
/// should be inherited by children meshes.
|
|
///
|
|
/// Only direct children are updated, to fulfill the expectations of glTF spec.
|
|
pub fn inherit_weights(
|
|
morph_nodes: Query<(&Children, &MorphWeights), (Without<Mesh3d>, Changed<MorphWeights>)>,
|
|
mut morph_primitives: Query<&mut MeshMorphWeights, With<Mesh3d>>,
|
|
) {
|
|
for (children, parent_weights) in &morph_nodes {
|
|
let mut iter = morph_primitives.iter_many_mut(children);
|
|
while let Some(mut child_weight) = iter.fetch_next() {
|
|
child_weight.clear_weights();
|
|
child_weight.extend_weights(parent_weights.weights());
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait MeshAabb {
|
|
/// Compute the Axis-Aligned Bounding Box of the mesh vertices in model space
|
|
///
|
|
/// Returns `None` if `self` doesn't have [`Mesh::ATTRIBUTE_POSITION`] of
|
|
/// type [`VertexAttributeValues::Float32x3`], or if `self` doesn't have any vertices.
|
|
fn compute_aabb(&self) -> Option<Aabb>;
|
|
}
|
|
|
|
impl MeshAabb for Mesh {
|
|
fn compute_aabb(&self) -> Option<Aabb> {
|
|
let Some(VertexAttributeValues::Float32x3(values)) =
|
|
self.attribute(Mesh::ATTRIBUTE_POSITION)
|
|
else {
|
|
return None;
|
|
};
|
|
|
|
Aabb::enclosing(values.iter().map(|p| Vec3::from_slice(p)))
|
|
}
|
|
}
|
|
|
|
/// The render world representation of a [`Mesh`].
|
|
#[derive(Debug, Clone)]
|
|
pub struct RenderMesh {
|
|
/// The number of vertices in the mesh.
|
|
pub vertex_count: u32,
|
|
|
|
/// Morph targets for the mesh, if present.
|
|
pub morph_targets: Option<TextureView>,
|
|
|
|
/// Information about the mesh data buffers, including whether the mesh uses
|
|
/// indices or not.
|
|
pub buffer_info: RenderMeshBufferInfo,
|
|
|
|
/// Precomputed pipeline key bits for this mesh.
|
|
pub key_bits: BaseMeshPipelineKey,
|
|
|
|
/// A reference to the vertex buffer layout.
|
|
///
|
|
/// Combined with [`RenderMesh::buffer_info`], this specifies the complete
|
|
/// layout of the buffers associated with this mesh.
|
|
pub layout: MeshVertexBufferLayoutRef,
|
|
}
|
|
|
|
impl RenderMesh {
|
|
/// Returns the primitive topology of this mesh (triangles, triangle strips,
|
|
/// etc.)
|
|
#[inline]
|
|
pub fn primitive_topology(&self) -> PrimitiveTopology {
|
|
self.key_bits.primitive_topology()
|
|
}
|
|
}
|
|
|
|
/// The index/vertex buffer info of a [`RenderMesh`].
|
|
#[derive(Debug, Clone)]
|
|
pub enum RenderMeshBufferInfo {
|
|
Indexed {
|
|
count: u32,
|
|
index_format: IndexFormat,
|
|
},
|
|
NonIndexed,
|
|
}
|
|
|
|
impl RenderAsset for RenderMesh {
|
|
type SourceAsset = Mesh;
|
|
type Param = (
|
|
SRes<RenderAssets<GpuImage>>,
|
|
SResMut<MeshVertexBufferLayouts>,
|
|
);
|
|
|
|
#[inline]
|
|
fn asset_usage(mesh: &Self::SourceAsset) -> RenderAssetUsages {
|
|
mesh.asset_usage
|
|
}
|
|
|
|
fn byte_len(mesh: &Self::SourceAsset) -> Option<usize> {
|
|
let mut vertex_size = 0;
|
|
for attribute_data in mesh.attributes() {
|
|
let vertex_format = attribute_data.0.format;
|
|
vertex_size += vertex_format.get_size() as usize;
|
|
}
|
|
|
|
let vertex_count = mesh.count_vertices();
|
|
let index_bytes = mesh.get_index_buffer_bytes().map(<[_]>::len).unwrap_or(0);
|
|
Some(vertex_size * vertex_count + index_bytes)
|
|
}
|
|
|
|
/// Converts the extracted mesh into a [`RenderMesh`].
|
|
fn prepare_asset(
|
|
mesh: Self::SourceAsset,
|
|
_: AssetId<Self::SourceAsset>,
|
|
(images, ref mut mesh_vertex_buffer_layouts): &mut SystemParamItem<Self::Param>,
|
|
) -> Result<Self, PrepareAssetError<Self::SourceAsset>> {
|
|
let morph_targets = match mesh.morph_targets() {
|
|
Some(mt) => {
|
|
let Some(target_image) = images.get(mt) else {
|
|
return Err(PrepareAssetError::RetryNextUpdate(mesh));
|
|
};
|
|
Some(target_image.texture_view.clone())
|
|
}
|
|
None => None,
|
|
};
|
|
|
|
let buffer_info = match mesh.indices() {
|
|
Some(indices) => RenderMeshBufferInfo::Indexed {
|
|
count: indices.len() as u32,
|
|
index_format: indices.into(),
|
|
},
|
|
None => RenderMeshBufferInfo::NonIndexed,
|
|
};
|
|
|
|
let mesh_vertex_buffer_layout =
|
|
mesh.get_mesh_vertex_buffer_layout(mesh_vertex_buffer_layouts);
|
|
|
|
let mut key_bits = BaseMeshPipelineKey::from_primitive_topology(mesh.primitive_topology());
|
|
key_bits.set(
|
|
BaseMeshPipelineKey::MORPH_TARGETS,
|
|
mesh.morph_targets().is_some(),
|
|
);
|
|
|
|
Ok(RenderMesh {
|
|
vertex_count: mesh.count_vertices() as u32,
|
|
buffer_info,
|
|
key_bits,
|
|
layout: mesh_vertex_buffer_layout,
|
|
morph_targets,
|
|
})
|
|
}
|
|
}
|