Skip to main content

shade/
common.rs

1use std::{fmt, slice};
2
3define_handle!(VertexBuffer);
4define_handle!(IndexBuffer);
5
6/// Index type for index buffers.
7#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
8pub enum IndexType { U8, U16, U32 }
9
10impl IndexType {
11	#[inline]
12	pub const fn size(self) -> usize {
13		match self {
14			IndexType::U8 => 1,
15			IndexType::U16 => 2,
16			IndexType::U32 => 4,
17		}
18	}
19}
20
21/// Trait for index types.
22pub trait TIndex: Copy + Ord + Default + dataview::Pod + fmt::Debug {
23	const TYPE: IndexType;
24}
25
26impl TIndex for u8 {
27	const TYPE: IndexType = IndexType::U8;
28}
29impl TIndex for u16 {
30	const TYPE: IndexType = IndexType::U16;
31}
32impl TIndex for u32 {
33	const TYPE: IndexType = IndexType::U32;
34}
35
36/// Trait for index collections.
37pub trait TIndices {
38	type Index: TIndex;
39
40	fn as_indices(&self) -> &[Self::Index];
41}
42impl<T: TIndex> TIndices for [T] {
43	type Index = T;
44
45	#[inline]
46	fn as_indices(&self) -> &[Self::Index] {
47		self
48	}
49}
50impl<T: TIndex, const N: usize> TIndices for [T; N] {
51	type Index = T;
52
53	#[inline]
54	fn as_indices(&self) -> &[Self::Index] {
55		self.as_slice()
56	}
57}
58impl<T: TIndex> TIndices for Vec<T> {
59	type Index = T;
60
61	#[inline]
62	fn as_indices(&self) -> &[Self::Index] {
63		self.as_slice()
64	}
65}
66
67/// Triangle indices.
68#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
69#[repr(C)]
70pub struct Index3 {
71	pub p1: u32,
72	pub p2: u32,
73	pub p3: u32,
74}
75unsafe impl dataview::Pod for Index3 {}
76impl TIndices for Vec<Index3> {
77	type Index = u32;
78
79	#[inline]
80	fn as_indices(&self) -> &[Self::Index] {
81		unsafe { slice::from_raw_parts(self.as_ptr() as *const u32, self.len() * 3) }
82	}
83}
84
85/// Primitive type.
86#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
87pub enum PrimType {
88	/// Triangles.
89	Triangles,
90	/// Lines.
91	Lines,
92}
93
94/// Blend mode.
95#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
96#[non_exhaustive]
97pub enum BlendMode {
98	/// Solid color.
99	///
100	/// ```text
101	/// result[rgb] = src[rgb]
102	/// result[a] = src[a]
103	/// ```
104	#[default]
105	Solid,
106
107	/// Alpha blending.
108	///
109	/// ```text
110	/// result[rgb] = src[rgb] * src[a] + dest[rgb] * (1 - src[a])
111	/// result[a] = src[a] + dest[a] * (1 - src[a])
112	/// ```
113	Alpha,
114
115	/// Premultiplied alpha blending.
116	///
117	/// ```text
118	/// result[rgb] = src[rgb] + dest[rgb] * (1 - src[a])
119	/// result[a] = src[a] + dest[a] * (1 - src[a])
120	/// ```
121	PremultipliedAlpha,
122
123	/// Additive blending.
124	///
125	/// ```text
126	/// result[rgb] = src[rgb] + dest[rgb]
127	/// ```
128	Additive,
129
130	/// Maximum of the src and dest colors.
131	///
132	/// ```text
133	/// result[rgb] = max(src[rgb], dest[rgb])
134	/// ```
135	Lighten,
136
137	/// Screen effect.
138	///
139	/// ```text
140	/// result[rgb] = src[rgb] + (1 - dest[rgb])
141	/// ```
142	Screen,
143
144	/// Minimum of the src and dest colors.
145	///
146	/// ```text
147	/// result[rgb] = min(src[rgb], dest[rgb])
148	/// ```
149	Darken,
150
151	/// Multiply blending.
152	///
153	/// ```text
154	/// result[rgb] = src[rgb] * dest[rgb]
155	/// ```
156	Multiply,
157}
158
159/// Comparison operator.
160#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
161pub enum Compare {
162	/// Never pass.
163	Never,
164	/// Pass if the new value is less than the old value.
165	Less,
166	/// Pass if the new value is equal to the old value.
167	Equal,
168	/// Pass if the new value is not equal to the old value.
169	NotEqual,
170	/// Pass if the new value is less than or equal to the old value.
171	LessEqual,
172	/// Pass if the new value is greater than the old value.
173	Greater,
174	/// Pass if the new value is greater than or equal to the old value.
175	GreaterEqual,
176	/// Always pass.
177	Always,
178}
179
180/// Cull mode.
181#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
182pub enum CullMode {
183	/// Cull counter-clockwise faces.
184	CCW,
185	/// Cull clockwise faces.
186	CW,
187}
188
189/// Buffer usage.
190#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
191pub enum BufferUsage {
192	/// Buffer is static and used frequently.
193	Static,
194	/// Buffer is dynamic and used frequently.
195	Dynamic,
196	/// Buffer is streamed and used infrequently.
197	Stream,
198}