Skip to main content

graphics_core/targets/
mod.rs

1//! Render target abstractions supporting both surface presentation and offscreen rendering.
2
3/// Dimensions of a render target including device pixel ratio scale factor.
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub struct TargetDimensions {
6    pub width: u32,
7    pub height: u32,
8    pub scale_factor: f64,
9}
10
11impl TargetDimensions {
12    pub fn new(width: u32, height: u32, scale_factor: f64) -> Self {
13        Self {
14            width: width.max(1),
15            height: height.max(1),
16            scale_factor: if scale_factor > 0.0 {
17                scale_factor
18            } else {
19                1.0
20            },
21        }
22    }
23
24    /// Physical pixel width.
25    pub fn physical_width(&self) -> u32 {
26        ((self.width as f64) * self.scale_factor).round() as u32
27    }
28
29    /// Physical pixel height.
30    pub fn physical_height(&self) -> u32 {
31        ((self.height as f64) * self.scale_factor).round() as u32
32    }
33}
34
35/// Configuration options for [`OffscreenTarget`] texture creation.
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub struct OffscreenTargetOptions {
38    pub color_usage: wgpu::TextureUsages,
39    pub depth_usage: wgpu::TextureUsages,
40}
41
42impl Default for OffscreenTargetOptions {
43    fn default() -> Self {
44        Self {
45            color_usage: wgpu::TextureUsages::RENDER_ATTACHMENT
46                | wgpu::TextureUsages::COPY_SRC
47                | wgpu::TextureUsages::TEXTURE_BINDING,
48            depth_usage: wgpu::TextureUsages::RENDER_ATTACHMENT
49                | wgpu::TextureUsages::COPY_SRC
50                | wgpu::TextureUsages::TEXTURE_BINDING,
51        }
52    }
53}
54
55impl OffscreenTargetOptions {
56    /// Returns options whose color texture includes `STORAGE_BINDING` (required by compute renderers like Vello).
57    pub fn storage_compatible() -> Self {
58        let mut opts = Self::default();
59        opts.color_usage |= wgpu::TextureUsages::STORAGE_BINDING;
60        opts
61    }
62
63    /// Returns default options for `color_format`, enabling `STORAGE_BINDING` for `Rgba8Unorm`.
64    pub fn for_format(color_format: wgpu::TextureFormat) -> Self {
65        if matches!(color_format, wgpu::TextureFormat::Rgba8Unorm) {
66            Self::storage_compatible()
67        } else {
68            Self::default()
69        }
70    }
71}
72
73/// An offscreen render target owning a color texture and an optional depth texture.
74pub struct OffscreenTarget {
75    pub dimensions: TargetDimensions,
76    pub color_texture: wgpu::Texture,
77    pub color_view: wgpu::TextureView,
78    pub color_format: wgpu::TextureFormat,
79    pub color_usage: wgpu::TextureUsages,
80    pub depth_texture: Option<wgpu::Texture>,
81    pub depth_view: Option<wgpu::TextureView>,
82    pub depth_format: Option<wgpu::TextureFormat>,
83    pub depth_usage: wgpu::TextureUsages,
84}
85
86impl OffscreenTarget {
87    /// Returns the default color texture usages for a given format.
88    pub fn default_color_usages(format: wgpu::TextureFormat) -> wgpu::TextureUsages {
89        OffscreenTargetOptions::for_format(format).color_usage
90    }
91
92    /// Creates a new offscreen render target with default usages for `color_format`.
93    pub fn new(
94        device: &wgpu::Device,
95        dimensions: TargetDimensions,
96        color_format: wgpu::TextureFormat,
97        depth_format: Option<wgpu::TextureFormat>,
98    ) -> Self {
99        Self::new_with_options(
100            device,
101            dimensions,
102            color_format,
103            depth_format,
104            OffscreenTargetOptions::for_format(color_format),
105        )
106    }
107
108    /// Creates a new offscreen render target with an explicit color texture usage mask.
109    pub fn new_with_usage(
110        device: &wgpu::Device,
111        dimensions: TargetDimensions,
112        color_format: wgpu::TextureFormat,
113        depth_format: Option<wgpu::TextureFormat>,
114        color_usage: wgpu::TextureUsages,
115    ) -> Self {
116        Self::new_with_options(
117            device,
118            dimensions,
119            color_format,
120            depth_format,
121            OffscreenTargetOptions {
122                color_usage,
123                ..Default::default()
124            },
125        )
126    }
127
128    /// Creates a new offscreen render target with `STORAGE_BINDING` enabled on the color texture.
129    pub fn new_storage_compatible(
130        device: &wgpu::Device,
131        dimensions: TargetDimensions,
132        color_format: wgpu::TextureFormat,
133        depth_format: Option<wgpu::TextureFormat>,
134    ) -> Self {
135        Self::new_with_options(
136            device,
137            dimensions,
138            color_format,
139            depth_format,
140            OffscreenTargetOptions::storage_compatible(),
141        )
142    }
143
144    /// Creates a new offscreen render target with full [`OffscreenTargetOptions`].
145    pub fn new_with_options(
146        device: &wgpu::Device,
147        dimensions: TargetDimensions,
148        color_format: wgpu::TextureFormat,
149        depth_format: Option<wgpu::TextureFormat>,
150        options: OffscreenTargetOptions,
151    ) -> Self {
152        let phys_w = dimensions.physical_width().max(1);
153        let phys_h = dimensions.physical_height().max(1);
154
155        let color_desc = wgpu::TextureDescriptor {
156            label: Some("graphics_core_offscreen_color"),
157            size: wgpu::Extent3d {
158                width: phys_w,
159                height: phys_h,
160                depth_or_array_layers: 1,
161            },
162            mip_level_count: 1,
163            sample_count: 1,
164            dimension: wgpu::TextureDimension::D2,
165            format: color_format,
166            usage: options.color_usage,
167            view_formats: &[],
168        };
169        let color_texture = device.create_texture(&color_desc);
170        let color_view = color_texture.create_view(&wgpu::TextureViewDescriptor::default());
171
172        let (depth_texture, depth_view) = if let Some(df) = depth_format {
173            let depth_desc = wgpu::TextureDescriptor {
174                label: Some("graphics_core_offscreen_depth"),
175                size: wgpu::Extent3d {
176                    width: phys_w,
177                    height: phys_h,
178                    depth_or_array_layers: 1,
179                },
180                mip_level_count: 1,
181                sample_count: 1,
182                dimension: wgpu::TextureDimension::D2,
183                format: df,
184                usage: options.depth_usage,
185                view_formats: &[],
186            };
187            let tex = device.create_texture(&depth_desc);
188            let view = tex.create_view(&wgpu::TextureViewDescriptor::default());
189            (Some(tex), Some(view))
190        } else {
191            (None, None)
192        };
193
194        Self {
195            dimensions,
196            color_texture,
197            color_view,
198            color_format,
199            color_usage: options.color_usage,
200            depth_texture,
201            depth_view,
202            depth_format,
203            depth_usage: options.depth_usage,
204        }
205    }
206
207    /// Recreates the underlying textures only when physical pixel dimensions change,
208    /// preserving the target's formats and usage flags. Returns `true` if reallocated.
209    pub fn ensure_dimensions(
210        &mut self,
211        device: &wgpu::Device,
212        dimensions: TargetDimensions,
213    ) -> bool {
214        let new_w = dimensions.physical_width().max(1);
215        let new_h = dimensions.physical_height().max(1);
216        if self.dimensions.physical_width().max(1) == new_w
217            && self.dimensions.physical_height().max(1) == new_h
218        {
219            self.dimensions = dimensions;
220            return false;
221        }
222        *self = Self::new_with_options(
223            device,
224            dimensions,
225            self.color_format,
226            self.depth_format,
227            OffscreenTargetOptions {
228                color_usage: self.color_usage,
229                depth_usage: self.depth_usage,
230            },
231        );
232        true
233    }
234}