# vulkan Vulkan for Sakura, as its registry, `vk.xml`, declares it: the functions of Vulkan's core, 1.0 to 1.4, and of the extensions bound, the structs and unions they take, laid out as C lays them out, and the constants they're asked for and give. It's Sakura's, built with [blossom](https://git.malunal.studio/sakura/blossom), and every file of its library is written by a tool of its own, `malunal.vulkan.bindings`, from the registry the Vulkan SDK installs, so it's written again as the SDK is updated, rather than edited. ```sakura import malunal::vulkan; val app := application_info{ application_name = "game".data, api_version = VK_API_VERSION_1_3 }; val info := instance_create_info{ application_info = address(&app) }; # its `s_type` is its own var instance: u64 = VK_NULL_HANDLE; if vkCreateInstance(&info, null, &mut instance) != VK_SUCCESS { ... } var count: u32 = 0; _ = vkEnumeratePhysicalDevices(instance, &mut count, null); var devices: u64[8]; _ = vkEnumeratePhysicalDevices(instance, &mut count, (&mut devices) as *mut u64); var properties := physical_device_properties{}; vkGetPhysicalDeviceProperties(devices[0], &mut properties); # properties.limits.timestamp_period, ... # An extension's function the loader doesn't export, found by name and called through its type. val name := vkGetInstanceProcAddr(instance, "vkSetDebugUtilsObjectNameEXT".data) as u64 as PFN_vkSetDebugUtilsObjectNameEXT; ``` A project reads it by requiring this folder, or its remote, and naming `malunal.vulkan`, whose module is `malunal::vulkan`, among a target's `deps`: ```yaml requires: - ../vulkan targets: - name: game type: program deps: [malunal.vulkan] ``` A program built with it links Vulkan's loader, `vulkan-1.dll` on Windows and `libvulkan.so.1` on Linux, which a GPU's drivers install, and the Vulkan SDK too. ## Names - **What C names keeps its name**: each function, as `vkCreateInstance`; each constant, as `VK_FORMAT_R8G8B8A8_UNORM`, an enum's values among them; and each function's type, as `PFN_vkCreateInstance`. - **A struct or a union** with a `VK_STRUCTURE_TYPE_` of its own is named by it, in lower case, as Vulkan's own names are made: `VkPhysicalDeviceVulkan13Features` is `physical_device_vulkan_1_3_features`, by `VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES`. Another is its C name in snake case, without its `Vk`: `VkExtent2D` is `extent_2d`, `VkSurfaceFormatKHR` is `surface_format_khr`. Each says its C name in its documentation. - **A member or a parameter** is its C name in snake case, without the `p` or `pp` of a pointer or the `pfn` of a function: `ppEnabledLayerNames` is `enabled_layer_names`, `pNext` is `next`, `pfnUserCallback` is `user_callback`. A capital begins a word, as does a run of capitals before one, `deviceUUID` as `device_uuid`; a number stays with the word before it, `shaderFloat64` as `shader_float64`, but for a dimension, `maxImageDimension2D` as `max_image_dimension_2d`. One that would be a word of Sakura's is followed by `_`, as `VkPipelineShaderStageCreateInfo`'s `module` is `module_`. ## Types - **Handles** are `u64`s, dispatchable or not, and `VK_NULL_HANDLE` is 0. - **Enums** are their values' numbers, `u32`, or `i32` for `VkResult`, whose errors are below 0; **flags** are `u32`, or `u64` for those of 64 bits, as `VkPipelineStageFlags2`; and `VkBool32` is a `u32`, `VK_TRUE` or `VK_FALSE`. A member of one of these says which C type it is, as `#! VkFormat`. - **Structs** are laid out as C lays them out: a struct's `s_type` is its own by default, so a literal names only what it means, and every other member is zero unless given. An array of arrays is written as Sakura writes them, innermost first, so `VkTransformMatrixKHR`'s `float matrix[3][4]` is `f32[4][3]`, indexed `matrix[row][column]` as C indexes it. A run of C's bitfields is one member of their type, named for them all, as `instance_custom_index_and_mask`, the first the lowest bits. - **A function's parameter** that points to one thing, which must be there, is a reference: `&T` where the function reads it, as `create_info: &instance_create_info`, and `&mut T` where it writes it, as `instance: &mut u64`. One that may be null, that points to an array, or that points to bytes is a pointer: `*T` or `*mut T`, as `allocator: *allocation_callbacks`, given `null`. A C array is a reference to one, as `blend_constants: &f32[4]`. - **A function C calls back** is a `cfn` type of its C name, as `PFN_vkDebugUtilsMessengerCallbackEXT`, its parameters pointers all, which a function of the program's is given as. ## What's bound - **Vulkan's core**, every version the registry has, 1.0 to 1.4 of the SDK's 1.4.328, each with its `VK_API_VERSION_` constant, and `VK_MAKE_API_VERSION` and the functions taking a version apart. - **Surfaces and swapchains**, whose functions the loader exports: `VK_KHR_surface`, `VK_KHR_swapchain`, `VK_KHR_display`, `VK_KHR_display_swapchain`, `VK_KHR_get_surface_capabilities2`, `VK_KHR_get_display_properties2`, and `VK_EXT_headless_surface`; and each system's windows': `VK_KHR_win32_surface` on Windows, and `VK_KHR_xlib_surface`, `VK_KHR_xcb_surface`, and `VK_KHR_wayland_surface` on Linux. - **`VK_EXT_debug_utils`**, which names objects and hears the validation layers: its functions are found with `vkGetInstanceProcAddr`. The functions the loader exports, the core's and those of surfaces and swapchains, are C's, found in it as the program starts, so they're called by their names; only those a program calls are linked, so binding a function of 1.4 asks nothing of an older loader unless a program calls it. Every command, exported or not, has its type, as `PFN_vkCmdDraw`, to call what `vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` give: a device's own functions, which skip the loader's dispatch, and an extension's the loader doesn't export, which are called only so. Each extension's name and version are constants, as `VK_KHR_SWAPCHAIN_EXTENSION_NAME`, a `strview`. ## Layout `vulkan/` is the module `malunal::vulkan`, written by `tools/`; `tools/` is the program `malunal.vulkan.bindings`. | Path | Holds | |---|---| | `vulkan/vulkan.sakura` | What the module is, the registry's version, each version of the core bound, and how a version is written | | `vulkan/constants.sakura` | The API's constants, those the extensions define, and each enum's values, under the enum's C name | | `vulkan/types.sakura` | The structs and unions, and the functions C calls back | | `vulkan/commands.sakura` | The functions the loader exports, and every command's type | | `vulkan/win32.sakura` | Windows' surfaces, a section of the module for Windows alone | | `vulkan/xlib.sakura`, `xcb.sakura`, `wayland.sakura` | Linux's surfaces, each a section for Linux alone | | `tools/bindings.sakura` | The tool: the extensions the library binds, which of them the loader exports, and its options | | `tools/registry.sakura` | `registry`: `vk.xml` read into records of its types, members, enums, commands, features, and extensions, Vulkan SC's left out | | `tools/picking.sakura` | `picking`: what a version of the core and the extensions need of the registry, the types their structs and functions name among it, and whether each is every system's or one platform's | | `tools/writing.sakura` | The Sakura written of what's picked: names, each declaration's type, constants' values, and the files | ## Writing it again From the package's root, with the Vulkan SDK installed and `VULKAN_SDK` naming it: ```sh blossom run malunal.vulkan.bindings ``` It reads `$VULKAN_SDK/share/vulkan/registry/vk.xml` and writes every file of `vulkan/`. `--from FILE` reads another registry, `--into FOLDER` writes elsewhere, `--version 1.3` binds the core only to that version, and `--extension NAME`, given once for each, binds an extension besides the library's own, with what it needs only where what it depends on is bound too. It refuses an extension the registry hasn't, one that's disabled or Vulkan SC's alone, one of a platform but Windows' and Linux's, and one whose dependencies aren't bound, saying why. The extensions the library binds, and which of them the loader exports, are lists at the top of `tools/bindings.sakura`. A platform's file it no longer writes is left as it was, to be removed. ## Testing `blossom test` runs two programs of `tests/`: - `malunal.vulkan.tests` checks the bindings against C: sizes and members' places of structs and unions, as C lays them out on 64-bit Windows and Linux, from `VkExtent2D` to `VkPhysicalDeviceProperties`, its limits, and its features, each `VkBool32` where the specification puts it; structs' own `s_type`s; and constants, versions, flags of 64 bits, `VkResult`'s errors, and extensions' names. Then it calls the loader: the version it has, an instance made, its GPUs found, the first's properties and its Vulkan 1.3 features read through a chain, and a function found by name and called through its type. It needs Vulkan's loader. - `malunal.vulkan.bindings.tests` runs the tool's code on a small registry of its own, written as `vk.xml` is: the names it gives, from `sType`s, in snake case, without pointers' prefixes, and of Sakura's words; members' types, of pointers, constant pointers to pointers, arrays named by constants, arrays of arrays, flags of 64 bits, bitfields, and callbacks; constants' values, of `(~0U)`, floats, bits, and offsets from an extension's number, negative where they're errors; the functions the loader exports and the types of the rest; a platform's file for its system alone; an older version bound without what's newer, nor what an extension needs only with it; Vulkan SC's parts left out; and each extension refused, with why. ## Not yet Vulkan Video's extensions, whose structs name the codecs' own headers, `vk_video/*.h`, which `video.xml` declares; platforms but Windows' and Linux's, as Android's, Apple's Metal, and Fuchsia's; handles and enums as types of their own, rather than numbers; and the Linux files built, which are written as Windows' are, but haven't been compiled for Linux.