wgpu_hal/vulkan/swapchain/
mod.rs

1use alloc::boxed::Box;
2use core::{any::Any, fmt::Debug, time::Duration};
3
4use crate::vulkan::{semaphore_list::SemaphoreType, DeviceShared};
5
6pub(super) use native::*;
7
8mod native;
9
10pub(super) trait Surface: Send + Sync + 'static {
11    /// Deletes the surface and associated resources.
12    ///
13    /// The surface must not be in use when it is deleted.
14    unsafe fn delete_surface(self: Box<Self>);
15
16    /// Returns the surface capabilities for the given adapter.
17    ///
18    /// Returns `None` if the surface is not compatible with the adapter.
19    fn surface_capabilities(&self, adapter: &super::Adapter) -> Option<crate::SurfaceCapabilities>;
20
21    /// Creates a swapchain for the surface with the given configuration.
22    ///
23    /// If this is not the first swapchain created for the surface, the old swapchain
24    /// must be provided. [`Swapchain::release_resources`] must be called on the old swapchain
25    /// before calling this method.
26    unsafe fn create_swapchain(
27        &self,
28        device: &super::Device,
29        config: &crate::SurfaceConfiguration,
30        provided_old_swapchain: Option<Box<dyn Swapchain>>,
31    ) -> Result<Box<dyn Swapchain>, crate::SurfaceError>;
32
33    /// Allows downcasting to the concrete type.
34    fn as_any(&self) -> &dyn Any;
35}
36
37pub(super) trait Swapchain: Send + Sync + 'static {
38    /// Releases all resources associated with the swapchain, without
39    /// destroying the swapchain itself. Must be called before calling
40    /// either [`Surface::create_swapchain`] or [`Swapchain::delete_swapchain`].
41    ///
42    /// The swapchain must not be in use when this is called.
43    unsafe fn release_resources(&mut self, device: &super::Device);
44
45    /// Deletes the swapchain.
46    ///
47    /// The swapchain must not be in use when it is deleted and
48    /// [`Swapchain::release_resources`] must have been called first.
49    unsafe fn delete_swapchain(self: Box<Self>);
50
51    /// Acquires the next available surface texture for rendering.
52    ///
53    /// `timeout` specifies the maximum time to wait for an image to become available.
54    /// If `None` is specified, this function will wait indefinitely.
55    ///
56    /// Returns `Ok(None)` if the timeout elapsed before an image became available.
57    unsafe fn acquire(
58        &mut self,
59        timeout: Option<Duration>,
60        fence: &super::Fence,
61    ) -> Result<Option<crate::AcquiredSurfaceTexture<crate::api::Vulkan>>, crate::SurfaceError>;
62
63    /// Tries to discard the acquired texture without presenting it.
64    ///
65    /// In practice, this doesn't really work in the current implementations.
66    unsafe fn discard_texture(
67        &mut self,
68        texture: super::SurfaceTexture,
69    ) -> Result<(), crate::SurfaceError>;
70
71    /// Presents the given surface texture using the queue.
72    unsafe fn present(
73        &mut self,
74        queue: &super::Queue,
75        texture: crate::vulkan::SurfaceTexture,
76    ) -> Result<(), crate::SurfaceError>;
77
78    /// Allows downcasting to the concrete type.
79    fn as_any(&self) -> &dyn Any;
80
81    /// Allows downcasting to the concrete type mutably.
82    fn as_any_mut(&mut self) -> &mut dyn Any;
83}
84
85/// Swapchain specific metadata associated with a surface texture.
86pub(super) trait SurfaceTextureMetadata: Debug + Send + Sync + 'static {
87    /// Returns a guard which can yield the semaphores needed for submission using this swapchain texture.
88    fn get_semaphore_guard(&self) -> Box<dyn SwapchainSubmissionSemaphoreGuard + '_>;
89
90    /// Allows downcasting to the concrete type.
91    fn as_any(&self) -> &dyn Any;
92}
93
94/// Guard type for managing swapchain submission semaphores.
95pub(super) trait SwapchainSubmissionSemaphoreGuard {
96    /// Sets the Fence value for this submission.
97    fn set_used_fence_value(&mut self, value: u64);
98
99    /// Gets semaphores to wait on before doing GPU work for this swapchain texture.
100    fn get_acquire_wait_semaphore(&mut self) -> Option<SemaphoreType>;
101
102    /// Gets the semaphore to signal when GPU work for this swapchain texture is complete.
103    fn get_submit_signal_semaphore(
104        &mut self,
105        device: &DeviceShared,
106    ) -> Result<SemaphoreType, crate::DeviceError>;
107}