From 4bf20e7d271a188dcb0f560c5625127960777a13 Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Tue, 28 Nov 2023 17:26:22 -0500 Subject: [PATCH] Swap material and mesh bind groups (#10485) # Objective - Materials should be a more frequent rebind then meshes (due to being able to use a single vertex buffer, such as in #10164) and therefore should be in a higher bind group. --- ## Changelog - For 2d and 3d mesh/material setups (but not UI materials, or other rendering setups such as gizmos, sprites, or text), mesh data is now in bind group 1, and material data is now in bind group 2, which is swapped from how they were before. ## Migration Guide - Custom 2d and 3d mesh/material shaders should now use bind group 2 `@group(2) @binding(x)` for their bound resources, instead of bind group 1. - Many internal pieces of rendering code have changed so that mesh data is now in bind group 1, and material data is now in bind group 2. Semi-custom rendering setups (that don't use the Material or Material2d APIs) should adapt to these changes. --- assets/shaders/array_texture.wgsl | 4 +- assets/shaders/circle_shader.wgsl | 3 +- assets/shaders/cubemap_unlit.wgsl | 6 +-- assets/shaders/custom_material.frag | 6 +-- assets/shaders/custom_material.vert | 4 +- assets/shaders/custom_material.wgsl | 6 +-- .../custom_material_screenspace_texture.wgsl | 4 +- assets/shaders/custom_vertex_attribute.wgsl | 2 +- assets/shaders/extended_material.wgsl | 2 +- assets/shaders/fallback_image_test.wgsl | 24 ++++++------ assets/shaders/line_material.wgsl | 2 +- assets/shaders/shader_defs.wgsl | 2 +- assets/shaders/show_prepass.wgsl | 2 +- assets/shaders/texture_binding_array.wgsl | 4 +- crates/bevy_pbr/src/material.rs | 12 +++--- crates/bevy_pbr/src/prepass/mod.rs | 8 ++-- .../src/prepass/prepass_bindings.wgsl | 2 +- crates/bevy_pbr/src/render/mesh.rs | 4 +- crates/bevy_pbr/src/render/mesh_bindings.wgsl | 12 ------ crates/bevy_pbr/src/render/morph.wgsl | 10 ----- crates/bevy_pbr/src/render/pbr_bindings.wgsl | 38 +++++++++---------- crates/bevy_pbr/src/render/skinning.wgsl | 7 +--- crates/bevy_pbr/src/render/wireframe.wgsl | 2 +- .../src/render_resource/bind_group.rs | 12 +++--- .../src/mesh2d/color_material.wgsl | 6 +-- crates/bevy_sprite/src/mesh2d/material.rs | 12 +++--- .../src/mesh2d/mesh2d_bindings.wgsl | 12 ------ examples/2d/mesh2d_manual.rs | 9 +---- examples/shader/shader_instancing.rs | 8 ---- examples/shader/texture_binding_array.rs | 4 +- 30 files changed, 88 insertions(+), 141 deletions(-) diff --git a/assets/shaders/array_texture.wgsl b/assets/shaders/array_texture.wgsl index 161efadb18..f67e08aa06 100644 --- a/assets/shaders/array_texture.wgsl +++ b/assets/shaders/array_texture.wgsl @@ -6,8 +6,8 @@ } #import bevy_core_pipeline::tonemapping::tone_mapping -@group(1) @binding(0) var my_array_texture: texture_2d_array; -@group(1) @binding(1) var my_array_texture_sampler: sampler; +@group(2) @binding(0) var my_array_texture: texture_2d_array; +@group(2) @binding(1) var my_array_texture_sampler: sampler; @fragment fn fragment( diff --git a/assets/shaders/circle_shader.wgsl b/assets/shaders/circle_shader.wgsl index 94f7445e08..7e3f676626 100644 --- a/assets/shaders/circle_shader.wgsl +++ b/assets/shaders/circle_shader.wgsl @@ -12,10 +12,9 @@ var input: CustomUiMaterial; fn fragment(in: UiVertexOutput) -> @location(0) vec4 { // the UVs are now adjusted around the middle of the rect. let uv = in.uv * 2.0 - 1.0; - + // circle alpha, the higher the power the harsher the falloff. let alpha = 1.0 - pow(sqrt(dot(uv, uv)), 100.0); return vec4(input.color.rgb, alpha); } - diff --git a/assets/shaders/cubemap_unlit.wgsl b/assets/shaders/cubemap_unlit.wgsl index 425eb2f5f2..14e45a045b 100644 --- a/assets/shaders/cubemap_unlit.wgsl +++ b/assets/shaders/cubemap_unlit.wgsl @@ -1,12 +1,12 @@ #import bevy_pbr::forward_io::VertexOutput #ifdef CUBEMAP_ARRAY -@group(1) @binding(0) var base_color_texture: texture_cube_array; +@group(2) @binding(0) var base_color_texture: texture_cube_array; #else -@group(1) @binding(0) var base_color_texture: texture_cube; +@group(2) @binding(0) var base_color_texture: texture_cube; #endif -@group(1) @binding(1) var base_color_sampler: sampler; +@group(2) @binding(1) var base_color_sampler: sampler; @fragment fn fragment( diff --git a/assets/shaders/custom_material.frag b/assets/shaders/custom_material.frag index 004bdf787a..a6bc9af0d2 100644 --- a/assets/shaders/custom_material.frag +++ b/assets/shaders/custom_material.frag @@ -3,10 +3,10 @@ layout(location = 0) in vec2 v_Uv; layout(location = 0) out vec4 o_Target; -layout(set = 1, binding = 0) uniform vec4 CustomMaterial_color; +layout(set = 2, binding = 0) uniform vec4 CustomMaterial_color; -layout(set = 1, binding = 1) uniform texture2D CustomMaterial_texture; -layout(set = 1, binding = 2) uniform sampler CustomMaterial_sampler; +layout(set = 2, binding = 1) uniform texture2D CustomMaterial_texture; +layout(set = 2, binding = 2) uniform sampler CustomMaterial_sampler; // wgsl modules can be imported and used in glsl // FIXME - this doesn't work any more ... diff --git a/assets/shaders/custom_material.vert b/assets/shaders/custom_material.vert index b9d6cafc1f..91d660ec31 100644 --- a/assets/shaders/custom_material.vert +++ b/assets/shaders/custom_material.vert @@ -23,9 +23,9 @@ struct Mesh { }; #ifdef PER_OBJECT_BUFFER_BATCH_SIZE -layout(set = 2, binding = 0) uniform Mesh Meshes[#{PER_OBJECT_BUFFER_BATCH_SIZE}]; +layout(set = 1, binding = 0) uniform Mesh Meshes[#{PER_OBJECT_BUFFER_BATCH_SIZE}]; #else -layout(set = 2, binding = 0) readonly buffer _Meshes { +layout(set = 1, binding = 0) readonly buffer _Meshes { Mesh Meshes[]; }; #endif // PER_OBJECT_BUFFER_BATCH_SIZE diff --git a/assets/shaders/custom_material.wgsl b/assets/shaders/custom_material.wgsl index 88d904dd1b..1b65627d45 100644 --- a/assets/shaders/custom_material.wgsl +++ b/assets/shaders/custom_material.wgsl @@ -2,9 +2,9 @@ // we can import items from shader modules in the assets folder with a quoted path #import "shaders/custom_material_import.wgsl"::COLOR_MULTIPLIER -@group(1) @binding(0) var material_color: vec4; -@group(1) @binding(1) var material_color_texture: texture_2d; -@group(1) @binding(2) var material_color_sampler: sampler; +@group(2) @binding(0) var material_color: vec4; +@group(2) @binding(1) var material_color_texture: texture_2d; +@group(2) @binding(2) var material_color_sampler: sampler; @fragment fn fragment( diff --git a/assets/shaders/custom_material_screenspace_texture.wgsl b/assets/shaders/custom_material_screenspace_texture.wgsl index a4afd64225..36da2a7f8c 100644 --- a/assets/shaders/custom_material_screenspace_texture.wgsl +++ b/assets/shaders/custom_material_screenspace_texture.wgsl @@ -4,8 +4,8 @@ utils::coords_to_viewport_uv, } -@group(1) @binding(0) var texture: texture_2d; -@group(1) @binding(1) var texture_sampler: sampler; +@group(2) @binding(0) var texture: texture_2d; +@group(2) @binding(1) var texture_sampler: sampler; @fragment fn fragment( diff --git a/assets/shaders/custom_vertex_attribute.wgsl b/assets/shaders/custom_vertex_attribute.wgsl index d17246f89a..89cefd80f1 100644 --- a/assets/shaders/custom_vertex_attribute.wgsl +++ b/assets/shaders/custom_vertex_attribute.wgsl @@ -3,7 +3,7 @@ struct CustomMaterial { color: vec4, }; -@group(1) @binding(0) var material: CustomMaterial; +@group(2) @binding(0) var material: CustomMaterial; struct Vertex { @builtin(instance_index) instance_index: u32, diff --git a/assets/shaders/extended_material.wgsl b/assets/shaders/extended_material.wgsl index c6fd8aea9d..fc69f30bb5 100644 --- a/assets/shaders/extended_material.wgsl +++ b/assets/shaders/extended_material.wgsl @@ -19,7 +19,7 @@ struct MyExtendedMaterial { quantize_steps: u32, } -@group(1) @binding(100) +@group(2) @binding(100) var my_extended_material: MyExtendedMaterial; @fragment diff --git a/assets/shaders/fallback_image_test.wgsl b/assets/shaders/fallback_image_test.wgsl index 5912478658..c48f091bcc 100644 --- a/assets/shaders/fallback_image_test.wgsl +++ b/assets/shaders/fallback_image_test.wgsl @@ -1,22 +1,22 @@ #import bevy_pbr::forward_io::VertexOutput -@group(1) @binding(0) var test_texture_1d: texture_1d; -@group(1) @binding(1) var test_texture_1d_sampler: sampler; +@group(2) @binding(0) var test_texture_1d: texture_1d; +@group(2) @binding(1) var test_texture_1d_sampler: sampler; -@group(1) @binding(2) var test_texture_2d: texture_2d; -@group(1) @binding(3) var test_texture_2d_sampler: sampler; +@group(2) @binding(2) var test_texture_2d: texture_2d; +@group(2) @binding(3) var test_texture_2d_sampler: sampler; -@group(1) @binding(4) var test_texture_2d_array: texture_2d_array; -@group(1) @binding(5) var test_texture_2d_array_sampler: sampler; +@group(2) @binding(4) var test_texture_2d_array: texture_2d_array; +@group(2) @binding(5) var test_texture_2d_array_sampler: sampler; -@group(1) @binding(6) var test_texture_cube: texture_cube; -@group(1) @binding(7) var test_texture_cube_sampler: sampler; +@group(2) @binding(6) var test_texture_cube: texture_cube; +@group(2) @binding(7) var test_texture_cube_sampler: sampler; -@group(1) @binding(8) var test_texture_cube_array: texture_cube_array; -@group(1) @binding(9) var test_texture_cube_array_sampler: sampler; +@group(2) @binding(8) var test_texture_cube_array: texture_cube_array; +@group(2) @binding(9) var test_texture_cube_array_sampler: sampler; -@group(1) @binding(10) var test_texture_3d: texture_3d; -@group(1) @binding(11) var test_texture_3d_sampler: sampler; +@group(2) @binding(10) var test_texture_3d: texture_3d; +@group(2) @binding(11) var test_texture_3d_sampler: sampler; @fragment fn fragment(in: VertexOutput) {} diff --git a/assets/shaders/line_material.wgsl b/assets/shaders/line_material.wgsl index e2ae15c131..cc7ca047d5 100644 --- a/assets/shaders/line_material.wgsl +++ b/assets/shaders/line_material.wgsl @@ -4,7 +4,7 @@ struct LineMaterial { color: vec4, }; -@group(1) @binding(0) var material: LineMaterial; +@group(2) @binding(0) var material: LineMaterial; @fragment fn fragment( diff --git a/assets/shaders/shader_defs.wgsl b/assets/shaders/shader_defs.wgsl index 7b98daca05..fdddc4caa1 100644 --- a/assets/shaders/shader_defs.wgsl +++ b/assets/shaders/shader_defs.wgsl @@ -4,7 +4,7 @@ struct CustomMaterial { color: vec4, }; -@group(1) @binding(0) var material: CustomMaterial; +@group(2) @binding(0) var material: CustomMaterial; @fragment fn fragment( diff --git a/assets/shaders/show_prepass.wgsl b/assets/shaders/show_prepass.wgsl index e80ea5c39d..c1b3a89750 100644 --- a/assets/shaders/show_prepass.wgsl +++ b/assets/shaders/show_prepass.wgsl @@ -11,7 +11,7 @@ struct ShowPrepassSettings { padding_1: u32, padding_2: u32, } -@group(1) @binding(0) var settings: ShowPrepassSettings; +@group(2) @binding(0) var settings: ShowPrepassSettings; @fragment fn fragment( diff --git a/assets/shaders/texture_binding_array.wgsl b/assets/shaders/texture_binding_array.wgsl index 440e3c6155..de7a4e1b96 100644 --- a/assets/shaders/texture_binding_array.wgsl +++ b/assets/shaders/texture_binding_array.wgsl @@ -1,7 +1,7 @@ #import bevy_pbr::forward_io::VertexOutput -@group(1) @binding(0) var textures: binding_array>; -@group(1) @binding(1) var nearest_sampler: sampler; +@group(2) @binding(0) var textures: binding_array>; +@group(2) @binding(1) var nearest_sampler: sampler; // We can also have array of samplers // var samplers: binding_array; diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index d5884bb655..04fd36d5a4 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -87,9 +87,9 @@ use std::marker::PhantomData; /// In WGSL shaders, the material's binding would look like this: /// /// ```wgsl -/// @group(1) @binding(0) var color: vec4; -/// @group(1) @binding(1) var color_texture: texture_2d; -/// @group(1) @binding(2) var color_sampler: sampler; +/// @group(2) @binding(0) var color: vec4; +/// @group(2) @binding(1) var color_texture: texture_2d; +/// @group(2) @binding(2) var color_sampler: sampler; /// ``` pub trait Material: Asset + AsBindGroup + Clone + Sized { /// Returns this material's vertex shader. If [`ShaderRef::Default`] is returned, the default mesh vertex shader @@ -335,7 +335,7 @@ where descriptor.fragment.as_mut().unwrap().shader = fragment_shader.clone(); } - descriptor.layout.insert(1, self.material_layout.clone()); + descriptor.layout.insert(2, self.material_layout.clone()); M::specialize(self, &mut descriptor, layout, key)?; Ok(descriptor) @@ -368,8 +368,8 @@ impl FromWorld for MaterialPipeline { type DrawMaterial = ( SetItemPipeline, SetMeshViewBindGroup<0>, - SetMaterialBindGroup, - SetMeshBindGroup<2>, + SetMeshBindGroup<1>, + SetMaterialBindGroup, DrawMesh, ); diff --git a/crates/bevy_pbr/src/prepass/mod.rs b/crates/bevy_pbr/src/prepass/mod.rs index 67ed3b8362..1103eeef93 100644 --- a/crates/bevy_pbr/src/prepass/mod.rs +++ b/crates/bevy_pbr/src/prepass/mod.rs @@ -320,7 +320,7 @@ where // NOTE: Eventually, it would be nice to only add this when the shaders are overloaded by the Material. // The main limitation right now is that bind group order is hardcoded in shaders. - bind_group_layouts.insert(1, self.material_layout.clone()); + bind_group_layouts.push(self.material_layout.clone()); #[cfg(all(feature = "webgl", target_arch = "wasm32"))] shader_defs.push("WEBGL2".into()); @@ -421,7 +421,7 @@ where &mut shader_defs, &mut vertex_attributes, ); - bind_group_layouts.insert(2, bind_group); + bind_group_layouts.insert(1, bind_group); let vertex_buffer_layout = layout.get_layout(&vertex_attributes)?; @@ -919,8 +919,8 @@ impl RenderCommand

for SetPrepassViewBindGroup< pub type DrawPrepass = ( SetItemPipeline, SetPrepassViewBindGroup<0>, - SetMaterialBindGroup, - SetMeshBindGroup<2>, + SetMeshBindGroup<1>, + SetMaterialBindGroup, DrawMesh, ); diff --git a/crates/bevy_pbr/src/prepass/prepass_bindings.wgsl b/crates/bevy_pbr/src/prepass/prepass_bindings.wgsl index 4e4e8c453f..b574ef3fa4 100644 --- a/crates/bevy_pbr/src/prepass/prepass_bindings.wgsl +++ b/crates/bevy_pbr/src/prepass/prepass_bindings.wgsl @@ -4,4 +4,4 @@ @group(0) @binding(2) var previous_view_proj: mat4x4; #endif // MOTION_VECTOR_PREPASS -// Material bindings will be in @group(1) +// Material bindings will be in @group(2) diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index e994996656..f54f576967 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -339,9 +339,9 @@ pub struct MeshPipeline { /// Use code like this in custom shaders: /// ```wgsl /// ##ifdef PER_OBJECT_BUFFER_BATCH_SIZE - /// @group(2) @binding(0) var mesh: array; + /// @group(1) @binding(0) var mesh: array; /// ##else - /// @group(2) @binding(0) var mesh: array; + /// @group(1) @binding(0) var mesh: array; /// ##endif // PER_OBJECT_BUFFER_BATCH_SIZE /// ``` pub per_object_buffer_batch_size: Option, diff --git a/crates/bevy_pbr/src/render/mesh_bindings.wgsl b/crates/bevy_pbr/src/render/mesh_bindings.wgsl index 81cca44e6c..2366dab155 100644 --- a/crates/bevy_pbr/src/render/mesh_bindings.wgsl +++ b/crates/bevy_pbr/src/render/mesh_bindings.wgsl @@ -2,20 +2,8 @@ #import bevy_pbr::mesh_types::Mesh -#ifdef MESH_BINDGROUP_1 - #ifdef PER_OBJECT_BUFFER_BATCH_SIZE @group(1) @binding(0) var mesh: array; #else @group(1) @binding(0) var mesh: array; #endif // PER_OBJECT_BUFFER_BATCH_SIZE - -#else // MESH_BINDGROUP_1 - -#ifdef PER_OBJECT_BUFFER_BATCH_SIZE -@group(2) @binding(0) var mesh: array; -#else -@group(2) @binding(0) var mesh: array; -#endif // PER_OBJECT_BUFFER_BATCH_SIZE - -#endif // MESH_BINDGROUP_1 diff --git a/crates/bevy_pbr/src/render/morph.wgsl b/crates/bevy_pbr/src/render/morph.wgsl index 7355f95f33..bd6eb1d041 100644 --- a/crates/bevy_pbr/src/render/morph.wgsl +++ b/crates/bevy_pbr/src/render/morph.wgsl @@ -4,19 +4,9 @@ #import bevy_pbr::mesh_types::MorphWeights; -#ifdef MESH_BINDGROUP_1 - @group(1) @binding(2) var morph_weights: MorphWeights; @group(1) @binding(3) var morph_targets: texture_3d; -#else - -@group(2) @binding(2) var morph_weights: MorphWeights; -@group(2) @binding(3) var morph_targets: texture_3d; - -#endif - - // NOTE: Those are the "hardcoded" values found in `MorphAttributes` struct // in crates/bevy_render/src/mesh/morph/visitors.rs // In an ideal world, the offsets are established dynamically and passed as #defines diff --git a/crates/bevy_pbr/src/render/pbr_bindings.wgsl b/crates/bevy_pbr/src/render/pbr_bindings.wgsl index c0e060ecab..2f26e010b0 100644 --- a/crates/bevy_pbr/src/render/pbr_bindings.wgsl +++ b/crates/bevy_pbr/src/render/pbr_bindings.wgsl @@ -2,24 +2,24 @@ #import bevy_pbr::pbr_types::StandardMaterial -@group(1) @binding(0) var material: StandardMaterial; -@group(1) @binding(1) var base_color_texture: texture_2d; -@group(1) @binding(2) var base_color_sampler: sampler; -@group(1) @binding(3) var emissive_texture: texture_2d; -@group(1) @binding(4) var emissive_sampler: sampler; -@group(1) @binding(5) var metallic_roughness_texture: texture_2d; -@group(1) @binding(6) var metallic_roughness_sampler: sampler; -@group(1) @binding(7) var occlusion_texture: texture_2d; -@group(1) @binding(8) var occlusion_sampler: sampler; -@group(1) @binding(9) var normal_map_texture: texture_2d; -@group(1) @binding(10) var normal_map_sampler: sampler; -@group(1) @binding(11) var depth_map_texture: texture_2d; -@group(1) @binding(12) var depth_map_sampler: sampler; +@group(2) @binding(0) var material: StandardMaterial; +@group(2) @binding(1) var base_color_texture: texture_2d; +@group(2) @binding(2) var base_color_sampler: sampler; +@group(2) @binding(3) var emissive_texture: texture_2d; +@group(2) @binding(4) var emissive_sampler: sampler; +@group(2) @binding(5) var metallic_roughness_texture: texture_2d; +@group(2) @binding(6) var metallic_roughness_sampler: sampler; +@group(2) @binding(7) var occlusion_texture: texture_2d; +@group(2) @binding(8) var occlusion_sampler: sampler; +@group(2) @binding(9) var normal_map_texture: texture_2d; +@group(2) @binding(10) var normal_map_sampler: sampler; +@group(2) @binding(11) var depth_map_texture: texture_2d; +@group(2) @binding(12) var depth_map_sampler: sampler; #ifdef PBR_TRANSMISSION_TEXTURES_SUPPORTED -@group(1) @binding(13) var specular_transmission_texture: texture_2d; -@group(1) @binding(14) var specular_transmission_sampler: sampler; -@group(1) @binding(15) var thickness_texture: texture_2d; -@group(1) @binding(16) var thickness_sampler: sampler; -@group(1) @binding(17) var diffuse_transmission_texture: texture_2d; -@group(1) @binding(18) var diffuse_transmission_sampler: sampler; +@group(2) @binding(13) var specular_transmission_texture: texture_2d; +@group(2) @binding(14) var specular_transmission_sampler: sampler; +@group(2) @binding(15) var thickness_texture: texture_2d; +@group(2) @binding(16) var thickness_sampler: sampler; +@group(2) @binding(17) var diffuse_transmission_texture: texture_2d; +@group(2) @binding(18) var diffuse_transmission_sampler: sampler; #endif diff --git a/crates/bevy_pbr/src/render/skinning.wgsl b/crates/bevy_pbr/src/render/skinning.wgsl index 3f23629d1d..4307a22ddf 100644 --- a/crates/bevy_pbr/src/render/skinning.wgsl +++ b/crates/bevy_pbr/src/render/skinning.wgsl @@ -4,12 +4,7 @@ #ifdef SKINNED -#ifdef MESH_BINDGROUP_1 - @group(1) @binding(1) var joint_matrices: SkinnedMesh; -#else - @group(2) @binding(1) var joint_matrices: SkinnedMesh; -#endif - +@group(1) @binding(1) var joint_matrices: SkinnedMesh; fn skin_model( indexes: vec4, diff --git a/crates/bevy_pbr/src/render/wireframe.wgsl b/crates/bevy_pbr/src/render/wireframe.wgsl index ed5c24b4b2..981e5e1b1d 100644 --- a/crates/bevy_pbr/src/render/wireframe.wgsl +++ b/crates/bevy_pbr/src/render/wireframe.wgsl @@ -4,7 +4,7 @@ struct WireframeMaterial { color: vec4, }; -@group(1) @binding(0) +@group(2) @binding(0) var material: WireframeMaterial; @fragment fn fragment(in: VertexOutput) -> @location(0) vec4 { diff --git a/crates/bevy_render/src/render_resource/bind_group.rs b/crates/bevy_render/src/render_resource/bind_group.rs index 22ab55fd12..619d9b31af 100644 --- a/crates/bevy_render/src/render_resource/bind_group.rs +++ b/crates/bevy_render/src/render_resource/bind_group.rs @@ -93,13 +93,13 @@ impl Deref for BindGroup { /// In WGSL shaders, the binding would look like this: /// /// ```wgsl -/// @group(1) @binding(0) var color: vec4; -/// @group(1) @binding(1) var color_texture: texture_2d; -/// @group(1) @binding(2) var color_sampler: sampler; -/// @group(1) @binding(3) var values: array; +/// @group(2) @binding(0) var color: vec4; +/// @group(2) @binding(1) var color_texture: texture_2d; +/// @group(2) @binding(2) var color_sampler: sampler; +/// @group(2) @binding(3) var values: array; /// ``` /// Note that the "group" index is determined by the usage context. It is not defined in [`AsBindGroup`]. For example, in Bevy material bind groups -/// are generally bound to group 1. +/// are generally bound to group 2. /// /// The following field-level attributes are supported: /// @@ -191,7 +191,7 @@ impl Deref for BindGroup { /// roughness: f32, /// }; /// -/// @group(1) @binding(0) var material: CoolMaterial; +/// @group(2) @binding(0) var material: CoolMaterial; /// ``` /// /// Some less common scenarios will require "struct-level" attributes. These are the currently supported struct-level attributes: diff --git a/crates/bevy_sprite/src/mesh2d/color_material.wgsl b/crates/bevy_sprite/src/mesh2d/color_material.wgsl index 1ed5d75341..7bc7725116 100644 --- a/crates/bevy_sprite/src/mesh2d/color_material.wgsl +++ b/crates/bevy_sprite/src/mesh2d/color_material.wgsl @@ -14,9 +14,9 @@ struct ColorMaterial { }; const COLOR_MATERIAL_FLAGS_TEXTURE_BIT: u32 = 1u; -@group(1) @binding(0) var material: ColorMaterial; -@group(1) @binding(1) var texture: texture_2d; -@group(1) @binding(2) var texture_sampler: sampler; +@group(2) @binding(0) var material: ColorMaterial; +@group(2) @binding(1) var texture: texture_2d; +@group(2) @binding(2) var texture_sampler: sampler; @fragment fn fragment( diff --git a/crates/bevy_sprite/src/mesh2d/material.rs b/crates/bevy_sprite/src/mesh2d/material.rs index d58fdfb7e3..f1e23415d0 100644 --- a/crates/bevy_sprite/src/mesh2d/material.rs +++ b/crates/bevy_sprite/src/mesh2d/material.rs @@ -95,9 +95,9 @@ use crate::{ /// color: vec4, /// } /// -/// @group(1) @binding(0) var material: CustomMaterial; -/// @group(1) @binding(1) var color_texture: texture_2d; -/// @group(1) @binding(2) var color_sampler: sampler; +/// @group(2) @binding(0) var material: CustomMaterial; +/// @group(2) @binding(1) var color_texture: texture_2d; +/// @group(2) @binding(2) var color_sampler: sampler; /// ``` pub trait Material2d: AsBindGroup + Asset + Clone + Sized { /// Returns this material's vertex shader. If [`ShaderRef::Default`] is returned, the default mesh vertex shader @@ -281,8 +281,8 @@ where } descriptor.layout = vec![ self.mesh2d_pipeline.view_layout.clone(), - self.material2d_layout.clone(), self.mesh2d_pipeline.mesh_layout.clone(), + self.material2d_layout.clone(), ]; M::specialize(&mut descriptor, layout, key)?; @@ -317,8 +317,8 @@ impl FromWorld for Material2dPipeline { type DrawMaterial2d = ( SetItemPipeline, SetMesh2dViewBindGroup<0>, - SetMaterial2dBindGroup, - SetMesh2dBindGroup<2>, + SetMesh2dBindGroup<1>, + SetMaterial2dBindGroup, DrawMesh2d, ); diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_bindings.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_bindings.wgsl index 3c3ec0906e..fc2bf643d5 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh2d_bindings.wgsl +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_bindings.wgsl @@ -2,20 +2,8 @@ #import bevy_sprite::mesh2d_types::Mesh2d -#ifdef MESH_BINDGROUP_1 - #ifdef PER_OBJECT_BUFFER_BATCH_SIZE @group(1) @binding(0) var mesh: array; #else @group(1) @binding(0) var mesh: array; #endif // PER_OBJECT_BUFFER_BATCH_SIZE - -#else // MESH_BINDGROUP_1 - -#ifdef PER_OBJECT_BUFFER_BATCH_SIZE -@group(2) @binding(0) var mesh: array; -#else -@group(2) @binding(0) var mesh: array; -#endif // PER_OBJECT_BUFFER_BATCH_SIZE - -#endif // MESH_BINDGROUP_1 diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index a940ed142f..bf3501c2a0 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -151,24 +151,19 @@ impl SpecializedRenderPipeline for ColoredMesh2dPipeline { false => TextureFormat::bevy_default(), }; - // Meshes typically live in bind group 2. Because we are using bind group 1 - // we need to add the MESH_BINDGROUP_1 shader def so that the bindings are correctly - // linked in the shader. - let shader_defs = vec!["MESH_BINDGROUP_1".into()]; - RenderPipelineDescriptor { vertex: VertexState { // Use our custom shader shader: COLORED_MESH2D_SHADER_HANDLE, entry_point: "vertex".into(), - shader_defs: shader_defs.clone(), + shader_defs: vec![], // Use our custom vertex buffer buffers: vec![vertex_layout], }, fragment: Some(FragmentState { // Use our custom shader shader: COLORED_MESH2D_SHADER_HANDLE, - shader_defs, + shader_defs: vec![], entry_point: "fragment".into(), targets: vec![Some(ColorTargetState { format, diff --git a/examples/shader/shader_instancing.rs b/examples/shader/shader_instancing.rs index 3ff58c0b96..8186b0d74d 100644 --- a/examples/shader/shader_instancing.rs +++ b/examples/shader/shader_instancing.rs @@ -204,14 +204,6 @@ impl SpecializedMeshPipeline for CustomPipeline { ) -> Result { let mut descriptor = self.mesh_pipeline.specialize(key, layout)?; - // meshes typically live in bind group 2. because we are using bindgroup 1 - // we need to add MESH_BINDGROUP_1 shader def so that the bindings are correctly - // linked in the shader - descriptor - .vertex - .shader_defs - .push("MESH_BINDGROUP_1".into()); - descriptor.vertex.shader = self.shader.clone(); descriptor.vertex.buffers.push(VertexBufferLayout { array_stride: std::mem::size_of::() as u64, diff --git a/examples/shader/texture_binding_array.rs b/examples/shader/texture_binding_array.rs index 82bcf34acc..91c23479c6 100644 --- a/examples/shader/texture_binding_array.rs +++ b/examples/shader/texture_binding_array.rs @@ -146,7 +146,7 @@ impl AsBindGroup for BindlessMaterial { Self: Sized, { vec![ - // @group(1) @binding(0) var textures: binding_array>; + // @group(2) @binding(0) var textures: binding_array>; BindGroupLayoutEntry { binding: 0, visibility: ShaderStages::FRAGMENT, @@ -157,7 +157,7 @@ impl AsBindGroup for BindlessMaterial { }, count: NonZeroU32::new(MAX_TEXTURE_COUNT as u32), }, - // @group(1) @binding(1) var nearest_sampler: sampler; + // @group(2) @binding(1) var nearest_sampler: sampler; BindGroupLayoutEntry { binding: 1, visibility: ShaderStages::FRAGMENT,