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
10/// Win32 `HWND`, handed to a Win32 [`NativeSurface`] at construction so it can
11/// build its [`DxgiHdrSource`](crate::auxil::dxgi::hdr::DxgiHdrSource) for the
12/// display-HDR query. Borrowed from the app's window.
13///
14/// A newtype rather than a bare `isize` so the cfg lives in one place: off Windows
15/// it is an uninhabited enum, so the non-Windows surface paths can't construct one
16/// and the constructor signatures stay cfg-free.
17#[cfg(windows)]
18#[derive(Clone, Copy)]
19pub(crate) struct WindowHandle(pub(crate) windows::Win32::Foundation::HWND);
20
21#[cfg(not(windows))]
22#[derive(Clone, Copy)]
23pub(crate) enum WindowHandle {}
24
25pub(super) trait Surface: Send + Sync + 'static {
26 /// Returns the surface capabilities for the given adapter.
27 ///
28 /// Returns `None` if the surface is not compatible with the adapter.
29 fn surface_capabilities(&self, adapter: &super::Adapter) -> Option<crate::SurfaceCapabilities>;
30
31 /// Creates a swapchain for the surface with the given configuration.
32 ///
33 /// If this is not the first swapchain created for the surface, the old swapchain
34 /// must be provided. [`Swapchain::release_resources`] must be called on the old swapchain
35 /// before calling this method.
36 unsafe fn create_swapchain(
37 &self,
38 device: &super::Device,
39 config: &crate::SurfaceConfiguration,
40 provided_old_swapchain: Option<Box<dyn Swapchain>>,
41 ) -> Result<Box<dyn Swapchain>, crate::SurfaceError>;
42
43 /// This surface's current display HDR info, if it can report it.
44 ///
45 /// `Some` only for Win32 surfaces (read through DXGI); `None` otherwise
46 /// (Wayland / X11 / Android / Metal), which is the default.
47 fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> {
48 None
49 }
50
51 /// Allows downcasting to the concrete type.
52 fn as_any(&self) -> &dyn Any;
53}
54
55pub(super) trait Swapchain: Send + Sync + 'static {
56 /// Releases all resources associated with the swapchain, without
57 /// destroying the swapchain itself. Must be called before calling
58 /// either [`Surface::create_swapchain`] or dropping the swapchain.
59 ///
60 /// The swapchain must not be in use when this is called.
61 unsafe fn release_resources(&mut self, device: &super::Device);
62
63 /// Acquires the next available surface texture for rendering.
64 ///
65 /// `timeout` specifies the maximum time to wait for an image to become available.
66 /// If `None` is specified, this function will wait indefinitely.
67 ///
68 /// Returns `Err(SurfaceError::Timeout)` if the timeout elapsed before an image became available.
69 unsafe fn acquire(
70 &mut self,
71 timeout: Option<Duration>,
72 fence: &super::Fence,
73 ) -> Result<crate::AcquiredSurfaceTexture<crate::api::Vulkan>, crate::SurfaceError>;
74
75 /// Tries to discard the acquired texture without presenting it.
76 ///
77 /// In practice, this doesn't really work in the current implementations.
78 unsafe fn discard_texture(
79 &mut self,
80 texture: super::SurfaceTexture,
81 ) -> Result<(), crate::SurfaceError>;
82
83 /// Presents the given surface texture using the queue.
84 unsafe fn present(
85 &mut self,
86 queue: &super::Queue,
87 texture: crate::vulkan::SurfaceTexture,
88 ) -> Result<(), crate::SurfaceError>;
89
90 /// Allows downcasting to the concrete type.
91 fn as_any(&self) -> &dyn Any;
92
93 /// Allows downcasting to the concrete type mutably.
94 fn as_any_mut(&mut self) -> &mut dyn Any;
95}
96
97/// Swapchain specific metadata associated with a surface texture.
98pub(super) trait SurfaceTextureMetadata: Debug + Send + Sync + 'static {
99 /// Returns a guard which can yield the semaphores needed for submission using this swapchain texture.
100 fn get_semaphore_guard(&self) -> Box<dyn SwapchainSubmissionSemaphoreGuard + '_>;
101
102 /// Allows downcasting to the concrete type.
103 fn as_any(&self) -> &dyn Any;
104}
105
106/// Guard type for managing swapchain submission semaphores.
107pub(super) trait SwapchainSubmissionSemaphoreGuard {
108 /// Sets the Fence value for this submission.
109 fn set_used_fence_value(&mut self, value: u64);
110
111 /// Gets semaphores to wait on before doing GPU work for this swapchain texture.
112 fn get_acquire_wait_semaphore(&mut self) -> Option<SemaphoreType>;
113
114 /// Gets the semaphore to signal when GPU work for this swapchain texture is complete.
115 fn get_submit_signal_semaphore(
116 &mut self,
117 device: &DeviceShared,
118 ) -> Result<SemaphoreType, crate::DeviceError>;
119}