Ash
A very lightweight wrapper around Vulkan
Overview
- A true Vulkan API without compromises
- Convenience features without limiting functionality
- Additional type safety
- Device local function pointer loading
- No validation, everything is unsafe
- Lifetime-safety on structs created with the builder pattern
- Generated from
vk.xml
- Support for Vulkan
1.1
,1.2
,1.3
-
no_std
support
⚠️ Semver compatibility warning
The Vulkan Video bindings are experimental and still seeing breaking changes in their upstream specification, and are only provided by Ash for early adopters. All related functions and types are semver-exempt [^1] (we allow breaking API changes while releasing Ash with non-breaking semver bumps).
[^1]: generator
complexity makes it so that we cannot easily hide these bindings behind a non-default
feature flag, and they are widespread across the generated codebase.
Features
Explicit returns with Result
// function signature
let instance = entry.create_instance
.expect;
Vec<T>
instead of mutable slices
;
let present_images = swapchain_loader.get_swapchain_images_khr.unwrap;
Note: Functions don't return Vec<T>
if this would limit the functionality. See p_next
.
Slices
;
Strongly typed handles
Each Vulkan handle type is exposed as a newtyped struct for improved type safety. Null handles can be constructed with
T::null()
, and handles may be freely converted to and from u64
with Handle::from_raw
and Handle::as_raw
for
interop with non-Ash Vulkan code.
Builder pattern
let queue_info = ;
let device_create_info = default
.queue_create_infos
.enabled_extension_names
.enabled_features;
let device: Device = instance
.create_device
.unwrap;
Pointer chains
Use base.push_next(ext)
to insert ext
at the front of the pointer chain attached to base
.
let mut variable_pointers = default;
let mut corner = default;
let mut device_create_info = default
.push_next
.push_next;
The generic argument of .push_next()
only allows valid structs to extend a given struct (known as structextends
in the Vulkan registry, mapped to Extends*
traits).
Only structs that are listed one or more times in any structextends
will implement a .push_next()
.
Flags and constants as associated constants
// Bitflag
COLOR_ATTACHMENT_READ | COLOR_ATTACHMENT_WRITE
// Constant
GRAPHICS,
Debug/Display for Flags
let flag = COLOR_ATTACHMENT_READ
| COLOR_ATTACHMENT_WRITE;
println!;
println!;
// Prints:
// Debug: AccessFlags(110000000)
// Display: COLOR_ATTACHMENT_READ | COLOR_ATTACHMENT_WRITE
Function pointer loading
Ash also takes care of loading the function pointers. Function pointers are split into 3 categories.
- Entry: Loads the Vulkan library. Needs to outlive
Instance
andDevice
. - Instance: Loads instance level functions. Needs to outlive the
Device
s it has created. - Device: Loads device local functions.
The loader is just one possible implementation:
- Device level functions are retrieved on a per device basis.
- Everything is loaded by default, functions that failed to load are initialized to a function that always panics.
- Do not call Vulkan 1.1 functions if you have created a 1.0 instance. Doing so will result in a panic.
Custom loaders can be implemented.
Extension loading
Additionally, every Vulkan extension has to be loaded explicitly. You can find all extensions directly under ash::*
in a module with their prefix (e.g. khr
or ext
).
use khr;
let swapchain_loader = new;
let swapchain = swapchain_loader.create_swapchain.unwrap;
Raw function pointers
Raw function pointers are available, if something hasn't been exposed yet in the higher level API. Please open an issue if anything is missing.
device.fp_v1_0.destroy_device;
Support for extension names
use ;
Implicit handles
Handles from Instance or Device are passed implicitly.
;
let pool = device.create_command_pool.unwrap;
Optional linking
The default loaded
cargo feature will dynamically load the default Vulkan library for the current platform with Entry::load
, meaning that the build environment does not have to have Vulkan development packages installed.
If, on the other hand, your application cannot handle Vulkan being missing at runtime, you can instead enable the linked
feature, which will link your binary with the Vulkan loader directly and expose the infallible Entry::linked
.
Use in no_std
environments
Ash can be used in no_std
environments (with alloc
) by disabling the std
feature.
Example
You can find the examples here.
All examples currently require: the LunarG Validation layers and a Vulkan library that is visible in your PATH
. An easy way to get started is to use the LunarG Vulkan SDK
Windows
Make sure that you have a Vulkan ready driver and install the LunarG Vulkan SDK.
Linux
Install a Vulkan driver for your graphics hardware of choice, and (optionally) the Validation Layers via your package manager:
- Arch Linux: https://wiki.archlinux.org/title/Vulkan.
- Gentoo: https://wiki.gentoo.org/wiki/Vulkan.
- Ubuntu/Debian: Besides installing a compatible graphics driver, install
vulkan-validationlayers
(Debian) for the Validation Layers. - Other distros: consult your distro documentation and/or package repository for the preferred method to install and use Vulkan.
macOS
Install the LunarG Vulkan SDK. The installer puts the SDK in $HOME/VulkanSDK/<version>
by default. You will need to set the following environment variables when running cargo:
VULKAN_SDK= /VulkanSDK/<version>/macOS \
DYLD_FALLBACK_LIBRARY_PATH= /lib\
VK_ICD_FILENAMES= /share/vulkan/icd.d/MoltenVK_icd.json\
VK_LAYER_PATH= /share/vulkan/explicit_layer.d\
Triangle
Displays a triangle with vertex colors.
Texture
Displays a texture on a quad.
Useful resources
Examples
- vulkan-tutorial-rust - A port of vulkan-tutorial.com.
- ash-sample-progression - A port of the LunarG examples.
- ash-nv-rt A raytracing example for ash.
Utility libraries
- vk-sync - Simplified Vulkan synchronization logic, written in rust.
- vk-mem-rs - This crate provides an FFI layer and idiomatic rust wrappers for the excellent AMD Vulkan Memory Allocator (VMA) C/C++ library.
- gpu-allocator - GPU Memory allocator written in pure Rust for Vulkan and DirectX 12.
- lahar - Tools for asynchronously uploading data to a Vulkan device.
Libraries that use ash
- gfx-rs - gfx-rs is a low-level, cross-platform graphics abstraction library in Rust.