| 1 |
use core::{arch::wasm32::*, mem::MaybeUninit}; |
| 2 |
|
| 3 |
use crate::core::{ |
| 4 |
storage::{Columns2, Columns3, Columns4, XY, XYZ}, |
| 5 |
traits::{ |
| 6 |
matrix::{ |
| 7 |
FloatMatrix2x2, FloatMatrix3x3, FloatMatrix4x4, Matrix, Matrix2x2, Matrix3x3, |
| 8 |
Matrix4x4, MatrixConst, |
| 9 |
}, |
| 10 |
projection::ProjectionMatrix, |
| 11 |
vector::{FloatVector4, Vector, Vector4, Vector4Const, VectorConst}, |
| 12 |
}, |
| 13 |
}; |
| 14 |
|
| 15 |
// v128 as a Matrix2x2 |
| 16 |
impl MatrixConst for v128 { |
| 17 |
const ZERO: v128 = const_f32x4!([0.0, 0.0, 0.0, 0.0]); |
| 18 |
const IDENTITY: v128 = const_f32x4!([1.0, 0.0, 0.0, 1.0]); |
| 19 |
} |
| 20 |
|
| 21 |
impl Matrix<f32> for v128 {} |
| 22 |
|
| 23 |
impl Matrix2x2<f32, XY<f32>> for v128 { |
| 24 |
#[inline(always)] |
| 25 |
fn new(m00: f32, m01: f32, m10: f32, m11: f32) -> Self { |
| 26 |
f32x4(m00, m01, m10, m11) |
| 27 |
} |
| 28 |
|
| 29 |
#[inline(always)] |
| 30 |
fn from_cols(x_axis: XY<f32>, y_axis: XY<f32>) -> Self { |
| 31 |
Matrix2x2::new(x_axis.x, x_axis.y, y_axis.x, y_axis.y) |
| 32 |
} |
| 33 |
|
| 34 |
#[inline(always)] |
| 35 |
fn x_axis(&self) -> &XY<f32> { |
| 36 |
unsafe { &(*(self as *const Self as *const Columns2<XY<f32>>)).x_axis } |
| 37 |
} |
| 38 |
|
| 39 |
#[inline(always)] |
| 40 |
fn y_axis(&self) -> &XY<f32> { |
| 41 |
unsafe { &(*(self as *const Self as *const Columns2<XY<f32>>)).y_axis } |
| 42 |
} |
| 43 |
|
| 44 |
#[inline] |
| 45 |
fn determinant(&self) -> f32 { |
| 46 |
// self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x |
| 47 |
let abcd = *self; |
| 48 |
let dcba = i32x4_shuffle::<3, 2, 5, 4>(abcd, abcd); |
| 49 |
let prod = f32x4_mul(abcd, dcba); |
| 50 |
let det = f32x4_sub(prod, i32x4_shuffle::<1, 1, 5, 5>(prod, prod)); |
| 51 |
f32x4_extract_lane::<0>(det) |
| 52 |
} |
| 53 |
|
| 54 |
#[inline(always)] |
| 55 |
fn transpose(&self) -> Self { |
| 56 |
i32x4_shuffle::<0, 2, 5, 7>(*self, *self) |
| 57 |
} |
| 58 |
|
| 59 |
#[inline] |
| 60 |
fn mul_vector(&self, other: XY<f32>) -> XY<f32> { |
| 61 |
let abcd = *self; |
| 62 |
let xxyy = f32x4(other.x, other.x, other.y, other.y); |
| 63 |
let axbxcydy = f32x4_mul(abcd, xxyy); |
| 64 |
let cydyaxbx = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy, axbxcydy); |
| 65 |
let result = f32x4_add(axbxcydy, cydyaxbx); |
| 66 |
let mut out: MaybeUninit<v128> = MaybeUninit::uninit(); |
| 67 |
unsafe { |
| 68 |
v128_store(out.as_mut_ptr(), result); |
| 69 |
*(&out.assume_init() as *const v128 as *const XY<f32>) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
#[inline] |
| 74 |
fn mul_matrix(&self, other: &Self) -> Self { |
| 75 |
let abcd = *self; |
| 76 |
let other = *other; |
| 77 |
let xxyy0 = i32x4_shuffle::<0, 0, 5, 5>(other, other); |
| 78 |
let xxyy1 = i32x4_shuffle::<2, 2, 7, 7>(other, other); |
| 79 |
let axbxcydy0 = f32x4_mul(abcd, xxyy0); |
| 80 |
let axbxcydy1 = f32x4_mul(abcd, xxyy1); |
| 81 |
let cydyaxbx0 = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy0, axbxcydy0); |
| 82 |
let cydyaxbx1 = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy1, axbxcydy1); |
| 83 |
let result0 = f32x4_add(axbxcydy0, cydyaxbx0); |
| 84 |
let result1 = f32x4_add(axbxcydy1, cydyaxbx1); |
| 85 |
i32x4_shuffle::<0, 1, 4, 5>(result0, result1) |
| 86 |
} |
| 87 |
|
| 88 |
#[inline] |
| 89 |
fn mul_scalar(&self, other: f32) -> Self { |
| 90 |
f32x4_mul(*self, f32x4_splat(other)) |
| 91 |
} |
| 92 |
|
| 93 |
#[inline] |
| 94 |
fn add_matrix(&self, other: &Self) -> Self { |
| 95 |
f32x4_add(*self, *other) |
| 96 |
} |
| 97 |
|
| 98 |
#[inline] |
| 99 |
fn sub_matrix(&self, other: &Self) -> Self { |
| 100 |
f32x4_sub(*self, *other) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl FloatMatrix2x2<f32, XY<f32>> for v128 { |
| 105 |
#[inline] |
| 106 |
fn abs_diff_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 107 |
FloatVector4::abs_diff_eq(*self, *other, max_abs_diff) |
| 108 |
} |
| 109 |
|
| 110 |
#[inline] |
| 111 |
fn inverse(&self) -> Self { |
| 112 |
const SIGN: v128 = const_f32x4!([1.0, -1.0, -1.0, 1.0]); |
| 113 |
let abcd = *self; |
| 114 |
let dcba = i32x4_shuffle::<3, 2, 5, 4>(abcd, abcd); |
| 115 |
let prod = f32x4_mul(abcd, dcba); |
| 116 |
let sub = f32x4_sub(prod, i32x4_shuffle::<1, 1, 5, 5>(prod, prod)); |
| 117 |
let det = i32x4_shuffle::<0, 0, 4, 4>(sub, sub); |
| 118 |
let tmp = f32x4_div(SIGN, det); |
| 119 |
glam_assert!(tmp.is_finite()); |
| 120 |
let dbca = i32x4_shuffle::<3, 1, 6, 4>(abcd, abcd); |
| 121 |
f32x4_mul(dbca, tmp) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
impl MatrixConst for Columns3<v128> { |
| 126 |
const ZERO: Columns3<v128> = Columns3 { |
| 127 |
x_axis: VectorConst::ZERO, |
| 128 |
y_axis: VectorConst::ZERO, |
| 129 |
z_axis: VectorConst::ZERO, |
| 130 |
}; |
| 131 |
const IDENTITY: Columns3<v128> = Columns3 { |
| 132 |
x_axis: v128::X, |
| 133 |
y_axis: v128::Y, |
| 134 |
z_axis: v128::Z, |
| 135 |
}; |
| 136 |
} |
| 137 |
|
| 138 |
impl Matrix<f32> for Columns3<v128> {} |
| 139 |
|
| 140 |
impl Matrix3x3<f32, v128> for Columns3<v128> { |
| 141 |
#[inline(always)] |
| 142 |
fn from_cols(x_axis: v128, y_axis: v128, z_axis: v128) -> Self { |
| 143 |
Self { |
| 144 |
x_axis, |
| 145 |
y_axis, |
| 146 |
z_axis, |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
#[inline(always)] |
| 151 |
fn x_axis(&self) -> &v128 { |
| 152 |
&self.x_axis |
| 153 |
} |
| 154 |
|
| 155 |
#[inline(always)] |
| 156 |
fn y_axis(&self) -> &v128 { |
| 157 |
&self.y_axis |
| 158 |
} |
| 159 |
|
| 160 |
#[inline(always)] |
| 161 |
fn z_axis(&self) -> &v128 { |
| 162 |
&self.z_axis |
| 163 |
} |
| 164 |
|
| 165 |
#[inline] |
| 166 |
fn transpose(&self) -> Self { |
| 167 |
let tmp0 = i32x4_shuffle::<0, 1, 4, 5>(self.x_axis, self.y_axis); |
| 168 |
let tmp1 = i32x4_shuffle::<2, 3, 6, 7>(self.x_axis, self.y_axis); |
| 169 |
|
| 170 |
Self { |
| 171 |
x_axis: i32x4_shuffle::<0, 2, 4, 4>(tmp0, self.z_axis), |
| 172 |
y_axis: i32x4_shuffle::<1, 3, 5, 5>(tmp0, self.z_axis), |
| 173 |
z_axis: i32x4_shuffle::<0, 2, 6, 6>(tmp1, self.z_axis), |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
impl FloatMatrix3x3<f32, v128> for Columns3<v128> { |
| 179 |
#[inline] |
| 180 |
fn transform_point2(&self, other: XY<f32>) -> XY<f32> { |
| 181 |
let mut res = self.x_axis.mul_scalar(other.x); |
| 182 |
res = self.y_axis.mul_scalar(other.y).add(res); |
| 183 |
res = self.z_axis.add(res); |
| 184 |
res.into() |
| 185 |
} |
| 186 |
|
| 187 |
#[inline] |
| 188 |
fn transform_vector2(&self, other: XY<f32>) -> XY<f32> { |
| 189 |
let mut res = self.x_axis.mul_scalar(other.x); |
| 190 |
res = self.y_axis.mul_scalar(other.y).add(res); |
| 191 |
res.into() |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
impl MatrixConst for Columns4<v128> { |
| 196 |
const ZERO: Columns4<v128> = Columns4 { |
| 197 |
x_axis: VectorConst::ZERO, |
| 198 |
y_axis: VectorConst::ZERO, |
| 199 |
z_axis: VectorConst::ZERO, |
| 200 |
w_axis: VectorConst::ZERO, |
| 201 |
}; |
| 202 |
const IDENTITY: Columns4<v128> = Columns4 { |
| 203 |
x_axis: v128::X, |
| 204 |
y_axis: v128::Y, |
| 205 |
z_axis: v128::Z, |
| 206 |
w_axis: v128::W, |
| 207 |
}; |
| 208 |
} |
| 209 |
|
| 210 |
impl Matrix<f32> for Columns4<v128> {} |
| 211 |
|
| 212 |
impl Matrix4x4<f32, v128> for Columns4<v128> { |
| 213 |
#[inline(always)] |
| 214 |
fn from_cols(x_axis: v128, y_axis: v128, z_axis: v128, w_axis: v128) -> Self { |
| 215 |
Self { |
| 216 |
x_axis, |
| 217 |
y_axis, |
| 218 |
z_axis, |
| 219 |
w_axis, |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
#[inline(always)] |
| 224 |
fn x_axis(&self) -> &v128 { |
| 225 |
&self.x_axis |
| 226 |
} |
| 227 |
|
| 228 |
#[inline(always)] |
| 229 |
fn y_axis(&self) -> &v128 { |
| 230 |
&self.y_axis |
| 231 |
} |
| 232 |
|
| 233 |
#[inline(always)] |
| 234 |
fn z_axis(&self) -> &v128 { |
| 235 |
&self.z_axis |
| 236 |
} |
| 237 |
|
| 238 |
#[inline(always)] |
| 239 |
fn w_axis(&self) -> &v128 { |
| 240 |
&self.w_axis |
| 241 |
} |
| 242 |
|
| 243 |
#[inline] |
| 244 |
fn determinant(&self) -> f32 { |
| 245 |
// Based on https://github.com/g-truc/glm `glm_mat4_determinant` |
| 246 |
let swp2a = i32x4_shuffle::<2, 1, 1, 0>(self.z_axis, self.z_axis); |
| 247 |
let swp3a = i32x4_shuffle::<3, 3, 2, 3>(self.w_axis, self.w_axis); |
| 248 |
let swp2b = i32x4_shuffle::<3, 3, 2, 3>(self.z_axis, self.z_axis); |
| 249 |
let swp3b = i32x4_shuffle::<2, 1, 2, 0>(self.w_axis, self.w_axis); |
| 250 |
let swp2c = i32x4_shuffle::<2, 1, 0, 0>(self.z_axis, self.z_axis); |
| 251 |
let swp3c = i32x4_shuffle::<0, 0, 2, 1>(self.w_axis, self.w_axis); |
| 252 |
|
| 253 |
let mula = f32x4_mul(swp2a, swp3a); |
| 254 |
let mulb = f32x4_mul(swp2b, swp3b); |
| 255 |
let mulc = f32x4_mul(swp2c, swp3c); |
| 256 |
let sube = f32x4_sub(mula, mulb); |
| 257 |
let subf = f32x4_sub(i32x4_shuffle::<6, 7, 2, 3>(mulc, mulc), mulc); |
| 258 |
|
| 259 |
let subfaca = i32x4_shuffle::<0, 0, 1, 2>(sube, sube); |
| 260 |
let swpfaca = i32x4_shuffle::<1, 0, 0, 0>(self.y_axis, self.y_axis); |
| 261 |
let mulfaca = f32x4_mul(swpfaca, subfaca); |
| 262 |
|
| 263 |
let subtmpb = i32x4_shuffle::<1, 3, 4, 4>(sube, subf); |
| 264 |
let subfacb = i32x4_shuffle::<0, 1, 1, 3>(subtmpb, subtmpb); |
| 265 |
let swpfacb = i32x4_shuffle::<2, 2, 1, 1>(self.y_axis, self.y_axis); |
| 266 |
let mulfacb = f32x4_mul(swpfacb, subfacb); |
| 267 |
|
| 268 |
let subres = f32x4_sub(mulfaca, mulfacb); |
| 269 |
let subtmpc = i32x4_shuffle::<2, 2, 4, 5>(sube, subf); |
| 270 |
let subfacc = i32x4_shuffle::<0, 2, 3, 3>(subtmpc, subtmpc); |
| 271 |
let swpfacc = i32x4_shuffle::<3, 3, 3, 2>(self.y_axis, self.y_axis); |
| 272 |
let mulfacc = f32x4_mul(swpfacc, subfacc); |
| 273 |
|
| 274 |
let addres = f32x4_add(subres, mulfacc); |
| 275 |
let detcof = f32x4_mul(addres, f32x4(1.0, -1.0, 1.0, -1.0)); |
| 276 |
|
| 277 |
Vector4::dot(self.x_axis, detcof) |
| 278 |
} |
| 279 |
|
| 280 |
#[inline] |
| 281 |
fn transpose(&self) -> Self { |
| 282 |
// Based on https://github.com/microsoft/DirectXMath `XMMatrixTranspose` |
| 283 |
let tmp0 = i32x4_shuffle::<0, 1, 4, 5>(self.x_axis, self.y_axis); |
| 284 |
let tmp1 = i32x4_shuffle::<2, 3, 6, 7>(self.x_axis, self.y_axis); |
| 285 |
let tmp2 = i32x4_shuffle::<0, 1, 4, 5>(self.z_axis, self.w_axis); |
| 286 |
let tmp3 = i32x4_shuffle::<2, 3, 6, 7>(self.z_axis, self.w_axis); |
| 287 |
|
| 288 |
Self { |
| 289 |
x_axis: i32x4_shuffle::<0, 2, 4, 6>(tmp0, tmp2), |
| 290 |
y_axis: i32x4_shuffle::<1, 3, 5, 7>(tmp0, tmp2), |
| 291 |
z_axis: i32x4_shuffle::<0, 2, 4, 6>(tmp1, tmp3), |
| 292 |
w_axis: i32x4_shuffle::<1, 3, 5, 7>(tmp1, tmp3), |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
impl FloatMatrix4x4<f32, v128> for Columns4<v128> { |
| 298 |
type SIMDVector3 = v128; |
| 299 |
|
| 300 |
fn inverse(&self) -> Self { |
| 301 |
// Based on https://github.com/g-truc/glm `glm_mat4_inverse` |
| 302 |
let fac0 = { |
| 303 |
let swp0a = i32x4_shuffle::<3, 3, 7, 7>(self.w_axis, self.z_axis); |
| 304 |
let swp0b = i32x4_shuffle::<2, 2, 6, 6>(self.w_axis, self.z_axis); |
| 305 |
|
| 306 |
let swp00 = i32x4_shuffle::<2, 2, 6, 6>(self.z_axis, self.y_axis); |
| 307 |
let swp01 = i32x4_shuffle::<0, 0, 4, 6>(swp0a, swp0a); |
| 308 |
let swp02 = i32x4_shuffle::<0, 0, 4, 6>(swp0b, swp0b); |
| 309 |
let swp03 = i32x4_shuffle::<3, 3, 7, 7>(self.z_axis, self.y_axis); |
| 310 |
|
| 311 |
let mul00 = f32x4_mul(swp00, swp01); |
| 312 |
let mul01 = f32x4_mul(swp02, swp03); |
| 313 |
f32x4_sub(mul00, mul01) |
| 314 |
}; |
| 315 |
let fac1 = { |
| 316 |
let swp0a = i32x4_shuffle::<3, 3, 7, 7>(self.w_axis, self.z_axis); |
| 317 |
let swp0b = i32x4_shuffle::<1, 1, 5, 5>(self.w_axis, self.z_axis); |
| 318 |
|
| 319 |
let swp00 = i32x4_shuffle::<1, 1, 5, 5>(self.z_axis, self.y_axis); |
| 320 |
let swp01 = i32x4_shuffle::<0, 0, 4, 6>(swp0a, swp0a); |
| 321 |
let swp02 = i32x4_shuffle::<0, 0, 4, 6>(swp0b, swp0b); |
| 322 |
let swp03 = i32x4_shuffle::<3, 3, 7, 7>(self.z_axis, self.y_axis); |
| 323 |
|
| 324 |
let mul00 = f32x4_mul(swp00, swp01); |
| 325 |
let mul01 = f32x4_mul(swp02, swp03); |
| 326 |
f32x4_sub(mul00, mul01) |
| 327 |
}; |
| 328 |
let fac2 = { |
| 329 |
let swp0a = i32x4_shuffle::<2, 2, 6, 6>(self.w_axis, self.z_axis); |
| 330 |
let swp0b = i32x4_shuffle::<1, 1, 5, 5>(self.w_axis, self.z_axis); |
| 331 |
|
| 332 |
let swp00 = i32x4_shuffle::<1, 1, 5, 5>(self.z_axis, self.y_axis); |
| 333 |
let swp01 = i32x4_shuffle::<0, 0, 4, 6>(swp0a, swp0a); |
| 334 |
let swp02 = i32x4_shuffle::<0, 0, 4, 6>(swp0b, swp0b); |
| 335 |
let swp03 = i32x4_shuffle::<2, 2, 6, 6>(self.z_axis, self.y_axis); |
| 336 |
|
| 337 |
let mul00 = f32x4_mul(swp00, swp01); |
| 338 |
let mul01 = f32x4_mul(swp02, swp03); |
| 339 |
f32x4_sub(mul00, mul01) |
| 340 |
}; |
| 341 |
let fac3 = { |
| 342 |
let swp0a = i32x4_shuffle::<3, 3, 7, 7>(self.w_axis, self.z_axis); |
| 343 |
let swp0b = i32x4_shuffle::<0, 0, 4, 4>(self.w_axis, self.z_axis); |
| 344 |
|
| 345 |
let swp00 = i32x4_shuffle::<0, 0, 4, 4>(self.z_axis, self.y_axis); |
| 346 |
let swp01 = i32x4_shuffle::<0, 0, 4, 6>(swp0a, swp0a); |
| 347 |
let swp02 = i32x4_shuffle::<0, 0, 4, 6>(swp0b, swp0b); |
| 348 |
let swp03 = i32x4_shuffle::<3, 3, 7, 7>(self.z_axis, self.y_axis); |
| 349 |
|
| 350 |
let mul00 = f32x4_mul(swp00, swp01); |
| 351 |
let mul01 = f32x4_mul(swp02, swp03); |
| 352 |
f32x4_sub(mul00, mul01) |
| 353 |
}; |
| 354 |
let fac4 = { |
| 355 |
let swp0a = i32x4_shuffle::<2, 2, 6, 6>(self.w_axis, self.z_axis); |
| 356 |
let swp0b = i32x4_shuffle::<0, 0, 4, 4>(self.w_axis, self.z_axis); |
| 357 |
|
| 358 |
let swp00 = i32x4_shuffle::<0, 0, 4, 4>(self.z_axis, self.y_axis); |
| 359 |
let swp01 = i32x4_shuffle::<0, 0, 4, 6>(swp0a, swp0a); |
| 360 |
let swp02 = i32x4_shuffle::<0, 0, 4, 6>(swp0b, swp0b); |
| 361 |
let swp03 = i32x4_shuffle::<2, 2, 6, 6>(self.z_axis, self.y_axis); |
| 362 |
|
| 363 |
let mul00 = f32x4_mul(swp00, swp01); |
| 364 |
let mul01 = f32x4_mul(swp02, swp03); |
| 365 |
f32x4_sub(mul00, mul01) |
| 366 |
}; |
| 367 |
let fac5 = { |
| 368 |
let swp0a = i32x4_shuffle::<1, 1, 5, 5>(self.w_axis, self.z_axis); |
| 369 |
let swp0b = i32x4_shuffle::<0, 0, 4, 4>(self.w_axis, self.z_axis); |
| 370 |
|
| 371 |
let swp00 = i32x4_shuffle::<0, 0, 4, 4>(self.z_axis, self.y_axis); |
| 372 |
let swp01 = i32x4_shuffle::<0, 0, 4, 6>(swp0a, swp0a); |
| 373 |
let swp02 = i32x4_shuffle::<0, 0, 4, 6>(swp0b, swp0b); |
| 374 |
let swp03 = i32x4_shuffle::<1, 1, 5, 5>(self.z_axis, self.y_axis); |
| 375 |
|
| 376 |
let mul00 = f32x4_mul(swp00, swp01); |
| 377 |
let mul01 = f32x4_mul(swp02, swp03); |
| 378 |
f32x4_sub(mul00, mul01) |
| 379 |
}; |
| 380 |
let sign_a = f32x4(-1.0, 1.0, -1.0, 1.0); |
| 381 |
let sign_b = f32x4(1.0, -1.0, 1.0, -1.0); |
| 382 |
|
| 383 |
let temp0 = i32x4_shuffle::<0, 0, 4, 4>(self.y_axis, self.x_axis); |
| 384 |
let vec0 = i32x4_shuffle::<0, 2, 6, 6>(temp0, temp0); |
| 385 |
|
| 386 |
let temp1 = i32x4_shuffle::<1, 1, 5, 5>(self.y_axis, self.x_axis); |
| 387 |
let vec1 = i32x4_shuffle::<0, 2, 6, 6>(temp1, temp1); |
| 388 |
|
| 389 |
let temp2 = i32x4_shuffle::<2, 2, 6, 6>(self.y_axis, self.x_axis); |
| 390 |
let vec2 = i32x4_shuffle::<0, 2, 6, 6>(temp2, temp2); |
| 391 |
|
| 392 |
let temp3 = i32x4_shuffle::<3, 3, 7, 7>(self.y_axis, self.x_axis); |
| 393 |
let vec3 = i32x4_shuffle::<0, 2, 6, 6>(temp3, temp3); |
| 394 |
|
| 395 |
let mul00 = f32x4_mul(vec1, fac0); |
| 396 |
let mul01 = f32x4_mul(vec2, fac1); |
| 397 |
let mul02 = f32x4_mul(vec3, fac2); |
| 398 |
let sub00 = f32x4_sub(mul00, mul01); |
| 399 |
let add00 = f32x4_add(sub00, mul02); |
| 400 |
let inv0 = f32x4_mul(sign_b, add00); |
| 401 |
|
| 402 |
let mul03 = f32x4_mul(vec0, fac0); |
| 403 |
let mul04 = f32x4_mul(vec2, fac3); |
| 404 |
let mul05 = f32x4_mul(vec3, fac4); |
| 405 |
let sub01 = f32x4_sub(mul03, mul04); |
| 406 |
let add01 = f32x4_add(sub01, mul05); |
| 407 |
let inv1 = f32x4_mul(sign_a, add01); |
| 408 |
|
| 409 |
let mul06 = f32x4_mul(vec0, fac1); |
| 410 |
let mul07 = f32x4_mul(vec1, fac3); |
| 411 |
let mul08 = f32x4_mul(vec3, fac5); |
| 412 |
let sub02 = f32x4_sub(mul06, mul07); |
| 413 |
let add02 = f32x4_add(sub02, mul08); |
| 414 |
let inv2 = f32x4_mul(sign_b, add02); |
| 415 |
|
| 416 |
let mul09 = f32x4_mul(vec0, fac2); |
| 417 |
let mul10 = f32x4_mul(vec1, fac4); |
| 418 |
let mul11 = f32x4_mul(vec2, fac5); |
| 419 |
let sub03 = f32x4_sub(mul09, mul10); |
| 420 |
let add03 = f32x4_add(sub03, mul11); |
| 421 |
let inv3 = f32x4_mul(sign_a, add03); |
| 422 |
|
| 423 |
let row0 = i32x4_shuffle::<0, 0, 4, 4>(inv0, inv1); |
| 424 |
let row1 = i32x4_shuffle::<0, 0, 4, 4>(inv2, inv3); |
| 425 |
let row2 = i32x4_shuffle::<0, 2, 4, 6>(row0, row1); |
| 426 |
|
| 427 |
let dot0 = Vector4::dot(self.x_axis, row2); |
| 428 |
glam_assert!(dot0 != 0.0); |
| 429 |
|
| 430 |
let rcp0 = f32x4_splat(dot0.recip()); |
| 431 |
|
| 432 |
Self { |
| 433 |
x_axis: f32x4_mul(inv0, rcp0), |
| 434 |
y_axis: f32x4_mul(inv1, rcp0), |
| 435 |
z_axis: f32x4_mul(inv2, rcp0), |
| 436 |
w_axis: f32x4_mul(inv3, rcp0), |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
#[inline(always)] |
| 441 |
fn transform_point3(&self, other: XYZ<f32>) -> XYZ<f32> { |
| 442 |
self.x_axis |
| 443 |
.mul_scalar(other.x) |
| 444 |
.add(self.y_axis.mul_scalar(other.y)) |
| 445 |
.add(self.z_axis.mul_scalar(other.z)) |
| 446 |
.add(self.w_axis) |
| 447 |
.into() |
| 448 |
} |
| 449 |
|
| 450 |
#[inline(always)] |
| 451 |
fn transform_vector3(&self, other: XYZ<f32>) -> XYZ<f32> { |
| 452 |
self.x_axis |
| 453 |
.mul_scalar(other.x) |
| 454 |
.add(self.y_axis.mul_scalar(other.y)) |
| 455 |
.add(self.z_axis.mul_scalar(other.z)) |
| 456 |
.into() |
| 457 |
} |
| 458 |
|
| 459 |
#[inline] |
| 460 |
fn transform_float4_as_point3(&self, other: v128) -> v128 { |
| 461 |
let mut res = self.x_axis.mul(Vector4::splat_x(other)); |
| 462 |
res = self.y_axis.mul_add(Vector4::splat_y(other), res); |
| 463 |
res = self.z_axis.mul_add(Vector4::splat_z(other), res); |
| 464 |
res = self.w_axis.add(res); |
| 465 |
res |
| 466 |
} |
| 467 |
|
| 468 |
#[inline] |
| 469 |
fn transform_float4_as_vector3(&self, other: v128) -> v128 { |
| 470 |
let mut res = self.x_axis.mul(Vector4::splat_x(other)); |
| 471 |
res = self.y_axis.mul_add(Vector4::splat_y(other), res); |
| 472 |
res = self.z_axis.mul_add(Vector4::splat_z(other), res); |
| 473 |
res |
| 474 |
} |
| 475 |
|
| 476 |
#[inline] |
| 477 |
fn project_float4_as_point3(&self, other: v128) -> v128 { |
| 478 |
let mut res = self.x_axis.mul(Vector4::splat_x(other)); |
| 479 |
res = self.y_axis.mul_add(Vector4::splat_y(other), res); |
| 480 |
res = self.z_axis.mul_add(Vector4::splat_z(other), res); |
| 481 |
res = self.w_axis.add(res); |
| 482 |
res = res.mul(res.splat_w().recip()); |
| 483 |
res |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
impl ProjectionMatrix<f32, v128> for Columns4<v128> {} |
| 488 |
|
| 489 |
impl From<Columns3<XYZ<f32>>> for Columns3<v128> { |
| 490 |
#[inline(always)] |
| 491 |
fn from(v: Columns3<XYZ<f32>>) -> Columns3<v128> { |
| 492 |
Self { |
| 493 |
x_axis: v.x_axis.into(), |
| 494 |
y_axis: v.y_axis.into(), |
| 495 |
z_axis: v.z_axis.into(), |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
impl From<Columns3<v128>> for Columns3<XYZ<f32>> { |
| 501 |
#[inline(always)] |
| 502 |
fn from(v: Columns3<v128>) -> Columns3<XYZ<f32>> { |
| 503 |
Self { |
| 504 |
x_axis: v.x_axis.into(), |
| 505 |
y_axis: v.y_axis.into(), |
| 506 |
z_axis: v.z_axis.into(), |
| 507 |
} |
| 508 |
} |
| 509 |
} |