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}
32
33impl SurfaceTexture {
34    #[cfg(custom)]
35    /// Returns custom implementation of SurfaceTexture (if custom backend and is internally T)
36    pub fn as_custom<T: crate::custom::SurfaceOutputDetailInterface>(&self) -> Option<&T> {
37        self.detail.as_custom()
38    }
39
40    /// The swapchain image this texture is, on a backend whose images are
41    /// numbered (Vulkan); `None` elsewhere (Cranpose fork).
42    pub fn image(&self) -> Option<SurfaceImage> {
43        self.detail.image()
44    }
45}
46
47impl Drop for SurfaceTexture {
48    fn drop(&mut self) {
49        if !self.presented {
50            if thread_panicking() {
51                // Best effort: release reference to `SwapchainAcquireSemaphore`
52                // This fixes <https://github.com/gfx-rs/wgpu/issues/8243>
53                // `Trying to destroy a SwapchainAcquireSemaphore that is still in use by a SurfaceTexture`
54                self.detail.texture_release();
55            } else {
56                self.detail.texture_discard();
57            }
58        }
59    }
60}
61
62/// Result of a call to [`Surface::get_current_texture`].
63///
64/// See variant documentation for how to handle each case.
65#[derive(Debug)]
66pub enum CurrentSurfaceTexture {
67    /// Successfully acquired a surface texture with no issues.
68    Success(SurfaceTexture),
69    /// Successfully acquired a surface texture, but texture no longer matches the properties of the underlying surface.
70    /// It's highly recommended to call [`Surface::configure`] again for optimal performance.
71    Suboptimal(SurfaceTexture),
72    /// A timeout was encountered while trying to acquire the next frame.
73    ///
74    /// Applications should skip the current frame and try again later.
75    Timeout,
76    /// The window is occluded (e.g. minimized or behind another window).
77    ///
78    /// Applications should skip the current frame and try again once the window
79    /// is no longer occluded.
80    Occluded,
81    /// The underlying surface has changed, and therefore the surface configuration is outdated.
82    ///
83    /// Call [`Surface::configure()`] and try again.
84    Outdated,
85    /// The surface has been lost and needs to be recreated.
86    ///
87    /// If the device as a whole is lost (see [`set_device_lost_callback()`][crate::Device::set_device_lost_callback]), then
88    /// you need to recreate the device and all resources.
89    /// Otherwise, call [`Instance::create_surface()`] to recreate the surface,
90    /// then [`Surface::configure()`], and try again.
91    Lost,
92    /// A validation error inside [`Surface::get_current_texture()`] was raised
93    /// and caught by an [error scope](crate::Device::push_error_scope) or
94    /// [`on_uncaptured_error()`][crate::Device::on_uncaptured_error].
95    ///
96    /// Applications should attend to the validation error and try again.
97    Validation,
98}
99
100fn thread_panicking() -> bool {
101    cfg_if::cfg_if! {
102        if #[cfg(std)] {
103            std::thread::panicking()
104        } else if #[cfg(panic = "abort")] {
105            // If `panic = "abort"` then a thread _cannot_ be observably panicking by definition.
106            false
107        } else {
108            // TODO: This is potentially overly pessimistic; it may be appropriate to instead allow a
109            // texture to not be discarded.
110            // Alternatively, this could _also_ be a `panic!`, since we only care if the thread is panicking
111            // when the surface has not been presented.
112            compile_error!(
113                "cannot determine if a thread is panicking without either `panic = \"abort\"` or `std`"
114            );
115        }
116    }
117}