Skip to content

Commit

Permalink
fix custom shader imports (bevyengine#10030)
Browse files Browse the repository at this point in the history
# Objective

assets v2 broke custom shader imports. fix them

## Solution

store handles of any file dependencies in the `Shader` to avoid them
being immediately dropped.
also added a use into the `shader_material` example so that it'll be
harder to break support in future.
  • Loading branch information
robtfm authored and Ray Redondo committed Jan 9, 2024
1 parent 44c1ce7 commit 7ed48b1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
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?
let _handle: Handle<Shader> = load_context.load(asset_path);
shader.file_dependencies.push(load_context.load(asset_path));
}
}
Ok(shader)
Expand Down

0 comments on commit 7ed48b1

Please sign in to comment.