Skip to main content

naga_rust_rt/
lib.rs

1//! This library provides types and functions which are used by the Rust code generated by
2//! [`naga_rust_back`] or [`naga_rust_embed`]. These types and functions implement vector arithmetic
3//! in the style expected by shader code, which differs from straightforward Rust in various ways.
4//!
5//! This library currently does not have a clean public API distinct from the API provided
6//! for the benefit of the code generator. Expect changes in future versions.
7//!
8//! [`naga_rust_back`]: https://docs.rs/naga-rust-back
9//! [`naga_rust_embed`]: https://docs.rs/naga-rust-embed
10
11#![no_std]
12
13#[cfg(feature = "alloc")]
14extern crate alloc;
15#[cfg(feature = "std")]
16extern crate std;
17
18mod matrix;
19pub mod texture;
20mod vector;
21
22// -------------------------------------------------------------------------------------------------
23
24pub use matrix::*;
25pub use texture::{
26    Sampler, Texture1d, Texture2d, Texture2dArray, Texture3d, TextureCube, TextureCubeArray,
27    TextureMultisampled2d,
28};
29pub use vector::*;
30
31// The generated code may use Into trait bounds, so it needs to name the trait.
32pub use core::convert::Into;
33// The generated code may implement Default, so it needs to name the trait.
34pub use core::default::Default;
35
36// -------------------------------------------------------------------------------------------------
37
38/// Returns the [WGSL zero value] of a type.
39///
40/// Conveniently, all of the types for which this is defined also implement [`Default`] to return
41/// zero, so this is actually an alias for [`Default::default()`].
42///
43/// [WGSL zero value]: https://www.w3.org/TR/2026/CRD-WGSL-20260507/#zero-value
44pub fn zero<T: Default>() -> T {
45    Default::default()
46}
47
48/// Equivalent to [`Into::into()`].
49///
50/// This function is provided for the use of the generated code in order to avoid the risk of
51/// method name clashes.
52pub fn into<T, U>(input: T) -> U
53where
54    T: Into<U>,
55{
56    input.into()
57}
58
59pub fn discard() {
60    // Best we can do for now, until we implement a codegen option to return Result instead.
61    panic!("shader reached discard instruction");
62}
63
64// Attributes which naga_rust_back emits that are currently for documentation purposes only,
65// and don’t actually transform the item.
66pub use naga_rust_macros::dummy_attribute as compute;
67pub use naga_rust_macros::dummy_attribute as fragment;
68pub use naga_rust_macros::dummy_attribute as vertex;
69pub use naga_rust_macros::dummy_attribute as workgroup_size;