script/dom/webxr/
xrwebgllayer.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::convert::TryInto;
6
7use dom_struct::dom_struct;
8use euclid::{Rect, Size2D};
9use js::rust::HandleObject;
10use servo_canvas_traits::webgl::{WebGLCommand, WebGLContextId, WebGLTextureId};
11use webxr_api::{ContextId as WebXRContextId, LayerId, LayerInit, Viewport};
12
13use crate::canvas_context::CanvasContext;
14use crate::conversions::Convert;
15use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants as constants;
16use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextMethods;
17use crate::dom::bindings::codegen::Bindings::XRWebGLLayerBinding::{
18    XRWebGLLayerInit, XRWebGLLayerMethods, XRWebGLRenderingContext,
19};
20use crate::dom::bindings::error::{Error, Fallible};
21use crate::dom::bindings::inheritance::Castable;
22use crate::dom::bindings::num::Finite;
23use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object_with_proto};
24use crate::dom::bindings::root::{Dom, DomRoot};
25use crate::dom::globalscope::GlobalScope;
26use crate::dom::webgl::webglframebuffer::WebGLFramebuffer;
27use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
28use crate::dom::webgl::webgltexture::WebGLTexture;
29use crate::dom::window::Window;
30use crate::dom::xrframe::XRFrame;
31use crate::dom::xrlayer::XRLayer;
32use crate::dom::xrsession::XRSession;
33use crate::dom::xrview::XRView;
34use crate::dom::xrviewport::XRViewport;
35use crate::script_runtime::CanGc;
36
37impl Convert<LayerInit> for XRWebGLLayerInit {
38    fn convert(self) -> LayerInit {
39        LayerInit::WebGLLayer {
40            alpha: self.alpha,
41            antialias: self.antialias,
42            depth: self.depth,
43            stencil: self.stencil,
44            framebuffer_scale_factor: *self.framebufferScaleFactor as f32,
45            ignore_depth_values: self.ignoreDepthValues,
46        }
47    }
48}
49
50#[dom_struct]
51pub(crate) struct XRWebGLLayer {
52    xr_layer: XRLayer,
53    antialias: bool,
54    depth: bool,
55    stencil: bool,
56    alpha: bool,
57    ignore_depth_values: bool,
58    /// If none, this is an inline session (the composition disabled flag is true)
59    framebuffer: Option<Dom<WebGLFramebuffer>>,
60}
61
62impl XRWebGLLayer {
63    pub(crate) fn new_inherited(
64        session: &XRSession,
65        context: &WebGLRenderingContext,
66        init: &XRWebGLLayerInit,
67        framebuffer: Option<&WebGLFramebuffer>,
68        layer_id: Option<LayerId>,
69    ) -> XRWebGLLayer {
70        XRWebGLLayer {
71            xr_layer: XRLayer::new_inherited(session, context, layer_id),
72            antialias: init.antialias,
73            depth: init.depth,
74            stencil: init.stencil,
75            alpha: init.alpha,
76            ignore_depth_values: init.ignoreDepthValues,
77            framebuffer: framebuffer.map(Dom::from_ref),
78        }
79    }
80
81    #[expect(clippy::too_many_arguments)]
82    fn new(
83        global: &GlobalScope,
84        proto: Option<HandleObject>,
85        session: &XRSession,
86        context: &WebGLRenderingContext,
87        init: &XRWebGLLayerInit,
88        framebuffer: Option<&WebGLFramebuffer>,
89        layer_id: Option<LayerId>,
90        can_gc: CanGc,
91    ) -> DomRoot<XRWebGLLayer> {
92        reflect_dom_object_with_proto(
93            Box::new(XRWebGLLayer::new_inherited(
94                session,
95                context,
96                init,
97                framebuffer,
98                layer_id,
99            )),
100            global,
101            proto,
102            can_gc,
103        )
104    }
105
106    pub(crate) fn layer_id(&self) -> Option<LayerId> {
107        self.xr_layer.layer_id()
108    }
109
110    pub(crate) fn context_id(&self) -> WebGLContextId {
111        self.xr_layer.context_id()
112    }
113
114    pub(crate) fn session(&self) -> &XRSession {
115        self.xr_layer.session()
116    }
117
118    pub(crate) fn size(&self) -> Size2D<u32, Viewport> {
119        if let Some(framebuffer) = self.framebuffer.as_ref() {
120            let size = framebuffer.size().unwrap_or((0, 0));
121            Size2D::new(
122                size.0.try_into().unwrap_or(0),
123                size.1.try_into().unwrap_or(0),
124            )
125        } else {
126            Size2D::from_untyped(self.context().size())
127        }
128    }
129
130    fn texture_target(&self) -> u32 {
131        if cfg!(target_os = "macos") {
132            glow::TEXTURE_RECTANGLE
133        } else {
134            glow::TEXTURE_2D
135        }
136    }
137
138    pub(crate) fn begin_frame(&self, frame: &XRFrame) -> Option<()> {
139        debug!("XRWebGLLayer begin frame");
140        let framebuffer = self.framebuffer.as_ref()?;
141        let sub_images = frame.get_sub_images(self.layer_id()?)?;
142        let session = self.session();
143        let context = framebuffer.upcast().context()?;
144
145        // TODO: Cache this texture
146        let color_texture_id = WebGLTextureId::new(sub_images.sub_image.as_ref()?.color_texture?);
147        let color_texture = WebGLTexture::new_webxr(
148            &context,
149            color_texture_id,
150            session,
151            CanGc::deprecated_note(),
152        );
153        let target = self.texture_target();
154
155        // Save the current bindings
156        let saved_framebuffer = context.get_draw_framebuffer_slot().get();
157        let saved_framebuffer_target = framebuffer.target();
158        let saved_texture_id = context
159            .textures()
160            .active_texture_slot(target, context.webgl_version())
161            .ok()
162            .and_then(|slot| slot.get().map(|texture| texture.id()));
163
164        // We have to pick a framebuffer target.
165        // If there is a draw framebuffer, we use its target,
166        // otherwise we just use DRAW_FRAMEBUFFER.
167        let framebuffer_target = saved_framebuffer
168            .as_ref()
169            .and_then(|fb| fb.target())
170            .unwrap_or(constants::DRAW_FRAMEBUFFER);
171
172        // Update the attachments
173        context.send_command(WebGLCommand::BindTexture(target, Some(color_texture_id)));
174        framebuffer.bind(framebuffer_target);
175        framebuffer
176            .texture2d_even_if_opaque(
177                constants::COLOR_ATTACHMENT0,
178                self.texture_target(),
179                Some(&color_texture),
180                0,
181            )
182            .ok()?;
183        if let Some(id) = sub_images.sub_image.as_ref()?.depth_stencil_texture {
184            // TODO: Cache this texture
185            let depth_stencil_texture_id = WebGLTextureId::new(id);
186            let depth_stencil_texture = WebGLTexture::new_webxr(
187                &context,
188                depth_stencil_texture_id,
189                session,
190                CanGc::deprecated_note(),
191            );
192            framebuffer
193                .texture2d_even_if_opaque(
194                    constants::DEPTH_STENCIL_ATTACHMENT,
195                    constants::TEXTURE_2D,
196                    Some(&depth_stencil_texture),
197                    0,
198                )
199                .ok()?;
200        }
201
202        // Restore the old bindings
203        context.send_command(WebGLCommand::BindTexture(target, saved_texture_id));
204        if let Some(framebuffer_target) = saved_framebuffer_target {
205            framebuffer.bind(framebuffer_target);
206        }
207        if let Some(framebuffer) = saved_framebuffer {
208            framebuffer.bind(framebuffer_target);
209        }
210        Some(())
211    }
212
213    pub(crate) fn end_frame(&self, _frame: &XRFrame) -> Option<()> {
214        debug!("XRWebGLLayer end frame");
215        // TODO: invalidate the old texture
216        let framebuffer = self.framebuffer.as_ref()?;
217        // TODO: rebind the current bindings
218        framebuffer.bind(constants::FRAMEBUFFER);
219        framebuffer
220            .texture2d_even_if_opaque(constants::COLOR_ATTACHMENT0, self.texture_target(), None, 0)
221            .ok()?;
222        framebuffer
223            .texture2d_even_if_opaque(
224                constants::DEPTH_STENCIL_ATTACHMENT,
225                constants::DEPTH_STENCIL_ATTACHMENT,
226                None,
227                0,
228            )
229            .ok()?;
230
231        if let Some(context) = framebuffer.upcast().context() {
232            context.Flush();
233        }
234
235        Some(())
236    }
237
238    pub(crate) fn context(&self) -> &WebGLRenderingContext {
239        self.xr_layer.context()
240    }
241}
242
243impl XRWebGLLayerMethods<crate::DomTypeHolder> for XRWebGLLayer {
244    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-xrwebgllayer>
245    fn Constructor(
246        global: &Window,
247        proto: Option<HandleObject>,
248        can_gc: CanGc,
249        session: &XRSession,
250        context: XRWebGLRenderingContext,
251        init: &XRWebGLLayerInit,
252    ) -> Fallible<DomRoot<Self>> {
253        let context = match context {
254            XRWebGLRenderingContext::WebGLRenderingContext(ctx) => ctx,
255            XRWebGLRenderingContext::WebGL2RenderingContext(ctx) => ctx.base_context(),
256        };
257
258        // Step 2
259        if session.is_ended() {
260            return Err(Error::InvalidState(None));
261        }
262        // XXXManishearth step 3: throw error if context is lost
263        // XXXManishearth step 4: check XR compat flag for immersive sessions
264
265        let (framebuffer, layer_id) = if session.is_immersive() {
266            // Step 9.2. "Initialize layer’s framebuffer to a new opaque framebuffer created with context."
267            let size = session
268                .with_session(|session| session.recommended_framebuffer_resolution())
269                .ok_or(Error::Operation(None))?;
270            let framebuffer = WebGLFramebuffer::maybe_new_webxr(session, &context, size, can_gc)
271                .ok_or(Error::Operation(None))?;
272
273            // Step 9.3. "Allocate and initialize resources compatible with session’s XR device,
274            // including GPU accessible memory buffers, as required to support the compositing of layer."
275            let context_id = WebXRContextId::from(context.context_id());
276            let layer_init: LayerInit = init.convert();
277            let layer_id = session
278                .with_session(|session| session.create_layer(context_id, layer_init))
279                .map_err(|_| Error::Operation(None))?;
280
281            // Step 9.4: "If layer’s resources were unable to be created for any reason,
282            // throw an OperationError and abort these steps."
283            (Some(framebuffer), Some(layer_id))
284        } else {
285            (None, None)
286        };
287
288        // Ensure that we finish setting up this layer before continuing.
289        context.Finish();
290
291        // Step 10. "Return layer."
292        Ok(XRWebGLLayer::new(
293            &global.global(),
294            proto,
295            session,
296            &context,
297            init,
298            framebuffer.as_deref(),
299            layer_id,
300            can_gc,
301        ))
302    }
303
304    /// <https://www.w3.org/TR/webxr/#dom-xrwebgllayer-getnativeframebufferscalefactor>
305    fn GetNativeFramebufferScaleFactor(_window: &Window, session: &XRSession) -> Finite<f64> {
306        let value: f64 = if session.is_ended() { 0.0 } else { 1.0 };
307        Finite::wrap(value)
308    }
309
310    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-antialias>
311    fn Antialias(&self) -> bool {
312        self.antialias
313    }
314
315    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-ignoredepthvalues>
316    fn IgnoreDepthValues(&self) -> bool {
317        self.ignore_depth_values
318    }
319
320    /// <https://www.w3.org/TR/webxr/#dom-xrwebgllayer-fixedfoveation>
321    fn GetFixedFoveation(&self) -> Option<Finite<f32>> {
322        // Fixed foveation is only available on Quest/Pico headset runtimes
323        None
324    }
325
326    /// <https://www.w3.org/TR/webxr/#dom-xrwebgllayer-fixedfoveation>
327    fn SetFixedFoveation(&self, _value: Option<Finite<f32>>) {
328        // no-op until fixed foveation is supported
329    }
330
331    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-framebuffer>
332    fn GetFramebuffer(&self) -> Option<DomRoot<WebGLFramebuffer>> {
333        self.framebuffer.as_ref().map(|x| DomRoot::from_ref(&**x))
334    }
335
336    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-framebufferwidth>
337    fn FramebufferWidth(&self) -> u32 {
338        self.size().width
339    }
340
341    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-framebufferheight>
342    fn FramebufferHeight(&self) -> u32 {
343        self.size().height
344    }
345
346    /// <https://immersive-web.github.io/webxr/#dom-xrwebgllayer-getviewport>
347    fn GetViewport(&self, view: &XRView) -> Option<DomRoot<XRViewport>> {
348        if self.session() != view.session() {
349            return None;
350        }
351
352        let index = view.viewport_index();
353
354        let viewport = self.session().with_session(|s| {
355            // Inline sessions
356            if s.viewports().is_empty() {
357                Rect::from_size(self.size().to_i32())
358            } else {
359                s.viewports()[index]
360            }
361        });
362
363        // NOTE: According to spec, viewport sizes should be recalculated here if the
364        // requested viewport scale has changed. However, existing browser implementations
365        // don't seem to do this for stereoscopic immersive sessions.
366        // Revisit if Servo gets support for handheld AR/VR via ARCore/ARKit
367
368        Some(XRViewport::new(
369            &self.global(),
370            viewport,
371            CanGc::deprecated_note(),
372        ))
373    }
374}