Shimizu - The Wayland Protocol, in Zig

wl_display is a specially defined object at id 1. You can acquire a proxy to it using shimizu.Connection.getDisplayProxy. However, this will not be the *wl_display Vulkan is expecting. Any references to Wayland objects in Vulkan will be using libwayland, and will require something that links libwayland, like zig-wayland to use.

It is possible to use shimizu and Vulkan together. I’ve done it for seizer, and you can see the crappy abstraction I made over it here. Note that this code has some cruft, and isn’t necessarily the most succinct. Here’s a high level overview of what needs to be done:

  1. Connect to the Wayland compositor.
  2. Acquire a Vulkan instance. This doesn’t require any knowledge of the Wayland compositor, you are just getting access to the GPU initially.
  3. Check if your Wayland compositor exposes a zwp_linux_dmabuf_v1 object from the linux-dmabuf-v1 protocol. We can make do without this extension, see the addendum for details.
  4. Check if your Vulkan device supports VK_EXT_external_memory_dma_buf. Same as above, we can make do without, see addendum for details.
  5. Create some images to render into, making sure they have dma_buf_bit_ext set. Normally you are instructed to use the Swapchain extension for this, however, we cannot, as it requires libwayland.
  6. Create wl_buffer objects using linux-dmabuf-v1 by exporting dma_buf handles from Vulkan.
  7. Tell Vulkan to render into one of images.
  8. Attach the associated wl_buffer to a wl_surface and commit it.
  9. Mark the Vulkan image/wl_buffer as in use until you receive a release event
  10. Grab another image/wl_buffer and repeat

And if that giant list didn’t scare you off, congratulations! :confetti_ball: You can now render to Wayland without linking libwayland! Unfortunately you will still need to link the appropriate libc.

Addendum: Rendering Without linux-dmabuf-v1

The linux-dmabuf-v1 protocol lets us tell the Wayland compositor about framebuffers/images that are located on the GPU. However, if that is unsupported, we can fall back to copying images from the GPU into main memory, and then passing that to the Wayland compositor. Here’s what to change:

5 Likes