encase/
lib.rs

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