Skip to main content

pebble/graphics/render/
frame.rs

1use crate::graphics::render::{render_pass::RenderPass, targets::Pass};
2
3pub struct Frame {
4    encoder: wgpu::CommandEncoder,
5    view: wgpu::TextureView,
6    surface: wgpu::SurfaceTexture
7}
8
9impl Frame {
10    pub(crate) fn new(encoder: wgpu::CommandEncoder, view: wgpu::TextureView, surface: wgpu::SurfaceTexture) -> Self {
11        Self { encoder, view, surface }
12    }
13
14    pub(crate) fn finish(self) -> (wgpu::CommandEncoder, wgpu::SurfaceTexture) {
15        (self.encoder, self.surface)
16    }
17
18    pub fn begin<'a>(&'a mut self, pass: Pass) -> RenderPass<'a> {
19        let color_attachments: Vec<_> = pass
20            .colors
21            .iter()
22            .map(|target| {
23                let view = target.attachment.map(|t| t.raw()).unwrap_or(&self.view);
24
25                Some(wgpu::RenderPassColorAttachment {
26                    view,
27                    resolve_target: None,
28                    depth_slice: None,
29                    ops: wgpu::Operations {
30                        load: wgpu::LoadOp::Clear(wgpu::Color {
31                            r: target.clear[0] as f64,
32                            g: target.clear[1] as f64,
33                            b: target.clear[2] as f64,
34                            a: target.clear[3] as f64,
35                        }),
36                        store: wgpu::StoreOp::Store
37                    }
38                })
39            }).collect();
40
41        let depth_stencil_attachment = pass.depth.as_ref().map(|d| wgpu::RenderPassDepthStencilAttachment {
42            view: d.attachment.raw(),
43            depth_ops: Some(wgpu::Operations {
44                load: wgpu::LoadOp::Clear(d.clear.unwrap()),
45                store: wgpu::StoreOp::Store,
46            }),
47            stencil_ops: None
48        });
49
50        RenderPass::new(self.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
51            label: None,
52            color_attachments: &color_attachments,
53            depth_stencil_attachment,
54            timestamp_writes: None,
55            occlusion_query_set: None,
56            multiview_mask: None
57        }))
58    }
59}
60
61pub struct ActiveFrame<'a> {
62    frame: &'a mut Frame
63}
64
65impl<'a> ActiveFrame<'a>{
66    pub fn begin_pass(&'a mut self, pass: Pass) -> RenderPass<'a> {
67        self.frame.begin(pass)
68    }
69}
70
71impl<'a> std::ops::Deref for ActiveFrame<'a> {
72    type Target = Frame;
73    
74    fn deref(&self) -> &Self::Target {
75        self.frame
76    }
77}
78
79impl<'a> std::ops::DerefMut for ActiveFrame<'a> {
80    fn deref_mut(&mut self) -> &mut Self::Target {
81        self.frame
82    }
83}
84
85#[derive(Default)]
86pub struct CurrentFrame {
87    frame: Option<Frame>
88}
89
90impl CurrentFrame {
91    pub(crate) fn set(&mut self, frame: Frame) {
92        self.frame = Some(frame);
93    }
94
95    pub(crate) fn take(&mut self) -> Option<Frame> {
96        self.frame.take()
97    }
98
99    pub fn active<'a>(&'a mut self) -> Option<ActiveFrame<'a>>{
100        self.frame.as_mut().map(|f| ActiveFrame {
101            frame: f
102        })
103    }
104}