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
// surfman/surfman/src/renderbuffers.rs
//
//! A utility module for backends that wrap surfaces in OpenGL FBOs.

use crate::context::{ContextAttributeFlags, ContextAttributes};
use crate::gl;
use crate::gl::types::GLuint;
use crate::Gl;
use std::thread;

use euclid::default::Size2D;

pub(crate) enum Renderbuffers {
    IndividualDepthStencil { depth: GLuint, stencil: GLuint },
    CombinedDepthStencil(GLuint),
}

impl Drop for Renderbuffers {
    fn drop(&mut self) {
        match *self {
            Renderbuffers::IndividualDepthStencil {
                depth: 0,
                stencil: 0,
            }
            | Renderbuffers::CombinedDepthStencil(0) => {}
            _ => {
                if !thread::panicking() {
                    panic!("Should have destroyed the FBO renderbuffers with `destroy()`!")
                }
            }
        }
    }
}

impl Renderbuffers {
    pub(crate) fn new(
        gl: &Gl,
        size: &Size2D<i32>,
        attributes: &ContextAttributes,
    ) -> Renderbuffers {
        unsafe {
            if attributes
                .flags
                .contains(ContextAttributeFlags::DEPTH | ContextAttributeFlags::STENCIL)
            {
                let mut renderbuffer = 0;
                gl.GenRenderbuffers(1, &mut renderbuffer);
                gl.BindRenderbuffer(gl::RENDERBUFFER, renderbuffer);
                gl.RenderbufferStorage(
                    gl::RENDERBUFFER,
                    gl::DEPTH24_STENCIL8,
                    size.width,
                    size.height,
                );
                gl.BindRenderbuffer(gl::RENDERBUFFER, 0);
                return Renderbuffers::CombinedDepthStencil(renderbuffer);
            }

            let (mut depth_renderbuffer, mut stencil_renderbuffer) = (0, 0);
            if attributes.flags.contains(ContextAttributeFlags::DEPTH) {
                gl.GenRenderbuffers(1, &mut depth_renderbuffer);
                gl.BindRenderbuffer(gl::RENDERBUFFER, depth_renderbuffer);
                gl.RenderbufferStorage(
                    gl::RENDERBUFFER,
                    gl::DEPTH_COMPONENT24,
                    size.width,
                    size.height,
                );
            }
            if attributes.flags.contains(ContextAttributeFlags::STENCIL) {
                gl.GenRenderbuffers(1, &mut stencil_renderbuffer);
                gl.BindRenderbuffer(gl::RENDERBUFFER, stencil_renderbuffer);
                gl.RenderbufferStorage(
                    gl::RENDERBUFFER,
                    gl::STENCIL_INDEX8,
                    size.width,
                    size.height,
                );
            }
            gl.BindRenderbuffer(gl::RENDERBUFFER, 0);

            Renderbuffers::IndividualDepthStencil {
                depth: depth_renderbuffer,
                stencil: stencil_renderbuffer,
            }
        }
    }

    pub(crate) fn bind_to_current_framebuffer(&self, gl: &Gl) {
        unsafe {
            match *self {
                Renderbuffers::CombinedDepthStencil(renderbuffer) => {
                    if renderbuffer != 0 {
                        gl.FramebufferRenderbuffer(
                            gl::FRAMEBUFFER,
                            gl::DEPTH_STENCIL_ATTACHMENT,
                            gl::RENDERBUFFER,
                            renderbuffer,
                        );
                    }
                }
                Renderbuffers::IndividualDepthStencil {
                    depth: depth_renderbuffer,
                    stencil: stencil_renderbuffer,
                } => {
                    if depth_renderbuffer != 0 {
                        gl.FramebufferRenderbuffer(
                            gl::FRAMEBUFFER,
                            gl::DEPTH_ATTACHMENT,
                            gl::RENDERBUFFER,
                            depth_renderbuffer,
                        );
                    }
                    if stencil_renderbuffer != 0 {
                        gl.FramebufferRenderbuffer(
                            gl::FRAMEBUFFER,
                            gl::STENCIL_ATTACHMENT,
                            gl::RENDERBUFFER,
                            stencil_renderbuffer,
                        );
                    }
                }
            }
        }
    }

    pub(crate) fn destroy(&mut self, gl: &Gl) {
        unsafe {
            gl.BindRenderbuffer(gl::RENDERBUFFER, 0);

            match *self {
                Renderbuffers::CombinedDepthStencil(ref mut renderbuffer) => {
                    if *renderbuffer != 0 {
                        gl.DeleteRenderbuffers(1, renderbuffer);
                        *renderbuffer = 0;
                    }
                }
                Renderbuffers::IndividualDepthStencil {
                    depth: ref mut depth_renderbuffer,
                    stencil: ref mut stencil_renderbuffer,
                } => {
                    if *stencil_renderbuffer != 0 {
                        gl.DeleteRenderbuffers(1, stencil_renderbuffer);
                        *stencil_renderbuffer = 0;
                    }
                    if *depth_renderbuffer != 0 {
                        gl.DeleteRenderbuffers(1, depth_renderbuffer);
                        *depth_renderbuffer = 0;
                    }
                }
            }
        }
    }
}