SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Windows: never load a dxcompiler.dll found on PATH; FXC unless DXC ships beside the app (#6756) (#7923)
MarkusRe0baf49b25f9b
diff --git a/Cargo.lock b/Cargo.lockindex c7d1c37b60..a8030a2e25 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4235,6 +4235,7 @@ dependencies = ["serde_json","solidcraft-3dx-client","solidcraft-engine",+ "solidcraft-gpu","solidcraft-space-mouse","solidcraft-ui-egui","ureq",diff --git a/apps/solidcraft/Cargo.toml b/apps/solidcraft/Cargo.tomlindex a4f91d3a91..a39478f403 100644--- a/apps/solidcraft/Cargo.toml+++ b/apps/solidcraft/Cargo.toml@@ -28,6 +28,8 @@ neg_cmp_op_on_partial_ord = "allow"[dependencies]solidcraft-engine = { workspace = true }+# The D3D12 shader compiler the window starts with (#6756).+solidcraft-gpu = { workspace = true }solidcraft-ui-egui = { workspace = true }solidcraft-space-mouse = { workspace = true }eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "wayland", "x11", "links"] }diff --git a/apps/solidcraft/src/main.rs b/apps/solidcraft/src/main.rsindex d2dc5c8db8..f2b9f4ef27 100644--- a/apps/solidcraft/src/main.rs+++ b/apps/solidcraft/src/main.rs@@ -1124,6 +1124,8 @@ fn wgpu_options(cpu: bool, preference: Option<&str>) -> eframe::egui_wgpu::WgpuClet intel_mac = cfg!(all(target_os = "macos", target_arch = "x86_64"));let indirect = std::env::var(startup::INDIRECT_VALIDATION_ENV).ok();setup.instance_descriptor.flags = startup::window_instance_flags(setup.instance_descriptor.flags, intel_mac, indirect.as_deref());+ // Never a `dxcompiler.dll` another program put on PATH (#6756).+ setup.instance_descriptor.backend_options.dx12.shader_compiler = solidcraft_gpu::dx12_compiler();let environment = std::env::var("WGPU_ADAPTER_NAME").ok();let adapter = graphics().and_then(|v| v.get("adapter").and_then(serde_json::Value::as_str).map(str::to_string));startup::configure_adapter(setup, cpu, environment, adapter);diff --git a/apps/solidcraft/src/startup.rs b/apps/solidcraft/src/startup.rsindex 97c458eedf..a5fdbd4031 100644--- a/apps/solidcraft/src/startup.rs+++ b/apps/solidcraft/src/startup.rs@@ -255,7 +255,7 @@ pub const FLOAT_DEPTH_STENCIL: eframe::wgpu::Features = eframe::wgpu::Features::/// asks for a float one only when whichever it picks has it.pub fn float_depth_everywhere(options: &eframe::egui_wgpu::WgpuConfiguration) -> bool {let eframe::egui_wgpu::WgpuSetup::CreateNew(setup) = &options.wgpu_setup else { return false };- let mut desc = eframe::wgpu::InstanceDescriptor::new_without_display_handle_from_env();+ let mut desc = solidcraft_gpu::instance_descriptor_from_env();desc.backends = setup.instance_descriptor.backends;let instance = eframe::wgpu::Instance::new(desc);let adapters = pollster::block_on(instance.enumerate_adapters(setup.instance_descriptor.backends));@@ -272,6 +272,7 @@ pub fn has_hardware_adapter(backends: eframe::wgpu::Backends) -> bool {let mut desc = eframe::wgpu::InstanceDescriptor::new_without_display_handle();desc.backends = backends;desc.flags = eframe::wgpu::InstanceFlags::from_build_config().with_env();+ desc.backend_options.dx12.shader_compiler = solidcraft_gpu::dx12_compiler();let instance = eframe::wgpu::Instance::new(desc);let adapters = pollster::block_on(instance.enumerate_adapters(backends));let found: Vec<String> = adapters.iter().map(|a| format!("{} ({:?})", a.get_info().name, a.get_info().device_type)).collect();@@ -591,7 +592,7 @@ mod tests {if cfg!(target_os = "windows") && std::env::var_os(WGPU_BACKEND_ENV).is_none() {assert_eq!(backends, eframe::wgpu::Backends::DX12);}- let mut desc = eframe::wgpu::InstanceDescriptor::new_without_display_handle_from_env();+ let mut desc = solidcraft_gpu::instance_descriptor_from_env();desc.backends = backends;let instance = eframe::wgpu::Instance::new(desc);let adapters = pollster::block_on(instance.enumerate_adapters(backends));diff --git a/crates/gpu/src/concurrent.rs b/crates/gpu/src/concurrent.rsindex 94facc85c3..fc8a0917e5 100644--- a/crates/gpu/src/concurrent.rs+++ b/crates/gpu/src/concurrent.rs@@ -71,42 +71,10 @@ fn threaded(backend: wgpu::Backend, fxc: impl FnOnce() -> bool) -> bool {}}-/// D3D12 compiles with FXC: `WGPU_DX12_COMPILER` says `fxc`, or leaves wgpu's choice (`auto`),-/// which takes DXC when it can load a `dxcompiler.dll` (#6756) and FXC otherwise. Looked up-/// once: neither changes while the app runs.+/// D3D12 compiles with FXC: the compiler every instance starts with ([`crate::dx12_compiler`]).#[cfg(not(target_arch = "wasm32"))]fn dx12_compiles_with_fxc() -> bool {- static FXC: std::sync::OnceLock<bool> = std::sync::OnceLock::new();- *FXC.get_or_init(|| fxc_chosen(std::env::var("WGPU_DX12_COMPILER").ok().as_deref(), dxcompiler_found))-}--/// [`dx12_compiles_with_fxc`] for the variable's value `setting`, read as wgpu reads it;-/// `dxc_found` looks for the DLL.-#[cfg(not(target_arch = "wasm32"))]-fn fxc_chosen(setting: Option<&str>, dxc_found: impl FnOnce() -> bool) -> bool {- match setting.map(str::to_lowercase).as_deref() {- Some("fxc") => true,- Some("dxc" | "dynamicdxc" | "staticdxc") => false,- // `auto`, unset, or a value wgpu does not know (it keeps `auto` then).- _ => !dxc_found(),- }-}--/// A `dxcompiler.dll` where Windows looks for one: beside the program, in the system folders, the-/// working folder or `PATH`. Conservative: one found there may be one Windows would not load.-#[cfg(not(target_arch = "wasm32"))]-fn dxcompiler_found() -> bool {- let program = std::env::current_exe().ok().and_then(|p| p.parent().map(std::path::Path::to_path_buf));- let windows = std::env::var_os("SystemRoot").map(std::path::PathBuf::from);- let system = windows.as_ref().map(|w| w.join("System32"));- let path = std::env::var_os("PATH");- program- .into_iter()- .chain(system)- .chain(windows)- .chain(std::env::current_dir().ok())- .chain(path.iter().flat_map(std::env::split_paths))- .any(|folder| folder.join("dxcompiler.dll").is_file())+ matches!(crate::dx12_compiler(), wgpu::Dx12Compiler::Fxc)}impl<'scope, 'env> Jobs<'scope, 'env> {@@ -189,18 +157,27 @@ mod tests {}#[test]- fn d3d12_compiles_with_fxc_unless_dxc_is_chosen_or_found() {- for setting in [Some("fxc"), Some("FXC")] {- assert!(fxc_chosen(setting, || true), "{setting:?} keeps FXC even with a DLL around");+ fn d3d12_never_loads_a_dxcompiler_found_on_path_unless_asked_to() {+ let beside = || Some(std::path::PathBuf::from(r"C:\Program Files\SolidCraft\dxcompiler.dll"));+ let is_fxc = |c: &wgpu::Dx12Compiler| matches!(c, wgpu::Dx12Compiler::Fxc);+ let by_name = |c: &wgpu::Dx12Compiler| matches!(c, wgpu::Dx12Compiler::DynamicDxc { dxc_path } if dxc_path == "dxcompiler.dll");+ let beside_program =+ |c: &wgpu::Dx12Compiler| matches!(c, wgpu::Dx12Compiler::DynamicDxc { dxc_path } if dxc_path.ends_with(r"SolidCraft\dxcompiler.dll"));+ // Left to SolidCraft (#6756): FXC, or DXC from the program's folder by its full path,+ // never a `dxcompiler.dll` Windows would find on PATH. wgpu does not trim the value: " fxc "+ // is a value it does not know.+ for setting in [None, Some("auto"), Some("AUTO"), Some(""), Some("unknown"), Some(" fxc ")] {+ assert!(is_fxc(&crate::dx12_compiler_for(setting, None)), "{setting:?} without DXC beside the program: FXC");+ assert!(beside_program(&crate::dx12_compiler_for(setting, beside())), "{setting:?} with DXC beside the program: that one");}- for setting in [Some("dxc"), Some("DynamicDxc"), Some("staticdxc")] {- assert!(!fxc_chosen(setting, || false), "{setting:?} chooses DXC");+ // Named compilers stay as wgpu reads them; `dxc` keeps loading the DLL by name.+ for setting in [Some("fxc"), Some("FXC")] {+ assert!(is_fxc(&crate::dx12_compiler_for(setting, beside())), "{setting:?} keeps FXC even with DXC beside the program");}- // wgpu does not trim the value: " fxc " is a value it does not know, so `auto`.- for setting in [None, Some("auto"), Some(""), Some("unknown"), Some(" fxc ")] {- assert!(fxc_chosen(setting, || false), "{setting:?} without a DLL: FXC");- assert!(!fxc_chosen(setting, || true), "{setting:?} with a DLL: DXC");+ for setting in [Some("dxc"), Some("DynamicDxc")] {+ assert!(by_name(&crate::dx12_compiler_for(setting, None)), "{setting:?} loads dxcompiler.dll by name");}+ assert!(matches!(crate::dx12_compiler_for(Some("staticdxc"), None), wgpu::Dx12Compiler::StaticDxc));}/// A buffer wgpu refuses: mapped for reading and for writing at once.diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rsindex 58517e8355..84d2588aae 100644--- a/crates/gpu/src/lib.rs+++ b/crates/gpu/src/lib.rs@@ -77,6 +77,49 @@ fn caught(scopes: [wgpu::ErrorScopeGuard; 3]) -> Option<String> {failed}+/// The D3D12 shader compiler every wgpu instance starts with (#6756). `WGPU_DX12_COMPILER` names+/// one as wgpu reads it; unset, `auto` or a value wgpu does not know leave the choice to+/// SolidCraft: DXC from the `dxcompiler.dll` beside the program, by its full path, else FXC.+/// wgpu's own `auto` loads `dxcompiler.dll` by name, so Windows takes the first one on `PATH`,+/// whichever program put it there, and DXC 1.8.2505 from such folders (Obsidian's, NVIDIA SDK+/// Manager's) crashed every start. `WGPU_DX12_COMPILER=dxc` still loads it that way. Looked up+/// once: neither the variable nor the program's folder changes while the app runs.+#[cfg(not(target_arch = "wasm32"))]+pub fn dx12_compiler() -> wgpu::Dx12Compiler {+ static CHOSEN: std::sync::OnceLock<wgpu::Dx12Compiler> = std::sync::OnceLock::new();+ CHOSEN.get_or_init(|| dx12_compiler_for(std::env::var("WGPU_DX12_COMPILER").ok().as_deref(), dxc_beside_program())).clone()+}++/// [`dx12_compiler`] for the variable's value `setting` and the `dxcompiler.dll` beside the+/// program, if there is one.+#[cfg(not(target_arch = "wasm32"))]+fn dx12_compiler_for(setting: Option<&str>, beside_program: Option<std::path::PathBuf>) -> wgpu::Dx12Compiler {+ match setting.map(str::parse::<wgpu::Dx12Compiler>) {+ Some(Ok(wgpu::Dx12Compiler::Auto) | Err(_)) | None => match beside_program.as_deref().and_then(std::path::Path::to_str) {+ Some(dxc_path) => wgpu::Dx12Compiler::DynamicDxc { dxc_path: dxc_path.to_string() },+ None => wgpu::Dx12Compiler::Fxc,+ },+ Some(Ok(named)) => named,+ }+}++/// The `dxcompiler.dll` in the program's own folder, if there is one.+#[cfg(not(target_arch = "wasm32"))]+fn dxc_beside_program() -> Option<std::path::PathBuf> {+ let program = std::env::current_exe().ok()?;+ let dll = program.parent()?.join("dxcompiler.dll");+ dll.is_file().then_some(dll)+}++/// wgpu's instance settings from the environment, with SolidCraft's D3D12 shader compiler+/// ([`dx12_compiler`]).+#[cfg(not(target_arch = "wasm32"))]+pub fn instance_descriptor_from_env() -> wgpu::InstanceDescriptor {+ let mut desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env();+ desc.backend_options.dx12.shader_compiler = dx12_compiler();+ desc+}+/// An adapter and device without a window (native only): the adapter [`select::pick`] chooses/// under the `SOLIDCRAFT_GPU` policy. The environment may narrow the backends and pick the D3D12/// shader compiler (`WGPU_BACKEND`, `WGPU_DX12_COMPILER`).diff --git a/crates/gpu/src/select.rs b/crates/gpu/src/select.rsindex 24c92cd4b2..583b2fb616 100644--- a/crates/gpu/src/select.rs+++ b/crates/gpu/src/select.rs@@ -169,7 +169,7 @@ fn report(info: &wgpu::AdapterInfo) -> AdapterReport {}fn instance() -> wgpu::Instance {- let mut desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env();+ let mut desc = crate::instance_descriptor_from_env();desc.backends = native_backends();wgpu::Instance::new(desc)}diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rsindex 3f7a5a320b..fd8a8d31b7 100644--- a/crates/ui-egui/src/gpu.rs+++ b/crates/ui-egui/src/gpu.rs@@ -210,13 +210,14 @@ pub fn analysis_colors(c: &crate::theme::AnalysisColors) -> [f32; 36] {pub(crate) fn offscreen_backends() -> wgpu::Backends {static CHOSEN: std::sync::OnceLock<wgpu::Backends> = std::sync::OnceLock::new();*CHOSEN.get_or_init(|| {- let default = wgpu::InstanceDescriptor::new_without_display_handle_from_env().backends;+ let default = solidcraft_gpu::instance_descriptor_from_env().backends;if !cfg!(target_os = "windows") || std::env::var("WGPU_BACKEND").is_ok_and(|v| !v.trim().is_empty()) {return default;}let hardware = |backends: wgpu::Backends| {let mut desc = wgpu::InstanceDescriptor::new_without_display_handle();desc.backends = backends;+ desc.backend_options.dx12.shader_compiler = solidcraft_gpu::dx12_compiler();let adapters = pollster::block_on(wgpu::Instance::new(desc).enumerate_adapters(backends));adapters.iter().any(|a| a.get_info().device_type != wgpu::DeviceType::Cpu)};@@ -232,7 +233,7 @@ pub(crate) fn offscreen_backends() -> wgpu::Backends {pub(crate) fn offscreen_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {static STARTING: std::sync::Mutex<()> = std::sync::Mutex::new(());let _one_at_a_time = STARTING.lock().unwrap_or_else(std::sync::PoisonError::into_inner);- let mut desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env();+ let mut desc = solidcraft_gpu::instance_descriptor_from_env();desc.backends = offscreen_backends();let instance = wgpu::Instance::new(desc);let adapter = pollster::block_on(instance.request_adapter(&Default::default())).ok()?;