|
| 1 | +// |
| 2 | +// Catalyst.h |
| 3 | +// SGPlayer |
| 4 | +// |
| 5 | +// Created by Steven Troughton-Smith on 31/08/2019. |
| 6 | +// Copyright © 2019 single. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#ifndef Catalyst_h |
| 10 | +#define Catalyst_h |
| 11 | + |
| 12 | +#define GLK_INLINE static __inline__ |
| 13 | + |
| 14 | +#if defined(__STRICT_ANSI__) |
| 15 | +struct _GLKMatrix4 |
| 16 | +{ |
| 17 | + float m[16]; |
| 18 | +} __attribute__((aligned(16))); |
| 19 | +typedef struct _GLKMatrix4 GLKMatrix4; |
| 20 | +#else |
| 21 | +union _GLKMatrix4 |
| 22 | +{ |
| 23 | + struct |
| 24 | + { |
| 25 | + float m00, m01, m02, m03; |
| 26 | + float m10, m11, m12, m13; |
| 27 | + float m20, m21, m22, m23; |
| 28 | + float m30, m31, m32, m33; |
| 29 | + }; |
| 30 | + float m[16]; |
| 31 | +} __attribute__((aligned(16))); |
| 32 | +typedef union _GLKMatrix4 GLKMatrix4; |
| 33 | +#endif |
| 34 | + |
| 35 | +GLK_INLINE GLKMatrix4 GLKMatrix4Multiply(GLKMatrix4 matrixLeft, GLKMatrix4 matrixRight) |
| 36 | +{ |
| 37 | +#if defined(GLK_SSE3_INTRINSICS) |
| 38 | + |
| 39 | + const __m128 l0 = _mm_load_ps(&matrixLeft.m[0]); |
| 40 | + const __m128 l1 = _mm_load_ps(&matrixLeft.m[4]); |
| 41 | + const __m128 l2 = _mm_load_ps(&matrixLeft.m[8]); |
| 42 | + const __m128 l3 = _mm_load_ps(&matrixLeft.m[12]); |
| 43 | + |
| 44 | + const __m128 r0 = _mm_load_ps(&matrixRight.m[0]); |
| 45 | + const __m128 r1 = _mm_load_ps(&matrixRight.m[4]); |
| 46 | + const __m128 r2 = _mm_load_ps(&matrixRight.m[8]); |
| 47 | + const __m128 r3 = _mm_load_ps(&matrixRight.m[12]); |
| 48 | + |
| 49 | + const __m128 m0 = l0 * _mm_shuffle_ps(r0, r0, _MM_SHUFFLE(0, 0, 0, 0)) |
| 50 | + + l1 * _mm_shuffle_ps(r0, r0, _MM_SHUFFLE(1, 1, 1, 1)) |
| 51 | + + l2 * _mm_shuffle_ps(r0, r0, _MM_SHUFFLE(2, 2, 2, 2)) |
| 52 | + + l3 * _mm_shuffle_ps(r0, r0, _MM_SHUFFLE(3, 3, 3, 3)); |
| 53 | + |
| 54 | + const __m128 m1 = l0 * _mm_shuffle_ps(r1, r1, _MM_SHUFFLE(0, 0, 0, 0)) |
| 55 | + + l1 * _mm_shuffle_ps(r1, r1, _MM_SHUFFLE(1, 1, 1, 1)) |
| 56 | + + l2 * _mm_shuffle_ps(r1, r1, _MM_SHUFFLE(2, 2, 2, 2)) |
| 57 | + + l3 * _mm_shuffle_ps(r1, r1, _MM_SHUFFLE(3, 3, 3, 3)); |
| 58 | + |
| 59 | + const __m128 m2 = l0 * _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(0, 0, 0, 0)) |
| 60 | + + l1 * _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(1, 1, 1, 1)) |
| 61 | + + l2 * _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(2, 2, 2, 2)) |
| 62 | + + l3 * _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(3, 3, 3, 3)); |
| 63 | + |
| 64 | + const __m128 m3 = l0 * _mm_shuffle_ps(r3, r3, _MM_SHUFFLE(0, 0, 0, 0)) |
| 65 | + + l1 * _mm_shuffle_ps(r3, r3, _MM_SHUFFLE(1, 1, 1, 1)) |
| 66 | + + l2 * _mm_shuffle_ps(r3, r3, _MM_SHUFFLE(2, 2, 2, 2)) |
| 67 | + + l3 * _mm_shuffle_ps(r3, r3, _MM_SHUFFLE(3, 3, 3, 3)); |
| 68 | + |
| 69 | + GLKMatrix4 m; |
| 70 | + _mm_store_ps(&m.m[0], m0); |
| 71 | + _mm_store_ps(&m.m[4], m1); |
| 72 | + _mm_store_ps(&m.m[8], m2); |
| 73 | + _mm_store_ps(&m.m[12], m3); |
| 74 | + return m; |
| 75 | + |
| 76 | +#else |
| 77 | + GLKMatrix4 m; |
| 78 | + |
| 79 | + m.m[0] = matrixLeft.m[0] * matrixRight.m[0] + matrixLeft.m[4] * matrixRight.m[1] + matrixLeft.m[8] * matrixRight.m[2] + matrixLeft.m[12] * matrixRight.m[3]; |
| 80 | + m.m[4] = matrixLeft.m[0] * matrixRight.m[4] + matrixLeft.m[4] * matrixRight.m[5] + matrixLeft.m[8] * matrixRight.m[6] + matrixLeft.m[12] * matrixRight.m[7]; |
| 81 | + m.m[8] = matrixLeft.m[0] * matrixRight.m[8] + matrixLeft.m[4] * matrixRight.m[9] + matrixLeft.m[8] * matrixRight.m[10] + matrixLeft.m[12] * matrixRight.m[11]; |
| 82 | + m.m[12] = matrixLeft.m[0] * matrixRight.m[12] + matrixLeft.m[4] * matrixRight.m[13] + matrixLeft.m[8] * matrixRight.m[14] + matrixLeft.m[12] * matrixRight.m[15]; |
| 83 | + |
| 84 | + m.m[1] = matrixLeft.m[1] * matrixRight.m[0] + matrixLeft.m[5] * matrixRight.m[1] + matrixLeft.m[9] * matrixRight.m[2] + matrixLeft.m[13] * matrixRight.m[3]; |
| 85 | + m.m[5] = matrixLeft.m[1] * matrixRight.m[4] + matrixLeft.m[5] * matrixRight.m[5] + matrixLeft.m[9] * matrixRight.m[6] + matrixLeft.m[13] * matrixRight.m[7]; |
| 86 | + m.m[9] = matrixLeft.m[1] * matrixRight.m[8] + matrixLeft.m[5] * matrixRight.m[9] + matrixLeft.m[9] * matrixRight.m[10] + matrixLeft.m[13] * matrixRight.m[11]; |
| 87 | + m.m[13] = matrixLeft.m[1] * matrixRight.m[12] + matrixLeft.m[5] * matrixRight.m[13] + matrixLeft.m[9] * matrixRight.m[14] + matrixLeft.m[13] * matrixRight.m[15]; |
| 88 | + |
| 89 | + m.m[2] = matrixLeft.m[2] * matrixRight.m[0] + matrixLeft.m[6] * matrixRight.m[1] + matrixLeft.m[10] * matrixRight.m[2] + matrixLeft.m[14] * matrixRight.m[3]; |
| 90 | + m.m[6] = matrixLeft.m[2] * matrixRight.m[4] + matrixLeft.m[6] * matrixRight.m[5] + matrixLeft.m[10] * matrixRight.m[6] + matrixLeft.m[14] * matrixRight.m[7]; |
| 91 | + m.m[10] = matrixLeft.m[2] * matrixRight.m[8] + matrixLeft.m[6] * matrixRight.m[9] + matrixLeft.m[10] * matrixRight.m[10] + matrixLeft.m[14] * matrixRight.m[11]; |
| 92 | + m.m[14] = matrixLeft.m[2] * matrixRight.m[12] + matrixLeft.m[6] * matrixRight.m[13] + matrixLeft.m[10] * matrixRight.m[14] + matrixLeft.m[14] * matrixRight.m[15]; |
| 93 | + |
| 94 | + m.m[3] = matrixLeft.m[3] * matrixRight.m[0] + matrixLeft.m[7] * matrixRight.m[1] + matrixLeft.m[11] * matrixRight.m[2] + matrixLeft.m[15] * matrixRight.m[3]; |
| 95 | + m.m[7] = matrixLeft.m[3] * matrixRight.m[4] + matrixLeft.m[7] * matrixRight.m[5] + matrixLeft.m[11] * matrixRight.m[6] + matrixLeft.m[15] * matrixRight.m[7]; |
| 96 | + m.m[11] = matrixLeft.m[3] * matrixRight.m[8] + matrixLeft.m[7] * matrixRight.m[9] + matrixLeft.m[11] * matrixRight.m[10] + matrixLeft.m[15] * matrixRight.m[11]; |
| 97 | + m.m[15] = matrixLeft.m[3] * matrixRight.m[12] + matrixLeft.m[7] * matrixRight.m[13] + matrixLeft.m[11] * matrixRight.m[14] + matrixLeft.m[15] * matrixRight.m[15]; |
| 98 | + |
| 99 | + return m; |
| 100 | +#endif |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +extern const GLKMatrix4 GLKMatrix4Identity; |
| 105 | + |
| 106 | +GLK_INLINE GLKMatrix4 GLKMatrix4MakeZRotation(float radians) |
| 107 | +{ |
| 108 | + float cos = cosf(radians); |
| 109 | + float sin = sinf(radians); |
| 110 | + |
| 111 | + GLKMatrix4 m = { cos, sin, 0.0f, 0.0f, |
| 112 | + -sin, cos, 0.0f, 0.0f, |
| 113 | + 0.0f, 0.0f, 1.0f, 0.0f, |
| 114 | + 0.0f, 0.0f, 0.0f, 1.0f }; |
| 115 | + |
| 116 | + return m; |
| 117 | +} |
| 118 | + |
| 119 | +GLK_INLINE GLKMatrix4 GLKMatrix4RotateZ(GLKMatrix4 matrix, float radians); |
| 120 | +GLK_INLINE GLKMatrix4 GLKMatrix4RotateZ(GLKMatrix4 matrix, float radians) |
| 121 | +{ |
| 122 | + GLKMatrix4 rm = GLKMatrix4MakeZRotation(radians); |
| 123 | + return GLKMatrix4Multiply(matrix, rm); |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +GLK_INLINE GLKMatrix4 GLKMatrix4Transpose(GLKMatrix4 matrix) |
| 128 | +{ |
| 129 | + GLKMatrix4 m = { matrix.m[0], matrix.m[4], matrix.m[8], matrix.m[12], |
| 130 | + matrix.m[1], matrix.m[5], matrix.m[9], matrix.m[13], |
| 131 | + matrix.m[2], matrix.m[6], matrix.m[10], matrix.m[14], |
| 132 | + matrix.m[3], matrix.m[7], matrix.m[11], matrix.m[15] }; |
| 133 | + return m; |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +GLK_INLINE GLKMatrix4 GLKMatrix4MakeYRotation(float radians) |
| 138 | +{ |
| 139 | + float cos = cosf(radians); |
| 140 | + float sin = sinf(radians); |
| 141 | + |
| 142 | + GLKMatrix4 m = { cos, 0.0f, -sin, 0.0f, |
| 143 | + 0.0f, 1.0f, 0.0f, 0.0f, |
| 144 | + sin, 0.0f, cos, 0.0f, |
| 145 | + 0.0f, 0.0f, 0.0f, 1.0f }; |
| 146 | + |
| 147 | + return m; |
| 148 | +} |
| 149 | + |
| 150 | +GLK_INLINE GLKMatrix4 GLKMatrix4RotateY(GLKMatrix4 matrix, float radians) |
| 151 | +{ |
| 152 | + GLKMatrix4 rm = GLKMatrix4MakeYRotation(radians); |
| 153 | + return GLKMatrix4Multiply(matrix, rm); |
| 154 | +} |
| 155 | + |
| 156 | +GLK_INLINE float GLKMathDegreesToRadians(float degrees) { return degrees * (M_PI / 180); }; |
| 157 | + |
| 158 | + |
| 159 | +#endif /* Catalyst_h */ |
0 commit comments