1use crate::Vec2;
4use std::{error::Error, fmt};
5
6#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum TextureAddressMode3d {
10 #[default]
12 Clamp,
13 Repeat,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq)]
41pub struct TextureUvTransform3d {
42 scale: Vec2,
43 offset: Vec2,
44}
45
46impl TextureUvTransform3d {
47 pub const IDENTITY: Self = Self {
49 scale: Vec2::new(1.0, 1.0),
50 offset: Vec2::ZERO,
51 };
52 pub const MAX_MAGNITUDE: f32 = 65_536.0;
54
55 pub fn new(scale: Vec2, offset: Vec2) -> Result<Self, TextureUvTransformError> {
57 for (scale, offset) in [(scale.x(), offset.x()), (scale.y(), offset.y())] {
58 for value in [scale, offset] {
59 if !value.is_finite() {
60 return Err(TextureUvTransformError::NonFinite);
61 }
62 if value != 0.0 && !value.is_normal() {
63 return Err(TextureUvTransformError::Subnormal);
64 }
65 if value.abs() > Self::MAX_MAGNITUDE {
66 return Err(TextureUvTransformError::OutOfRange);
67 }
68 }
69 if (f64::from(scale) + f64::from(offset)).abs() > f64::from(Self::MAX_MAGNITUDE) {
70 return Err(TextureUvTransformError::OutOfRange);
71 }
72 }
73 Ok(Self { scale, offset })
74 }
75 pub const fn scale(self) -> Vec2 {
77 self.scale
78 }
79 pub const fn offset(self) -> Vec2 {
81 self.offset
82 }
83}
84impl Default for TextureUvTransform3d {
85 fn default() -> Self {
86 Self::IDENTITY
87 }
88}
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92#[non_exhaustive]
93pub enum TextureUvTransformError {
94 NonFinite,
96 Subnormal,
98 OutOfRange,
100}
101impl fmt::Display for TextureUvTransformError {
102 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
103 formatter.write_str(match self {
104 Self::NonFinite => "3D UV scale and offset must be finite",
105 Self::Subnormal => "3D UV coefficients must be zero or normal finite values",
106 Self::OutOfRange => "3D UV coefficients and endpoints must remain within +/-65536",
107 })
108 }
109}
110impl Error for TextureUvTransformError {}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115 #[test]
116 fn affine_uv_bounds_cover_negative_zero_and_extreme_coefficients() {
117 assert!(TextureUvTransform3d::new(Vec2::new(-8.0, 0.0), Vec2::new(3.0, -2.0)).is_ok());
118 for invalid in [f32::MAX, f32::INFINITY, f32::NAN, f32::from_bits(1)] {
119 assert!(TextureUvTransform3d::new(Vec2::new(invalid, 1.0), Vec2::ZERO).is_err());
120 }
121 assert!(TextureUvTransform3d::new(Vec2::new(65536.0, 1.0), Vec2::new(1.0, 0.0)).is_err());
122 assert!(
123 TextureUvTransform3d::new(Vec2::new(65536.0, 1.0), Vec2::new(-65536.0, 0.0)).is_ok()
124 );
125 assert_eq!(
126 TextureUvTransform3d::default(),
127 TextureUvTransform3d::IDENTITY
128 );
129 }
130}