Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix custom shader imports #10030

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion assets/shaders/custom_material.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import bevy_pbr::mesh_vertex_output MeshVertexOutput
// we can import items from shader modules in the assets folder with a quoted path
#import "shaders/custom_material_import.wgsl" COLOR_MULTIPLIER

struct CustomMaterial {
color: vec4<f32>,
Expand All @@ -12,5 +14,5 @@ struct CustomMaterial {
fn fragment(
mesh: MeshVertexOutput,
) -> @location(0) vec4<f32> {
return material.color * textureSample(base_color_texture, base_color_sampler, mesh.uv);
return material.color * textureSample(base_color_texture, base_color_sampler, mesh.uv) * COLOR_MULTIPLIER;
}
2 changes: 2 additions & 0 deletions assets/shaders/custom_material_import.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this is made available to the importing module
const COLOR_MULTIPLIER: vec4<f32> = vec4<f32>(1.0, 1.0, 1.0, 0.5);
13 changes: 9 additions & 4 deletions crates/bevy_render/src/render_resource/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct Shader {
pub additional_imports: Vec<naga_oil::compose::ImportDefinition>,
// any shader defs that will be included when this module is used
pub shader_defs: Vec<ShaderDefVal>,
// we must store strong handles to our dependencies to stop them
// from being immediately dropped if we are the only user.
pub file_dependencies: Vec<Handle<Shader>>,
}

impl Shader {
Expand Down Expand Up @@ -75,6 +78,7 @@ impl Shader {
source: Source::Wgsl(source),
additional_imports: Default::default(),
shader_defs: Default::default(),
file_dependencies: Default::default(),
}
}

Expand Down Expand Up @@ -104,6 +108,7 @@ impl Shader {
source: Source::Glsl(source, stage),
additional_imports: Default::default(),
shader_defs: Default::default(),
file_dependencies: Default::default(),
}
}

Expand All @@ -116,6 +121,7 @@ impl Shader {
source: Source::SpirV(source.into()),
additional_imports: Default::default(),
shader_defs: Default::default(),
file_dependencies: Default::default(),
}
}

Expand Down Expand Up @@ -246,7 +252,7 @@ impl AssetLoader for ShaderLoader {

let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let shader = match ext {
let mut shader = match ext {
"spv" => Shader::from_spirv(bytes, load_context.path().to_string_lossy()),
"wgsl" => Shader::from_wgsl(
String::from_utf8(bytes)?,
Expand All @@ -270,11 +276,10 @@ impl AssetLoader for ShaderLoader {
_ => panic!("unhandled extension: {ext}"),
};

// collect file dependencies
// collect and store file dependencies
for import in &shader.imports {
if let ShaderImport::AssetPath(asset_path) = import {
// TODO: should we just allow this handle to be dropped?
james7132 marked this conversation as resolved.
Show resolved Hide resolved
let _handle: Handle<Shader> = load_context.load(asset_path);
shader.file_dependencies.push(load_context.load(asset_path));
}
}
Ok(shader)
Expand Down