1use crate::Version;
44use crate::error::MeshError;
45
46#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
48pub enum StructCode {
49 F32,
51 F16,
53 U8,
55 I8,
57 U16,
59 I16,
61 U32,
63 I32,
65}
66
67const _: () = crate::send_sync::<StructCode>();
68
69impl StructCode {
70 #[must_use]
72 pub const fn code(self) -> char {
73 match self {
74 Self::F32 => 'f',
75 Self::F16 => 'e',
76 Self::U8 => 'B',
77 Self::I8 => 'b',
78 Self::U16 => 'H',
79 Self::I16 => 'h',
80 Self::U32 => 'I',
81 Self::I32 => 'i',
82 }
83 }
84
85 #[must_use]
87 pub const fn size(self) -> u32 {
88 match self {
89 Self::U8 | Self::I8 => 1,
90 Self::F16 | Self::U16 | Self::I16 => 2,
91 Self::F32 | Self::U32 | Self::I32 => 4,
92 }
93 }
94
95 #[must_use]
97 pub const fn is_float(self) -> bool {
98 matches!(self, Self::F32 | Self::F16)
99 }
100}
101
102#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
105pub enum Norm {
106 UNorm,
108 SNorm,
110}
111
112const _: () = crate::send_sync::<Norm>();
113
114impl Norm {
115 #[must_use]
121 #[allow(clippy::cast_precision_loss)] pub fn value(self, code: StructCode, raw: i64) -> f64 {
123 let bits = code.size() * 8;
124 let v = raw as f64;
125 match self {
126 Self::UNorm => v / f64::from(u32::MAX >> (32 - bits)),
127 Self::SNorm => (v / f64::from(u32::MAX >> (33 - bits))).max(-1.0),
128 }
129 }
130}
131
132#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
135#[allow(non_camel_case_types)]
136pub enum VertexChannelFormat {
137 kChannelFormatFloat = 0,
139 kChannelFormatFloat16 = 1,
141 kChannelFormatColor = 2,
143 kChannelFormatByte = 3,
145 kChannelFormatUInt32 = 4,
147}
148
149const _: () = crate::send_sync::<VertexChannelFormat>();
150
151impl VertexChannelFormat {
152 pub const ALL: [Self; 5] = [
154 Self::kChannelFormatFloat,
155 Self::kChannelFormatFloat16,
156 Self::kChannelFormatColor,
157 Self::kChannelFormatByte,
158 Self::kChannelFormatUInt32,
159 ];
160
161 #[must_use]
163 pub fn from_value(value: u8) -> Option<Self> {
164 Self::ALL.into_iter().find(|m| *m as u8 == value)
165 }
166
167 #[must_use]
170 pub const fn struct_code(self) -> StructCode {
171 match self {
172 Self::kChannelFormatFloat => StructCode::F32,
173 Self::kChannelFormatFloat16 => StructCode::F16,
174 Self::kChannelFormatColor | Self::kChannelFormatByte => StructCode::U8,
175 Self::kChannelFormatUInt32 => StructCode::U32,
176 }
177 }
178
179 #[must_use]
182 pub const fn norm(self) -> Option<Norm> {
183 match self {
184 Self::kChannelFormatColor => Some(Norm::UNorm),
185 _ => None,
186 }
187 }
188}
189
190#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
193#[allow(non_camel_case_types)]
194pub enum VertexFormat2017 {
195 kVertexFormatFloat = 0,
197 kVertexFormatFloat16 = 1,
199 kVertexFormatColor = 2,
201 kVertexFormatUNorm8 = 3,
203 kVertexFormatSNorm8 = 4,
205 kVertexFormatUNorm16 = 5,
207 kVertexFormatSNorm16 = 6,
209 kVertexFormatUInt8 = 7,
211 kVertexFormatSInt8 = 8,
213 kVertexFormatUInt16 = 9,
215 kVertexFormatSInt16 = 10,
217 kVertexFormatUInt32 = 11,
219 kVertexFormatSInt32 = 12,
221}
222
223const _: () = crate::send_sync::<VertexFormat2017>();
224
225impl VertexFormat2017 {
226 pub const ALL: [Self; 13] = [
228 Self::kVertexFormatFloat,
229 Self::kVertexFormatFloat16,
230 Self::kVertexFormatColor,
231 Self::kVertexFormatUNorm8,
232 Self::kVertexFormatSNorm8,
233 Self::kVertexFormatUNorm16,
234 Self::kVertexFormatSNorm16,
235 Self::kVertexFormatUInt8,
236 Self::kVertexFormatSInt8,
237 Self::kVertexFormatUInt16,
238 Self::kVertexFormatSInt16,
239 Self::kVertexFormatUInt32,
240 Self::kVertexFormatSInt32,
241 ];
242
243 #[must_use]
245 pub fn from_value(value: u8) -> Option<Self> {
246 Self::ALL.into_iter().find(|m| *m as u8 == value)
247 }
248
249 #[must_use]
252 pub const fn struct_code(self) -> StructCode {
253 match self {
254 Self::kVertexFormatFloat => StructCode::F32,
255 Self::kVertexFormatFloat16 => StructCode::F16,
256 Self::kVertexFormatColor | Self::kVertexFormatUNorm8 | Self::kVertexFormatUInt8 => {
257 StructCode::U8
258 }
259 Self::kVertexFormatSNorm8 | Self::kVertexFormatSInt8 => StructCode::I8,
260 Self::kVertexFormatUNorm16 | Self::kVertexFormatUInt16 => StructCode::U16,
261 Self::kVertexFormatSNorm16 | Self::kVertexFormatSInt16 => StructCode::I16,
262 Self::kVertexFormatUInt32 => StructCode::U32,
263 Self::kVertexFormatSInt32 => StructCode::I32,
264 }
265 }
266
267 #[must_use]
270 pub const fn norm(self) -> Option<Norm> {
271 match self {
272 Self::kVertexFormatColor | Self::kVertexFormatUNorm8 | Self::kVertexFormatUNorm16 => {
273 Some(Norm::UNorm)
274 }
275 Self::kVertexFormatSNorm8 | Self::kVertexFormatSNorm16 => Some(Norm::SNorm),
276 _ => None,
277 }
278 }
279}
280
281#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
283#[allow(non_camel_case_types)]
284pub enum VertexFormat {
285 kVertexFormatFloat = 0,
287 kVertexFormatFloat16 = 1,
289 kVertexFormatUNorm8 = 2,
291 kVertexFormatSNorm8 = 3,
293 kVertexFormatUNorm16 = 4,
295 kVertexFormatSNorm16 = 5,
297 kVertexFormatUInt8 = 6,
299 kVertexFormatSInt8 = 7,
301 kVertexFormatUInt16 = 8,
303 kVertexFormatSInt16 = 9,
305 kVertexFormatUInt32 = 10,
307 kVertexFormatSInt32 = 11,
309}
310
311const _: () = crate::send_sync::<VertexFormat>();
312
313impl VertexFormat {
314 pub const ALL: [Self; 12] = [
316 Self::kVertexFormatFloat,
317 Self::kVertexFormatFloat16,
318 Self::kVertexFormatUNorm8,
319 Self::kVertexFormatSNorm8,
320 Self::kVertexFormatUNorm16,
321 Self::kVertexFormatSNorm16,
322 Self::kVertexFormatUInt8,
323 Self::kVertexFormatSInt8,
324 Self::kVertexFormatUInt16,
325 Self::kVertexFormatSInt16,
326 Self::kVertexFormatUInt32,
327 Self::kVertexFormatSInt32,
328 ];
329
330 #[must_use]
332 pub fn from_value(value: u8) -> Option<Self> {
333 Self::ALL.into_iter().find(|m| *m as u8 == value)
334 }
335
336 #[must_use]
339 pub const fn struct_code(self) -> StructCode {
340 match self {
341 Self::kVertexFormatFloat => StructCode::F32,
342 Self::kVertexFormatFloat16 => StructCode::F16,
343 Self::kVertexFormatUNorm8 | Self::kVertexFormatUInt8 => StructCode::U8,
344 Self::kVertexFormatSNorm8 | Self::kVertexFormatSInt8 => StructCode::I8,
345 Self::kVertexFormatUNorm16 | Self::kVertexFormatUInt16 => StructCode::U16,
346 Self::kVertexFormatSNorm16 | Self::kVertexFormatSInt16 => StructCode::I16,
347 Self::kVertexFormatUInt32 => StructCode::U32,
348 Self::kVertexFormatSInt32 => StructCode::I32,
349 }
350 }
351
352 #[must_use]
355 pub const fn norm(self) -> Option<Norm> {
356 match self {
357 Self::kVertexFormatUNorm8 | Self::kVertexFormatUNorm16 => Some(Norm::UNorm),
358 Self::kVertexFormatSNorm8 | Self::kVertexFormatSNorm16 => Some(Norm::SNorm),
359 _ => None,
360 }
361 }
362}
363
364#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
366pub enum FormatTable {
367 Channel,
369 V2017,
371 V2019,
373}
374
375const _: () = crate::send_sync::<FormatTable>();
376
377impl FormatTable {
378 #[must_use]
380 pub const fn for_major(major: u16) -> Self {
381 if major < 2017 {
382 Self::Channel
383 } else if major < 2019 {
384 Self::V2017
385 } else {
386 Self::V2019
387 }
388 }
389
390 #[must_use]
392 pub const fn python_name(self) -> &'static str {
393 match self {
394 Self::Channel => "VertexChannelFormat",
395 Self::V2017 => "VertexFormat2017",
396 Self::V2019 => "VertexFormat",
397 }
398 }
399
400 #[must_use]
403 pub fn norm(self, format: u8) -> Option<Norm> {
404 match self {
405 Self::Channel => {
406 VertexChannelFormat::from_value(format).and_then(VertexChannelFormat::norm)
407 }
408 Self::V2017 => VertexFormat2017::from_value(format).and_then(VertexFormat2017::norm),
409 Self::V2019 => VertexFormat::from_value(format).and_then(VertexFormat::norm),
410 }
411 }
412
413 #[must_use]
415 pub fn struct_code(self, format: u8) -> Option<StructCode> {
416 match self {
417 Self::Channel => {
418 VertexChannelFormat::from_value(format).map(VertexChannelFormat::struct_code)
419 }
420 Self::V2017 => VertexFormat2017::from_value(format).map(VertexFormat2017::struct_code),
421 Self::V2019 => VertexFormat::from_value(format).map(VertexFormat::struct_code),
422 }
423 }
424}
425
426pub fn get_channel_dtype(version: Version, format: u8) -> Result<StructCode, MeshError> {
430 let table = FormatTable::for_major(version.major()?);
431 table
432 .struct_code(format)
433 .ok_or(MeshError::InvalidFormat { format, table })
434}
435
436pub fn get_channel_component_size(version: Version, format: u8) -> Result<u32, MeshError> {
440 get_channel_dtype(version, format).map(StructCode::size)
441}
442
443#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
447pub enum MeshTopology {
448 Triangles = 0,
450 TriangleStrip = 1,
452 Quads = 2,
454 Lines = 3,
456 LineStrip = 4,
458 Points = 5,
460}
461
462const _: () = crate::send_sync::<MeshTopology>();
463
464impl MeshTopology {
465 pub const ALL: [Self; 6] = [
467 Self::Triangles,
468 Self::TriangleStrip,
469 Self::Quads,
470 Self::Lines,
471 Self::LineStrip,
472 Self::Points,
473 ];
474
475 #[must_use]
477 pub const fn value(self) -> i64 {
478 self as i64
479 }
480
481 #[must_use]
483 pub fn from_value(value: i64) -> Option<Self> {
484 Self::ALL.into_iter().find(|m| m.value() == value)
485 }
486}
487
488#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
491#[allow(non_camel_case_types)]
492pub enum GfxPrimitiveType {
493 kPrimitiveTriangles = 0,
495 kPrimitiveTriangleStrip = 1,
497 kPrimitiveQuads = 2,
499 kPrimitiveLines = 3,
501 kPrimitiveLineStrip = 4,
503 kPrimitivePoints = 5,
505}
506
507const _: () = crate::send_sync::<GfxPrimitiveType>();
508
509impl GfxPrimitiveType {
510 pub const ALL: [Self; 6] = [
512 Self::kPrimitiveTriangles,
513 Self::kPrimitiveTriangleStrip,
514 Self::kPrimitiveQuads,
515 Self::kPrimitiveLines,
516 Self::kPrimitiveLineStrip,
517 Self::kPrimitivePoints,
518 ];
519
520 #[must_use]
522 pub const fn value(self) -> i64 {
523 self as i64
524 }
525
526 #[must_use]
528 pub fn from_value(value: i64) -> Option<Self> {
529 Self::ALL.into_iter().find(|m| m.value() == value)
530 }
531}