Pages

Friday, September 28, 2018

Cross Platform Vulkan

With the release of MacOS 10.14 Mojave, Apple has deprecated not only its long suffering OpenGL implementation, but also OpenCL. As they were the original creator of OpenCL, I think it's safe to say it's as good as dead. That leaves Apple with no viable Graphics or Compute platform. Fortunately, The Brenwill Workshop open sourced their MoltenVK library, which acts as a very lean Vulkan layer that sits on top of Metal. After getting a very rudimentary single window with a shader example going on the 3 platforms I care about, being Windows, Linux and MacOS, I can say that not only does MoltenVK work, it is arguably the best library on the entire MacOS ecosystem. There are a few small gotchas, and I wish to detail them here.

Firstly, there are only three supported surface formats. This is because MoltenVK uses a CAMetalLayer for its rendering, and this only supports five formats, and two of them are just downright odd, so justifiably ignored. These translate into the following Vulkan surface formats...

VK_FORMAT_B8G8R8A8_UNORM
VK_FORMAT_B8G8R8A8_SRGB
VK_FORMAT_R16G16B16A16_SFLOAT


...which gives us plenty to work with. This should not be considered odd at all, the current Nvidia drivers on Linux, 396.54, as well as the current Nvidia Windows drivers, 411.70, only report the first two of those formats on the 750M I tested (the same MacBookPro I tested the Mac version on). It should be noted, that on Windows and Linux, Vulkan will initialise with other formats, where as on Mac it will crash with a...

*** Terminating app due to uncaught exception 'CAMetalLayerInvalid', reason: 'invalid pixel format 70'


The only other quirk I encountered was the order of operations. As the shader takes a single float in a uniform buffer, the exact location of the vkCmdCopyBuffer() call to update the uniform buffer caused the drawing to stop right after the clear command was issued, so I was left with a ClearColor coloured window, even though everything was reporting success. That was without the full Command Queue debugging options enabled, so I suspect that will complain if you have it enabled.

If you want to play with shaders on Vulkan, on either of the three platforms, the repository is here.