Skip to main content

nox_spirv/
lib.rs

1//! Compact SPIR-V reflection library written in pure-Rust, with zero dependencies and minimal
2//! allocations.
3//!
4//! # Allocation policy
5//! - Strings and arrays are just slices to the SPIR-V passed to [`Module`].
6//! - Only [`decorations`][1] and [`instructions streams`][2] with a [`result id`][3] are cached using
7//!   an extra allocation.
8//! - Parsing is mostly done on-demand.
9//! 
10//! [1]: op::Decoration
11//! [2]: stream::InstructionStream
12//! [3]: op::IdResult
13//! # Usage 
14//! ``` rust
15//! use nox_spirv::op;
16//! use nox_spirv::Module;
17//! use nox_spirv::reflect::{Reflector, ResourceType};
18//! 
19//! let spirv: &[u32] = ...;
20//! let module = Module::new(spirv);
21//! let mut reflector = Reflector::new(module).unwrap();
22//! // Must be set before reflecting resources.
23//! reflector.set_entry_point(c"main", op::ExecutionModel::FRAGMENT).unwrap();
24//! for ubo in reflector.resources_for_type(ResourceType::UniformBuffer).unwrap() {
25//!     match ubo {
26//!         Ok(ubo) => {
27//!             let mut set = None;
28//!             let mut binding = None;
29//!             for dec in reflector.decorations(ubo.variable_id) {
30//!                 if let op::Decoration::DescriptorSet { descriptor_set } = dec.decoration {
31//!                     set = Some(descriptor_set);
32//!                 } else if let op::Decoration::Binding { binding_point } = dec.decoration {
33//!                     binding = Some(binding_point);
34//!                 }
35//!             }
36//!             println!("Uniform buffer (set {}, binding {}): {}",
37//!                 set.unwrap(), binding.unwrap(), ubo.name.unwrap_or_default(),
38//!             );
39//!         },
40//!         Err(err) => eprintln!("parse error: {err}")
41//!     }
42//! }
43//! for pc in reflector.resources_for_type(ResourceType::PushConstant).unwrap() {
44//!     match pc {
45//!         Ok(pc) => {
46//!             let size = reflector
47//!                 .type_description(pc.base_type_id)
48//!                 .unwrap().size_hint.declared();
49//!             println!("Push constant (size {size}): {}", pc.name.unwrap_or_default());
50//!         },
51//!         Err(err) => eprintln!("parse error: {err}"),
52//!     }
53//! }
54//! ```
55
56pub mod stream;
57pub mod op;
58mod module;
59pub mod reflect;
60
61
62mod core;
63
64pub use {
65    core::*,
66    module::*,
67};