JFLib

gpu/vk_swap.rs#

Source: lib/gpu/vk_swap.rs

vk_swap -- window surfaces and swapchains

  Surface::new(&instance, window) -> Result<Surface>   a plat window (Windows: its HWND and
       HINSTANCE through plat::win_native, VK_KHR_win32_surface; Android: the ANativeWindow of
       plat::win_gpu_child, VK_KHR_android_surface, NotReady while there is none); other targets:
       Unsupported
  device.create_swapchain(&surface, &SwapchainDesc) -> Result<Swapchain>
       B8G8R8A8 (or R8G8B8A8) UNORM / SRGB, sRGB non-linear colour space; the present mode of
       the desc or jfgui_GPU_PRESENT=fifo|mailbox|immediate, FIFO when the surface lacks it;
       image count: the surface's minimum + 1 (or SwapchainDesc::min_images); copy_src adds
       TRANSFER_SRC (read-back)
  sc.acquire() -> Result<Option<Frame>>    None while the window is minimised or has no area;
       rebuilds the swapchain first when the window's size changed, the last present said
       out-of-date / suboptimal, resize() or set_present_mode() asked for it, or the last frame
       was never presented; waits until the frame slot's previous submission is done
       (frames_in_flight)
  sc.try_acquire() -> Result<TryAcquire>   never waits, never rebuilds: Frame, NotReady (no
       image free yet, or the frame slot's previous submission runs), Stale (the window's size
       changed, the last present said out-of-date / suboptimal, resize() asked: rebuild() first),
       NoArea (minimised)
  sc.rebuild()                             what acquire() does when stale (any thread, once
       the swapchain has moved there; waits for the queue to be idle)
  sc.view(&frame) / texture(&frame)        the image: Layout::Undefined at the start of the
       frame's commands, Layout::Present at their end
  sc.present(frame, [cmd, ..]) -> Result<SubmitId>   submit (waiting for the image at the
       colour-output, copy and compute stages, signalling the image's render-done semaphore),
       then vkQueuePresentKHR
  sc.size(), format(), present_mode(), resize(), set_present_mode(m)

Rebuilding (and dropping) waits for the queue to be idle -- vkQueueWaitIdle covers the
presents too, the timeline does not see a present's semaphore wait -- then creates the new
swapchain with the old one as oldSwapchain and destroys the old one with its views and
semaphores (resizes are rare; a frame never waits for this).  Semaphores: one acquire
semaphore per frame slot (reused once the slot's submission completed), one render-done
semaphore per image (the present that waited on it has finished when the image is acquired
again).
The window must not be presented by the CPU (plat::win_present) at the same time: a plat
window that never presents keeps nothing of its own to paint (win_api.rs WM_PAINT).

Android: the image stays upright (VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) and the compositor turns
it; the surface's currentTransform would ask us to draw it turned, with every rectangle turned as
well.  The system then says SUBOPTIMAL at every present while the display is turned, which means
nothing here: a new size is seen in window_size (a surface there never says OUT_OF_DATE for it).
  • struct Surface(pub(crate) Arc<SurfShared>) a window as a target for swapchains

impl Surface

  • fn track_size(&self) (Linux: the window's size for a rebuild on a worker; here the driver knows it)

  • fn new(instance: &Instance, window: i32) -> Result<Surface> the surface of plat window window (an index of plat::win_create)

  • fn native(instance: &Instance, nw: plat::NativeWindow, window: i32) -> Result<Surface> the surface of a window given by its handles (the ANativeWindow of plat::win_gpu_child) whose size and state are those of plat window window

  • fn native(instance: &Instance, nw: plat::NativeWindow, window: i32) -> Result<Surface> the surface of a window given by its handles (Windows: the child of plat::win_gpu_child) whose size and state are those of plat window window

  • struct Frame an acquired swapchain image (present it with Swapchain::present)

  • enum TryAcquire what Swapchain::try_acquire found

    • Frame(Frame)
    • NotReady -- no image is free yet, or the frame slot's previous submission still runs: try again later
    • Stale -- the swapchain does not fit the window any more: Swapchain::rebuild first
    • NoArea -- the window is minimised or has no area

impl Frame

  • fn index(&self) -> u32 the image's index in the swapchain

  • fn size(&self) -> (u32, u32)

  • struct Swapchain the images a window shows, presented in turn

impl Device

  • fn create_swapchain(&self, surface: &Surface, desc: &SwapchainDesc) -> Result<Swapchain>

impl Swapchain

  • fn rebuild(&mut self) -> Result<()> (re)create the swapchain for the window's size now; the old images may still be in use: waits for the queue to be idle (resizes are rare)
  • fn try_acquire(&mut self) -> Result<TryAcquire> the next image to draw into, without waiting and without rebuilding (see TryAcquire)
  • fn acquire(&mut self) -> Result<Option<Frame>> the next image to draw into; None while the window is minimised or has no area
  • fn view(&self, frame: &Frame) -> &TextureView the frame's image as a render target (Undefined at the start, Present at the end)
  • fn texture(&self, frame: &Frame) -> &Texture the frame's image (copies: SwapchainDesc::copy_src)
  • fn present<'a, I: IntoIterator<Item = CommandBuffer<'a>>>(&self, frame: Frame, cmds: I) -> Result<SubmitId> submit the frame's commands (they leave the image in Layout::Present) and show it
  • fn prepare(&mut self) -> Result<bool> (Linux: the display has answered for the images; here nothing to wait for)
  • fn flush(&self) -> Result<bool> (Linux: a frame reaches the window after the GPU finished it; here present has done that)
  • fn busy(&self) -> bool (Linux: a frame is on its way to the window; here never)
  • fn next_look(&self, retry: u64) -> u64 when a frame that found NotReady should try again: retry (the caller's time)
  • fn damage(&self, _rects: Option<&[[i32; 4]]>) (Linux: what the next image changes, for the compositor; here the whole image counts)
  • fn resize(&mut self) rebuild at the next acquire (the window's size is also checked at every acquire)
  • fn set_present_mode(&mut self, m: PresentMode) another present mode from the next acquire on (FIFO when the surface lacks it)
  • fn present_mode(&self) -> PresentMode
  • fn format(&self) -> Format
  • fn size(&self) -> (u32, u32) the images' size (0, 0 while the window has no area)
  • fn image_count(&self) -> usize

Source: docs/API.md in the JFLib repository.