Skip to main content

script/dom/webgpu/
gpu.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
5use std::rc::Rc;
6
7use dom_struct::dom_struct;
8use js::jsapi::HandleObject;
9use js::realm::CurrentRealm;
10use script_bindings::reflector::{Reflector, reflect_dom_object};
11use servo_constellation_traits::ScriptToConstellationMessage;
12use webgpu_traits::WebGPUAdapterResponse;
13use wgpu_types::PowerPreference;
14
15use super::wgsllanguagefeatures::WGSLLanguageFeatures;
16use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
17    GPUMethods, GPUPowerPreference, GPURequestAdapterOptions, GPUTextureFormat,
18};
19use crate::dom::bindings::error::Error;
20use crate::dom::bindings::reflector::DomGlobal;
21use crate::dom::bindings::root::{DomRoot, MutNullableDom};
22use crate::dom::bindings::str::DOMString;
23use crate::dom::globalscope::GlobalScope;
24use crate::dom::promise::Promise;
25use crate::dom::webgpu::gpuadapter::GPUAdapter;
26use crate::routed_promise::{RoutedPromiseListener, callback_promise};
27use crate::script_runtime::CanGc;
28
29#[dom_struct]
30#[expect(clippy::upper_case_acronyms)]
31pub(crate) struct GPU {
32    reflector_: Reflector,
33    /// Same object for <https://www.w3.org/TR/webgpu/#dom-gpu-wgsllanguagefeatures>
34    wgsl_language_features: MutNullableDom<WGSLLanguageFeatures>,
35}
36
37impl GPU {
38    pub(crate) fn new_inherited() -> GPU {
39        GPU {
40            reflector_: Reflector::new(),
41            wgsl_language_features: MutNullableDom::default(),
42        }
43    }
44
45    pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<GPU> {
46        reflect_dom_object(Box::new(GPU::new_inherited()), global, can_gc)
47    }
48}
49
50impl GPUMethods<crate::DomTypeHolder> for GPU {
51    /// <https://gpuweb.github.io/gpuweb/#dom-gpu-requestadapter>
52    fn RequestAdapter(
53        &self,
54        cx: &mut CurrentRealm,
55        options: &GPURequestAdapterOptions,
56    ) -> Rc<Promise> {
57        let global = &self.global();
58        // 1. Let promise be a new promise.
59        let promise = Promise::new_in_realm(cx);
60        let task_source = global.task_manager().dom_manipulation_task_source();
61        let callback = callback_promise(&promise, self, task_source);
62
63        let power_preference = match options.powerPreference {
64            Some(GPUPowerPreference::Low_power) => PowerPreference::LowPower,
65            Some(GPUPowerPreference::High_performance) => PowerPreference::HighPerformance,
66            None => PowerPreference::default(),
67        };
68        let ids = global.wgpu_id_hub().create_adapter_id();
69
70        // 3. Issue the initialization steps on the Device timeline of this
71
72        /*
73        We do some steps here to avoid IPC round-trips
74        1. options.featureLevel must be a feature level string.
75        If any are unmet
76            Let adapter be null, issue the resolution steps on contentTimeline, and return.
77        If adapter is null:
78            Resolve promise with null.
79        */
80        match &*options.featureLevel.str() {
81            "core" => {},
82            "compatibility" => {
83                // Set options.featureLevel to "compatibility" if the user agent chooses to support it, or "core" if not.
84                // and wgpu does not support "compatibility" yet so we return core for now
85            },
86            _ => {
87                promise.resolve_native_with_cx(cx, &None::<GPUAdapter>);
88                return promise;
89            },
90        }
91        let script_to_constellation_chan = global.script_to_constellation_chan();
92        if script_to_constellation_chan
93            .send(ScriptToConstellationMessage::RequestAdapter(
94                callback,
95                wgpu_core::instance::RequestAdapterOptions {
96                    power_preference,
97                    compatible_surface: None,
98                    force_fallback_adapter: options.forceFallbackAdapter,
99                },
100                ids,
101            ))
102            .is_err()
103        {
104            promise.reject_error_with_cx(cx, Error::Operation(None));
105        }
106        // 4. Return promise
107        promise
108    }
109
110    /// <https://gpuweb.github.io/gpuweb/#dom-gpu-getpreferredcanvasformat>
111    fn GetPreferredCanvasFormat(&self) -> GPUTextureFormat {
112        // From https://github.com/mozilla-firefox/firefox/blob/24d49101ce17b78c3ba1217d00297fe2891be6b3/dom/webgpu/Instance.h#L68
113        if cfg!(target_os = "android") {
114            GPUTextureFormat::Rgba8unorm
115        } else {
116            GPUTextureFormat::Bgra8unorm
117        }
118    }
119
120    /// <https://www.w3.org/TR/webgpu/#dom-gpu-wgsllanguagefeatures>
121    fn WgslLanguageFeatures(
122        &self,
123        cx: &mut js::context::JSContext,
124    ) -> DomRoot<WGSLLanguageFeatures> {
125        self.wgsl_language_features
126            .or_init(|| WGSLLanguageFeatures::new(cx, &self.global(), None))
127    }
128}
129
130impl RoutedPromiseListener<WebGPUAdapterResponse> for GPU {
131    fn handle_response(
132        &self,
133        cx: &mut js::context::JSContext,
134        response: WebGPUAdapterResponse,
135        promise: &Rc<Promise>,
136    ) {
137        match response {
138            Some(Ok(adapter)) => {
139                let adapter = GPUAdapter::new(
140                    &self.global(),
141                    adapter.channel,
142                    DOMString::from(format!(
143                        "{} ({:?})",
144                        adapter.adapter_info.name, adapter.adapter_id.0
145                    )),
146                    HandleObject::null(),
147                    adapter.features,
148                    adapter.limits,
149                    adapter.adapter_info,
150                    adapter.adapter_id,
151                    CanGc::from_cx(cx),
152                );
153                promise.resolve_native_with_cx(cx, &adapter);
154            },
155            Some(Err(e)) => {
156                warn!("Could not get GPUAdapter ({:?})", e);
157                promise.resolve_native_with_cx(cx, &None::<GPUAdapter>);
158            },
159            None => {
160                warn!("Couldn't get a response, because WebGPU is disabled");
161                promise.resolve_native_with_cx(cx, &None::<GPUAdapter>);
162            },
163        }
164    }
165}