JFLib

gpu/web_swap.rs#

Source: lib/gpu/web_swap.rs

web_swap -- a canvas as a swapchain in the browser (through web/gpu.js)

  Surface::native(&instance, nw, window)   the OffscreenCanvas plat::win_gpu_child brought from the
       page (lib/plat/web_gpu.rs); Surface::new asks for it, NotReady until it is here
  device.create_swapchain(&surface, &SwapchainDesc)   getContext("webgpu"), configure: the format the
       browser prefers (bgra8unorm or rgba8unorm), opaque, sRGB; copy_src adds COPY_SRC; srgb a view in
       the -srgb format.  The present mode and the image count are not ours: the browser composes the
       newest image at its next frame, present_mode() says Mailbox
  sc.try_acquire()   getCurrentTexture.  NotReady never: the image is there at once, and a second
       frame in one turn of the worker's event loop draws over the first, which nobody saw
  sc.present(frame, [cmd, ..])   queue.submit.  There is no present: the browser takes the image when
       the worker returns to its event loop (OffscreenCanvas of a worker: no main thread involved)

A swapchain cannot be read back into this turn: Buffer::read later (web_res.rs).
  • struct Surface(pub(crate) Rc<SurfInner>) a canvas as a target for swapchains

impl Surface

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

  • fn new(instance: &Instance, window: i32) -> Result<Surface> the canvas of plat window window (NotReady: it is on its way from the page)

  • fn native(_instance: &Instance, nw: plat::NativeWindow, window: i32) -> Result<Surface> the canvas of plat::win_gpu_child, whose size is that of plat window window

  • struct Frame a frame being drawn

  • enum TryAcquire what Swapchain::try_acquire found

    • Frame(Frame)
    • NotReady -- (never in the browser)
    • Stale -- the swapchain does not fit the window any more: Swapchain::rebuild first
    • NoArea -- the window has no area

impl Frame

  • fn index(&self) -> u32 (the browser has one current image)

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

  • struct Swapchain images shown on a canvas

impl Device

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

impl Swapchain

  • fn rebuild(&mut self) -> Result<()> the canvas takes the window's size now
  • fn try_acquire(&mut self) -> Result<TryAcquire> the image to draw into (the same one until the worker returns to its event loop)
  • fn acquire(&mut self) -> Result<Option<Frame>> the image to draw into; 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, _frame: Frame, cmds: I) -> Result<SubmitId> submit the frame's commands; the browser shows the image when the worker's turn ends
  • 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 the browser does 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) the next acquire takes the window's size
  • fn set_present_mode(&mut self, _m: PresentMode) (the browser's: no present modes)
  • fn present_mode(&self) -> PresentMode
  • fn format(&self) -> Format
  • fn size(&self) -> (u32, u32) the images' size
  • fn image_count(&self) -> usize

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