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