mirror of
https://github.com/bevyengine/bevy
synced 2024-11-24 21:53:07 +00:00
fix custom shader imports (#10030)
# 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.
This commit is contained in:
parent
687e379800
commit
30cb95d96e
3 changed files with 14 additions and 5 deletions
|
@ -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>,
|
||||
|
@ -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
assets/shaders/custom_material_import.wgsl
Normal file
2
assets/shaders/custom_material_import.wgsl
Normal file
|
@ -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);
|
|
@ -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 {
|
||||
|
@ -75,6 +78,7 @@ impl Shader {
|
|||
source: Source::Wgsl(source),
|
||||
additional_imports: Default::default(),
|
||||
shader_defs: Default::default(),
|
||||
file_dependencies: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,6 +108,7 @@ impl Shader {
|
|||
source: Source::Glsl(source, stage),
|
||||
additional_imports: Default::default(),
|
||||
shader_defs: Default::default(),
|
||||
file_dependencies: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,6 +121,7 @@ impl Shader {
|
|||
source: Source::SpirV(source.into()),
|
||||
additional_imports: Default::default(),
|
||||
shader_defs: Default::default(),
|
||||
file_dependencies: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)?,
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue