1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
/* * * This file is a part of NovaEngine * https://gitlab.com/MindSpunk/NovaEngine * * * MIT License * * Copyright (c) 2018 Nathan Voglsam * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ use core::f64::consts::PI; use core::fmt::{Debug, Display}; use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; use num_traits::Float; pub enum FloatType { F32, F64, } /// /// This type represents a real number. A macro trait that makes using generic floats much easier /// pub trait Real: Float + Add + AddAssign + Sub + SubAssign + Mul + MulAssign + Div + DivAssign + Copy + Clone + Pi + IntoDegrees + IntoRadians + PartialEq + PartialOrd + Debug + Display { /// /// Is this type a single-precision floating point. Useful for identifying the underlying /// representation for SIMD purposes /// fn is_f32() -> bool; /// /// Is this type a double-precision floating point. Useful for identifying the underlying /// representation for SIMD purposes /// fn is_f64() -> bool; /// /// Return the floating point type as an enum. Mostly just a convinience instead of using /// `is_f32()` or `is_f64()` /// fn float_type() -> FloatType; /// /// Force get this value as single-precision /// fn as_f32(self) -> f32; /// /// Force get this value as double-precision /// fn as_f64(self) -> f64; } impl Real for f32 { #[inline] fn is_f32() -> bool { true } #[inline] fn is_f64() -> bool { false } #[inline] fn float_type() -> FloatType { FloatType::F32 } #[inline] fn as_f32(self) -> f32 { self } #[inline] fn as_f64(self) -> f64 { f64::from(self) } } impl Real for f64 { #[inline] fn is_f32() -> bool { false } #[inline] fn is_f64() -> bool { true } #[inline] fn float_type() -> FloatType { FloatType::F64 } #[inline] fn as_f32(self) -> f32 { self as f32 } #[inline] fn as_f64(self) -> f64 { self } } /// /// This type can perform linear interpolate from a to b with factor /// pub trait Lerp<F: Real> { /// /// Return the result of interpolating between self and b with factor `factor` /// fn lerp(&self, b: &Self, factor: F) -> Self; } impl Lerp<f32> for f32 { fn lerp(&self, b: &Self, factor: f32) -> Self { *self + ((*self - *b) * factor) } } impl Lerp<f64> for f64 { fn lerp(&self, b: &Self, factor: f64) -> Self { *self + ((*self - *b) * factor) } } /// /// This type has a constant PI /// pub trait Pi { /// /// Get a value that represents Pi /// fn pi() -> Self; } impl<T: Real> Pi for T { fn pi() -> Self { T::from(PI).unwrap() } } /// /// This type can convert from degrees to radians /// pub trait IntoRadians { /// /// Consume self and return it after converting the internal elements from degrees to radians /// fn into_radians(self) -> Self; } impl<T: Real + Pi> IntoRadians for T { fn into_radians(self) -> Self { crate::units::radians(self) } } /// /// This type can convert from radians to degrees /// pub trait IntoDegrees { /// /// Consume self and return it after converting the internal elements from radians to degrees /// fn into_degrees(self) -> Self; } impl<T: Real + Pi> IntoDegrees for T { fn into_degrees(self) -> Self { crate::units::degrees(self) } } /// /// This type can be used to produce a dot product /// pub trait DotProduct<T: Real> { /// /// Produce the dot product of self and rhs /// fn dot(&self, rhs: &Self) -> T; } /// /// This type can be used to produce a cross product /// pub trait CrossProduct { /// /// Produce the cross product of self and rhs /// /// # Note /// /// This can only really be implemented for a 3 component vector but is a trait to allow for /// separating storage from implementation /// fn cross(&self, rhs: &Self) -> Self; } /// /// This types supports performing a matrix transpose /// /// # Info /// /// Similar to the `Add` or `Mul` traits in that it takes ownership and passes the underlying object /// through the function. /// pub trait Transpose { fn transpose(self) -> Self; } /// /// This type supports performing a matrix transpose /// /// # Info /// /// Similar to the `AddAssign` or `MulAssign` traits in that it takes a mutable reference to the /// underlying object and performs the transpose in place. /// pub trait TransposeAssign { fn transpose_assign(&mut self); } // TODO: Document me pub trait Inverse { fn inverse(self) -> Self; } pub trait InverseAssign { fn inverse_assign(&mut self); } /// /// Packing the underlying data of a vector or matrix /// pub trait Pack { type GLSLOutput; type HLSLOutput; type GLSLOutputArray; type HLSLOutputArray; type CPUOutput; /// /// Convert the struct into packed data ready to be uploaded and consumed with hlsl standard /// conventions. /// /// This will often round vectors up to alignment multiple sizes with padding bytes and is /// important for matrices as hlsl shaders are expecting the matrices to be row major. /// /// # Warning /// /// If the matrix this is implemented on is row major it will have to perform an implict /// transpose and so CAN CHANGE the underlying data beyond adding GPU required padding. /// fn into_packed_glsl(self) -> Self::GLSLOutput; /// /// Convert the struct into packed data ready to be uploaded and consumed with hlsl standard /// conventions. /// /// This will often round vectors up to alignment multiple sizes with padding bytes and is /// important for matrices as hlsl shaders are expecting the matrices to be row major. /// /// # Warning /// /// If the matrix this is implemented on is column major it will have to perform an implict /// transpose and so CAN CHANGE the underlying data beyond adding GPU required padding /// fn into_packed_hlsl(self) -> Self::HLSLOutput; /// /// Convert the struct into packed data ready to be uploaded and consumed with hlsl standard /// conventions. /// /// This function produces a semantically similar result to `into_packed_hlsl` but differs in /// that for some GPU packing conventions (read: std430) the padding for an item, like a vec3, /// differs whether it is on it's own or if it is an array element. /// /// This will often round vectors up to alignment multiple sizes with padding bytes and is /// important for matrices as hlsl shaders are expecting the matrices to be row major. /// /// # Warning /// /// If the matrix this is implemented on is row major it will have to perform an implict /// transpose and so CAN CHANGE the underlying data beyond adding GPU required padding. /// fn into_packed_glsl_array(self) -> Self::GLSLOutputArray; /// /// Convert the struct into packed data ready to be uploaded and consumed with hlsl standard /// conventions. /// /// This function produces a semantically similar result to `into_packed_hlsl` but differs in /// that for some GPU packing conventions (read: std430) the padding for an item, like a vec3, /// differs whether it is on it's own or if it is an array element. /// /// This will often round vectors up to alignment multiple sizes with padding bytes and is /// important for matrices as hlsl shaders are expecting the matrices to be row major. /// /// # Warning /// /// If the matrix this is implemented on is column major it will have to perform an implict /// transpose and so CAN CHANGE the underlying data beyond adding GPU required padding. /// fn into_packed_hlsl_array(self) -> Self::HLSLOutputArray; /// /// Convert the struct into packed data for general purpose use on the CPU. This would be /// ideal for things like serialization where you don't need to conform to special GPU alignment /// and padding rules. /// /// This should, by general convention, just produce a flat array of the individual components /// and should match the underlying number of components (3 for a Vec3, etc). /// /// # Warning /// /// There should be no padding in the results of this function. /// fn into_packed_cpu(self) -> Self::CPUOutput; } /// /// Trait that marks a type as able to be packable into a std140 compatible form /// pub trait IntoSTD140 { type Output: Pack; fn into_std140(self) -> Self::Output; } /// /// This type abstracts a matrix where you can get a copy of a column /// pub trait Column { type Output; /// /// Get a copy of a given column of a matrix /// fn get_column(&self, col: usize) -> Self::Output; } /// /// This type abstracts a matrix where you can get a reference of a column /// pub trait ColumnRef: Column { /// /// Get a reference to a given column of a matrix /// fn get_column_ref(&self, col: usize) -> &Self::Output; } /// /// This type abstracts a matrix where you can get a mutable reference of a column /// pub trait ColumnRefMut: Column { /// /// Get a mutable reference to a given column of a matrix /// fn get_column_ref_mut(&mut self, col: usize) -> &mut Self::Output; } /// /// This type abstracts a matrix where you can get a copy of a row /// pub trait Row { type Output; /// /// Get a copy of a given row of a matrix /// fn get_row(&self, row: usize) -> Self::Output; } /// /// This type abstracts a matrix where you can get a reference of a row /// pub trait RowRef: Row { /// /// Get a reference to a given row of a matrix /// fn get_row_ref(&self, row: usize) -> &Self::Output; } /// /// This type abstracts a matrix where you can get a mutable reference of a row /// pub trait RowRefMut: Row { /// /// Get a mutable reference to a given row of a matrix /// fn get_row_ref_mut(&mut self, row: usize) -> &mut Self::Output; } /// /// This type abstracts a vector or other object that can represents a length /// pub trait Length { type Output; fn length(&self) -> Self::Output; } /// /// This type abstracts a vector or other object that can represents a length. Get's the square of /// the length as this can often skip an expensive square root calculation. /// pub trait LengthSquared { type Output; fn length_squared(&self) -> Self::Output; } /// /// This type abstracts a vector or other object that can be normalized to represent the same /// direction while having a length of 1 /// pub trait Normalize { fn normalize(self) -> Self; } /// /// This type abstracts a vector or other object that can be normalized to represent the same /// direction while having a length of 1 /// pub trait NormalizeAssign { fn normalize_assign(&mut self); } pub mod std140 { use crate::traits::{IntoSTD140, Pack}; /// /// A wrapper struct that is used to implement std140 packing for the underlying type /// #[repr(transparent)] #[derive(Copy, Clone, Debug)] pub struct F32Pack(f32); impl IntoSTD140 for f32 { type Output = F32Pack; fn into_std140(self) -> Self::Output { F32Pack(self) } } impl Pack for F32Pack { type GLSLOutput = f32; type HLSLOutput = f32; type GLSLOutputArray = f32; type HLSLOutputArray = f32; type CPUOutput = f32; fn into_packed_glsl(self) -> Self::GLSLOutput { self.0 } fn into_packed_hlsl(self) -> Self::HLSLOutput { self.0 } fn into_packed_glsl_array(self) -> Self::GLSLOutputArray { self.0 } fn into_packed_hlsl_array(self) -> Self::HLSLOutputArray { self.0 } fn into_packed_cpu(self) -> Self::CPUOutput { self.0 } } /// /// A wrapper struct that is used to implement std140 packing for the underlying type /// #[repr(transparent)] #[derive(Copy, Clone, Debug)] pub struct F64Pack(f64); impl IntoSTD140 for f64 { type Output = F64Pack; fn into_std140(self) -> Self::Output { F64Pack(self) } } impl Pack for F64Pack { type GLSLOutput = f64; type HLSLOutput = f64; type GLSLOutputArray = f64; type HLSLOutputArray = f64; type CPUOutput = f64; fn into_packed_glsl(self) -> Self::GLSLOutput { self.0 } fn into_packed_hlsl(self) -> Self::HLSLOutput { self.0 } fn into_packed_glsl_array(self) -> Self::GLSLOutputArray { self.0 } fn into_packed_hlsl_array(self) -> Self::HLSLOutputArray { self.0 } fn into_packed_cpu(self) -> Self::CPUOutput { self.0 } } }