[−][src]Crate shader_types
DO NOT USE THIS CRATE
There are fundamental flaws with this method of defining padding. This crate is kept live
and not yanked for backwards compat only. For a crate that serves this exact purpose, look
at the wonderful glsl-layout
which doesn't suffer from
the same problems.
Old Readme:
Vector and Matrix types that are properly aligned for use in std140 uniforms.
All the types in this library have the same alignment and size as the equivilant glsl type in the default mode (std140).
This fixes the padding within members of structs but padding between members needs to be minded.
The types in padding
are there to make this easier.
Vectors are constructable to/from an array of their underlying type. Matrices are constructable
to/from both 1d and 2d arrays as well as an array of the underlying vector type. (eg. Mat2
can be
constructed from [Vec2; 2]
)
Features
bytemuck
all types implementZeroable
andPod
.mint
enable conversions to/from equivalent mint types.
Example
For the following glsl:
layout(set = 0, binding = 0) uniform Block {
mat4 mvp;
vec3 position;
vec3 normal;
vec2 uv;
int constants[3];
};
This struct is rife with padding. However it's now easy to mind the padding:
use shader_types::{Vec2, Vec3, Mat4, ArrayMember}; // Definition #[repr(C)] #[derive(Copy, Clone)] struct UniformBlock { mvp: Mat4, // 16 align + 64 size position: Vec3, // 16 align + 12 size normal: Vec3, // 16 align + 12 size uv: Vec2, // 8 align + 8 size constants: [ArrayMember<i32>; 3] // 3x 16 align + 4 size } fn generate_mvp() -> [f32; 16] { // ... } // Construction let block = UniformBlock { // Anything that can be converted to a [f32; 16] or [[f32; 4]; 4] works mvp: Mat4::from(generate_mvp()), position: Vec3::new([0.0, 1.0, 2.0]), // `from` also works normal: Vec3::new([-2.0, 2.0, 3.0]), uv: Vec2::new([0.0, 1.0]), constants: [ArrayMember(0), ArrayMember(1), ArrayMember(2)] }; // Supports bytemuck with the `bytemuck` feature unsafe impl bytemuck::Zeroable for UniformBlock {} unsafe impl bytemuck::Pod for UniformBlock {} let block_u8: &[u8] = bytemuck::cast_slice(&[block]);
MSRV
Rust 1.34
Modules
padding | Correctly sized padding helpers. |
Structs
ArrayMember | Pads an element to be in an array in a shader. |
BVec2 | Vector of 2 |
BVec3 | Vector of 3 |
BVec4 | Vector of 4 |
Bool | A boolean value. Has size 4 and alignment 4. |
DMat2x2 | Matrix of |
DMat2x3 | Matrix of |
DMat2x4 | Matrix of |
DMat3x2 | Matrix of |
DMat3x3 | Matrix of |
DMat3x4 | Matrix of |
DMat4x2 | Matrix of |
DMat4x3 | Matrix of |
DMat4x4 | Matrix of |
DVec2 | Vector of 2 |
DVec3 | Vector of 3 |
DVec4 | Vector of 4 |
DynamicOffsetMember | Pads a structure for use with dynamic offsets in graphics apis. |
IVec2 | Vector of 2 |
IVec3 | Vector of 3 |
IVec4 | Vector of 4 |
Mat2x2 | Matrix of |
Mat2x3 | Matrix of |
Mat2x4 | Matrix of |
Mat3x2 | Matrix of |
Mat3x3 | Matrix of |
Mat3x4 | Matrix of |
Mat4x2 | Matrix of |
Mat4x3 | Matrix of |
Mat4x4 | Matrix of |
UVec2 | Vector of 2 |
UVec3 | Vector of 3 |
UVec4 | Vector of 4 |
Vec2 | Vector of 2 |
Vec3 | Vector of 3 |
Vec4 | Vector of 4 |
Type Definitions
DMat2 | Matrix of f64s with 2 columns and 3 rows. Alignment 16, size 48. |
DMat3 | Matrix of f64s with 3 columns and 3 rows. Alignment 32, size 96. |
DMat4 | Matrix of f64s with 4 columns and 4 rows. Alignment 32, size 128. |
Double | A single double-precision floating point number. Has size 8 and alignment 8. |
Float | A single single-precision floating point number. Has size 4 and alignment 4. |
Int | A single signed integer. Has size 4 and alignment 4. |
Mat2 | Matrix of f32s with 2 columns and 2 rows. Alignment 8, size 16. |
Mat3 | Matrix of f32s with 3 columns and 3 rows. Alignment 16, size 48. |
Mat4 | Matrix of f32s with 4 columns and 4 rows. Alignment 16, size 64. |
Uint | A single unsigned integer. Has size 4 and alignment 4. |