Skip to content

Commit 1db0214

Browse files
gagnusmockersf
authored andcommitted
Added feature switch to default Standard Material's new anisotropy texture to off (#14048)
# Objective - Standard Material is starting to run out of samplers (currently uses 13 with no additional features off, I think in 0.13 it was 12). - This change adds a new feature switch, modelled on the other ones which add features to Standard Material, to turn off the new anisotropy feature by default. ## Solution - feature + texture define ## Testing - Anisotropy example still works fine - Other samples work fine - Standard Material now takes 12 samplers by default on my Mac instead of 13 ## Migration Guide - Add feature pbr_anisotropy_texture if you are using that texture in any standard materials. --------- Co-authored-by: John Payne <[email protected]>
1 parent 309c224 commit 1db0214

File tree

10 files changed

+59
-21
lines changed

10 files changed

+59
-21
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ pbr_multi_layer_material_textures = [
312312
"bevy_internal/pbr_multi_layer_material_textures",
313313
]
314314

315+
# Enable support for anisotropy texture in the `StandardMaterial`, at the risk of blowing past the global, per-shader texture limit on older/lower-end GPUs
316+
pbr_anisotropy_texture = ["bevy_internal/pbr_anisotropy_texture"]
317+
315318
# Enable some limitations to be able to use WebGL2. Please refer to the [WebGL2 and WebGPU](https://github.com/bevyengine/bevy/tree/latest/examples#webgl2-and-webgpu) section of the examples README for more information on how to run Wasm builds with WebGPU.
316319
webgl2 = ["bevy_internal/webgl"]
317320

@@ -3211,7 +3214,7 @@ wasm = true
32113214
name = "anisotropy"
32123215
path = "examples/3d/anisotropy.rs"
32133216
doc-scrape-examples = true
3214-
required-features = ["jpeg"]
3217+
required-features = ["jpeg", "pbr_anisotropy_texture"]
32153218

32163219
[package.metadata.example.anisotropy]
32173220
name = "Anisotropy"

crates/bevy_gltf/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ keywords = ["bevy"]
1212
dds = ["bevy_render/dds"]
1313
pbr_transmission_textures = ["bevy_pbr/pbr_transmission_textures"]
1414
pbr_multi_layer_material_textures = []
15+
pbr_anisotropy_texture = []
1516

1617
[dependencies]
1718
# bevy

crates/bevy_gltf/src/loader.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,9 @@ fn load_material(
10861086
clearcoat_normal_texture: clearcoat.clearcoat_normal_texture,
10871087
anisotropy_strength: anisotropy.anisotropy_strength.unwrap_or_default() as f32,
10881088
anisotropy_rotation: anisotropy.anisotropy_rotation.unwrap_or_default() as f32,
1089+
#[cfg(feature = "pbr_anisotropy_texture")]
10891090
anisotropy_channel: anisotropy.anisotropy_channel,
1091+
#[cfg(feature = "pbr_anisotropy_texture")]
10901092
anisotropy_texture: anisotropy.anisotropy_texture,
10911093
..Default::default()
10921094
}
@@ -1898,11 +1900,14 @@ impl ClearcoatExtension {
18981900
struct AnisotropyExtension {
18991901
anisotropy_strength: Option<f64>,
19001902
anisotropy_rotation: Option<f64>,
1903+
#[cfg(feature = "pbr_anisotropy_texture")]
19011904
anisotropy_channel: UvChannel,
1905+
#[cfg(feature = "pbr_anisotropy_texture")]
19021906
anisotropy_texture: Option<Handle<Image>>,
19031907
}
19041908

19051909
impl AnisotropyExtension {
1910+
#[allow(unused_variables)]
19061911
fn parse(
19071912
load_context: &mut LoadContext,
19081913
document: &Document,
@@ -1913,6 +1918,7 @@ impl AnisotropyExtension {
19131918
.get("KHR_materials_anisotropy")?
19141919
.as_object()?;
19151920

1921+
#[cfg(feature = "pbr_anisotropy_texture")]
19161922
let (anisotropy_channel, anisotropy_texture) = extension
19171923
.get("anisotropyTexture")
19181924
.and_then(|value| value::from_value::<json::texture::Info>(value.clone()).ok())
@@ -1927,7 +1933,9 @@ impl AnisotropyExtension {
19271933
Some(AnisotropyExtension {
19281934
anisotropy_strength: extension.get("anisotropyStrength").and_then(Value::as_f64),
19291935
anisotropy_rotation: extension.get("anisotropyRotation").and_then(Value::as_f64),
1936+
#[cfg(feature = "pbr_anisotropy_texture")]
19301937
anisotropy_channel: anisotropy_channel.unwrap_or_default(),
1938+
#[cfg(feature = "pbr_anisotropy_texture")]
19311939
anisotropy_texture,
19321940
})
19331941
}

crates/bevy_internal/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ pbr_multi_layer_material_textures = [
111111
"bevy_gltf?/pbr_multi_layer_material_textures",
112112
]
113113

114+
# Anisotropy texture in `StandardMaterial`:
115+
pbr_anisotropy_texture = [
116+
"bevy_pbr?/pbr_anisotropy_texture",
117+
"bevy_gltf?/pbr_anisotropy_texture",
118+
]
119+
114120
# Optimise for WebGL2
115121
webgl = [
116122
"bevy_core_pipeline?/webgl",

crates/bevy_pbr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ webgl = []
1313
webgpu = []
1414
pbr_transmission_textures = []
1515
pbr_multi_layer_material_textures = []
16+
pbr_anisotropy_texture = []
1617
shader_format_glsl = ["bevy_render/shader_format_glsl"]
1718
trace = ["bevy_render/trace"]
1819
ios_simulator = ["bevy_render/ios_simulator"]

crates/bevy_pbr/src/pbr_material.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ pub struct StandardMaterial {
215215
///
216216
/// **Important:** The [`StandardMaterial::diffuse_transmission`] property must be set to a value higher than 0.0,
217217
/// or this texture won't have any effect.
218-
#[texture(19)]
219-
#[sampler(20)]
218+
#[cfg_attr(feature = "pbr_transmission_textures", texture(19))]
219+
#[cfg_attr(feature = "pbr_transmission_textures", sampler(20))]
220220
#[cfg(feature = "pbr_transmission_textures")]
221221
pub diffuse_transmission_texture: Option<Handle<Image>>,
222222

@@ -256,8 +256,8 @@ pub struct StandardMaterial {
256256
///
257257
/// **Important:** The [`StandardMaterial::specular_transmission`] property must be set to a value higher than 0.0,
258258
/// or this texture won't have any effect.
259-
#[texture(15)]
260-
#[sampler(16)]
259+
#[cfg_attr(feature = "pbr_transmission_textures", texture(15))]
260+
#[cfg_attr(feature = "pbr_transmission_textures", sampler(16))]
261261
#[cfg(feature = "pbr_transmission_textures")]
262262
pub specular_transmission_texture: Option<Handle<Image>>,
263263

@@ -285,8 +285,8 @@ pub struct StandardMaterial {
285285
///
286286
/// **Important:** The [`StandardMaterial::thickness`] property must be set to a value higher than 0.0,
287287
/// or this texture won't have any effect.
288-
#[texture(17)]
289-
#[sampler(18)]
288+
#[cfg_attr(feature = "pbr_transmission_textures", texture(17))]
289+
#[cfg_attr(feature = "pbr_transmission_textures", sampler(18))]
290290
#[cfg(feature = "pbr_transmission_textures")]
291291
pub thickness_texture: Option<Handle<Image>>,
292292

@@ -420,8 +420,8 @@ pub struct StandardMaterial {
420420
/// main [`StandardMaterial::clearcoat`] factor.
421421
///
422422
/// As this is a non-color map, it must not be loaded as sRGB.
423-
#[texture(21)]
424-
#[sampler(22)]
423+
#[cfg_attr(feature = "pbr_multi_layer_material_textures", texture(21))]
424+
#[cfg_attr(feature = "pbr_multi_layer_material_textures", sampler(22))]
425425
#[cfg(feature = "pbr_multi_layer_material_textures")]
426426
pub clearcoat_texture: Option<Handle<Image>>,
427427

@@ -445,8 +445,8 @@ pub struct StandardMaterial {
445445
/// [`StandardMaterial::clearcoat_perceptual_roughness`] factor.
446446
///
447447
/// As this is a non-color map, it must not be loaded as sRGB.
448-
#[texture(23)]
449-
#[sampler(24)]
448+
#[cfg_attr(feature = "pbr_multi_layer_material_textures", texture(23))]
449+
#[cfg_attr(feature = "pbr_multi_layer_material_textures", sampler(24))]
450450
#[cfg(feature = "pbr_multi_layer_material_textures")]
451451
pub clearcoat_roughness_texture: Option<Handle<Image>>,
452452

@@ -467,8 +467,8 @@ pub struct StandardMaterial {
467467
/// in both [`StandardMaterial::normal_map_texture`] and this field.
468468
///
469469
/// As this is a non-color map, it must not be loaded as sRGB.
470-
#[texture(25)]
471-
#[sampler(26)]
470+
#[cfg_attr(feature = "pbr_multi_layer_material_textures", texture(25))]
471+
#[cfg_attr(feature = "pbr_multi_layer_material_textures", sampler(26))]
472472
#[cfg(feature = "pbr_multi_layer_material_textures")]
473473
pub clearcoat_normal_texture: Option<Handle<Image>>,
474474

@@ -513,6 +513,7 @@ pub struct StandardMaterial {
513513
/// The UV channel to use for the [`StandardMaterial::anisotropy_texture`].
514514
///
515515
/// Defaults to [`UvChannel::Uv0`].
516+
#[cfg(feature = "pbr_anisotropy_texture")]
516517
pub anisotropy_channel: UvChannel,
517518

518519
/// An image texture that allows the
@@ -536,8 +537,9 @@ pub struct StandardMaterial {
536537
///
537538
/// [`KHR_materials_anisotropy` specification]:
538539
/// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_anisotropy/README.md
539-
#[texture(13)]
540-
#[sampler(14)]
540+
#[cfg_attr(feature = "pbr_anisotropy_texture", texture(13))]
541+
#[cfg_attr(feature = "pbr_anisotropy_texture", sampler(14))]
542+
#[cfg(feature = "pbr_anisotropy_texture")]
541543
pub anisotropy_texture: Option<Handle<Image>>,
542544

543545
/// Support two-sided lighting by automatically flipping the normals for "back" faces
@@ -814,7 +816,9 @@ impl Default for StandardMaterial {
814816
clearcoat_normal_texture: None,
815817
anisotropy_strength: 0.0,
816818
anisotropy_rotation: 0.0,
819+
#[cfg(feature = "pbr_anisotropy_texture")]
817820
anisotropy_channel: UvChannel::Uv0,
821+
#[cfg(feature = "pbr_anisotropy_texture")]
818822
anisotropy_texture: None,
819823
flip_normal_map_y: false,
820824
double_sided: false,
@@ -999,8 +1003,11 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
9991003
}
10001004
}
10011005

1002-
if self.anisotropy_texture.is_some() {
1003-
flags |= StandardMaterialFlags::ANISOTROPY_TEXTURE;
1006+
#[cfg(feature = "pbr_anisotropy_texture")]
1007+
{
1008+
if self.anisotropy_texture.is_some() {
1009+
flags |= StandardMaterialFlags::ANISOTROPY_TEXTURE;
1010+
}
10041011
}
10051012

10061013
#[cfg(feature = "pbr_multi_layer_material_textures")]
@@ -1207,10 +1214,14 @@ impl From<&StandardMaterial> for StandardMaterialKey {
12071214
StandardMaterialKey::NORMAL_MAP_UV,
12081215
material.normal_map_channel != UvChannel::Uv0,
12091216
);
1210-
key.set(
1211-
StandardMaterialKey::ANISOTROPY_UV,
1212-
material.anisotropy_channel != UvChannel::Uv0,
1213-
);
1217+
1218+
#[cfg(feature = "pbr_anisotropy_texture")]
1219+
{
1220+
key.set(
1221+
StandardMaterialKey::ANISOTROPY_UV,
1222+
material.anisotropy_channel != UvChannel::Uv0,
1223+
);
1224+
}
12141225

12151226
#[cfg(feature = "pbr_multi_layer_material_textures")]
12161227
{

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,9 @@ impl SpecializedMeshPipeline for MeshPipeline {
16421642
if cfg!(feature = "pbr_multi_layer_material_textures") {
16431643
shader_defs.push("PBR_MULTI_LAYER_MATERIAL_TEXTURES_SUPPORTED".into());
16441644
}
1645+
if cfg!(feature = "pbr_anisotropy_texture") {
1646+
shader_defs.push("PBR_ANISOTROPY_TEXTURE_SUPPORTED".into());
1647+
}
16451648

16461649
let mut bind_group_layout = vec![self.get_view_layout(key.into()).clone()];
16471650

crates/bevy_pbr/src/render/pbr_bindings.wgsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
@group(2) @binding(10) var normal_map_sampler: sampler;
1616
@group(2) @binding(11) var depth_map_texture: texture_2d<f32>;
1717
@group(2) @binding(12) var depth_map_sampler: sampler;
18+
#ifdef PBR_ANISOTROPY_TEXTURE_SUPPORTED
1819
@group(2) @binding(13) var anisotropy_texture: texture_2d<f32>;
1920
@group(2) @binding(14) var anisotropy_sampler: sampler;
21+
#endif
2022
#ifdef PBR_TRANSMISSION_TEXTURES_SUPPORTED
2123
@group(2) @binding(15) var specular_transmission_texture: texture_2d<f32>;
2224
@group(2) @binding(16) var specular_transmission_sampler: sampler;

crates/bevy_pbr/src/render/pbr_fragment.wgsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ fn pbr_input_from_standard_material(
420420
//
421421
// This code comes from the `KHR_materials_anisotropy` spec:
422422
// <https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_anisotropy/README.md#individual-lights>
423+
#ifdef PBR_ANISOTROPY_TEXTURE_SUPPORTED
423424
#ifdef VERTEX_TANGENTS
424425
#ifdef STANDARD_MATERIAL_ANISOTROPY
425426

@@ -456,6 +457,7 @@ fn pbr_input_from_standard_material(
456457

457458
#endif // STANDARD_MATERIAL_ANISOTROPY
458459
#endif // VERTEX_TANGENTS
460+
#endif // PBR_ANISOTROPY_TEXTURE_SUPPORTED
459461

460462
#endif // LOAD_PREPASS_NORMALS
461463

docs/cargo_features.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ The default feature set enables most of the expected features of a game engine,
7070
|meshlet_processor|Enables processing meshes into meshlet meshes for bevy_pbr|
7171
|minimp3|MP3 audio format support (through minimp3)|
7272
|mp3|MP3 audio format support|
73+
|pbr_anisotropy_texture|Enable support for anisotropy texture in the `StandardMaterial`, at the risk of blowing past the global, per-shader texture limit on older/lower-end GPUs|
7374
|pbr_multi_layer_material_textures|Enable support for multi-layer material textures in the `StandardMaterial`, at the risk of blowing past the global, per-shader texture limit on older/lower-end GPUs|
7475
|pbr_transmission_textures|Enable support for transmission-related textures in the `StandardMaterial`, at the risk of blowing past the global, per-shader texture limit on older/lower-end GPUs|
7576
|pnm|PNM image format support, includes pam, pbm, pgm and ppm|

0 commit comments

Comments
 (0)