Skip to main content

pebble/graphics/render/
targets.rs

1use crate::graphics::pipeline::texture_view::TextureView;
2
3/// One color attachment for a [`Pass`]. `attachment: None` means "the
4/// swapchain's own view" — pass a [`TextureView`] to render into your own
5/// texture instead (e.g. for post-processing).
6pub struct ColorTarget<'a> {
7    pub(crate) attachment: Option<&'a TextureView>,
8    pub(crate) clear: [f32; 4]
9}
10
11pub struct ColorTargetBuilder<'a> {
12    target: ColorTarget<'a>
13}
14
15impl<'a> ColorTargetBuilder<'a> {
16    pub fn new() -> Self {
17        Self {
18            target: ColorTarget {
19                attachment: None,
20                clear: [0.0, 0.0, 0.0, 1.0]
21            }
22        }
23    }
24
25    pub fn with_attachment(mut self, view: &'a TextureView) -> Self {
26        self.target.attachment = Some(view);
27        self
28    }
29
30    pub fn with_clear(mut self, clear: [f32; 4]) -> Self {
31        self.target.clear = clear;
32        self
33    }
34
35    pub fn build(self) -> ColorTarget<'a> {
36        self.target
37    }
38}
39
40/// A depth attachment for a [`Pass`] — unlike a color target, always
41/// pointed at your own texture (there's no swapchain depth buffer).
42pub struct DepthTarget<'a> {
43    pub(crate) attachment: &'a TextureView,
44    pub(crate) clear: Option<f32>,
45}
46
47pub struct DepthTargetBuilder<'a> {
48    target: DepthTarget<'a>
49}
50
51impl<'a> DepthTargetBuilder<'a> {
52    pub fn new(view: &'a TextureView) -> Self {
53        Self {
54            target: DepthTarget {
55                attachment: view,
56                clear: Some(1.0)
57            }
58        }
59    }
60
61    pub fn with_clear(mut self, clear: f32) -> Self {
62        self.target.clear = Some(clear);
63        self
64    }
65
66    pub fn build(self) -> DepthTarget<'a> {
67        self.target
68    }
69}
70
71/// What to render into, passed to [`Frame::begin`](crate::graphics::render::frame::Frame::begin).
72/// Zero color targets plus a depth target is a valid, depth-only pass (e.g.
73/// a shadow map).
74pub struct Pass<'a> {
75    pub(crate) colors: Vec<ColorTarget<'a>>,
76    pub(crate) depth: Option<DepthTarget<'a>>
77}
78
79pub struct PassBuilder<'a> {
80    targets: Vec<ColorTarget<'a>>,
81    depth: Option<DepthTarget<'a>>,
82}
83
84impl<'a> PassBuilder<'a> {
85    pub fn new() -> Self {
86        Self { targets: Vec::new(), depth: None }
87    }
88
89    pub fn with_target(mut self, target: ColorTarget<'a>) -> Self {
90        self.targets.push(target);
91        self
92    }
93
94    pub fn with_depth(mut self, depth: DepthTarget<'a>) -> Self {
95        self.depth = Some(depth);
96        self
97    }
98
99    pub fn build(self) -> Pass<'a> {
100        Pass {
101            colors: self.targets,
102            depth: self.depth,
103        }
104    }
105}