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