encase/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![deny(rustdoc::broken_intra_doc_links)]
3#![warn(
4    future_incompatible,
5    nonstandard_style,
6    rust_2018_idioms,
7    rust_2021_compatibility,
8    unused,
9    // missing_docs,
10    single_use_lifetimes,
11    trivial_casts,
12    trivial_numeric_casts,
13    // unreachable_pub,
14    unused_qualifications,
15    variant_size_differences
16)]
17#![doc = include_str!("../README.md")]
18#![doc(
19    html_logo_url = "https://raw.githubusercontent.com/teoxoy/encase/3d6d2e4d7670863e97463a15ceeafac6d13ee73e/logo.svg"
20)]
21
22/// Used to implement `ShaderType` for structs
23///
24/// # Attributes
25///
26/// Field attributes
27///
28/// - `#[align(X)]` where `X` is a power of 2 [`u32`] literal (equivalent to [WGSL align attribute](https://gpuweb.github.io/gpuweb/wgsl/#attribute-align))
29///
30///     Used to increase the alignment of the field
31///
32/// - `#[size(X)]` where `X` is a [`u32`] literal (equivalent to [WGSL size attribute](https://gpuweb.github.io/gpuweb/wgsl/#attribute-size))
33///
34///     Used to increase the size of the field
35///
36/// - `#[size(runtime)]` can only be attached to the last field of the struct
37///
38///     Used to denote the fact that the field it is attached to is a runtime-sized array
39///
40/// # Note about generics
41///
42/// While structs using generic type parameters are supported by this derive macro
43///
44/// - the `#[align(X)]` and `#[size(X)]` attributes will only work
45///   if they are attached to fields whose type contains no generic type parameters
46///
47/// # Examples
48///
49/// Simple
50///
51/// ```
52/// # use mint;
53/// # use crate::encase::ShaderType;
54/// #[derive(ShaderType)]
55/// struct AffineTransform2D {
56///     matrix: mint::ColumnMatrix2<f32>,
57///     translate: mint::Vector2<f32>
58/// }
59/// ```
60///
61/// Contains a runtime-sized array
62///
63/// _The [`ArrayLength`] type can be used to explicitly write or read the length of the contained runtime-sized array_
64///
65/// ```
66/// # use mint;
67/// # use crate::encase::ShaderType;
68/// # use crate::encase::ArrayLength;
69/// #[derive(ShaderType)]
70/// struct Positions {
71///     length: ArrayLength,
72///     #[size(runtime)]
73///     positions: Vec<mint::Point2<f32>>
74/// }
75/// ```
76///
77/// Complex
78///
79/// ```
80/// # use crate::encase::{ShaderType, ShaderSize};
81/// #[derive(ShaderType)]
82/// struct Complex<
83///     'a,
84///     'b: 'a,
85///     E: 'a + ShaderType + ShaderSize,
86///     T: 'b + ShaderType + ShaderSize,
87///     const N: usize,
88/// > {
89///     array: [&'a mut E; N],
90///     #[size(runtime)]
91///     rts_array: &'a mut Vec<&'b T>,
92/// }
93/// ```
94///
95pub use encase_derive::ShaderType;
96
97#[macro_use]
98mod utils;
99mod core;
100mod types;
101
102mod impls;
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}