vulkanalia 0.28.0

Vulkan bindings for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// SPDX-License-Identifier: Apache-2.0

//! Vulkan bindings for Rust.
//!
//! For a detailed guide on how this crate (thinly) wraps the Vulkan API, see
//! the `API Concepts` section of the `Overview` chapter of the `vulkanalia`
//! Vulkan tutorial which can be found
//! [here](https://kylemayes.github.io/vulkanalia/overview.html#api-concepts).
//!
//! For the classic triangle example, see
//! [here](https://github.com/KyleMayes/vulkanalia/tree/master/examples/src).
//!
//! #### Quick Tips
//!
//! ##### 1. Use builder structs, but don't `build` them unless you need to!
//!
//! `vulkanalia`'s implementation of Vulkan structs like
//! [`vk::InstanceCreateInfo`] each have associated builder structs that make
//! constructing instances of these structs simple (
//! [`vk::InstanceCreateInfoBuilder`] in this case).
//!
//! You can call the `build` method on a builder struct to extract the Vulkan
//! struct, but this discards valuable information! Builder structs are often
//! parameterized with lifetimes that ensure that the values passed to the
//! builder struct live at least as long as the builder struct itself.
//!
//! Any Vulkan command in `vulkanalia` that takes a Vulkan struct like
//! [`vk::InstanceCreateInfo`] as an argument can also accept the associated
//! builder struct as an argument. This means you can skip those `build` calls
//! and directly invoke Vulkan commands with your builder structs so that the
//! lifetimes can protect you from an entire class of bugs.
//!
//! __Don't__
//!
//! ```ignore
//! let info = vk::InstanceCreateInfo::builder()
//!     .enabled_extension_names(&vec![/* 3 extension names */])
//!     .build();
//!
//! let instance = entry.create_instance(&info, None).unwrap();
//! // 💥: this will (hopefully) crash when the Vulkan implementation attempts
//! // to read a now deallocated list of extension names
//! ```
//!
//! __Do__
//!
//! ```ignore
//! let info = vk::InstanceCreateInfo::builder()
//!     .enabled_extension_names(&vec![/* 3 extension names */]);
//!     // Look ma, no `build`!
//!
//! let instance = entry.create_instance(&info, None).unwrap();
//! // 🦀: this will raise an error at compiletime about how the reference to
//! // the temporary list of extension names doesn't live long enough
//! ```

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "no_std_error", feature(error_in_core))]

extern crate alloc;

pub mod bytecode;
pub mod chain;
pub mod loader;
pub mod vk;

#[cfg(feature = "window")]
pub mod window;

use alloc::boxed::Box;
use alloc::collections::btree_set::BTreeSet;
use alloc::sync::Arc;
use core::ffi::c_char;
use core::fmt;
use core::mem;
use core::slice;

use self::loader::{Loader, LoaderError};
use self::prelude::v1_0::*;
use self::vk::{DeviceCommands, EntryCommands, InstanceCommands};

/// Preludes.
pub mod prelude {
    /// Vulkan 1.0 prelude.
    pub mod v1_0 {
        pub use crate::vk;
        pub use crate::vk::{DeviceV1_0, EntryV1_0, InstanceV1_0};
        pub use crate::vk::{Handle, HasBuilder};
        pub use crate::{Device, Entry, Instance, VkResult, VkSuccessResult};
    }

    /// Vulkan 1.1 prelude.
    pub mod v1_1 {
        pub use crate::prelude::v1_0::*;
        pub use crate::vk::{DeviceV1_1, EntryV1_1, InstanceV1_1};
    }

    /// Vulkan 1.2 prelude.
    pub mod v1_2 {
        pub use crate::prelude::v1_1::*;
        pub use crate::vk::{DeviceV1_2, EntryV1_2, InstanceV1_2};
    }

    /// Vulkan 1.3 prelude.
    pub mod v1_3 {
        pub use crate::prelude::v1_2::*;
        pub use crate::vk::{DeviceV1_3, EntryV1_3, InstanceV1_3};
    }
}

/// The result of a executing a fallible Vulkan command.
pub type VkResult<T> = Result<T, vk::ErrorCode>;
/// The result of a executing a fallible Vulkan command with multiple success codes.
pub type VkSuccessResult<T> = Result<(T, vk::SuccessCode), vk::ErrorCode>;

/// An extension trait for [`vk::Result`].
pub trait ResultExt {
    /// Converts a [`vk::Result`] into a [`VkResult`].
    fn result(self) -> VkResult<()>;
}

impl ResultExt for vk::Result {
    #[inline]
    fn result(self) -> VkResult<()> {
        match self {
            vk::Result::SUCCESS => Ok(()),
            error => Err(error.into()),
        }
    }
}

/// A Vulkan version.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Version {
    /// The major version (the `x` in `x.y.z`).
    pub major: u32,
    /// The minor version (the `y` in `x.y.z`).
    pub minor: u32,
    /// The patch version (the `z` in `x.y.z`).
    ///
    /// If the version of Vulkan is not at least `1.1.0`, then the patch version
    /// will not be known due to the `vkEnumerateInstanceVersion` function not
    /// being available. In this case this field is set to `0`.
    pub patch: u32,
}

impl Version {
    /// The version for Vulkan `1.0.x`.
    pub const V1_0_0: Version = Version::new(1, 0, 0);
    /// The version for Vulkan `1.1.0`.
    pub const V1_1_0: Version = Version::new(1, 1, 0);
    /// The version for Vulkan `1.2.0`.
    pub const V1_2_0: Version = Version::new(1, 2, 0);

    /// Constructs a new Vulkan version.
    pub const fn new(major: u32, minor: u32, patch: u32) -> Self {
        Self {
            major,
            minor,
            patch,
        }
    }
}

impl Default for Version {
    #[inline]
    fn default() -> Self {
        Self::V1_0_0
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
    }
}

impl From<u32> for Version {
    #[inline]
    fn from(version: u32) -> Self {
        Self::new(
            vk::version_major(version),
            vk::version_minor(version),
            vk::version_patch(version),
        )
    }
}

impl From<Version> for u32 {
    fn from(version: Version) -> Self {
        vk::make_version(version.major, version.minor, version.patch)
    }
}

impl From<(u32, u32, u32)> for Version {
    fn from((major, minor, patch): (u32, u32, u32)) -> Self {
        Self::new(major, minor, patch)
    }
}

impl From<Version> for (u32, u32, u32) {
    fn from(version: Version) -> Self {
        (version.major, version.minor, version.patch)
    }
}

/// A Vulkan entry point.
#[derive(Clone)]
pub struct Entry {
    _loader: Arc<dyn Loader>,
    get_instance: vk::PFN_vkGetInstanceProcAddr,
    get_device: vk::PFN_vkGetDeviceProcAddr,
    commands: EntryCommands,
}

impl Entry {
    /// Loads a Vulkan entry point using a Vulkan function loader.
    ///
    /// # Safety
    ///
    /// The [`Loader::load`] method will be called on the supplied [`Loader`]
    /// implementation to load the entry commands so the safety requirements of
    /// [`Loader::load`] for the [`Loader`] implementation used must be upheld.
    #[inline]
    pub unsafe fn new(loader: impl Loader + 'static) -> Result<Self, Box<dyn LoaderError>> {
        let loader = Arc::new(loader);

        type F = extern "system" fn();
        let raw = loader.load(b"vkGetInstanceProcAddr")?;
        let get_instance = mem::transmute::<F, vk::PFN_vkGetInstanceProcAddr>(raw);
        let raw = loader.load(b"vkGetDeviceProcAddr")?;
        let get_device = mem::transmute::<F, vk::PFN_vkGetDeviceProcAddr>(raw);

        let load = |n| get_instance(vk::Instance::null(), n);
        let commands = EntryCommands::load(load);

        Ok(Self {
            _loader: loader,
            get_instance,
            get_device,
            commands,
        })
    }

    /// Gets the instance-level version of this Vulkan entry point.
    #[inline]
    pub fn version(&self) -> VkResult<Version> {
        let name = b"vkEnumerateInstanceVersion\0".as_ptr() as *const c_char;
        let raw = unsafe { (self.get_instance)(vk::Instance::null(), name) };
        let enumerate: Option<vk::PFN_vkEnumerateInstanceVersion> = unsafe { mem::transmute(raw) };
        if let Some(enumerate) = enumerate {
            let mut version = 0;
            match unsafe { enumerate(&mut version) } {
                vk::Result::SUCCESS => Ok(Version::from(version)),
                error => Err(error.into()),
            }
        } else {
            Ok(Version::V1_0_0)
        }
    }

    /// Creates a Vulkan instance using this Vulkan entry point.
    ///
    /// # Safety
    ///
    /// The [`Loader::load`] method will be called on the supplied [`Loader`]
    /// implementation to load the instance commands so the safety requirements
    /// of [`Loader::load`] for the [`Loader`] implementation used must be
    /// upheld.
    #[inline]
    pub unsafe fn create_instance(
        &self,
        info: &vk::InstanceCreateInfo,
        allocator: Option<&vk::AllocationCallbacks>,
    ) -> VkResult<Instance> {
        let handle = EntryV1_0::create_instance(self, info, allocator)?;
        let instance_load = |n| (self.get_instance)(handle, n);
        let commands = InstanceCommands::load(instance_load);
        let version = self.version()?;
        let extensions = get_names(info.enabled_extension_count, info.enabled_extension_names);
        let layers = get_names(info.enabled_layer_count, info.enabled_layer_names);
        Ok(Instance {
            get_instance: self.get_instance,
            get_device: self.get_device,
            handle,
            commands,
            version,
            extensions,
            layers,
        })
    }
}

impl fmt::Debug for Entry {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Entry")
    }
}

unsafe impl Send for Entry {}
unsafe impl Sync for Entry {}

/// A Vulkan instance.
#[derive(Clone)]
pub struct Instance {
    get_instance: vk::PFN_vkGetInstanceProcAddr,
    get_device: vk::PFN_vkGetDeviceProcAddr,
    handle: vk::Instance,
    commands: InstanceCommands,
    version: Version,
    extensions: BTreeSet<vk::ExtensionName>,
    layers: BTreeSet<vk::ExtensionName>,
}

impl Instance {
    /// Gets the version for this Vulkan instance.
    #[inline]
    pub fn version(&self) -> Version {
        self.version
    }

    /// Gets the loaded extensions for this Vulkan instance.
    #[inline]
    pub fn extensions(&self) -> &BTreeSet<vk::ExtensionName> {
        &self.extensions
    }

    /// Gets the loaded layers for this Vulkan instance.
    #[inline]
    pub fn layers(&self) -> &BTreeSet<vk::ExtensionName> {
        &self.layers
    }

    /// Creates a Vulkan device using this Vulkan instance.
    ///
    /// # Safety
    ///
    /// The [`Loader::load`] method will be called on the supplied [`Loader`]
    /// implementation to load the device commands so the safety requirements of
    /// [`Loader::load`] for the [`Loader`] implementation used must be upheld.
    #[inline]
    pub unsafe fn create_device(
        &self,
        physical_device: vk::PhysicalDevice,
        info: &vk::DeviceCreateInfo,
        allocator: Option<&vk::AllocationCallbacks>,
    ) -> VkResult<Device> {
        let handle = InstanceV1_0::create_device(self, physical_device, info, allocator)?;
        let instance_load = |n| (self.get_instance)(self.handle, n);
        let device_load = |n| (self.get_device)(handle, n);
        let commands = DeviceCommands::load(instance_load, device_load);
        let extensions = get_names(info.enabled_extension_count, info.enabled_extension_names);
        let layers = get_names(info.enabled_layer_count, info.enabled_layer_names);
        Ok(Device {
            handle,
            commands,
            extensions,
            layers,
        })
    }
}

impl fmt::Debug for Instance {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Instance")
            .field("handle", &self.handle)
            .field("extensions", &self.extensions)
            .field("layers", &self.layers)
            .finish()
    }
}

unsafe impl Send for Instance {}
unsafe impl Sync for Instance {}

/// A Vulkan device.
#[derive(Clone)]
pub struct Device {
    handle: vk::Device,
    commands: DeviceCommands,
    extensions: BTreeSet<vk::ExtensionName>,
    layers: BTreeSet<vk::ExtensionName>,
}

impl Device {
    /// Gets the loaded extensions for this Vulkan device.
    #[inline]
    pub fn extensions(&self) -> &BTreeSet<vk::ExtensionName> {
        &self.extensions
    }

    /// Gets the loaded layers for this Vulkan device.
    #[inline]
    pub fn layers(&self) -> &BTreeSet<vk::ExtensionName> {
        &self.layers
    }
}

impl fmt::Debug for Device {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Device")
            .field("handle", &self.handle)
            .field("extensions", &self.extensions)
            .field("layers", &self.layers)
            .finish()
    }
}

unsafe impl Send for Device {}
unsafe impl Sync for Device {}

#[inline]
unsafe fn get_names(
    num_strings: u32,
    strings: *const *const c_char,
) -> BTreeSet<vk::ExtensionName> {
    if strings.is_null() || num_strings == 0 {
        return BTreeSet::new();
    }

    slice::from_raw_parts(strings, num_strings as usize)
        .iter()
        .map(|s| vk::ExtensionName::from_ptr(*s))
        .collect()
}