glsl_layout/lib.rs
1#![allow(non_camel_case_types)]
2//!
3//! This crates provides data types to build structures ready to upload into UBO.
4//! Data layout will match one for uniform blocks declared with `layout(std140)`.
5//! See [specs](https://www.khronos.org/registry/OpenGL/specs/gl/glspec45.core.pdf#page=159) for alignment rules.
6//!
7//! # Examples
8//!
9//! ```rust
10//! # macro_rules! offset_of {
11//! # ($type:ty: $($name:ident).+) => {
12//! # unsafe { // No dereferencing
13//! # let value: &$type = &*::std::ptr::null();
14//! # let offset = &value $(.$name)+ as *const _ as usize;
15//! # let base = value as *const _ as usize;
16//! # offset - base
17//! # }
18//! # }
19//! # }
20//! #
21//! # #[macro_use]
22//! # extern crate glsl_layout;
23//! # use glsl_layout::*;
24//! # fn main() {
25//! # use std::mem::size_of;
26//! #
27//! #[derive(Debug, Default, Clone, Copy, Uniform)]
28//! struct Foo {
29//! x: int,
30//! y: vec3,
31//! z: float,
32//! w: mat4x4,
33//! a: [f32; 3],
34//! b: f32,
35//! }
36//!
37//! type UFoo = <Foo as Uniform>::Std140;
38//!
39//! assert_eq!(
40//! offset_of!(UFoo: y),
41//! round_up_to(size_of::<int>(), 16), // `vec3` has alignment of size `vec4`
42//! "Offset of field `y` must be equal of size of `x` rounded up to the alignment",
43//! );
44//!
45//! assert_eq!(
46//! offset_of!(UFoo: z),
47//! round_up_to(offset_of!(UFoo: y) + size_of::<vec3>(), 4),
48//! "Field `z` must follow `y`. `y` should not have padding at the end",
49//! );
50//!
51//! assert_eq!(
52//! offset_of!(UFoo: b),
53//! offset_of!(UFoo: a) + size_of::<[[f32; 4]; 3]>(),
54//! "Field `b` must follow `a`. But `a` has padding at the end.",
55//! );
56//! #
57//! let foo_uniform = Foo {
58//! x: 2,
59//! y: [0.0; 3].into(),
60//! z: 0.0,
61//! w: [[0.0; 4]; 4].into(),
62//! a: [0.0; 3].into(),
63//! b: 0.0,
64//! }.std140();
65//! # }
66//!
67//! # fn round_up_to(offset: usize, align: usize) -> usize {
68//! # if offset % align == 0 {
69//! # offset
70//! # } else {
71//! # (((offset - 1) / align) + 1) * align
72//! # }
73//! # }
74//! ```
75//!
76
77#[doc(hidden)]
78pub mod align;
79mod scalar;
80mod vec;
81
82#[macro_use]
83mod array;
84mod mat;
85mod uniform;
86
87#[cfg(feature = "cgmath")]
88mod cgmath;
89
90#[cfg(feature = "nalgebra")]
91mod nalgebra;
92
93#[cfg(feature = "glam")]
94mod glam;
95
96pub use array::*;
97pub use mat::*;
98pub use scalar::*;
99pub use uniform::*;
100pub use vec::*;
101
102#[allow(unused_imports)]
103#[macro_use]
104extern crate glsl_layout_derive;
105#[doc(hidden)]
106pub use glsl_layout_derive::*;
107
108#[test]
109fn test_derive() {
110 use crate as glsl_layout;
111 #[derive(Copy, Clone, Uniform)]
112 struct Test {
113 a: [u32; 3],
114 b: vec2,
115 c: dmat4x3,
116 }
117}
118
119#[test]
120fn test_array() {
121 use crate as glsl_layout;
122 #[derive(Copy, Clone, Uniform)]
123 struct Test {
124 a: [u32; 3],
125 b: vec2,
126 c: [dmat4x3; 32],
127 }
128}