diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 732519932..07fe0f0a4 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -1,4 +1,5 @@ use alloc::{borrow::ToOwned as _, boxed::Box, string::String, sync::Arc, vec, vec::Vec}; +use core::fmt; use hashbrown::HashMap; use thiserror::Error; @@ -414,64 +415,13 @@ impl Instance { }) } - /// This function checks that the adapter obeys WebGPU's adapter capability - /// guarantees. Most of the limits are adjusted in wgpu-hal's - /// `adjust_raw_limits` fn. So we only check the remaining properties here. - /// See . fn adapter_allowed(&self, raw: &hal::DynExposedAdapter) -> bool { - // Check "All alignment-class limits must be powers of 2." - // - // Even if the application has not requested strict WebGPU compliance, - // non-power-of-two alignment limits are nonsensical, so don't attempt - // to use such a device. - let min_uniform_buffer_offset_alignment = - raw.capabilities.limits.min_uniform_buffer_offset_alignment; - if !min_uniform_buffer_offset_alignment.is_power_of_two() { - log::error!( - "Adapter {:?} min_uniform_buffer_offset_alignment limit is not a power of 2: {:?}", - raw.info, - min_uniform_buffer_offset_alignment - ); - return false; - } - let min_storage_buffer_offset_alignment = - raw.capabilities.limits.min_storage_buffer_offset_alignment; - if !min_storage_buffer_offset_alignment.is_power_of_two() { - log::error!( - "Adapter {:?} min_storage_buffer_offset_alignment limit is not a power of 2: {:?}", - raw.info, - min_storage_buffer_offset_alignment - ); - return false; - } - - // Following checks are only enabled if `STRICT_WEBGPU_COMPLIANCE` is set. - if !self.flags.contains(InstanceFlags::STRICT_WEBGPU_COMPLIANCE) { - return true; - } - - // Check "All supported limits must be either the default value or better." - let failed_limits = check_limits(&wgt::Limits::defaults(), &raw.capabilities.limits); - if !failed_limits.is_empty() { - log::debug!( - "Adapter {:?} is not WebGPU compliant due to limits: {:?}", - raw.info, - failed_limits - ); - return false; - } - - if !raw.capabilities.downlevel.is_webgpu_compliant() { - let missing_flags = wgt::DownlevelFlags::compliant() - raw.capabilities.downlevel.flags; - log::debug!( - "Adapter {:?} is not WebGPU compliant due to missing downlevel flags: {:?}", - raw.info, - missing_flags - ); - return false; - } - - true + adapter_allowed( + self.flags, + &raw.info, + &raw.capabilities.limits, + &raw.capabilities.downlevel, + ) } pub fn enumerate_adapters( @@ -1315,3 +1265,170 @@ impl Global { Ok((device_id, queue_id)) } } + +/// This function checks that the adapter obeys WebGPU's adapter capability +/// guarantees. Most of the limits are adjusted in wgpu-hal's +/// `adjust_raw_limits` fn. So we only check the remaining properties here. +/// See . +fn adapter_allowed( + flags: InstanceFlags, + info: &impl fmt::Debug, + limits: &wgt::Limits, + downlevel: &wgt::DownlevelCapabilities, +) -> bool { + // Check "All alignment-class limits must be powers of 2." + // + // Even if the application has not requested strict WebGPU compliance, + // non-power-of-two alignment limits are nonsensical, so don't attempt + // to use such a device. + let min_uniform_buffer_offset_alignment = limits.min_uniform_buffer_offset_alignment; + if !min_uniform_buffer_offset_alignment.is_power_of_two() { + log::error!( + "Adapter {:?} min_uniform_buffer_offset_alignment limit is not a power of 2: {:?}", + info, + min_uniform_buffer_offset_alignment + ); + return false; + } + let min_storage_buffer_offset_alignment = limits.min_storage_buffer_offset_alignment; + if !min_storage_buffer_offset_alignment.is_power_of_two() { + log::error!( + "Adapter {:?} min_storage_buffer_offset_alignment limit is not a power of 2: {:?}", + info, + min_storage_buffer_offset_alignment + ); + return false; + } + + // Following checks are only enabled if `STRICT_WEBGPU_COMPLIANCE` is set. + if !flags.contains(InstanceFlags::STRICT_WEBGPU_COMPLIANCE) { + return true; + } + + // Check "All supported limits must be either the default value or better." + let failed_limits = check_limits(&wgt::Limits::defaults(), limits); + if !failed_limits.is_empty() { + log::debug!( + "Adapter {:?} is not WebGPU compliant due to limits: {:?}", + info, + failed_limits + ); + return false; + } + + if !downlevel.is_webgpu_compliant() { + let missing_flags = wgt::DownlevelFlags::compliant() - downlevel.flags; + log::debug!( + "Adapter {:?} is not WebGPU compliant due to missing downlevel flags: {:?}", + info, + missing_flags + ); + return false; + } + + true +} + +#[cfg(test)] +mod tests { + use super::*; + + fn compliant_downlevel() -> wgt::DownlevelCapabilities { + wgt::DownlevelCapabilities { + flags: wgt::DownlevelFlags::compliant(), + ..Default::default() + } + } + + #[test] + fn non_power_of_two_uniform_alignment_always_rejected() { + let limits = wgt::Limits { + min_uniform_buffer_offset_alignment: 3, + ..wgt::Limits::defaults() + }; + assert!(!adapter_allowed( + InstanceFlags::empty(), + &"", + &limits, + &compliant_downlevel() + )); + assert!(!adapter_allowed( + InstanceFlags::STRICT_WEBGPU_COMPLIANCE, + &"", + &limits, + &compliant_downlevel() + )); + } + + #[test] + fn non_power_of_two_storage_alignment_always_rejected() { + let limits = wgt::Limits { + min_storage_buffer_offset_alignment: 96, + ..wgt::Limits::defaults() + }; + assert!(!adapter_allowed( + InstanceFlags::empty(), + &"", + &limits, + &compliant_downlevel() + )); + assert!(!adapter_allowed( + InstanceFlags::STRICT_WEBGPU_COMPLIANCE, + &"", + &limits, + &compliant_downlevel() + )); + } + + #[test] + fn low_limits_allowed_without_strict_compliance() { + let limits = wgt::Limits { + max_texture_dimension_1d: 1, + ..wgt::Limits::defaults() + }; + assert!(adapter_allowed( + InstanceFlags::empty(), + &"", + &limits, + &wgt::DownlevelCapabilities::default() + )); + } + + #[test] + fn low_limits_rejected_with_strict_compliance() { + let limits = wgt::Limits { + max_texture_dimension_1d: 1, + ..wgt::Limits::defaults() + }; + assert!(!adapter_allowed( + InstanceFlags::STRICT_WEBGPU_COMPLIANCE, + &"", + &limits, + &compliant_downlevel() + )); + } + + #[test] + fn missing_downlevel_flags_rejected_with_strict_compliance() { + let downlevel = wgt::DownlevelCapabilities { + flags: wgt::DownlevelFlags::empty(), + ..Default::default() + }; + assert!(!adapter_allowed( + InstanceFlags::STRICT_WEBGPU_COMPLIANCE, + &"", + &wgt::Limits::defaults(), + &downlevel + )); + } + + #[test] + fn fully_compliant_adapter_always_allowed() { + assert!(adapter_allowed( + InstanceFlags::STRICT_WEBGPU_COMPLIANCE, + &"", + &wgt::Limits::defaults(), + &compliant_downlevel() + )); + } +}