Skip to content

Commit

Permalink
add max_shader_model to Dx12Compiler (#7167)
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy authored Feb 18, 2025
1 parent e590555 commit b5e32ce
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ gpu-descriptor = "0.3"
# DX12 dependencies
gpu-allocator = { version = "0.27", default-features = false }
range-alloc = "0.1"
mach-dxcompiler-rs = { version = "0.1.4", default-features = false }
mach-dxcompiler-rs = { version = "0.1.4", default-features = false } # remember to increase max_shader_model if applicable
windows-core = { version = "0.58", default-features = false }

# Gles dependencies
Expand Down
20 changes: 16 additions & 4 deletions wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,18 @@ impl super::Adapter {
}
};

let shader_model = if dxc_container.is_none() {
naga::back::hlsl::ShaderModel::V5_1
} else {
let shader_model = if let Some(ref dxc_container) = dxc_container {
let max_shader_model = match dxc_container.max_shader_model {
wgt::DxcShaderModel::V6_0 => Direct3D12::D3D_SHADER_MODEL_6_0,
wgt::DxcShaderModel::V6_1 => Direct3D12::D3D_SHADER_MODEL_6_1,
wgt::DxcShaderModel::V6_2 => Direct3D12::D3D_SHADER_MODEL_6_2,
wgt::DxcShaderModel::V6_3 => Direct3D12::D3D_SHADER_MODEL_6_3,
wgt::DxcShaderModel::V6_4 => Direct3D12::D3D_SHADER_MODEL_6_4,
wgt::DxcShaderModel::V6_5 => Direct3D12::D3D_SHADER_MODEL_6_5,
wgt::DxcShaderModel::V6_6 => Direct3D12::D3D_SHADER_MODEL_6_6,
wgt::DxcShaderModel::V6_7 => Direct3D12::D3D_SHADER_MODEL_6_7,
};

let mut versions = [
Direct3D12::D3D_SHADER_MODEL_6_7,
Direct3D12::D3D_SHADER_MODEL_6_6,
Expand All @@ -239,7 +248,8 @@ impl super::Adapter {
Direct3D12::D3D_SHADER_MODEL_6_1,
Direct3D12::D3D_SHADER_MODEL_6_0,
]
.iter();
.iter()
.filter(|shader_model| shader_model.0 <= max_shader_model.0);

let highest_shader_model = loop {
if let Some(&sm) = versions.next() {
Expand Down Expand Up @@ -274,6 +284,8 @@ impl super::Adapter {
Direct3D12::D3D_SHADER_MODEL_6_7 => naga::back::hlsl::ShaderModel::V6_7,
_ => unreachable!(),
}
} else {
naga::back::hlsl::ShaderModel::V5_1
};
let private_caps = super::PrivateCapabilities {
instance_flags,
Expand Down
2 changes: 2 additions & 0 deletions wgpu-hal/src/dx12/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ impl crate::Instance for super::Instance {
wgt::Dx12Compiler::DynamicDxc {
dxil_path,
dxc_path,
max_shader_model,
} => {
let container = super::shader_compilation::get_dynamic_dxc_container(
dxc_path.into(),
dxil_path.into(),
max_shader_model,
)
.map_err(|e| {
crate::InstanceError::with_source(String::from("Failed to load dynamic DXC"), e)
Expand Down
4 changes: 4 additions & 0 deletions wgpu-hal/src/dx12/shader_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ unsafe fn dxc_create_instance<T: DxcObj>(

// Destructor order should be fine since _dxil and _dxc don't rely on each other.
pub(super) struct DxcContainer {
pub(super) max_shader_model: wgt::DxcShaderModel,
compiler: Dxc::IDxcCompiler3,
utils: Dxc::IDxcUtils,
validator: Option<Dxc::IDxcValidator>,
Expand All @@ -157,6 +158,7 @@ pub(super) enum GetDynamicDXCContainerError {
pub(super) fn get_dynamic_dxc_container(
dxc_path: PathBuf,
dxil_path: PathBuf,
max_shader_model: wgt::DxcShaderModel,
) -> Result<DxcContainer, GetDynamicDXCContainerError> {
let dxc = DxcLib::new_dynamic(dxc_path)
.map_err(|e| GetDynamicDXCContainerError::FailedToLoad("dxcompiler.dll", e))?;
Expand All @@ -169,6 +171,7 @@ pub(super) fn get_dynamic_dxc_container(
let validator = dxil.create_instance::<Dxc::IDxcValidator>()?;

Ok(DxcContainer {
max_shader_model,
compiler,
utils,
validator: Some(validator),
Expand Down Expand Up @@ -198,6 +201,7 @@ pub(super) fn get_static_dxc_container() -> Result<DxcContainer, crate::DeviceEr
})?;

Ok(DxcContainer {
max_shader_model: wgt::DxcShaderModel::V6_7,
compiler,
utils,
validator: None,
Expand Down
19 changes: 19 additions & 0 deletions wgpu-types/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,20 @@ impl NoopBackendOptions {
}
}

/// DXC shader model.
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub enum DxcShaderModel {
V6_0,
V6_1,
V6_2,
V6_3,
V6_4,
V6_5,
V6_6,
V6_7,
}

/// Selects which DX12 shader compiler to use.
///
/// If the `DynamicDxc` option is selected, but `dxcompiler.dll` and `dxil.dll` files aren't found,
Expand All @@ -361,6 +375,8 @@ pub enum Dx12Compiler {
dxc_path: String,
/// Path to `dxil.dll`.
dxil_path: String,
/// Maximum shader model the given dll supports.
max_shader_model: DxcShaderModel,
},
/// The statically-linked variant of Dxc.
///
Expand All @@ -371,10 +387,13 @@ pub enum Dx12Compiler {

impl Dx12Compiler {
/// Helper function to construct a `DynamicDxc` variant with default paths.
///
/// The dll must support at least shader model 6.5.
pub fn default_dynamic_dxc() -> Self {
Self::DynamicDxc {
dxc_path: String::from("dxcompiler.dll"),
dxil_path: String::from("dxil.dll"),
max_shader_model: DxcShaderModel::V6_5,
}
}

Expand Down
20 changes: 10 additions & 10 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ pub use wgt::{
Color, ColorTargetState, ColorWrites, CommandBufferDescriptor, CompareFunction,
CompositeAlphaMode, CopyExternalImageDestInfo, CoreCounters, DepthBiasState, DepthStencilState,
DeviceLostReason, DeviceType, DownlevelCapabilities, DownlevelFlags, DownlevelLimits,
Dx12BackendOptions, Dx12Compiler, DynamicOffset, Extent3d, Face, Features, FeaturesWGPU,
FeaturesWebGPU, FilterMode, FrontFace, GlBackendOptions, GlFenceBehavior, Gles3MinorVersion,
HalCounters, ImageSubresourceRange, IndexFormat, InstanceDescriptor, InstanceFlags,
InternalCounters, Limits, MemoryHints, MultisampleState, NoopBackendOptions, Origin2d,
Origin3d, PipelineStatisticsTypes, PollError, PollStatus, PolygonMode, PowerPreference,
PredefinedColorSpace, PresentMode, PresentationTimestamp, PrimitiveState, PrimitiveTopology,
PushConstantRange, QueryType, RenderBundleDepthStencil, SamplerBindingType, SamplerBorderColor,
ShaderLocation, ShaderModel, ShaderRuntimeChecks, ShaderStages, StencilFaceState,
StencilOperation, StencilState, StorageTextureAccess, SurfaceCapabilities, SurfaceStatus,
TexelCopyBufferLayout, TextureAspect, TextureDimension, TextureFormat,
Dx12BackendOptions, Dx12Compiler, DxcShaderModel, DynamicOffset, Extent3d, Face, Features,
FeaturesWGPU, FeaturesWebGPU, FilterMode, FrontFace, GlBackendOptions, GlFenceBehavior,
Gles3MinorVersion, HalCounters, ImageSubresourceRange, IndexFormat, InstanceDescriptor,
InstanceFlags, InternalCounters, Limits, MemoryHints, MultisampleState, NoopBackendOptions,
Origin2d, Origin3d, PipelineStatisticsTypes, PollError, PollStatus, PolygonMode,
PowerPreference, PredefinedColorSpace, PresentMode, PresentationTimestamp, PrimitiveState,
PrimitiveTopology, PushConstantRange, QueryType, RenderBundleDepthStencil, SamplerBindingType,
SamplerBorderColor, ShaderLocation, ShaderModel, ShaderRuntimeChecks, ShaderStages,
StencilFaceState, StencilOperation, StencilState, StorageTextureAccess, SurfaceCapabilities,
SurfaceStatus, TexelCopyBufferLayout, TextureAspect, TextureDimension, TextureFormat,
TextureFormatFeatureFlags, TextureFormatFeatures, TextureSampleType, TextureTransition,
TextureUsages, TextureUses, TextureViewDimension, VertexAttribute, VertexFormat,
VertexStepMode, WasmNotSend, WasmNotSendSync, WasmNotSync, COPY_BUFFER_ALIGNMENT,
Expand Down

0 comments on commit b5e32ce

Please sign in to comment.