Skip to content

Commit 3945a6d

Browse files
authored
Fix wesl in wasm and webgl2 (#18591)
# Objective - feature `shader_format_wesl` doesn't compile in Wasm - once fixed, example `shader_material_wesl` doesn't work in WebGL2 ## Solution - remove special path handling when loading shaders. this seems like a way to escape the asset folder which we don't want to allow, and can't compile on android or wasm, and can't work on iOS (filesystem is rooted there) - pad material so that it's 16 bits. I couldn't get conditional compilation to work in wesl for type declaration, it fails to parse - the shader renders the color `(0.0, 0.0, 0.0, 0.0)` when it's not a polka dot. this renders as black on WebGPU/metal/..., and white on WebGL2. change it to `(0.0, 0.0, 0.0, 1.0)` so that it's black everywhere
1 parent 4a31d8e commit 3945a6d

File tree

4 files changed

+11
-27
lines changed

4 files changed

+11
-27
lines changed

assets/shaders/custom_material.wesl

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ struct VertexOutput {
66
}
77

88
struct CustomMaterial {
9-
time: f32,
9+
// Needed for 16-bit alignment on WebGL2
10+
time: vec4<f32>,
1011
}
1112

1213
@group(2) @binding(0) var<uniform> material: CustomMaterial;
@@ -15,5 +16,5 @@ struct CustomMaterial {
1516
fn fragment(
1617
mesh: VertexOutput,
1718
) -> @location(0) vec4<f32> {
18-
return make_polka_dots(mesh.uv, material.time);
19-
}
19+
return make_polka_dots(mesh.uv, material.time.x);
20+
}

assets/shaders/util.wesl

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ fn make_polka_dots(pos: vec2<f32>, time: f32) -> vec4<f32> {
4040
is_dot = step(dist_from_center, 0.3 + wave_normalized * 0.2);
4141
}
4242

43-
return vec4<f32>(dot_color * is_dot, is_dot);
44-
}
43+
return vec4<f32>(dot_color * is_dot, 1.0);
44+
}

crates/bevy_render/src/render_resource/shader.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -163,26 +163,8 @@ impl Shader {
163163

164164
match import_path {
165165
ShaderImport::AssetPath(asset_path) => {
166-
let asset_path = std::path::PathBuf::from(&asset_path);
167-
168-
// Get the base path and canonicalize it to match the format of the asset path
169-
let mut base_path = bevy_asset::io::file::FileAssetReader::get_base_path();
170-
base_path.push("assets");
171-
let base_path = base_path.canonicalize().unwrap_or(base_path);
172-
173-
// Try to make the path relative to the base path
174-
let relative_path = match asset_path.canonicalize() {
175-
Ok(canonical_asset_path) => {
176-
match canonical_asset_path.strip_prefix(&base_path) {
177-
Ok(rel_path) => rel_path.to_path_buf(),
178-
Err(_) => canonical_asset_path,
179-
}
180-
}
181-
Err(_) => asset_path,
182-
};
183-
184166
// Create the shader import path - always starting with "/"
185-
let shader_path = std::path::Path::new("/").join(&relative_path);
167+
let shader_path = std::path::Path::new("/").join(&asset_path);
186168

187169
// Convert to a string with forward slashes and without extension
188170
let import_path_str = shader_path

examples/shader/shader_material_wesl.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn setup(
5959
commands.spawn((
6060
Mesh3d(meshes.add(Cuboid::default())),
6161
MeshMaterial3d(materials.add(CustomMaterial {
62-
time: 0.0,
62+
time: Vec4::ZERO,
6363
party_mode: false,
6464
})),
6565
Transform::from_xyz(0.0, 0.5, 0.0),
@@ -80,7 +80,7 @@ fn update(
8080
) {
8181
for (material, mut transform) in query.iter_mut() {
8282
let material = materials.get_mut(material).unwrap();
83-
material.time = time.elapsed_secs();
83+
material.time.x = time.elapsed_secs();
8484
if keys.just_pressed(KeyCode::Space) {
8585
material.party_mode = !material.party_mode;
8686
}
@@ -95,8 +95,9 @@ fn update(
9595
#[derive(Asset, TypePath, AsBindGroup, Clone)]
9696
#[bind_group_data(CustomMaterialKey)]
9797
struct CustomMaterial {
98+
// Needed for 16 bit alignment in WebGL2
9899
#[uniform(0)]
99-
time: f32,
100+
time: Vec4,
100101
party_mode: bool,
101102
}
102103

0 commit comments

Comments
 (0)