[][src]Crate shader_types

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])

Example

For the following glsl:

layout(set = 0, binding = 0) uniform Block {
    mat4 mvp;
    vec3 position;
    vec3 normal;
    vec2 uv;
}

This struct is rife with padding. However it's now easy to mind the padding:

use shader_types::{Vec2, Vec3, Mat4};
use shader_types::padding::Pad2Float;

// 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
    _padding: Pad2Float, // Struct is 16 byte aligned, so we need (the space of) 2 more floats.
}

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]),
    _padding: Pad2Float::new(), // `default` also works
};

// 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]);

Modules

padding

Correctly sized padding helpers.

Structs

DMat2x2

Matrix of f64s with 2 columns and 2 rows. Alignment 16, size 32.

DMat2x3

Matrix of f64s with 2 columns and 3 rows. Alignment 16, size 48.

DMat2x4

Matrix of f64s with 2 columns and 4 rows. Alignment 16, size 64.

DMat3x2

Matrix of f64s with 3 columns and 2 rows. Alignment 32, size 64.

DMat3x3

Matrix of f64s with 3 columns and 3 rows. Alignment 32, size 96.

DMat3x4

Matrix of f64s with 3 columns and 4 rows. Alignment 32, size 128.

DMat4x2

Matrix of f64s with 4 columns and 2 rows. Alignment 32, size 64.

DMat4x3

Matrix of f64s with 4 columns and 3 rows. Alignment 32, size 96.

DMat4x4

Matrix of f64s with 4 columns and 4 rows. Alignment 32, size 128.

DVec2

Vector of 2 f64s. Alignment 16, size 32.

DVec3

Vector of 3 f64s. Alignment 32, size 48.

DVec4

Vector of 4 f64s. Alignment 32, size 64.

IVec2

Vector of 2 i32s. Alignment 8, size 16.

IVec3

Vector of 3 i32s. Alignment 16, size 24.

IVec4

Vector of 4 i32s. Alignment 16, size 32.

Mat2x2

Matrix of f32s with 2 columns and 2 rows. Alignment 8, size 16.

Mat2x3

Matrix of f32s with 2 columns and 3 rows. Alignment 8, size 32.

Mat2x4

Matrix of f32s with 2 columns and 4 rows. Alignment 8, size 32.

Mat3x2

Matrix of f32s with 3 columns and 2 rows. Alignment 16, size 32.

Mat3x3

Matrix of f32s with 3 columns and 3 rows. Alignment 16, size 48.

Mat3x4

Matrix of f32s with 3 columns and 4 rows. Alignment 16, size 64.

Mat4x2

Matrix of f32s with 4 columns and 2 rows. Alignment 16, size 32.

Mat4x3

Matrix of f32s with 4 columns and 3 rows. Alignment 16, size 48.

Mat4x4

Matrix of f32s with 4 columns and 4 rows. Alignment 16, size 64.

UVec2

Vector of 2 u32s. Alignment 8, size 16.

UVec3

Vector of 3 u32s. Alignment 16, size 24.

UVec4

Vector of 4 u32s. Alignment 16, size 32.

Vec2

Vector of 2 f32s. Alignment 8, size 16.

Vec3

Vector of 3 f32s. Alignment 16, size 24.

Vec4

Vector of 4 f32s. Alignment 16, size 32.

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.

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.