Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions wgpu-hal/src/auxil/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,31 @@ impl crate::TextureCopy {
}
}

/// Clamp the limits in `limits` to honor any HAL-imposed maximums.
/// Clamp the limits in `limits` to honor HAL-imposed maximums and WebGPU
/// alignment requirements.
///
/// Limits that do not have a HAL-defined maximum are left unchanged.
/// 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 wants to have its own consistent view of the limits, so
// it may duplicate some of these 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 <https://github.com/gfx-rs/wgpu/issues/8715>.

limits.max_bind_groups = limits.max_bind_groups.min(crate::MAX_BIND_GROUPS as u32);
limits.max_storage_buffer_binding_size &= !(wgt::STORAGE_BINDING_SIZE_ALIGNMENT - 1);
limits.max_vertex_buffers = limits
.max_vertex_buffers
.min(crate::MAX_VERTEX_BUFFERS as u32);
limits.max_color_attachments = limits
.max_color_attachments
.min(crate::MAX_COLOR_ATTACHMENTS as u32);

// Round some limits down to the WebGPU alignment requirement, to avoid
// suggesting values that won't work. (In particular, the CTS queries limits
// and then tests the exact limit value.)
limits.max_storage_buffer_binding_size &= !(wgt::STORAGE_BINDING_SIZE_ALIGNMENT - 1);
limits.max_vertex_buffer_array_stride &= !(wgt::VERTEX_ALIGNMENT as u32 - 1);

limits
}
11 changes: 8 additions & 3 deletions wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ impl super::PrivateCapabilities {
MTLLanguageVersion::V1_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 <https://github.com/gfx-rs/wgpu/issues/8715>.
Self {
msl_version,
// macOS 10.11 doesn't support read-write resources
Expand Down Expand Up @@ -723,7 +728,7 @@ impl super::PrivateCapabilities {
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),
max_vertex_buffers: 31.min(crate::MAX_VERTEX_BUFFERS as u32), // duplicative of `apply_hal_limits`
max_textures_per_stage: if os_type == super::OsType::Macos
|| (family_check && device.supports_family(MTLGPUFamily::Apple6))
{
Expand Down Expand Up @@ -1162,7 +1167,7 @@ impl super::PrivateCapabilities {
let base = wgt::Limits::default();
// Be careful adjusting limits here. The `AdapterShared` stores the
// original `PrivateCapabilities`, so code could accidentally use
// the wrong value.
// the wrong value. See <https://github.com/gfx-rs/wgpu/issues/8715>.

let limits = wgt::Limits {
max_texture_dimension_1d: self.max_texture_size as u32,
Expand Down Expand Up @@ -1241,7 +1246,7 @@ impl super::PrivateCapabilities {
// 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.
// happen. See <https://github.com/gfx-rs/wgpu/issues/8715>.
debug_assert!(
crate::auxil::apply_hal_limits(limits.clone()) == limits,
"Limits were modified by apply_hal_limits\nOriginal:\n{:#?}\nModified:\n{:#?}",
Expand Down