gpu/code/programs/
clear_program.rs

1use crate::prelude::*;
2use crate::{Context, Framebuffer, GLContext};
3
4
5
6// ====================
7// === ClearProgram ===
8// ====================
9
10/// A program that clears colors, depth and stencil of a `framebuffer`.
11pub struct ClearProgram {
12    gl: GLContext,
13    color: (f32, f32, f32, f32),
14    depth: f32,
15    stencil: i32
16}
17
18impl ClearProgram {
19    /// Color buffer bit.
20    pub const COLOR   : u32 = glow::COLOR_BUFFER_BIT;
21    /// Depth buffer bit.
22    pub const DEPTH   : u32 = glow::DEPTH_BUFFER_BIT;
23    /// Stencil buffer bit.
24    pub const STENCIL : u32 = glow::STENCIL_BUFFER_BIT;
25
26    /// Creates a new `ClearProgram`.
27    pub fn new(context:&Context) -> Self {
28        let gl = context.gl_context();
29        let color = (0.0, 0.0, 0.0, 0.0);
30        let depth = 1.0; // FIXME: is it default?
31        let stencil = 0; // FIXME: is it default?
32        Self { gl, color, depth, stencil}
33    }
34
35    /// Sets the color clear value.
36    pub fn set_color(&mut self, color: (f32, f32, f32, f32)) { self.color = color; }
37    /// Gets the color clear value.
38    pub fn color(&self) -> (f32, f32, f32, f32) { self.color }
39
40    /// Sets the depth clear value.
41    pub fn set_depth(&mut self, depth: f32) { self.depth = depth; }
42    /// Gets the depth clear value.
43    pub fn depth(&self) -> f32 { self.depth }
44
45    /// Sets the stencil clear value.
46    pub fn set_stencil(&mut self, stencil: i32) { self.stencil = stencil; }
47    /// Gets the stencil clear value.
48    pub fn stencil(&self) -> i32 { self.stencil }
49
50    /// Clear the target `Framebuffer` using the buffer bit mask.
51    /// Here is an example that clears color, depth and stencil in a single call:
52    /// ```rust,ignore
53    /// clear(framebuffer, ClearProgram::COLOR | ClearProgram::DEPTH | ClearProgram::STENCIL)
54    /// ```
55    pub fn clear(&self, framebuffer:&mut Framebuffer, clear_mask: u32) {
56        let gl = &self.gl;
57        unsafe {
58            framebuffer.bind();
59            gl.clear_color(self.color.0, self.color.1, self.color.2, self.color.3);
60            gl.clear_depth_f32(self.depth);
61            gl.clear_stencil(self.stencil);
62            gl.clear(clear_mask);
63        }
64    }
65}