vulkan_rust/lib.rs
1#![warn(missing_docs)]
2#![forbid(unsafe_op_in_unsafe_fn)]
3
4//! Ergonomic Vulkan wrapper built on generated FFI bindings.
5//!
6//! `vulkan-rust` provides safe-ish wrappers around the raw Vulkan API exposed by
7//! [`vulkan-rust-sys`](vk). The raw types live in the re-exported [`vk`] module; this
8//! crate adds ergonomic methods on [`Entry`], [`Instance`], and [`Device`] that
9//! handle output parameters, two-call enumeration, and error checking.
10//!
11//! # Quick start
12//!
13//! ```no_run
14//! use vulkan_rust::{Entry, LibloadingLoader};
15//!
16//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
17//! let loader = unsafe { LibloadingLoader::new() }?;
18//! let entry = unsafe { Entry::new(loader) }?;
19//! // entry is now ready to create instances, enumerate layers, etc.
20//! # Ok(())
21//! # }
22//! ```
23//!
24//! # Panics
25//!
26//! Wrapper methods on [`Instance`] and [`Device`] panic if the underlying
27//! Vulkan function pointer was not loaded. This happens when calling a
28//! command from an extension or Vulkan version that was not enabled at
29//! instance or device creation time. The panic message names the missing
30//! command (e.g., `"vkCmdDrawMeshTasksEXT not loaded"`).
31//!
32//! Core Vulkan 1.0 commands are always loaded and will not panic.
33//!
34//! # Crate structure
35//!
36//! | Module | Purpose |
37//! |--------|---------|
38//! | [`vk`] | Re-export of `vulkan-rust-sys`,raw `#[repr(C)]` types, handles, enums |
39//! | [`Entry`] | Vulkan entry point,loads the library, enumerates layers/extensions |
40//! | [`Instance`] | Vulkan instance,physical device queries, instance-level commands |
41//! | [`Device`] | Vulkan logical device,all device-level commands |
42//! | [`bytecode`] | SPIR-V byte alignment helpers |
43//! | [`Version`] | Decoded Vulkan version (major.minor.patch) from a packed `u32` |
44//!
45//! # Guide
46//!
47//! The companion [vulkan_rust Guide](https://hiddentale.github.io/vulkan_rust/)
48//! covers Vulkan concepts in depth:
49//!
50//! | Topic | Guide chapter |
51//! |-------|---------------|
52//! | First steps | [Hello Triangle tutorial](https://hiddentale.github.io/vulkan_rust/getting-started/hello-triangle-1.html) |
53//! | Handles, lifetimes, parent-child | [The Vulkan Object Model](https://hiddentale.github.io/vulkan_rust/concepts/object-model.html) |
54//! | Heaps, allocation, mapping | [Memory Management](https://hiddentale.github.io/vulkan_rust/concepts/memory.html) |
55//! | Fences, semaphores, barriers | [Synchronization](https://hiddentale.github.io/vulkan_rust/concepts/synchronization.html) |
56//! | Attachments, subpasses | [Render Passes & Framebuffers](https://hiddentale.github.io/vulkan_rust/concepts/render-passes.html) |
57//! | Graphics & compute pipelines | [Pipelines](https://hiddentale.github.io/vulkan_rust/concepts/pipelines.html) |
58//! | Layouts, pools, sets | [Descriptor Sets & Resource Binding](https://hiddentale.github.io/vulkan_rust/concepts/descriptors.html) |
59//! | Recording & submission | [Command Buffers](https://hiddentale.github.io/vulkan_rust/concepts/command-buffers.html) |
60//! | Extension struct chains | [The pNext Extension Chain](https://hiddentale.github.io/vulkan_rust/concepts/pnext.html) |
61//! | Debug messenger, layers | [Validation Layers & Debugging](https://hiddentale.github.io/vulkan_rust/concepts/validation.html) |
62//! | Safety model, two-crate design | [Design Decisions](https://hiddentale.github.io/vulkan_rust/architecture/design.html) |
63//! | Error types, Result pattern | [Error Handling Philosophy](https://hiddentale.github.io/vulkan_rust/architecture/errors.html) |
64//! | Porting from ash | [Migration Guide](https://hiddentale.github.io/vulkan_rust/how-to/migrate-from-ash.html) |
65//! | C API to Rust mapping | [C-to-Rust Reference](https://hiddentale.github.io/vulkan_rust/how-to/c-to-rust.html) |
66//!
67//! # Feature flags
68//!
69//! | Flag | Default | Description |
70//! |------|---------|-------------|
71//! | `surface` | yes | Enables [`required_extensions`] and [`SurfaceError`] for window surface creation via [`raw-window-handle`](https://docs.rs/raw-window-handle). Disable with `default-features = false` for headless use. |
72
73#[doc(inline)]
74pub use vulkan_rust_sys as vk;
75
76pub mod bytecode;
77mod device;
78mod entry;
79mod error;
80mod generated;
81mod instance;
82mod loader;
83#[cfg(feature = "surface")]
84mod surface;
85#[doc(hidden)]
86pub mod test_helpers;
87mod version;
88
89pub use bytecode::{BytecodeError, cast_to_u32};
90pub use device::Device;
91pub use entry::Entry;
92pub use error::{LoadError, VkError, VkResult};
93pub use instance::Instance;
94pub use loader::{LibloadingLoader, Loader};
95#[cfg(feature = "surface")]
96pub use surface::{SurfaceError, required_extensions};
97pub use version::Version;
98
99// Static assertions: Entry, Instance, and Device must be Send + Sync.
100// They are auto-derived (handle is usize, commands are Option<fn ptr>,
101// loader is Option<Arc<dyn Loader>> where Loader: Send + Sync), but
102// we pin this here so a future field change triggers a compile error.
103const _: () = {
104 fn assert_send_sync<T: Send + Sync>() {}
105 #[allow(dead_code)]
106 fn _check() {
107 assert_send_sync::<Entry>();
108 assert_send_sync::<Instance>();
109 assert_send_sync::<Device>();
110 }
111};
112
113/// Shared mutex for Vulkan runtime tests.
114///
115/// NVIDIA implicit layers (`VK_LAYER_NV_optimus`, `VK_LAYER_NV_present`)
116/// are not thread-safe during concurrent `vkCreateInstance` calls. All
117/// `#[ignore]` tests that create Vulkan instances must acquire this lock.
118#[cfg(test)]
119pub(crate) static VK_TEST_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());