use crate::Version;
use crate::error::MeshError;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StructCode {
F32,
F16,
U8,
I8,
U16,
I16,
U32,
I32,
}
const _: () = crate::send_sync::<StructCode>();
impl StructCode {
#[must_use]
pub const fn code(self) -> char {
match self {
Self::F32 => 'f',
Self::F16 => 'e',
Self::U8 => 'B',
Self::I8 => 'b',
Self::U16 => 'H',
Self::I16 => 'h',
Self::U32 => 'I',
Self::I32 => 'i',
}
}
#[must_use]
pub const fn size(self) -> u32 {
match self {
Self::U8 | Self::I8 => 1,
Self::F16 | Self::U16 | Self::I16 => 2,
Self::F32 | Self::U32 | Self::I32 => 4,
}
}
#[must_use]
pub const fn is_float(self) -> bool {
matches!(self, Self::F32 | Self::F16)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Norm {
UNorm,
SNorm,
}
const _: () = crate::send_sync::<Norm>();
impl Norm {
#[must_use]
#[allow(clippy::cast_precision_loss)] pub fn value(self, code: StructCode, raw: i64) -> f64 {
let bits = code.size() * 8;
let v = raw as f64;
match self {
Self::UNorm => v / f64::from(u32::MAX >> (32 - bits)),
Self::SNorm => (v / f64::from(u32::MAX >> (33 - bits))).max(-1.0),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
pub enum VertexChannelFormat {
kChannelFormatFloat = 0,
kChannelFormatFloat16 = 1,
kChannelFormatColor = 2,
kChannelFormatByte = 3,
kChannelFormatUInt32 = 4,
}
const _: () = crate::send_sync::<VertexChannelFormat>();
impl VertexChannelFormat {
pub const ALL: [Self; 5] = [
Self::kChannelFormatFloat,
Self::kChannelFormatFloat16,
Self::kChannelFormatColor,
Self::kChannelFormatByte,
Self::kChannelFormatUInt32,
];
#[must_use]
pub fn from_value(value: u8) -> Option<Self> {
Self::ALL.into_iter().find(|m| *m as u8 == value)
}
#[must_use]
pub const fn struct_code(self) -> StructCode {
match self {
Self::kChannelFormatFloat => StructCode::F32,
Self::kChannelFormatFloat16 => StructCode::F16,
Self::kChannelFormatColor | Self::kChannelFormatByte => StructCode::U8,
Self::kChannelFormatUInt32 => StructCode::U32,
}
}
#[must_use]
pub const fn norm(self) -> Option<Norm> {
match self {
Self::kChannelFormatColor => Some(Norm::UNorm),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
pub enum VertexFormat2017 {
kVertexFormatFloat = 0,
kVertexFormatFloat16 = 1,
kVertexFormatColor = 2,
kVertexFormatUNorm8 = 3,
kVertexFormatSNorm8 = 4,
kVertexFormatUNorm16 = 5,
kVertexFormatSNorm16 = 6,
kVertexFormatUInt8 = 7,
kVertexFormatSInt8 = 8,
kVertexFormatUInt16 = 9,
kVertexFormatSInt16 = 10,
kVertexFormatUInt32 = 11,
kVertexFormatSInt32 = 12,
}
const _: () = crate::send_sync::<VertexFormat2017>();
impl VertexFormat2017 {
pub const ALL: [Self; 13] = [
Self::kVertexFormatFloat,
Self::kVertexFormatFloat16,
Self::kVertexFormatColor,
Self::kVertexFormatUNorm8,
Self::kVertexFormatSNorm8,
Self::kVertexFormatUNorm16,
Self::kVertexFormatSNorm16,
Self::kVertexFormatUInt8,
Self::kVertexFormatSInt8,
Self::kVertexFormatUInt16,
Self::kVertexFormatSInt16,
Self::kVertexFormatUInt32,
Self::kVertexFormatSInt32,
];
#[must_use]
pub fn from_value(value: u8) -> Option<Self> {
Self::ALL.into_iter().find(|m| *m as u8 == value)
}
#[must_use]
pub const fn struct_code(self) -> StructCode {
match self {
Self::kVertexFormatFloat => StructCode::F32,
Self::kVertexFormatFloat16 => StructCode::F16,
Self::kVertexFormatColor | Self::kVertexFormatUNorm8 | Self::kVertexFormatUInt8 => {
StructCode::U8
}
Self::kVertexFormatSNorm8 | Self::kVertexFormatSInt8 => StructCode::I8,
Self::kVertexFormatUNorm16 | Self::kVertexFormatUInt16 => StructCode::U16,
Self::kVertexFormatSNorm16 | Self::kVertexFormatSInt16 => StructCode::I16,
Self::kVertexFormatUInt32 => StructCode::U32,
Self::kVertexFormatSInt32 => StructCode::I32,
}
}
#[must_use]
pub const fn norm(self) -> Option<Norm> {
match self {
Self::kVertexFormatColor | Self::kVertexFormatUNorm8 | Self::kVertexFormatUNorm16 => {
Some(Norm::UNorm)
}
Self::kVertexFormatSNorm8 | Self::kVertexFormatSNorm16 => Some(Norm::SNorm),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
pub enum VertexFormat {
kVertexFormatFloat = 0,
kVertexFormatFloat16 = 1,
kVertexFormatUNorm8 = 2,
kVertexFormatSNorm8 = 3,
kVertexFormatUNorm16 = 4,
kVertexFormatSNorm16 = 5,
kVertexFormatUInt8 = 6,
kVertexFormatSInt8 = 7,
kVertexFormatUInt16 = 8,
kVertexFormatSInt16 = 9,
kVertexFormatUInt32 = 10,
kVertexFormatSInt32 = 11,
}
const _: () = crate::send_sync::<VertexFormat>();
impl VertexFormat {
pub const ALL: [Self; 12] = [
Self::kVertexFormatFloat,
Self::kVertexFormatFloat16,
Self::kVertexFormatUNorm8,
Self::kVertexFormatSNorm8,
Self::kVertexFormatUNorm16,
Self::kVertexFormatSNorm16,
Self::kVertexFormatUInt8,
Self::kVertexFormatSInt8,
Self::kVertexFormatUInt16,
Self::kVertexFormatSInt16,
Self::kVertexFormatUInt32,
Self::kVertexFormatSInt32,
];
#[must_use]
pub fn from_value(value: u8) -> Option<Self> {
Self::ALL.into_iter().find(|m| *m as u8 == value)
}
#[must_use]
pub const fn struct_code(self) -> StructCode {
match self {
Self::kVertexFormatFloat => StructCode::F32,
Self::kVertexFormatFloat16 => StructCode::F16,
Self::kVertexFormatUNorm8 | Self::kVertexFormatUInt8 => StructCode::U8,
Self::kVertexFormatSNorm8 | Self::kVertexFormatSInt8 => StructCode::I8,
Self::kVertexFormatUNorm16 | Self::kVertexFormatUInt16 => StructCode::U16,
Self::kVertexFormatSNorm16 | Self::kVertexFormatSInt16 => StructCode::I16,
Self::kVertexFormatUInt32 => StructCode::U32,
Self::kVertexFormatSInt32 => StructCode::I32,
}
}
#[must_use]
pub const fn norm(self) -> Option<Norm> {
match self {
Self::kVertexFormatUNorm8 | Self::kVertexFormatUNorm16 => Some(Norm::UNorm),
Self::kVertexFormatSNorm8 | Self::kVertexFormatSNorm16 => Some(Norm::SNorm),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FormatTable {
Channel,
V2017,
V2019,
}
const _: () = crate::send_sync::<FormatTable>();
impl FormatTable {
#[must_use]
pub const fn for_major(major: u16) -> Self {
if major < 2017 {
Self::Channel
} else if major < 2019 {
Self::V2017
} else {
Self::V2019
}
}
#[must_use]
pub const fn python_name(self) -> &'static str {
match self {
Self::Channel => "VertexChannelFormat",
Self::V2017 => "VertexFormat2017",
Self::V2019 => "VertexFormat",
}
}
#[must_use]
pub fn norm(self, format: u8) -> Option<Norm> {
match self {
Self::Channel => {
VertexChannelFormat::from_value(format).and_then(VertexChannelFormat::norm)
}
Self::V2017 => VertexFormat2017::from_value(format).and_then(VertexFormat2017::norm),
Self::V2019 => VertexFormat::from_value(format).and_then(VertexFormat::norm),
}
}
#[must_use]
pub fn struct_code(self, format: u8) -> Option<StructCode> {
match self {
Self::Channel => {
VertexChannelFormat::from_value(format).map(VertexChannelFormat::struct_code)
}
Self::V2017 => VertexFormat2017::from_value(format).map(VertexFormat2017::struct_code),
Self::V2019 => VertexFormat::from_value(format).map(VertexFormat::struct_code),
}
}
}
pub fn get_channel_dtype(version: Version, format: u8) -> Result<StructCode, MeshError> {
let table = FormatTable::for_major(version.major()?);
table
.struct_code(format)
.ok_or(MeshError::InvalidFormat { format, table })
}
pub fn get_channel_component_size(version: Version, format: u8) -> Result<u32, MeshError> {
get_channel_dtype(version, format).map(StructCode::size)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MeshTopology {
Triangles = 0,
TriangleStrip = 1,
Quads = 2,
Lines = 3,
LineStrip = 4,
Points = 5,
}
const _: () = crate::send_sync::<MeshTopology>();
impl MeshTopology {
pub const ALL: [Self; 6] = [
Self::Triangles,
Self::TriangleStrip,
Self::Quads,
Self::Lines,
Self::LineStrip,
Self::Points,
];
#[must_use]
pub const fn value(self) -> i64 {
self as i64
}
#[must_use]
pub fn from_value(value: i64) -> Option<Self> {
Self::ALL.into_iter().find(|m| m.value() == value)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
pub enum GfxPrimitiveType {
kPrimitiveTriangles = 0,
kPrimitiveTriangleStrip = 1,
kPrimitiveQuads = 2,
kPrimitiveLines = 3,
kPrimitiveLineStrip = 4,
kPrimitivePoints = 5,
}
const _: () = crate::send_sync::<GfxPrimitiveType>();
impl GfxPrimitiveType {
pub const ALL: [Self; 6] = [
Self::kPrimitiveTriangles,
Self::kPrimitiveTriangleStrip,
Self::kPrimitiveQuads,
Self::kPrimitiveLines,
Self::kPrimitiveLineStrip,
Self::kPrimitivePoints,
];
#[must_use]
pub const fn value(self) -> i64 {
self as i64
}
#[must_use]
pub fn from_value(value: i64) -> Option<Self> {
Self::ALL.into_iter().find(|m| m.value() == value)
}
}