2022-01-18 06:45:10 +11:00
# Ash
2016-12-10 06:33:25 +11:00
2016-12-10 09:13:58 +11:00
A very lightweight wrapper around Vulkan
2016-12-10 06:38:22 +11:00
2021-07-30 20:32:52 +10:00
[![Crates.io Version ](https://img.shields.io/crates/v/ash.svg )](https://crates.io/crates/ash)
2016-12-10 06:49:49 +11:00
[![Documentation ](https://docs.rs/ash/badge.svg )](https://docs.rs/ash)
2023-11-18 02:55:05 +11:00
[![Build Status ](https://github.com/ash-rs/ash/workflows/CI/badge.svg )](https://github.com/ash-rs/ash/actions?workflow=CI)
2021-07-30 20:32:52 +10:00
[![LICENSE ](https://img.shields.io/badge/license-MIT-blue.svg )](LICENSE-MIT)
2022-07-29 02:48:15 +10:00
[![LICENSE ](https://img.shields.io/badge/license-Apache--2.0-blue.svg )](LICENSE-APACHE)
2016-12-10 06:33:25 +11:00
[![Join the chat at https://gitter.im/MaikKlein/ash ](https://badges.gitter.im/MaikKlein/ash.svg )](https://gitter.im/MaikKlein/ash?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2023-03-10 09:39:44 +11:00
[![MSRV ](https://img.shields.io/badge/rustc-1.60.0+-ab6000.svg )](https://blog.rust-lang.org/2022/04/07/Rust-1.60.0.html)
2016-08-14 09:13:39 +10:00
2018-11-12 04:05:03 +11:00
## Overview
2023-04-17 06:19:09 +10:00
2018-11-12 04:06:48 +11:00
- [x] A true Vulkan API without compromises
2018-11-13 02:25:31 +11:00
- [x] Convenience features without limiting functionality
2018-11-13 00:53:06 +11:00
- [x] Additional type safety
- [x] Device local function pointer loading
2018-11-12 04:06:48 +11:00
- [x] No validation, everything is **unsafe**
- [x] Generated from `vk.xml`
2022-03-24 08:04:12 +11:00
- [x] Support for Vulkan `1.1` , `1.2` , `1.3`
2018-11-12 04:05:03 +11:00
2022-11-23 09:13:09 +11:00
## ⚠️ Semver compatibility warning
2023-04-17 06:19:09 +10:00
2022-11-23 09:13:09 +11:00
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.
2018-11-12 04:05:03 +11:00
## Features
2023-04-17 06:19:09 +10:00
2016-12-28 14:31:48 +11:00
### Explicit returns with `Result`
2023-04-17 06:19:09 +10:00
2020-08-15 06:40:06 +10:00
```rust
2017-04-30 23:46:38 +10:00
// function signature
2016-12-29 18:06:17 +11:00
pub fn create_instance(& self,
2023-10-26 17:30:16 +11:00
create_info: & vk::InstanceCreateInfo< '_>,
allocation_callbacks: Option< & vk::AllocationCallbacks< '_>>)
2017-04-30 23:46:38 +10:00
-> Result< Instance , InstanceError > { .. }
2017-01-05 18:35:37 +11:00
let instance = entry.create_instance(& create_info, None)
2016-12-29 18:06:17 +11:00
.expect("Instance creation error");
2016-12-10 05:25:48 +11:00
```
2018-11-13 00:53:06 +11:00
### `Vec<T>` instead of mutable slices
2018-11-12 04:05:03 +11:00
2020-08-15 06:40:06 +10:00
```rust
2019-01-19 20:00:44 +11:00
pub fn get_swapchain_images(& self,
swapchain: vk::SwapchainKHR)
-> VkResult< Vec < vk::Image > >;
2016-12-29 18:06:17 +11:00
let present_images = swapchain_loader.get_swapchain_images_khr(swapchain).unwrap();
2016-12-10 05:25:48 +11:00
```
2023-04-17 06:19:09 +10:00
_Note_: Functions don't return `Vec<T>` if this would limit the functionality. See `p_next` .
2016-12-28 14:31:48 +11:00
2018-11-13 00:53:06 +11:00
### Slices
2023-04-17 06:19:09 +10:00
2020-08-15 06:40:06 +10:00
```rust
2016-12-24 07:41:16 +11:00
pub fn cmd_pipeline_barrier(& self,
command_buffer: vk::CommandBuffer,
src_stage_mask: vk::PipelineStageFlags,
dst_stage_mask: vk::PipelineStageFlags,
dependency_flags: vk::DependencyFlags,
2023-10-26 17:30:16 +11:00
memory_barriers: & [vk::MemoryBarrier< '_>],
buffer_memory_barriers: & [vk::BufferMemoryBarrier< '_>],
image_memory_barriers: &[vk::ImageMemoryBarrier<'_>]);
2016-12-10 05:25:48 +11:00
```
2016-12-28 14:31:48 +11:00
2018-11-20 15:19:50 +11:00
### 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.
2018-11-12 04:05:03 +11:00
### Builder pattern
2019-03-20 20:37:55 +11:00
2020-08-15 06:40:06 +10:00
```rust
2023-04-17 06:19:09 +10:00
let queue_info = [vk::DeviceQueueCreateInfo::default()
2019-03-20 20:37:55 +11:00
.queue_family_index(queue_family_index)
2023-04-17 06:19:09 +10:00
.queue_priorities(&priorities)];
2019-03-20 20:37:55 +11:00
2023-04-17 06:19:09 +10:00
let device_create_info = vk::DeviceCreateInfo::default()
2019-03-20 20:37:55 +11:00
.queue_create_infos(& queue_info)
.enabled_extension_names(& device_extension_names_raw)
.enabled_features(&features);
let device: Device = instance
.create_device(pdevice, & device_create_info, None)
.unwrap();
```
2023-04-17 06:19:09 +10:00
### Pointer chains
2021-03-03 04:22:04 +11:00
2023-04-17 06:19:09 +10:00
Use `base.push_next(ext)` to insert `ext` at the front of the pointer chain attached to `base` .
2021-03-03 04:22:04 +11:00
2020-08-15 06:40:06 +10:00
```rust
2023-04-17 06:19:09 +10:00
let mut variable_pointers = vk::PhysicalDeviceVariablePointerFeatures::default();
let mut corner = vk::PhysicalDeviceCornerSampledImageFeaturesNV::default();
2019-03-20 20:37:55 +11:00
2023-04-17 06:19:09 +10:00
let mut device_create_info = vk::DeviceCreateInfo::default()
2019-03-10 03:33:42 +11:00
.push_next(& mut corner)
.push_next(& mut variable_pointers);
```
2023-04-17 06:19:09 +10:00
The generic argument of `.push_next()` only allows valid structs to extend a given struct (known as [`structextends` in the Vulkan registry ](https://registry.khronos.org/vulkan/specs/1.3/styleguide.html#extensions-interactions ), mapped to `Extends*` traits).
Only structs that are listed one or more times in any `structextends` will implement a `.push_next()` .
2019-03-10 03:33:42 +11:00
2018-11-12 04:05:03 +11:00
### Flags and constants as associated constants
2017-01-01 18:31:13 +11:00
2020-08-15 06:40:06 +10:00
```rust
2018-11-13 00:53:06 +11:00
// Bitflag
vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE
2017-01-01 18:31:13 +11:00
```
2020-08-15 06:40:06 +10:00
```rust
2018-11-13 00:53:06 +11:00
// Constant
vk::PipelineBindPoint::GRAPHICS,
2017-01-01 18:31:13 +11:00
```
2023-04-17 06:19:09 +10:00
2018-11-12 04:05:03 +11:00
### Debug/Display for Flags
2017-01-01 18:31:13 +11:00
2020-08-15 06:40:06 +10:00
```rust
2018-11-12 04:05:03 +11:00
let flag = vk::AccessFlags::COLOR_ATTACHMENT_READ
| vk::AccessFlags::COLOR_ATTACHMENT_WRITE;
println!("Debug: {:?}", flag);
println!("Display: {}", flag);
// Prints:
// Debug: AccessFlags(110000000)
// Display: COLOR_ATTACHMENT_READ | COLOR_ATTACHMENT_WRITE
2017-01-01 18:31:13 +11:00
```
2018-11-12 04:05:03 +11:00
### Function pointer loading
2023-04-17 06:19:09 +10:00
2018-11-13 20:07:37 +11:00
Ash also takes care of loading the function pointers. Function pointers are split into 3 categories.
2023-04-17 06:19:09 +10:00
- Entry: Loads the Vulkan library. Needs to outlive `Instance` and `Device` .
- Instance: Loads instance level functions. Needs to outlive the `Device` s it has created.
- Device: Loads device **local** functions.
2018-11-13 20:07:37 +11:00
The loader is just one possible implementation:
2023-04-17 06:19:09 +10:00
- 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.
2018-11-13 20:07:37 +11:00
Custom loaders can be implemented.
2018-11-12 04:05:03 +11:00
2016-12-28 14:31:48 +11:00
### Extension loading
2023-04-17 06:19:09 +10:00
2023-11-18 02:55:05 +11:00
Additionally, every Vulkan extension has to be loaded explicitly. You can find all extensions under [`ash::extensions` ](https://github.com/ash-rs/ash/tree/master/ash/src/extensions ).
2023-04-17 06:19:09 +10:00
2020-08-15 06:40:06 +10:00
```rust
2019-01-19 20:00:44 +11:00
use ash::extensions::khr::Swapchain;
2018-11-12 04:05:03 +11:00
let swapchain_loader = Swapchain::new(& instance, &device);
2019-01-19 20:00:44 +11:00
let swapchain = swapchain_loader.create_swapchain(&swapchain_create_info).unwrap();
2016-12-10 05:25:48 +11:00
```
2016-12-28 14:31:48 +11:00
2018-11-13 00:53:06 +11:00
### 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.
2020-08-15 06:40:06 +10:00
```rust
2018-11-13 00:53:06 +11:00
device.fp_v1_0().destroy_device(...);
```
2017-01-01 18:31:13 +11:00
### Support for extension names
2023-04-17 06:19:09 +10:00
2020-08-15 06:40:06 +10:00
```rust
2017-01-01 18:31:13 +11:00
use ash::extensions::{Swapchain, XlibSurface, Surface, DebugReport};
#[cfg(all(unix, not(target_os = "android")))]
fn extension_names() -> Vec< *const i8> {
vec![
2023-03-10 09:53:04 +11:00
Surface::NAME.as_ptr(),
XlibSurface::NAME.as_ptr(),
DebugReport::NAME.as_ptr()
2017-01-01 18:31:13 +11:00
]
}
```
2016-12-28 14:31:48 +11:00
### Implicit handles
2023-04-17 06:19:09 +10:00
2018-11-12 04:05:03 +11:00
Handles from Instance or Device are passed implicitly.
2023-04-17 06:19:09 +10:00
2020-08-15 06:40:06 +10:00
```rust
2016-12-24 07:41:16 +11:00
pub fn create_command_pool(& self,
2023-10-26 17:30:16 +11:00
create_info: & vk::CommandPoolCreateInfo< '_>)
2016-12-24 07:41:16 +11:00
-> VkResult< vk::CommandPool > ;
2016-12-10 05:25:48 +11:00
2016-12-24 07:41:16 +11:00
let pool = device.create_command_pool(&pool_create_info).unwrap();
2016-12-10 05:25:48 +11:00
```
2016-12-28 14:22:13 +11:00
2021-09-10 06:50:34 +10:00
### Optional linking
2022-01-18 06:45:10 +11:00
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.
2022-01-06 07:22:30 +11:00
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` .
2021-09-10 06:50:34 +10:00
2016-12-10 05:39:16 +11:00
## Example
2023-04-17 06:19:09 +10:00
2023-11-18 03:27:28 +11:00
You can find the examples [here ](https://github.com/ash-rs/ash/tree/master/ash-examples ).
2017-01-01 18:37:12 +11:00
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 ](https://lunarg.com/vulkan-sdk/ )
2023-04-17 06:19:09 +10:00
2017-01-01 18:43:36 +11:00
#### Windows
2023-04-17 06:19:09 +10:00
2017-01-01 18:43:36 +11:00
Make sure that you have a Vulkan ready driver and install the [LunarG Vulkan SDK ](https://lunarg.com/vulkan-sdk/ ).
2023-04-17 06:19:09 +10:00
2017-01-01 18:43:36 +11:00
#### Linux
2023-04-17 06:19:09 +10:00
2017-01-01 18:43:36 +11:00
Make sure that you have a Vulkan ready driver and install the [LunarG Vulkan SDK ](https://lunarg.com/vulkan-sdk/ ). You also have to add the library and layers to your path. Have a look at my [post ](http://askubuntu.com/a/803110/77183 ) if you are unsure how to do that.
2023-04-17 06:19:09 +10:00
2018-07-07 14:55:59 +10:00
#### macOS
2023-04-17 06:19:09 +10:00
2022-04-08 04:47:37 +10:00
Install the [LunarG Vulkan SDK ](https://lunarg.com/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:
2023-04-17 06:19:09 +10:00
2022-04-08 04:47:37 +10:00
```sh
VULKAN_SDK=$HOME/VulkanSDK/< version > /macOS \
DYLD_FALLBACK_LIBRARY_PATH=$VULKAN_SDK/lib \
VK_ICD_FILENAMES=$VULKAN_SDK/share/vulkan/icd.d/MoltenVK_icd.json \
VK_LAYER_PATH=$VULKAN_SDK/share/vulkan/explicit_layer.d \
cargo run ...
```
2016-12-10 05:39:16 +11:00
2023-11-18 03:27:28 +11:00
### [Triangle](https://github.com/ash-rs/ash/blob/master/ash-examples/src/bin/triangle.rs)
2023-04-17 06:19:09 +10:00
2019-11-17 10:45:05 +11:00
Displays a triangle with vertex colors.
2023-04-17 06:19:09 +10:00
2023-11-18 03:27:28 +11:00
```sh
cargo run -p ash-examples --bin triangle
2019-11-17 10:45:05 +11:00
```
2016-12-10 05:47:29 +11:00
![screenshot ](http://i.imgur.com/PQZcL6w.jpg )
2016-12-10 05:39:16 +11:00
2023-11-18 03:27:28 +11:00
### [Texture](https://github.com/ash-rs/ash/blob/master/ash-examples/src/bin/texture.rs)
2023-04-17 06:19:09 +10:00
2016-12-26 11:23:19 +11:00
Displays a texture on a quad.
2023-04-17 06:19:09 +10:00
2023-11-18 03:27:28 +11:00
```sh
cargo run -p ash-examples --bin texture
2016-12-12 14:43:46 +11:00
```
2023-04-17 06:19:09 +10:00
2016-12-12 14:30:01 +11:00
![texture ](http://i.imgur.com/trow00H.png )
2016-12-12 14:19:21 +11:00
2019-02-15 20:08:38 +11:00
## Useful resources
### Examples
2023-04-17 06:19:09 +10:00
- [vulkan-tutorial-rust ](https://github.com/Usami-Renko/vulkan-tutorial-rust ) - A port of [vulkan-tutorial.com ](https://vulkan-tutorial.com ).
- [ash-sample-progression ](https://github.com/bzm3r/ash-sample-progression ) - A port of the LunarG examples.
- [ash-nv-rt ](https://github.com/gwihlidal/ash-nv-rt ) A raytracing example for ash.
2019-02-15 20:08:38 +11:00
### Utility libraries
2023-04-17 06:19:09 +10:00
- [vk-sync ](https://github.com/gwihlidal/vk-sync-rs ) - Simplified Vulkan synchronization logic, written in rust.
- [vk-mem-rs ](https://github.com/gwihlidal/vk-mem-rs ) - This crate provides an FFI layer and idiomatic rust wrappers for the excellent AMD Vulkan Memory Allocator (VMA) C/C++ library.
2023-11-18 02:55:05 +11:00
- [gpu-allocator ](https://github.com/Traverse-Research/gpu-allocator ) - GPU Memory allocator written in pure Rust for Vulkan and DirectX 12.
2023-04-17 06:19:09 +10:00
- [lahar ](https://github.com/Ralith/lahar ) - Tools for asynchronously uploading data to a Vulkan device.
2019-02-15 20:08:38 +11:00
### Libraries that use ash
2023-04-17 06:19:09 +10:00
- [gfx-rs ](https://github.com/gfx-rs/gfx ) - gfx-rs is a low-level, cross-platform graphics abstraction library in Rust.
2019-02-15 20:08:38 +11:00
2016-12-10 07:04:31 +11:00
## A thanks to
2023-04-17 06:19:09 +10:00
- [Api with no secrets ](https://software.intel.com/en-us/articles/api-without-secrets-introduction-to-vulkan-part-1 )
- [Vulkan tutorial ](http://jhenriques.net/development.html )
- [Vulkan examples ](https://github.com/SaschaWillems/Vulkan )
- [Vulkan tutorial ](https://vulkan-tutorial.com/ )
- [Vulkano ](https://github.com/vulkano-rs/vulkano )
- [vk-rs ](https://github.com/Osspial/vk-rs )