simple_wgpu/
render_pass.rs

1use crate::{
2    command_encoder::{CommandEncoder, Pass},
3    draw_call::DrawCall,
4    render_texture::RenderTexture,
5};
6
7/// A color attachment for a [RenderPass]
8///
9/// Equivalent to [wgpu::RenderPassColorAttachment]
10#[derive(Debug, Clone)]
11pub struct ColorAttachment {
12    pub target: RenderTexture,
13    pub resolve_target: Option<RenderTexture>,
14    pub ops: wgpu::Operations<wgpu::Color>,
15}
16
17/// A depth/stencil attachment for a [RenderPass]
18///
19/// Equivalent to [wgpu::RenderPassDepthStencilAttachment]
20#[derive(Debug)]
21pub struct DepthStencilAttachment {
22    pub target: RenderTexture,
23    pub depth_ops: Option<wgpu::Operations<f32>>,
24    pub stencil_ops: Option<wgpu::Operations<u32>>,
25}
26
27/// Record a render pass
28///
29/// Create via [`CommandEncoder::render_pass`].
30///
31/// The equivalent to [wgpu::RenderPass].
32pub struct RenderPass<'a> {
33    label: Option<String>,
34    color_attachments: Vec<ColorAttachment>,
35    depth_stencil_attachment: Option<DepthStencilAttachment>,
36    multisample: Option<wgpu::MultisampleState>,
37    draw_calls: Vec<DrawCall>,
38    frame: &'a mut CommandEncoder,
39}
40
41impl<'a> RenderPass<'a> {
42    pub(crate) fn new(
43        label: Option<&str>,
44        color_attachments: Vec<ColorAttachment>,
45        depth_stencil_attachment: Option<DepthStencilAttachment>,
46        multisample: Option<wgpu::MultisampleState>,
47        frame: &'a mut CommandEncoder,
48    ) -> Self {
49        Self {
50            label: label.map(|s| s.to_string()),
51            color_attachments,
52            depth_stencil_attachment,
53            multisample,
54            draw_calls: vec![],
55            frame,
56        }
57    }
58
59    /// Dispatch a draw call
60    pub fn draw(&mut self, draw_call: DrawCall) {
61        self.draw_calls.push(draw_call);
62    }
63}
64
65impl<'a> Drop for RenderPass<'a> {
66    fn drop(&mut self) {
67        self.frame.passes.push(Pass::Render {
68            label: self.label.take(),
69            color_attachments: self.color_attachments.drain(..).collect(),
70            depth_stencil_attachment: self.depth_stencil_attachment.take(),
71            multisample: self.multisample,
72            draw_calls: self.draw_calls.drain(..).collect(),
73        });
74    }
75}