Skip to content

Commit 2f56f3e

Browse files
committed
Fix embeded shader as entry point
1 parent 2e5f821 commit 2f56f3e

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

crates/bevy_shader/src/shader_cache.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl<ShaderModule, RenderDevice> ShaderCache<ShaderModule, RenderDevice> {
210210
if let ShaderImport::AssetPath(path) = shader.import_path() {
211211
let shader_resolver =
212212
ShaderResolver::new(&self.asset_paths, &self.shaders);
213-
let module_path = wesl::ModulePath::from_path(path);
213+
let module_path = wesl_module_path_from_asset_path(path);
214214
let mut compiler_options = wesl::CompileOptions {
215215
imports: true,
216216
condcomp: true,
@@ -360,24 +360,8 @@ impl<ShaderModule, RenderDevice> ShaderCache<ShaderModule, RenderDevice> {
360360
if let Source::Wesl(_) = shader.source
361361
&& let ShaderImport::AssetPath(path) = shader.import_path()
362362
{
363-
use bevy_asset::{io::AssetSourceId, AssetPath};
364-
use std::path::PathBuf;
365-
366-
let asset_path = AssetPath::from(path);
367-
let module_path = if let AssetSourceId::Name(source) = asset_path.source() {
368-
let mut comp = vec!["bevy_asset".to_string()];
369-
let mut path = PathBuf::new();
370-
path.push(source.as_ref());
371-
path.push(asset_path.path());
372-
comp.extend(wesl::ModulePath::from_path(path).components);
373-
wesl::ModulePath {
374-
origin: wesl::syntax::PathOrigin::Package,
375-
components: comp,
376-
}
377-
} else {
378-
wesl::ModulePath::from_path(asset_path.path())
379-
};
380-
self.asset_paths.insert(module_path, id);
363+
self.asset_paths
364+
.insert(wesl_module_path_from_asset_path(path), id);
381365
}
382366
self.shaders.insert(id, shader);
383367
pipelines_to_queue
@@ -427,6 +411,27 @@ impl<'a> wesl::Resolver for ShaderResolver<'a> {
427411
}
428412
}
429413

414+
#[cfg(feature = "shader_format_wesl")]
415+
fn wesl_module_path_from_asset_path(path: &String) -> wesl::ModulePath {
416+
use bevy_asset::{io::AssetSourceId, AssetPath};
417+
use std::path::PathBuf;
418+
419+
let asset_path = AssetPath::from(path);
420+
if let AssetSourceId::Name(source) = asset_path.source() {
421+
let mut comp = vec!["bevy_asset".to_string()];
422+
let mut path = PathBuf::new();
423+
path.push(source.as_ref());
424+
path.push(asset_path.path());
425+
comp.extend(wesl::ModulePath::from_path(path).components);
426+
wesl::ModulePath {
427+
origin: wesl::syntax::PathOrigin::Package,
428+
components: comp,
429+
}
430+
} else {
431+
wesl::ModulePath::from_path(asset_path.path())
432+
}
433+
}
434+
430435
/// Type of error returned by a `PipelineCache` when the creation of a GPU pipeline object failed.
431436
#[cfg_attr(
432437
not(target_arch = "wasm32"),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import super::util::make_polka_dots;
2+
import bevy_asset::embedded::shader_material_wesl::files::vertex_attr;
3+
4+
struct CustomMaterial {
5+
// Needed for 16-bit alignment on WebGL2
6+
time: vec4<f32>,
7+
}
8+
9+
@group(3) @binding(0) var<uniform> material: CustomMaterial;
10+
11+
@fragment
12+
fn fragment(
13+
mesh: vertex_attr::VertexOutput,
14+
) -> @location(0) vec4<f32> {
15+
return make_polka_dots(mesh.uv, material.time.x);
16+
}
File renamed without changes.

examples/shader/shader_material_wesl.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use bevy::{
1313
};
1414

1515
/// This example uses shader source files from the assets subdirectory
16-
const FRAGMENT_SHADER_ASSET_PATH: &str = "shaders/custom_material.wesl";
16+
const FRAGMENT_SHADER_ASSET_PATH: &str =
17+
"embedded://shader_material_wesl/files/custom_material.wesl";
1718

1819
fn main() {
1920
App::new()
@@ -47,10 +48,13 @@ struct VertexAttrShader(Handle<Shader>);
4748

4849
impl Plugin for CustomMaterialPlugin {
4950
fn build(&self, app: &mut App) {
51+
embedded_asset!(app, "examples/shader", "files/custom_material.wesl");
5052
embedded_asset!(app, "examples/shader", "files/vertex_attr.wesl");
53+
embedded_asset!(app, "examples/shader", "files/util.wesl");
5154

5255
let asset_server = app.world_mut().resource_mut::<AssetServer>();
53-
let utils_handle = asset_server.load::<Shader>("shaders/util.wesl");
56+
let utils_handle =
57+
asset_server.load::<Shader>("embedded://shader_material_wesl/files/util.wesl");
5458
let vertex_attr_handle =
5559
asset_server.load::<Shader>("embedded://shader_material_wesl/files/vertex_attr.wesl");
5660
app.insert_resource(UtilityShader(utils_handle))

0 commit comments

Comments
 (0)