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.
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        // wgpu does not implement 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        // FLOAT32_BLENDABLE not implemented in wgpu dx12 backend; https://github.com/gfx-rs/wgpu/issues/6555
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::FLOAT32_BLENDABLE)
380        .union(Features::CLIP_DISTANCES),
381};
382
383// e.g. Radeon Vega
384const BUCKET_A2: Bucket = Bucket {
385    name: "a2",
386    limits: Limits {
387        max_color_attachment_bytes_per_sample: 128,
388        max_compute_workgroup_storage_size: 64 << 10, // 64 kB,
389        max_sampled_textures_per_shader_stage: 48,
390        max_storage_buffer_binding_size: 1 << 30, // 1 GB,
391        max_storage_buffers_per_shader_stage: 16,
392        max_vertex_attributes: 32,
393        ..UPLEVEL.limits
394    },
395    info: BucketedAdapterInfo {
396        subgroup_min_size: 64,
397        subgroup_max_size: 64,
398        ..UPLEVEL.info
399    },
400    features: UPLEVEL.features.union(Features::SHADER_F16),
401};
402
403// e.g. Intel Arc, UHD 600 Series, Iris Xe
404const BUCKET_I1: Bucket = Bucket {
405    name: "i1",
406    limits: Limits {
407        max_color_attachment_bytes_per_sample: 128,
408        max_sampled_textures_per_shader_stage: 48,
409        max_storage_buffer_binding_size: 1 << 29, // 512 MB,
410        max_storage_buffers_per_shader_stage: 16,
411        ..UPLEVEL.limits
412    },
413    info: BucketedAdapterInfo {
414        subgroup_min_size: 8,
415        subgroup_max_size: 32,
416        ..UPLEVEL.info
417    },
418    features: UPLEVEL.features.union(Features::SHADER_F16),
419};
420
421// e.g. GeForce GTX 1650, GeForce RTX 20, 30, 40, 50 Series
422const BUCKET_N1: Bucket = Bucket {
423    name: "n1",
424    limits: Limits {
425        max_color_attachment_bytes_per_sample: 128,
426        max_compute_workgroup_storage_size: 48 << 10, // 48 kB,
427        max_sampled_textures_per_shader_stage: 48,
428        max_storage_buffer_binding_size: 1 << 30, // 1 GB,
429        max_storage_buffers_per_shader_stage: 16,
430        max_vertex_attributes: 32,
431        ..UPLEVEL.limits
432    },
433    info: BucketedAdapterInfo {
434        subgroup_min_size: 32,
435        subgroup_max_size: 32,
436        ..UPLEVEL.info
437    },
438    features: UPLEVEL.features.union(Features::SHADER_F16),
439};
440
441// e.g. Radeon RX 6000, 7000, 9000 Series
442const BUCKET_A1: Bucket = Bucket {
443    name: "a1",
444    limits: Limits {
445        max_color_attachment_bytes_per_sample: 128,
446        max_sampled_textures_per_shader_stage: 48,
447        max_storage_buffer_binding_size: 1 << 30, // 1 GB,
448        max_storage_buffers_per_shader_stage: 16,
449        max_vertex_attributes: 32,
450        ..UPLEVEL.limits
451    },
452    info: BucketedAdapterInfo {
453        subgroup_min_size: 32,
454        subgroup_max_size: 64,
455        ..UPLEVEL.info
456    },
457    features: UPLEVEL.features.union(Features::SHADER_F16),
458};
459
460// e.g. GeForce GTX 1050, Radeon WX 5100
461const BUCKET_NO_F16: Bucket = Bucket {
462    name: "no-f16",
463    limits: Limits {
464        max_color_attachment_bytes_per_sample: 128,
465        max_compute_workgroup_storage_size: 48 << 10, // 48 kB
466        max_sampled_textures_per_shader_stage: 48,
467        max_storage_buffer_binding_size: 1 << 30, // 1 GB
468        max_storage_buffers_per_shader_stage: 16,
469        max_vertex_attributes: 32,
470        ..UPLEVEL.limits
471    },
472    info: BucketedAdapterInfo {
473        subgroup_min_size: 32,
474        subgroup_max_size: 64,
475        ..UPLEVEL.info
476    },
477    features: UPLEVEL.features,
478};
479
480const BUCKET_LLVMPIPE: Bucket = Bucket {
481    name: "llvmpipe",
482    limits: Limits {
483        max_color_attachment_bytes_per_sample: 128,
484        max_sampled_textures_per_shader_stage: 48,
485        max_storage_buffers_per_shader_stage: 16,
486        max_vertex_attributes: 32,
487        ..UPLEVEL.limits
488    },
489    info: BucketedAdapterInfo {
490        is_fallback_adapter: true,
491        subgroup_min_size: 8,
492        subgroup_max_size: 8,
493    },
494    features: UPLEVEL
495        .features
496        .union(Features::SHADER_F16)
497        .union(Features::CLIP_DISTANCES),
498};
499
500// a.k.a. Microsoft Basic Render Driver
501const BUCKET_WARP: Bucket = Bucket {
502    name: "warp",
503    limits: Limits {
504        max_color_attachment_bytes_per_sample: 128,
505        max_sampled_textures_per_shader_stage: 48,
506        max_storage_buffers_per_shader_stage: 16,
507        max_vertex_attributes: 32,
508        ..UPLEVEL.limits
509    },
510    info: BucketedAdapterInfo {
511        is_fallback_adapter: true,
512        subgroup_min_size: 4,
513        subgroup_max_size: 128,
514    },
515    features: UPLEVEL.features.union(Features::SHADER_F16),
516};
517
518// WebGPU default limits, not a fallback adapter
519const BUCKET_DEFAULT: Bucket = Bucket {
520    name: "default",
521    limits: Limits::defaults(),
522    info: BucketedAdapterInfo::defaults(),
523    features: Features::empty(),
524};
525
526// WebGPU default limits, is a fallback adapter
527const BUCKET_FALLBACK: Bucket = Bucket {
528    name: "fallback",
529    limits: Limits::defaults(),
530    info: BucketedAdapterInfo {
531        is_fallback_adapter: true,
532        ..BucketedAdapterInfo::defaults()
533    },
534    features: Features::empty(),
535};
536
537#[cfg(test)]
538mod tests {
539    use super::*;
540    use wgt::Features;
541
542    #[test]
543    fn enumerate_webgpu_features() {
544        let difference = Features::all_webgpu_mask().difference(
545            Features::DEPTH_CLIP_CONTROL
546                .union(Features::DEPTH32FLOAT_STENCIL8)
547                .union(Features::TEXTURE_COMPRESSION_ASTC)
548                .union(Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D)
549                .union(Features::TEXTURE_COMPRESSION_BC)
550                .union(Features::TEXTURE_COMPRESSION_BC_SLICED_3D)
551                .union(Features::TEXTURE_COMPRESSION_ETC2)
552                .union(Features::TIMESTAMP_QUERY)
553                .union(Features::INDIRECT_FIRST_INSTANCE)
554                .union(Features::SHADER_F16)
555                .union(Features::RG11B10UFLOAT_RENDERABLE)
556                .union(Features::BGRA8UNORM_STORAGE)
557                .union(Features::FLOAT32_FILTERABLE)
558                .union(Features::FLOAT32_BLENDABLE)
559                .union(Features::CLIP_DISTANCES)
560                .union(Features::DUAL_SOURCE_BLENDING)
561                .union(Features::SUBGROUP)
562                //.union(Features::TEXTURE_FORMATS_TIER1) not implemented
563                //.union(Features::TEXTURE_FORMATS_TIER2) not implemented
564                .union(Features::PRIMITIVE_INDEX)
565                //.union(Features::TEXTURE_COMPONENT_SWIZZLE) not implemented
566                // Standard-track features not in official spec
567                .union(Features::IMMEDIATES),
568        );
569        assert!(
570            difference.is_empty(),
571            "New WebGPU features should be assigned to appropriate limit buckets; missing {difference:?}"
572        );
573    }
574
575    #[test]
576    fn relationships() {
577        // Check that each bucket is a superset of UPLEVEL, ignoring the `is_fallback_adapter` flag.
578        for bucket in [
579            &BUCKET_M1,
580            &BUCKET_A2,
581            &BUCKET_I1,
582            &BUCKET_N1,
583            &BUCKET_A1,
584            &BUCKET_NO_F16,
585            &BUCKET_WARP,
586            &BUCKET_LLVMPIPE,
587        ] {
588            let info = AdapterInfo {
589                subgroup_min_size: bucket.info.subgroup_min_size,
590                subgroup_max_size: bucket.info.subgroup_max_size,
591                ..AdapterInfo::new(
592                    DeviceType::DiscreteGpu, // not a fallback adapter
593                    wgt::Backend::Noop,
594                )
595            };
596            assert!(
597                UPLEVEL.is_compatible(&bucket.limits, &info, bucket.features),
598                "Bucket `{}` should be a superset of UPLEVEL",
599                bucket.name(),
600            );
601        }
602    }
603}