This commit is contained in:
charlotte 🌸 2025-07-16 17:44:25 -04:00 committed by GitHub
commit 40b83ba0a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 128 additions and 103 deletions

View File

@ -7,8 +7,8 @@
}
#import bevy_core_pipeline::tonemapping::tone_mapping
@group(3) @binding(0) var my_array_texture: texture_2d_array<f32>;
@group(3) @binding(1) var my_array_texture_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var my_array_texture: texture_2d_array<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var my_array_texture_sampler: sampler;
@fragment
fn fragment(

View File

@ -3,8 +3,8 @@
view_transformations::position_world_to_clip
}
@group(3) @binding(0) var texture: texture_2d<f32>;
@group(3) @binding(1) var texture_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var texture_sampler: sampler;
struct Vertex {
@builtin(instance_index) instance_index: u32,

View File

@ -15,12 +15,12 @@ struct MaterialBindings {
}
#ifdef BINDLESS
@group(3) @binding(0) var<storage> materials: array<MaterialBindings>;
@group(3) @binding(10) var<storage> material_color: binding_array<Color>;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<storage> materials: array<MaterialBindings>;
@group(#{MATERIAL_BIND_GROUP}) @binding(10) var<storage> material_color: binding_array<Color>;
#else // BINDLESS
@group(3) @binding(0) var<uniform> material_color: Color;
@group(3) @binding(1) var material_color_texture: texture_2d<f32>;
@group(3) @binding(2) var material_color_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> material_color: Color;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var material_color_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var material_color_sampler: sampler;
#endif // BINDLESS
@fragment

View File

@ -1,12 +1,12 @@
#import bevy_pbr::forward_io::VertexOutput
#ifdef CUBEMAP_ARRAY
@group(3) @binding(0) var base_color_texture: texture_cube_array<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var base_color_texture: texture_cube_array<f32>;
#else
@group(3) @binding(0) var base_color_texture: texture_cube<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var base_color_texture: texture_cube<f32>;
#endif
@group(3) @binding(1) var base_color_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var base_color_sampler: sampler;
@fragment
fn fragment(

View File

@ -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(3) @binding(0) var<uniform> material_color: vec4<f32>;
@group(3) @binding(1) var material_color_texture: texture_2d<f32>;
@group(3) @binding(2) var material_color_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> material_color: vec4<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var material_color_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var material_color_sampler: sampler;
@fragment
fn fragment(

View File

@ -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(2) @binding(0) var<uniform> material_color: vec4<f32>;
@group(2) @binding(1) var base_color_texture: texture_2d<f32>;
@group(2) @binding(2) var base_color_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> material_color: vec4<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var base_color_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var base_color_sampler: sampler;
@fragment
fn fragment(mesh: VertexOutput) -> @location(0) vec4<f32> {

View File

@ -4,8 +4,8 @@
utils::coords_to_viewport_uv,
}
@group(3) @binding(0) var texture: texture_2d<f32>;
@group(3) @binding(1) var texture_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var texture_sampler: sampler;
@fragment
fn fragment(

View File

@ -5,7 +5,7 @@
struct CustomMaterial {
color: vec4<f32>,
};
@group(3) @binding(0) var<uniform> material: CustomMaterial;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> material: CustomMaterial;
struct Vertex {
@builtin(instance_index) instance_index: u32,

View File

@ -25,7 +25,7 @@ struct MyExtendedMaterial {
#endif
}
@group(3) @binding(100)
@group(#{MATERIAL_BIND_GROUP}) @binding(100)
var<uniform> my_extended_material: MyExtendedMaterial;
@fragment

View File

@ -42,19 +42,19 @@ struct ExampleBindlessExtendedMaterial {
// 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:
@group(#{MATERIAL_BIND_GROUP}) @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:
@group(#{MATERIAL_BIND_GROUP}) @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;
@group(#{MATERIAL_BIND_GROUP}) @binding(50) var<uniform> example_extended_material: ExampleBindlessExtendedMaterial;
@group(#{MATERIAL_BIND_GROUP}) @binding(51) var modulate_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(52) var modulate_sampler: sampler;
#endif // BINDLESS

View File

@ -1,22 +1,22 @@
#import bevy_pbr::forward_io::VertexOutput
@group(3) @binding(0) var test_texture_1d: texture_1d<f32>;
@group(3) @binding(1) var test_texture_1d_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var test_texture_1d: texture_1d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var test_texture_1d_sampler: sampler;
@group(3) @binding(2) var test_texture_2d: texture_2d<f32>;
@group(3) @binding(3) var test_texture_2d_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var test_texture_2d: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(3) var test_texture_2d_sampler: sampler;
@group(3) @binding(4) var test_texture_2d_array: texture_2d_array<f32>;
@group(3) @binding(5) var test_texture_2d_array_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(4) var test_texture_2d_array: texture_2d_array<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(5) var test_texture_2d_array_sampler: sampler;
@group(3) @binding(6) var test_texture_cube: texture_cube<f32>;
@group(3) @binding(7) var test_texture_cube_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(6) var test_texture_cube: texture_cube<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(7) var test_texture_cube_sampler: sampler;
@group(3) @binding(8) var test_texture_cube_array: texture_cube_array<f32>;
@group(3) @binding(9) var test_texture_cube_array_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(8) var test_texture_cube_array: texture_cube_array<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(9) var test_texture_cube_array_sampler: sampler;
@group(3) @binding(10) var test_texture_3d: texture_3d<f32>;
@group(3) @binding(11) var test_texture_3d_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(10) var test_texture_3d: texture_3d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(11) var test_texture_3d_sampler: sampler;
@fragment
fn fragment(in: VertexOutput) {}

View File

@ -12,7 +12,7 @@ struct VoxelVisualizationIrradianceVolumeInfo {
intensity: f32,
}
@group(3) @binding(100)
@group(#{MATERIAL_BIND_GROUP}) @binding(100)
var<uniform> irradiance_volume_info: VoxelVisualizationIrradianceVolumeInfo;
@fragment

View File

@ -4,7 +4,7 @@ struct LineMaterial {
color: vec4<f32>,
};
@group(3) @binding(0) var<uniform> material: LineMaterial;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> material: LineMaterial;
@fragment
fn fragment(

View File

@ -1,7 +1,7 @@
#import bevy_pbr::forward_io::VertexOutput
@group(3) @binding(0) var material_color_texture: texture_2d<f32>;
@group(3) @binding(1) var material_color_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var material_color_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var material_color_sampler: sampler;
@fragment
fn fragment(

View File

@ -4,7 +4,7 @@ struct CustomMaterial {
color: vec4<f32>,
};
@group(3) @binding(0) var<uniform> material: CustomMaterial;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> material: CustomMaterial;
@fragment
fn fragment(

View File

@ -11,7 +11,7 @@ struct ShowPrepassSettings {
padding_1: u32,
padding_2: u32,
}
@group(3) @binding(0) var<uniform> settings: ShowPrepassSettings;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> settings: ShowPrepassSettings;
@fragment
fn fragment(

View File

@ -3,7 +3,7 @@
view_transformations::position_world_to_clip
}
@group(3) @binding(0) var<storage, read> colors: array<vec4<f32>, 5>;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<storage, read> colors: array<vec4<f32>, 5>;
struct Vertex {
@builtin(instance_index) instance_index: u32,

View File

@ -1,7 +1,7 @@
#import bevy_pbr::forward_io::VertexOutput
@group(3) @binding(0) var textures: binding_array<texture_2d<f32>>;
@group(3) @binding(1) var nearest_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var textures: binding_array<texture_2d<f32>>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var nearest_sampler: sampler;
// We can also have array of samplers
// var samplers: binding_array<sampler>;

View File

@ -23,9 +23,9 @@ struct WaterSettings {
@group(0) @binding(1) var<uniform> globals: Globals;
@group(3) @binding(100) var water_normals_texture: texture_2d<f32>;
@group(3) @binding(101) var water_normals_sampler: sampler;
@group(3) @binding(102) var<uniform> water_settings: WaterSettings;
@group(#{MATERIAL_BIND_GROUP}) @binding(100) var water_normals_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(101) var water_normals_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(102) var<uniform> water_settings: WaterSettings;
// Samples a single octave of noise and returns the resulting normal.
fn sample_noise_octave(uv: vec2<f32>, strength: f32) -> vec3<f32> {

View File

@ -10,7 +10,7 @@
}
#import bevy_render::maths::project_onto
@group(3) @binding(200)
@group(#{MATERIAL_BIND_GROUP}) @binding(200)
var<uniform> inv_depth_fade_factor: f32;
struct ForwardDecalInformation {

View File

@ -58,6 +58,8 @@ use core::{hash::Hash, marker::PhantomData};
use smallvec::SmallVec;
use tracing::error;
pub const MATERIAL_BIND_GROUP_INDEX: usize = 3;
/// Materials are used alongside [`MaterialPlugin`], [`Mesh3d`], and [`MeshMaterial3d`]
/// to spawn entities that are rendered with a specific [`Material`] type. They serve as an easy to use high level
/// way to render [`Mesh3d`] entities with custom shader logic.
@ -451,6 +453,16 @@ impl SpecializedMeshPipeline for MaterialPipelineSpecializer {
.pipeline
.mesh_pipeline
.specialize(key.mesh_key, layout)?;
descriptor.vertex.shader_defs.push(ShaderDefVal::UInt(
"MATERIAL_BIND_GROUP".into(),
MATERIAL_BIND_GROUP_INDEX as u32,
));
if let Some(ref mut fragment) = descriptor.fragment {
fragment.shader_defs.push(ShaderDefVal::UInt(
"MATERIAL_BIND_GROUP".into(),
MATERIAL_BIND_GROUP_INDEX as u32,
));
};
if let Some(vertex_shader) = self.properties.get_shader(MaterialVertexShader) {
descriptor.vertex.shader = vertex_shader.clone();
}
@ -490,7 +502,7 @@ pub type DrawMaterial = (
SetMeshViewBindGroup<0>,
SetMeshViewBindingArrayBindGroup<1>,
SetMeshBindGroup<2>,
SetMaterialBindGroup<3>,
SetMaterialBindGroup<MATERIAL_BIND_GROUP_INDEX>,
DrawMesh,
);

View File

@ -37,53 +37,53 @@ struct StandardMaterialBindings {
specular_tint_sampler: u32, // 30
}
@group(3) @binding(0) var<storage> material_indices: array<StandardMaterialBindings>;
@group(3) @binding(10) var<storage> material_array: array<StandardMaterial>;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<storage> material_indices: array<StandardMaterialBindings>;
@group(#{MATERIAL_BIND_GROUP}) @binding(10) var<storage> material_array: array<StandardMaterial>;
#else // BINDLESS
@group(3) @binding(0) var<uniform> material: StandardMaterial;
@group(3) @binding(1) var base_color_texture: texture_2d<f32>;
@group(3) @binding(2) var base_color_sampler: sampler;
@group(3) @binding(3) var emissive_texture: texture_2d<f32>;
@group(3) @binding(4) var emissive_sampler: sampler;
@group(3) @binding(5) var metallic_roughness_texture: texture_2d<f32>;
@group(3) @binding(6) var metallic_roughness_sampler: sampler;
@group(3) @binding(7) var occlusion_texture: texture_2d<f32>;
@group(3) @binding(8) var occlusion_sampler: sampler;
@group(3) @binding(9) var normal_map_texture: texture_2d<f32>;
@group(3) @binding(10) var normal_map_sampler: sampler;
@group(3) @binding(11) var depth_map_texture: texture_2d<f32>;
@group(3) @binding(12) var depth_map_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> material: StandardMaterial;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var base_color_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var base_color_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(3) var emissive_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(4) var emissive_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(5) var metallic_roughness_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(6) var metallic_roughness_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(7) var occlusion_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(8) var occlusion_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(9) var normal_map_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(10) var normal_map_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(11) var depth_map_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(12) var depth_map_sampler: sampler;
#ifdef PBR_ANISOTROPY_TEXTURE_SUPPORTED
@group(3) @binding(13) var anisotropy_texture: texture_2d<f32>;
@group(3) @binding(14) var anisotropy_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(13) var anisotropy_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(14) var anisotropy_sampler: sampler;
#endif // PBR_ANISOTROPY_TEXTURE_SUPPORTED
#ifdef PBR_TRANSMISSION_TEXTURES_SUPPORTED
@group(3) @binding(15) var specular_transmission_texture: texture_2d<f32>;
@group(3) @binding(16) var specular_transmission_sampler: sampler;
@group(3) @binding(17) var thickness_texture: texture_2d<f32>;
@group(3) @binding(18) var thickness_sampler: sampler;
@group(3) @binding(19) var diffuse_transmission_texture: texture_2d<f32>;
@group(3) @binding(20) var diffuse_transmission_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(15) var specular_transmission_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(16) var specular_transmission_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(17) var thickness_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(18) var thickness_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(19) var diffuse_transmission_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(20) var diffuse_transmission_sampler: sampler;
#endif // PBR_TRANSMISSION_TEXTURES_SUPPORTED
#ifdef PBR_MULTI_LAYER_MATERIAL_TEXTURES_SUPPORTED
@group(3) @binding(21) var clearcoat_texture: texture_2d<f32>;
@group(3) @binding(22) var clearcoat_sampler: sampler;
@group(3) @binding(23) var clearcoat_roughness_texture: texture_2d<f32>;
@group(3) @binding(24) var clearcoat_roughness_sampler: sampler;
@group(3) @binding(25) var clearcoat_normal_texture: texture_2d<f32>;
@group(3) @binding(26) var clearcoat_normal_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(21) var clearcoat_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(22) var clearcoat_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(23) var clearcoat_roughness_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(24) var clearcoat_roughness_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(25) var clearcoat_normal_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(26) var clearcoat_normal_sampler: sampler;
#endif // PBR_MULTI_LAYER_MATERIAL_TEXTURES_SUPPORTED
#ifdef PBR_SPECULAR_TEXTURES_SUPPORTED
@group(3) @binding(27) var specular_texture: texture_2d<f32>;
@group(3) @binding(28) var specular_sampler: sampler;
@group(3) @binding(29) var specular_tint_texture: texture_2d<f32>;
@group(3) @binding(30) var specular_tint_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(27) var specular_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(28) var specular_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(29) var specular_tint_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(30) var specular_tint_sampler: sampler;
#endif // PBR_SPECULAR_TEXTURES_SUPPORTED
#endif // BINDLESS

View File

@ -16,22 +16,22 @@
// Binding 0 is the bindless index table.
// Filtering samplers.
@group(3) @binding(1) var bindless_samplers_filtering: binding_array<sampler>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var bindless_samplers_filtering: binding_array<sampler>;
// Non-filtering samplers (nearest neighbor).
@group(3) @binding(2) var bindless_samplers_non_filtering: binding_array<sampler>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var bindless_samplers_non_filtering: binding_array<sampler>;
// Comparison samplers (typically for shadow mapping).
@group(3) @binding(3) var bindless_samplers_comparison: binding_array<sampler>;
@group(#{MATERIAL_BIND_GROUP}) @binding(3) var bindless_samplers_comparison: binding_array<sampler>;
// 1D textures.
@group(3) @binding(4) var bindless_textures_1d: binding_array<texture_1d<f32>>;
@group(#{MATERIAL_BIND_GROUP}) @binding(4) var bindless_textures_1d: binding_array<texture_1d<f32>>;
// 2D textures.
@group(3) @binding(5) var bindless_textures_2d: binding_array<texture_2d<f32>>;
@group(#{MATERIAL_BIND_GROUP}) @binding(5) var bindless_textures_2d: binding_array<texture_2d<f32>>;
// 2D array textures.
@group(3) @binding(6) var bindless_textures_2d_array: binding_array<texture_2d_array<f32>>;
@group(#{MATERIAL_BIND_GROUP}) @binding(6) var bindless_textures_2d_array: binding_array<texture_2d_array<f32>>;
// 3D textures.
@group(3) @binding(7) var bindless_textures_3d: binding_array<texture_3d<f32>>;
@group(#{MATERIAL_BIND_GROUP}) @binding(7) var bindless_textures_3d: binding_array<texture_3d<f32>>;
// Cubemap textures.
@group(3) @binding(8) var bindless_textures_cube: binding_array<texture_cube<f32>>;
@group(#{MATERIAL_BIND_GROUP}) @binding(8) var bindless_textures_cube: binding_array<texture_cube<f32>>;
// Cubemap array textures.
@group(3) @binding(9) var bindless_textures_cube_array: binding_array<texture_cube_array<f32>>;
@group(#{MATERIAL_BIND_GROUP}) @binding(9) var bindless_textures_cube_array: binding_array<texture_cube_array<f32>>;
#endif // BINDLESS

View File

@ -21,9 +21,9 @@ const COLOR_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u; // (0u32
const COLOR_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 1073741824u; // (1u32 << 30)
const COLOR_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 2147483648u; // (2u32 << 30)
@group(2) @binding(0) var<uniform> material: ColorMaterial;
@group(2) @binding(1) var texture: texture_2d<f32>;
@group(2) @binding(2) var texture_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var<uniform> material: ColorMaterial;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var texture_sampler: sampler;
@fragment
fn fragment(

View File

@ -24,7 +24,7 @@ use bevy_platform::collections::HashMap;
use bevy_reflect::{prelude::ReflectDefault, Reflect};
use bevy_render::camera::extract_cameras;
use bevy_render::render_phase::{DrawFunctionId, InputUniformIndex};
use bevy_render::render_resource::CachedRenderPipelineId;
use bevy_render::render_resource::{CachedRenderPipelineId, ShaderDefVal};
use bevy_render::view::RenderVisibleEntities;
use bevy_render::RenderStartup;
use bevy_render::{
@ -52,6 +52,8 @@ use core::{hash::Hash, marker::PhantomData};
use derive_more::derive::From;
use tracing::error;
pub const MATERIAL_2D_BIND_GROUP_INDEX: usize = 2;
/// Materials are used alongside [`Material2dPlugin`], [`Mesh2d`], and [`MeshMaterial2d`]
/// to spawn entities that are rendered with a specific [`Material2d`] type. They serve as an easy to use high level
/// way to render [`Mesh2d`] entities with custom shader logic.
@ -445,6 +447,16 @@ where
layout: &MeshVertexBufferLayoutRef,
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
let mut descriptor = self.mesh2d_pipeline.specialize(key.mesh_key, layout)?;
descriptor.vertex.shader_defs.push(ShaderDefVal::UInt(
"MATERIAL_BIND_GROUP".into(),
MATERIAL_2D_BIND_GROUP_INDEX as u32,
));
if let Some(ref mut fragment) = descriptor.fragment {
fragment.shader_defs.push(ShaderDefVal::UInt(
"MATERIAL_BIND_GROUP".into(),
MATERIAL_2D_BIND_GROUP_INDEX as u32,
));
}
if let Some(vertex_shader) = &self.vertex_shader {
descriptor.vertex.shader = vertex_shader.clone();
}

View File

@ -16,9 +16,9 @@ struct VertexOutput {
@location(1) tile_index: u32,
}
@group(2) @binding(0) var tileset: texture_2d_array<f32>;
@group(2) @binding(1) var tileset_sampler: sampler;
@group(2) @binding(2) var tile_indices: texture_2d<u32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(0) var tileset: texture_2d_array<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(1) var tileset_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(2) var tile_indices: texture_2d<u32>;
@vertex
fn vertex(vertex: Vertex) -> VertexOutput {

View File

@ -14,7 +14,8 @@ following changes to the default bind group numbering have been made in 3d:
- `@group(3)` material binding resources
Most users who are not using mid-level render APIs will simply need to switch their material bind groups
from `@group(2)` to `@group(3)`.
from `@group(2)` to `@group(#{MATERIAL_BIND_GROUP})`. The `MATERIAL_BIND_GROUP` shader def has been added
to ensure backwards compatibility in the event the bind group numbering changes again in the future.
Exported float constants from shaders without an explicit type declaration like `const FOO = 1.0;` are no
longer supported and must be explicitly typed like `const FOO: f32 = 1.0;`.