Skip to main content

rouge_render/
target.rs

1use crate::Gpu;
2use crate::gpu::create_texture;
3use wgpu::{
4    CurrentSurfaceTexture, Surface, SurfaceConfiguration, SurfaceTexture, Texture, TextureFormat,
5    TextureUsages, TextureView, TextureViewDescriptor,
6};
7
8pub struct TargetFrame {
9    pub view: TextureView,
10    texture: Option<SurfaceTexture>,
11}
12
13pub enum Acquire {
14    Frame(TargetFrame),
15    Retry,
16    Skip,
17}
18
19pub enum Target {
20    Offscreen(Texture),
21    Swapchain {
22        surface: Surface<'static>,
23        config: SurfaceConfiguration,
24        before_present: Option<Box<dyn Fn() + Send>>,
25    },
26}
27
28impl Target {
29    pub fn offscreen(gpu: &Gpu, width: u32, height: u32) -> Self {
30        Target::Offscreen(create_texture(
31            &gpu.device,
32            "rouge offscreen target",
33            (width.max(1), height.max(1)),
34            TextureFormat::Rgba8Unorm,
35            TextureUsages::RENDER_ATTACHMENT | TextureUsages::COPY_SRC,
36        ))
37    }
38
39    pub fn swapchain(gpu: &Gpu, surface: Surface<'static>, config: SurfaceConfiguration) -> Self {
40        surface.configure(&gpu.device, &config);
41        Target::Swapchain {
42            surface,
43            config,
44            before_present: None,
45        }
46    }
47
48    pub fn before_present(mut self, notify: impl Fn() + Send + 'static) -> Self {
49        if let Target::Swapchain { before_present, .. } = &mut self {
50            *before_present = Some(Box::new(notify));
51        }
52        self
53    }
54
55    pub fn format(&self) -> TextureFormat {
56        match self {
57            Target::Offscreen(texture) => texture.format(),
58            Target::Swapchain { config, .. } => config.format,
59        }
60    }
61
62    pub fn size(&self) -> (u32, u32) {
63        match self {
64            Target::Offscreen(texture) => (texture.width(), texture.height()),
65            Target::Swapchain { config, .. } => (config.width, config.height),
66        }
67    }
68
69    pub fn texture(&self) -> Option<&Texture> {
70        match self {
71            Target::Offscreen(texture) => Some(texture),
72            Target::Swapchain { .. } => None,
73        }
74    }
75
76    pub fn resize(&mut self, gpu: &Gpu, width: u32, height: u32) {
77        if (width, height) == self.size() || width == 0 || height == 0 {
78            return;
79        }
80        match self {
81            Target::Offscreen(_) => *self = Target::offscreen(gpu, width, height),
82            Target::Swapchain {
83                surface, config, ..
84            } => {
85                (config.width, config.height) = (width, height);
86                surface.configure(&gpu.device, config);
87            }
88        }
89    }
90
91    pub fn acquire(&mut self, gpu: &Gpu) -> Acquire {
92        let (texture, surface) = match self {
93            Target::Offscreen(texture) => (texture.clone(), None),
94            Target::Swapchain {
95                surface, config, ..
96            } => match surface.get_current_texture() {
97                CurrentSurfaceTexture::Success(frame)
98                | CurrentSurfaceTexture::Suboptimal(frame) => (frame.texture.clone(), Some(frame)),
99                CurrentSurfaceTexture::Outdated | CurrentSurfaceTexture::Lost => {
100                    surface.configure(&gpu.device, config);
101                    return Acquire::Retry;
102                }
103                CurrentSurfaceTexture::Timeout => return Acquire::Retry,
104                _ => return Acquire::Skip,
105            },
106        };
107        Acquire::Frame(TargetFrame {
108            view: texture.create_view(&TextureViewDescriptor::default()),
109            texture: surface,
110        })
111    }
112
113    pub fn present(&self, gpu: &Gpu, frame: TargetFrame) {
114        if let Target::Swapchain {
115            before_present: Some(notify),
116            ..
117        } = self
118        {
119            notify();
120        }
121        if let Some(texture) = frame.texture {
122            gpu.queue.present(texture);
123        }
124    }
125}