Skip to main content

wgpu/api/
surface_texture.rs

1use crate::*;
2
3/// Surface texture that can be rendered to.
4/// Result of a successful call to [`Surface::get_current_texture`].
5///
6/// This type is unique to the Rust API of `wgpu`. In the WebGPU specification,
7/// the [`GPUCanvasContext`](https://gpuweb.github.io/gpuweb/#canvas-context) provides
8/// a texture without any additional information.
9#[derive(Debug, Clone)]
10pub struct SurfaceTexture {
11    /// Accessible view of the frame.
12    pub texture: Texture,
13    pub(crate) presented: bool,
14    pub(crate) detail: dispatch::DispatchSurfaceOutputDetail,
15}
16#[cfg(send_sync)]
17static_assertions::assert_impl_all!(SurfaceTexture: Send, Sync);
18
19crate::cmp::impl_eq_ord_hash_proxy!(SurfaceTexture => .texture.inner);
20
21/// The swapchain image a [`SurfaceTexture`] is (Cranpose fork).
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub struct SurfaceImage {
24    /// The image's index in the swapchain.
25    pub index: u32,
26    /// Whether the texture holds what the image last presented: the surface
27    /// keeps presented images (see [`Surface::keep_presented_images`]) and
28    /// this one was presented since the surface was configured. Otherwise its
29    /// contents are undefined.
30    pub kept: bool,
31    /// How many images the swapchain hands out: until it has handed out that
32    /// many since it was configured, or one twice, the next may be one it
33    /// never handed out, which holds nothing.
34    pub images: u32,
35}
36
37impl SurfaceTexture {
38    #[cfg(custom)]
39    /// Returns custom implementation of SurfaceTexture (if custom backend and is internally T)
40    pub fn as_custom<T: crate::custom::SurfaceOutputDetailInterface>(&self) -> Option<&T> {
41        self.detail.as_custom()
42    }
43
44    /// The swapchain image this texture is, on a backend whose images are
45    /// numbered (Vulkan); `None` elsewhere (Cranpose fork).
46    pub fn image(&self) -> Option<SurfaceImage> {
47        self.detail.image()
48    }
49}
50
51impl Drop for SurfaceTexture {
52    fn drop(&mut self) {
53        if !self.presented {
54            if thread_panicking() {
55                // Best effort: release reference to `SwapchainAcquireSemaphore`
56                // This fixes <https://github.com/gfx-rs/wgpu/issues/8243>
57                // `Trying to destroy a SwapchainAcquireSemaphore that is still in use by a SurfaceTexture`
58                self.detail.texture_release();
59            } else {
60                self.detail.texture_discard();
61            }
62        }
63    }
64}
65
66/// Result of a call to [`Surface::get_current_texture`].
67///
68/// See variant documentation for how to handle each case.
69#[derive(Debug)]
70pub enum CurrentSurfaceTexture {
71    /// Successfully acquired a surface texture with no issues.
72    Success(SurfaceTexture),
73    /// Successfully acquired a surface texture, but texture no longer matches the properties of the underlying surface.
74    /// It's highly recommended to call [`Surface::configure`] again for optimal performance.
75    Suboptimal(SurfaceTexture),
76    /// A timeout was encountered while trying to acquire the next frame.
77    ///
78    /// Applications should skip the current frame and try again later.
79    Timeout,
80    /// The window is occluded (e.g. minimized or behind another window).
81    ///
82    /// Applications should skip the current frame and try again once the window
83    /// is no longer occluded.
84    Occluded,
85    /// The underlying surface has changed, and therefore the surface configuration is outdated.
86    ///
87    /// Call [`Surface::configure()`] and try again.
88    Outdated,
89    /// The surface has been lost and needs to be recreated.
90    ///
91    /// If the device as a whole is lost (see [`set_device_lost_callback()`][crate::Device::set_device_lost_callback]), then
92    /// you need to recreate the device and all resources.
93    /// Otherwise, call [`Instance::create_surface()`] to recreate the surface,
94    /// then [`Surface::configure()`], and try again.
95    Lost,
96    /// A validation error inside [`Surface::get_current_texture()`] was raised
97    /// and caught by an [error scope](crate::Device::push_error_scope) or
98    /// [`on_uncaptured_error()`][crate::Device::on_uncaptured_error].
99    ///
100    /// Applications should attend to the validation error and try again.
101    Validation,
102}
103
104fn thread_panicking() -> bool {
105    cfg_if::cfg_if! {
106        if #[cfg(std)] {
107            std::thread::panicking()
108        } else if #[cfg(panic = "abort")] {
109            // If `panic = "abort"` then a thread _cannot_ be observably panicking by definition.
110            false
111        } else {
112            // TODO: This is potentially overly pessimistic; it may be appropriate to instead allow a
113            // texture to not be discarded.
114            // Alternatively, this could _also_ be a `panic!`, since we only care if the thread is panicking
115            // when the surface has not been presented.
116            compile_error!(
117                "cannot determine if a thread is panicking without either `panic = \"abort\"` or `std`"
118            );
119        }
120    }
121}