Debian Codegraph @

third_party/rust/ash/README.md

raw
1 # Ash
2
3 A very lightweight wrapper around Vulkan
4
5 [![Crates.io Version](https://img.shields.io/crates/v/ash.svg)](https://crates.io/crates/ash)
6 [![Documentation](https://docs.rs/ash/badge.svg)](https://docs.rs/ash)
7 [![Build Status](https://github.com/ash-rs/ash/workflows/CI/badge.svg)](https://github.com/ash-rs/ash/actions?workflow=CI)
8 [![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE-MIT)
9 [![LICENSE](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE-APACHE)
10 [![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)
11 [![MSRV](https://img.shields.io/badge/rustc-1.69.0+-ab6000.svg)](https://blog.rust-lang.org/2023/04/20/Rust-1.69.0.html)
12
13 ## Overview
14
15 - [x] A true Vulkan API without compromises
16 - [x] Convenience features without limiting functionality
17 - [x] Additional type safety
18 - [x] Device local function pointer loading
19 - [x] No validation, everything is **unsafe**
20 - [x] Lifetime-safety on structs created with the builder pattern
21 - [x] Generated from `vk.xml`
22 - [x] Support for Vulkan `1.1`, `1.2`, `1.3`
23 - [x] `no_std` support
24
25 ## ⚠️ Semver compatibility warning
26
27 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).
28
29 [^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.
30
31 ## Features
32
33 ### Explicit returns with `Result`
34
35 ```rust
36 // function signature
37 pub fn create_instance(&self,
38 create_info: &vk::InstanceCreateInfo<'_>,
39 allocation_callbacks: Option<&vk::AllocationCallbacks<'_>>)
40 -> Result<Instance, InstanceError> { .. }
41 let instance = entry.create_instance(&create_info, None)
42 .expect("Instance creation error");
43 ```
44
45 ### `Vec<T>` instead of mutable slices
46
47 ```rust
48 pub fn get_swapchain_images(&self,
49 swapchain: vk::SwapchainKHR)
50 -> VkResult<Vec<vk::Image>>;
51 let present_images = swapchain_loader.get_swapchain_images_khr(swapchain).unwrap();
52 ```
53
54 _Note_: Functions don't return `Vec<T>` if this would limit the functionality. See `p_next`.
55
56 ### Slices
57
58 ```rust
59 pub fn cmd_pipeline_barrier(&self,
60 command_buffer: vk::CommandBuffer,
61 src_stage_mask: vk::PipelineStageFlags,
62 dst_stage_mask: vk::PipelineStageFlags,
63 dependency_flags: vk::DependencyFlags,
64 memory_barriers: &[vk::MemoryBarrier<'_>],
65 buffer_memory_barriers: &[vk::BufferMemoryBarrier<'_>],
66 image_memory_barriers: &[vk::ImageMemoryBarrier<'_>]);
67 ```
68
69 ### Strongly typed handles
70
71 Each Vulkan handle type is exposed as a newtyped struct for improved type safety. Null handles can be constructed with
72 `T::null()`, and handles may be freely converted to and from `u64` with `Handle::from_raw` and `Handle::as_raw` for
73 interop with non-Ash Vulkan code.
74
75 ### Builder pattern
76
77 ```rust
78 let queue_info = [vk::DeviceQueueCreateInfo::default()
79 .queue_family_index(queue_family_index)
80 .queue_priorities(&priorities)];
81
82 let device_create_info = vk::DeviceCreateInfo::default()
83 .queue_create_infos(&queue_info)
84 .enabled_extension_names(&device_extension_names_raw)
85 .enabled_features(&features);
86
87 let device: Device = instance
88 .create_device(pdevice, &device_create_info, None)
89 .unwrap();
90 ```
91
92 ### Pointer chains
93
94 Use `base.push_next(ext)` to insert `ext` at the front of the pointer chain attached to `base`.
95
96 ```rust
97 let mut variable_pointers = vk::PhysicalDeviceVariablePointerFeatures::default();
98 let mut corner = vk::PhysicalDeviceCornerSampledImageFeaturesNV::default();
99
100 let mut device_create_info = vk::DeviceCreateInfo::default()
101 .push_next(&mut corner)
102 .push_next(&mut variable_pointers);
103 ```
104
105 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).
106 Only structs that are listed one or more times in any `structextends` will implement a `.push_next()`.
107
108 ### Flags and constants as associated constants
109
110 ```rust
111 // Bitflag
112 vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE
113 ```
114
115 ```rust
116 // Constant
117 vk::PipelineBindPoint::GRAPHICS,
118 ```
119
120 ### Debug/Display for Flags
121
122 ```rust
123 let flag = vk::AccessFlags::COLOR_ATTACHMENT_READ
124 | vk::AccessFlags::COLOR_ATTACHMENT_WRITE;
125 println!("Debug: {:?}", flag);
126 println!("Display: {}", flag);
127 // Prints:
128 // Debug: AccessFlags(110000000)
129 // Display: COLOR_ATTACHMENT_READ | COLOR_ATTACHMENT_WRITE
130 ```
131
132 ### Function pointer loading
133
134 Ash also takes care of loading the function pointers. Function pointers are split into 3 categories.
135
136 - Entry: Loads the Vulkan library. Needs to outlive `Instance` and `Device`.
137 - Instance: Loads instance level functions. Needs to outlive the `Device`s it has created.
138 - Device: Loads device **local** functions.
139
140 The loader is just one possible implementation:
141
142 - Device level functions are retrieved on a per device basis.
143 - Everything is loaded by default, functions that failed to load are initialized to a function that always panics.
144 - Do not call Vulkan 1.1 functions if you have created a 1.0 instance. Doing so will result in a panic.
145
146 Custom loaders can be implemented.
147
148 ### Extension loading
149
150 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`).
151
152 ```rust
153 use ash::khr;
154 let swapchain_loader = khr::swapchain::Device::new(&instance, &device);
155 let swapchain = swapchain_loader.create_swapchain(&swapchain_create_info).unwrap();
156 ```
157
158 ### Raw function pointers
159
160 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.
161
162 ```rust
163 device.fp_v1_0().destroy_device(...);
164 ```
165
166 ### Support for extension names
167
168 ```rust
169 use ash::{ext, khr};
170 #[cfg(all(unix, not(target_os = "android")))]
171 fn extension_names() -> Vec<*const i8> {
172 vec![
173 khr::surface::NAME.as_ptr(),
174 khr::xlib_surface::NAME.as_ptr(),
175 ext::debug_utils::NAME.as_ptr(),
176 ]
177 }
178 ```
179
180 ### Implicit handles
181
182 Handles from Instance or Device are passed implicitly.
183
184 ```rust
185 pub fn create_command_pool(&self,
186 create_info: &vk::CommandPoolCreateInfo<'_>)
187 -> VkResult<vk::CommandPool>;
188
189 let pool = device.create_command_pool(&pool_create_info).unwrap();
190 ```
191
192 ### Optional linking
193
194 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.
195
196 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`.
197
198 ### Use in `no_std` environments
199
200 Ash can be used in `no_std` environments (with `alloc`) by disabling the `std` feature.
201
202 ## Example
203
204 You can find the examples [here](https://github.com/ash-rs/ash/tree/master/ash-examples).
205 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/)
206
207 #### Windows
208
209 Make sure that you have a Vulkan ready driver and install the [LunarG Vulkan SDK](https://lunarg.com/vulkan-sdk/).
210
211 #### Linux
212
213 Install a Vulkan driver for your graphics hardware of choice, and (optionally) the [Validation Layers](https://github.com/KhronosGroup/Vulkan-ValidationLayers) via your package manager:
214
215 - Arch Linux: https://wiki.archlinux.org/title/Vulkan.
216 - Gentoo: https://wiki.gentoo.org/wiki/Vulkan.
217 - Ubuntu/Debian: Besides installing a compatible graphics driver, install [`vulkan-validationlayers`](https://packages.ubuntu.com/vulkan-validationlayers) ([Debian](https://packages.debian.org/search?keywords=vulkan-validationlayers)) for the Validation Layers.
218 - Other distros: consult your distro documentation and/or package repository for the preferred method to install and use Vulkan.
219
220 #### macOS
221
222 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:
223
224 ```sh
225 VULKAN_SDK=$HOME/VulkanSDK/<version>/macOS \
226 DYLD_FALLBACK_LIBRARY_PATH=$VULKAN_SDK/lib \
227 VK_ICD_FILENAMES=$VULKAN_SDK/share/vulkan/icd.d/MoltenVK_icd.json \
228 VK_LAYER_PATH=$VULKAN_SDK/share/vulkan/explicit_layer.d \
229 cargo run ...
230 ```
231
232 ### [Triangle](https://github.com/ash-rs/ash/blob/master/ash-examples/src/bin/triangle.rs)
233
234 Displays a triangle with vertex colors.
235
236 ```sh
237 cargo run -p ash-examples --bin triangle
238 ```
239
240 ![screenshot](https://i.imgur.com/PQZcL6w.jpg)
241
242 ### [Texture](https://github.com/ash-rs/ash/blob/master/ash-examples/src/bin/texture.rs)
243
244 Displays a texture on a quad.
245
246 ```sh
247 cargo run -p ash-examples --bin texture
248 ```
249
250 ![texture](https://i.imgur.com/trow00H.png)
251
252 ## Useful resources
253
254 ### Examples
255
256 - [vulkan-tutorial-rust](https://github.com/Usami-Renko/vulkan-tutorial-rust) - A port of [vulkan-tutorial.com](https://vulkan-tutorial.com).
257 - [ash-sample-progression](https://github.com/bzm3r/ash-sample-progression) - A port of the LunarG examples.
258 - [ash-nv-rt](https://github.com/gwihlidal/ash-nv-rt) A raytracing example for ash.
259
260 ### Utility libraries
261
262 - [vk-sync](https://github.com/gwihlidal/vk-sync-rs) - Simplified Vulkan synchronization logic, written in rust.
263 - [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.
264 - [gpu-allocator](https://github.com/Traverse-Research/gpu-allocator) - GPU Memory allocator written in pure Rust for Vulkan and DirectX 12.
265 - [lahar](https://github.com/Ralith/lahar) - Tools for asynchronously uploading data to a Vulkan device.
266
267 ### Libraries that use ash
268
269 - [gfx-rs](https://github.com/gfx-rs/gfx) - gfx-rs is a low-level, cross-platform graphics abstraction library in Rust.
270
271 ## A thanks to
272
273 - [Api with no secrets](https://software.intel.com/en-us/articles/api-without-secrets-introduction-to-vulkan-part-1)
274 - [Vulkan tutorial](https://jhenriques.net/development.html)
275 - [Vulkan examples](https://github.com/SaschaWillems/Vulkan)
276 - [Vulkan tutorial](https://vulkan-tutorial.com/)
277 - [Vulkano](https://github.com/vulkano-rs/vulkano)
278 - [vk-rs](https://github.com/Osspial/vk-rs)