Skip to main content

script/dom/webgpu/
gpusupportedfeatures.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5// check-tidy: no specs after this line
6
7use dom_struct::dom_struct;
8use indexmap::IndexSet;
9use js::rust::HandleObject;
10use script_bindings::cell::DomRefCell;
11use script_bindings::like::Setlike;
12use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto};
13use wgpu_types::Features;
14
15use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
16    GPUFeatureName, GPUSupportedFeaturesMethods,
17};
18use crate::dom::bindings::error::Fallible;
19use crate::dom::bindings::root::DomRoot;
20use crate::dom::bindings::str::DOMString;
21use crate::dom::globalscope::GlobalScope;
22use crate::script_runtime::CanGc;
23
24#[dom_struct]
25pub(crate) struct GPUSupportedFeatures {
26    reflector: Reflector,
27    // internal storage for features
28    #[custom_trace]
29    internal: DomRefCell<IndexSet<GPUFeatureName>>,
30    #[ignore_malloc_size_of = "defined in wgpu-types"]
31    #[no_trace]
32    features: Features,
33}
34
35impl GPUSupportedFeatures {
36    fn new(
37        global: &GlobalScope,
38        proto: Option<HandleObject>,
39        features: Features,
40        can_gc: CanGc,
41    ) -> DomRoot<GPUSupportedFeatures> {
42        let mut set = IndexSet::new();
43        // everything that wgpu currently does is considered as part of "core"
44        set.insert(GPUFeatureName::Core_features_and_limits);
45        if features.contains(Features::DEPTH_CLIP_CONTROL) {
46            set.insert(GPUFeatureName::Depth_clip_control);
47        }
48        if features.contains(Features::DEPTH32FLOAT_STENCIL8) {
49            set.insert(GPUFeatureName::Depth32float_stencil8);
50        }
51        if features.contains(Features::TEXTURE_COMPRESSION_BC) {
52            set.insert(GPUFeatureName::Texture_compression_bc);
53        }
54        // TODO: texture-compression-bc-sliced-3d when wgpu supports it
55        if features.contains(Features::TEXTURE_COMPRESSION_ETC2) {
56            set.insert(GPUFeatureName::Texture_compression_etc2);
57        }
58        if features.contains(Features::TEXTURE_COMPRESSION_ASTC) {
59            set.insert(GPUFeatureName::Texture_compression_astc);
60        }
61        if features.contains(Features::TIMESTAMP_QUERY) {
62            set.insert(GPUFeatureName::Timestamp_query);
63        }
64        if features.contains(Features::INDIRECT_FIRST_INSTANCE) {
65            set.insert(GPUFeatureName::Indirect_first_instance);
66        }
67        // While this feature exists in wgpu, it's not supported by naga yet
68        // https://github.com/gfx-rs/wgpu/issues/4384
69        /*
70        if features.contains(Features::SHADER_F16) {
71            set.insert(GPUFeatureName::Shader_f16);
72        }
73        */
74        if features.contains(Features::RG11B10UFLOAT_RENDERABLE) {
75            set.insert(GPUFeatureName::Rg11b10ufloat_renderable);
76        }
77        if features.contains(Features::BGRA8UNORM_STORAGE) {
78            set.insert(GPUFeatureName::Bgra8unorm_storage);
79        }
80        if features.contains(Features::FLOAT32_FILTERABLE) {
81            set.insert(GPUFeatureName::Float32_filterable);
82        }
83        // TODO: clip-distances when wgpu supports it
84        if features.contains(Features::DUAL_SOURCE_BLENDING) {
85            set.insert(GPUFeatureName::Dual_source_blending);
86        }
87        // While this feature exists in wgpu, it's not supported by naga yet
88        // https://github.com/gfx-rs/wgpu/issues/5555
89        /*
90        if features.contains(Features::SUBGROUP) {
91            set.insert(GPUFeatureName::Subgroups);
92        }
93        */
94
95        reflect_dom_object_with_proto(
96            Box::new(GPUSupportedFeatures {
97                reflector: Reflector::new(),
98                internal: DomRefCell::new(set),
99                features,
100            }),
101            global,
102            proto,
103            can_gc,
104        )
105    }
106
107    #[expect(non_snake_case)]
108    pub(crate) fn Constructor(
109        global: &GlobalScope,
110        proto: Option<HandleObject>,
111        features: Features,
112        can_gc: CanGc,
113    ) -> Fallible<DomRoot<GPUSupportedFeatures>> {
114        Ok(GPUSupportedFeatures::new(global, proto, features, can_gc))
115    }
116}
117
118impl GPUSupportedFeatures {
119    pub(crate) fn wgpu_features(&self) -> &Features {
120        &self.features
121    }
122}
123
124impl GPUSupportedFeaturesMethods<crate::DomTypeHolder> for GPUSupportedFeatures {
125    fn Size(&self) -> u32 {
126        self.internal.size()
127    }
128}
129
130pub(crate) fn gpu_to_wgt_feature(feature: GPUFeatureName) -> Option<Features> {
131    match feature {
132        // everything that wgpu currently does is considered as part of "core"
133        GPUFeatureName::Core_features_and_limits => Some(Features::empty()),
134        GPUFeatureName::Depth_clip_control => Some(Features::DEPTH_CLIP_CONTROL),
135        GPUFeatureName::Depth32float_stencil8 => Some(Features::DEPTH32FLOAT_STENCIL8),
136        GPUFeatureName::Texture_compression_bc => Some(Features::TEXTURE_COMPRESSION_BC),
137        GPUFeatureName::Texture_compression_etc2 => Some(Features::TEXTURE_COMPRESSION_ETC2),
138        GPUFeatureName::Texture_compression_astc => Some(Features::TEXTURE_COMPRESSION_ASTC),
139        GPUFeatureName::Timestamp_query => Some(Features::TIMESTAMP_QUERY),
140        GPUFeatureName::Indirect_first_instance => Some(Features::INDIRECT_FIRST_INSTANCE),
141        // While this feature exists in wgpu, it's not supported by naga yet
142        // https://github.com/gfx-rs/wgpu/issues/4384
143        GPUFeatureName::Shader_f16 => None,
144        GPUFeatureName::Rg11b10ufloat_renderable => Some(Features::RG11B10UFLOAT_RENDERABLE),
145        GPUFeatureName::Bgra8unorm_storage => Some(Features::BGRA8UNORM_STORAGE),
146        GPUFeatureName::Float32_filterable => Some(Features::FLOAT32_FILTERABLE),
147        GPUFeatureName::Dual_source_blending => Some(Features::DUAL_SOURCE_BLENDING),
148        GPUFeatureName::Texture_compression_bc_sliced_3d => None,
149        GPUFeatureName::Clip_distances => None,
150        // While this feature exists in wgpu, it's not supported by naga yet
151        // https://github.com/gfx-rs/wgpu/issues/5555
152        GPUFeatureName::Subgroups => None,
153    }
154}
155
156impl Setlike for GPUSupportedFeatures {
157    type Key = DOMString;
158
159    #[inline(always)]
160    fn get_index(&self, index: u32) -> Option<Self::Key> {
161        self.internal
162            .get_index(index)
163            .map(|key| key.as_str().into())
164    }
165    #[inline(always)]
166    fn size(&self) -> u32 {
167        self.internal.size()
168    }
169    #[inline(always)]
170    fn add(&self, _key: Self::Key) {
171        unreachable!("readonly");
172    }
173    #[inline(always)]
174    fn has(&self, key: Self::Key) -> bool {
175        if let Ok(key) = key.parse() {
176            self.internal.has(key)
177        } else {
178            false
179        }
180    }
181    #[inline(always)]
182    fn clear(&self) {
183        unreachable!("readonly");
184    }
185    #[inline(always)]
186    fn delete(&self, _key: Self::Key) -> bool {
187        unreachable!("readonly");
188    }
189}