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
// SPDX-License-Identifier: Apache-2.0


//! Vulkan bindings for Rust.


pub mod loader;
pub mod vk;

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

use std::collections::HashSet;
use std::error;
use std::ffi::{CStr, CString};
use std::fmt;
use std::mem;
use std::os::raw::c_char;
use std::slice;
use std::sync::Arc;

use self::loader::Loader;
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::{ConvertCStr, Handle, HasBuilder};
        pub use crate::vk::{DeviceV1_0, EntryV1_0, InstanceV1_0};
        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};
    }
}

/// 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>;

/// 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.

    pub patch: Option<u32>,
}

impl Default for Version {
    #[inline]
    fn default() -> Self {
        Self {
            major: 1,
            minor: 0,
            patch: None,
        }
    }
}

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

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

/// 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.

    #[inline]
    pub fn new(loader: impl Loader + 'static) -> Result<Self, Box<dyn error::Error + 'static>> {
        let loader = Arc::new(loader);

        let raw = unsafe { loader.load(b"vkGetInstanceProcAddr")? };
        let get_instance = unsafe { mem::transmute::<_, vk::PFN_vkGetInstanceProcAddr>(raw) };
        let raw = unsafe { loader.load(b"vkGetDeviceProcAddr")? };
        let get_device = unsafe { mem::transmute::<_, vk::PFN_vkGetDeviceProcAddr>(raw) };

        let load = |n| unsafe { mem::transmute(get_instance(vk::Instance::null(), n)) };
        let commands = EntryCommands::load(load);

        Ok(Self {
            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 = (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 enumerate(&mut version) {
                vk::Result::SUCCESS => Ok(Version::from(version)),
                error => Err(error.into()),
            }
        } else {
            Ok(Version::default())
        }
    }

    /// Creates a Vulkan instance using this Vulkan entry point.

    #[inline]
    pub fn create_instance(
        &self,
        info: &vk::InstanceCreateInfo,
        allocator: Option<&vk::AllocationCallbacks>,
    ) -> VkResult<Instance> {
        let handle = EntryV1_0::create_instance(self, info, allocator)?;
        let load = |n| unsafe { mem::transmute((self.get_instance)(handle, n)) };
        let commands = InstanceCommands::load(load);
        let extensions = get_extensions(info.enabled_extension_count, info.enabled_extension_names);
        let layers = get_layers(info.enabled_layer_count, info.enabled_layer_names);
        Ok(Instance {
            get_device: self.get_device,
            handle,
            commands,
            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_device: vk::PFN_vkGetDeviceProcAddr,
    handle: vk::Instance,
    commands: InstanceCommands,
    extensions: HashSet<vk::ExtensionName>,
    layers: HashSet<CString>,
}

impl Instance {
    /// Gets the loaded extensions for this Vulkan instance.

    #[inline]
    pub fn extensions(&self) -> &HashSet<vk::ExtensionName> {
        &self.extensions
    }

    /// Gets the loaded layers for this Vulkan instance.

    #[inline]
    pub fn layers(&self) -> &HashSet<CString> {
        &self.layers
    }

    /// Creates a Vulkan device using this Vulkan instance.

    #[inline]
    pub 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 load = |n| unsafe { mem::transmute((self.get_device)(handle, n)) };
        let commands = DeviceCommands::load(load);
        let extensions = get_extensions(info.enabled_extension_count, info.enabled_extension_names);
        let layers = get_layers(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: HashSet<vk::ExtensionName>,
    layers: HashSet<CString>,
}

impl Device {
    /// Gets the loaded extensions for this Vulkan device.

    #[inline]
    pub fn extensions(&self) -> &HashSet<vk::ExtensionName> {
        &self.extensions
    }

    /// Gets the loaded layers for this Vulkan device.

    #[inline]
    pub fn layers(&self) -> &HashSet<CString> {
        &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]
fn get_extensions(num_strings: u32, strings: *const *const c_char) -> HashSet<vk::ExtensionName> {
    unsafe { slice::from_raw_parts(strings, num_strings as usize) }
        .iter()
        .map(|s| vk::ExtensionName::from_cstr(unsafe { CStr::from_ptr(*s) }))
        .collect()
}

#[inline]
fn get_layers(num_layers: u32, layers: *const *const c_char) -> HashSet<CString> {
    unsafe { slice::from_raw_parts(layers, num_layers as usize) }
        .iter()
        .map(|l| unsafe { CStr::from_ptr(*l) }.into())
        .collect()
}