Skip to main content

encase/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![deny(rustdoc::broken_intra_doc_links)]
3#![warn(
4    clippy::semicolon_if_nothing_returned,
5    future_incompatible,
6    nonstandard_style,
7    rust_2018_idioms,
8    rust_2021_compatibility,
9    unused,
10    // missing_docs,
11    single_use_lifetimes,
12    trivial_casts,
13    trivial_numeric_casts,
14    // unreachable_pub,
15    unused_qualifications,
16    variant_size_differences
17)]
18#![doc = include_str!("../README.md")]
19#![doc(
20    html_logo_url = "https://raw.githubusercontent.com/teoxoy/encase/3d6d2e4d7670863e97463a15ceeafac6d13ee73e/logo.svg"
21)]
22#![no_std]
23
24extern crate alloc;
25
26/// Used to implement `ShaderType` for structs
27///
28/// # Attributes
29///
30/// Field attributes
31///
32/// - `#[shader(align(X))]` where `X` is a power of 2 [`u32`] literal (equivalent to [WGSL align attribute](https://gpuweb.github.io/gpuweb/wgsl/#attribute-align))
33///
34///     Used to increase the alignment of the field
35///
36/// - `#[shader(size(X))]` where `X` is a [`u32`] literal (equivalent to [WGSL size attribute](https://gpuweb.github.io/gpuweb/wgsl/#attribute-size))
37///
38///     Used to increase the size of the field
39///
40/// - `#[shader(size(runtime))]` can only be attached to the last field of the struct
41///
42///     Used to denote the fact that the field it is attached to is a runtime-sized array
43///
44/// # Note about generics
45///
46/// While structs using generic type parameters are supported by this derive macro
47///
48/// - the `#[shader(align(X))]` and `#[shader(size(X))]` attributes will only work
49///   if they are attached to fields whose type contains no generic type parameters
50///
51/// # Examples
52///
53/// Simple
54///
55/// ```
56/// # use crate::encase::ShaderType;
57/// #[derive(ShaderType)]
58/// struct AffineTransform2D {
59///     matrix: test_impl::Mat2x2f,
60///     translate: test_impl::Vec2f,
61/// }
62/// ```
63///
64/// Contains a runtime-sized array
65///
66/// _The [`ArrayLength`] type can be used to explicitly write or read the length of the contained runtime-sized array_
67///
68/// ```
69/// # use crate::encase::ShaderType;
70/// # use crate::encase::ArrayLength;
71/// #[derive(ShaderType)]
72/// struct Positions {
73///     length: ArrayLength,
74///     #[shader(size(runtime))]
75///     positions: Vec<test_impl::Vec2f>
76/// }
77/// ```
78///
79/// Complex
80///
81/// ```
82/// # use crate::encase::{ShaderType, ShaderSize};
83/// #[derive(ShaderType)]
84/// struct Complex<
85///     'a,
86///     'b: 'a,
87///     E: 'a + ShaderType + ShaderSize,
88///     T: 'b + ShaderType + ShaderSize,
89///     const N: usize,
90/// > {
91///     array: [&'a mut E; N],
92///     #[shader(size(runtime))]
93///     rts_array: &'a mut Vec<&'b T>,
94/// }
95/// ```
96///
97pub use encase_derive::ShaderType;
98
99#[macro_use]
100mod utils;
101mod core;
102mod types;
103
104pub use crate::core::{
105    CalculateSizeFor, DynamicStorageBuffer, DynamicUniformBuffer, ShaderSize, ShaderType,
106    StorageBuffer, UniformBuffer,
107};
108pub use types::runtime_sized_array::ArrayLength;
109
110pub mod internal {
111    pub use super::core::{
112        AlignmentValue, BufferMut, BufferRef, CreateFrom, EnlargeError, Error, ReadContext,
113        ReadFrom, Reader, Result, SizeValue, WriteContext, WriteInto, Writer,
114    };
115}
116
117/// Module containing items necessary to implement `ShaderType` for runtime-sized arrays
118pub mod rts_array {
119    #[doc(inline)]
120    pub use super::impl_rts_array;
121    pub use super::types::runtime_sized_array::{Length, Truncate};
122}
123
124/// Module containing items necessary to implement `ShaderType` for vectors
125pub mod vector {
126    #[doc(inline)]
127    pub use super::impl_vector;
128    pub use super::types::vector::{
129        AsMutVectorParts, AsRefVectorParts, FromVectorParts, VectorScalar,
130    };
131}
132
133/// Module containing items necessary to implement `ShaderType` for matrices
134pub mod matrix {
135    #[doc(inline)]
136    pub use super::impl_matrix;
137    pub use super::types::matrix::{
138        AsMutMatrixParts, AsRefMatrixParts, FromMatrixParts, MatrixScalar,
139    };
140}
141
142/// Private module used by macros
143#[doc(hidden)]
144pub mod private {
145    pub use super::build_struct;
146    pub use super::core::AlignmentValue;
147    pub use super::core::BufferMut;
148    pub use super::core::BufferRef;
149    pub use super::core::CreateFrom;
150    pub use super::core::Metadata;
151    pub use super::core::ReadFrom;
152    pub use super::core::Reader;
153    pub use super::core::RuntimeSizedArray;
154    pub use super::core::SizeValue;
155    pub use super::core::WriteInto;
156    pub use super::core::Writer;
157    pub use super::types::array::ArrayMetadata;
158    pub use super::types::matrix::*;
159    pub use super::types::r#struct::StructMetadata;
160    pub use super::types::runtime_sized_array::{ArrayLength, Length, Truncate};
161    pub use super::types::vector::*;
162    pub use super::utils::consume_zsts;
163    pub use super::CalculateSizeFor;
164    pub use super::ShaderSize;
165    pub use super::ShaderType;
166    pub use const_panic::concat_assert;
167}