| 1 | #[cfg(any( |
| 2 | all(debug_assertions, feature = "debug-glam-assert"), |
| 3 | feature = "glam-assert" |
| 4 | ))] |
| 5 | macro_rules! glam_assert { |
| 6 | ($($arg:tt)*) => ( assert!($($arg)*); ) |
| 7 | } |
| 8 | #[cfg(not(any( |
| 9 | all(debug_assertions, feature = "debug-glam-assert"), |
| 10 | feature = "glam-assert" |
| 11 | )))] |
| 12 | macro_rules! glam_assert { |
| 13 | ($($arg:tt)*) => {}; |
| 14 | } |
| 15 | |
| 16 | macro_rules! const_assert { |
| 17 | ($x:expr $(,)?) => { |
| 18 | // FIXME: everything is align 16 on spirv - ignore for now |
| 19 | #[cfg(not(target_arch = "spirv"))] |
| 20 | #[allow(unknown_lints, clippy::eq_op)] |
| 21 | const _: [(); 0 - !{ |
| 22 | const ASSERT: bool = $x; |
| 23 | ASSERT |
| 24 | } as usize] = []; |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | macro_rules! const_assert_eq { |
| 29 | ($x:expr, $y:expr $(,)?) => { |
| 30 | const_assert!($x == $y); |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | #[macro_export] |
| 35 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 36 | macro_rules! const_m128 { |
| 37 | ($fx4:expr) => { |
| 38 | unsafe { $crate::cast::Vec4Cast { fx4: $fx4 }.m128 } |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | /// Creates a `Vec2` that can be used to initialize a constant value. |
| 43 | /// |
| 44 | /// ``` |
| 45 | /// use glam::{const_vec2, Vec2}; |
| 46 | /// const ONE: Vec2 = const_vec2!([1.0; 2]); |
| 47 | /// const X: Vec2 = const_vec2!([1.0, 0.0]); |
| 48 | /// ``` |
| 49 | #[macro_export] |
| 50 | macro_rules! const_vec2 { |
| 51 | ($fx2:expr) => { |
| 52 | unsafe { $crate::cast::Vec2Cast { fx2: $fx2 }.v2 } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | /// Creates a `Vec3` that can be used to initialize a constant value. |
| 57 | /// |
| 58 | /// ``` |
| 59 | /// use glam::{const_vec3, Vec3}; |
| 60 | /// const ONE: Vec3 = const_vec3!([1.0; 3]); |
| 61 | /// const X: Vec3 = const_vec3!([1.0, 0.0, 0.0]); |
| 62 | /// ``` |
| 63 | #[macro_export] |
| 64 | macro_rules! const_vec3 { |
| 65 | ($fx3:expr) => { |
| 66 | unsafe { $crate::cast::Vec3Cast { fx3: $fx3 }.v3 } |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | /// Creates a `Vec3A` that can be used to initialize a constant value. |
| 71 | /// |
| 72 | /// ``` |
| 73 | /// use glam::{const_vec3a, Vec3A}; |
| 74 | /// const ONE: Vec3A = const_vec3a!([1.0; 3]); |
| 75 | /// const X: Vec3A = const_vec3a!([1.0, 0.0, 0.0]); |
| 76 | /// ``` |
| 77 | #[macro_export] |
| 78 | macro_rules! const_vec3a { |
| 79 | ($fx3:expr) => { |
| 80 | unsafe { |
| 81 | $crate::cast::Vec4Cast { |
| 82 | fx4: [$fx3[0], $fx3[1], $fx3[2], 0.0], |
| 83 | } |
| 84 | .v3a |
| 85 | } |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | /// Creates a `Vec4` that can be used to initialize a constant value. |
| 90 | /// |
| 91 | /// ``` |
| 92 | /// use glam::{const_vec4, Vec4}; |
| 93 | /// const ONE: Vec4 = const_vec4!([1.0; 4]); |
| 94 | /// const X: Vec4 = const_vec4!([1.0, 0.0, 0.0, 0.0]); |
| 95 | /// ``` |
| 96 | #[macro_export] |
| 97 | macro_rules! const_vec4 { |
| 98 | ($fx4:expr) => { |
| 99 | unsafe { $crate::cast::Vec4Cast { fx4: $fx4 }.v4 } |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | /// Creates a `Mat2` from two column vectors that can be used to initialize a constant value. |
| 104 | /// |
| 105 | /// ``` |
| 106 | /// use glam::{const_mat2, Mat2}; |
| 107 | /// const ZERO: Mat2 = const_mat2!([0.0; 4]); |
| 108 | /// const IDENTITY: Mat2 = const_mat2!([1.0, 0.0], [0.0, 1.0]); |
| 109 | /// ``` |
| 110 | #[macro_export] |
| 111 | macro_rules! const_mat2 { |
| 112 | ($col0:expr, $col1:expr) => { |
| 113 | unsafe { |
| 114 | $crate::cast::Mat2Cast { |
| 115 | v2x2: [$crate::const_vec2!($col0), $crate::const_vec2!($col1)], |
| 116 | } |
| 117 | .m2 |
| 118 | } |
| 119 | }; |
| 120 | ($fx4:expr) => { |
| 121 | $crate::const_mat2!( |
| 122 | $crate::cast::Vec4Cast { fx4: $fx4 }.fx2x2[0], |
| 123 | $crate::cast::Vec4Cast { fx4: $fx4 }.fx2x2[1] |
| 124 | ) |
| 125 | }; |
| 126 | } |
| 127 | |
| 128 | /// Creates a `Mat3` from three column vectors that can be used to initialize a constant value. |
| 129 | /// |
| 130 | /// ``` |
| 131 | /// use glam::{const_mat3, Mat3}; |
| 132 | /// const ZERO: Mat3 = const_mat3!([0.0; 9]); |
| 133 | /// const IDENTITY: Mat3 = const_mat3!([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]); |
| 134 | /// ``` |
| 135 | #[macro_export] |
| 136 | macro_rules! const_mat3 { |
| 137 | ($col0:expr, $col1:expr, $col2:expr) => { |
| 138 | unsafe { |
| 139 | $crate::cast::Mat3Cast { |
| 140 | v3x3: [ |
| 141 | $crate::const_vec3!($col0), |
| 142 | $crate::const_vec3!($col1), |
| 143 | $crate::const_vec3!($col2), |
| 144 | ], |
| 145 | } |
| 146 | .m3 |
| 147 | } |
| 148 | }; |
| 149 | ($fx9:expr) => { |
| 150 | $crate::const_mat3!( |
| 151 | $crate::cast::F32x9Cast { fx9: $fx9 }.fx3x3[0], |
| 152 | $crate::cast::F32x9Cast { fx9: $fx9 }.fx3x3[1], |
| 153 | $crate::cast::F32x9Cast { fx9: $fx9 }.fx3x3[2] |
| 154 | ) |
| 155 | }; |
| 156 | } |
| 157 | |
| 158 | /// Creates a `Mat3A` from three column vectors that can be used to initialize a constant value. |
| 159 | /// |
| 160 | /// ``` |
| 161 | /// use glam::{const_mat3a, Mat3A}; |
| 162 | /// const ZERO: Mat3A = const_mat3a!([0.0; 9]); |
| 163 | /// const IDENTITY: Mat3A = const_mat3a!([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]); |
| 164 | /// ``` |
| 165 | #[macro_export] |
| 166 | macro_rules! const_mat3a { |
| 167 | ($col0:expr, $col1:expr, $col2:expr) => { |
| 168 | unsafe { |
| 169 | $crate::cast::Mat3ACast { |
| 170 | v3x3: [ |
| 171 | $crate::const_vec3a!($col0), |
| 172 | $crate::const_vec3a!($col1), |
| 173 | $crate::const_vec3a!($col2), |
| 174 | ], |
| 175 | } |
| 176 | .m3 |
| 177 | } |
| 178 | }; |
| 179 | ($fx9:expr) => { |
| 180 | $crate::const_mat3a!( |
| 181 | $crate::cast::F32x9Cast { fx9: $fx9 }.fx3x3[0], |
| 182 | $crate::cast::F32x9Cast { fx9: $fx9 }.fx3x3[1], |
| 183 | $crate::cast::F32x9Cast { fx9: $fx9 }.fx3x3[2] |
| 184 | ) |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | /// Creates a `Mat4` from four column vectors that can be used to initialize a constant value. |
| 189 | /// |
| 190 | /// ``` |
| 191 | /// use glam::{const_mat4, Mat4}; |
| 192 | /// const ZERO: Mat4 = const_mat4!([0.0; 16]); |
| 193 | /// const IDENTITY: Mat4 = const_mat4!( |
| 194 | /// [1.0, 0.0, 0.0, 0.0], |
| 195 | /// [0.0, 1.0, 0.0, 0.0], |
| 196 | /// [0.0, 0.0, 1.0, 0.0], |
| 197 | /// [0.0, 0.0, 0.0, 1.0] |
| 198 | /// ); |
| 199 | /// ``` |
| 200 | #[macro_export] |
| 201 | macro_rules! const_mat4 { |
| 202 | ($col0:expr, $col1:expr, $col2:expr, $col3:expr) => { |
| 203 | unsafe { |
| 204 | $crate::cast::Mat4Cast { |
| 205 | v4x4: [ |
| 206 | $crate::const_vec4!($col0), |
| 207 | $crate::const_vec4!($col1), |
| 208 | $crate::const_vec4!($col2), |
| 209 | $crate::const_vec4!($col3), |
| 210 | ], |
| 211 | } |
| 212 | .m4 |
| 213 | } |
| 214 | }; |
| 215 | ($fx16:expr) => { |
| 216 | $crate::const_mat4!( |
| 217 | $crate::cast::F32x16Cast { fx16: $fx16 }.fx4x4[0], |
| 218 | $crate::cast::F32x16Cast { fx16: $fx16 }.fx4x4[1], |
| 219 | $crate::cast::F32x16Cast { fx16: $fx16 }.fx4x4[2], |
| 220 | $crate::cast::F32x16Cast { fx16: $fx16 }.fx4x4[3] |
| 221 | ) |
| 222 | }; |
| 223 | } |
| 224 | |
| 225 | /// Creates a `Quat` from `x`, `y`, `z` and `w` values that can be used to initialize a constant |
| 226 | /// value. |
| 227 | /// |
| 228 | /// ``` |
| 229 | /// use glam::{const_quat, Quat}; |
| 230 | /// const IDENTITY: Quat = const_quat!([0.0, 0.0, 0.0, 1.0]); |
| 231 | /// ``` |
| 232 | #[macro_export] |
| 233 | macro_rules! const_quat { |
| 234 | ($fx4:expr) => { |
| 235 | unsafe { $crate::cast::Vec4Cast { fx4: $fx4 }.q } |
| 236 | }; |
| 237 | } |
| 238 | |
| 239 | /// Creates a `DVec2` that can be used to initialize a constant value. |
| 240 | /// |
| 241 | /// ``` |
| 242 | /// use glam::{const_dvec2, DVec2}; |
| 243 | /// const ONE: DVec2 = const_dvec2!([1.0; 2]); |
| 244 | /// const X: DVec2 = const_dvec2!([1.0, 0.0]); |
| 245 | /// ``` |
| 246 | #[macro_export] |
| 247 | macro_rules! const_dvec2 { |
| 248 | ($fx2:expr) => { |
| 249 | unsafe { $crate::cast::DVec2Cast { fx2: $fx2 }.v2 } |
| 250 | }; |
| 251 | } |
| 252 | |
| 253 | /// Creates a `DVec3` that can be used to initialize a constant value. |
| 254 | /// |
| 255 | /// ``` |
| 256 | /// use glam::{const_dvec3, DVec3}; |
| 257 | /// const ONE: DVec3 = const_dvec3!([1.0; 3]); |
| 258 | /// const X: DVec3 = const_dvec3!([1.0, 0.0, 0.0]); |
| 259 | /// ``` |
| 260 | #[macro_export] |
| 261 | macro_rules! const_dvec3 { |
| 262 | ($fx3:expr) => { |
| 263 | unsafe { $crate::cast::DVec3Cast { fx3: $fx3 }.v3 } |
| 264 | }; |
| 265 | } |
| 266 | |
| 267 | /// Creates a `DVec4` that can be used to initialize a constant value. |
| 268 | /// |
| 269 | /// ``` |
| 270 | /// use glam::{const_dvec4, DVec4}; |
| 271 | /// const ONE: DVec4 = const_dvec4!([1.0; 4]); |
| 272 | /// const X: DVec4 = const_dvec4!([1.0, 0.0, 0.0, 0.0]); |
| 273 | /// ``` |
| 274 | #[macro_export] |
| 275 | macro_rules! const_dvec4 { |
| 276 | ($fx4:expr) => { |
| 277 | unsafe { $crate::cast::DVec4Cast { fx4: $fx4 }.v4 } |
| 278 | }; |
| 279 | } |
| 280 | |
| 281 | /// Creates a `DMat2` from two column vectors that can be used to initialize a constant value. |
| 282 | /// |
| 283 | /// ``` |
| 284 | /// use glam::{const_dmat2, DMat2}; |
| 285 | /// const ZERO: DMat2 = const_dmat2!([0.0; 4]); |
| 286 | /// const IDENTITY: DMat2 = const_dmat2!([1.0, 0.0], [0.0, 1.0]); |
| 287 | /// ``` |
| 288 | #[macro_export] |
| 289 | macro_rules! const_dmat2 { |
| 290 | ($col0:expr, $col1:expr) => { |
| 291 | unsafe { |
| 292 | $crate::cast::DMat2Cast { |
| 293 | v2x2: [$crate::const_dvec2!($col0), $crate::const_dvec2!($col1)], |
| 294 | } |
| 295 | .m2 |
| 296 | } |
| 297 | }; |
| 298 | ($fx4:expr) => { |
| 299 | $crate::const_dmat2!( |
| 300 | $crate::cast::DVec4Cast { fx4: $fx4 }.fx2x2[0], |
| 301 | $crate::cast::DVec4Cast { fx4: $fx4 }.fx2x2[1] |
| 302 | ) |
| 303 | }; |
| 304 | } |
| 305 | |
| 306 | /// Creates a `DMat3` from three column vectors that can be used to initialize a constant value. |
| 307 | /// |
| 308 | /// ``` |
| 309 | /// # #[macro_use] extern crate glam; |
| 310 | /// use glam::{const_dmat3, DMat3}; |
| 311 | /// const ZERO: DMat3 = const_dmat3!([0.0; 9]); |
| 312 | /// const IDENTITY: DMat3 = const_dmat3!([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]); |
| 313 | /// ``` |
| 314 | #[macro_export] |
| 315 | macro_rules! const_dmat3 { |
| 316 | ($col0:expr, $col1:expr, $col2:expr) => { |
| 317 | unsafe { |
| 318 | $crate::cast::DMat3Cast { |
| 319 | v3x3: [ |
| 320 | $crate::const_dvec3!($col0), |
| 321 | $crate::const_dvec3!($col1), |
| 322 | $crate::const_dvec3!($col2), |
| 323 | ], |
| 324 | } |
| 325 | .m3 |
| 326 | } |
| 327 | }; |
| 328 | ($fx9:expr) => { |
| 329 | $crate::const_dmat3!( |
| 330 | $crate::cast::F64x9Cast { fx9: $fx9 }.fx3x3[0], |
| 331 | $crate::cast::F64x9Cast { fx9: $fx9 }.fx3x3[1], |
| 332 | $crate::cast::F64x9Cast { fx9: $fx9 }.fx3x3[2] |
| 333 | ) |
| 334 | }; |
| 335 | } |
| 336 | |
| 337 | /// Creates a `DMat4` from four column vectors that can be used to initialize a constant value. |
| 338 | /// |
| 339 | /// ``` |
| 340 | /// use glam::{const_dmat4, DMat4}; |
| 341 | /// const ZERO: DMat4 = const_dmat4!([0.0; 16]); |
| 342 | /// const IDENTITY: DMat4 = const_dmat4!( |
| 343 | /// [1.0, 0.0, 0.0, 0.0], |
| 344 | /// [0.0, 1.0, 0.0, 0.0], |
| 345 | /// [0.0, 0.0, 1.0, 0.0], |
| 346 | /// [0.0, 0.0, 0.0, 1.0] |
| 347 | /// ); |
| 348 | /// ``` |
| 349 | #[macro_export] |
| 350 | macro_rules! const_dmat4 { |
| 351 | ($col0:expr, $col1:expr, $col2:expr, $col3:expr) => { |
| 352 | unsafe { |
| 353 | $crate::cast::DMat4Cast { |
| 354 | v4x4: [ |
| 355 | $crate::const_dvec4!($col0), |
| 356 | $crate::const_dvec4!($col1), |
| 357 | $crate::const_dvec4!($col2), |
| 358 | $crate::const_dvec4!($col3), |
| 359 | ], |
| 360 | } |
| 361 | .m4 |
| 362 | } |
| 363 | }; |
| 364 | ($fx16:expr) => { |
| 365 | $crate::const_dmat4!( |
| 366 | $crate::cast::F64x16Cast { fx16: $fx16 }.fx4x4[0], |
| 367 | $crate::cast::F64x16Cast { fx16: $fx16 }.fx4x4[1], |
| 368 | $crate::cast::F64x16Cast { fx16: $fx16 }.fx4x4[2], |
| 369 | $crate::cast::F64x16Cast { fx16: $fx16 }.fx4x4[3] |
| 370 | ) |
| 371 | }; |
| 372 | } |
| 373 | |
| 374 | /// Creates a `DQuat` from `x`, `y`, `z` and `w` values that can be used to initialize a constant |
| 375 | /// value. |
| 376 | /// |
| 377 | /// ``` |
| 378 | /// use glam::{const_dquat, DQuat}; |
| 379 | /// const IDENTITY: DQuat = const_dquat!([0.0, 0.0, 0.0, 1.0]); |
| 380 | /// ``` |
| 381 | #[macro_export] |
| 382 | macro_rules! const_dquat { |
| 383 | ($fx4:expr) => { |
| 384 | unsafe { $crate::cast::DVec4Cast { fx4: $fx4 }.q } |
| 385 | }; |
| 386 | } |
| 387 | |
| 388 | /// Creates a `IVec2` that can be used to initialize a constant value. |
| 389 | /// |
| 390 | /// ``` |
| 391 | /// use glam::{const_ivec2, IVec2}; |
| 392 | /// const ONE: IVec2 = const_ivec2!([1; 2]); |
| 393 | /// const X: IVec2 = const_ivec2!([1, 0]); |
| 394 | /// ``` |
| 395 | #[macro_export] |
| 396 | macro_rules! const_ivec2 { |
| 397 | ($ix2:expr) => { |
| 398 | unsafe { $crate::cast::IVec2Cast { ix2: $ix2 }.v2 } |
| 399 | }; |
| 400 | } |
| 401 | |
| 402 | /// Creates a `IVec3` that can be used to initialize a constant value. |
| 403 | /// |
| 404 | /// ``` |
| 405 | /// use glam::{const_ivec3, IVec3}; |
| 406 | /// const ONE: IVec3 = const_ivec3!([1; 3]); |
| 407 | /// const X: IVec3 = const_ivec3!([1, 0, 0]); |
| 408 | /// ``` |
| 409 | #[macro_export] |
| 410 | macro_rules! const_ivec3 { |
| 411 | ($ix3:expr) => { |
| 412 | unsafe { $crate::cast::IVec3Cast { ix3: $ix3 }.v3 } |
| 413 | }; |
| 414 | } |
| 415 | |
| 416 | /// Creates a `IVec4` that can be used to initialize a constant value. |
| 417 | /// |
| 418 | /// ``` |
| 419 | /// use glam::{const_ivec4, IVec4}; |
| 420 | /// const ONE: IVec4 = const_ivec4!([1; 4]); |
| 421 | /// const X: IVec4 = const_ivec4!([1, 0, 0, 0]); |
| 422 | /// ``` |
| 423 | #[macro_export] |
| 424 | macro_rules! const_ivec4 { |
| 425 | ($ix4:expr) => { |
| 426 | unsafe { $crate::cast::IVec4Cast { ix4: $ix4 }.v4 } |
| 427 | }; |
| 428 | } |
| 429 | |
| 430 | /// Creates a `UVec2` that can be used to initialize a constant value. |
| 431 | /// |
| 432 | /// ``` |
| 433 | /// use glam::{const_uvec2, UVec2}; |
| 434 | /// const ONE: UVec2 = const_uvec2!([1; 2]); |
| 435 | /// const X: UVec2 = const_uvec2!([1, 0]); |
| 436 | /// ``` |
| 437 | #[macro_export] |
| 438 | macro_rules! const_uvec2 { |
| 439 | ($ux2:expr) => { |
| 440 | unsafe { $crate::cast::UVec2Cast { ux2: $ux2 }.v2 } |
| 441 | }; |
| 442 | } |
| 443 | |
| 444 | /// Creates a `UVec3` that can be used to initialize a constant value. |
| 445 | /// |
| 446 | /// ``` |
| 447 | /// use glam::{const_uvec3, UVec3}; |
| 448 | /// const ONE: UVec3 = const_uvec3!([1; 3]); |
| 449 | /// const X: UVec3 = const_uvec3!([1, 0, 0]); |
| 450 | /// ``` |
| 451 | #[macro_export] |
| 452 | macro_rules! const_uvec3 { |
| 453 | ($ux3:expr) => { |
| 454 | unsafe { $crate::cast::UVec3Cast { ux3: $ux3 }.v3 } |
| 455 | }; |
| 456 | } |
| 457 | |
| 458 | /// Creates a `UVec4` that can be used to initialize a constant value. |
| 459 | /// |
| 460 | /// ``` |
| 461 | /// use glam::{const_uvec4, UVec4}; |
| 462 | /// const ONE: UVec4 = const_uvec4!([1; 4]); |
| 463 | /// const X: UVec4 = const_uvec4!([1, 0, 0, 0]); |
| 464 | /// ``` |
| 465 | #[macro_export] |
| 466 | macro_rules! const_uvec4 { |
| 467 | ($ux4:expr) => { |
| 468 | unsafe { $crate::cast::UVec4Cast { ux4: $ux4 }.v4 } |
| 469 | }; |
| 470 | } |