summaryrefslogtreecommitdiffstats
path: root/wgpu/src/window/swap_chain.rs
blob: 6f545fceac3b2fb3839cc3c3ab97259036709165 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::Viewport;

/// The rendering target of a window.
///
/// It represents a series of virtual framebuffers with a scale factor.
#[derive(Debug)]
pub struct SwapChain {
    raw: wgpu::SwapChain,
    viewport: Viewport,
}

impl SwapChain {}

impl SwapChain {
    /// Creates a new [`SwapChain`] for the given surface.
    ///
    /// [`SwapChain`]: struct.SwapChain.html
    pub fn new(
        device: &wgpu::Device,
        surface: &wgpu::Surface,
        width: u32,
        height: u32,
    ) -> SwapChain {
        SwapChain {
            raw: new_swap_chain(surface, width, height, device),
            viewport: Viewport::new(width, height),
        }
    }

    /// Returns the next frame of the [`SwapChain`] alongside its [`Viewport`].
    ///
    /// [`SwapChain`]: struct.SwapChain.html
    /// [`Viewport`]: ../struct.Viewport.html
    pub fn next_frame(&mut self) -> (wgpu::SwapChainOutput<'_>, &Viewport) {
        (self.raw.get_next_texture(), &self.viewport)
    }
}

fn new_swap_chain(
    surface: &wgpu::Surface,
    width: u32,
    height: u32,
    device: &wgpu::Device,
) -> wgpu::SwapChain {
    device.create_swap_chain(
        &surface,
        &wgpu::SwapChainDescriptor {
            usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
            format: wgpu::TextureFormat::Bgra8UnormSrgb,
            width,
            height,
            present_mode: wgpu::PresentMode::Vsync,
        },
    )
}