aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/macros.rs
blob: 9096958d9ccf70db84f54a87b324948e8caa3759 (plain)
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
/*
 * Copyright (C) Oscar Shrimpton 2020
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

//! Helper macros, mostly for the graphics pipeline definitions

/// Macro for easily defining buffer attribute descriptions
/// Usage:
/// ```
/// // 0 is the binding value
/// let attributes: Vec<AttributeDesc> = pipeline_vb_attributes!(0,
/// size_of::<f32>() * 3; Rgb32Sfloat
///     size_of::<f32>() * 2; Rg32Sfloat,
///     size_of::<u32>(); R32Sint
/// );
/// ```
/// See the hal::pso::Format enum for possible types
macro_rules! pipeline_vb_attributes {
	// Start of recursion
	( $binding:expr,
		$firstSize:expr; $firstType:ident,
		$( $size:expr; $type:ident ),*
	) => ({
		use hal::pso::{AttributeDesc, Element};

		let mut vec = Vec::new();

		vec.push(AttributeDesc {
			location: 0,
			binding: $binding,
			element: Element {
				format: Format::$firstType,
				offset: 0
			}
		});

		pipeline_vb_attributes!(
			vec; $binding; 1; $firstSize,
			$($size; $type),*
		);

		vec
	});

	// Special case for single item
	( $binding:expr; $firstSize:expr; $firstType:ident ) => ({
		vec![
			AttributeDesc {
				location: 0,
				binding: $binding,
				element: Element {
					format: Format::$firstType,
					offset: $firstSize as u32
				}
			}
		]
	});

	// Middle of recursion
	( $vec:ident; $binding:expr; $location:expr; $prevSize:expr,
		$firstSize:expr; $firstType:ident,
		$($size:expr; $type:ident),* ) => ({

		$vec.push(AttributeDesc {
			location: $location,
			binding: $binding,
			element: Element {
				format: Format::$firstType,
				offset: $prevSize as u32
			}
		});

		pipeline_vb_attributes!(
			$vec; $binding; ($location + 1); ($prevSize + $firstSize),
			$($size; $type),*
		);
	});

	// End of recursion
	( $vec:ident; $binding:expr; $location:expr; $prevSize:expr,
		$firstSize:expr; $firstType:ident ) => ({
			$vec.push(AttributeDesc {
				location: $location,
				binding: $binding,
				element: Element {
					format: Format::$firstType,
					offset: $prevSize as u32
				}
			});
		});
}