three_d/core/render_target/
clear_state.rs1use crate::core::*;
2
3#[derive(Debug, Copy, Clone, PartialEq)]
8pub struct ClearState {
9 pub red: Option<f32>,
11 pub green: Option<f32>,
13 pub blue: Option<f32>,
15 pub alpha: Option<f32>,
17 pub depth: Option<f32>,
19}
20
21impl ClearState {
22 pub const fn none() -> Self {
26 Self {
27 red: None,
28 green: None,
29 blue: None,
30 alpha: None,
31 depth: None,
32 }
33 }
34
35 pub const fn depth(depth: f32) -> Self {
39 Self {
40 red: None,
41 green: None,
42 blue: None,
43 alpha: None,
44 depth: Some(depth),
45 }
46 }
47
48 pub const fn color(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
52 Self {
53 red: Some(red),
54 green: Some(green),
55 blue: Some(blue),
56 alpha: Some(alpha),
57 depth: None,
58 }
59 }
60
61 pub const fn color_and_depth(red: f32, green: f32, blue: f32, alpha: f32, depth: f32) -> Self {
65 Self {
66 red: Some(red),
67 green: Some(green),
68 blue: Some(blue),
69 alpha: Some(alpha),
70 depth: Some(depth),
71 }
72 }
73
74 pub(in crate::core) fn apply(&self, context: &Context) {
75 context.set_write_mask(WriteMask {
76 red: self.red.is_some(),
77 green: self.green.is_some(),
78 blue: self.blue.is_some(),
79 alpha: self.alpha.is_some(),
80 depth: self.depth.is_some(),
81 });
82 unsafe {
83 let clear_color = self.red.is_some()
84 || self.green.is_some()
85 || self.blue.is_some()
86 || self.alpha.is_some();
87 if clear_color {
88 context.clear_color(
89 self.red.unwrap_or(0.0),
90 self.green.unwrap_or(0.0),
91 self.blue.unwrap_or(0.0),
92 self.alpha.unwrap_or(1.0),
93 );
94 }
95 if let Some(depth) = self.depth {
96 context.clear_depth_f32(depth);
97 }
98 context.clear(if clear_color && self.depth.is_some() {
99 crate::context::COLOR_BUFFER_BIT | crate::context::DEPTH_BUFFER_BIT
100 } else if clear_color {
101 crate::context::COLOR_BUFFER_BIT
102 } else {
103 crate::context::DEPTH_BUFFER_BIT
104 });
105 }
106 }
107}
108
109impl Default for ClearState {
110 fn default() -> Self {
111 Self::color_and_depth(0.0, 0.0, 0.0, 1.0, 1.0)
112 }
113}