Skip to content

Commit b1f0615

Browse files
Waridleymockersf
authored andcommitted
Pad SkyUniforms to 16 bytes for WASM (#12078)
# Objective Fixes Skyboxes on WebGL, which broke in Bevy 0.13 due to the addition of the `brightness` uniform, when previously the skybox pipeline only had view and global uniforms. ```ignore panicked at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.19.1/src/backend/wgpu_core.rs:3009:5: wgpu error: Validation Error Caused by: In Device::create_render_pipeline note: label = `skybox_pipeline` In the provided shader, the type given for group 0 binding 3 has a size of 4. As the device does not support `DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED`, the type must have a size that is a multiple of 16 bytes. ``` It would be nice if this could be backported to a 0.13.1 patch as well if possible. I'm needing to rely on my own fork for now. ## Solution Similar to the Globals uniform solution here: https://github.com/bevyengine/bevy/blob/d31de3f1398080661a83a04dcbdd31a7ee9fa76e/crates/bevy_render/src/globals.rs#L59-L60 I've added 3 conditional fields to `SkyboxUniforms`.
1 parent 784b9bd commit b1f0615

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

crates/bevy_core_pipeline/src/skybox/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ impl ExtractComponent for Skybox {
9393
skybox.clone(),
9494
SkyboxUniforms {
9595
brightness: skybox.brightness * exposure,
96+
#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
97+
_wasm_padding_8b: 0,
98+
#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
99+
_wasm_padding_12b: 0,
100+
#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
101+
_wasm_padding_16b: 0,
96102
},
97103
))
98104
}
@@ -102,6 +108,12 @@ impl ExtractComponent for Skybox {
102108
#[derive(Component, ShaderType, Clone)]
103109
pub struct SkyboxUniforms {
104110
brightness: f32,
111+
#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
112+
_wasm_padding_8b: u32,
113+
#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
114+
_wasm_padding_12b: u32,
115+
#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
116+
_wasm_padding_16b: u32,
105117
}
106118

107119
#[derive(Resource)]

crates/bevy_core_pipeline/src/skybox/skybox.wgsl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
#import bevy_render::view::View
22
#import bevy_pbr::utils::coords_to_viewport_uv
33

4+
struct SkyboxUniforms {
5+
brightness: f32,
6+
#ifdef SIXTEEN_BYTE_ALIGNMENT
7+
_wasm_padding_8b: u32,
8+
_wasm_padding_12b: u32,
9+
_wasm_padding_16b: u32,
10+
#endif
11+
}
12+
413
@group(0) @binding(0) var skybox: texture_cube<f32>;
514
@group(0) @binding(1) var skybox_sampler: sampler;
615
@group(0) @binding(2) var<uniform> view: View;
7-
@group(0) @binding(3) var<uniform> brightness: f32;
16+
@group(0) @binding(3) var<uniform> uniforms: SkyboxUniforms;
817

918
fn coords_to_ray_direction(position: vec2<f32>, viewport: vec4<f32>) -> vec3<f32> {
1019
// Using world positions of the fragment and camera to calculate a ray direction
@@ -63,5 +72,5 @@ fn skybox_fragment(in: VertexOutput) -> @location(0) vec4<f32> {
6372
let ray_direction = coords_to_ray_direction(in.position.xy, view.viewport);
6473

6574
// Cube maps are left-handed so we negate the z coordinate.
66-
return textureSample(skybox, skybox_sampler, ray_direction * vec3(1.0, 1.0, -1.0)) * brightness;
75+
return textureSample(skybox, skybox_sampler, ray_direction * vec3(1.0, 1.0, -1.0)) * uniforms.brightness;
6776
}

0 commit comments

Comments
 (0)