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
/* 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/. */

use crate::SurfmanGL;
use sparkle::gl;
use sparkle::gl::GLuint;
use sparkle::gl::Gl;
use std::collections::HashMap;
use surfman::Device as SurfmanDevice;
use webxr_api::ContextId;
use webxr_api::GLContexts;
use webxr_api::LayerId;

// A utility to clear a color texture and optional depth/stencil texture
pub(crate) struct GlClearer {
    fbos: HashMap<(LayerId, GLuint, Option<GLuint>), GLuint>,
}

impl GlClearer {
    pub(crate) fn new() -> GlClearer {
        let fbos = HashMap::new();
        GlClearer { fbos }
    }

    fn fbo(
        &mut self,
        gl: &Gl,
        layer_id: LayerId,
        color: GLuint,
        color_target: GLuint,
        depth_stencil: Option<GLuint>,
    ) -> GLuint {
        *self
            .fbos
            .entry((layer_id, color, depth_stencil))
            .or_insert_with(|| {
                // Save the current GL state
                let mut bound_fbos = [0, 0];
                unsafe {
                    gl.get_integer_v(gl::DRAW_FRAMEBUFFER_BINDING, &mut bound_fbos[0..]);
                    gl.get_integer_v(gl::READ_FRAMEBUFFER_BINDING, &mut bound_fbos[1..]);
                }

                // Generate and set attachments of a new FBO
                let fbo = gl.gen_framebuffers(1)[0];

                gl.bind_framebuffer(gl::FRAMEBUFFER, fbo);
                gl.framebuffer_texture_2d(
                    gl::FRAMEBUFFER,
                    gl::COLOR_ATTACHMENT0,
                    color_target,
                    color,
                    0,
                );
                gl.framebuffer_texture_2d(
                    gl::FRAMEBUFFER,
                    gl::DEPTH_STENCIL_ATTACHMENT,
                    gl::TEXTURE_2D,
                    depth_stencil.unwrap_or(0),
                    0,
                );

                // Restore the GL state
                gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, bound_fbos[0] as GLuint);
                gl.bind_framebuffer(gl::READ_FRAMEBUFFER, bound_fbos[1] as GLuint);
                debug_assert_eq!(gl.get_error(), gl::NO_ERROR);

                fbo
            })
    }

    pub(crate) fn clear(
        &mut self,
        device: &mut SurfmanDevice,
        contexts: &mut dyn GLContexts<SurfmanGL>,
        context_id: ContextId,
        layer_id: LayerId,
        color: GLuint,
        color_target: GLuint,
        depth_stencil: Option<GLuint>,
    ) {
        let gl = match contexts.bindings(device, context_id) {
            None => return,
            Some(gl) => gl,
        };
        let fbo = self.fbo(gl, layer_id, color, color_target, depth_stencil);

        // Save the current GL state
        let mut bound_fbos = [0, 0];
        let mut clear_color = [0., 0., 0., 0.];
        let mut clear_depth = [0.];
        let mut clear_stencil = [0];
        let mut color_mask = [0, 0, 0, 0];
        let mut depth_mask = [0];
        let mut stencil_mask = [0];
        let scissor_enabled = gl.is_enabled(gl::SCISSOR_TEST);
        let rasterizer_enabled = gl.is_enabled(gl::RASTERIZER_DISCARD);
        unsafe {
            gl.get_integer_v(gl::DRAW_FRAMEBUFFER_BINDING, &mut bound_fbos[0..]);
            gl.get_integer_v(gl::READ_FRAMEBUFFER_BINDING, &mut bound_fbos[1..]);
            gl.get_float_v(gl::COLOR_CLEAR_VALUE, &mut clear_color[..]);
            gl.get_float_v(gl::DEPTH_CLEAR_VALUE, &mut clear_depth[..]);
            gl.get_integer_v(gl::STENCIL_CLEAR_VALUE, &mut clear_stencil[..]);
            gl.get_boolean_v(gl::DEPTH_WRITEMASK, &mut depth_mask[..]);
            gl.get_integer_v(gl::STENCIL_WRITEMASK, &mut stencil_mask[..]);
            gl.get_boolean_v(gl::COLOR_WRITEMASK, &mut color_mask[..]);
        }

        // Clear it
        gl.bind_framebuffer(gl::FRAMEBUFFER, fbo);
        gl.clear_color(0., 0., 0., 1.);
        gl.clear_depth(1.);
        gl.clear_stencil(0);
        gl.disable(gl::SCISSOR_TEST);
        gl.disable(gl::RASTERIZER_DISCARD);
        gl.depth_mask(true);
        gl.stencil_mask(0xFFFFFFFF);
        gl.color_mask(true, true, true, true);
        gl.clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);

        // Restore the GL state
        gl.bind_framebuffer(gl::DRAW_FRAMEBUFFER, bound_fbos[0] as GLuint);
        gl.bind_framebuffer(gl::READ_FRAMEBUFFER, bound_fbos[1] as GLuint);
        gl.clear_color(
            clear_color[0],
            clear_color[1],
            clear_color[2],
            clear_color[3],
        );
        gl.color_mask(
            color_mask[0] != 0,
            color_mask[1] != 0,
            color_mask[2] != 0,
            color_mask[3] != 0,
        );
        gl.clear_depth(clear_depth[0] as f64);
        gl.clear_stencil(clear_stencil[0]);
        gl.depth_mask(depth_mask[0] != 0);
        gl.stencil_mask(stencil_mask[0] as gl::GLuint);
        if scissor_enabled {
            gl.enable(gl::SCISSOR_TEST);
        }
        if rasterizer_enabled {
            gl.enable(gl::RASTERIZER_DISCARD);
        }
        debug_assert_eq!(gl.get_error(), gl::NO_ERROR);
    }

    pub(crate) fn destroy_layer(
        &mut self,
        device: &mut SurfmanDevice,
        contexts: &mut dyn GLContexts<SurfmanGL>,
        context_id: ContextId,
        layer_id: LayerId,
    ) {
        let gl = match contexts.bindings(device, context_id) {
            None => return,
            Some(gl) => gl,
        };
        self.fbos.retain(|&(other_id, _, _), &mut fbo| {
            if layer_id != other_id {
                true
            } else {
                gl.delete_framebuffers(&[fbo]);
                false
            }
        })
    }
}