Skip to content

Commit d190106

Browse files
authored
Load FXC dynamically to remove the dependency on d3dcompiler_47.dll (#7588)
1 parent 97794f1 commit d190106

File tree

5 files changed

+268
-154
lines changed

5 files changed

+268
-154
lines changed

wgpu-hal/src/dx12/adapter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl super::Adapter {
5757
library: &Arc<D3D12Lib>,
5858
instance_flags: wgt::InstanceFlags,
5959
memory_budget_thresholds: wgt::MemoryBudgetThresholds,
60-
dxc_container: Option<Arc<shader_compilation::DxcContainer>>,
60+
compiler_container: Arc<shader_compilation::CompilerContainer>,
6161
) -> Option<crate::ExposedAdapter<super::Api>> {
6262
// Create the device so that we can get the capabilities.
6363
let device = {
@@ -224,8 +224,8 @@ impl super::Adapter {
224224
}
225225
};
226226

227-
let shader_model = if let Some(ref dxc_container) = dxc_container {
228-
let max_shader_model = match dxc_container.max_shader_model {
227+
let shader_model = if let Some(max_shader_model) = compiler_container.max_shader_model() {
228+
let max_shader_model = match max_shader_model {
229229
wgt::DxcShaderModel::V6_0 => Direct3D12::D3D_SHADER_MODEL_6_0,
230230
wgt::DxcShaderModel::V6_1 => Direct3D12::D3D_SHADER_MODEL_6_1,
231231
wgt::DxcShaderModel::V6_2 => Direct3D12::D3D_SHADER_MODEL_6_2,
@@ -519,7 +519,7 @@ impl super::Adapter {
519519
presentation_timer,
520520
workarounds,
521521
memory_budget_thresholds,
522-
dxc_container,
522+
compiler_container,
523523
},
524524
info,
525525
features,
@@ -658,7 +658,7 @@ impl crate::Adapter for super::Adapter {
658658
self.private_caps,
659659
&self.library,
660660
self.memory_budget_thresholds,
661-
self.dxc_container.clone(),
661+
self.compiler_container.clone(),
662662
)?;
663663
Ok(crate::OpenDevice {
664664
device,

wgpu-hal/src/dx12/device.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl super::Device {
4646
private_caps: super::PrivateCapabilities,
4747
library: &Arc<D3D12Lib>,
4848
memory_budget_thresholds: wgt::MemoryBudgetThresholds,
49-
dxc_container: Option<Arc<shader_compilation::DxcContainer>>,
49+
compiler_container: Arc<shader_compilation::CompilerContainer>,
5050
) -> Result<Self, crate::DeviceError> {
5151
if private_caps
5252
.instance_flags
@@ -202,7 +202,7 @@ impl super::Device {
202202
render_doc: Default::default(),
203203
null_rtv_handle,
204204
mem_allocator,
205-
dxc_container,
205+
compiler_container,
206206
counters: Default::default(),
207207
})
208208
}
@@ -325,27 +325,14 @@ impl super::Device {
325325

326326
let source_name = stage.module.raw_name.as_deref();
327327

328-
// Compile with DXC if available, otherwise fall back to FXC
329-
let result = if let Some(ref dxc_container) = self.dxc_container {
330-
shader_compilation::compile_dxc(
331-
self,
332-
&source,
333-
source_name,
334-
raw_ep,
335-
stage_bit,
336-
&full_stage,
337-
dxc_container,
338-
)
339-
} else {
340-
shader_compilation::compile_fxc(
341-
self,
342-
&source,
343-
source_name,
344-
raw_ep,
345-
stage_bit,
346-
&full_stage,
347-
)
348-
};
328+
let result = self.compiler_container.compile(
329+
self,
330+
&source,
331+
source_name,
332+
raw_ep,
333+
stage_bit,
334+
&full_stage,
335+
);
349336

350337
let log_level = if result.is_ok() {
351338
log::Level::Info

wgpu-hal/src/dx12/instance.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use windows::{
1010
};
1111

1212
use super::SurfaceTarget;
13-
use crate::{auxil, dx12::D3D12Lib};
13+
use crate::{
14+
auxil,
15+
dx12::{shader_compilation::CompilerContainer, D3D12Lib},
16+
};
1417

1518
impl crate::Instance for super::Instance {
1619
type A = super::Api;
@@ -66,39 +69,34 @@ impl crate::Instance for super::Instance {
6669
}
6770
}
6871

69-
// Initialize DXC shader compiler
70-
let dxc_container = match desc.backend_options.dx12.shader_compiler.clone() {
72+
// Initialize the shader compiler
73+
let compiler_container = match desc.backend_options.dx12.shader_compiler.clone() {
7174
wgt::Dx12Compiler::DynamicDxc {
7275
dxc_path,
7376
max_shader_model,
74-
} => {
75-
let container = super::shader_compilation::get_dynamic_dxc_container(
76-
dxc_path.into(),
77-
max_shader_model,
78-
)
79-
.map_err(|e| {
77+
} => CompilerContainer::new_dynamic_dxc(dxc_path.into(), max_shader_model).map_err(
78+
|e| {
8079
crate::InstanceError::with_source(String::from("Failed to load dynamic DXC"), e)
81-
})?;
80+
},
81+
)?,
82+
wgt::Dx12Compiler::StaticDxc => CompilerContainer::new_static_dxc().map_err(|e| {
83+
crate::InstanceError::with_source(String::from("Failed to load static DXC"), e)
84+
})?,
85+
wgt::Dx12Compiler::Fxc => CompilerContainer::new_fxc().map_err(|e| {
86+
crate::InstanceError::with_source(String::from("Failed to load FXC"), e)
87+
})?,
88+
};
8289

83-
Some(Arc::new(container))
90+
match compiler_container {
91+
CompilerContainer::DynamicDxc(..) => {
92+
log::debug!("Using dynamic DXC for shader compilation")
8493
}
85-
wgt::Dx12Compiler::StaticDxc => {
86-
let container =
87-
super::shader_compilation::get_static_dxc_container().map_err(|e| {
88-
crate::InstanceError::with_source(
89-
String::from("Failed to load static DXC"),
90-
e,
91-
)
92-
})?;
93-
94-
Some(Arc::new(container))
94+
CompilerContainer::StaticDxc(..) => {
95+
log::debug!("Using static DXC for shader compilation")
96+
}
97+
CompilerContainer::Fxc(..) => {
98+
log::debug!("Using FXC for shader compilation")
9599
}
96-
wgt::Dx12Compiler::Fxc => None,
97-
};
98-
99-
match dxc_container {
100-
Some(_) => log::debug!("Using DXC for shader compilation"),
101-
None => log::debug!("Using FXC for shader compilation"),
102100
}
103101

104102
Ok(Self {
@@ -110,7 +108,7 @@ impl crate::Instance for super::Instance {
110108
supports_allow_tearing,
111109
flags: desc.flags,
112110
memory_budget_thresholds: desc.memory_budget_thresholds,
113-
dxc_container,
111+
compiler_container: Arc::new(compiler_container),
114112
})
115113
}
116114

@@ -148,7 +146,7 @@ impl crate::Instance for super::Instance {
148146
&self.library,
149147
self.flags,
150148
self.memory_budget_thresholds,
151-
self.dxc_container.clone(),
149+
self.compiler_container.clone(),
152150
)
153151
})
154152
.collect()

wgpu-hal/src/dx12/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ pub struct Instance {
460460
_lib_dxgi: DxgiLib,
461461
flags: wgt::InstanceFlags,
462462
memory_budget_thresholds: wgt::MemoryBudgetThresholds,
463-
dxc_container: Option<Arc<shader_compilation::DxcContainer>>,
463+
compiler_container: Arc<shader_compilation::CompilerContainer>,
464464
}
465465

466466
impl Instance {
@@ -592,7 +592,7 @@ pub struct Adapter {
592592
#[allow(unused)]
593593
workarounds: Workarounds,
594594
memory_budget_thresholds: wgt::MemoryBudgetThresholds,
595-
dxc_container: Option<Arc<shader_compilation::DxcContainer>>,
595+
compiler_container: Arc<shader_compilation::CompilerContainer>,
596596
}
597597

598598
unsafe impl Send for Adapter {}
@@ -655,7 +655,7 @@ pub struct Device {
655655
render_doc: auxil::renderdoc::RenderDoc,
656656
null_rtv_handle: descriptor::Handle,
657657
mem_allocator: Allocator,
658-
dxc_container: Option<Arc<shader_compilation::DxcContainer>>,
658+
compiler_container: Arc<shader_compilation::CompilerContainer>,
659659
counters: Arc<wgt::HalCounters>,
660660
}
661661

0 commit comments

Comments
 (0)