2023-10-17 21:28:08 +00:00
|
|
|
use bevy_asset::{Asset, Handle};
|
2024-08-25 20:16:34 +00:00
|
|
|
use bevy_ecs::system::SystemParamItem;
|
2023-11-15 12:48:36 +00:00
|
|
|
use bevy_reflect::{impl_type_path, Reflect};
|
2023-10-17 21:28:08 +00:00
|
|
|
use bevy_render::{
|
2024-03-01 20:56:21 +00:00
|
|
|
mesh::MeshVertexBufferLayoutRef,
|
2023-10-17 21:28:08 +00:00
|
|
|
render_resource::{
|
|
|
|
AsBindGroup, AsBindGroupError, BindGroupLayout, RenderPipelineDescriptor, Shader,
|
|
|
|
ShaderRef, SpecializedMeshPipelineError, UnpreparedBindGroup,
|
|
|
|
},
|
|
|
|
renderer::RenderDevice,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{Material, MaterialPipeline, MaterialPipelineKey, MeshPipeline, MeshPipelineKey};
|
|
|
|
|
|
|
|
pub struct MaterialExtensionPipeline {
|
|
|
|
pub mesh_pipeline: MeshPipeline,
|
|
|
|
pub material_layout: BindGroupLayout,
|
|
|
|
pub vertex_shader: Option<Handle<Shader>>,
|
|
|
|
pub fragment_shader: Option<Handle<Shader>>,
|
Add a bindless mode to `AsBindGroup`. (#16368)
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.
![Screenshot 2024-11-12
161713](https://github.com/user-attachments/assets/4334b362-42c8-4d64-9cfb-6835f019b95c)
## 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.
2024-12-03 18:00:34 +00:00
|
|
|
pub bindless: bool,
|
2023-10-17 21:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MaterialExtensionKey<E: MaterialExtension> {
|
|
|
|
pub mesh_key: MeshPipelineKey,
|
|
|
|
pub bind_group_data: E::Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A subset of the `Material` trait for defining extensions to a base `Material`, such as the builtin `StandardMaterial`.
|
2024-09-24 11:42:59 +00:00
|
|
|
///
|
2023-10-17 21:28:08 +00:00
|
|
|
/// A user type implementing the trait should be used as the `E` generic param in an `ExtendedMaterial` struct.
|
|
|
|
pub trait MaterialExtension: Asset + AsBindGroup + Clone + Sized {
|
|
|
|
/// Returns this material's vertex shader. If [`ShaderRef::Default`] is returned, the base material mesh vertex shader
|
|
|
|
/// will be used.
|
|
|
|
fn vertex_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this material's fragment shader. If [`ShaderRef::Default`] is returned, the base material mesh fragment shader
|
|
|
|
/// will be used.
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn fragment_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this material's prepass vertex shader. If [`ShaderRef::Default`] is returned, the base material prepass vertex shader
|
|
|
|
/// will be used.
|
|
|
|
fn prepass_vertex_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this material's prepass fragment shader. If [`ShaderRef::Default`] is returned, the base material prepass fragment shader
|
|
|
|
/// will be used.
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn prepass_fragment_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this material's deferred vertex shader. If [`ShaderRef::Default`] is returned, the base material deferred vertex shader
|
|
|
|
/// will be used.
|
|
|
|
fn deferred_vertex_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this material's prepass fragment shader. If [`ShaderRef::Default`] is returned, the base material deferred fragment shader
|
|
|
|
/// will be used.
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn deferred_fragment_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
2024-03-25 19:08:27 +00:00
|
|
|
/// Returns this material's [`crate::meshlet::MeshletMesh`] fragment shader. If [`ShaderRef::Default`] is returned,
|
|
|
|
/// the default meshlet mesh fragment shader will be used.
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[cfg(feature = "meshlet")]
|
|
|
|
fn meshlet_mesh_fragment_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this material's [`crate::meshlet::MeshletMesh`] prepass fragment shader. If [`ShaderRef::Default`] is returned,
|
|
|
|
/// the default meshlet mesh prepass fragment shader will be used.
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[cfg(feature = "meshlet")]
|
|
|
|
fn meshlet_mesh_prepass_fragment_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this material's [`crate::meshlet::MeshletMesh`] deferred fragment shader. If [`ShaderRef::Default`] is returned,
|
|
|
|
/// the default meshlet mesh deferred fragment shader will be used.
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[cfg(feature = "meshlet")]
|
|
|
|
fn meshlet_mesh_deferred_fragment_shader() -> ShaderRef {
|
|
|
|
ShaderRef::Default
|
|
|
|
}
|
|
|
|
|
2023-10-17 21:28:08 +00:00
|
|
|
/// Customizes the default [`RenderPipelineDescriptor`] for a specific entity using the entity's
|
2024-03-01 20:56:21 +00:00
|
|
|
/// [`MaterialPipelineKey`] and [`MeshVertexBufferLayoutRef`] as input.
|
2023-10-17 21:28:08 +00:00
|
|
|
/// Specialization for the base material is applied before this function is called.
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[inline]
|
|
|
|
fn specialize(
|
|
|
|
pipeline: &MaterialExtensionPipeline,
|
|
|
|
descriptor: &mut RenderPipelineDescriptor,
|
2024-03-01 20:56:21 +00:00
|
|
|
layout: &MeshVertexBufferLayoutRef,
|
2023-10-17 21:28:08 +00:00
|
|
|
key: MaterialExtensionKey<Self>,
|
|
|
|
) -> Result<(), SpecializedMeshPipelineError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A material that extends a base [`Material`] with additional shaders and data.
|
|
|
|
///
|
|
|
|
/// The data from both materials will be combined and made available to the shader
|
|
|
|
/// so that shader functions built for the base material (and referencing the base material
|
|
|
|
/// bindings) will work as expected, and custom alterations based on custom data can also be used.
|
|
|
|
///
|
|
|
|
/// If the extension `E` returns a non-default result from `vertex_shader()` it will be used in place of the base
|
|
|
|
/// material's vertex shader.
|
|
|
|
///
|
|
|
|
/// If the extension `E` returns a non-default result from `fragment_shader()` it will be used in place of the base
|
|
|
|
/// fragment shader.
|
|
|
|
///
|
|
|
|
/// When used with `StandardMaterial` as the base, all the standard material fields are
|
|
|
|
/// present, so the `pbr_fragment` shader functions can be called from the extension shader (see
|
|
|
|
/// the `extended_material` example).
|
2024-07-04 23:59:48 +00:00
|
|
|
#[derive(Asset, Clone, Debug, Reflect)]
|
2023-11-15 12:48:36 +00:00
|
|
|
#[reflect(type_path = false)]
|
2023-10-17 21:28:08 +00:00
|
|
|
pub struct ExtendedMaterial<B: Material, E: MaterialExtension> {
|
|
|
|
pub base: B,
|
|
|
|
pub extension: E,
|
|
|
|
}
|
|
|
|
|
2024-04-18 12:57:14 +00:00
|
|
|
impl<B, E> Default for ExtendedMaterial<B, E>
|
|
|
|
where
|
|
|
|
B: Material + Default,
|
|
|
|
E: MaterialExtension + Default,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
base: B::default(),
|
|
|
|
extension: E::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 12:48:36 +00:00
|
|
|
// We don't use the `TypePath` derive here due to a bug where `#[reflect(type_path = false)]`
|
|
|
|
// causes the `TypePath` derive to not generate an implementation.
|
|
|
|
impl_type_path!((in bevy_pbr::extended_material) ExtendedMaterial<B: Material, E: MaterialExtension>);
|
|
|
|
|
2023-10-17 21:28:08 +00:00
|
|
|
impl<B: Material, E: MaterialExtension> AsBindGroup for ExtendedMaterial<B, E> {
|
|
|
|
type Data = (<B as AsBindGroup>::Data, <E as AsBindGroup>::Data);
|
2024-08-25 20:16:34 +00:00
|
|
|
type Param = (<B as AsBindGroup>::Param, <E as AsBindGroup>::Param);
|
2023-10-17 21:28:08 +00:00
|
|
|
|
|
|
|
fn unprepared_bind_group(
|
|
|
|
&self,
|
|
|
|
layout: &BindGroupLayout,
|
|
|
|
render_device: &RenderDevice,
|
2024-08-25 20:16:34 +00:00
|
|
|
(base_param, extended_param): &mut SystemParamItem<'_, '_, Self::Param>,
|
2024-12-10 17:48:56 +00:00
|
|
|
mut force_no_bindless: bool,
|
2023-11-28 23:43:40 +00:00
|
|
|
) -> Result<UnpreparedBindGroup<Self::Data>, AsBindGroupError> {
|
2024-12-10 17:48:56 +00:00
|
|
|
// Only allow bindless mode if both the base material and the extension
|
|
|
|
// support it.
|
|
|
|
force_no_bindless = force_no_bindless
|
|
|
|
|| B::BINDLESS_SLOT_COUNT.is_none()
|
|
|
|
|| E::BINDLESS_SLOT_COUNT.is_none();
|
|
|
|
|
2023-10-17 21:28:08 +00:00
|
|
|
// add together the bindings of the base material and the user material
|
|
|
|
let UnpreparedBindGroup {
|
|
|
|
mut bindings,
|
|
|
|
data: base_data,
|
2024-12-10 17:48:56 +00:00
|
|
|
} = B::unprepared_bind_group(
|
|
|
|
&self.base,
|
|
|
|
layout,
|
|
|
|
render_device,
|
|
|
|
base_param,
|
|
|
|
force_no_bindless,
|
|
|
|
)?;
|
|
|
|
let extended_bindgroup = E::unprepared_bind_group(
|
|
|
|
&self.extension,
|
|
|
|
layout,
|
|
|
|
render_device,
|
|
|
|
extended_param,
|
|
|
|
force_no_bindless,
|
|
|
|
)?;
|
2023-10-17 21:28:08 +00:00
|
|
|
|
Add a bindless mode to `AsBindGroup`. (#16368)
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.
![Screenshot 2024-11-12
161713](https://github.com/user-attachments/assets/4334b362-42c8-4d64-9cfb-6835f019b95c)
## 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.
2024-12-03 18:00:34 +00:00
|
|
|
bindings.extend(extended_bindgroup.bindings.0);
|
2023-10-17 21:28:08 +00:00
|
|
|
|
|
|
|
Ok(UnpreparedBindGroup {
|
|
|
|
bindings,
|
|
|
|
data: (base_data, extended_bindgroup.data),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bind_group_layout_entries(
|
|
|
|
render_device: &RenderDevice,
|
2024-12-10 17:48:56 +00:00
|
|
|
mut force_no_bindless: bool,
|
2023-10-17 21:28:08 +00:00
|
|
|
) -> Vec<bevy_render::render_resource::BindGroupLayoutEntry>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2024-12-10 17:48:56 +00:00
|
|
|
// Only allow bindless mode if both the base material and the extension
|
|
|
|
// support it.
|
|
|
|
force_no_bindless = force_no_bindless
|
|
|
|
|| B::BINDLESS_SLOT_COUNT.is_none()
|
|
|
|
|| E::BINDLESS_SLOT_COUNT.is_none();
|
|
|
|
|
2023-10-17 21:28:08 +00:00
|
|
|
// add together the bindings of the standard material and the user material
|
2024-12-10 17:48:56 +00:00
|
|
|
let mut entries = B::bind_group_layout_entries(render_device, force_no_bindless);
|
|
|
|
entries.extend(E::bind_group_layout_entries(
|
|
|
|
render_device,
|
|
|
|
force_no_bindless,
|
|
|
|
));
|
2023-10-17 21:28:08 +00:00
|
|
|
entries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: Material, E: MaterialExtension> Material for ExtendedMaterial<B, E> {
|
2023-11-28 23:43:40 +00:00
|
|
|
fn vertex_shader() -> ShaderRef {
|
2023-10-17 21:28:08 +00:00
|
|
|
match E::vertex_shader() {
|
|
|
|
ShaderRef::Default => B::vertex_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 23:43:40 +00:00
|
|
|
fn fragment_shader() -> ShaderRef {
|
2023-10-17 21:28:08 +00:00
|
|
|
match E::fragment_shader() {
|
|
|
|
ShaderRef::Default => B::fragment_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:43:55 +00:00
|
|
|
fn alpha_mode(&self) -> crate::AlphaMode {
|
|
|
|
B::alpha_mode(&self.base)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn opaque_render_method(&self) -> crate::OpaqueRendererMethod {
|
|
|
|
B::opaque_render_method(&self.base)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn depth_bias(&self) -> f32 {
|
|
|
|
B::depth_bias(&self.base)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reads_view_transmission_texture(&self) -> bool {
|
|
|
|
B::reads_view_transmission_texture(&self.base)
|
|
|
|
}
|
|
|
|
|
2023-11-28 23:43:40 +00:00
|
|
|
fn prepass_vertex_shader() -> ShaderRef {
|
2023-10-17 21:28:08 +00:00
|
|
|
match E::prepass_vertex_shader() {
|
|
|
|
ShaderRef::Default => B::prepass_vertex_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 23:43:40 +00:00
|
|
|
fn prepass_fragment_shader() -> ShaderRef {
|
2023-10-17 21:28:08 +00:00
|
|
|
match E::prepass_fragment_shader() {
|
|
|
|
ShaderRef::Default => B::prepass_fragment_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 23:43:40 +00:00
|
|
|
fn deferred_vertex_shader() -> ShaderRef {
|
2023-10-17 21:28:08 +00:00
|
|
|
match E::deferred_vertex_shader() {
|
|
|
|
ShaderRef::Default => B::deferred_vertex_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 23:43:40 +00:00
|
|
|
fn deferred_fragment_shader() -> ShaderRef {
|
2023-10-17 21:28:08 +00:00
|
|
|
match E::deferred_fragment_shader() {
|
|
|
|
ShaderRef::Default => B::deferred_fragment_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 19:08:27 +00:00
|
|
|
#[cfg(feature = "meshlet")]
|
|
|
|
fn meshlet_mesh_fragment_shader() -> ShaderRef {
|
|
|
|
match E::meshlet_mesh_fragment_shader() {
|
|
|
|
ShaderRef::Default => B::meshlet_mesh_fragment_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "meshlet")]
|
|
|
|
fn meshlet_mesh_prepass_fragment_shader() -> ShaderRef {
|
|
|
|
match E::meshlet_mesh_prepass_fragment_shader() {
|
|
|
|
ShaderRef::Default => B::meshlet_mesh_prepass_fragment_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "meshlet")]
|
|
|
|
fn meshlet_mesh_deferred_fragment_shader() -> ShaderRef {
|
|
|
|
match E::meshlet_mesh_deferred_fragment_shader() {
|
|
|
|
ShaderRef::Default => B::meshlet_mesh_deferred_fragment_shader(),
|
|
|
|
specified => specified,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 21:28:08 +00:00
|
|
|
fn specialize(
|
|
|
|
pipeline: &MaterialPipeline<Self>,
|
|
|
|
descriptor: &mut RenderPipelineDescriptor,
|
2024-03-01 20:56:21 +00:00
|
|
|
layout: &MeshVertexBufferLayoutRef,
|
2023-10-17 21:28:08 +00:00
|
|
|
key: MaterialPipelineKey<Self>,
|
|
|
|
) -> Result<(), SpecializedMeshPipelineError> {
|
|
|
|
// Call the base material's specialize function
|
|
|
|
let MaterialPipeline::<Self> {
|
|
|
|
mesh_pipeline,
|
|
|
|
material_layout,
|
|
|
|
vertex_shader,
|
|
|
|
fragment_shader,
|
Add a bindless mode to `AsBindGroup`. (#16368)
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.
![Screenshot 2024-11-12
161713](https://github.com/user-attachments/assets/4334b362-42c8-4d64-9cfb-6835f019b95c)
## 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.
2024-12-03 18:00:34 +00:00
|
|
|
bindless,
|
2023-10-17 21:28:08 +00:00
|
|
|
..
|
|
|
|
} = pipeline.clone();
|
|
|
|
let base_pipeline = MaterialPipeline::<B> {
|
|
|
|
mesh_pipeline,
|
|
|
|
material_layout,
|
|
|
|
vertex_shader,
|
|
|
|
fragment_shader,
|
Add a bindless mode to `AsBindGroup`. (#16368)
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.
![Screenshot 2024-11-12
161713](https://github.com/user-attachments/assets/4334b362-42c8-4d64-9cfb-6835f019b95c)
## 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.
2024-12-03 18:00:34 +00:00
|
|
|
bindless,
|
2023-10-17 21:28:08 +00:00
|
|
|
marker: Default::default(),
|
|
|
|
};
|
|
|
|
let base_key = MaterialPipelineKey::<B> {
|
|
|
|
mesh_key: key.mesh_key,
|
|
|
|
bind_group_data: key.bind_group_data.0,
|
|
|
|
};
|
|
|
|
B::specialize(&base_pipeline, descriptor, layout, base_key)?;
|
|
|
|
|
|
|
|
// Call the extended material's specialize function afterwards
|
|
|
|
let MaterialPipeline::<Self> {
|
|
|
|
mesh_pipeline,
|
|
|
|
material_layout,
|
|
|
|
vertex_shader,
|
|
|
|
fragment_shader,
|
Add a bindless mode to `AsBindGroup`. (#16368)
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.
![Screenshot 2024-11-12
161713](https://github.com/user-attachments/assets/4334b362-42c8-4d64-9cfb-6835f019b95c)
## 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.
2024-12-03 18:00:34 +00:00
|
|
|
bindless,
|
2023-10-17 21:28:08 +00:00
|
|
|
..
|
|
|
|
} = pipeline.clone();
|
|
|
|
|
|
|
|
E::specialize(
|
|
|
|
&MaterialExtensionPipeline {
|
|
|
|
mesh_pipeline,
|
|
|
|
material_layout,
|
|
|
|
vertex_shader,
|
|
|
|
fragment_shader,
|
Add a bindless mode to `AsBindGroup`. (#16368)
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.
![Screenshot 2024-11-12
161713](https://github.com/user-attachments/assets/4334b362-42c8-4d64-9cfb-6835f019b95c)
## 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.
2024-12-03 18:00:34 +00:00
|
|
|
bindless,
|
2023-10-17 21:28:08 +00:00
|
|
|
},
|
|
|
|
descriptor,
|
|
|
|
layout,
|
|
|
|
MaterialExtensionKey {
|
|
|
|
mesh_key: key.mesh_key,
|
|
|
|
bind_group_data: key.bind_group_data.1,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|