storm 0.11.0

A personal 2D game engine designed for performance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
mod cgmath;
mod core;

/// Macro which configures a structure to act as a std140 uniform for use in shaders.
///
/// # Example
/// ```
/// // The macro relies on the `std140` module being available on the path.
/// use storm::graphics::std140;
///
/// #[std140::uniform]
/// #[derive(Copy, Clone)]
/// pub struct SpriteUniform {
///     pub ortho: std140::mat4,
/// }
/// ```
pub use storm_macro::uniform;

/// Marker trait for element types supported by the `#[std140::uniform]` macro. These types have
/// specific safety, padding, and alignment requirements.
pub unsafe trait Std140Element: Copy {}

/// Marker trait for structs supported by the `#[std140::uniform]` macro. These types have specific
/// safety, padding, and alignment requirements.
pub unsafe trait Std140Struct: Copy {}

// float ========================================

/// A 32-bit floating point value.
#[repr(C, align(4))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct float {
    /// The first value.
    pub x: f32,
}

unsafe impl Std140Element for float {}
impl float {
    /// Creates a new [float] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0.0)
    }

    /// Creates a new [float] with the given value in all positions.
    pub const fn fill(v: f32) -> Self {
        Self {
            x: v,
        }
    }
}

/// A column vector of 2 float values.
#[repr(C, align(8))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct vec2 {
    /// The first value.
    pub x: f32,
    /// The second value.
    pub y: f32,
}

unsafe impl Std140Element for vec2 {}
impl vec2 {
    /// Creates a new [vec2] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0.0)
    }

    /// Creates a new [vec2] with the given value in all positions.
    pub const fn fill(v: f32) -> Self {
        Self {
            x: v,
            y: v,
        }
    }
}

/// A column vector of 3 float values.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct vec3 {
    /// The first value.
    pub x: f32,
    /// The second value.
    pub y: f32,
    /// The third value.
    pub z: f32,
}

unsafe impl Std140Element for vec3 {}
unsafe impl Std140Struct for vec3 {}
impl vec3 {
    /// Creates a new [vec3] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0.0)
    }

    /// Creates a new [vec3] with the given value in all positions.
    pub const fn fill(v: f32) -> Self {
        Self {
            x: v,
            y: v,
            z: v,
        }
    }
}

/// A column vector of 4 float values.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct vec4 {
    /// The first value.
    pub x: f32,
    /// The second value.
    pub y: f32,
    /// The third value.
    pub z: f32,
    /// The fourth value.
    pub w: f32,
}

unsafe impl Std140Element for vec4 {}
unsafe impl Std140Struct for vec4 {}
impl vec4 {
    /// Creates a new [vec4] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0.0)
    }

    /// Creates a new [vec4] with the given value in all positions.
    pub const fn fill(v: f32) -> Self {
        Self {
            x: v,
            y: v,
            z: v,
            w: v,
        }
    }
}

// int ==========================================

/// A 32-bit signed integer value.
#[repr(C, align(4))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct int {
    /// The first value.
    pub x: i32,
}

unsafe impl Std140Element for int {}
impl int {
    /// Creates a new [int] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0)
    }

    /// Creates a new [int] with the given value in all positions.
    pub const fn fill(v: i32) -> Self {
        Self {
            x: v,
        }
    }
}

/// A column vector of 2 int values.
#[repr(C, align(8))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct ivec2 {
    /// The first value.
    pub x: i32,
    /// The second value.
    pub y: i32,
}

unsafe impl Std140Element for ivec2 {}
impl ivec2 {
    /// Creates a new [ivec2] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0)
    }

    /// Creates a new [ivec2] with the given value in all positions.
    pub const fn fill(v: i32) -> Self {
        Self {
            x: v,
            y: v,
        }
    }
}

/// A column vector of 3 int values.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct ivec3 {
    /// The first value.
    pub x: i32,
    /// The second value.
    pub y: i32,
    /// The third value.
    pub z: i32,
}

unsafe impl Std140Element for ivec3 {}
unsafe impl Std140Struct for ivec3 {}
impl ivec3 {
    /// Creates a new [ivec3] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0)
    }

    /// Creates a new [ivec3] with the given value in all positions.
    pub const fn fill(v: i32) -> Self {
        Self {
            x: v,
            y: v,
            z: v,
        }
    }
}

/// A column vector of 4 int values.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct ivec4 {
    /// The first value.
    pub x: i32,
    /// The second value.
    pub y: i32,
    /// The third value.
    pub z: i32,
    /// The fourth value.
    pub w: i32,
}

unsafe impl Std140Element for ivec4 {}
unsafe impl Std140Struct for ivec4 {}
impl ivec4 {
    /// Creates a new [ivec4] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0)
    }

    /// Creates a new [ivec4] with the given value in all positions.
    pub const fn fill(v: i32) -> Self {
        Self {
            x: v,
            y: v,
            z: v,
            w: v,
        }
    }
}

// uint =========================================

/// A 32-bit unsigned integer value.
#[repr(C, align(4))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct uint {
    /// The first value.
    pub x: u32,
}

unsafe impl Std140Element for uint {}
impl uint {
    /// Creates a new [uint] with zeros in all positions.
    pub const fn zero() -> Self {
        Self {
            x: 0,
        }
    }

    /// Creates a new [uint] with the given value in all positions.
    pub const fn fill(v: u32) -> Self {
        Self {
            x: v,
        }
    }
}

/// A column vector of 2 uint values.
#[repr(C, align(8))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct uvec2 {
    /// The first value.
    pub x: u32,
    /// The second value.
    pub y: u32,
}

unsafe impl Std140Element for uvec2 {}
impl uvec2 {
    /// Creates a new [uvec2] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0)
    }

    /// Creates a new [uvec2] with the given value in all positions.
    pub const fn fill(v: u32) -> Self {
        Self {
            x: v,
            y: v,
        }
    }
}

/// A column vector of 3 uint values.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct uvec3 {
    /// The first value.
    pub x: u32,
    /// The second value.
    pub y: u32,
    /// The third value.
    pub z: u32,
}

unsafe impl Std140Element for uvec3 {}
unsafe impl Std140Struct for uvec3 {}
impl uvec3 {
    /// Creates a new [uvec3] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0)
    }

    /// Creates a new [uvec3] with the given value in all positions.
    pub const fn fill(v: u32) -> Self {
        Self {
            x: v,
            y: v,
            z: v,
        }
    }
}

/// A column vector of 4 uint values.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct uvec4 {
    /// The first value.
    pub x: u32,
    /// The second value.
    pub y: u32,
    /// The third value.
    pub z: u32,
    /// The fourth value.
    pub w: u32,
}

unsafe impl Std140Element for uvec4 {}
unsafe impl Std140Struct for uvec4 {}
impl uvec4 {
    /// Creates a new [uvec4] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0)
    }

    /// Creates a new [uvec4] with the given value in all positions.
    pub const fn fill(v: u32) -> Self {
        Self {
            x: v,
            y: v,
            z: v,
            w: v,
        }
    }
}

// boolean ======================================

/// A 32-bit boolean value.
#[repr(u32)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum boolean {
    /// Represents true.
    True,
    /// Represents false.
    False,
}

/// A column vector of 2 boolean values.
#[repr(C, align(8))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct bvec2 {
    /// The first value.
    pub x: boolean,
    /// The second value.
    pub y: boolean,
}

unsafe impl Std140Element for bvec2 {}
impl bvec2 {
    /// Creates a new [bvec2] with false in all positions.
    pub const fn falsey() -> Self {
        Self::fill(boolean::False)
    }

    /// Creates a new [bvec2] with true in all positions.
    pub const fn truthy() -> Self {
        Self::fill(boolean::True)
    }

    /// Creates a new [bvec2] with the given value in all positions.
    pub const fn fill(v: boolean) -> Self {
        Self {
            x: v,
            y: v,
        }
    }
}

/// A column vector of 3 boolean values.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct bvec3 {
    /// The first value.
    pub x: boolean,
    /// The second value.
    pub y: boolean,
    /// The third value.
    pub z: boolean,
}

unsafe impl Std140Element for bvec3 {}
unsafe impl Std140Struct for bvec3 {}
impl bvec3 {
    /// Creates a new [bvec3] with false in all positions.
    pub const fn falsey() -> Self {
        Self::fill(boolean::False)
    }

    /// Creates a new [bvec3] with true in all positions.
    pub const fn truthy() -> Self {
        Self::fill(boolean::True)
    }

    /// Creates a new [bvec3] with the given value in all positions.
    pub const fn fill(v: boolean) -> Self {
        Self {
            x: v,
            y: v,
            z: v,
        }
    }
}

/// A column vector of 4 boolean values.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct bvec4 {
    /// The first value.
    pub x: boolean,
    /// The second value.
    pub y: boolean,
    /// The third value.
    pub z: boolean,
    /// The fourth value.
    pub w: boolean,
}

unsafe impl Std140Element for bvec4 {}
unsafe impl Std140Struct for bvec4 {}
impl bvec4 {
    /// Creates a new [bvec4] with false in all positions.
    pub const fn falsey() -> Self {
        Self::fill(boolean::False)
    }

    /// Creates a new [bvec4] with true in all positions.
    pub const fn truthy() -> Self {
        Self::fill(boolean::True)
    }

    /// Creates a new [bvec4] with the given value in all positions.
    pub const fn fill(v: boolean) -> Self {
        Self {
            x: v,
            y: v,
            z: v,
            w: v,
        }
    }
}

// matn =========================================

/// A matrix with 2 columns and up to 2 rows, represented by 2 vec4 vectors.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct mat2 {
    /// The first value.
    pub x: vec4,
    /// The second value.
    pub y: vec4,
}

unsafe impl Std140Element for mat2 {}
unsafe impl Std140Struct for mat2 {}
impl mat2 {
    /// Creates a new [mat2] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0.0)
    }

    /// Creates a new [mat2] with the given value in all positions.
    pub const fn fill(v: f32) -> Self {
        Self {
            x: vec4::fill(v),
            y: vec4::fill(v),
        }
    }
}

/// A matrix with 3 columns and up to 3 rows, represented by 3 vec4 vectors.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct mat3 {
    /// The first value.
    pub x: vec4,
    /// The second value.
    pub y: vec4,
    /// The third value.
    pub z: vec4,
}

unsafe impl Std140Element for mat3 {}
unsafe impl Std140Struct for mat3 {}
impl mat3 {
    /// Creates a new [mat3] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0.0)
    }

    /// Creates a new [mat3] with the given value in all positions.
    pub const fn fill(v: f32) -> Self {
        Self {
            x: vec4::fill(v),
            y: vec4::fill(v),
            z: vec4::fill(v),
        }
    }
}

/// A matrix with 4 columns and up to 4 rows, represented by 4 vec4 vectors.
#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct mat4 {
    /// The first value.
    pub x: vec4,
    /// The second value.
    pub y: vec4,
    /// The third value.
    pub z: vec4,
    /// The fourth value.
    pub w: vec4,
}

unsafe impl Std140Element for mat4 {}
unsafe impl Std140Struct for mat4 {}
unsafe impl<const N: usize> Std140Element for [mat4; N] {}
unsafe impl<const N: usize> Std140Struct for [mat4; N] {}
impl mat4 {
    /// Creates a new [mat4] with zeros in all positions.
    pub const fn zero() -> Self {
        Self::fill(0.0)
    }

    /// Creates a new [mat4] with the given value in all positions.
    pub const fn fill(v: f32) -> Self {
        Self {
            x: vec4::fill(v),
            y: vec4::fill(v),
            z: vec4::fill(v),
            w: vec4::fill(v),
        }
    }
}