Skip to content

Commit

Permalink
Fix shader import hot reloading on windows (#10502)
Browse files Browse the repository at this point in the history
# Objective

Hot reloading shader imports on windows is currently broken due to
inconsistent `/` and `\` usage ('/` is used in the user facing APIs and
`\` is produced by notify-rs (and likely other OS apis).

Fixes #10500

## Solution

Standardize import paths when loading a `Shader`. The correct long term
fix is to standardize AssetPath on `/`-only, but this is the right scope
of fix for a patch release.

---------

Co-authored-by: François <[email protected]>
  • Loading branch information
cart and mockersf authored Nov 11, 2023
1 parent c505610 commit 0eeb8f9
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions crates/bevy_render/src/render_resource/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,30 +259,24 @@ impl AssetLoader for ShaderLoader {
) -> BoxedFuture<'a, Result<Shader, Self::Error>> {
Box::pin(async move {
let ext = load_context.path().extension().unwrap().to_str().unwrap();

let path = load_context.asset_path().to_string();
// On windows, the path will inconsistently use \ or /.
// TODO: remove this once AssetPath forces cross-platform "slash" consistency. See #10511
let path = path.replace(std::path::MAIN_SEPARATOR, "/");
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let mut shader = match ext {
"spv" => Shader::from_spirv(bytes, load_context.path().to_string_lossy()),
"wgsl" => Shader::from_wgsl(
String::from_utf8(bytes)?,
load_context.path().to_string_lossy(),
),
"vert" => Shader::from_glsl(
String::from_utf8(bytes)?,
naga::ShaderStage::Vertex,
load_context.path().to_string_lossy(),
),
"frag" => Shader::from_glsl(
String::from_utf8(bytes)?,
naga::ShaderStage::Fragment,
load_context.path().to_string_lossy(),
),
"comp" => Shader::from_glsl(
String::from_utf8(bytes)?,
naga::ShaderStage::Compute,
load_context.path().to_string_lossy(),
),
"wgsl" => Shader::from_wgsl(String::from_utf8(bytes)?, path),
"vert" => {
Shader::from_glsl(String::from_utf8(bytes)?, naga::ShaderStage::Vertex, path)
}
"frag" => {
Shader::from_glsl(String::from_utf8(bytes)?, naga::ShaderStage::Fragment, path)
}
"comp" => {
Shader::from_glsl(String::from_utf8(bytes)?, naga::ShaderStage::Compute, path)
}
_ => panic!("unhandled extension: {ext}"),
};

Expand Down

0 comments on commit 0eeb8f9

Please sign in to comment.