Skip to main content

script/dom/webgl/
webglframebuffer.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// https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl
6use std::cell::Cell;
7
8use dom_struct::dom_struct;
9#[cfg(feature = "webxr")]
10use euclid::Size2D;
11use script_bindings::cell::DomRefCell;
12use script_bindings::reflector::reflect_dom_object;
13use script_bindings::weakref::WeakRef;
14use servo_canvas_traits::webgl::{
15    WebGLCommand, WebGLError, WebGLFramebufferBindingRequest, WebGLFramebufferId,
16    WebGLRenderbufferId, WebGLResult, WebGLTextureId, WebGLVersion, webgl_channel,
17};
18#[cfg(feature = "webxr")]
19use webxr_api::Viewport;
20
21use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants as constants;
22use crate::dom::bindings::inheritance::Castable;
23use crate::dom::bindings::reflector::DomGlobal;
24#[cfg(feature = "webxr")]
25use crate::dom::bindings::root::MutNullableDom;
26use crate::dom::bindings::root::{Dom, DomRoot};
27use crate::dom::webgl::webglobject::WebGLObject;
28use crate::dom::webgl::webglrenderbuffer::WebGLRenderbuffer;
29use crate::dom::webgl::webglrenderingcontext::{Operation, WebGLRenderingContext};
30use crate::dom::webgl::webgltexture::WebGLTexture;
31#[cfg(feature = "webxr")]
32use crate::dom::xrsession::XRSession;
33use crate::script_runtime::CanGc;
34
35pub(crate) enum CompleteForRendering {
36    Complete,
37    Incomplete,
38    MissingColorAttachment,
39}
40
41#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
42#[derive(Clone, JSTraceable, MallocSizeOf)]
43enum WebGLFramebufferAttachment {
44    Renderbuffer(Dom<WebGLRenderbuffer>),
45    Texture {
46        texture: Dom<WebGLTexture>,
47        level: i32,
48    },
49}
50
51impl WebGLFramebufferAttachment {
52    fn needs_initialization(&self) -> bool {
53        match *self {
54            WebGLFramebufferAttachment::Renderbuffer(ref r) => !r.is_initialized(),
55            WebGLFramebufferAttachment::Texture { .. } => false,
56        }
57    }
58
59    fn mark_initialized(&self) {
60        match *self {
61            WebGLFramebufferAttachment::Renderbuffer(ref r) => r.mark_initialized(),
62            WebGLFramebufferAttachment::Texture { .. } => (),
63        }
64    }
65
66    fn root(&self) -> WebGLFramebufferAttachmentRoot {
67        match self {
68            WebGLFramebufferAttachment::Renderbuffer(rb) => {
69                WebGLFramebufferAttachmentRoot::Renderbuffer(DomRoot::from_ref(rb))
70            },
71            WebGLFramebufferAttachment::Texture { texture, .. } => {
72                WebGLFramebufferAttachmentRoot::Texture(DomRoot::from_ref(texture))
73            },
74        }
75    }
76
77    fn detach(&self) {
78        match self {
79            WebGLFramebufferAttachment::Renderbuffer(rb) => rb.detach_from_framebuffer(),
80            WebGLFramebufferAttachment::Texture { texture, .. } => {
81                texture.detach_from_framebuffer()
82            },
83        }
84    }
85}
86
87#[derive(Clone, JSTraceable, MallocSizeOf)]
88pub(crate) enum WebGLFramebufferAttachmentRoot {
89    Renderbuffer(DomRoot<WebGLRenderbuffer>),
90    Texture(DomRoot<WebGLTexture>),
91}
92
93#[derive(JSTraceable, MallocSizeOf)]
94struct DroppableWebGLFramebuffer {
95    #[no_trace]
96    id: WebGLFramebufferId,
97    is_deleted: Cell<bool>,
98    context: WeakRef<WebGLRenderingContext>,
99}
100
101impl DroppableWebGLFramebuffer {
102    fn new(
103        id: WebGLFramebufferId,
104        is_deleted: Cell<bool>,
105        context: WeakRef<WebGLRenderingContext>,
106    ) -> Self {
107        Self {
108            id,
109            is_deleted,
110            context,
111        }
112    }
113}
114
115impl DroppableWebGLFramebuffer {
116    pub(crate) fn id(&self) -> WebGLFramebufferId {
117        self.id
118    }
119    pub(crate) fn is_deleted(&self) -> bool {
120        self.is_deleted.get()
121    }
122
123    pub(crate) fn set_deleted(&self, deleted: bool) {
124        self.is_deleted.set(deleted);
125    }
126
127    pub(crate) fn delete(&self, operation_fallibility: Operation) {
128        if !self.is_deleted() {
129            self.set_deleted(true);
130            if let Some(context) = self.context.root() {
131                let cmd = WebGLCommand::DeleteFramebuffer(self.id());
132                match operation_fallibility {
133                    Operation::Fallible => context.send_command_ignored(cmd),
134                    Operation::Infallible => context.send_command(cmd),
135                }
136            }
137        }
138    }
139}
140
141impl Drop for DroppableWebGLFramebuffer {
142    fn drop(&mut self) {
143        self.delete(Operation::Fallible);
144    }
145}
146
147#[dom_struct(associated_memory)] // actual memory usage is reported in WebGLFramebufferAttachment
148pub(crate) struct WebGLFramebuffer {
149    webgl_object: WebGLObject,
150    #[no_trace]
151    webgl_version: WebGLVersion,
152    target: Cell<Option<u32>>,
153    size: Cell<Option<(i32, i32)>>,
154    status: Cell<u32>,
155    // The attachment points for textures and renderbuffers on this
156    // FBO.
157    colors: Vec<DomRefCell<Option<WebGLFramebufferAttachment>>>,
158    depth: DomRefCell<Option<WebGLFramebufferAttachment>>,
159    stencil: DomRefCell<Option<WebGLFramebufferAttachment>>,
160    depthstencil: DomRefCell<Option<WebGLFramebufferAttachment>>,
161    color_read_buffer: DomRefCell<u32>,
162    color_draw_buffers: DomRefCell<Vec<u32>>,
163    is_initialized: Cell<bool>,
164    // Framebuffers for XR keep a reference to the XR session.
165    // https://github.com/immersive-web/webxr/issues/856
166    #[cfg(feature = "webxr")]
167    xr_session: MutNullableDom<XRSession>,
168    droppable: DroppableWebGLFramebuffer,
169}
170
171impl WebGLFramebuffer {
172    fn new_inherited(context: &WebGLRenderingContext, id: WebGLFramebufferId) -> Self {
173        Self {
174            webgl_object: WebGLObject::new_inherited(context),
175            webgl_version: context.webgl_version(),
176            target: Cell::new(None),
177            size: Cell::new(None),
178            status: Cell::new(constants::FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT),
179            colors: vec![DomRefCell::new(None); context.limits().max_color_attachments as usize],
180            depth: DomRefCell::new(None),
181            stencil: DomRefCell::new(None),
182            depthstencil: DomRefCell::new(None),
183            color_read_buffer: DomRefCell::new(constants::COLOR_ATTACHMENT0),
184            color_draw_buffers: DomRefCell::new(vec![constants::COLOR_ATTACHMENT0]),
185            is_initialized: Cell::new(false),
186            #[cfg(feature = "webxr")]
187            xr_session: Default::default(),
188            droppable: DroppableWebGLFramebuffer::new(id, Cell::new(false), WeakRef::new(context)),
189        }
190    }
191
192    pub(crate) fn maybe_new(
193        context: &WebGLRenderingContext,
194        can_gc: CanGc,
195    ) -> Option<DomRoot<Self>> {
196        let (sender, receiver) = webgl_channel().unwrap();
197        context.send_command(WebGLCommand::CreateFramebuffer(sender));
198        let id = receiver.recv().unwrap()?;
199        let framebuffer = WebGLFramebuffer::new(context, id, can_gc);
200        Some(framebuffer)
201    }
202
203    // TODO: depth, stencil and alpha
204    // https://github.com/servo/servo/issues/24498
205    #[cfg(feature = "webxr")]
206    pub(crate) fn maybe_new_webxr(
207        session: &XRSession,
208        context: &WebGLRenderingContext,
209        size: Size2D<i32, Viewport>,
210        can_gc: CanGc,
211    ) -> Option<DomRoot<Self>> {
212        let framebuffer = Self::maybe_new(context, can_gc)?;
213        framebuffer.size.set(Some((size.width, size.height)));
214        framebuffer.status.set(constants::FRAMEBUFFER_COMPLETE);
215        framebuffer.xr_session.set(Some(session));
216        Some(framebuffer)
217    }
218
219    pub(crate) fn new(
220        context: &WebGLRenderingContext,
221        id: WebGLFramebufferId,
222        can_gc: CanGc,
223    ) -> DomRoot<Self> {
224        reflect_dom_object(
225            Box::new(WebGLFramebuffer::new_inherited(context, id)),
226            &*context.global(),
227            can_gc,
228        )
229    }
230}
231
232impl WebGLFramebuffer {
233    pub(crate) fn id(&self) -> WebGLFramebufferId {
234        self.droppable.id()
235    }
236
237    #[cfg(feature = "webxr")]
238    fn is_in_xr_session(&self) -> bool {
239        self.xr_session.get().is_some()
240    }
241
242    #[cfg(not(feature = "webxr"))]
243    fn is_in_xr_session(&self) -> bool {
244        false
245    }
246
247    pub(crate) fn validate_transparent(&self) -> WebGLResult<()> {
248        if self.is_in_xr_session() {
249            Err(WebGLError::InvalidOperation)
250        } else {
251            Ok(())
252        }
253    }
254
255    pub(crate) fn bind(&self, target: u32) {
256        if !self.is_in_xr_session() {
257            // Update the framebuffer status on binding.  It may have
258            // changed if its attachments were resized or deleted while
259            // we've been unbound.
260            self.update_status();
261        }
262
263        self.target.set(Some(target));
264        self.upcast().send_command(WebGLCommand::BindFramebuffer(
265            target,
266            WebGLFramebufferBindingRequest::Explicit(self.id()),
267        ));
268    }
269
270    pub(crate) fn delete(&self, operation_fallibility: Operation) {
271        self.droppable.delete(operation_fallibility);
272    }
273
274    pub(crate) fn is_deleted(&self) -> bool {
275        // TODO: if a framebuffer has an attachment which is invalid due to
276        // being outside a webxr rAF, should this make the framebuffer invalid?
277        // https://github.com/immersive-web/layers/issues/196
278        self.droppable.is_deleted()
279    }
280
281    pub(crate) fn size(&self) -> Option<(i32, i32)> {
282        self.size.get()
283    }
284
285    pub(crate) fn get_attachment_formats(
286        &self,
287    ) -> WebGLResult<(Option<u32>, Option<u32>, Option<u32>)> {
288        if self.check_status() != constants::FRAMEBUFFER_COMPLETE {
289            return Err(WebGLError::InvalidFramebufferOperation);
290        }
291        let color = match self.attachment(constants::COLOR_ATTACHMENT0) {
292            Some(WebGLFramebufferAttachmentRoot::Renderbuffer(rb)) => Some(rb.internal_format()),
293            _ => None,
294        };
295        let depth = match self.attachment(constants::DEPTH_ATTACHMENT) {
296            Some(WebGLFramebufferAttachmentRoot::Renderbuffer(rb)) => Some(rb.internal_format()),
297            _ => None,
298        };
299        let stencil = match self.attachment(constants::STENCIL_ATTACHMENT) {
300            Some(WebGLFramebufferAttachmentRoot::Renderbuffer(rb)) => Some(rb.internal_format()),
301            _ => None,
302        };
303        Ok((color, depth, stencil))
304    }
305
306    fn check_attachment_constraints<'a>(
307        &self,
308        attachment: &Option<WebGLFramebufferAttachment>,
309        mut constraints: impl Iterator<Item = &'a u32>,
310        fb_size: &mut Option<(i32, i32)>,
311    ) -> Result<(), u32> {
312        // Get the size of this attachment.
313        let (format, size) = match attachment {
314            Some(WebGLFramebufferAttachment::Renderbuffer(att_rb)) => {
315                (Some(att_rb.internal_format()), att_rb.size())
316            },
317            Some(WebGLFramebufferAttachment::Texture {
318                texture: att_tex,
319                level,
320            }) => match att_tex.image_info_at_face(0, *level as u32) {
321                Some(info) => (
322                    Some(info.internal_format().as_gl_constant()),
323                    Some((info.width() as i32, info.height() as i32)),
324                ),
325                None => return Err(constants::FRAMEBUFFER_INCOMPLETE_ATTACHMENT),
326            },
327            None => (None, None),
328        };
329
330        // Make sure that, if we've found any other attachment,
331        // that the size matches.
332        if size.is_some() {
333            if fb_size.is_some() && size != *fb_size {
334                return Err(constants::FRAMEBUFFER_INCOMPLETE_DIMENSIONS);
335            } else {
336                *fb_size = size;
337            }
338        }
339
340        if let Some(format) = format &&
341            constraints.all(|c| *c != format)
342        {
343            return Err(constants::FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
344        }
345
346        Ok(())
347    }
348
349    pub(crate) fn update_status(&self) {
350        let z = self.depth.borrow();
351        let s = self.stencil.borrow();
352        let zs = self.depthstencil.borrow();
353        let has_z = z.is_some();
354        let has_s = s.is_some();
355        let has_zs = zs.is_some();
356
357        let is_supported = match self.webgl_version {
358            // From the WebGL 1.0 spec, 6.6 ("Framebuffer Object Attachments"):
359            //
360            //    "In the WebGL API, it is an error to concurrently attach
361            //     renderbuffers to the following combinations of
362            //     attachment points:
363            //
364            //     DEPTH_ATTACHMENT + DEPTH_STENCIL_ATTACHMENT
365            //     STENCIL_ATTACHMENT + DEPTH_STENCIL_ATTACHMENT
366            //     DEPTH_ATTACHMENT + STENCIL_ATTACHMENT
367            //
368            //     If any of the constraints above are violated, then:
369            //
370            //     checkFramebufferStatus must return FRAMEBUFFER_UNSUPPORTED."
371            WebGLVersion::WebGL1 => !(has_zs && (has_z || has_s)) && !(has_z && has_s),
372
373            // In WebGL 2.0, DEPTH_STENCIL_ATTACHMENT is considered an alias for
374            // DEPTH_ATTACHMENT + STENCIL_ATTACHMENT, i.e., the same image is attached to both DEPTH_ATTACHMENT
375            // and STENCIL_ATTACHMENT, overwriting the original images attached to the two attachment points.
376            // If different images are bound to the depth and stencil attachment points, checkFramebufferStatus
377            // returns FRAMEBUFFER_UNSUPPORTED, and getFramebufferAttachmentParameter with attachment of
378            // DEPTH_STENCIL_ATTACHMENT generates an INVALID_OPERATION error.
379            // -- WebGL 2.0 spec, 4.1.5 Framebuffer Object Attachments
380            WebGLVersion::WebGL2 => {
381                use WebGLFramebufferAttachment::{Renderbuffer, Texture};
382                match (&*z, &*s) {
383                    (Some(Renderbuffer(a)), Some(Renderbuffer(b))) => a.id() == b.id(),
384                    (Some(Texture { texture: a, .. }), Some(Texture { texture: b, .. })) => {
385                        a.id() == b.id()
386                    },
387                    _ => !has_z || !has_s,
388                }
389            },
390        };
391        if !is_supported {
392            return self.status.set(constants::FRAMEBUFFER_UNSUPPORTED);
393        }
394
395        let mut fb_size = None;
396
397        let attachments = [&*z, &*s, &*zs];
398        let webgl1_attachment_constraints = &[
399            &[
400                constants::DEPTH_COMPONENT16,
401                constants::DEPTH_COMPONENT24,
402                constants::DEPTH_COMPONENT32F,
403                constants::DEPTH24_STENCIL8,
404                constants::DEPTH32F_STENCIL8,
405            ][..],
406            &[
407                constants::STENCIL_INDEX8,
408                constants::DEPTH24_STENCIL8,
409                constants::DEPTH32F_STENCIL8,
410            ][..],
411            &[constants::DEPTH_STENCIL][..],
412        ];
413        let webgl2_attachment_constraints = &[
414            &[constants::DEPTH_STENCIL][..],
415            &[constants::DEPTH_STENCIL][..],
416            &[][..],
417        ];
418        let empty_attachment_constrains = &[&[][..], &[][..], &[][..]];
419        let extra_attachment_constraints = match self.webgl_version {
420            WebGLVersion::WebGL1 => empty_attachment_constrains,
421            WebGLVersion::WebGL2 => webgl2_attachment_constraints,
422        };
423        let attachment_constraints = webgl1_attachment_constraints
424            .iter()
425            .zip(extra_attachment_constraints.iter())
426            .map(|(a, b)| a.iter().chain(b.iter()));
427
428        for (attachment, constraints) in attachments.iter().zip(attachment_constraints) {
429            if let Err(errnum) =
430                self.check_attachment_constraints(attachment, constraints, &mut fb_size)
431            {
432                return self.status.set(errnum);
433            }
434        }
435
436        let webgl1_color_constraints = &[
437            constants::RGB,
438            constants::RGB565,
439            constants::RGB5_A1,
440            constants::RGBA,
441            constants::RGBA4,
442        ][..];
443        let webgl2_color_constraints = &[
444            constants::ALPHA,
445            constants::LUMINANCE,
446            constants::LUMINANCE_ALPHA,
447            constants::R11F_G11F_B10F,
448            constants::R16F,
449            constants::R16I,
450            constants::R16UI,
451            constants::R32F,
452            constants::R32I,
453            constants::R32UI,
454            constants::R8,
455            constants::R8_SNORM,
456            constants::R8I,
457            constants::R8UI,
458            constants::RG16F,
459            constants::RG16I,
460            constants::RG16UI,
461            constants::RG32F,
462            constants::RG32I,
463            constants::RG32UI,
464            constants::RG8,
465            constants::RG8_SNORM,
466            constants::RG8I,
467            constants::RG8UI,
468            constants::RGB10_A2,
469            constants::RGB10_A2UI,
470            constants::RGB16F,
471            constants::RGB16I,
472            constants::RGB16UI,
473            constants::RGB32F,
474            constants::RGB32I,
475            constants::RGB32UI,
476            constants::RGB8,
477            constants::RGB8_SNORM,
478            constants::RGB8I,
479            constants::RGB8UI,
480            constants::RGB9_E5,
481            constants::RGBA16F,
482            constants::RGBA16I,
483            constants::RGBA16UI,
484            constants::RGBA32F,
485            constants::RGBA32I,
486            constants::RGBA32UI,
487            constants::RGBA8,
488            constants::RGBA8_SNORM,
489            constants::RGBA8I,
490            constants::RGBA8UI,
491            constants::SRGB8,
492            constants::SRGB8_ALPHA8,
493        ][..];
494        let empty_color_constrains = &[][..];
495        let extra_color_constraints = match self.webgl_version {
496            WebGLVersion::WebGL1 => empty_color_constrains,
497            WebGLVersion::WebGL2 => webgl2_color_constraints,
498        };
499        let color_constraints = webgl1_color_constraints
500            .iter()
501            .chain(extra_color_constraints.iter());
502
503        let has_c = self.colors.iter().any(|att| att.borrow().is_some());
504        for attachment in self.colors.iter() {
505            let attachment = attachment.borrow();
506            let constraints = color_constraints.clone();
507            if let Err(errnum) =
508                self.check_attachment_constraints(&attachment, constraints, &mut fb_size)
509            {
510                return self.status.set(errnum);
511            }
512        }
513
514        self.size.set(fb_size);
515
516        if has_c || has_z || has_zs || has_s {
517            if self.size.get().is_some_and(|(w, h)| w != 0 && h != 0) {
518                self.status.set(constants::FRAMEBUFFER_COMPLETE);
519            } else {
520                self.status
521                    .set(constants::FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
522            }
523        } else {
524            self.status
525                .set(constants::FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
526        }
527    }
528
529    pub(crate) fn check_status(&self) -> u32 {
530        // For opaque framebuffers, check to see if the XR session is currently processing an rAF
531        // https://immersive-web.github.io/webxr/#opaque-framebuffer
532        #[cfg(feature = "webxr")]
533        if let Some(xr_session) = self.xr_session.get() {
534            return if xr_session.is_outside_raf() {
535                constants::FRAMEBUFFER_UNSUPPORTED
536            } else {
537                constants::FRAMEBUFFER_COMPLETE
538            };
539        }
540
541        self.status.get()
542        // TODO: if a framebuffer has an attachment which is invalid due to
543        // being outside a webxr rAF, should this make the framebuffer incomplete?
544        // https://github.com/immersive-web/layers/issues/196
545    }
546
547    pub(crate) fn check_status_for_rendering(&self) -> CompleteForRendering {
548        let result = self.check_status();
549        if result != constants::FRAMEBUFFER_COMPLETE {
550            return CompleteForRendering::Incomplete;
551        }
552
553        // XR framebuffers are complete inside an rAF
554        // https://github.com/immersive-web/webxr/issues/854
555        #[cfg(feature = "webxr")]
556        if self.xr_session.get().is_some() {
557            return CompleteForRendering::Complete;
558        }
559
560        if self.colors.iter().all(|att| att.borrow().is_none()) {
561            return CompleteForRendering::MissingColorAttachment;
562        }
563
564        if !self.is_initialized.get() {
565            let attachments = [
566                (&self.depth, constants::DEPTH_BUFFER_BIT),
567                (&self.stencil, constants::STENCIL_BUFFER_BIT),
568                (
569                    &self.depthstencil,
570                    constants::DEPTH_BUFFER_BIT | constants::STENCIL_BUFFER_BIT,
571                ),
572            ];
573            let mut clear_bits = 0;
574            for &(attachment, bits) in &attachments {
575                if let Some(ref att) = *attachment.borrow() &&
576                    att.needs_initialization()
577                {
578                    att.mark_initialized();
579                    clear_bits |= bits;
580                }
581            }
582            for attachment in self.colors.iter() {
583                if let Some(ref att) = *attachment.borrow() &&
584                    att.needs_initialization()
585                {
586                    att.mark_initialized();
587                    clear_bits |= constants::COLOR_BUFFER_BIT;
588                }
589            }
590
591            if let Some(context) = self.upcast().context() {
592                context.initialize_framebuffer(clear_bits);
593            }
594
595            self.is_initialized.set(true);
596        }
597
598        // TODO: if a framebuffer has an attachment which is invalid due to
599        // being outside a webxr rAF, should this make the framebuffer incomplete?
600        // https://github.com/immersive-web/layers/issues/196
601
602        CompleteForRendering::Complete
603    }
604
605    pub(crate) fn renderbuffer(
606        &self,
607        attachment: u32,
608        rb: Option<&WebGLRenderbuffer>,
609    ) -> WebGLResult<()> {
610        // Opaque framebuffers cannot have their attachments changed
611        // https://immersive-web.github.io/webxr/#opaque-framebuffer
612        self.validate_transparent()?;
613
614        let binding = self
615            .attachment_binding(attachment)
616            .ok_or(WebGLError::InvalidEnum)?;
617
618        let rb_id = match rb {
619            Some(rb) => {
620                if !rb.ever_bound() {
621                    return Err(WebGLError::InvalidOperation);
622                }
623                *binding.borrow_mut() =
624                    Some(WebGLFramebufferAttachment::Renderbuffer(Dom::from_ref(rb)));
625                rb.attach_to_framebuffer(self);
626                Some(rb.id())
627            },
628
629            _ => None,
630        };
631
632        self.upcast()
633            .send_command(WebGLCommand::FramebufferRenderbuffer(
634                self.target.get().unwrap(),
635                attachment,
636                constants::RENDERBUFFER,
637                rb_id,
638            ));
639
640        if rb.is_none() {
641            self.detach_binding(binding, attachment)?;
642        }
643
644        self.update_status();
645        self.is_initialized.set(false);
646        Ok(())
647    }
648
649    fn detach_binding(
650        &self,
651        binding: &DomRefCell<Option<WebGLFramebufferAttachment>>,
652        attachment: u32,
653    ) -> WebGLResult<()> {
654        // Opaque framebuffers cannot have their attachments changed
655        // https://immersive-web.github.io/webxr/#opaque-framebuffer
656        self.validate_transparent()?;
657
658        if let Some(att) = &*binding.borrow() {
659            att.detach();
660        }
661        *binding.borrow_mut() = None;
662        if INTERESTING_ATTACHMENT_POINTS.contains(&attachment) {
663            self.reattach_depth_stencil()?;
664        }
665        Ok(())
666    }
667
668    fn attachment_binding(
669        &self,
670        attachment: u32,
671    ) -> Option<&DomRefCell<Option<WebGLFramebufferAttachment>>> {
672        match attachment {
673            constants::COLOR_ATTACHMENT0..=constants::COLOR_ATTACHMENT15 => {
674                let idx = attachment - constants::COLOR_ATTACHMENT0;
675                self.colors.get(idx as usize)
676            },
677            constants::DEPTH_ATTACHMENT => Some(&self.depth),
678            constants::STENCIL_ATTACHMENT => Some(&self.stencil),
679            constants::DEPTH_STENCIL_ATTACHMENT => Some(&self.depthstencil),
680            _ => None,
681        }
682    }
683
684    fn reattach_depth_stencil(&self) -> WebGLResult<()> {
685        // Opaque framebuffers cannot have their attachments changed
686        // https://immersive-web.github.io/webxr/#opaque-framebuffer
687        self.validate_transparent()?;
688
689        let reattach = |attachment: &WebGLFramebufferAttachment, attachment_point| {
690            let webgl_object = self.upcast();
691            match *attachment {
692                WebGLFramebufferAttachment::Renderbuffer(ref rb) => {
693                    rb.attach_to_framebuffer(self);
694                    webgl_object.send_command(WebGLCommand::FramebufferRenderbuffer(
695                        self.target.get().unwrap(),
696                        attachment_point,
697                        constants::RENDERBUFFER,
698                        Some(rb.id()),
699                    ));
700                },
701                WebGLFramebufferAttachment::Texture { ref texture, level } => {
702                    texture.attach_to_framebuffer(self);
703                    webgl_object.send_command(WebGLCommand::FramebufferTexture2D(
704                        self.target.get().unwrap(),
705                        attachment_point,
706                        texture.target().expect("missing texture target"),
707                        Some(texture.id()),
708                        level,
709                    ));
710                },
711            }
712        };
713
714        // Since the DEPTH_STENCIL attachment causes both the DEPTH and STENCIL
715        // attachments to be overwritten, we need to ensure that we reattach
716        // the DEPTH and STENCIL attachments when any of those attachments
717        // is cleared.
718        if let Some(ref depth) = *self.depth.borrow() {
719            reattach(depth, constants::DEPTH_ATTACHMENT);
720        }
721        if let Some(ref stencil) = *self.stencil.borrow() {
722            reattach(stencil, constants::STENCIL_ATTACHMENT);
723        }
724        if let Some(ref depth_stencil) = *self.depthstencil.borrow() {
725            reattach(depth_stencil, constants::DEPTH_STENCIL_ATTACHMENT);
726        }
727        Ok(())
728    }
729
730    pub(crate) fn attachment(&self, attachment: u32) -> Option<WebGLFramebufferAttachmentRoot> {
731        let binding = self.attachment_binding(attachment)?;
732        binding
733            .borrow()
734            .as_ref()
735            .map(WebGLFramebufferAttachment::root)
736    }
737
738    pub(crate) fn texture2d(
739        &self,
740        attachment: u32,
741        textarget: u32,
742        texture: Option<&WebGLTexture>,
743        level: i32,
744    ) -> WebGLResult<()> {
745        // Opaque framebuffers cannot have their attachments changed
746        // https://immersive-web.github.io/webxr/#opaque-framebuffer
747        self.validate_transparent()?;
748        if let Some(texture) = texture {
749            //     "If texture is not zero, then texture must either
750            //      name an existing texture object with an target of
751            //      textarget, or texture must name an existing cube
752            //      map texture and textarget must be one of:
753            //      TEXTURE_CUBE_MAP_POSITIVE_X,
754            //      TEXTURE_CUBE_MAP_POSITIVE_Y,
755            //      TEXTURE_CUBE_MAP_POSITIVE_Z,
756            //      TEXTURE_CUBE_MAP_NEGATIVE_X,
757            //      TEXTURE_CUBE_MAP_NEGATIVE_Y, or
758            //      TEXTURE_CUBE_MAP_NEGATIVE_Z. Otherwise,
759            //      INVALID_OPERATION is generated."
760            let is_cube = match textarget {
761                constants::TEXTURE_2D => false,
762
763                constants::TEXTURE_CUBE_MAP_POSITIVE_X => true,
764                constants::TEXTURE_CUBE_MAP_POSITIVE_Y => true,
765                constants::TEXTURE_CUBE_MAP_POSITIVE_Z => true,
766                constants::TEXTURE_CUBE_MAP_NEGATIVE_X => true,
767                constants::TEXTURE_CUBE_MAP_NEGATIVE_Y => true,
768                constants::TEXTURE_CUBE_MAP_NEGATIVE_Z => true,
769
770                _ => return Err(WebGLError::InvalidEnum),
771            };
772
773            match texture.target() {
774                Some(constants::TEXTURE_CUBE_MAP) if is_cube => {},
775                Some(_) if !is_cube => {},
776                _ => return Err(WebGLError::InvalidOperation),
777            }
778
779            let Some(context) = self.upcast().context() else {
780                return Err(WebGLError::ContextLost);
781            };
782            let max_tex_size = if is_cube {
783                context.limits().max_cube_map_tex_size
784            } else {
785                context.limits().max_tex_size
786            };
787            if level < 0 || level as u32 > max_tex_size.ilog2() {
788                return Err(WebGLError::InvalidValue);
789            }
790        }
791        self.texture2d_even_if_opaque(attachment, textarget, texture, level)
792    }
793
794    pub(crate) fn texture2d_even_if_opaque(
795        &self,
796        attachment: u32,
797        textarget: u32,
798        texture: Option<&WebGLTexture>,
799        level: i32,
800    ) -> WebGLResult<()> {
801        let binding = self
802            .attachment_binding(attachment)
803            .ok_or(WebGLError::InvalidEnum)?;
804
805        let tex_id = match texture {
806            // Note, from the GLES 2.0.25 spec, page 113:
807            //      "If texture is zero, then textarget and level are ignored."
808            Some(texture) => {
809                *binding.borrow_mut() = Some(WebGLFramebufferAttachment::Texture {
810                    texture: Dom::from_ref(texture),
811                    level,
812                });
813                texture.attach_to_framebuffer(self);
814
815                Some(texture.id())
816            },
817
818            _ => None,
819        };
820
821        self.upcast()
822            .send_command(WebGLCommand::FramebufferTexture2D(
823                self.target.get().unwrap(),
824                attachment,
825                textarget,
826                tex_id,
827                level,
828            ));
829
830        if texture.is_none() {
831            self.detach_binding(binding, attachment)?;
832        }
833
834        self.update_status();
835        self.is_initialized.set(false);
836        Ok(())
837    }
838
839    pub(crate) fn texture_layer(
840        &self,
841        attachment: u32,
842        texture: Option<&WebGLTexture>,
843        level: i32,
844        layer: i32,
845    ) -> WebGLResult<()> {
846        let binding = self
847            .attachment_binding(attachment)
848            .ok_or(WebGLError::InvalidEnum)?;
849
850        let Some(context) = self.upcast().context() else {
851            return Err(WebGLError::ContextLost);
852        };
853
854        let tex_id = match texture {
855            Some(texture) => {
856                let (max_level, max_layer) = match texture.target() {
857                    Some(constants::TEXTURE_3D) => (
858                        context.limits().max_3d_texture_size.ilog2(),
859                        context.limits().max_3d_texture_size - 1,
860                    ),
861                    Some(constants::TEXTURE_2D) => (
862                        context.limits().max_tex_size.ilog2(),
863                        context.limits().max_array_texture_layers - 1,
864                    ),
865                    _ => return Err(WebGLError::InvalidOperation),
866                };
867
868                if level < 0 || level as u32 >= max_level {
869                    return Err(WebGLError::InvalidValue);
870                }
871                if layer < 0 || layer as u32 >= max_layer {
872                    return Err(WebGLError::InvalidValue);
873                }
874
875                *binding.borrow_mut() = Some(WebGLFramebufferAttachment::Texture {
876                    texture: Dom::from_ref(texture),
877                    level,
878                });
879                texture.attach_to_framebuffer(self);
880
881                Some(texture.id())
882            },
883            _ => None,
884        };
885
886        context.send_command(WebGLCommand::FramebufferTextureLayer(
887            self.target.get().unwrap(),
888            attachment,
889            tex_id,
890            level,
891            layer,
892        ));
893        Ok(())
894    }
895
896    fn with_matching_renderbuffers_id<F>(&self, rb_id: &WebGLRenderbufferId, mut closure: F)
897    where
898        F: FnMut(&DomRefCell<Option<WebGLFramebufferAttachment>>, u32),
899    {
900        let attachments = [
901            (&self.depth, constants::DEPTH_ATTACHMENT),
902            (&self.stencil, constants::STENCIL_ATTACHMENT),
903            (&self.depthstencil, constants::DEPTH_STENCIL_ATTACHMENT),
904        ];
905
906        fn has_matching_id(
907            attachment: &DomRefCell<Option<WebGLFramebufferAttachment>>,
908            target: &WebGLRenderbufferId,
909        ) -> bool {
910            match *attachment.borrow() {
911                Some(WebGLFramebufferAttachment::Renderbuffer(ref att_rb)) => {
912                    att_rb.id() == *target
913                },
914                _ => false,
915            }
916        }
917
918        for (attachment, name) in &attachments {
919            if has_matching_id(attachment, rb_id) {
920                closure(attachment, *name);
921            }
922        }
923
924        for (idx, attachment) in self.colors.iter().enumerate() {
925            if has_matching_id(attachment, rb_id) {
926                let name = constants::COLOR_ATTACHMENT0 + idx as u32;
927                closure(attachment, name);
928            }
929        }
930    }
931
932    fn with_matching_textures_id<F>(&self, tex_id: WebGLTextureId, mut closure: F)
933    where
934        F: FnMut(&DomRefCell<Option<WebGLFramebufferAttachment>>, u32),
935    {
936        let attachments = [
937            (&self.depth, constants::DEPTH_ATTACHMENT),
938            (&self.stencil, constants::STENCIL_ATTACHMENT),
939            (&self.depthstencil, constants::DEPTH_STENCIL_ATTACHMENT),
940        ];
941
942        fn has_matching_id(
943            attachment: &DomRefCell<Option<WebGLFramebufferAttachment>>,
944            target: &WebGLTextureId,
945        ) -> bool {
946            matches!(*attachment.borrow(), Some(WebGLFramebufferAttachment::Texture {
947                                     texture: ref att_texture,
948                                     ..
949                                }) if att_texture.id() == *target)
950        }
951
952        for (attachment, name) in &attachments {
953            if has_matching_id(attachment, &tex_id) {
954                closure(attachment, *name);
955            }
956        }
957
958        for (idx, attachment) in self.colors.iter().enumerate() {
959            if has_matching_id(attachment, &tex_id) {
960                let name = constants::COLOR_ATTACHMENT0 + idx as u32;
961                closure(attachment, name);
962            }
963        }
964    }
965
966    pub(crate) fn detach_renderbuffer_by_id(&self, rb_id: &WebGLRenderbufferId) -> WebGLResult<()> {
967        // Opaque framebuffers cannot have their attachments changed
968        // https://immersive-web.github.io/webxr/#opaque-framebuffer
969        self.validate_transparent()?;
970
971        let mut depth_or_stencil_updated = false;
972        self.with_matching_renderbuffers_id(rb_id, |att, name| {
973            depth_or_stencil_updated |= INTERESTING_ATTACHMENT_POINTS.contains(&name);
974            if let Some(att) = &*att.borrow() {
975                att.detach();
976            }
977            *att.borrow_mut() = None;
978            self.update_status();
979        });
980
981        if depth_or_stencil_updated {
982            self.reattach_depth_stencil()?;
983        }
984        Ok(())
985    }
986
987    pub(crate) fn detach_texture(&self, tex_id: WebGLTextureId) -> WebGLResult<()> {
988        // Opaque framebuffers cannot have their attachments changed
989        // https://immersive-web.github.io/webxr/#opaque-framebuffer
990        self.validate_transparent()?;
991
992        let mut depth_or_stencil_updated = false;
993        self.with_matching_textures_id(tex_id, |att, name| {
994            depth_or_stencil_updated |= INTERESTING_ATTACHMENT_POINTS.contains(&name);
995            if let Some(att) = &*att.borrow() {
996                att.detach();
997            }
998            *att.borrow_mut() = None;
999            self.update_status();
1000        });
1001
1002        if depth_or_stencil_updated {
1003            self.reattach_depth_stencil()?;
1004        }
1005        Ok(())
1006    }
1007
1008    pub(crate) fn invalidate_renderbuffer(&self, rb: &WebGLRenderbuffer) {
1009        self.with_matching_renderbuffers_id(&rb.id(), |_att, _| {
1010            self.is_initialized.set(false);
1011            self.update_status();
1012        });
1013    }
1014
1015    pub(crate) fn invalidate_texture(&self, texture: &WebGLTexture) {
1016        self.with_matching_textures_id(texture.id(), |_att, _name| {
1017            self.update_status();
1018        });
1019    }
1020
1021    pub(crate) fn set_read_buffer(&self, buffer: u32) -> WebGLResult<()> {
1022        let Some(context) = self.upcast().context() else {
1023            return Err(WebGLError::ContextLost);
1024        };
1025
1026        match buffer {
1027            constants::NONE => {},
1028            _ if context.valid_color_attachment_enum(buffer) => {},
1029            _ => return Err(WebGLError::InvalidOperation),
1030        };
1031
1032        *self.color_read_buffer.borrow_mut() = buffer;
1033        context.send_command(WebGLCommand::ReadBuffer(buffer));
1034        Ok(())
1035    }
1036
1037    pub(crate) fn set_draw_buffers(&self, buffers: Vec<u32>) -> WebGLResult<()> {
1038        let Some(context) = self.upcast().context() else {
1039            return Err(WebGLError::ContextLost);
1040        };
1041
1042        if buffers.len() > context.limits().max_draw_buffers as usize {
1043            return Err(WebGLError::InvalidValue);
1044        }
1045
1046        let enums_valid = buffers
1047            .iter()
1048            .all(|&val| val == constants::NONE || context.valid_color_attachment_enum(val));
1049        if !enums_valid {
1050            return Err(WebGLError::InvalidEnum);
1051        }
1052
1053        let values_valid = buffers.iter().enumerate().all(|(i, &val)| {
1054            val == constants::NONE || val == (constants::COLOR_ATTACHMENT0 + i as u32)
1055        });
1056        if !values_valid {
1057            return Err(WebGLError::InvalidOperation);
1058        }
1059
1060        self.color_draw_buffers.borrow_mut().clone_from(&buffers);
1061        context.send_command(WebGLCommand::DrawBuffers(buffers));
1062        Ok(())
1063    }
1064
1065    pub(crate) fn read_buffer(&self) -> u32 {
1066        *self.color_read_buffer.borrow()
1067    }
1068
1069    pub(crate) fn draw_buffer_i(&self, index: usize) -> u32 {
1070        let buffers = &*self.color_draw_buffers.borrow();
1071        *buffers.get(index).unwrap_or(&constants::NONE)
1072    }
1073
1074    pub(crate) fn target(&self) -> Option<u32> {
1075        self.target.get()
1076    }
1077}
1078
1079static INTERESTING_ATTACHMENT_POINTS: &[u32] = &[
1080    constants::DEPTH_ATTACHMENT,
1081    constants::STENCIL_ATTACHMENT,
1082    constants::DEPTH_STENCIL_ATTACHMENT,
1083];