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 wgpu_types::Features;
11
12use crate::dom::bindings::cell::DomRefCell;
13use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
14    GPUFeatureName, GPUSupportedFeaturesMethods,
15};
16use crate::dom::bindings::error::Fallible;
17use crate::dom::bindings::like::Setlike;
18use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
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        if features.contains(Features::DEPTH_CLIP_CONTROL) {
44            set.insert(GPUFeatureName::Depth_clip_control);
45        }
46        if features.contains(Features::DEPTH32FLOAT_STENCIL8) {
47            set.insert(GPUFeatureName::Depth32float_stencil8);
48        }
49        if features.contains(Features::TEXTURE_COMPRESSION_BC) {
50            set.insert(GPUFeatureName::Texture_compression_bc);
51        }
52        // TODO: texture-compression-bc-sliced-3d when wgpu supports it
53        if features.contains(Features::TEXTURE_COMPRESSION_ETC2) {
54            set.insert(GPUFeatureName::Texture_compression_etc2);
55        }
56        if features.contains(Features::TEXTURE_COMPRESSION_ASTC) {
57            set.insert(GPUFeatureName::Texture_compression_astc);
58        }
59        if features.contains(Features::TIMESTAMP_QUERY) {
60            set.insert(GPUFeatureName::Timestamp_query);
61        }
62        if features.contains(Features::INDIRECT_FIRST_INSTANCE) {
63            set.insert(GPUFeatureName::Indirect_first_instance);
64        }
65        // While this feature exists in wgpu, it's not supported by naga yet
66        // https://github.com/gfx-rs/wgpu/issues/4384
67        /*
68        if features.contains(Features::SHADER_F16) {
69            set.insert(GPUFeatureName::Shader_f16);
70        }
71        */
72        if features.contains(Features::RG11B10UFLOAT_RENDERABLE) {
73            set.insert(GPUFeatureName::Rg11b10ufloat_renderable);
74        }
75        if features.contains(Features::BGRA8UNORM_STORAGE) {
76            set.insert(GPUFeatureName::Bgra8unorm_storage);
77        }
78        if features.contains(Features::FLOAT32_FILTERABLE) {
79            set.insert(GPUFeatureName::Float32_filterable);
80        }
81        // TODO: clip-distances when wgpu supports it
82        if features.contains(Features::DUAL_SOURCE_BLENDING) {
83            set.insert(GPUFeatureName::Dual_source_blending);
84        }
85
86        reflect_dom_object_with_proto(
87            Box::new(GPUSupportedFeatures {
88                reflector: Reflector::new(),
89                internal: DomRefCell::new(set),
90                features,
91            }),
92            global,
93            proto,
94            can_gc,
95        )
96    }
97
98    #[allow(non_snake_case)]
99    pub(crate) fn Constructor(
100        global: &GlobalScope,
101        proto: Option<HandleObject>,
102        features: Features,
103        can_gc: CanGc,
104    ) -> Fallible<DomRoot<GPUSupportedFeatures>> {
105        Ok(GPUSupportedFeatures::new(global, proto, features, can_gc))
106    }
107}
108
109impl GPUSupportedFeatures {
110    pub(crate) fn wgpu_features(&self) -> Features {
111        self.features
112    }
113}
114
115impl GPUSupportedFeaturesMethods<crate::DomTypeHolder> for GPUSupportedFeatures {
116    fn Size(&self) -> u32 {
117        self.internal.size()
118    }
119}
120
121pub(crate) fn gpu_to_wgt_feature(feature: GPUFeatureName) -> Option<Features> {
122    match feature {
123        GPUFeatureName::Depth_clip_control => Some(Features::DEPTH_CLIP_CONTROL),
124        GPUFeatureName::Depth32float_stencil8 => Some(Features::DEPTH32FLOAT_STENCIL8),
125        GPUFeatureName::Texture_compression_bc => Some(Features::TEXTURE_COMPRESSION_BC),
126        GPUFeatureName::Texture_compression_etc2 => Some(Features::TEXTURE_COMPRESSION_ETC2),
127        GPUFeatureName::Texture_compression_astc => Some(Features::TEXTURE_COMPRESSION_ASTC),
128        GPUFeatureName::Timestamp_query => Some(Features::TIMESTAMP_QUERY),
129        GPUFeatureName::Indirect_first_instance => Some(Features::INDIRECT_FIRST_INSTANCE),
130        // While this feature exists in wgpu, it's not supported by naga yet
131        // https://github.com/gfx-rs/wgpu/issues/4384
132        GPUFeatureName::Shader_f16 => None,
133        GPUFeatureName::Rg11b10ufloat_renderable => Some(Features::RG11B10UFLOAT_RENDERABLE),
134        GPUFeatureName::Bgra8unorm_storage => Some(Features::BGRA8UNORM_STORAGE),
135        GPUFeatureName::Float32_filterable => Some(Features::FLOAT32_FILTERABLE),
136        GPUFeatureName::Dual_source_blending => Some(Features::DUAL_SOURCE_BLENDING),
137        GPUFeatureName::Texture_compression_bc_sliced_3d => None,
138        GPUFeatureName::Clip_distances => None,
139    }
140}
141
142impl Setlike for GPUSupportedFeatures {
143    type Key = DOMString;
144
145    #[inline(always)]
146    fn get_index(&self, index: u32) -> Option<Self::Key> {
147        self.internal
148            .get_index(index)
149            .map(|k| DOMString::from_string(k.as_str().to_owned()))
150    }
151    #[inline(always)]
152    fn size(&self) -> u32 {
153        self.internal.size()
154    }
155    #[inline(always)]
156    fn add(&self, _key: Self::Key) {
157        unreachable!("readonly");
158    }
159    #[inline(always)]
160    fn has(&self, key: Self::Key) -> bool {
161        if let Ok(key) = key.parse() {
162            self.internal.has(key)
163        } else {
164            false
165        }
166    }
167    #[inline(always)]
168    fn clear(&self) {
169        unreachable!("readonly");
170    }
171    #[inline(always)]
172    fn delete(&self, _key: Self::Key) -> bool {
173        unreachable!("readonly");
174    }
175}