wgpu_noboiler/
render_pass.rs

1use wgpu::{
2    Color, CommandEncoder, LoadOp, Operations, RenderPass, RenderPassColorAttachment,
3    RenderPassDepthStencilAttachment, RenderPassDescriptor, TextureView,
4};
5
6/// Builder Patter for wgpu [RenderPass]
7pub struct RenderPassCreator<'a> {
8    view: &'a TextureView,
9
10    label: &'a str,
11
12    clear_color: Color,
13
14    depth_stencil_attachment: Option<RenderPassDepthStencilAttachment<'a>>,
15
16    color_attachments: Vec<Option<RenderPassColorAttachment<'a>>>,
17}
18
19impl<'a> RenderPassCreator<'a> {
20    pub fn new(view: &'a TextureView) -> RenderPassCreator<'a> {
21        RenderPassCreator {
22            view,
23            label: "Render Pass",
24            clear_color: Color::WHITE,
25            depth_stencil_attachment: None,
26            color_attachments: vec![],
27        }
28    }
29
30    /// sets label (name)
31    pub fn label(mut self, label: &'a str) -> Self {
32        self.label = label;
33        self
34    }
35
36    /// sets clear_color (background color)
37    pub fn clear_color(mut self, clear_color: Color) -> Self {
38        self.clear_color = clear_color;
39        self
40    }
41
42    /// sets the used [RenderPassDepthStencilAttachment]
43    pub fn depth_stencil_attachment(
44        mut self,
45        depth_stencil_attachment: RenderPassDepthStencilAttachment<'a>,
46    ) -> Self {
47        self.depth_stencil_attachment = Some(depth_stencil_attachment);
48        self
49    }
50
51    /// creates a [RenderPass]
52    pub fn build(mut self, encoder: &'a mut CommandEncoder) -> RenderPass<'a> {
53        self.color_attachments.push(Some(RenderPassColorAttachment {
54            view: self.view,
55            resolve_target: None,
56            ops: Operations {
57                load: LoadOp::Clear(self.clear_color),
58                store: true,
59            },
60        }));
61
62        let descriptor = RenderPassDescriptor {
63            label: Some(self.label),
64            color_attachments: &self.color_attachments,
65            depth_stencil_attachment: self.depth_stencil_attachment.clone(),
66        };
67
68        encoder.begin_render_pass(&descriptor)
69    }
70
71    /// returns the [RenderPassDescriptor]
72    pub fn descriptor(&'a mut self) -> RenderPassDescriptor<'a, 'a> {
73        self.color_attachments.push(Some(RenderPassColorAttachment {
74            view: self.view,
75            resolve_target: None,
76            ops: Operations {
77                load: LoadOp::Clear(self.clear_color),
78                store: true,
79            },
80        }));
81
82        RenderPassDescriptor {
83            label: Some(self.label),
84            color_attachments: &self.color_attachments,
85            depth_stencil_attachment: self.depth_stencil_attachment.clone(),
86        }
87    }
88}