Skip to main content

cranpose_render_wgpu/
initial_present.rs

1use crate::{
2    frame_graph::{WgpuFrameGraph, WgpuFrameGraphExecutor},
3    render::CLEAR_COLOR,
4};
5
6/// Clears `view` to the framework's default background; see
7/// [`clear_to_background`].
8pub fn clear_to_default_background(
9    device: &wgpu::Device,
10    queue: &wgpu::Queue,
11    view: &wgpu::TextureView,
12) {
13    clear_to_background(device, queue, view, CLEAR_COLOR);
14}
15
16/// Clears `view` to `color` and submits the
17/// work, through the same `WgpuFrameGraph` every other command encoder
18/// and submission in this crate is required to go through (enforced by
19/// `render_contract.rs`'s `wgpu_command_buffers_are_owned_by_frame_graph_executor`)
20/// — a fresh, one-shot `WgpuFrameGraphExecutor`, since a placeholder
21/// clear has no frame-to-frame state worth pooling. Does not present or
22/// acquire anything itself — callers that mean to show the clear on
23/// screen still acquire a frame and call `wgpu::SurfaceTexture::present`
24/// themselves, exactly as a real content frame would.
25pub fn clear_to_background(
26    device: &wgpu::Device,
27    queue: &wgpu::Queue,
28    view: &wgpu::TextureView,
29    color: wgpu::Color,
30) {
31    let mut graph = WgpuFrameGraph::new(Some("Cranpose Initial Present Clear"));
32    let target = graph.import_surface("initial-present-clear-target");
33    graph.add_fallible_command_pass(Some("Initial Present Clear"), &[], &[target], |context| {
34        let _pass = context
35            .encoder
36            .begin_render_pass(&wgpu::RenderPassDescriptor {
37                label: Some("Cranpose Initial Present Clear"),
38                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
39                    view,
40                    resolve_target: None,
41                    depth_slice: None,
42                    ops: wgpu::Operations {
43                        load: wgpu::LoadOp::Clear(color),
44                        store: wgpu::StoreOp::Store,
45                    },
46                })],
47                depth_stencil_attachment: None,
48                timestamp_writes: None,
49                occlusion_query_set: None,
50                multiview_mask: None,
51            });
52        Ok(())
53    });
54    if let Err(error) = WgpuFrameGraphExecutor::new().execute_recorded_graph(device, queue, graph) {
55        log::error!("[initial-present] placeholder clear failed: {error:?}");
56    }
57}