steamengine_renderer/
render_pass.rs

1use wgpu::{
2    QuerySet, RenderPassColorAttachment, RenderPassDepthStencilAttachment, RenderPassDescriptor,
3    RenderPassTimestampWrites, TextureView,
4};
5pub struct RenderPassDescriptorBuilder<'a> {
6    label: &'a str,
7    color_attachments: &'a [Option<RenderPassColorAttachment<'a>>],
8    depth_stencil_attachment: Option<RenderPassDepthStencilAttachment<'a>>,
9    occlusion_query_set: Option<&'a QuerySet>,
10    timestamp_writes: Option<RenderPassTimestampWrites<'a>>,
11}
12impl<'a> RenderPassDescriptorBuilder<'a> {
13    pub fn new(label: &'a str) -> Self {
14        Self {
15            label,
16            color_attachments: &[],
17            depth_stencil_attachment: None,
18            occlusion_query_set: None,
19            timestamp_writes: None,
20        }
21    }
22
23    pub fn with_colors(
24        mut self,
25        color_attachments: &'a [Option<RenderPassColorAttachment>],
26    ) -> Self {
27        self.color_attachments = color_attachments;
28        self
29    }
30    pub fn with_depth(
31        mut self,
32        depth_stencil_attachment: RenderPassDepthStencilAttachment<'a>,
33    ) -> Self {
34        self.depth_stencil_attachment = Some(depth_stencil_attachment);
35        self
36    }
37    pub fn with_occlusion(mut self, occlusion_query_set: &'a QuerySet) -> Self {
38        self.occlusion_query_set = Some(occlusion_query_set);
39        self
40    }
41    pub fn with_timestamp(mut self, timestamp_writes: RenderPassTimestampWrites<'a>) -> Self {
42        self.timestamp_writes = Some(timestamp_writes);
43        self
44    }
45    pub fn build(self) -> RenderPassDescriptor<'a> {
46        RenderPassDescriptor {
47            label: Some(self.label),
48            color_attachments: self.color_attachments,
49            depth_stencil_attachment: self.depth_stencil_attachment,
50            timestamp_writes: self.timestamp_writes,
51            occlusion_query_set: self.occlusion_query_set,
52        }
53    }
54}
55
56#[derive(Clone)]
57pub struct RenderPassColorAttachmentBuilder<'a> {
58    resolve_target: Option<&'a wgpu::TextureView>,
59    ops: wgpu::Operations<wgpu::Color>,
60}
61
62impl<'a> RenderPassColorAttachmentBuilder<'a> {
63    pub fn new() -> Self {
64        Self {
65            resolve_target: None,
66            ops: wgpu::Operations::default(),
67        }
68    }
69    pub fn resolve_target(mut self, resolve_target: &'a wgpu::TextureView) -> Self {
70        self.resolve_target = Some(resolve_target);
71        self
72    }
73    pub fn ops(mut self, ops: wgpu::Operations<wgpu::Color>) -> Self {
74        self.ops = ops;
75        self
76    }
77    pub fn from_color(r: f64, g: f64, b: f64, a: f64) -> Self {
78        Self {
79            resolve_target: None,
80            ops: wgpu::Operations {
81                load: wgpu::LoadOp::Clear(wgpu::Color { r, g, b, a }),
82                store: wgpu::StoreOp::Store,
83            },
84        }
85    }
86    pub fn build(self, view: &'a wgpu::TextureView) -> RenderPassColorAttachment<'a> {
87        RenderPassColorAttachment {
88            view,
89            resolve_target: self.resolve_target,
90            ops: self.ops,
91        }
92    }
93}
94
95pub struct RenderPassDepthStencilAttachmentBuilder {
96    depth_ops: Option<wgpu::Operations<f32>>,
97    stencil_ops: Option<wgpu::Operations<u32>>,
98}
99impl RenderPassDepthStencilAttachmentBuilder {
100    pub fn new() -> Self {
101        Self {
102            depth_ops: None,
103            stencil_ops: None,
104        }
105    }
106    pub fn depth_ops(mut self, ops: wgpu::Operations<f32>) -> Self {
107        self.depth_ops = Some(ops);
108        self
109    }
110    pub fn stencil_ops(mut self, ops: wgpu::Operations<u32>) -> Self {
111        self.stencil_ops = Some(ops);
112        self
113    }
114    pub fn build(self, view: &TextureView) -> RenderPassDepthStencilAttachment {
115        RenderPassDepthStencilAttachment {
116            view,
117            depth_ops: self.depth_ops,
118            stencil_ops: self.stencil_ops,
119        }
120    }
121}