Skip to content

Commit ca25a67

Browse files
authored
Fix the extended_material example on WebGL2 (#18812)
# Objective - Fixes #13872 (also mentioned in #17167) ## Solution - Added conditional padding fields to the shader uniform ## Alternatives ### 1- Use a UVec4 Replace the `u32` field in `MyExtension` by a `UVec4` and only use the `x` coordinate. (This was the original approach, but for consistency with the rest of the codebase, separate padding fields seem to be preferred) ### 2- Don't fix it, unlist it While the fix is quite simple, it does muddy the waters a tiny bit due to `quantize_steps` now being a UVec4 instead of a simple u32. We could simply remove this example from the examples that support WebGL2. ## Testing - Ran the example locally on WebGL2 (and native Vulkan) successfully
1 parent 0f75142 commit ca25a67

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

assets/shaders/extended_material.wgsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
struct MyExtendedMaterial {
1919
quantize_steps: u32,
20+
#ifdef SIXTEEN_BYTE_ALIGNMENT
21+
// Web examples WebGL2 support: structs must be 16 byte aligned.
22+
_webgl2_padding_8b: u32,
23+
_webgl2_padding_12b: u32,
24+
_webgl2_padding_16b: u32,
25+
#endif
2026
}
2127

2228
@group(3) @binding(100)

examples/shader/extended_material.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn setup(
4141
// change the above to `OpaqueRendererMethod::Deferred` or add the `DefaultOpaqueRendererMethod` resource.
4242
..Default::default()
4343
},
44-
extension: MyExtension { quantize_steps: 3 },
44+
extension: MyExtension::new(1),
4545
})),
4646
Transform::from_xyz(0.0, 0.5, 0.0),
4747
));
@@ -69,12 +69,30 @@ fn rotate_things(mut q: Query<&mut Transform, With<Rotate>>, time: Res<Time>) {
6969
}
7070
}
7171

72-
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
72+
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone, Default)]
7373
struct MyExtension {
7474
// We need to ensure that the bindings of the base material and the extension do not conflict,
7575
// so we start from binding slot 100, leaving slots 0-99 for the base material.
7676
#[uniform(100)]
7777
quantize_steps: u32,
78+
// Web examples WebGL2 support: structs must be 16 byte aligned.
79+
#[cfg(feature = "webgl2")]
80+
#[uniform(100)]
81+
_webgl2_padding_8b: u32,
82+
#[cfg(feature = "webgl2")]
83+
#[uniform(100)]
84+
_webgl2_padding_12b: u32,
85+
#[cfg(feature = "webgl2")]
86+
#[uniform(100)]
87+
_webgl2_padding_16b: u32,
88+
}
89+
impl MyExtension {
90+
fn new(quantize_steps: u32) -> Self {
91+
Self {
92+
quantize_steps,
93+
..default()
94+
}
95+
}
7896
}
7997

8098
impl MaterialExtension for MyExtension {

0 commit comments

Comments
 (0)