JFLib

gpu/mtl_swap.rs (Apple)#

Source: lib/gpu/mtl_swap.rs

mtl_swap -- swapchains on Metal (AP6c, docs/APPLE.md): a CAMetalLayer on a plat window

  Surface::new(&instance, window)        macOS: the window's GPU layer, shown (plat::win_native)
  Surface::native(&instance, nw, window) the UI output's layer (plat::win_gpu_child, hidden until
                                         the output shows it)
  device.create_swapchain(&surface, &desc)   the layer gets the device, the format (BGRA8, sRGB
       when asked), framebufferOnly unless copy_src, three drawables, the window's size
  try_acquire() never waits: nextDrawable only while fewer drawables are in use than the layer
       has (in use: this swapchain's taken and not presented, the layer's presented and not yet
       on the screen -- also those of an earlier swapchain on it --, plus the one on the screen;
       the drawables' presented handlers count what reached it); NotReady otherwise
  present(frame, cmds)                   the last command buffer presents the drawable

Present modes: Fifo only.  Without displaySyncEnabled a window on macOS still shows at the rate of
the compositor (measured: 8.3 ms per frame either way at 120 Hz), so Immediate and Mailbox are Fifo
here, as the API allows for a missing mode.  The layer's colour space is sRGB, as the CGImage of the CPU path (plat's
apple_mac.rs): both show the same pixels.  Layer properties change inside a Core Animation
transaction, so a swapchain may live on another thread than the window (the UI's output job).
  • struct Surface(pub(crate) Arc<SurfInner>) a window as a target for swapchains

impl Surface

  • fn track_size(&self) the window's size now, for a swapchain made or rebuilt on a worker (call on the main thread)

  • fn new(instance: &Instance, window: i32) -> Result<Surface> the surface of plat window window: its GPU layer, shown

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

  • struct Frame a frame being drawn

  • enum TryAcquire what Swapchain::try_acquire found

    • Frame(Frame)
    • NotReady -- no drawable free yet (try again with the next look)
    • Stale -- the swapchain does not fit the window any more: Swapchain::rebuild first
    • NoArea -- the window has no area (minimised, hidden or gone)

impl Frame

  • fn index(&self) -> u32

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

  • struct Swapchain images shown in a window through its CAMetalLayer

impl Device

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

impl Swapchain

  • fn rebuild(&mut self) -> Result<()> the drawables take the window's size now
  • fn try_acquire(&mut self) -> Result<TryAcquire> the image to draw into, never waiting
  • fn acquire(&mut self) -> Result<Option<Frame>> the image to draw into (waits for a drawable); None while the window has no area
  • fn view(&self, _frame: &Frame) -> &TextureView the frame's image as a render target
  • fn texture(&self, _frame: &Frame) -> &Texture the frame's image (copies: SwapchainDesc::copy_src)
  • fn present<'a, I: IntoIterator<Item = CommandBuffer<'a>>>(&self, mut frame: Frame, cmds: I) -> Result<SubmitId> submit the frame's commands; the last command buffer presents its drawable
  • 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; Metal does that)
  • fn busy(&self) -> bool a frame is on its way to the screen
  • fn next_look(&self, retry: u64) -> u64 when a frame that found NotReady should try again
  • 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) the next acquire takes the window's size
  • fn set_present_mode(&mut self, _m: PresentMode) (Fifo only: see the top)
  • fn present_mode(&self) -> PresentMode
  • fn format(&self) -> Format
  • fn size(&self) -> (u32, u32)
  • fn image_count(&self) -> usize

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