diff --git a/wgpu-hal/src/auxil/mod.rs b/wgpu-hal/src/auxil/mod.rs index 696951a0d..3f477c785 100644 --- a/wgpu-hal/src/auxil/mod.rs +++ b/wgpu-hal/src/auxil/mod.rs @@ -125,12 +125,6 @@ impl crate::TextureCopy { /// Other limits are left unchanged. #[cfg_attr(not(any_backend), allow(dead_code))] pub(crate) fn apply_hal_limits(mut limits: wgt::Limits) -> wgt::Limits { - // The Metal backend maintains two copies of many limit values (one as - // `wgt::Limits` and one as `metal::PrivateCapabilities`). In order to avoid - // confusing discrepancies between the two, some of the logic here is - // duplicated in the initialization of `metal::PrivateCapabilities`. - // See . - limits.max_bind_groups = limits.max_bind_groups.min(crate::MAX_BIND_GROUPS as u32); limits.max_vertex_buffers = limits .max_vertex_buffers diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index 189f51992..2f030d713 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -118,7 +118,8 @@ impl crate::Adapter for super::Adapter { use crate::TextureFormatCapabilities as Tfc; use wgt::TextureFormat as Tf; - let pc = &self.shared.private_caps; + let msl_version = self.shared.private_caps.msl_version; + let pc = &self.shared.private_texture_format_caps; // Affected formats documented at: // https://developer.apple.com/documentation/metal/mtlreadwritetexturetier/mtlreadwritetexturetier1?language=objc // https://developer.apple.com/documentation/metal/mtlreadwritetexturetier/mtlreadwritetexturetier2?language=objc @@ -141,7 +142,7 @@ impl crate::Adapter for super::Adapter { } else { Tfc::empty() }; - let is_not_apple1x = super::PrivateCapabilities::supports_any( + let is_not_apple1x = super::CapabilitiesQuery::supports_any( self.shared.device.as_ref(), &[ MTLFeatureSet::iOS_GPUFamily2_v1, @@ -150,7 +151,7 @@ impl crate::Adapter for super::Adapter { ], ); - let image_atomic_if = if pc.msl_version >= MTLLanguageVersion::Version3_1 { + let image_atomic_if = if msl_version >= MTLLanguageVersion::Version3_1 { Tfc::STORAGE_ATOMIC } else { Tfc::empty() @@ -389,7 +390,11 @@ impl crate::Adapter for super::Adapter { wgt::TextureFormat::Bgra8UnormSrgb, wgt::TextureFormat::Rgba16Float, ]; - if self.shared.private_caps.format_rgb10a2_unorm_all { + if self + .shared + .private_texture_format_caps + .format_rgb10a2_unorm_all + { formats.push(wgt::TextureFormat::Rgb10a2Unorm); } @@ -542,7 +547,7 @@ const DEPTH_CLIP_MODE: &[MTLFeatureSet] = &[ MTLFeatureSet::macOS_GPUFamily1_v1, ]; -impl super::PrivateCapabilities { +impl super::CapabilitiesQuery { fn supports_any(raw: &ProtocolObject, features_sets: &[MTLFeatureSet]) -> bool { features_sets .iter() @@ -658,11 +663,6 @@ impl super::PrivateCapabilities { MTLLanguageVersion::Version1_0 }; - // The `PrivateCapabilities` we are constructing here duplicates many of the limits - // in `wgt::Limits`, creating a risk of confusion if the limits are adjusted between - // the initialization here and the final `wgt::Limits` values. To reduce this risk, - // some of the calculations here duplicate logic in `auxil::apply_hal_limits`. - // See . Self { msl_version, // macOS 10.11 doesn't support read-write resources @@ -1046,14 +1046,6 @@ impl super::PrivateCapabilities { } } - pub fn device_type(&self) -> wgt::DeviceType { - if self.has_unified_memory.unwrap_or(self.low_power) { - wgt::DeviceType::IntegratedGpu - } else { - wgt::DeviceType::DiscreteGpu - } - } - pub fn features(&self) -> wgt::Features { use wgt::Features as F; @@ -1210,11 +1202,7 @@ impl super::PrivateCapabilities { .flags .set(wgt::DownlevelFlags::ANISOTROPIC_FILTERING, true); - // Be careful adjusting limits here. The `AdapterShared` stores the - // original `PrivateCapabilities`, so code could accidentally use - // the wrong value. See . - - let limits = wgt::Limits { + let limits = crate::auxil::apply_hal_limits(wgt::Limits { // // WebGPU LIMITS: // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits @@ -1237,14 +1225,12 @@ impl super::PrivateCapabilities { max_storage_textures_per_shader_stage: self.max_textures_per_stage.1, max_storage_buffers_per_shader_stage: MAX_STORAGE_BUFFERS_PER_SHADER_STAGE, max_uniform_buffers_per_shader_stage: MAX_UNIFORM_BUFFERS_PER_SHADER_STAGE, - max_vertex_buffers: MAX_VERTEX_BUFFERS.min(crate::MAX_VERTEX_BUFFERS as u32), // duplicative of `apply_hal_limits`, + max_vertex_buffers: MAX_VERTEX_BUFFERS, max_buffer_size: self.max_buffer_size, - // Note: any adjustment here will not be reflected in the stored `PrivateCapabilities`. // No limit, use maxBufferSize. max_uniform_buffer_binding_size: self.max_buffer_size, // No limit, use maxBufferSize. - max_storage_buffer_binding_size: self.max_buffer_size - & !(wgt::STORAGE_BINDING_SIZE_ALIGNMENT as u64 - 1), + max_storage_buffer_binding_size: self.max_buffer_size, min_uniform_buffer_offset_alignment: self.constant_buffer_offset_alignment, // No documented limit. Use 32, which is the lowest allowed value. min_storage_buffer_offset_alignment: 32, @@ -1303,18 +1289,7 @@ impl super::PrivateCapabilities { max_mesh_output_primitives: 256, max_mesh_output_layers: self.max_texture_layers as u32, max_mesh_multiview_view_count: 0, - }; - - // Since a bunch of the limits are duplicated between `Limits` and - // `PrivateCapabilities`, reducing the limits at this point could make - // things inconsistent and lead to confusion. Make sure that doesn't - // happen. See . - debug_assert!( - crate::auxil::apply_hal_limits(limits.clone()) == limits, - "Limits were modified by apply_hal_limits\nOriginal:\n{:#?}\nModified:\n{:#?}", - limits, - crate::auxil::apply_hal_limits(limits.clone()) - ); + }); crate::Capabilities { limits, @@ -1373,6 +1348,56 @@ impl super::PrivateCapabilities { ] } + pub fn private_capabilities(&self) -> super::PrivateCapabilities { + super::PrivateCapabilities { + msl_version: self.msl_version, + low_power: self.low_power, + headless: self.headless, + has_unified_memory: self.has_unified_memory, + timestamp_query_support: self.timestamp_query_support, + supports_memoryless_storage: self.supports_memoryless_storage, + mesh_shaders: self.mesh_shaders, + } + } + + pub fn private_texture_format_capabilities(&self) -> super::PrivateTextureFormatCapabilities { + super::PrivateTextureFormatCapabilities { + read_write_texture_tier: self.read_write_texture_tier, + sample_count_mask: self.sample_count_mask, + int64_atomics: self.int64_atomics, + msaa_desktop: self.msaa_desktop, + msaa_apple3: self.msaa_apple3, + msaa_apple7: self.msaa_apple7, + format_r32float_all: self.format_r32float_all, + format_rgba8_srgb_all: self.format_rgba8_srgb_all, + format_rgb10a2_uint_write: self.format_rgb10a2_uint_write, + format_rgb10a2_unorm_all: self.format_rgb10a2_unorm_all, + format_rg11b10_all: self.format_rg11b10_all, + format_rg32float_all: self.format_rg32float_all, + format_rgba32float_all: self.format_rgba32float_all, + format_depth16unorm: self.format_depth16unorm, + format_depth16unorm_filter: self.format_depth16unorm_filter, + format_depth32float_filter: self.format_depth32float_filter, + format_depth24_stencil8: self.format_depth24_stencil8, + format_bc: self.format_bc, + format_eac_etc: self.format_eac_etc, + format_astc: self.format_astc, + format_astc_hdr: self.format_astc_hdr, + } + } +} + +impl super::PrivateCapabilities { + pub fn device_type(&self) -> wgt::DeviceType { + if self.has_unified_memory.unwrap_or(self.low_power) { + wgt::DeviceType::IntegratedGpu + } else { + wgt::DeviceType::DiscreteGpu + } + } +} + +impl super::PrivateTextureFormatCapabilities { pub fn map_format(&self, format: wgt::TextureFormat) -> MTLPixelFormat { use wgt::TextureFormat as Tf; use MTLPixelFormat as MTL; diff --git a/wgpu-hal/src/metal/command.rs b/wgpu-hal/src/metal/command.rs index 50774c139..bea78bb0e 100644 --- a/wgpu-hal/src/metal/command.rs +++ b/wgpu-hal/src/metal/command.rs @@ -560,7 +560,10 @@ impl crate::CommandEncoder for super::CommandEncoder { T: Iterator, { let dst_texture = if src.format != dst.format { - let raw_format = self.shared.private_caps.map_format(src.format); + let raw_format = self + .shared + .private_texture_format_caps + .map_format(src.format); Cow::Owned(autoreleasepool(|_| { dst.raw.newTextureViewWithPixelFormat(raw_format).unwrap() })) diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 770c3da30..90e5021aa 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -377,8 +377,10 @@ impl super::Device { raw: Retained>, features: wgt::Features, ) -> super::Device { + let capabilities_query = super::CapabilitiesQuery::new(&raw); + let shared = super::AdapterShared::new(raw, &capabilities_query); super::Device { - shared: Arc::new(super::AdapterShared::new(raw)), + shared: Arc::new(shared), features, counters: Default::default(), } @@ -458,7 +460,10 @@ impl crate::Device for super::Device { &self, desc: &crate::TextureDescriptor, ) -> DeviceResult { - let mtl_format = self.shared.private_caps.map_format(desc.format); + let mtl_format = self + .shared + .private_texture_format_caps + .map_format(desc.format); autoreleasepool(|_| { let descriptor = MTLTextureDescriptor::new(); @@ -545,10 +550,14 @@ impl crate::Device for super::Device { let raw_format = self .shared - .private_caps + .private_texture_format_caps .map_view_format(desc.format, aspects); - let format_equal = raw_format == self.shared.private_caps.map_format(texture.format); + let format_equal = raw_format + == self + .shared + .private_texture_format_caps + .map_format(texture.format); let type_equal = raw_type == texture.raw_type; let range_full_resource = desc.range @@ -1509,7 +1518,10 @@ impl crate::Device for super::Device { continue; }; - let raw_format = self.shared.private_caps.map_format(ct.format); + let raw_format = self + .shared + .private_texture_format_caps + .map_format(ct.format); at_descriptor.setPixelFormat(raw_format); at_descriptor.setWriteMask(conv::map_color_write(ct.write_mask)); @@ -1531,7 +1543,10 @@ impl crate::Device for super::Device { // Setup depth stencil state let depth_stencil = match desc.depth_stencil { Some(ref ds) => { - let raw_format = self.shared.private_caps.map_format(ds.format); + let raw_format = self + .shared + .private_texture_format_caps + .map_format(ds.format); let aspects = crate::FormatAspects::from(ds.format); if aspects.contains(crate::FormatAspects::DEPTH) { descriptor.setDepthAttachmentPixelFormat(raw_format); diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 9c46f6e9e..c4ab289dc 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -119,7 +119,7 @@ crate::impl_dyn_resource!( /// Provides availability information about Mac APIs. /// /// This may include Metal features that depend only on software support. -/// Features with varying hardware support are in [`PrivateCapabilities`] +/// Features with varying hardware support are in [`CapabilitiesQuery`] /// /// When feature detection is only needed once, it may also be done inline. struct OsFeatures; @@ -184,35 +184,8 @@ impl crate::Instance for Instance { _surface_hint: Option<&Surface>, ) -> Vec> { let devices = objc2_metal::MTLCopyAllDevices(); - let mut adapters: Vec> = devices - .into_iter() - .map(|dev| { - let name = dev.name().to_string(); - let shared = AdapterShared::new(dev); - crate::ExposedAdapter { - info: wgt::AdapterInfo { - name, - vendor: 0, - device: 0, - device_type: shared.private_caps.device_type(), - device_pci_bus_id: String::new(), - driver: String::new(), - driver_info: String::new(), - backend: wgt::Backend::Metal, - // These are hardcoded based on typical values for Metal devices - // - // See - // for more information. - subgroup_min_size: 4, - subgroup_max_size: 64, - transient_saves_memory: shared.private_caps.supports_memoryless_storage, - }, - features: shared.private_caps.features(), - capabilities: shared.private_caps.capabilities(), - adapter: Adapter::new(Arc::new(shared)), - } - }) - .collect(); + let mut adapters: Vec> = + devices.into_iter().map(AdapterShared::expose).collect(); adapters.sort_by_key(|ad| { ( ad.adapter.shared.private_caps.low_power, @@ -241,11 +214,8 @@ bitflags!( } ); -// TODO(https://github.com/gfx-rs/wgpu/issues/8715): Eliminate duplication with -// `wgt::Limits`. Keeping multiple sets of limits creates a risk of confusion. #[allow(dead_code)] -#[derive(Clone, Debug)] -struct PrivateCapabilities { +struct CapabilitiesQuery { msl_version: MTLLanguageVersion, fragment_rw_storage: bool, read_write_texture_tier: MTLReadWriteTextureTier, @@ -316,11 +286,6 @@ struct PrivateCapabilities { max_sampler_binding_array_elements: ResourceIndex, buffer_alignment: u64, constant_buffer_offset_alignment: u32, - - /// Platform-reported maximum buffer size - /// - /// This value is clamped to `u32::MAX` for `wgt::Limits`, so you probably - /// shouldn't be looking at this copy. max_buffer_size: u64, max_texture_size: u64, max_texture_3d_size: u64, @@ -355,6 +320,42 @@ struct PrivateCapabilities { supports_raytracing: bool, } +#[derive(Debug)] +struct PrivateCapabilities { + msl_version: MTLLanguageVersion, + low_power: bool, + headless: bool, + has_unified_memory: Option, + timestamp_query_support: TimestampQuerySupport, + supports_memoryless_storage: bool, + mesh_shaders: bool, +} + +#[derive(Debug)] +struct PrivateTextureFormatCapabilities { + read_write_texture_tier: MTLReadWriteTextureTier, + sample_count_mask: crate::TextureFormatCapabilities, + int64_atomics: bool, + msaa_desktop: bool, + msaa_apple3: bool, + msaa_apple7: bool, + format_r32float_all: bool, + format_rgba8_srgb_all: bool, + format_rgb10a2_uint_write: bool, + format_rgb10a2_unorm_all: bool, + format_rg11b10_all: bool, + format_rg32float_all: bool, + format_rgba32float_all: bool, + format_depth16unorm: bool, + format_depth16unorm_filter: bool, + format_depth32float_filter: bool, + format_depth24_stencil8: bool, + format_bc: bool, + format_eac_etc: bool, + format_astc: bool, + format_astc_hdr: bool, +} + #[derive(Clone, Debug)] struct PrivateDisabilities { /// Near depth is not respected properly on some Intel GPUs. @@ -381,6 +382,7 @@ struct AdapterShared { device: Retained>, disabilities: PrivateDisabilities, private_caps: PrivateCapabilities, + private_texture_format_caps: PrivateTextureFormatCapabilities, settings: Settings, presentation_timer: time::PresentationTimer, } @@ -389,18 +391,54 @@ unsafe impl Send for AdapterShared {} unsafe impl Sync for AdapterShared {} impl AdapterShared { - fn new(device: Retained>) -> Self { - let private_caps = PrivateCapabilities::new(&device); + fn new( + device: Retained>, + capabilities_query: &CapabilitiesQuery, + ) -> Self { + let private_caps = capabilities_query.private_capabilities(); + let private_texture_format_caps = capabilities_query.private_texture_format_capabilities(); log::debug!("{private_caps:#?}"); + log::debug!("{private_texture_format_caps:#?}"); Self { disabilities: PrivateDisabilities::new(&device), private_caps, + private_texture_format_caps, device, settings: Settings::default(), presentation_timer: time::PresentationTimer::new(), } } + + fn expose(device: Retained>) -> crate::ExposedAdapter { + let name = device.name().to_string(); + let capabilities_query = CapabilitiesQuery::new(&device); + let shared = AdapterShared::new(device, &capabilities_query); + let features = capabilities_query.features(); + let capabilities = capabilities_query.capabilities(); + crate::ExposedAdapter { + info: wgt::AdapterInfo { + name, + vendor: 0, + device: 0, + device_type: shared.private_caps.device_type(), + device_pci_bus_id: String::new(), + driver: String::new(), + driver_info: String::new(), + backend: wgt::Backend::Metal, + // These are hardcoded based on typical values for Metal devices + // + // See + // for more information. + subgroup_min_size: 4, + subgroup_max_size: 64, + transient_saves_memory: shared.private_caps.supports_memoryless_storage, + }, + features, + capabilities, + adapter: Adapter::new(Arc::new(shared)), + } + } } pub struct Adapter { diff --git a/wgpu-hal/src/metal/surface.rs b/wgpu-hal/src/metal/surface.rs index 2756460c2..e08ac591e 100644 --- a/wgpu-hal/src/metal/surface.rs +++ b/wgpu-hal/src/metal/surface.rs @@ -65,7 +65,7 @@ impl crate::Surface for super::Surface { ) -> Result<(), crate::SurfaceError> { log::debug!("build swapchain {config:?}"); - let caps = &device.shared.private_caps; + let caps = &device.shared.private_texture_format_caps; *self.swapchain_format.write() = Some(config.format); *self.extent.write() = config.extent;