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/// - `#[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))
30///
31///     Used to increase the alignment of the field
32///
33/// - `#[shader(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/// - `#[shader(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 `#[shader(align(X))]` and `#[shader(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 crate::encase::ShaderType;
54/// #[derive(ShaderType)]
55/// struct AffineTransform2D {
56///     matrix: test_impl::Mat2x2f,
57///     translate: test_impl::Vec2f,
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 crate::encase::ShaderType;
67/// # use crate::encase::ArrayLength;
68/// #[derive(ShaderType)]
69/// struct Positions {
70///     length: ArrayLength,
71///     #[shader(size(runtime))]
72///     positions: Vec<test_impl::Vec2f>
73/// }
74/// ```
75///
76/// Complex
77///
78/// ```
79/// # use crate::encase::{ShaderType, ShaderSize};
80/// #[derive(ShaderType)]
81/// struct Complex<
82///     'a,
83///     'b: 'a,
84///     E: 'a + ShaderType + ShaderSize,
85///     T: 'b + ShaderType + ShaderSize,
86///     const N: usize,
87/// > {
88///     array: [&'a mut E; N],
89///     #[shader(size(runtime))]
90///     rts_array: &'a mut Vec<&'b T>,
91/// }
92/// ```
93///
94pub use encase_derive::ShaderType;
95
96#[macro_use]
97mod utils;
98mod core;
99mod types;
100
101pub use crate::core::{
102    CalculateSizeFor, DynamicStorageBuffer, DynamicUniformBuffer, ShaderSize, ShaderType,
103    StorageBuffer, UniformBuffer,
104};
105pub use types::runtime_sized_array::ArrayLength;
106
107pub mod internal {
108    pub use super::core::{
109        AlignmentValue, BufferMut, BufferRef, CreateFrom, EnlargeError, Error, ReadContext,
110        ReadFrom, Reader, Result, SizeValue, WriteContext, WriteInto, Writer,
111    };
112}
113
114/// Module containing items necessary to implement `ShaderType` for runtime-sized arrays
115pub mod rts_array {
116    #[doc(inline)]
117    pub use super::impl_rts_array;
118    pub use super::types::runtime_sized_array::{Length, Truncate};
119}
120
121/// Module containing items necessary to implement `ShaderType` for vectors
122pub mod vector {
123    #[doc(inline)]
124    pub use super::impl_vector;
125    pub use super::types::vector::{
126        AsMutVectorParts, AsRefVectorParts, FromVectorParts, VectorScalar,
127    };
128}
129
130/// Module containing items necessary to implement `ShaderType` for matrices
131pub mod matrix {
132    #[doc(inline)]
133    pub use super::impl_matrix;
134    pub use super::types::matrix::{
135        AsMutMatrixParts, AsRefMatrixParts, FromMatrixParts, MatrixScalar,
136    };
137}
138
139/// Private module used by macros
140#[doc(hidden)]
141pub mod private {
142    pub use super::build_struct;
143    pub use super::core::AlignmentValue;
144    pub use super::core::BufferMut;
145    pub use super::core::BufferRef;
146    pub use super::core::CreateFrom;
147    pub use super::core::Metadata;
148    pub use super::core::ReadFrom;
149    pub use super::core::Reader;
150    pub use super::core::RuntimeSizedArray;
151    pub use super::core::SizeValue;
152    pub use super::core::WriteInto;
153    pub use super::core::Writer;
154    pub use super::types::array::ArrayMetadata;
155    pub use super::types::matrix::*;
156    pub use super::types::r#struct::StructMetadata;
157    pub use super::types::runtime_sized_array::{ArrayLength, Length, Truncate};
158    pub use super::types::vector::*;
159    pub use super::utils::consume_zsts;
160    pub use super::CalculateSizeFor;
161    pub use super::ShaderSize;
162    pub use super::ShaderType;
163    pub use const_panic::concat_assert;
164}