Skip to main content

alpha2_render/
present.rs

1//! Presenting rendered frames on a window's surface.
2
3/// The pipeline that copies a rendered texture onto a presentation surface (a canvas, a
4/// `CAMetalLayer`, an Android window), stretched to the surface's size.
5pub struct SurfacePresenter {
6    bind_groups: crate::bind_group::BindGroups,
7    layout: wgpu::BindGroupLayout,
8    pipeline: wgpu::RenderPipeline,
9    sampler: crate::bind_group::LinearSampler,
10}
11
12impl SurfacePresenter {
13    /// A presenter drawing into surfaces of `format`.
14    #[must_use]
15    pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
16        let bind_groups = crate::bind_group::BindGroups::default();
17        let sampler = crate::bind_group::LinearSampler::new(device, &bind_groups);
18        let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
19            label: Some("alpha2 surface presenter layout"),
20            entries: &[wgpu::BindGroupLayoutEntry {
21                binding: 0,
22                visibility: wgpu::ShaderStages::FRAGMENT,
23                ty: wgpu::BindingType::Texture {
24                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
25                    view_dimension: wgpu::TextureViewDimension::D2,
26                    multisampled: false,
27                },
28                count: None,
29            }],
30        });
31        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
32            label: Some("alpha2 surface presenter shader"),
33            source: wgpu::ShaderSource::Wgsl(alpha2_shaders::CANVAS_PRESENT_WGSL.into()),
34        });
35        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
36            label: Some("alpha2 surface presenter pipeline layout"),
37            bind_group_layouts: &[Some(&layout), Some(&sampler.layout)],
38            immediate_size: 0,
39        });
40        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
41            label: Some("alpha2 surface presenter pipeline"),
42            layout: Some(&pipeline_layout),
43            vertex: wgpu::VertexState {
44                module: &shader,
45                entry_point: Some("vs"),
46                compilation_options: wgpu::PipelineCompilationOptions::default(),
47                buffers: &[],
48            },
49            primitive: wgpu::PrimitiveState::default(),
50            depth_stencil: None,
51            multisample: wgpu::MultisampleState::default(),
52            fragment: Some(wgpu::FragmentState {
53                module: &shader,
54                entry_point: Some("fs"),
55                compilation_options: wgpu::PipelineCompilationOptions::default(),
56                targets: &[Some(wgpu::ColorTargetState {
57                    format,
58                    blend: None,
59                    write_mask: wgpu::ColorWrites::ALL,
60                })],
61            }),
62            multiview_mask: None,
63            cache: None,
64        });
65        Self {
66            bind_groups,
67            layout,
68            pipeline,
69            sampler,
70        }
71    }
72
73    /// Draws `source` over all of `target` and submits the work.
74    pub fn draw(
75        &self,
76        device: &wgpu::Device,
77        gpu: &alpha2_gpu::GpuContext,
78        source: &wgpu::TextureView,
79        target: &wgpu::TextureView,
80    ) {
81        let bind_group = self.bind_groups.create(
82            device,
83            &wgpu::BindGroupDescriptor {
84                label: Some("alpha2 surface presenter bind group"),
85                layout: &self.layout,
86                entries: &[wgpu::BindGroupEntry {
87                    binding: 0,
88                    resource: wgpu::BindingResource::TextureView(source),
89                }],
90            },
91            0,
92        );
93        // Submitted before returning, reading only the caller's view: no pooled texture can be
94        // let go of while it is unsubmitted.
95        #[allow(clippy::disallowed_methods)]
96        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
97            label: Some("alpha2 surface presenter encoder"),
98        });
99        {
100            let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
101                label: Some("alpha2 surface presenter pass"),
102                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
103                    view: target,
104                    depth_slice: None,
105                    resolve_target: None,
106                    ops: wgpu::Operations {
107                        load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
108                        store: wgpu::StoreOp::Store,
109                    },
110                })],
111                depth_stencil_attachment: None,
112                timestamp_writes: None,
113                occlusion_query_set: None,
114                multiview_mask: None,
115            });
116            pass.set_pipeline(&self.pipeline);
117            pass.set_bind_group(0, &bind_group, &[]);
118            self.sampler.set(&mut pass);
119            pass.draw(0..3, 0..1);
120        }
121        gpu.submit([encoder.finish()]);
122    }
123}