Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable GPU-based validation for DX12 #5146

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ Bottom level categories:
- Eager release of GPU resources comes from device.trackers. By @bradwerth in [#5075](https://github.com/gfx-rs/wgpu/pull/5075)
- `wgpu-types`'s `trace` and `replay` features have been replaced by the `serde` feature. By @KirmesBude in [#5149](https://github.com/gfx-rs/wgpu/pull/5149)
- `wgpu-core`'s `serial-pass` feature has been removed. Use `serde` instead. By @KirmesBude in [#5149](https://github.com/gfx-rs/wgpu/pull/5149)
- Added `InstanceFlags::GPU_BASED_VALIDATION`, which enables GPU-based validation for shaders. This is currently only supported on the DX12 back end; other platforms ignore this flag, for now.
- This has been added to the set of flags set by `InstanceFlags::debugging` and `InstanceFlags::from_build_config`. If you notice your graphics workloads running more slowly, this may be the culprit.
- As with other instance flags, this flag can be changed in calls to `InstanceFlags::with_env` with the new `WGPU_GPU_BASED_VALIDATION` environment variable.

By @ErichDonGubler in [#5046](https://github.com/gfx-rs/wgpu/pull/5046).


### Bug Fixes
Expand Down
15 changes: 14 additions & 1 deletion d3d12/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::com::ComPtr;
use winapi::um::d3d12sdklayers;
#[cfg(any(feature = "libloading", feature = "implicit-link"))]
use winapi::Interface as _;
use winapi::{
shared::{minwindef::TRUE, winerror::S_OK},
um::d3d12sdklayers,
};

pub type Debug = ComPtr<d3d12sdklayers::ID3D12Debug>;

Expand Down Expand Up @@ -40,4 +43,14 @@ impl Debug {
pub fn enable_layer(&self) {
unsafe { self.EnableDebugLayer() }
}

pub fn enable_gpu_based_validation(&self) -> bool {
let (ptr, hr) = unsafe { self.cast::<d3d12sdklayers::ID3D12Debug1>() };
if hr == S_OK {
unsafe { ptr.SetEnableGPUBasedValidation(TRUE) };
true
} else {
false
}
ErichDonGubler marked this conversation as resolved.
Show resolved Hide resolved
}
}
18 changes: 16 additions & 2 deletions wgpu-hal/src/dx12/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@ impl crate::Instance<super::Api> for super::Instance {
crate::InstanceError::with_source(String::from("failed to load d3d12.dll"), e)
})?;

if desc.flags.contains(wgt::InstanceFlags::VALIDATION) {
if desc
.flags
.intersects(wgt::InstanceFlags::VALIDATION | wgt::InstanceFlags::GPU_BASED_VALIDATION)
{
// Enable debug layer
match lib_main.get_debug_interface() {
Ok(pair) => match pair.into_result() {
Ok(debug_controller) => {
debug_controller.enable_layer();
if desc.flags.intersects(wgt::InstanceFlags::VALIDATION) {
debug_controller.enable_layer();
}
if desc
.flags
.intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION)
{
#[allow(clippy::collapsible_if)]
if !debug_controller.enable_gpu_based_validation() {
log::warn!("Failed to enable GPU-based validation");
}
}
}
Err(err) => {
log::warn!("Unable to enable D3D12 debug interface: {}", err);
Expand Down
15 changes: 13 additions & 2 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,14 @@ bitflags::bitflags! {
/// This mainly applies to a Vulkan driver's compliance version. If the major compliance version
/// is `0`, then the driver is ignored. This flag allows that driver to be enabled for testing.
const ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER = 1 << 3;
/// Enable GPU-based validation. Currently, this only changes behavior on the DX12
/// backend.
///
/// Supported platforms:
///
/// - D3D12; called ["GPU-based validation", or
/// "GBV"](https://web.archive.org/web/20230206120404/https://learn.microsoft.com/en-us/windows/win32/direct3d12/using-d3d12-debug-layer-gpu-based-validation)
const GPU_BASED_VALIDATION = 1 << 4;
}
}

Expand All @@ -905,9 +913,9 @@ impl Default for InstanceFlags {
}

impl InstanceFlags {
/// Enable debugging and validation flags.
/// Enable recommended debugging and validation flags.
pub fn debugging() -> Self {
InstanceFlags::DEBUG | InstanceFlags::VALIDATION
InstanceFlags::DEBUG | InstanceFlags::VALIDATION | InstanceFlags::GPU_BASED_VALIDATION
}

/// Infer good defaults from the build type
Expand Down Expand Up @@ -950,6 +958,9 @@ impl InstanceFlags {
if let Some(bit) = env("WGPU_ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER") {
self.set(Self::ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER, bit);
}
if let Some(bit) = env("WGPU_GPU_BASED_VALIDATION") {
self.set(Self::GPU_BASED_VALIDATION, bit);
}

self
}
Expand Down
Loading