From fe0afc9b5e1dbb8222139f26dd7e1319587eb8e9 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Thu, 11 Jan 2024 14:09:12 -0500 Subject: [PATCH] feat(vulkan): enable GPU-based validation for Vulkan backend Logic for doing this was sourced from page 3 of [LunarG's guide], with the following section stack: * Activating GPU-Assisted Validation * Enabling and Specifying Options with the Programmatic Interface [LunarG's guide]: https://www.lunarg.com/wp-content/uploads/2019/02/GPU-Assisted-Validation_v3_02_22_19.pdf --- CHANGELOG.md | 4 ++-- wgpu-hal/src/vulkan/instance.rs | 34 ++++++++++++++++++++++++++++++++- wgpu-types/src/lib.rs | 6 ++++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56877adcdf2..bc997c57d3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,11 +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. +- Added `InstanceFlags::GPU_BASED_VALIDATION`, which enables GPU-based validation for shaders. This is currently only supported on the DX12 and Vulkan backends; 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). + By @ErichDonGubler in [#5146](https://github.com/gfx-rs/wgpu/pull/5146), [#5046](https://github.com/gfx-rs/wgpu/pull/5046). - `wgpu::Instance` can now report which `wgpu::Backends` are available based on the build configuration. By @wumpf [#5167](https://github.com/gfx-rs/wgpu/pull/5167) ```diff -wgpu::Instance::any_backend_feature_enabled() diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index 199ce511883..72708d76895 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -285,6 +285,10 @@ impl super::Instance { extensions.push(ext::DebugUtils::name()); } + if flags.intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION) { + extensions.push(vk::ExtValidationFeaturesFn::name()); + } + // VK_EXT_swapchain_colorspace // Provides wide color gamut extensions.push(vk::ExtSwapchainColorspaceFn::name()); @@ -653,6 +657,24 @@ impl crate::Instance for super::Instance { let validation_layer_name = CStr::from_bytes_with_nul(b"VK_LAYER_KHRONOS_validation\0").unwrap(); let validation_layer_properties = find_layer(&instance_layers, validation_layer_name); + let validation_features_enabled = || { + validation_layer_properties.is_some().then(|| { + let exts = Self::enumerate_instance_extension_properties( + &entry, + Some(validation_layer_name), + )?; + let mut ext_names = exts + .iter() + .filter_map(|ext| cstr_from_bytes_until_nul(&ext.extension_name)); + let found = + ext_names.any(|ext_name| ext_name == vk::ExtValidationFeaturesFn::name()); + Ok(found) + }) + }; + let gpu_based_validation_enabled = desc + .flags + .intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION) + && validation_features_enabled().transpose()?.unwrap_or(false); let nv_optimus_layer = CStr::from_bytes_with_nul(b"VK_LAYER_NV_optimus\0").unwrap(); let has_nv_optimus = find_layer(&instance_layers, nv_optimus_layer).is_some(); @@ -664,7 +686,7 @@ impl crate::Instance for super::Instance { // Request validation layer if asked. let mut debug_utils = None; - if desc.flags.intersects(wgt::InstanceFlags::VALIDATION) { + if desc.flags.intersects(wgt::InstanceFlags::VALIDATION) || gpu_based_validation_enabled { if let Some(layer_properties) = validation_layer_properties { layers.push(validation_layer_name); @@ -765,6 +787,16 @@ impl crate::Instance for super::Instance { create_info = create_info.push_next(vk_create_info); } + let mut gpu_assisted_validation = vk::ValidationFeaturesEXT::builder() + .enabled_validation_features(&[ + vk::ValidationFeatureEnableEXT::GPU_ASSISTED, + vk::ValidationFeatureEnableEXT::GPU_ASSISTED_RESERVE_BINDING_SLOT, + vk::ValidationFeatureEnableEXT::SYNCHRONIZATION_VALIDATION, + ]); + if dbg!(gpu_based_validation_enabled) { + create_info = create_info.push_next(&mut gpu_assisted_validation); + } + unsafe { profiling::scope!("vkCreateInstance"); entry.create_instance(&create_info, None) diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index d2d493a7caf..29bae14838a 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -904,13 +904,15 @@ 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. + /// Enable GPU-based validation. Currently, this only changes behavior on the DX12 and + /// Vulkan backends. /// /// 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) const GPU_BASED_VALIDATION = 1 << 4; } }