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