diff --git a/cts_runner/fail.lst b/cts_runner/fail.lst index 71c4d11c4..47f94e1ec 100644 --- a/cts_runner/fail.lst +++ b/cts_runner/fail.lst @@ -20,7 +20,6 @@ webgpu:api,validation,compute_pipeline:overrides,workgroup_size,limits:* // 0% webgpu:api,validation,createBindGroup:buffer,resource_state:* // 0%, https://github.com/gfx-rs/wgpu/issues/7881 webgpu:api,validation,createBindGroup:external_texture,* // 0%, no external external texture in deno webgpu:api,validation,createBindGroup:texture,resource_state:* // crash, https://github.com/gfx-rs/wgpu/issues/7881 -webgpu:api,validation,createBindGroupLayout:max_resources_per_stage,in_pipeline_layout:* // metal, https://github.com/gfx-rs/wgpu/issues/8173 webgpu:api,validation,createBindGroupLayout:visibility,VERTEX_shader_stage_buffer_type:* // 25%, writable storage buffers not allowed in VERTEX webgpu:api,validation,createBindGroupLayout:visibility,VERTEX_shader_stage_storage_texture_access:* // 25%, write-access storage textures not allowed in VERTEX webgpu:api,validation,createBindGroupLayout:visibility:* // 25%, missing per-stage storage limits diff --git a/cts_runner/test.lst b/cts_runner/test.lst index a841792a6..eceb27bd6 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -96,8 +96,7 @@ webgpu:api,validation,createBindGroup:texture_must_have_correct_component_type:* webgpu:api,validation,createBindGroup:texture_must_have_correct_dimension:* webgpu:api,validation,createBindGroupLayout:duplicate_bindings:* webgpu:api,validation,createBindGroupLayout:max_dynamic_buffers:* -webgpu:api,validation,createBindGroupLayout:max_resources_per_stage,in_bind_group_layout:* -fails-if(metal) webgpu:api,validation,createBindGroupLayout:max_resources_per_stage,in_pipeline_layout:* +webgpu:api,validation,createBindGroupLayout:max_resources_per_stage,* webgpu:api,validation,createBindGroupLayout:maximum_binding_limit:* webgpu:api,validation,createBindGroupLayout:multisampled_validation:* webgpu:api,validation,createBindGroupLayout:storage_texture,formats:* diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index 7bb72e6be..ea5fd16ce 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -31,6 +31,23 @@ use super::{OsFeatures, TimestampQuerySupport}; /// [new command buffer]: https://developer.apple.com/documentation/metal/mtlcommandqueue/makecommandbuffer()?language=objc const MAX_COMMAND_BUFFERS: usize = 4096; +// Metal has a single buffer limit that we must split across 3 WebGPU limits: +// The Metal limit is: 31 "Maximum number of entries in the buffer argument table, per graphics or kernel function". +// We must split it across: +// - maxStorageBuffersPerShaderStage; must be at least 8 +// - maxUniformBuffersPerShaderStage; must be at least 12 +// - maxVertexBuffers; must be at least 8 +// We require 2 additional internal buffers: +// - one for immediate data +// - one for sizes of other buffers +// We use the last buffer for an acceleration structure. +const MAX_STORAGE_BUFFERS_PER_SHADER_STAGE: u32 = 8; +const MAX_UNIFORM_BUFFERS_PER_SHADER_STAGE: u32 = 12; +const MAX_VERTEX_BUFFERS: u32 = 8; +const MAX_ACCELERATION_STRUCTURES_PER_SHADER_STAGE: u32 = 1; +// Use the end of the range for vertex buffers. +pub const VERTEX_BUFFER_SLOT_START: u32 = 31 - 8; + unsafe impl Send for super::Adapter {} unsafe impl Sync for super::Adapter {} @@ -750,18 +767,21 @@ impl super::PrivateCapabilities { format_depth32float_none: os_type != super::OsType::Macos, format_bgr10a2_all: Self::supports_any(device, BGR10A2_ALL), format_bgr10a2_no_write: !Self::supports_any(device, BGR10A2_ALL), - max_buffers_per_stage: 31, - max_vertex_buffers: 31.min(crate::MAX_VERTEX_BUFFERS as u32), // duplicative of `apply_hal_limits` + // "Maximum number of entries in the texture argument table, per graphics or kernel function" + // The tuple is (sampled, storage). + // The default limit split in WebGPU is 80%-20%. + // - The default maxSampledTexturesPerShaderStage in WebGPU is 16. + // - The default maxStorageTexturesPerShaderStage in WebGPU is 4. + // Use a split of 75%-25% which can exactly split 128 and 96. max_textures_per_stage: if os_type == super::OsType::Macos || (family_check && device.supportsFamily(MTLGPUFamily::Apple6)) { - 128 + (96, 32) // 128 } else if family_check && device.supportsFamily(MTLGPUFamily::Apple4) { - 96 + (72, 24) // 96 } else { - 31 + (23, 8) // 31 }, - max_samplers_per_stage: 16, max_binding_array_elements: if argument_buffers == Some(MTLArgumentBuffersTier::Tier2) { 1_000_000 } else if family_check && device.supportsFamily(MTLGPUFamily::Apple4) { @@ -784,11 +804,25 @@ impl super::PrivateCapabilities { } else { 16 }, + // "Buffer alignment for copying an existing texture to a buffer" buffer_alignment: if matches!(os_type, super::OsType::Macos | super::OsType::VisionOs) { 256 + } else if family_check && device.supportsFamily(MTLGPUFamily::Apple3) { + 16 } else { 64 }, + // "Minimum constant buffer offset alignment" + constant_buffer_offset_alignment: if matches!( + os_type, + super::OsType::Macos | super::OsType::VisionOs + ) { + 256 + } else if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily2_v1) { + 32 + } else { + 4 + }, max_buffer_size: if available!(macos = 10.14, ios = 12.0, tvos = 12.0, visionos = 1.0) { device.maxBufferLength() as u64 } else if os_type == super::OsType::Macos { @@ -796,7 +830,12 @@ impl super::PrivateCapabilities { } else { 1 << 28 // 256MB on iOS 8.0+ }, - max_texture_size: if Self::supports_any( + // "Maximum 1D texture width" & + // "Maximum 2D texture width and height" & + // "Maximum cube map texture width and height" + max_texture_size: if family_check && device.supportsFamily(MTLGPUFamily::Apple10) { + 32768 + } else if Self::supports_any( device, &[ MTLFeatureSet::iOS_GPUFamily3_v1, @@ -808,7 +847,9 @@ impl super::PrivateCapabilities { } else { 8192 }, + // "Maximum 3D texture width, height, and depth" max_texture_3d_size: 2048, + // "Maximum number of layers per 1D texture array, 2D texture array, or 3D texture" max_texture_layers: 2048, max_fragment_input_components: if os_type == super::OsType::Macos || device.supportsFeatureSet(MTLFeatureSet::iOS_GPUFamily4_v1) @@ -817,8 +858,8 @@ impl super::PrivateCapabilities { } else { 60 }, + // "Maximum number of color render targets per render pass descriptor" // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=7 - // 8 is supported on everything on that list max_color_render_targets: if Self::supports_any( device, &[ @@ -831,29 +872,39 @@ impl super::PrivateCapabilities { } else { 4 }, + // "Maximum total render target size, per pixel, when using multiple color render targets" // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=7 max_color_attachment_bytes_per_sample: if family_check - && device.supportsFamily(MTLGPUFamily::Apple7) + && device.supportsFamily(MTLGPUFamily::Apple4) { - 128 - } else if family_check && device.supportsFamily(MTLGPUFamily::Apple4) { - 64 + 64 // 512 bits + } else if family_check && device.supportsFamily(MTLGPUFamily::Apple2) { + 32 // 256 bits + } else if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1) { + // No Limit, use max_color_render_targets * MAX_TARGET_PIXEL_BYTE_COST + 8 * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST as u8 // 1024 bits } else { - 32 + 16 // 128 bits }, - max_varying_components: if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1) + // This limit is the minimum of: + // - "Maximum scalar or vector inputs to a fragment function" + // - "Maximum number of input components to a fragment function" / 4 + max_inter_stage_shader_variables: if (family_check + && device.supportsFamily(MTLGPUFamily::Apple4)) + || device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1) { - 124 + 31 // min(32 or 124, 124 / 4) } else { - 60 + 15 // min(60, 60 / 4) }, + // "Maximum threads per threadgroup" // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=6 // These are older checks but still hold true; no entry in this table supports // more than 1024 threads. max_threads_per_group: if Self::supports_any( device, &[ - MTLFeatureSet::iOS_GPUFamily4_v2, + MTLFeatureSet::iOS_GPUFamily4_v1, MTLFeatureSet::macOS_GPUFamily1_v1, ], ) { @@ -861,6 +912,7 @@ impl super::PrivateCapabilities { } else { 512 }, + // "Maximum total threadgroup memory allocation" // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=6 // These are older checks but still hold true; no entry in this table supports // more than 32kb. @@ -868,7 +920,7 @@ impl super::PrivateCapabilities { device, &[ MTLFeatureSet::iOS_GPUFamily4_v1, - MTLFeatureSet::macOS_GPUFamily1_v2, + MTLFeatureSet::macOS_GPUFamily1_v1, ], ) { 32 << 10 @@ -1158,42 +1210,49 @@ impl super::PrivateCapabilities { .flags .set(wgt::DownlevelFlags::ANISOTROPIC_FILTERING, true); - let base = wgt::Limits::default(); // Be careful adjusting limits here. The `AdapterShared` stores the // original `PrivateCapabilities`, so code could accidentally use // the wrong value. See . let limits = wgt::Limits { + // + // WebGPU LIMITS: + // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits + // max_texture_dimension_1d: self.max_texture_size as u32, max_texture_dimension_2d: self.max_texture_size as u32, max_texture_dimension_3d: self.max_texture_3d_size as u32, max_texture_array_layers: self.max_texture_layers as u32, + // No real limit. max_bind_groups: 8, + // No real limit. max_bindings_per_bind_group: 65535, - max_dynamic_uniform_buffers_per_pipeline_layout: base - .max_dynamic_uniform_buffers_per_pipeline_layout, - max_dynamic_storage_buffers_per_pipeline_layout: base - .max_dynamic_storage_buffers_per_pipeline_layout, - max_sampled_textures_per_shader_stage: self.max_textures_per_stage, - max_samplers_per_shader_stage: self.max_samplers_per_stage, - max_storage_buffers_per_shader_stage: self.max_buffers_per_stage, - max_storage_textures_per_shader_stage: self.max_textures_per_stage, - max_uniform_buffers_per_shader_stage: self.max_buffers_per_stage, - max_binding_array_elements_per_shader_stage: self.max_binding_array_elements, - max_binding_array_sampler_elements_per_shader_stage: self - .max_sampler_binding_array_elements, - max_binding_array_acceleration_structure_elements_per_shader_stage: 0, + // No limit, use maxUniformBuffersPerShaderStage. + max_dynamic_uniform_buffers_per_pipeline_layout: MAX_UNIFORM_BUFFERS_PER_SHADER_STAGE, + // No limit, use maxStorageBuffersPerShaderStage. + max_dynamic_storage_buffers_per_pipeline_layout: MAX_STORAGE_BUFFERS_PER_SHADER_STAGE, + // "Maximum number of entries in the sampler state argument table, per graphics or kernel function" + max_samplers_per_shader_stage: 16, + max_sampled_textures_per_shader_stage: self.max_textures_per_stage.0, + 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_buffer_size: self.max_buffer_size, // Note: any adjustment here will not be reflected in the stored `PrivateCapabilities`. - max_uniform_buffer_binding_size: self.max_buffer_size.min(!0u32 as u64), - max_storage_buffer_binding_size: self.max_buffer_size.min(!0u32 as u64) + // 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_vertex_buffers: self.max_vertex_buffers, + 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, + // "Maximum number of vertex attributes, per vertex descriptor" max_vertex_attributes: 31, - max_vertex_buffer_array_stride: base.max_vertex_buffer_array_stride, - max_immediate_size: 0x1000, - max_inter_stage_shader_variables: self.max_varying_components / 4, - min_uniform_buffer_offset_alignment: self.buffer_alignment as u32, - min_storage_buffer_offset_alignment: self.buffer_alignment as u32, + // No documented limit, matches Vulkan's minimum limit and D3D12's static limit. + max_vertex_buffer_array_stride: 2048, + max_inter_stage_shader_variables: self.max_inter_stage_shader_variables, max_color_attachments: self.max_color_render_targets as u32, max_color_attachment_bytes_per_sample: self.max_color_attachment_bytes_per_sample as u32, @@ -1202,10 +1261,19 @@ impl super::PrivateCapabilities { max_compute_workgroup_size_x: self.max_threads_per_group, max_compute_workgroup_size_y: self.max_threads_per_group, max_compute_workgroup_size_z: self.max_threads_per_group, + // No documented limit, matches Vulkan's minimum limit and D3D12's static limit. max_compute_workgroups_per_dimension: 0xFFFF, - max_buffer_size: self.max_buffer_size, + max_immediate_size: 0x1000, + // + // NATIVE (Non-WebGPU) LIMITS: + // max_non_sampler_bindings: u32::MAX, + max_binding_array_elements_per_shader_stage: self.max_binding_array_elements, + max_binding_array_sampler_elements_per_shader_stage: self + .max_sampler_binding_array_elements, + max_binding_array_acceleration_structure_elements_per_shader_stage: 0, + // from https://developer.apple.com/documentation/metal/mtlaccelerationstructureusage/extendedlimits max_blas_primitive_count: 1 << 28, max_blas_geometry_count: 1 << 24, @@ -1213,7 +1281,8 @@ impl super::PrivateCapabilities { // From 2.17.7 in https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf // > [Acceleration structures] are opaque objects that can be bound directly using // buffer binding points or via argument buffers - max_acceleration_structures_per_shader_stage: self.max_buffers_per_stage, + max_acceleration_structures_per_shader_stage: + MAX_ACCELERATION_STRUCTURES_PER_SHADER_STAGE, max_multiview_view_count: if self.supported_vertex_amplification_factor > 1 { self.supported_vertex_amplification_factor diff --git a/wgpu-hal/src/metal/command.rs b/wgpu-hal/src/metal/command.rs index e02262640..50774c139 100644 --- a/wgpu-hal/src/metal/command.rs +++ b/wgpu-hal/src/metal/command.rs @@ -13,7 +13,7 @@ use objc2_metal::{ MTLVisibilityResultMode, }; -use super::{conv, TimestampQuerySupport}; +use super::{adapter::VERTEX_BUFFER_SLOT_START, conv, TimestampQuerySupport}; use crate::CommandEncoder as _; use alloc::{ borrow::{Cow, ToOwned as _}, @@ -435,7 +435,7 @@ impl super::CommandState { // they were added to the map. result_sizes.extend(stage_info.vertex_buffer_mappings.iter().map(|vbm| { self.vertex_buffer_size_map - .get(&(vbm.id as u64)) + .get(&vbm.id) .map(|size| u32::try_from(size.get()).unwrap_or(u32::MAX)) .unwrap_or_default() })); @@ -1344,7 +1344,7 @@ impl crate::CommandEncoder for super::CommandEncoder { index: u32, binding: crate::BufferBinding<'a, super::Buffer>, ) { - let buffer_index = self.shared.private_caps.max_vertex_buffers as u64 - 1 - index as u64; + let buffer_index = VERTEX_BUFFER_SLOT_START + index; let encoder = self.state.render.as_ref().unwrap(); unsafe { encoder.setVertexBuffer_offset_atIndex( diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 870b050cc..770c3da30 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -24,7 +24,7 @@ use objc2_metal::{ MTLVertexStepFunction, }; -use super::{conv, PassthroughShader, ShaderModuleSource}; +use super::{adapter::VERTEX_BUFFER_SLOT_START, conv, PassthroughShader, ShaderModuleSource}; use crate::{auxil::map_naga_stage, TlasInstance}; type DeviceResult = Result; @@ -845,14 +845,6 @@ impl crate::Device for super::Device { info.sizes_buffer = Some(info.counters.buffers); info.counters.buffers += 1; } - - if info.counters.buffers > self.shared.private_caps.max_buffers_per_stage - || info.counters.textures > self.shared.private_caps.max_textures_per_stage - || info.counters.samplers > self.shared.private_caps.max_samplers_per_stage - { - log::error!("Resource limit exceeded: {info:?}"); - return Err(crate::DeviceError::OutOfMemory); - } } let immediates_infos = stage_data.map_ref(|info| { @@ -862,8 +854,6 @@ impl crate::Device for super::Device { }) }); - let total_counters = stage_data.map_ref(|info| info.counters.clone()); - let per_stage_map = stage_data.map(|info| naga::back::msl::EntryPointResources { immediates_buffer: info .pc_buffer @@ -879,7 +869,6 @@ impl crate::Device for super::Device { Ok(super::PipelineLayout { bind_group_infos, immediates_infos, - total_counters, total_immediates: desc.immediate_size, per_stage_map, }) @@ -1281,7 +1270,7 @@ impl crate::Device for super::Device { } let mapping = naga::back::msl::VertexBufferMapping { - id: self.shared.private_caps.max_vertex_buffers - 1 - i as u32, + id: VERTEX_BUFFER_SLOT_START + i as u32, stride: if vbl.array_stride > 0 { vbl.array_stride.try_into().unwrap() } else { @@ -1341,27 +1330,11 @@ impl crate::Device for super::Device { }); } - // Validate vertex buffer count - if desc.layout.total_counters.vs.buffers + (vertex_buffers.len() as u32) - > self.shared.private_caps.max_vertex_buffers - { - let msg = format!( - "pipeline needs too many buffers in the vertex stage: {} vertex and {} layout", - vertex_buffers.len(), - desc.layout.total_counters.vs.buffers - ); - return Err(crate::PipelineError::Linkage( - wgt::ShaderStages::VERTEX, - msg, - )); - } - // Set the pipeline vertex buffer info if !vertex_buffers.is_empty() { let vertex_descriptor = MTLVertexDescriptor::new(); for (i, vb) in vertex_buffers.iter().enumerate() { - let buffer_index = - self.shared.private_caps.max_vertex_buffers as usize - 1 - i; + let buffer_index = VERTEX_BUFFER_SLOT_START as usize + i; let buffer_desc = unsafe { vertex_descriptor .layouts() diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 66a2e608a..9c46f6e9e 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -311,13 +311,11 @@ struct PrivateCapabilities { format_depth32float_none: bool, format_bgr10a2_all: bool, format_bgr10a2_no_write: bool, - max_buffers_per_stage: ResourceIndex, - max_vertex_buffers: ResourceIndex, - max_textures_per_stage: ResourceIndex, - max_samplers_per_stage: ResourceIndex, + max_textures_per_stage: (ResourceIndex, ResourceIndex), max_binding_array_elements: ResourceIndex, max_sampler_binding_array_elements: ResourceIndex, buffer_alignment: u64, + constant_buffer_offset_alignment: u32, /// Platform-reported maximum buffer size /// @@ -330,7 +328,7 @@ struct PrivateCapabilities { max_fragment_input_components: u64, max_color_render_targets: u8, max_color_attachment_bytes_per_sample: u8, - max_varying_components: u32, + max_inter_stage_shader_variables: u32, max_threads_per_group: u32, max_total_threadgroup_memory: u32, sample_count_mask: crate::TextureFormatCapabilities, @@ -737,7 +735,6 @@ struct ImmediateDataInfo { pub struct PipelineLayout { bind_group_infos: [Option; crate::MAX_BIND_GROUPS], immediates_infos: MultiStageData>, - total_counters: MultiStageResourceCounters, total_immediates: u32, per_stage_map: MultiStageResources, } @@ -1027,7 +1024,7 @@ struct CommandState { /// [`ResourceBinding`]: naga::ResourceBinding storage_buffer_length_map: FastHashMap, - vertex_buffer_size_map: FastHashMap, + vertex_buffer_size_map: FastHashMap, immediates: Vec,