Fix dxc compile error when shader label contains : (#10134)

This commit is contained in:
Lucas Meurer authored and GitHub committed 2026-09-01 16:35:51 -07:00
1 parent 818586c4d1
commit 5887663c8e
4 files changed
+159 -3

No files matched your search

+1
View File
@@ -180,6 +180,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109).
#### DX12
- Make sure padding bytes are 0 in the destination buffer after a `copy_texture_to_buffer` when `UnrestrictedBufferTextureCopyPitchSupported` is not available. By @teoxoy in [#10005](https://github.com/gfx-rs/wgpu/pull/10005).
- Sanitize shader labels before passing them to DXC as the source name. Fixes compilation when label contains a `:`. By @lucasmerlin in [#10134](https://github.com/gfx-rs/wgpu/pull/10134).
#### Vulkan
+2
View File
@@ -18,6 +18,7 @@ use wgpu_test::{GpuTestInitializer, TestingContext};
pub mod array_size_overrides;
pub mod compilation_messages;
pub mod data_builtins;
pub mod module_label;
pub mod numeric_builtins;
pub mod struct_layout;
pub mod workgroup_size_overrides;
@@ -27,6 +28,7 @@ pub fn all_tests(tests: &mut Vec<GpuTestInitializer>) {
array_size_overrides::all_tests(tests);
compilation_messages::all_tests(tests);
data_builtins::all_tests(tests);
module_label::all_tests(tests);
numeric_builtins::all_tests(tests);
struct_layout::all_tests(tests);
workgroup_size_overrides::all_tests(tests);
@@ -0,0 +1,63 @@
//! Shader module labels are handed to the backend's shader compiler, which may
//! interpret them as more than an opaque string. DXC in particular reads the
//! label as the input file's path, so a label containing a `:` used to fail to
//! compile at all.
use wgpu::{DownlevelFlags, Limits};
use wgpu_test::{apply, gpu_test, GpuTestConfiguration, TestParameters, TestingContext};
pub fn all_tests(vec: &mut Vec<wgpu_test::GpuTestInitializer>) {
vec.push(SHADER_MODULE_LABEL_PUNCTUATION);
}
const SHADER: &str = r#"
@compute @workgroup_size(1)
fn main() {}
"#;
/// Labels containing punctuation that a shader compiler might read as path
/// syntax must not affect compilation.
#[apply(gpu_test!)]
static SHADER_MODULE_LABEL_PUNCTUATION: GpuTestConfiguration = GpuTestConfiguration::new()
.parameters(
TestParameters::default()
.downlevel_flags(DownlevelFlags::COMPUTE_SHADERS)
.limits(Limits::downlevel_defaults()),
)
.run_sync(|ctx| {
for label in [
"Renderer::shader",
"Renderer:shader",
"a::b::c",
"renderer/shader",
"C:/shader",
"shader with spaces",
"shader\twith\ttabs",
"/foo",
"-foo",
"",
] {
create_pipeline(&ctx, label);
}
});
fn create_pipeline(ctx: &TestingContext, label: &str) {
let module = ctx
.device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some(label),
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(SHADER)),
});
// The backend compiles the shader here, not in `create_shader_module`.
let _pipeline = ctx
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some(label),
layout: None,
module: &module,
entry_point: Some("main"),
compilation_options: Default::default(),
cache: None,
});
}
+93 -3
View File
@@ -373,9 +373,10 @@ fn compile_dxc(
) -> Result<crate::dx12::CompiledShader, crate::PipelineError> {
profiling::scope!("compile_dxc");
let source_name = source_name.and_then(|cstr| cstr.to_str().ok());
let source_name = source_name.map(OPCWSTR::new);
let source_name = source_name
.and_then(|cstr| cstr.to_str().ok())
.and_then(sanitize_source_name);
let source_name = source_name.as_deref().map(OPCWSTR::new);
let raw_ep = OPCWSTR::new(raw_ep);
let full_stage = OPCWSTR::new(full_stage);
@@ -448,3 +449,92 @@ fn compile_dxc(
Ok(crate::dx12::CompiledShader::Dxc(blob))
}
/// Prepare a shader module label for use as DXC's input file name.
///
/// `compile_dxc` passes this as the first positional argument, which DXC reads
/// as the input file's path. Characters that it would misread there are mapped
/// to `-`: control characters and exotic whitespace, path separators, and
/// colons, which DXC takes for a drive designator when the name has no
/// separator.
///
/// The result is anchored to the current directory, so that a label starting
/// with `-` cannot be parsed as a command-line flag. An empty label would leave
/// a bare `./`, which would error since it is a directory, so we drop those.
fn sanitize_source_name(name: &str) -> Option<String> {
if name.is_empty() {
return None;
}
let name: String = name
.chars()
.map(|c| {
let unsafe_char = c.is_control()
|| std::path::is_separator(c)
|| c == ':'
|| (c.is_whitespace() && c != ' ');
if unsafe_char {
'-'
} else {
c
}
})
.collect();
Some(format!("./{name}"))
}
#[cfg(test)]
mod tests {
use super::sanitize_source_name;
#[test]
fn sanitize_source_name_anchors_to_current_dir() {
assert_eq!(sanitize_source_name("shader").as_deref(), Some("./shader"));
assert_eq!(
sanitize_source_name("shader with spaces").as_deref(),
Some("./shader with spaces")
);
}
#[test]
fn sanitize_source_name_replaces_path_syntax() {
assert_eq!(
sanitize_source_name("Renderer::shader").as_deref(),
Some("./Renderer--shader")
);
assert_eq!(
sanitize_source_name("renderer/shader").as_deref(),
Some("./renderer-shader")
);
assert_eq!(
sanitize_source_name("renderer\\shader").as_deref(),
Some("./renderer-shader")
);
assert_eq!(
sanitize_source_name("C:/shader").as_deref(),
Some("./C--shader")
);
}
#[test]
fn sanitize_source_name_defuses_command_line_flags() {
assert_eq!(sanitize_source_name("-foo").as_deref(), Some("./-foo"));
assert_eq!(sanitize_source_name("/foo").as_deref(), Some("./-foo"));
}
#[test]
fn sanitize_source_name_replaces_control_chars() {
assert_eq!(
sanitize_source_name("shader\twith\ttabs").as_deref(),
Some("./shader-with-tabs")
);
assert_eq!(sanitize_source_name("a\nb").as_deref(), Some("./a-b"));
assert_eq!(sanitize_source_name("a\u{7}b").as_deref(), Some("./a-b"));
}
#[test]
fn sanitize_source_name_drops_empty() {
assert_eq!(sanitize_source_name(""), None);
}
}