[][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 implement Zeroable and Pod.
  • 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 Bool values. Has size 8 and alignment 8.

BVec3

Vector of 3 Bool values. Has size 12 and alignment 16.

BVec4

Vector of 4 Bool values. Has size 16 and alignment 16.

Bool

A boolean value. Has size 4 and alignment 4.

DMat2x2

Matrix of f64 values with 2 rows and 2 columns. Has size 32 and alignment 16.

DMat2x3

Matrix of f64 values with 2 rows and 3 columns. Has size 48 and alignment 16.

DMat2x4

Matrix of f64 values with 2 rows and 4 columns. Has size 64 and alignment 16.

DMat3x2

Matrix of f64 values with 3 rows and 2 columns. Has size 64 and alignment 32.

DMat3x3

Matrix of f64 values with 3 rows and 3 columns. Has size 96 and alignment 32.

DMat3x4

Matrix of f64 values with 3 rows and 4 columns. Has size 128 and alignment 32.

DMat4x2

Matrix of f64 values with 4 rows and 2 columns. Has size 64 and alignment 32.

DMat4x3

Matrix of f64 values with 4 rows and 3 columns. Has size 96 and alignment 32.

DMat4x4

Matrix of f64 values with 4 rows and 4 columns. Has size 128 and alignment 32.

DVec2

Vector of 2 f64 values. Has size 16 and alignment 16.

DVec3

Vector of 3 f64 values. Has size 24 and alignment 32.

DVec4

Vector of 4 f64 values. Has size 32 and alignment 32.

DynamicOffsetMember

Pads a structure for use with dynamic offsets in graphics apis.

IVec2

Vector of 2 i32 values. Has size 8 and alignment 8.

IVec3

Vector of 3 i32 values. Has size 12 and alignment 16.

IVec4

Vector of 4 i32 values. Has size 16 and alignment 16.

Mat2x2

Matrix of f32 values with 2 rows and 2 columns. Has size 16 and alignment 8.

Mat2x3

Matrix of f32 values with 2 rows and 3 columns. Has size 32 and alignment 8.

Mat2x4

Matrix of f32 values with 2 rows and 4 columns. Has size 32 and alignment 8.

Mat3x2

Matrix of f32 values with 3 rows and 2 columns. Has size 32 and alignment 16.

Mat3x3

Matrix of f32 values with 3 rows and 3 columns. Has size 48 and alignment 16.

Mat3x4

Matrix of f32 values with 3 rows and 4 columns. Has size 64 and alignment 16.

Mat4x2

Matrix of f32 values with 4 rows and 2 columns. Has size 32 and alignment 16.

Mat4x3

Matrix of f32 values with 4 rows and 3 columns. Has size 48 and alignment 16.

Mat4x4

Matrix of f32 values with 4 rows and 4 columns. Has size 64 and alignment 16.

UVec2

Vector of 2 u32 values. Has size 8 and alignment 8.

UVec3

Vector of 3 u32 values. Has size 12 and alignment 16.

UVec4

Vector of 4 u32 values. Has size 16 and alignment 16.

Vec2

Vector of 2 f32 values. Has size 8 and alignment 8.

Vec3

Vector of 3 f32 values. Has size 12 and alignment 16.

Vec4

Vector of 4 f32 values. Has size 16 and alignment 16.

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.