Skip to content

Commit b0f603f

Browse files
Alter compile_{d,f}xc to take Option<&CStr>
1 parent a6d3d13 commit b0f603f

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

wgpu-hal/src/dx12/device.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,7 @@ impl super::Device {
259259
.as_ref()
260260
.map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("{e}")))?;
261261

262-
let source_name = stage
263-
.module
264-
.raw_name
265-
.as_ref()
266-
.and_then(|cstr| cstr.to_str().ok())
267-
.unwrap_or_default();
262+
let source_name = stage.module.raw_name.as_deref();
268263

269264
// Compile with DXC if available, otherwise fall back to FXC
270265
let (result, log_level) = if let Some(ref dxc_container) = self.dxc_container {

wgpu-hal/src/dx12/shader_compilation.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ffi::CStr;
12
use std::ptr;
23

34
pub(super) use dxc::{compile_dxc, get_dxc_container, DxcContainer};
@@ -14,7 +15,7 @@ use crate::auxil::dxgi::result::HResult;
1415
pub(super) fn compile_fxc(
1516
device: &super::Device,
1617
source: &str,
17-
source_name: &str,
18+
source_name: Option<&CStr>,
1819
raw_ep: &std::ffi::CString,
1920
stage_bit: wgt::ShaderStages,
2021
full_stage: String,
@@ -34,11 +35,7 @@ pub(super) fn compile_fxc(
3435
}
3536

3637
// If no name has been set, D3DCompile wants the null pointer.
37-
let source_name = if source_name.is_empty() {
38-
ptr::null()
39-
} else {
40-
source_name.as_ptr()
41-
};
38+
let source_name = source_name.map(|cstr| cstr.as_ptr()).unwrap_or(ptr::null());
4239

4340
let mut error = d3d12::Blob::null();
4441
let hr = unsafe {
@@ -86,6 +83,7 @@ pub(super) fn compile_fxc(
8683
// The Dxc implementation is behind a feature flag so that users who don't want to use dxc can disable the feature.
8784
#[cfg(feature = "dxc_shader_compiler")]
8885
mod dxc {
86+
use std::ffi::CStr;
8987
use std::path::PathBuf;
9088

9189
// Destructor order should be fine since _dxil and _dxc don't rely on each other.
@@ -140,7 +138,7 @@ mod dxc {
140138
pub(crate) fn compile_dxc(
141139
device: &crate::dx12::Device,
142140
source: &str,
143-
source_name: &str,
141+
source_name: Option<&CStr>,
144142
raw_ep: &str,
145143
stage_bit: wgt::ShaderStages,
146144
full_stage: String,
@@ -174,6 +172,10 @@ mod dxc {
174172
Err(e) => return (Err(e), log::Level::Error),
175173
};
176174

175+
let source_name = source_name
176+
.and_then(|cstr| cstr.to_str().ok())
177+
.unwrap_or("");
178+
177179
let compiled = dxc_container.compiler.compile(
178180
&blob,
179181
source_name,
@@ -271,6 +273,7 @@ mod dxc {
271273
// These are stubs for when the `dxc_shader_compiler` feature is disabled.
272274
#[cfg(not(feature = "dxc_shader_compiler"))]
273275
mod dxc {
276+
use std::ffi::CStr;
274277
use std::path::PathBuf;
275278

276279
pub(crate) struct DxcContainer {}
@@ -288,7 +291,7 @@ mod dxc {
288291
pub(crate) fn compile_dxc(
289292
_device: &crate::dx12::Device,
290293
_source: &str,
291-
_source_name: &str,
294+
_source_name: Option<&CStr>,
292295
_raw_ep: &str,
293296
_stage_bit: wgt::ShaderStages,
294297
_full_stage: String,

0 commit comments

Comments
 (0)