1use 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;
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::webglobject::WebGLObject;
28use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
29use crate::dom::webgl::webgltexture::WebGLTexture;
30use crate::dom::window::Window;
31use crate::dom::xrframe::XRFrame;
32use crate::dom::xrlayer::XRLayer;
33use crate::dom::xrsession::XRSession;
34use crate::dom::xrview::XRView;
35use crate::dom::xrviewport::XRViewport;
36use crate::script_runtime::CanGc;
37
38impl Convert<LayerInit> for XRWebGLLayerInit {
39 fn convert(self) -> LayerInit {
40 LayerInit::WebGLLayer {
41 alpha: self.alpha,
42 antialias: self.antialias,
43 depth: self.depth,
44 stencil: self.stencil,
45 framebuffer_scale_factor: *self.framebufferScaleFactor as f32,
46 ignore_depth_values: self.ignoreDepthValues,
47 }
48 }
49}
50
51#[dom_struct]
52pub(crate) struct XRWebGLLayer {
53 xr_layer: XRLayer,
54 antialias: bool,
55 depth: bool,
56 stencil: bool,
57 alpha: bool,
58 ignore_depth_values: bool,
59 framebuffer: Option<Dom<WebGLFramebuffer>>,
61}
62
63impl XRWebGLLayer {
64 pub(crate) fn new_inherited(
65 session: &XRSession,
66 context: &WebGLRenderingContext,
67 init: &XRWebGLLayerInit,
68 framebuffer: Option<&WebGLFramebuffer>,
69 layer_id: Option<LayerId>,
70 ) -> XRWebGLLayer {
71 XRWebGLLayer {
72 xr_layer: XRLayer::new_inherited(session, context, layer_id),
73 antialias: init.antialias,
74 depth: init.depth,
75 stencil: init.stencil,
76 alpha: init.alpha,
77 ignore_depth_values: init.ignoreDepthValues,
78 framebuffer: framebuffer.map(Dom::from_ref),
79 }
80 }
81
82 #[allow(clippy::too_many_arguments)]
83 fn new(
84 global: &GlobalScope,
85 proto: Option<HandleObject>,
86 session: &XRSession,
87 context: &WebGLRenderingContext,
88 init: &XRWebGLLayerInit,
89 framebuffer: Option<&WebGLFramebuffer>,
90 layer_id: Option<LayerId>,
91 can_gc: CanGc,
92 ) -> DomRoot<XRWebGLLayer> {
93 reflect_dom_object_with_proto(
94 Box::new(XRWebGLLayer::new_inherited(
95 session,
96 context,
97 init,
98 framebuffer,
99 layer_id,
100 )),
101 global,
102 proto,
103 can_gc,
104 )
105 }
106
107 pub(crate) fn layer_id(&self) -> Option<LayerId> {
108 self.xr_layer.layer_id()
109 }
110
111 pub(crate) fn context_id(&self) -> WebGLContextId {
112 self.xr_layer.context_id()
113 }
114
115 pub(crate) fn session(&self) -> &XRSession {
116 self.xr_layer.session()
117 }
118
119 pub(crate) fn size(&self) -> Size2D<u32, Viewport> {
120 if let Some(framebuffer) = self.framebuffer.as_ref() {
121 let size = framebuffer.size().unwrap_or((0, 0));
122 Size2D::new(
123 size.0.try_into().unwrap_or(0),
124 size.1.try_into().unwrap_or(0),
125 )
126 } else {
127 Size2D::from_untyped(self.context().size())
128 }
129 }
130
131 fn texture_target(&self) -> u32 {
132 if cfg!(target_os = "macos") {
133 glow::TEXTURE_RECTANGLE
134 } else {
135 glow::TEXTURE_2D
136 }
137 }
138
139 pub(crate) fn begin_frame(&self, frame: &XRFrame) -> Option<()> {
140 debug!("XRWebGLLayer begin frame");
141 let framebuffer = self.framebuffer.as_ref()?;
142 let context = framebuffer.upcast::<WebGLObject>().context();
143 let sub_images = frame.get_sub_images(self.layer_id()?)?;
144 let session = self.session();
145 let color_texture_id = WebGLTextureId::new(sub_images.sub_image.as_ref()?.color_texture?);
147 let color_texture =
148 WebGLTexture::new_webxr(context, color_texture_id, session, CanGc::note());
149 let target = self.texture_target();
150
151 let saved_framebuffer = context.get_draw_framebuffer_slot().get();
153 let saved_framebuffer_target = framebuffer.target();
154 let saved_texture_id = context
155 .textures()
156 .active_texture_slot(target, context.webgl_version())
157 .ok()
158 .and_then(|slot| slot.get().map(|texture| texture.id()));
159
160 let framebuffer_target = saved_framebuffer
164 .as_ref()
165 .and_then(|fb| fb.target())
166 .unwrap_or(constants::DRAW_FRAMEBUFFER);
167
168 context.send_command(WebGLCommand::BindTexture(target, Some(color_texture_id)));
170 framebuffer.bind(framebuffer_target);
171 framebuffer
172 .texture2d_even_if_opaque(
173 constants::COLOR_ATTACHMENT0,
174 self.texture_target(),
175 Some(&color_texture),
176 0,
177 )
178 .ok()?;
179 if let Some(id) = sub_images.sub_image.as_ref()?.depth_stencil_texture {
180 let depth_stencil_texture_id = WebGLTextureId::new(id);
182 let depth_stencil_texture =
183 WebGLTexture::new_webxr(context, depth_stencil_texture_id, session, CanGc::note());
184 framebuffer
185 .texture2d_even_if_opaque(
186 constants::DEPTH_STENCIL_ATTACHMENT,
187 constants::TEXTURE_2D,
188 Some(&depth_stencil_texture),
189 0,
190 )
191 .ok()?;
192 }
193
194 context.send_command(WebGLCommand::BindTexture(target, saved_texture_id));
196 if let Some(framebuffer_target) = saved_framebuffer_target {
197 framebuffer.bind(framebuffer_target);
198 }
199 if let Some(framebuffer) = saved_framebuffer {
200 framebuffer.bind(framebuffer_target);
201 }
202 Some(())
203 }
204
205 pub(crate) fn end_frame(&self, _frame: &XRFrame) -> Option<()> {
206 debug!("XRWebGLLayer end frame");
207 let framebuffer = self.framebuffer.as_ref()?;
209 framebuffer.bind(constants::FRAMEBUFFER);
211 framebuffer
212 .texture2d_even_if_opaque(constants::COLOR_ATTACHMENT0, self.texture_target(), None, 0)
213 .ok()?;
214 framebuffer
215 .texture2d_even_if_opaque(
216 constants::DEPTH_STENCIL_ATTACHMENT,
217 constants::DEPTH_STENCIL_ATTACHMENT,
218 None,
219 0,
220 )
221 .ok()?;
222 framebuffer.upcast::<WebGLObject>().context().Flush();
223 Some(())
224 }
225
226 pub(crate) fn context(&self) -> &WebGLRenderingContext {
227 self.xr_layer.context()
228 }
229}
230
231impl XRWebGLLayerMethods<crate::DomTypeHolder> for XRWebGLLayer {
232 fn Constructor(
234 global: &Window,
235 proto: Option<HandleObject>,
236 can_gc: CanGc,
237 session: &XRSession,
238 context: XRWebGLRenderingContext,
239 init: &XRWebGLLayerInit,
240 ) -> Fallible<DomRoot<Self>> {
241 let context = match context {
242 XRWebGLRenderingContext::WebGLRenderingContext(ctx) => ctx,
243 XRWebGLRenderingContext::WebGL2RenderingContext(ctx) => ctx.base_context(),
244 };
245
246 if session.is_ended() {
248 return Err(Error::InvalidState(None));
249 }
250 let (framebuffer, layer_id) = if session.is_immersive() {
254 let size = session
256 .with_session(|session| session.recommended_framebuffer_resolution())
257 .ok_or(Error::Operation)?;
258 let framebuffer = WebGLFramebuffer::maybe_new_webxr(session, &context, size, can_gc)
259 .ok_or(Error::Operation)?;
260
261 let context_id = WebXRContextId::from(context.context_id());
264 let layer_init: LayerInit = init.convert();
265 let layer_id = session
266 .with_session(|session| session.create_layer(context_id, layer_init))
267 .map_err(|_| Error::Operation)?;
268
269 (Some(framebuffer), Some(layer_id))
272 } else {
273 (None, None)
274 };
275
276 context.Finish();
278
279 Ok(XRWebGLLayer::new(
281 &global.global(),
282 proto,
283 session,
284 &context,
285 init,
286 framebuffer.as_deref(),
287 layer_id,
288 can_gc,
289 ))
290 }
291
292 fn GetNativeFramebufferScaleFactor(_window: &Window, session: &XRSession) -> Finite<f64> {
294 let value: f64 = if session.is_ended() { 0.0 } else { 1.0 };
295 Finite::wrap(value)
296 }
297
298 fn Antialias(&self) -> bool {
300 self.antialias
301 }
302
303 fn IgnoreDepthValues(&self) -> bool {
305 self.ignore_depth_values
306 }
307
308 fn GetFixedFoveation(&self) -> Option<Finite<f32>> {
310 None
312 }
313
314 fn SetFixedFoveation(&self, _value: Option<Finite<f32>>) {
316 }
318
319 fn GetFramebuffer(&self) -> Option<DomRoot<WebGLFramebuffer>> {
321 self.framebuffer.as_ref().map(|x| DomRoot::from_ref(&**x))
322 }
323
324 fn FramebufferWidth(&self) -> u32 {
326 self.size().width
327 }
328
329 fn FramebufferHeight(&self) -> u32 {
331 self.size().height
332 }
333
334 fn GetViewport(&self, view: &XRView) -> Option<DomRoot<XRViewport>> {
336 if self.session() != view.session() {
337 return None;
338 }
339
340 let index = view.viewport_index();
341
342 let viewport = self.session().with_session(|s| {
343 if s.viewports().is_empty() {
345 Rect::from_size(self.size().to_i32())
346 } else {
347 s.viewports()[index]
348 }
349 });
350
351 Some(XRViewport::new(&self.global(), viewport, CanGc::note()))
357 }
358}