diff --git a/CHANGELOG.md b/CHANGELOG.md index 53a43857bc3..39d7f4c636d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,15 @@ Bottom level categories: - `tan` - `tanh` - Eager release of GPU resources comes from device.trackers. By @bradwerth in [#5075](https://github.com/gfx-rs/wgpu/pull/5075) +- Added `InstanceFlags::GPU_BASED_VALIDATION`, which enables GPU-based validation for shaders. This is currently only supported on the DX12 and Vulkan back ends; 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 diff --git a/wgpu-hal/src/dx12/instance.rs b/wgpu-hal/src/dx12/instance.rs index d7ed762195d..36d110132f4 100644 --- a/wgpu-hal/src/dx12/instance.rs +++ b/wgpu-hal/src/dx12/instance.rs @@ -20,13 +20,22 @@ impl crate::Instance 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 !debug_controller.enable_gpu_based_validation() { + if desc.flags.intersects(wgt::InstanceFlags::VALIDATION) { + debug_controller.enable_layer(); + } + if desc + .flags + .intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION) + && !debug_controller.enable_gpu_based_validation() + { log::warn!("Failed to enable GPU-based validation"); } } diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index 5d2b0be7628..eaca395c0c5 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -759,7 +759,12 @@ impl crate::Instance for super::Instance { let mut gpu_assisted_validation = vk::ValidationFeaturesEXT::builder() .enabled_validation_features(gpu_assisted_validation) .build(); - create_info = create_info.push_next(&mut gpu_assisted_validation); + if desc + .flags + .intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION) + { + create_info = create_info.push_next(&mut gpu_assisted_validation); + } unsafe { profiling::scope!("vkCreateInstance"); diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index fa2a8df5f8b..e192db29c57 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -895,6 +895,18 @@ 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 DX12 and Vulkan + /// back ends. + /// + /// 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) + /// - Vulkan, via the `VK_LAYER_KHRONOS_validation` layer; called ["GPU-Assisted + /// Validation"](https://github.com/KhronosGroup/Vulkan-ValidationLayers/blob/e45aeb85079e0835694cb8f03e6681fd18ae72c9/docs/gpu_validation.md#gpu-assisted-validation) + /// - Apple; called ["shader + /// validation"](https://developer.apple.com/documentation/xcode/validating-your-apps-metal-shader-usage) + const GPU_BASED_VALIDATION = 1 << 4; } } @@ -907,7 +919,7 @@ impl Default for InstanceFlags { impl InstanceFlags { /// Enable 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 @@ -950,6 +962,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 }