ash_direct_entry/lib.rs
1//! This crate provides an `EntryCustom` that directly links to libvulkan, so situations where
2//! `dlopen()`-ing it is undesirable (e.g. NixOS) can be avoided.
3//!
4//! ## Example
5//!
6//! ```
7//! # fn main() {
8//! use ash::vk;
9//! use ash_direct_entry::LibVulkan;
10//!
11//! let entry = LibVulkan::entry();
12//!
13//! let app_info = vk::ApplicationInfo {
14//! api_version: vk::make_api_version(0, 1, 0, 0),
15//! ..Default::default()
16//! };
17//! let create_info = vk::InstanceCreateInfo {
18//! p_application_info: &app_info,
19//! ..Default::default()
20//! };
21//! let instance = unsafe { entry.create_instance(&create_info, None).unwrap() };
22//! # }
23//! ```
24
25use ash::EntryCustom;
26use std::{ffi::c_void, os::raw::c_char, ptr};
27
28/// A convenient alias.
29pub type Entry = EntryCustom<LibVulkan>;
30
31/// A marker type for [`EntryCustom`](https://docs.rs/ash/0.32.1/ash/struct.EntryCustom.html).
32#[derive(Clone, Copy, Debug)]
33pub struct LibVulkan;
34
35impl LibVulkan {
36 /// Returns an ash Entry for the libvulkan that is linked to the binary.
37 ///
38 /// ## Example
39 ///
40 /// ```
41 /// # fn main() {
42 /// use ash_direct_entry::LibVulkan;
43 ///
44 /// let entry = LibVulkan::entry();
45 /// entry.fp_v1_0();
46 /// entry.fp_v1_1();
47 /// entry.fp_v1_2();
48 /// # }
49 /// ```
50 pub fn entry() -> EntryCustom<LibVulkan> {
51 EntryCustom::new_custom(LibVulkan, |_, name| match name.to_bytes() {
52 b"vkGetInstanceProcAddr" => vkGetInstanceProcAddr as *mut c_void,
53 _ => ptr::null(),
54 })
55 .expect("The behavior of ash changed in an unexpected way")
56 }
57}
58
59#[link(name = "vulkan")]
60extern "C" {
61 fn vkGetInstanceProcAddr(instance: *mut c_void, name: *const c_char) -> extern "C" fn();
62}