1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

// https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl
use std::cell::Cell;

use canvas_traits::webgl::{
    webgl_channel, WebGLCommand, WebGLError, WebGLFramebufferBindingRequest, WebGLFramebufferId,
    WebGLRenderbufferId, WebGLResult, WebGLTextureId, WebGLVersion,
};
use dom_struct::dom_struct;
use euclid::Size2D;
use webxr_api::Viewport;

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants as constants;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::webglobject::WebGLObject;
use crate::dom::webglrenderbuffer::WebGLRenderbuffer;
use crate::dom::webglrenderingcontext::{Operation, WebGLRenderingContext};
use crate::dom::webgltexture::WebGLTexture;
use crate::dom::xrsession::XRSession;

pub enum CompleteForRendering {
    Complete,
    Incomplete,
    MissingColorAttachment,
}

fn log2(n: u32) -> u32 {
    31 - n.leading_zeros()
}

#[crown::unrooted_must_root_lint::must_root]
#[derive(Clone, JSTraceable, MallocSizeOf)]
enum WebGLFramebufferAttachment {
    Renderbuffer(Dom<WebGLRenderbuffer>),
    Texture {
        texture: Dom<WebGLTexture>,
        level: i32,
    },
}

impl WebGLFramebufferAttachment {
    fn needs_initialization(&self) -> bool {
        match *self {
            WebGLFramebufferAttachment::Renderbuffer(ref r) => !r.is_initialized(),
            WebGLFramebufferAttachment::Texture { .. } => false,
        }
    }

    fn mark_initialized(&self) {
        match *self {
            WebGLFramebufferAttachment::Renderbuffer(ref r) => r.mark_initialized(),
            WebGLFramebufferAttachment::Texture { .. } => (),
        }
    }

    fn root(&self) -> WebGLFramebufferAttachmentRoot {
        match *self {
            WebGLFramebufferAttachment::Renderbuffer(ref rb) => {
                WebGLFramebufferAttachmentRoot::Renderbuffer(DomRoot::from_ref(rb))
            },
            WebGLFramebufferAttachment::Texture { ref texture, .. } => {
                WebGLFramebufferAttachmentRoot::Texture(DomRoot::from_ref(texture))
            },
        }
    }

    fn detach(&self) {
        match self {
            WebGLFramebufferAttachment::Renderbuffer(rb) => rb.detach_from_framebuffer(),
            WebGLFramebufferAttachment::Texture { ref texture, .. } => {
                texture.detach_from_framebuffer()
            },
        }
    }
}

#[derive(Clone, JSTraceable, MallocSizeOf)]
pub enum WebGLFramebufferAttachmentRoot {
    Renderbuffer(DomRoot<WebGLRenderbuffer>),
    Texture(DomRoot<WebGLTexture>),
}

#[dom_struct]
pub struct WebGLFramebuffer {
    webgl_object: WebGLObject,
    #[no_trace]
    webgl_version: WebGLVersion,
    #[no_trace]
    id: WebGLFramebufferId,
    target: Cell<Option<u32>>,
    is_deleted: Cell<bool>,
    size: Cell<Option<(i32, i32)>>,
    status: Cell<u32>,
    // The attachment points for textures and renderbuffers on this
    // FBO.
    colors: Vec<DomRefCell<Option<WebGLFramebufferAttachment>>>,
    depth: DomRefCell<Option<WebGLFramebufferAttachment>>,
    stencil: DomRefCell<Option<WebGLFramebufferAttachment>>,
    depthstencil: DomRefCell<Option<WebGLFramebufferAttachment>>,
    color_read_buffer: DomRefCell<u32>,
    color_draw_buffers: DomRefCell<Vec<u32>>,
    is_initialized: Cell<bool>,
    // Framebuffers for XR keep a reference to the XR session.
    // https://github.com/immersive-web/webxr/issues/856
    xr_session: MutNullableDom<XRSession>,
}

impl WebGLFramebuffer {
    fn new_inherited(context: &WebGLRenderingContext, id: WebGLFramebufferId) -> Self {
        Self {
            webgl_object: WebGLObject::new_inherited(context),
            webgl_version: context.webgl_version(),
            id,
            target: Cell::new(None),
            is_deleted: Cell::new(false),
            size: Cell::new(None),
            status: Cell::new(constants::FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT),
            colors: vec![DomRefCell::new(None); context.limits().max_color_attachments as usize],
            depth: DomRefCell::new(None),
            stencil: DomRefCell::new(None),
            depthstencil: DomRefCell::new(None),
            color_read_buffer: DomRefCell::new(constants::COLOR_ATTACHMENT0),
            color_draw_buffers: DomRefCell::new(vec![constants::COLOR_ATTACHMENT0]),
            is_initialized: Cell::new(false),
            xr_session: Default::default(),
        }
    }

    pub fn maybe_new(context: &WebGLRenderingContext) -> Option<DomRoot<Self>> {
        let (sender, receiver) = webgl_channel().unwrap();
        context.send_command(WebGLCommand::CreateFramebuffer(sender));
        let id = receiver.recv().unwrap()?;
        let framebuffer = WebGLFramebuffer::new(context, id);
        Some(framebuffer)
    }

    // TODO: depth, stencil and alpha
    // https://github.com/servo/servo/issues/24498
    pub fn maybe_new_webxr(
        session: &XRSession,
        context: &WebGLRenderingContext,
        size: Size2D<i32, Viewport>,
    ) -> Option<DomRoot<Self>> {
        let framebuffer = Self::maybe_new(context)?;
        framebuffer.size.set(Some((size.width, size.height)));
        framebuffer.status.set(constants::FRAMEBUFFER_COMPLETE);
        framebuffer.xr_session.set(Some(session));
        Some(framebuffer)
    }

    pub fn new(context: &WebGLRenderingContext, id: WebGLFramebufferId) -> DomRoot<Self> {
        reflect_dom_object(
            Box::new(WebGLFramebuffer::new_inherited(context, id)),
            &*context.global(),
        )
    }
}

impl WebGLFramebuffer {
    pub fn id(&self) -> WebGLFramebufferId {
        self.id
    }

    fn is_in_xr_session(&self) -> bool {
        self.xr_session.get().is_some()
    }

    pub fn validate_transparent(&self) -> WebGLResult<()> {
        if self.is_in_xr_session() {
            Err(WebGLError::InvalidOperation)
        } else {
            Ok(())
        }
    }

    pub fn bind(&self, target: u32) {
        if !self.is_in_xr_session() {
            // Update the framebuffer status on binding.  It may have
            // changed if its attachments were resized or deleted while
            // we've been unbound.
            self.update_status();
        }

        self.target.set(Some(target));
        self.upcast::<WebGLObject>()
            .context()
            .send_command(WebGLCommand::BindFramebuffer(
                target,
                WebGLFramebufferBindingRequest::Explicit(self.id),
            ));
    }

    pub fn delete(&self, operation_fallibility: Operation) {
        if !self.is_deleted.get() {
            self.is_deleted.set(true);
            let context = self.upcast::<WebGLObject>().context();
            let cmd = WebGLCommand::DeleteFramebuffer(self.id);
            match operation_fallibility {
                Operation::Fallible => context.send_command_ignored(cmd),
                Operation::Infallible => context.send_command(cmd),
            }
        }
    }

    pub fn is_deleted(&self) -> bool {
        // TODO: if a framebuffer has an attachment which is invalid due to
        // being outside a webxr rAF, should this make the framebuffer invalid?
        // https://github.com/immersive-web/layers/issues/196
        self.is_deleted.get()
    }

    pub fn size(&self) -> Option<(i32, i32)> {
        self.size.get()
    }

    fn check_attachment_constraints<'a>(
        &self,
        attachment: &Option<WebGLFramebufferAttachment>,
        mut constraints: impl Iterator<Item = &'a u32>,
        fb_size: &mut Option<(i32, i32)>,
    ) -> Result<(), u32> {
        // Get the size of this attachment.
        let (format, size) = match attachment {
            Some(WebGLFramebufferAttachment::Renderbuffer(ref att_rb)) => {
                (Some(att_rb.internal_format()), att_rb.size())
            },
            Some(WebGLFramebufferAttachment::Texture {
                texture: ref att_tex,
                level,
            }) => match att_tex.image_info_at_face(0, *level as u32) {
                Some(info) => (
                    Some(info.internal_format().as_gl_constant()),
                    Some((info.width() as i32, info.height() as i32)),
                ),
                None => return Err(constants::FRAMEBUFFER_INCOMPLETE_ATTACHMENT),
            },
            None => (None, None),
        };

        // Make sure that, if we've found any other attachment,
        // that the size matches.
        if size.is_some() {
            if fb_size.is_some() && size != *fb_size {
                return Err(constants::FRAMEBUFFER_INCOMPLETE_DIMENSIONS);
            } else {
                *fb_size = size;
            }
        }

        if let Some(format) = format {
            if constraints.all(|c| *c != format) {
                return Err(constants::FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
            }
        }

        Ok(())
    }

    pub fn update_status(&self) {
        let z = self.depth.borrow();
        let s = self.stencil.borrow();
        let zs = self.depthstencil.borrow();
        let has_z = z.is_some();
        let has_s = s.is_some();
        let has_zs = zs.is_some();

        let is_supported = match self.webgl_version {
            // From the WebGL 1.0 spec, 6.6 ("Framebuffer Object Attachments"):
            //
            //    "In the WebGL API, it is an error to concurrently attach
            //     renderbuffers to the following combinations of
            //     attachment points:
            //
            //     DEPTH_ATTACHMENT + DEPTH_STENCIL_ATTACHMENT
            //     STENCIL_ATTACHMENT + DEPTH_STENCIL_ATTACHMENT
            //     DEPTH_ATTACHMENT + STENCIL_ATTACHMENT
            //
            //     If any of the constraints above are violated, then:
            //
            //     checkFramebufferStatus must return FRAMEBUFFER_UNSUPPORTED."
            WebGLVersion::WebGL1 => !(has_zs && (has_z || has_s)) && !(has_z && has_s),

            // In WebGL 2.0, DEPTH_STENCIL_ATTACHMENT is considered an alias for
            // DEPTH_ATTACHMENT + STENCIL_ATTACHMENT, i.e., the same image is attached to both DEPTH_ATTACHMENT
            // and STENCIL_ATTACHMENT, overwriting the original images attached to the two attachment points.
            // If different images are bound to the depth and stencil attachment points, checkFramebufferStatus
            // returns FRAMEBUFFER_UNSUPPORTED, and getFramebufferAttachmentParameter with attachment of
            // DEPTH_STENCIL_ATTACHMENT generates an INVALID_OPERATION error.
            // -- WebGL 2.0 spec, 4.1.5 Framebuffer Object Attachments
            WebGLVersion::WebGL2 => {
                use WebGLFramebufferAttachment::{Renderbuffer, Texture};
                match (&*z, &*s) {
                    (Some(Renderbuffer(a)), Some(Renderbuffer(b))) => a.id() == b.id(),
                    (Some(Texture { texture: a, .. }), Some(Texture { texture: b, .. })) => {
                        a.id() == b.id()
                    },
                    _ => !has_z || !has_s,
                }
            },
        };
        if !is_supported {
            return self.status.set(constants::FRAMEBUFFER_UNSUPPORTED);
        }

        let mut fb_size = None;

        let attachments = [&*z, &*s, &*zs];
        let webgl1_attachment_constraints = &[
            &[
                constants::DEPTH_COMPONENT16,
                constants::DEPTH_COMPONENT24,
                constants::DEPTH_COMPONENT32F,
                constants::DEPTH24_STENCIL8,
                constants::DEPTH32F_STENCIL8,
            ][..],
            &[
                constants::STENCIL_INDEX8,
                constants::DEPTH24_STENCIL8,
                constants::DEPTH32F_STENCIL8,
            ][..],
            &[constants::DEPTH_STENCIL][..],
        ];
        let webgl2_attachment_constraints = &[
            &[constants::DEPTH_STENCIL][..],
            &[constants::DEPTH_STENCIL][..],
            &[][..],
        ];
        let empty_attachment_constrains = &[&[][..], &[][..], &[][..]];
        let extra_attachment_constraints = match self.webgl_version {
            WebGLVersion::WebGL1 => empty_attachment_constrains,
            WebGLVersion::WebGL2 => webgl2_attachment_constraints,
        };
        let attachment_constraints = webgl1_attachment_constraints
            .iter()
            .zip(extra_attachment_constraints.iter())
            .map(|(a, b)| a.iter().chain(b.iter()));

        for (attachment, constraints) in attachments.iter().zip(attachment_constraints) {
            if let Err(errnum) =
                self.check_attachment_constraints(attachment, constraints, &mut fb_size)
            {
                return self.status.set(errnum);
            }
        }

        let webgl1_color_constraints = &[
            constants::RGB,
            constants::RGB565,
            constants::RGB5_A1,
            constants::RGBA,
            constants::RGBA4,
        ][..];
        let webgl2_color_constraints = &[
            constants::ALPHA,
            constants::LUMINANCE,
            constants::LUMINANCE_ALPHA,
            constants::R11F_G11F_B10F,
            constants::R16F,
            constants::R16I,
            constants::R16UI,
            constants::R32F,
            constants::R32I,
            constants::R32UI,
            constants::R8,
            constants::R8_SNORM,
            constants::R8I,
            constants::R8UI,
            constants::RG16F,
            constants::RG16I,
            constants::RG16UI,
            constants::RG32F,
            constants::RG32I,
            constants::RG32UI,
            constants::RG8,
            constants::RG8_SNORM,
            constants::RG8I,
            constants::RG8UI,
            constants::RGB10_A2,
            constants::RGB10_A2UI,
            constants::RGB16F,
            constants::RGB16I,
            constants::RGB16UI,
            constants::RGB32F,
            constants::RGB32I,
            constants::RGB32UI,
            constants::RGB8,
            constants::RGB8_SNORM,
            constants::RGB8I,
            constants::RGB8UI,
            constants::RGB9_E5,
            constants::RGBA16F,
            constants::RGBA16I,
            constants::RGBA16UI,
            constants::RGBA32F,
            constants::RGBA32I,
            constants::RGBA32UI,
            constants::RGBA8,
            constants::RGBA8_SNORM,
            constants::RGBA8I,
            constants::RGBA8UI,
            constants::SRGB8,
            constants::SRGB8_ALPHA8,
        ][..];
        let empty_color_constrains = &[][..];
        let extra_color_constraints = match self.webgl_version {
            WebGLVersion::WebGL1 => empty_color_constrains,
            WebGLVersion::WebGL2 => webgl2_color_constraints,
        };
        let color_constraints = webgl1_color_constraints
            .iter()
            .chain(extra_color_constraints.iter());

        let has_c = self.colors.iter().any(|att| att.borrow().is_some());
        for attachment in self.colors.iter() {
            let attachment = attachment.borrow();
            let constraints = color_constraints.clone();
            if let Err(errnum) =
                self.check_attachment_constraints(&attachment, constraints, &mut fb_size)
            {
                return self.status.set(errnum);
            }
        }

        self.size.set(fb_size);

        if has_c || has_z || has_zs || has_s {
            if self.size.get().map_or(false, |(w, h)| w != 0 && h != 0) {
                self.status.set(constants::FRAMEBUFFER_COMPLETE);
            } else {
                self.status
                    .set(constants::FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
            }
        } else {
            self.status
                .set(constants::FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
        }
    }

    pub fn check_status(&self) -> u32 {
        // For opaque framebuffers, check to see if the XR session is currently processing an rAF
        // https://immersive-web.github.io/webxr/#opaque-framebuffer
        if let Some(xr_session) = self.xr_session.get() {
            if xr_session.is_outside_raf() {
                constants::FRAMEBUFFER_UNSUPPORTED
            } else {
                constants::FRAMEBUFFER_COMPLETE
            }
        } else {
            self.status.get()
        }
        // TODO: if a framebuffer has an attachment which is invalid due to
        // being outside a webxr rAF, should this make the framebuffer incomplete?
        // https://github.com/immersive-web/layers/issues/196
    }

    pub fn check_status_for_rendering(&self) -> CompleteForRendering {
        let result = self.check_status();
        if result != constants::FRAMEBUFFER_COMPLETE {
            return CompleteForRendering::Incomplete;
        }

        // XR framebuffers are complete inside an rAF
        // https://github.com/immersive-web/webxr/issues/854
        if self.xr_session.get().is_some() {
            return CompleteForRendering::Complete;
        }

        if self.colors.iter().all(|att| att.borrow().is_none()) {
            return CompleteForRendering::MissingColorAttachment;
        }

        if !self.is_initialized.get() {
            let attachments = [
                (&self.depth, constants::DEPTH_BUFFER_BIT),
                (&self.stencil, constants::STENCIL_BUFFER_BIT),
                (
                    &self.depthstencil,
                    constants::DEPTH_BUFFER_BIT | constants::STENCIL_BUFFER_BIT,
                ),
            ];
            let mut clear_bits = 0;
            for &(attachment, bits) in &attachments {
                if let Some(ref att) = *attachment.borrow() {
                    if att.needs_initialization() {
                        att.mark_initialized();
                        clear_bits |= bits;
                    }
                }
            }
            for attachment in self.colors.iter() {
                if let Some(ref att) = *attachment.borrow() {
                    if att.needs_initialization() {
                        att.mark_initialized();
                        clear_bits |= constants::COLOR_BUFFER_BIT;
                    }
                }
            }
            self.upcast::<WebGLObject>()
                .context()
                .initialize_framebuffer(clear_bits);
            self.is_initialized.set(true);
        }

        // TODO: if a framebuffer has an attachment which is invalid due to
        // being outside a webxr rAF, should this make the framebuffer incomplete?
        // https://github.com/immersive-web/layers/issues/196

        CompleteForRendering::Complete
    }

    pub fn renderbuffer(&self, attachment: u32, rb: Option<&WebGLRenderbuffer>) -> WebGLResult<()> {
        // Opaque framebuffers cannot have their attachments changed
        // https://immersive-web.github.io/webxr/#opaque-framebuffer
        self.validate_transparent()?;

        let binding = self
            .attachment_binding(attachment)
            .ok_or(WebGLError::InvalidEnum)?;

        let rb_id = match rb {
            Some(rb) => {
                if !rb.ever_bound() {
                    return Err(WebGLError::InvalidOperation);
                }
                *binding.borrow_mut() =
                    Some(WebGLFramebufferAttachment::Renderbuffer(Dom::from_ref(rb)));
                rb.attach_to_framebuffer(self);
                Some(rb.id())
            },

            _ => None,
        };

        self.upcast::<WebGLObject>()
            .context()
            .send_command(WebGLCommand::FramebufferRenderbuffer(
                self.target.get().unwrap(),
                attachment,
                constants::RENDERBUFFER,
                rb_id,
            ));

        if rb.is_none() {
            self.detach_binding(binding, attachment)?;
        }

        self.update_status();
        self.is_initialized.set(false);
        Ok(())
    }

    fn detach_binding(
        &self,
        binding: &DomRefCell<Option<WebGLFramebufferAttachment>>,
        attachment: u32,
    ) -> WebGLResult<()> {
        // Opaque framebuffers cannot have their attachments changed
        // https://immersive-web.github.io/webxr/#opaque-framebuffer
        self.validate_transparent()?;

        if let Some(att) = &*binding.borrow() {
            att.detach();
        }
        *binding.borrow_mut() = None;
        if INTERESTING_ATTACHMENT_POINTS.contains(&attachment) {
            self.reattach_depth_stencil()?;
        }
        Ok(())
    }

    fn attachment_binding(
        &self,
        attachment: u32,
    ) -> Option<&DomRefCell<Option<WebGLFramebufferAttachment>>> {
        match attachment {
            constants::COLOR_ATTACHMENT0..=constants::COLOR_ATTACHMENT15 => {
                let idx = attachment - constants::COLOR_ATTACHMENT0;
                self.colors.get(idx as usize)
            },
            constants::DEPTH_ATTACHMENT => Some(&self.depth),
            constants::STENCIL_ATTACHMENT => Some(&self.stencil),
            constants::DEPTH_STENCIL_ATTACHMENT => Some(&self.depthstencil),
            _ => None,
        }
    }

    fn reattach_depth_stencil(&self) -> WebGLResult<()> {
        // Opaque framebuffers cannot have their attachments changed
        // https://immersive-web.github.io/webxr/#opaque-framebuffer
        self.validate_transparent()?;

        let reattach = |attachment: &WebGLFramebufferAttachment, attachment_point| {
            let context = self.upcast::<WebGLObject>().context();
            match *attachment {
                WebGLFramebufferAttachment::Renderbuffer(ref rb) => {
                    rb.attach_to_framebuffer(self);
                    context.send_command(WebGLCommand::FramebufferRenderbuffer(
                        self.target.get().unwrap(),
                        attachment_point,
                        constants::RENDERBUFFER,
                        Some(rb.id()),
                    ));
                },
                WebGLFramebufferAttachment::Texture { ref texture, level } => {
                    texture.attach_to_framebuffer(self);
                    context.send_command(WebGLCommand::FramebufferTexture2D(
                        self.target.get().unwrap(),
                        attachment_point,
                        texture.target().expect("missing texture target"),
                        Some(texture.id()),
                        level,
                    ));
                },
            }
        };

        // Since the DEPTH_STENCIL attachment causes both the DEPTH and STENCIL
        // attachments to be overwritten, we need to ensure that we reattach
        // the DEPTH and STENCIL attachments when any of those attachments
        // is cleared.
        if let Some(ref depth) = *self.depth.borrow() {
            reattach(depth, constants::DEPTH_ATTACHMENT);
        }
        if let Some(ref stencil) = *self.stencil.borrow() {
            reattach(stencil, constants::STENCIL_ATTACHMENT);
        }
        if let Some(ref depth_stencil) = *self.depthstencil.borrow() {
            reattach(depth_stencil, constants::DEPTH_STENCIL_ATTACHMENT);
        }
        Ok(())
    }

    pub fn attachment(&self, attachment: u32) -> Option<WebGLFramebufferAttachmentRoot> {
        let binding = self.attachment_binding(attachment)?;
        binding
            .borrow()
            .as_ref()
            .map(WebGLFramebufferAttachment::root)
    }

    pub fn texture2d(
        &self,
        attachment: u32,
        textarget: u32,
        texture: Option<&WebGLTexture>,
        level: i32,
    ) -> WebGLResult<()> {
        // Opaque framebuffers cannot have their attachments changed
        // https://immersive-web.github.io/webxr/#opaque-framebuffer
        self.validate_transparent()?;
        if let Some(texture) = texture {
            //     "If texture is not zero, then texture must either
            //      name an existing texture object with an target of
            //      textarget, or texture must name an existing cube
            //      map texture and textarget must be one of:
            //      TEXTURE_CUBE_MAP_POSITIVE_X,
            //      TEXTURE_CUBE_MAP_POSITIVE_Y,
            //      TEXTURE_CUBE_MAP_POSITIVE_Z,
            //      TEXTURE_CUBE_MAP_NEGATIVE_X,
            //      TEXTURE_CUBE_MAP_NEGATIVE_Y, or
            //      TEXTURE_CUBE_MAP_NEGATIVE_Z. Otherwise,
            //      INVALID_OPERATION is generated."
            let is_cube = match textarget {
                constants::TEXTURE_2D => false,

                constants::TEXTURE_CUBE_MAP_POSITIVE_X => true,
                constants::TEXTURE_CUBE_MAP_POSITIVE_Y => true,
                constants::TEXTURE_CUBE_MAP_POSITIVE_Z => true,
                constants::TEXTURE_CUBE_MAP_NEGATIVE_X => true,
                constants::TEXTURE_CUBE_MAP_NEGATIVE_Y => true,
                constants::TEXTURE_CUBE_MAP_NEGATIVE_Z => true,

                _ => return Err(WebGLError::InvalidEnum),
            };

            match texture.target() {
                Some(constants::TEXTURE_CUBE_MAP) if is_cube => {},
                Some(_) if !is_cube => {},
                _ => return Err(WebGLError::InvalidOperation),
            }

            let context = self.upcast::<WebGLObject>().context();
            let max_tex_size = if is_cube {
                context.limits().max_cube_map_tex_size
            } else {
                context.limits().max_tex_size
            };
            if level < 0 || level as u32 > log2(max_tex_size) {
                return Err(WebGLError::InvalidValue);
            }
        }
        self.texture2d_even_if_opaque(attachment, textarget, texture, level)
    }

    pub fn texture2d_even_if_opaque(
        &self,
        attachment: u32,
        textarget: u32,
        texture: Option<&WebGLTexture>,
        level: i32,
    ) -> WebGLResult<()> {
        let binding = self
            .attachment_binding(attachment)
            .ok_or(WebGLError::InvalidEnum)?;

        let tex_id = match texture {
            // Note, from the GLES 2.0.25 spec, page 113:
            //      "If texture is zero, then textarget and level are ignored."
            Some(texture) => {
                *binding.borrow_mut() = Some(WebGLFramebufferAttachment::Texture {
                    texture: Dom::from_ref(texture),
                    level,
                });
                texture.attach_to_framebuffer(self);

                Some(texture.id())
            },

            _ => None,
        };

        self.upcast::<WebGLObject>()
            .context()
            .send_command(WebGLCommand::FramebufferTexture2D(
                self.target.get().unwrap(),
                attachment,
                textarget,
                tex_id,
                level,
            ));

        if texture.is_none() {
            self.detach_binding(binding, attachment)?;
        }

        self.update_status();
        self.is_initialized.set(false);
        Ok(())
    }

    pub fn texture_layer(
        &self,
        attachment: u32,
        texture: Option<&WebGLTexture>,
        level: i32,
        layer: i32,
    ) -> WebGLResult<()> {
        let binding = self
            .attachment_binding(attachment)
            .ok_or(WebGLError::InvalidEnum)?;

        let context = self.upcast::<WebGLObject>().context();

        let tex_id = match texture {
            Some(texture) => {
                let (max_level, max_layer) = match texture.target() {
                    Some(constants::TEXTURE_3D) => (
                        log2(context.limits().max_3d_texture_size),
                        context.limits().max_3d_texture_size - 1,
                    ),
                    Some(constants::TEXTURE_2D) => (
                        log2(context.limits().max_tex_size),
                        context.limits().max_array_texture_layers - 1,
                    ),
                    _ => return Err(WebGLError::InvalidOperation),
                };

                if level < 0 || level as u32 >= max_level {
                    return Err(WebGLError::InvalidValue);
                }
                if layer < 0 || layer as u32 >= max_layer {
                    return Err(WebGLError::InvalidValue);
                }

                *binding.borrow_mut() = Some(WebGLFramebufferAttachment::Texture {
                    texture: Dom::from_ref(texture),
                    level,
                });
                texture.attach_to_framebuffer(self);

                Some(texture.id())
            },
            _ => None,
        };

        context.send_command(WebGLCommand::FramebufferTextureLayer(
            self.target.get().unwrap(),
            attachment,
            tex_id,
            level,
            layer,
        ));
        Ok(())
    }

    fn with_matching_renderbuffers<F>(&self, rb: &WebGLRenderbuffer, mut closure: F)
    where
        F: FnMut(&DomRefCell<Option<WebGLFramebufferAttachment>>, u32),
    {
        let rb_id = rb.id();
        let attachments = [
            (&self.depth, constants::DEPTH_ATTACHMENT),
            (&self.stencil, constants::STENCIL_ATTACHMENT),
            (&self.depthstencil, constants::DEPTH_STENCIL_ATTACHMENT),
        ];

        fn has_matching_id(
            attachment: &DomRefCell<Option<WebGLFramebufferAttachment>>,
            target: &WebGLRenderbufferId,
        ) -> bool {
            match *attachment.borrow() {
                Some(WebGLFramebufferAttachment::Renderbuffer(ref att_rb)) => {
                    att_rb.id() == *target
                },
                _ => false,
            }
        }

        for (attachment, name) in &attachments {
            if has_matching_id(attachment, &rb_id) {
                closure(attachment, *name);
            }
        }

        for (idx, attachment) in self.colors.iter().enumerate() {
            if has_matching_id(attachment, &rb_id) {
                let name = constants::COLOR_ATTACHMENT0 + idx as u32;
                closure(attachment, name);
            }
        }
    }

    fn with_matching_textures<F>(&self, texture: &WebGLTexture, mut closure: F)
    where
        F: FnMut(&DomRefCell<Option<WebGLFramebufferAttachment>>, u32),
    {
        let tex_id = texture.id();
        let attachments = [
            (&self.depth, constants::DEPTH_ATTACHMENT),
            (&self.stencil, constants::STENCIL_ATTACHMENT),
            (&self.depthstencil, constants::DEPTH_STENCIL_ATTACHMENT),
        ];

        fn has_matching_id(
            attachment: &DomRefCell<Option<WebGLFramebufferAttachment>>,
            target: &WebGLTextureId,
        ) -> bool {
            matches!(*attachment.borrow(), Some(WebGLFramebufferAttachment::Texture {
                                     texture: ref att_texture,
                                     ..
                                }) if att_texture.id() == *target)
        }

        for (attachment, name) in &attachments {
            if has_matching_id(attachment, &tex_id) {
                closure(attachment, *name);
            }
        }

        for (idx, attachment) in self.colors.iter().enumerate() {
            if has_matching_id(attachment, &tex_id) {
                let name = constants::COLOR_ATTACHMENT0 + idx as u32;
                closure(attachment, name);
            }
        }
    }

    pub fn detach_renderbuffer(&self, rb: &WebGLRenderbuffer) -> WebGLResult<()> {
        // Opaque framebuffers cannot have their attachments changed
        // https://immersive-web.github.io/webxr/#opaque-framebuffer
        self.validate_transparent()?;

        let mut depth_or_stencil_updated = false;
        self.with_matching_renderbuffers(rb, |att, name| {
            depth_or_stencil_updated |= INTERESTING_ATTACHMENT_POINTS.contains(&name);
            if let Some(att) = &*att.borrow() {
                att.detach();
            }
            *att.borrow_mut() = None;
            self.update_status();
        });

        if depth_or_stencil_updated {
            self.reattach_depth_stencil()?;
        }
        Ok(())
    }

    pub fn detach_texture(&self, texture: &WebGLTexture) -> WebGLResult<()> {
        // Opaque framebuffers cannot have their attachments changed
        // https://immersive-web.github.io/webxr/#opaque-framebuffer
        self.validate_transparent()?;

        let mut depth_or_stencil_updated = false;
        self.with_matching_textures(texture, |att, name| {
            depth_or_stencil_updated |= INTERESTING_ATTACHMENT_POINTS.contains(&name);
            if let Some(att) = &*att.borrow() {
                att.detach();
            }
            *att.borrow_mut() = None;
            self.update_status();
        });

        if depth_or_stencil_updated {
            self.reattach_depth_stencil()?;
        }
        Ok(())
    }

    pub fn invalidate_renderbuffer(&self, rb: &WebGLRenderbuffer) {
        self.with_matching_renderbuffers(rb, |_att, _| {
            self.is_initialized.set(false);
            self.update_status();
        });
    }

    pub fn invalidate_texture(&self, texture: &WebGLTexture) {
        self.with_matching_textures(texture, |_att, _name| {
            self.update_status();
        });
    }

    pub fn set_read_buffer(&self, buffer: u32) -> WebGLResult<()> {
        let context = self.upcast::<WebGLObject>().context();

        match buffer {
            constants::NONE => {},
            _ if context.valid_color_attachment_enum(buffer) => {},
            _ => return Err(WebGLError::InvalidOperation),
        };

        *self.color_read_buffer.borrow_mut() = buffer;
        context.send_command(WebGLCommand::ReadBuffer(buffer));
        Ok(())
    }

    pub fn set_draw_buffers(&self, buffers: Vec<u32>) -> WebGLResult<()> {
        let context = self.upcast::<WebGLObject>().context();

        if buffers.len() > context.limits().max_draw_buffers as usize {
            return Err(WebGLError::InvalidValue);
        }

        let enums_valid = buffers
            .iter()
            .all(|&val| val == constants::NONE || context.valid_color_attachment_enum(val));
        if !enums_valid {
            return Err(WebGLError::InvalidEnum);
        }

        let values_valid = buffers.iter().enumerate().all(|(i, &val)| {
            val == constants::NONE || val == (constants::COLOR_ATTACHMENT0 + i as u32)
        });
        if !values_valid {
            return Err(WebGLError::InvalidOperation);
        }

        *self.color_draw_buffers.borrow_mut() = buffers.clone();
        context.send_command(WebGLCommand::DrawBuffers(buffers));
        Ok(())
    }

    pub fn read_buffer(&self) -> u32 {
        *self.color_read_buffer.borrow()
    }

    pub fn draw_buffer_i(&self, index: usize) -> u32 {
        let buffers = &*self.color_draw_buffers.borrow();
        *buffers.get(index).unwrap_or(&constants::NONE)
    }

    pub fn target(&self) -> Option<u32> {
        self.target.get()
    }
}

impl Drop for WebGLFramebuffer {
    fn drop(&mut self) {
        self.delete(Operation::Fallible);
    }
}

static INTERESTING_ATTACHMENT_POINTS: &[u32] = &[
    constants::DEPTH_ATTACHMENT,
    constants::STENCIL_ATTACHMENT,
    constants::DEPTH_STENCIL_ATTACHMENT,
];