
# Objective Upgrade to `wgpu` version `25.0`. Depends on https://github.com/bevyengine/naga_oil/pull/121 ## Solution ### Problem The biggest issue we face upgrading is the following requirement: > To facilitate this change, there was an additional validation rule put in place: if there is a binding array in a bind group, you may not use dynamic offset buffers or uniform buffers in that bind group. This requirement comes from vulkan rules on UpdateAfterBind descriptors. This is a major difficulty for us, as there are a number of binding arrays that are used in the view bind group. Note, this requirement does not affect merely uniform buffors that use dynamic offset but the use of *any* uniform in a bind group that also has a binding array. ### Attempted fixes The easiest fix would be to change uniforms to be storage buffers whenever binding arrays are in use: ```wgsl #ifdef BINDING_ARRAYS_ARE_USED @group(0) @binding(0) var<uniform> view: View; @group(0) @binding(1) var<uniform> lights: types::Lights; #else @group(0) @binding(0) var<storage> view: array<View>; @group(0) @binding(1) var<storage> lights: array<types::Lights>; #endif ``` This requires passing the view index to the shader so that we know where to index into the buffer: ```wgsl struct PushConstants { view_index: u32, } var<push_constant> push_constants: PushConstants; ``` Using push constants is no problem because binding arrays are only usable on native anyway. However, this greatly complicates the ability to access `view` in shaders. For example: ```wgsl #ifdef BINDING_ARRAYS_ARE_USED mesh_view_bindings::view.view_from_world[0].z #else mesh_view_bindings::view[mesh_view_bindings::view_index].view_from_world[0].z #endif ``` Using this approach would work but would have the effect of polluting our shaders with ifdef spam basically *everywhere*. Why not use a function? Unfortunately, the following is not valid wgsl as it returns a binding directly from a function in the uniform path. ```wgsl fn get_view() -> View { #if BINDING_ARRAYS_ARE_USED let view_index = push_constants.view_index; let view = views[view_index]; #endif return view; } ``` This also poses problems for things like lights where we want to return a ptr to the light data. Returning ptrs from wgsl functions isn't allowed even if both bindings were buffers. The next attempt was to simply use indexed buffers everywhere, in both the binding array and non binding array path. This would be viable if push constants were available everywhere to pass the view index, but unfortunately they are not available on webgpu. This means either passing the view index in a storage buffer (not ideal for such a small amount of state) or using push constants sometimes and uniform buffers only on webgpu. However, this kind of conditional layout infects absolutely everything. Even if we were to accept just using storage buffer for the view index, there's also the additional problem that some dynamic offsets aren't actually per-view but per-use of a setting on a camera, which would require passing that uniform data on *every* camera regardless of whether that rendering feature is being used, which is also gross. As such, although it's gross, the simplest solution just to bump binding arrays into `@group(1)` and all other bindings up one bind group. This should still bring us under the device limit of 4 for most users. ### Next steps / looking towards the future I'd like to avoid needing split our view bind group into multiple parts. In the future, if `wgpu` were to add `@builtin(draw_index)`, we could build a list of draw state in gpu processing and avoid the need for any kind of state change at all (see https://github.com/gfx-rs/wgpu/issues/6823). This would also provide significantly more flexibility to handle things like offsets into other arrays that may not be per-view. ### Testing Tested a number of examples, there are probably more that are still broken. --------- Co-authored-by: François Mockers <mockersf@gmail.com> Co-authored-by: Elabajaba <Elabajaba@users.noreply.github.com>
108 lines
4.3 KiB
WebGPU Shading Language
108 lines
4.3 KiB
WebGPU Shading Language
// The shader that goes with `extended_material_bindless.rs`.
|
|
//
|
|
// This code demonstrates how to write shaders that are compatible with both
|
|
// bindless and non-bindless mode. See the `#ifdef BINDLESS` blocks.
|
|
|
|
#import bevy_pbr::{
|
|
forward_io::{FragmentOutput, VertexOutput},
|
|
mesh_bindings::mesh,
|
|
pbr_fragment::pbr_input_from_standard_material,
|
|
pbr_functions::{apply_pbr_lighting, main_pass_post_lighting_processing},
|
|
}
|
|
#import bevy_render::bindless::{bindless_samplers_filtering, bindless_textures_2d}
|
|
|
|
#ifdef BINDLESS
|
|
#import bevy_pbr::pbr_bindings::{material_array, material_indices}
|
|
#else // BINDLESS
|
|
#import bevy_pbr::pbr_bindings::material
|
|
#endif // BINDLESS
|
|
|
|
// Stores the indices of the bindless resources in the bindless resource arrays,
|
|
// for the `ExampleBindlessExtension` fields.
|
|
struct ExampleBindlessExtendedMaterialIndices {
|
|
// The index of the `ExampleBindlessExtendedMaterial` data in
|
|
// `example_extended_material`.
|
|
material: u32,
|
|
// The index of the texture we're going to modulate the base color with in
|
|
// the `bindless_textures_2d` array.
|
|
modulate_texture: u32,
|
|
// The index of the sampler we're going to sample the modulated texture with
|
|
// in the `bindless_samplers_filtering` array.
|
|
modulate_texture_sampler: u32,
|
|
}
|
|
|
|
// Plain data associated with this example material.
|
|
struct ExampleBindlessExtendedMaterial {
|
|
// The color that we multiply the base color, base color texture, and
|
|
// modulated texture with.
|
|
modulate_color: vec4<f32>,
|
|
}
|
|
|
|
#ifdef BINDLESS
|
|
|
|
// The indices of the bindless resources in the bindless resource arrays, for
|
|
// the `ExampleBindlessExtension` fields.
|
|
@group(3) @binding(100) var<storage> example_extended_material_indices:
|
|
array<ExampleBindlessExtendedMaterialIndices>;
|
|
// An array that holds the `ExampleBindlessExtendedMaterial` plain old data,
|
|
// indexed by `ExampleBindlessExtendedMaterialIndices.material`.
|
|
@group(3) @binding(101) var<storage> example_extended_material:
|
|
array<ExampleBindlessExtendedMaterial>;
|
|
|
|
#else // BINDLESS
|
|
|
|
// In non-bindless mode, we simply use a uniform for the plain old data.
|
|
@group(3) @binding(50) var<uniform> example_extended_material: ExampleBindlessExtendedMaterial;
|
|
@group(3) @binding(51) var modulate_texture: texture_2d<f32>;
|
|
@group(3) @binding(52) var modulate_sampler: sampler;
|
|
|
|
#endif // BINDLESS
|
|
|
|
@fragment
|
|
fn fragment(
|
|
in: VertexOutput,
|
|
@builtin(front_facing) is_front: bool,
|
|
) -> FragmentOutput {
|
|
#ifdef BINDLESS
|
|
// Fetch the material slot. We'll use this in turn to fetch the bindless
|
|
// indices from `example_extended_material_indices`.
|
|
let slot = mesh[in.instance_index].material_and_lightmap_bind_group_slot & 0xffffu;
|
|
#endif // BINDLESS
|
|
|
|
// Generate a `PbrInput` struct from the `StandardMaterial` bindings.
|
|
var pbr_input = pbr_input_from_standard_material(in, is_front);
|
|
|
|
// Calculate the UV for the texture we're about to sample.
|
|
#ifdef BINDLESS
|
|
let uv_transform = material_array[material_indices[slot].material].uv_transform;
|
|
#else // BINDLESS
|
|
let uv_transform = material.uv_transform;
|
|
#endif // BINDLESS
|
|
let uv = (uv_transform * vec3(in.uv, 1.0)).xy;
|
|
|
|
// Multiply the base color by the `modulate_texture` and `modulate_color`.
|
|
#ifdef BINDLESS
|
|
// Notice how we fetch the texture, sampler, and plain extended material
|
|
// data from the appropriate arrays.
|
|
pbr_input.material.base_color *= textureSample(
|
|
bindless_textures_2d[example_extended_material_indices[slot].modulate_texture],
|
|
bindless_samplers_filtering[
|
|
example_extended_material_indices[slot].modulate_texture_sampler
|
|
],
|
|
uv
|
|
) * example_extended_material[example_extended_material_indices[slot].material].modulate_color;
|
|
#else // BINDLESS
|
|
pbr_input.material.base_color *= textureSample(modulate_texture, modulate_sampler, uv) *
|
|
example_extended_material.modulate_color;
|
|
#endif // BINDLESS
|
|
|
|
var out: FragmentOutput;
|
|
// Apply lighting.
|
|
out.color = apply_pbr_lighting(pbr_input);
|
|
// Apply in-shader post processing (fog, alpha-premultiply, and also
|
|
// tonemapping, debanding if the camera is non-HDR). Note this does not
|
|
// include fullscreen postprocessing effects like bloom.
|
|
out.color = main_pass_post_lighting_processing(pbr_input, out.color);
|
|
return out;
|
|
}
|