wgpu_core/
limits.rs

1//! Functionality related to device and adapter limits.
2//!
3//! # Limit Bucketing
4//!
5//! Web browsers make various information about their operating environment
6//! available to content to provide a better experience. For example, content is
7//! able to detect whether the device has a touch-screen, in order to provide an
8//! appropriate user interface.
9//!
10//! [Browser fingerprinting][bfp] employs this information for the purpose of
11//! constructing a unique "fingerprint" value that is unique to a single browser
12//! or shared among a relatively small number of browsers. Fingerprinting can be
13//! used for various purposes, including to identify and track users across
14//! different websites.
15//!
16//! Limit bucketing can reduce the ability to fingerprint users based on GPU
17//! hardware characteristics when using `wgpu` in applications like a web
18//! browser.
19//!
20//! When limit bucketing is enabled, the adapter limits offered by `wgpu` do not
21//! necessarily reflect the exact capabilities of the hardware. Instead, the
22//! hardware capabilities are rounded down to one of several pre-defined buckets.
23//! The goal of doing this is for there to be enough devices assigned to each
24//! bucket that knowledge of which bucket applies is minimally useful for
25//! fingerprinting.
26//!
27//! Limit bucketing may be requested by setting `apply_limit_buckets` in
28//! [`wgt::RequestAdapterOptions`] or by setting `apply_limit_buckets` to
29//! true when calling [`enumerate_adapters`].
30//!
31//! If your application does not expose `wgpu` to untrusted content, limit
32//! bucketing is not necessary.
33//!
34//! [bfp]: https://support.mozilla.org/en-US/kb/firefox-protection-against-fingerprinting
35//! [`enumerate_adapters`]: `crate::instance::Instance::enumerate_adapters`
36
37use core::mem;
38
39use alloc::{borrow::Cow, vec::Vec};
40use thiserror::Error;
41use wgt::error::{ErrorType, WebGpuError};
42use wgt::{AdapterInfo, AdapterLimitBucketInfo, DeviceType, Features, Limits};
43
44use crate::api_log;
45
46#[derive(Clone, Debug, Error)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48#[error("Limit '{name}' value {requested} is better than allowed {allowed}")]
49pub struct FailedLimit {
50    name: Cow<'static, str>,
51    requested: u64,
52    allowed: u64,
53}
54
55impl WebGpuError for FailedLimit {
56    fn webgpu_error_type(&self) -> ErrorType {
57        ErrorType::Validation
58    }
59}
60
61pub(crate) fn check_limits(requested: &Limits, allowed: &Limits) -> Vec<FailedLimit> {
62    let mut failed = Vec::new();
63
64    requested.check_limits_with_fail_fn(allowed, false, |name, requested, allowed| {
65        failed.push(FailedLimit {
66            name: Cow::Borrowed(name),
67            requested,
68            allowed,
69        })
70    });
71
72    failed
73}
74
75/// Fields in [`wgt::AdapterInfo`] relevant to limit bucketing.
76pub(crate) struct BucketedAdapterInfo {
77    // Equivalent to `adapter.info.device_type == wgt::DeviceType::Cpu`
78    is_fallback_adapter: bool,
79
80    subgroup_min_size: u32,
81    subgroup_max_size: u32,
82}
83
84impl BucketedAdapterInfo {
85    const fn defaults() -> Self {
86        Self {
87            is_fallback_adapter: false,
88            subgroup_min_size: 4,
89            subgroup_max_size: 128,
90        }
91    }
92}
93
94impl Default for BucketedAdapterInfo {
95    fn default() -> Self {
96        Self::defaults()
97    }
98}
99
100pub(crate) struct Bucket {
101    name: &'static str,
102    limits: Limits,
103    info: BucketedAdapterInfo,
104    features: Features,
105}
106
107impl Bucket {
108    pub fn name(&self) -> &'static str {
109        self.name
110    }
111
112    /// Returns `true` if the device having `limits`, `info`, and `features` satisfies
113    /// the bucket definition in `self`.
114    pub fn is_compatible(&self, limits: &Limits, info: &AdapterInfo, features: Features) -> bool {
115        // In the context of limit checks, "allowed" or "available" means
116        // what the device supports. If an application requests an
117        // unsupported value, the error message might say "limit of {} exceeds
118        // allowed value {}". For purposes of bucket compatibility, the bucket values
119        // take the place of application-requested values. If the bucket value
120        // is beyond what the device supports, then the device does not qualify
121        // for that bucket.
122        let candidate_is_fallback_adapter = info.device_type == DeviceType::Cpu;
123
124        let failing_limits = check_limits(&self.limits, limits);
125        let limits_ok = failing_limits.is_empty();
126
127        if !limits_ok {
128            log::debug!("Failing limits: {:#?}", failing_limits);
129        }
130
131        let bucket_has_subgroups = self.features.contains(Features::SUBGROUP);
132        let subgroups_ok = !bucket_has_subgroups
133            || info.subgroup_min_size >= self.info.subgroup_min_size
134                && info.subgroup_max_size <= self.info.subgroup_max_size;
135        if !subgroups_ok {
136            log::debug!(
137                "Subgroup min/max {}/{} is not compatible with allowed {}/{}",
138                self.info.subgroup_min_size,
139                self.info.subgroup_max_size,
140                info.subgroup_min_size,
141                info.subgroup_max_size,
142            );
143        }
144
145        let features_ok = features.contains(self.features);
146        if !features_ok {
147            log::debug!("{:?} are not available", self.features - features);
148        }
149
150        limits_ok
151            && candidate_is_fallback_adapter == self.info.is_fallback_adapter
152            && subgroups_ok
153            && features_ok
154    }
155
156    pub fn try_apply_to(&self, adapter: &mut hal::DynExposedAdapter) -> bool {
157        if !self.is_compatible(
158            &adapter.capabilities.limits,
159            &adapter.info,
160            adapter.features,
161        ) {
162            log::debug!("bucket `{}` is not compatible", self.name);
163            return false;
164        }
165
166        let raw_limits = mem::replace(&mut adapter.capabilities.limits, self.limits.clone());
167
168        // Features in EXEMPT_FEATURES are not affected by limit bucketing.
169        let exposed_features = adapter
170            .features
171            .intersection(EXEMPT_FEATURES)
172            .union(self.features);
173        let raw_features = mem::replace(&mut adapter.features, exposed_features);
174
175        let (bucket_subgroup_min_size, bucket_subgroup_max_size) =
176            if self.features.contains(Features::SUBGROUP) {
177                (self.info.subgroup_min_size, self.info.subgroup_max_size)
178            } else {
179                // WebGPU requires that we report these values when subgroups are
180                // not supported
181                (
182                    wgt::MINIMUM_SUBGROUP_MIN_SIZE,
183                    wgt::MAXIMUM_SUBGROUP_MAX_SIZE,
184                )
185            };
186        let raw_subgroup_min_size = mem::replace(
187            &mut adapter.info.subgroup_min_size,
188            bucket_subgroup_min_size,
189        );
190        let raw_subgroup_max_size = mem::replace(
191            &mut adapter.info.subgroup_max_size,
192            bucket_subgroup_max_size,
193        );
194
195        adapter.info.limit_bucket = Some(AdapterLimitBucketInfo {
196            name: Cow::Borrowed(self.name),
197            raw_limits,
198            raw_features,
199            raw_subgroup_min_size,
200            raw_subgroup_max_size,
201        });
202
203        true
204    }
205}
206
207/// Apply [limit bucketing][lt] to the adapter limits and features in `raw`.
208///
209/// Finds a supported bucket and replaces the capabilities with the set defined by
210/// the bucket. If no suitable bucket is found, returns `None`, but this should only
211/// happen with downlevel devices, and attempting to use limit bucketing with
212/// downlevel devices is not recommended.
213///
214/// [lt]: self#Limit-bucketing
215pub fn apply_limit_buckets(mut raw: hal::DynExposedAdapter) -> Option<hal::DynExposedAdapter> {
216    for bucket in buckets() {
217        if bucket.try_apply_to(&mut raw) {
218            let name = bucket.name();
219            api_log!("Applied limit bucket `{name}`");
220            return Some(raw);
221        }
222    }
223    log::warn!(
224        "No suitable limit bucket found for device with {:?}, {:?}, {:?}",
225        raw.capabilities.limits,
226        raw.info,
227        raw.features,
228    );
229    None
230}
231
232/// These features are left alone by limit bucketing. They will be exposed to higher layers
233/// whenever the device supports them, and they are not considered when determining bucket
234/// compatibility.
235///
236/// All four features in the list are related to external textures. (The texture format
237/// features are used internally by Firefox to support external textures.)
238///
239/// Handling them this way is a bit of a kludge, but is expected to be a short-term
240/// situation only until external texture support is universally available.
241///
242/// Note that while NV12 and P010 can be hidden from content by excluding them from WebIDL,
243/// TEXTURE_FORMATS_16BIT_NORM will eventually be replaced with TEXTURE_FORMATS_TIER1, and
244/// at that point neither excluding the tier1 formats from WebIDL entirely nor allowing
245/// content to use them on a device that doesn't have the feature enabled will be
246/// acceptable. See <https://github.com/gfx-rs/wgpu/issues/8122>.
247const EXEMPT_FEATURES: Features = Features::EXTERNAL_TEXTURE
248    .union(Features::TEXTURE_FORMAT_NV12)
249    .union(Features::TEXTURE_FORMAT_P010)
250    .union(Features::TEXTURE_FORMAT_16BIT_NORM);
251
252/// Return the defined adapter feature/limit buckets
253///
254/// Buckets are not always subsets of preceding buckets, but [`enumerate_adapters`]
255/// considers them in the order they are listed here and uses the first bucket satisfied
256/// by the device.
257///
258/// [`enumerate_adapters`]: `crate::instance::Instance::enumerate_adapters`
259pub(crate) fn buckets() -> impl Iterator<Item = &'static Bucket> {
260    [
261        &BUCKET_M1,
262        &BUCKET_A2,
263        &BUCKET_I1,
264        &BUCKET_N1,
265        &BUCKET_A1,
266        &BUCKET_NO_F16,
267        &BUCKET_LLVMPIPE,
268        &BUCKET_WARP,
269        &BUCKET_DEFAULT,
270        &BUCKET_FALLBACK,
271    ]
272    .iter()
273    .copied()
274}
275
276// The following limits could be higher for some hardware, but are capped where they
277// are to avoid introducing platform or backend dependencies.
278//
279// **`max_vertex_attributes`:** While there is broad support for 32, Intel hardware with
280// Vulkan only supports 29; the D3D12 backend is also limited to 30.
281// See <https://gitlab.freedesktop.org/mesa/mesa/-/blob/465c186fc5f72c51bda943ac0e19f6512f8e6262/src/intel/vulkan/anv_private.h#L188>.
282//
283// **`max_dynamic_{storage,uniform}_buffers_per_pipeline_layout`:** These are limited to
284// 4 and 8 by DX12.
285
286// UPLEVEL is not a bucket that is actually applied to devices. It serves as a baseline from
287// which most of the rest of the buckets are derived. (It could be a real bucket if desired,
288// but since UPLEVEL is an intersection across many devices, there is usually a better match
289// for any particular device.)
290const UPLEVEL: Bucket = Bucket {
291    name: "uplevel-defaults",
292    limits: Limits {
293        max_bind_groups: 8,
294        // use default max_bind_groups_plus_vertex_buffers
295        // use default max_bindings_per_bind_group
296        max_buffer_size: 1 << 30, // 1 GB
297        max_color_attachment_bytes_per_sample: 64,
298        // use default max_color_attachments
299        max_compute_invocations_per_workgroup: 1024,
300        max_compute_workgroup_size_x: 1024,
301        max_compute_workgroup_size_y: 1024,
302        // use default max_compute_workgroup_size_z
303        max_compute_workgroup_storage_size: 32 << 10, // 32 kB
304        // use default max_compute_workgroups_per_dimension
305        // use default max_dynamic_storage_buffers_per_pipeline_layout
306        // use default max_dynamic_uniform_buffers_per_pipeline_layout
307        max_inter_stage_shader_variables: 28,
308        // use default max_sampled_textures_per_shader_stage
309        // use default max_samplers_per_shader_stage
310        // use default max_storage_buffer_binding_size
311        // wgpu does not implement max_storage_buffers_in_fragment_stage: 8,
312        // wgpu does not implement max_storage_buffers_in_vertex_stage: 8,
313        // use default max_storage_buffers_per_shader_stage
314        // wgpu does not implement max_storage_textures_in_fragment_stage: 8,
315        // wgpu does not implement max_storage_textures_in_vertex_stage: 8,
316        max_storage_textures_per_shader_stage: 8,
317        max_texture_array_layers: 2048,
318        max_texture_dimension_1d: 16384,
319        max_texture_dimension_2d: 16384,
320        // use default max_texture_dimension_3d
321        // use default max_uniform_buffer_binding_size
322        // use default max_uniform_buffers_per_shader_stage
323        max_vertex_attributes: 29,
324        // use default max_vertex_buffer_array_stride
325        // use default max_vertex_buffers
326        // use default min_storage_buffer_offset_alignment
327        // use default min_uniform_buffer_offset_alignment
328        ..Limits::defaults()
329    },
330    info: BucketedAdapterInfo {
331        is_fallback_adapter: false,
332        subgroup_min_size: 4,
333        subgroup_max_size: 128,
334    },
335    features: Features::DEPTH_CLIP_CONTROL
336        .union(Features::DEPTH32FLOAT_STENCIL8)
337        // omit TEXTURE_COMPRESSION_ASTC
338        // omit TEXTURE_COMPRESSION_ASTC_SLICED_3D
339        .union(Features::TEXTURE_COMPRESSION_BC)
340        .union(Features::TEXTURE_COMPRESSION_BC_SLICED_3D)
341        // omit TEXTURE_COMPRESSION_ETC2
342        .union(Features::TIMESTAMP_QUERY)
343        .union(Features::INDIRECT_FIRST_INSTANCE)
344        // omit SHADER_F16
345        .union(Features::RG11B10UFLOAT_RENDERABLE)
346        .union(Features::BGRA8UNORM_STORAGE)
347        .union(Features::FLOAT32_FILTERABLE)
348        .union(Features::FLOAT32_BLENDABLE)
349        // CLIP_DISTANCES not implemented in wgpu dx12 backend; https://github.com/gfx-rs/wgpu/issues/6236
350        .union(Features::DUAL_SOURCE_BLENDING)
351        // TIER1/TIER2 not implemented in wgpu; https://github.com/gfx-rs/wgpu/issues/8122
352        .union(Features::PRIMITIVE_INDEX)
353        // TEXTURE_COMPONENT_SWIZZLE not implemented in wgpu; https://github.com/gfx-rs/wgpu/issues/1028
354        .union(Features::SUBGROUP)
355        .union(Features::IMMEDIATES),
356};
357
358// e.g. Apple M Series
359const BUCKET_M1: Bucket = Bucket {
360    name: "m1",
361    limits: Limits {
362        max_dynamic_uniform_buffers_per_pipeline_layout: 12,
363        max_sampled_textures_per_shader_stage: 48,
364        max_storage_buffer_binding_size: 1 << 30, // 1 GB,
365        max_vertex_attributes: 31,
366        ..UPLEVEL.limits
367    },
368    info: BucketedAdapterInfo {
369        subgroup_min_size: 4,
370        subgroup_max_size: 64,
371        ..UPLEVEL.info
372    },
373    features: UPLEVEL
374        .features
375        .union(Features::TEXTURE_COMPRESSION_ASTC)
376        .union(Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D)
377        .union(Features::TEXTURE_COMPRESSION_ETC2)
378        .union(Features::SHADER_F16)
379        .union(Features::CLIP_DISTANCES),
380};
381
382// e.g. Radeon Vega
383const BUCKET_A2: Bucket = Bucket {
384    name: "a2",
385    limits: Limits {
386        max_color_attachment_bytes_per_sample: 128,
387        max_compute_workgroup_storage_size: 64 << 10, // 64 kB,
388        max_sampled_textures_per_shader_stage: 48,
389        max_storage_buffer_binding_size: 1 << 30, // 1 GB,
390        max_storage_buffers_per_shader_stage: 16,
391        max_vertex_attributes: 30,
392        ..UPLEVEL.limits
393    },
394    info: BucketedAdapterInfo {
395        subgroup_min_size: 64,
396        subgroup_max_size: 64,
397        ..UPLEVEL.info
398    },
399    features: UPLEVEL.features.union(Features::SHADER_F16),
400};
401
402// e.g. Intel Arc, UHD 600 Series, Iris Xe
403const BUCKET_I1: Bucket = Bucket {
404    name: "i1",
405    limits: Limits {
406        max_color_attachment_bytes_per_sample: 128,
407        max_sampled_textures_per_shader_stage: 48,
408        max_storage_buffer_binding_size: 1 << 29, // 512 MB,
409        max_storage_buffers_per_shader_stage: 16,
410        ..UPLEVEL.limits
411    },
412    info: BucketedAdapterInfo {
413        subgroup_min_size: 8,
414        subgroup_max_size: 32,
415        ..UPLEVEL.info
416    },
417    features: UPLEVEL.features.union(Features::SHADER_F16),
418};
419
420// e.g. GeForce GTX 1650, GeForce RTX 20, 30, 40, 50 Series
421const BUCKET_N1: Bucket = Bucket {
422    name: "n1",
423    limits: Limits {
424        max_color_attachment_bytes_per_sample: 128,
425        max_compute_workgroup_storage_size: 48 << 10, // 48 kB,
426        max_sampled_textures_per_shader_stage: 48,
427        max_storage_buffer_binding_size: 1 << 30, // 1 GB,
428        max_storage_buffers_per_shader_stage: 16,
429        max_vertex_attributes: 30,
430        ..UPLEVEL.limits
431    },
432    info: BucketedAdapterInfo {
433        subgroup_min_size: 32,
434        subgroup_max_size: 32,
435        ..UPLEVEL.info
436    },
437    features: UPLEVEL.features.union(Features::SHADER_F16),
438};
439
440// e.g. Radeon RX 6000, 7000, 9000 Series
441const BUCKET_A1: Bucket = Bucket {
442    name: "a1",
443    limits: Limits {
444        max_color_attachment_bytes_per_sample: 128,
445        max_sampled_textures_per_shader_stage: 48,
446        max_storage_buffer_binding_size: 1 << 30, // 1 GB,
447        max_storage_buffers_per_shader_stage: 16,
448        max_vertex_attributes: 30,
449        ..UPLEVEL.limits
450    },
451    info: BucketedAdapterInfo {
452        subgroup_min_size: 32,
453        subgroup_max_size: 64,
454        ..UPLEVEL.info
455    },
456    features: UPLEVEL.features.union(Features::SHADER_F16),
457};
458
459// e.g. GeForce GTX 1050, Radeon WX 5100
460const BUCKET_NO_F16: Bucket = Bucket {
461    name: "no-f16",
462    limits: Limits {
463        max_color_attachment_bytes_per_sample: 128,
464        max_compute_workgroup_storage_size: 48 << 10, // 48 kB
465        max_sampled_textures_per_shader_stage: 48,
466        max_storage_buffer_binding_size: 1 << 30, // 1 GB
467        max_storage_buffers_per_shader_stage: 16,
468        max_vertex_attributes: 30,
469        ..UPLEVEL.limits
470    },
471    info: BucketedAdapterInfo {
472        subgroup_min_size: 32,
473        subgroup_max_size: 64,
474        ..UPLEVEL.info
475    },
476    features: UPLEVEL.features,
477};
478
479const BUCKET_LLVMPIPE: Bucket = Bucket {
480    name: "llvmpipe",
481    limits: Limits {
482        max_color_attachment_bytes_per_sample: 128,
483        max_sampled_textures_per_shader_stage: 48,
484        max_storage_buffers_per_shader_stage: 16,
485        max_vertex_attributes: 32,
486        ..UPLEVEL.limits
487    },
488    info: BucketedAdapterInfo {
489        is_fallback_adapter: true,
490        subgroup_min_size: 8,
491        subgroup_max_size: 8,
492    },
493    features: UPLEVEL
494        .features
495        .union(Features::SHADER_F16)
496        .union(Features::CLIP_DISTANCES),
497};
498
499// a.k.a. Microsoft Basic Render Driver
500const BUCKET_WARP: Bucket = Bucket {
501    name: "warp",
502    limits: Limits {
503        max_color_attachment_bytes_per_sample: 128,
504        max_sampled_textures_per_shader_stage: 48,
505        max_storage_buffers_per_shader_stage: 16,
506        max_vertex_attributes: 30,
507        ..UPLEVEL.limits
508    },
509    info: BucketedAdapterInfo {
510        is_fallback_adapter: true,
511        subgroup_min_size: 4,
512        subgroup_max_size: 128,
513    },
514    features: UPLEVEL.features.union(Features::SHADER_F16),
515};
516
517// WebGPU default limits, not a fallback adapter
518const BUCKET_DEFAULT: Bucket = Bucket {
519    name: "default",
520    limits: Limits::defaults(),
521    info: BucketedAdapterInfo::defaults(),
522    features: Features::empty(),
523};
524
525// WebGPU default limits, is a fallback adapter
526const BUCKET_FALLBACK: Bucket = Bucket {
527    name: "fallback",
528    limits: Limits::defaults(),
529    info: BucketedAdapterInfo {
530        is_fallback_adapter: true,
531        ..BucketedAdapterInfo::defaults()
532    },
533    features: Features::empty(),
534};
535
536#[cfg(test)]
537mod tests {
538    use super::*;
539    use wgt::Features;
540
541    #[test]
542    fn enumerate_webgpu_features() {
543        let difference = Features::all_webgpu_mask().difference(
544            Features::DEPTH_CLIP_CONTROL
545                .union(Features::DEPTH32FLOAT_STENCIL8)
546                .union(Features::TEXTURE_COMPRESSION_ASTC)
547                .union(Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D)
548                .union(Features::TEXTURE_COMPRESSION_BC)
549                .union(Features::TEXTURE_COMPRESSION_BC_SLICED_3D)
550                .union(Features::TEXTURE_COMPRESSION_ETC2)
551                .union(Features::TIMESTAMP_QUERY)
552                .union(Features::INDIRECT_FIRST_INSTANCE)
553                .union(Features::SHADER_F16)
554                .union(Features::RG11B10UFLOAT_RENDERABLE)
555                .union(Features::BGRA8UNORM_STORAGE)
556                .union(Features::FLOAT32_FILTERABLE)
557                .union(Features::FLOAT32_BLENDABLE)
558                .union(Features::CLIP_DISTANCES)
559                .union(Features::DUAL_SOURCE_BLENDING)
560                .union(Features::SUBGROUP)
561                //.union(Features::TEXTURE_FORMATS_TIER1) not implemented
562                //.union(Features::TEXTURE_FORMATS_TIER2) not implemented
563                .union(Features::PRIMITIVE_INDEX)
564                //.union(Features::TEXTURE_COMPONENT_SWIZZLE) not implemented
565                // Standard-track features not in official spec
566                .union(Features::IMMEDIATES),
567        );
568        assert!(
569            difference.is_empty(),
570            "New WebGPU features should be assigned to appropriate limit buckets; missing {difference:?}"
571        );
572    }
573
574    #[test]
575    fn relationships() {
576        // Check that each bucket is a superset of UPLEVEL, ignoring the `is_fallback_adapter` flag.
577        for bucket in [
578            &BUCKET_M1,
579            &BUCKET_A2,
580            &BUCKET_I1,
581            &BUCKET_N1,
582            &BUCKET_A1,
583            &BUCKET_NO_F16,
584            &BUCKET_WARP,
585            &BUCKET_LLVMPIPE,
586        ] {
587            let info = AdapterInfo {
588                subgroup_min_size: bucket.info.subgroup_min_size,
589                subgroup_max_size: bucket.info.subgroup_max_size,
590                ..AdapterInfo::new(
591                    DeviceType::DiscreteGpu, // not a fallback adapter
592                    wgt::Backend::Noop,
593                )
594            };
595            assert!(
596                UPLEVEL.is_compatible(&bucket.limits, &info, bucket.features),
597                "Bucket `{}` should be a superset of UPLEVEL",
598                bucket.name(),
599            );
600        }
601    }
602}