From ac3601254554e6fe310a26645376a213f599878b Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 14:07:23 +0100 Subject: [PATCH 1/8] added some matrix/math funcs --- src/engine/include/models/math/matrix.hpp | 56 ++++++++++++++- src/engine/include/utils/math.hpp | 2 + src/engine/models/math/matrix.cpp | 76 +++++++++++++++++--- src/engine/utils/math.cpp | 84 +++++++++++++++++++++++ 4 files changed, 207 insertions(+), 11 deletions(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 366794b..5e13bd7 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -26,14 +26,64 @@ public: float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44); Matrix(const Matrix &v); - Matrix operator*(Matrix &v); + Matrix operator*(const Matrix &v); + void operator*=(const Matrix &v); Matrix(); ~Matrix(); void lookAt(Vector3 &t_up, Vector3 &t_position, Vector3 &t_target); void identity(); - void makeZRotation(float t_radians); - void translate(float t_x, float t_y, float t_z); + inline void unit() { identity(); }; + + // Translation + + void translate(const Vector3 &t_val); + + inline void translation(const Vector3 &t_val) + { + identity(); + translate(t_val); + } + + // Rotation + + void rotateX(const float &t_radians); + void rotateY(const float &t_radians); + void rotateZ(const float &t_radians); + + inline void rotationX(const float &t_radians) + { + identity(); + rotateX(t_radians); + } + + inline void rotationY(const float &t_radians) + { + identity(); + rotateY(t_radians); + } + + inline void rotationZ(const float &t_radians) + { + identity(); + rotateZ(t_radians); + } + + inline void rotate(const Vector3 &t_val) + { + rotateX(t_val.x); + rotateY(t_val.y); + rotateX(t_val.z); + } + + inline void rotation(const Vector3 &t_val) + { + identity(); + rotate(t_val); + } + + // + void setPerspective(ScreenSettings &screen); const void print() const; }; diff --git a/src/engine/include/utils/math.hpp b/src/engine/include/utils/math.hpp index 4c5f115..92af50c 100644 --- a/src/engine/include/utils/math.hpp +++ b/src/engine/include/utils/math.hpp @@ -28,6 +28,8 @@ public: const static float PI = 3.1415926535897932384626433832795F; const static float HALF_PI = 1.5707963267948966192313216916398F; static float cos(float x); + static float asin(float x); + static float mod(float x, float y); static inline float sin(float x) { return cos(x - HALF_PI); }; static float sqrt(float x); static float invSqrt(float x); diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index 7aaaf37..ec927a1 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -84,17 +84,41 @@ void Matrix::identity() : "r"(this->data)); } -void Matrix::translate(float x, float y, float z) +void Matrix::translate(const Vector3 &t_val) { - this->identity(); - this->data[(3 << 2) + 0] = x; // 3,0 - this->data[(3 << 2) + 1] = y; // 3,1 - this->data[(3 << 2) + 2] = z; // 3,2 + this->data[12] += t_val.x; // 3,0 + this->data[13] += t_val.y; // 3,1 + this->data[14] += t_val.z; // 3,2 } -void Matrix::makeZRotation(float t_radians) +void Matrix::rotateX(const float &t_radians) { - this->identity(); + Matrix temp = Matrix(); + temp.identity(); + float c = Math::cos(t_radians); + float s = Math::sin(t_radians); + this->data[5] = c; // 1,1 + this->data[6] = s; // 1,2 + this->data[9] = -s; // 2,1 + this->data[10] = c; // 2,2 +} + +void Matrix::rotateY(const float &t_radians) +{ + Matrix temp = Matrix(); + temp.identity(); + float c = Math::cos(t_radians); + float s = Math::sin(t_radians); + this->data[0] = c; // 0,0 + this->data[2] = -s; // 0,3 + this->data[8] = s; // 2,0 + this->data[10] = c; // 2,2 +} + +void Matrix::rotateZ(const float &t_radians) +{ + Matrix temp = Matrix(); + temp.identity(); float c = Math::cos(t_radians); float s = Math::sin(t_radians); this->data[0] = c; // 0,0 @@ -103,7 +127,7 @@ void Matrix::makeZRotation(float t_radians) this->data[5] = c; // 1,1 } -Matrix Matrix::operator*(Matrix &t) +Matrix Matrix::operator*(const Matrix &t) { Matrix result; asm volatile( @@ -141,6 +165,42 @@ Matrix Matrix::operator*(Matrix &t) return result; } +void Matrix::operator*=(const Matrix &t) +{ + asm volatile( + "lqc2 vf1, 0x00(%1) \n\t" + "lqc2 vf2, 0x10(%1) \n\t" + "lqc2 vf3, 0x20(%1) \n\t" + "lqc2 vf4, 0x30(%1) \n\t" + "lqc2 vf5, 0x00(%2) \n\t" + "lqc2 vf6, 0x10(%2) \n\t" + "lqc2 vf7, 0x20(%2) \n\t" + "lqc2 vf8, 0x30(%2) \n\t" + "vmulax.xyzw ACC, vf5, vf1 \n\t" + "vmadday.xyzw ACC, vf6, vf1 \n\t" + "vmaddaz.xyzw ACC, vf7, vf1 \n\t" + "vmaddw.xyzw vf1, vf8, vf1 \n\t" + "vmulax.xyzw ACC, vf5, vf2 \n\t" + "vmadday.xyzw ACC, vf6, vf2 \n\t" + "vmaddaz.xyzw ACC, vf7, vf2 \n\t" + "vmaddw.xyzw vf2, vf8, vf2 \n\t" + "vmulax.xyzw ACC, vf5, vf3 \n\t" + "vmadday.xyzw ACC, vf6, vf3 \n\t" + "vmaddaz.xyzw ACC, vf7, vf3 \n\t" + "vmaddw.xyzw vf3, vf8, vf3 \n\t" + "vmulax.xyzw ACC, vf5, vf4 \n\t" + "vmadday.xyzw ACC, vf6, vf4 \n\t" + "vmaddaz.xyzw ACC, vf7, vf4 \n\t" + "vmaddw.xyzw vf4, vf8, vf4 \n\t" + "sqc2 vf1, 0x00(%0) \n\t" + "sqc2 vf2, 0x10(%0) \n\t" + "sqc2 vf3, 0x20(%0) \n\t" + "sqc2 vf4, 0x30(%0) \n\t" + : + : "r"(this->data), "r"(this->data), "r"(t.data) + : "memory"); +} + /** Create empty matrix */ Matrix::Matrix() { diff --git a/src/engine/utils/math.cpp b/src/engine/utils/math.cpp index ddf960f..c273467 100644 --- a/src/engine/utils/math.cpp +++ b/src/engine/utils/math.cpp @@ -99,3 +99,87 @@ void manyVec3ToNative(VECTOR *o_result, Vector3 *t_vec, int t_amount, float t_fo for (int i = 0; i < t_amount; i++) vec3ToNative(o_result[i], t_vec[i], t_fourthVal); } + +static float asin(float x) +{ + float r; + asm volatile( + "lui $9, 0x3f00 \n\t" + ".set noreorder \n\t" + ".align 3 \n\t" + "abs.s %0, %1 \n\t" + "lui $8, 0xbe22 \n\t" + "mtc1 $9, $f1 \n\t" + "ori $8, $8, 0xf983 \n\t" + "mtc1 $8, $f8 \n\t" + "lui $9, 0x4b00 \n\t" + "mtc1 $9, $f3 \n\t" + "lui $8, 0x3f80 \n\t" + "mtc1 $8, $f2 \n\t" + "mula.s %0, $f8 \n\t" + "msuba.s $f3, $f2 \n\t" + "madda.s $f3, $f2 \n\t" + "lui $8, 0x40c9 \n\t" + "msuba.s %0, $f8 \n\t" + "ori $8, 0x0fdb \n\t" + "msub.s %0, $f1, $f2 \n\t" + "lui $9, 0xc225 \n\t" + "abs.s %0, %0 \n\t" + "lui $10, 0x3e80 \n\t" + "mtc1 $10, $f7 \n\t" + "ori $9, 0x5de1 \n\t" + "sub.s %0, %0, $f7 \n\t" + "lui $10, 0x42a3 \n\t" + "mtc1 $8, $f3 \n\t" + "ori $10, 0x3458 \n\t" + "mtc1 $9, $f4 \n\t" + "lui $8, 0xc299 \n\t" + "mtc1 $10, $f5 \n\t" + "ori $8, 0x2663 \n\t" + "mul.s $f8, %0, %0 \n\t" + "lui $9, 0x421e \n\t" + "mtc1 $8, $f6 \n\t" + "ori $9, 0xd7bb \n\t" + "mtc1 $9, $f7 \n\t" + "nop \n\t" + "mul.s $f1, %0, $f8 \n\t" + "mul.s $f9, $f8, $f8 \n\t" + "mula.s $f3, %0 \n\t" + "mul.s $f2, $f1, $f8 \n\t" + "madda.s $f4, $f1 \n\t" + "mul.s $f1, $f1, $f9 \n\t" + "mul.s %0, $f2, $f9 \n\t" + "madda.s $f5, $f2 \n\t" + "madda.s $f6, $f1 \n\t" + "madd.s %0, $f7, %0 \n\t" + ".set reorder \n\t" + : "=&f"(r) + : "f"(x) + : "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7", "$f8", "$f9", "$8", "$9", "$10"); + return r; +} + +static float mod(float x, float y) +{ + /* + * Portable fmod(x,y) implementation for systems + * that don't have it. Adapted from code found here: + * http://www.opensource.apple.com/source/python/python-3/python/Python/fmod.c + */ + float i, f; + + if (y == 0.0f) + { + return 0.0f; + } + + i = floorf(x / y); + f = x - i * y; + + if ((x < 0.0f) != (y < 0.0f)) + { + f = f - y; + } + + return f; +} From 8256efa7c8e4e5b206a820bcc0a9fb65a1625dde Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 14:10:50 +0100 Subject: [PATCH 2/8] switched zbuffer to 24bit mode --- src/engine/modules/renderer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 9b55597..02cadff 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -203,19 +203,19 @@ void Renderer::allocateBuffers(float t_screenW, float t_screenH) frameBuffers[0].width = (u16)t_screenW; frameBuffers[0].height = (u16)t_screenH; frameBuffers[0].mask = 0; - frameBuffers[0].psm = GS_PSM_32; + frameBuffers[0].psm = GS_PSM_24; frameBuffers[0].address = graph_vram_allocate((u16)t_screenW, (u16)t_screenH, frameBuffers[0].psm, GRAPH_ALIGN_PAGE); frameBuffers[1].width = (u16)t_screenW; frameBuffers[1].height = (u16)t_screenH; frameBuffers[1].mask = 0; - frameBuffers[1].psm = GS_PSM_32; + frameBuffers[1].psm = GS_PSM_24; frameBuffers[1].address = graph_vram_allocate((u16)t_screenW, (u16)t_screenH, frameBuffers[1].psm, GRAPH_ALIGN_PAGE); zBuffer.enable = DRAW_ENABLE; zBuffer.mask = 0; zBuffer.method = ZTEST_METHOD_GREATER_EQUAL; - zBuffer.zsm = GS_ZBUF_32; + zBuffer.zsm = GS_ZBUF_24; zBuffer.address = graph_vram_allocate((u16)t_screenW, (u16)t_screenH, zBuffer.zsm, GRAPH_ALIGN_PAGE); PRINT_LOG("Framebuffers, zBuffer set and allocated!"); From 3dd51607f0eab05be370c8ef31c87b2fe444ba69 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 14:12:30 +0100 Subject: [PATCH 3/8] refactored viewproj matrix calcs a bit --- src/engine/include/modules/vif_sender.hpp | 1 - src/engine/modules/vif_sender.cpp | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index 467e111..a3e1e36 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -41,7 +41,6 @@ private: packet2_t *currPacket; u8 context; packet2_t *matricesPacket __attribute__((aligned(64))); - MATRIX localWorld, localScreen; VECTOR position, rotation; u32 vertCount; }; diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 3135e09..771b05f 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -71,12 +71,12 @@ void VifSender::uploadMicroProgram() void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) { - vec3ToNative(position, t_position, 1.0F); - vec3ToNative(rotation, t_rotation, 1.0F); - create_local_world(localWorld, position, rotation); - create_local_screen(localScreen, localWorld, t_renderData.worldView->data, t_renderData.perspective->data); + Matrix viewProj = Matrix(); + viewProj.rotation(t_rotation); // model matrix + viewProj.translate(t_position); // model matrix + viewProj *= *t_renderData.worldView * *t_renderData.perspective; // viewproj = model * (view * projection) packet2_reset(matricesPacket, false); - packet2_utils_vu_add_unpack_data(matricesPacket, 0, &localScreen, 8, 0); + packet2_utils_vu_add_unpack_data(matricesPacket, 0, &viewProj.data, 8, 0); packet2_utils_vu_add_end_tag(matricesPacket); dma_channel_wait(DMA_CHANNEL_VIF1, 0); dma_channel_send_packet2(matricesPacket, DMA_CHANNEL_VIF1, 1); From b050e4643bcc26a07b4e7d488110283c4574b5aa Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 23:18:31 +0100 Subject: [PATCH 4/8] multiply hotfix! added scale, angle rotation --- src/engine/include/models/math/matrix.hpp | 4 +- src/engine/models/math/matrix.cpp | 167 ++++++++++++++-------- 2 files changed, 109 insertions(+), 62 deletions(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 5e13bd7..975486f 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -82,8 +82,10 @@ public: rotate(t_val); } - // + void rotationByAngle(const float &t_angle, const Vector3 &t_axis); + // + void setScale(const Vector3 &t_val); void setPerspective(ScreenSettings &screen); const void print() const; }; diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index ec927a1..3c1d330 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -84,6 +84,15 @@ void Matrix::identity() : "r"(this->data)); } +void Matrix::setScale(const Vector3 &t_val) +{ + this->identity(); + this->data[0] = t_val.x; + this->data[5] = t_val.y; + this->data[10] = t_val.z; + this->data[15] = 1.0F; +} + void Matrix::translate(const Vector3 &t_val) { this->data[12] += t_val.x; // 3,0 @@ -130,75 +139,79 @@ void Matrix::rotateZ(const float &t_radians) Matrix Matrix::operator*(const Matrix &t) { Matrix result; - asm volatile( - "lqc2 vf1, 0x00(%1) \n\t" - "lqc2 vf2, 0x10(%1) \n\t" - "lqc2 vf3, 0x20(%1) \n\t" - "lqc2 vf4, 0x30(%1) \n\t" - "lqc2 vf5, 0x00(%2) \n\t" - "lqc2 vf6, 0x10(%2) \n\t" - "lqc2 vf7, 0x20(%2) \n\t" - "lqc2 vf8, 0x30(%2) \n\t" - "vmulax.xyzw ACC, vf5, vf1 \n\t" - "vmadday.xyzw ACC, vf6, vf1 \n\t" - "vmaddaz.xyzw ACC, vf7, vf1 \n\t" - "vmaddw.xyzw vf1, vf8, vf1 \n\t" - "vmulax.xyzw ACC, vf5, vf2 \n\t" - "vmadday.xyzw ACC, vf6, vf2 \n\t" - "vmaddaz.xyzw ACC, vf7, vf2 \n\t" - "vmaddw.xyzw vf2, vf8, vf2 \n\t" - "vmulax.xyzw ACC, vf5, vf3 \n\t" - "vmadday.xyzw ACC, vf6, vf3 \n\t" - "vmaddaz.xyzw ACC, vf7, vf3 \n\t" - "vmaddw.xyzw vf3, vf8, vf3 \n\t" - "vmulax.xyzw ACC, vf5, vf4 \n\t" - "vmadday.xyzw ACC, vf6, vf4 \n\t" - "vmaddaz.xyzw ACC, vf7, vf4 \n\t" - "vmaddw.xyzw vf4, vf8, vf4 \n\t" - "sqc2 vf1, 0x00(%0) \n\t" - "sqc2 vf2, 0x10(%0) \n\t" - "sqc2 vf3, 0x20(%0) \n\t" - "sqc2 vf4, 0x30(%0) \n\t" + asm __volatile__( + "lqc2 vf4, 0x00(%1) \n\t" + "lqc2 vf5, 0x10(%1) \n\t" + "lqc2 vf6, 0x20(%1) \n\t" + "lqc2 vf7, 0x30(%1) \n\t" + + "lqc2 vf8, 0x00(%2) \n\t" + "lqc2 vf9, 0x10(%2) \n\t" + "lqc2 vf10, 0x20(%2) \n\t" + "lqc2 vf11, 0x30(%2) \n\t" + + "vmulax.xyzw ACC, vf4, vf8 \n\t" + "vmadday.xyzw ACC, vf5, vf8 \n\t" + "vmaddaz.xyzw ACC, vf6, vf8 \n\t" + "vmaddw.xyzw vf12, vf7, vf8 \n\t" + "vmulax.xyzw ACC, vf4, vf9 \n\t" + "vmadday.xyzw ACC, vf5, vf9 \n\t" + "vmaddaz.xyzw ACC, vf6, vf9 \n\t" + "vmaddw.xyzw vf13, vf7, vf9 \n\t" + "vmulax.xyzw ACC, vf4, vf10 \n\t" + "vmadday.xyzw ACC, vf5, vf10 \n\t" + "vmaddaz.xyzw ACC, vf6, vf10 \n\t" + "vmaddw.xyzw vf14, vf7, vf10 \n\t" + "vmulax.xyzw ACC, vf4, vf11 \n\t" + "vmadday.xyzw ACC, vf5, vf11 \n\t" + "vmaddaz.xyzw ACC, vf6, vf11 \n\t" + "vmaddw.xyzw vf15, vf7, vf11 \n\t" + + "sqc2 vf12, 0x00(%0) \n\t" + "sqc2 vf13, 0x10(%0) \n\t" + "sqc2 vf14, 0x20(%0) \n\t" + "sqc2 vf15, 0x30(%0) \n\t" : - : "r"(result.data), "r"(this->data), "r"(t.data) - : "memory"); + : "r"(result.data), "r"(this->data), "r"(t.data)); return result; } void Matrix::operator*=(const Matrix &t) { asm volatile( - "lqc2 vf1, 0x00(%1) \n\t" - "lqc2 vf2, 0x10(%1) \n\t" - "lqc2 vf3, 0x20(%1) \n\t" - "lqc2 vf4, 0x30(%1) \n\t" - "lqc2 vf5, 0x00(%2) \n\t" - "lqc2 vf6, 0x10(%2) \n\t" - "lqc2 vf7, 0x20(%2) \n\t" - "lqc2 vf8, 0x30(%2) \n\t" - "vmulax.xyzw ACC, vf5, vf1 \n\t" - "vmadday.xyzw ACC, vf6, vf1 \n\t" - "vmaddaz.xyzw ACC, vf7, vf1 \n\t" - "vmaddw.xyzw vf1, vf8, vf1 \n\t" - "vmulax.xyzw ACC, vf5, vf2 \n\t" - "vmadday.xyzw ACC, vf6, vf2 \n\t" - "vmaddaz.xyzw ACC, vf7, vf2 \n\t" - "vmaddw.xyzw vf2, vf8, vf2 \n\t" - "vmulax.xyzw ACC, vf5, vf3 \n\t" - "vmadday.xyzw ACC, vf6, vf3 \n\t" - "vmaddaz.xyzw ACC, vf7, vf3 \n\t" - "vmaddw.xyzw vf3, vf8, vf3 \n\t" - "vmulax.xyzw ACC, vf5, vf4 \n\t" - "vmadday.xyzw ACC, vf6, vf4 \n\t" - "vmaddaz.xyzw ACC, vf7, vf4 \n\t" - "vmaddw.xyzw vf4, vf8, vf4 \n\t" - "sqc2 vf1, 0x00(%0) \n\t" - "sqc2 vf2, 0x10(%0) \n\t" - "sqc2 vf3, 0x20(%0) \n\t" - "sqc2 vf4, 0x30(%0) \n\t" + "lqc2 vf4, 0x00(%1) \n\t" + "lqc2 vf5, 0x10(%1) \n\t" + "lqc2 vf6, 0x20(%1) \n\t" + "lqc2 vf7, 0x30(%1) \n\t" + + "lqc2 vf8, 0x00(%2) \n\t" + "lqc2 vf9, 0x10(%2) \n\t" + "lqc2 vf10, 0x20(%2) \n\t" + "lqc2 vf11, 0x30(%2) \n\t" + + "vmulax.xyzw ACC, vf4, vf8 \n\t" + "vmadday.xyzw ACC, vf5, vf8 \n\t" + "vmaddaz.xyzw ACC, vf6, vf8 \n\t" + "vmaddw.xyzw vf12, vf7, vf8 \n\t" + "vmulax.xyzw ACC, vf4, vf9 \n\t" + "vmadday.xyzw ACC, vf5, vf9 \n\t" + "vmaddaz.xyzw ACC, vf6, vf9 \n\t" + "vmaddw.xyzw vf13, vf7, vf9 \n\t" + "vmulax.xyzw ACC, vf4, vf10 \n\t" + "vmadday.xyzw ACC, vf5, vf10 \n\t" + "vmaddaz.xyzw ACC, vf6, vf10 \n\t" + "vmaddw.xyzw vf14, vf7, vf10 \n\t" + "vmulax.xyzw ACC, vf4, vf11 \n\t" + "vmadday.xyzw ACC, vf5, vf11 \n\t" + "vmaddaz.xyzw ACC, vf6, vf11 \n\t" + "vmaddw.xyzw vf15, vf7, vf11 \n\t" + + "sqc2 vf12, 0x00(%0) \n\t" + "sqc2 vf13, 0x10(%0) \n\t" + "sqc2 vf14, 0x20(%0) \n\t" + "sqc2 vf15, 0x30(%0) \n\t" : - : "r"(this->data), "r"(this->data), "r"(t.data) - : "memory"); + : "r"(this->data), "r"(this->data), "r"(t.data)); } /** Create empty matrix */ @@ -275,6 +288,38 @@ void Matrix::setPerspective(ScreenSettings &t_screen) this->data[15] = 0.0F; } +void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) +{ + Vector3 localAxis = Vector3(t_axis); + localAxis.normalize(); + float x = localAxis.x; + float y = localAxis.y; + float z = localAxis.z; + + float c = Math::cos(t_angle); + float s = Math::sin(t_angle); + + this->data[0] = x * x * (1 - c) + c; + this->data[1] = y * x * (1 - c) + z * s; + this->data[2] = x * z * (1 - c) - y * s; + this->data[3] = 0.0F; + + this->data[4] = x * y * (1 - c) - z * s; + this->data[5] = y * y * (1 - c) + c; + this->data[6] = y * z * (1 - c) + x * s; + this->data[7] = 0.0F; + + this->data[8] = x * z * (1 - c) + y * s; + this->data[9] = y * z * (1 - c) - x * s; + this->data[10] = z * z * (1 - c) + c; + this->data[11] = 0.0F; + + this->data[12] = 0.0F; + this->data[13] = 0.0F; + this->data[14] = 0.0F; + this->data[15] = 1.0F; +} + /** Create a view matrix that transforms coordinates in * such a way that the user looks at a target vector * direction from a position vector. From f4e699025abe21a78bfea337b5dbd6a5bc31b9e0 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 12 Dec 2020 23:19:12 +0100 Subject: [PATCH 5/8] added useful funcs --- src/engine/include/utils/math.hpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/engine/include/utils/math.hpp b/src/engine/include/utils/math.hpp index 92af50c..3218047 100644 --- a/src/engine/include/utils/math.hpp +++ b/src/engine/include/utils/math.hpp @@ -24,15 +24,23 @@ class Math { public: - const static float HALF_ANG2RAD = 3.14159265358979323846 / 360.0; + const static float HALF_ANG2RAD = 3.14159265358979323846F / 360.0F; + const static float ANG2RAD = 3.14159265358979323846F / 180.0F; const static float PI = 3.1415926535897932384626433832795F; const static float HALF_PI = 1.5707963267948966192313216916398F; static float cos(float x); static float asin(float x); static float mod(float x, float y); static inline float sin(float x) { return cos(x - HALF_PI); }; + static inline float tan(float x) { return sin(x) / cos(x); } static float sqrt(float x); static float invSqrt(float x); + static inline u32 max(u32 a, u32 b) { return (a > b) ? a : b; } + static inline s32 max(s32 a, s32 b) { return (a > b) ? a : b; } + static inline float max(float a, float b) { return (a > b) ? a : b; } + static inline u32 min(u32 a, u32 b) { return (a < b) ? a : b; } + static inline s32 min(s32 a, s32 b) { return (a < b) ? a : b; } + static inline float min(float a, float b) { return (a < b) ? a : b; } private: Math(); From 3f4d2c55212eccbcc0d906649ff3208cd4970572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Sobczy=C5=84ski?= <32263891+h4570@users.noreply.github.com> Date: Sun, 13 Dec 2020 00:19:18 +0100 Subject: [PATCH 6/8] Revert "Urgent hotfix in matrix multiply, added few funcs" --- src/engine/include/models/math/matrix.hpp | 4 +- src/engine/models/math/matrix.cpp | 167 ++++++++-------------- 2 files changed, 62 insertions(+), 109 deletions(-) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index 975486f..5e13bd7 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -82,10 +82,8 @@ public: rotate(t_val); } - void rotationByAngle(const float &t_angle, const Vector3 &t_axis); - // - void setScale(const Vector3 &t_val); + void setPerspective(ScreenSettings &screen); const void print() const; }; diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index 3c1d330..ec927a1 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -84,15 +84,6 @@ void Matrix::identity() : "r"(this->data)); } -void Matrix::setScale(const Vector3 &t_val) -{ - this->identity(); - this->data[0] = t_val.x; - this->data[5] = t_val.y; - this->data[10] = t_val.z; - this->data[15] = 1.0F; -} - void Matrix::translate(const Vector3 &t_val) { this->data[12] += t_val.x; // 3,0 @@ -139,79 +130,75 @@ void Matrix::rotateZ(const float &t_radians) Matrix Matrix::operator*(const Matrix &t) { Matrix result; - asm __volatile__( - "lqc2 vf4, 0x00(%1) \n\t" - "lqc2 vf5, 0x10(%1) \n\t" - "lqc2 vf6, 0x20(%1) \n\t" - "lqc2 vf7, 0x30(%1) \n\t" - - "lqc2 vf8, 0x00(%2) \n\t" - "lqc2 vf9, 0x10(%2) \n\t" - "lqc2 vf10, 0x20(%2) \n\t" - "lqc2 vf11, 0x30(%2) \n\t" - - "vmulax.xyzw ACC, vf4, vf8 \n\t" - "vmadday.xyzw ACC, vf5, vf8 \n\t" - "vmaddaz.xyzw ACC, vf6, vf8 \n\t" - "vmaddw.xyzw vf12, vf7, vf8 \n\t" - "vmulax.xyzw ACC, vf4, vf9 \n\t" - "vmadday.xyzw ACC, vf5, vf9 \n\t" - "vmaddaz.xyzw ACC, vf6, vf9 \n\t" - "vmaddw.xyzw vf13, vf7, vf9 \n\t" - "vmulax.xyzw ACC, vf4, vf10 \n\t" - "vmadday.xyzw ACC, vf5, vf10 \n\t" - "vmaddaz.xyzw ACC, vf6, vf10 \n\t" - "vmaddw.xyzw vf14, vf7, vf10 \n\t" - "vmulax.xyzw ACC, vf4, vf11 \n\t" - "vmadday.xyzw ACC, vf5, vf11 \n\t" - "vmaddaz.xyzw ACC, vf6, vf11 \n\t" - "vmaddw.xyzw vf15, vf7, vf11 \n\t" - - "sqc2 vf12, 0x00(%0) \n\t" - "sqc2 vf13, 0x10(%0) \n\t" - "sqc2 vf14, 0x20(%0) \n\t" - "sqc2 vf15, 0x30(%0) \n\t" + asm volatile( + "lqc2 vf1, 0x00(%1) \n\t" + "lqc2 vf2, 0x10(%1) \n\t" + "lqc2 vf3, 0x20(%1) \n\t" + "lqc2 vf4, 0x30(%1) \n\t" + "lqc2 vf5, 0x00(%2) \n\t" + "lqc2 vf6, 0x10(%2) \n\t" + "lqc2 vf7, 0x20(%2) \n\t" + "lqc2 vf8, 0x30(%2) \n\t" + "vmulax.xyzw ACC, vf5, vf1 \n\t" + "vmadday.xyzw ACC, vf6, vf1 \n\t" + "vmaddaz.xyzw ACC, vf7, vf1 \n\t" + "vmaddw.xyzw vf1, vf8, vf1 \n\t" + "vmulax.xyzw ACC, vf5, vf2 \n\t" + "vmadday.xyzw ACC, vf6, vf2 \n\t" + "vmaddaz.xyzw ACC, vf7, vf2 \n\t" + "vmaddw.xyzw vf2, vf8, vf2 \n\t" + "vmulax.xyzw ACC, vf5, vf3 \n\t" + "vmadday.xyzw ACC, vf6, vf3 \n\t" + "vmaddaz.xyzw ACC, vf7, vf3 \n\t" + "vmaddw.xyzw vf3, vf8, vf3 \n\t" + "vmulax.xyzw ACC, vf5, vf4 \n\t" + "vmadday.xyzw ACC, vf6, vf4 \n\t" + "vmaddaz.xyzw ACC, vf7, vf4 \n\t" + "vmaddw.xyzw vf4, vf8, vf4 \n\t" + "sqc2 vf1, 0x00(%0) \n\t" + "sqc2 vf2, 0x10(%0) \n\t" + "sqc2 vf3, 0x20(%0) \n\t" + "sqc2 vf4, 0x30(%0) \n\t" : - : "r"(result.data), "r"(this->data), "r"(t.data)); + : "r"(result.data), "r"(this->data), "r"(t.data) + : "memory"); return result; } void Matrix::operator*=(const Matrix &t) { asm volatile( - "lqc2 vf4, 0x00(%1) \n\t" - "lqc2 vf5, 0x10(%1) \n\t" - "lqc2 vf6, 0x20(%1) \n\t" - "lqc2 vf7, 0x30(%1) \n\t" - - "lqc2 vf8, 0x00(%2) \n\t" - "lqc2 vf9, 0x10(%2) \n\t" - "lqc2 vf10, 0x20(%2) \n\t" - "lqc2 vf11, 0x30(%2) \n\t" - - "vmulax.xyzw ACC, vf4, vf8 \n\t" - "vmadday.xyzw ACC, vf5, vf8 \n\t" - "vmaddaz.xyzw ACC, vf6, vf8 \n\t" - "vmaddw.xyzw vf12, vf7, vf8 \n\t" - "vmulax.xyzw ACC, vf4, vf9 \n\t" - "vmadday.xyzw ACC, vf5, vf9 \n\t" - "vmaddaz.xyzw ACC, vf6, vf9 \n\t" - "vmaddw.xyzw vf13, vf7, vf9 \n\t" - "vmulax.xyzw ACC, vf4, vf10 \n\t" - "vmadday.xyzw ACC, vf5, vf10 \n\t" - "vmaddaz.xyzw ACC, vf6, vf10 \n\t" - "vmaddw.xyzw vf14, vf7, vf10 \n\t" - "vmulax.xyzw ACC, vf4, vf11 \n\t" - "vmadday.xyzw ACC, vf5, vf11 \n\t" - "vmaddaz.xyzw ACC, vf6, vf11 \n\t" - "vmaddw.xyzw vf15, vf7, vf11 \n\t" - - "sqc2 vf12, 0x00(%0) \n\t" - "sqc2 vf13, 0x10(%0) \n\t" - "sqc2 vf14, 0x20(%0) \n\t" - "sqc2 vf15, 0x30(%0) \n\t" + "lqc2 vf1, 0x00(%1) \n\t" + "lqc2 vf2, 0x10(%1) \n\t" + "lqc2 vf3, 0x20(%1) \n\t" + "lqc2 vf4, 0x30(%1) \n\t" + "lqc2 vf5, 0x00(%2) \n\t" + "lqc2 vf6, 0x10(%2) \n\t" + "lqc2 vf7, 0x20(%2) \n\t" + "lqc2 vf8, 0x30(%2) \n\t" + "vmulax.xyzw ACC, vf5, vf1 \n\t" + "vmadday.xyzw ACC, vf6, vf1 \n\t" + "vmaddaz.xyzw ACC, vf7, vf1 \n\t" + "vmaddw.xyzw vf1, vf8, vf1 \n\t" + "vmulax.xyzw ACC, vf5, vf2 \n\t" + "vmadday.xyzw ACC, vf6, vf2 \n\t" + "vmaddaz.xyzw ACC, vf7, vf2 \n\t" + "vmaddw.xyzw vf2, vf8, vf2 \n\t" + "vmulax.xyzw ACC, vf5, vf3 \n\t" + "vmadday.xyzw ACC, vf6, vf3 \n\t" + "vmaddaz.xyzw ACC, vf7, vf3 \n\t" + "vmaddw.xyzw vf3, vf8, vf3 \n\t" + "vmulax.xyzw ACC, vf5, vf4 \n\t" + "vmadday.xyzw ACC, vf6, vf4 \n\t" + "vmaddaz.xyzw ACC, vf7, vf4 \n\t" + "vmaddw.xyzw vf4, vf8, vf4 \n\t" + "sqc2 vf1, 0x00(%0) \n\t" + "sqc2 vf2, 0x10(%0) \n\t" + "sqc2 vf3, 0x20(%0) \n\t" + "sqc2 vf4, 0x30(%0) \n\t" : - : "r"(this->data), "r"(this->data), "r"(t.data)); + : "r"(this->data), "r"(this->data), "r"(t.data) + : "memory"); } /** Create empty matrix */ @@ -288,38 +275,6 @@ void Matrix::setPerspective(ScreenSettings &t_screen) this->data[15] = 0.0F; } -void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) -{ - Vector3 localAxis = Vector3(t_axis); - localAxis.normalize(); - float x = localAxis.x; - float y = localAxis.y; - float z = localAxis.z; - - float c = Math::cos(t_angle); - float s = Math::sin(t_angle); - - this->data[0] = x * x * (1 - c) + c; - this->data[1] = y * x * (1 - c) + z * s; - this->data[2] = x * z * (1 - c) - y * s; - this->data[3] = 0.0F; - - this->data[4] = x * y * (1 - c) - z * s; - this->data[5] = y * y * (1 - c) + c; - this->data[6] = y * z * (1 - c) + x * s; - this->data[7] = 0.0F; - - this->data[8] = x * z * (1 - c) + y * s; - this->data[9] = y * z * (1 - c) - x * s; - this->data[10] = z * z * (1 - c) + c; - this->data[11] = 0.0F; - - this->data[12] = 0.0F; - this->data[13] = 0.0F; - this->data[14] = 0.0F; - this->data[15] = 1.0F; -} - /** Create a view matrix that transforms coordinates in * such a way that the user looks at a target vector * direction from a position vector. From 4a1bca12aa0d1f4de4e2cf9018a89f4bd2086aba Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 13 Dec 2020 00:23:04 +0100 Subject: [PATCH 7/8] small undo in rendering pipeline --- src/engine/modules/vif_sender.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 771b05f..2ef738a 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -71,12 +71,19 @@ void VifSender::uploadMicroProgram() void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) { - Matrix viewProj = Matrix(); - viewProj.rotation(t_rotation); // model matrix - viewProj.translate(t_position); // model matrix - viewProj *= *t_renderData.worldView * *t_renderData.perspective; // viewproj = model * (view * projection) + vec3ToNative(position, t_position, 1.0F); + vec3ToNative(rotation, t_rotation, 1.0F); + MATRIX localWorld, localScreen; + create_local_world(localWorld, position, rotation); + create_local_screen(localScreen, localWorld, t_renderData.worldView->data, t_renderData.perspective->data); + // Small undo, because im testing new matrix funcs, which are DIFFERENT than PS2SDK, so cant be used right now. + // Matrix viewProj = Matrix(); + // viewProj.rotation(t_rotation); // model matrix + // viewProj.translate(t_position); // model matrix + // viewProj *= *t_renderData.worldView * *t_renderData.perspective; // viewproj = model * (view * projection) packet2_reset(matricesPacket, false); - packet2_utils_vu_add_unpack_data(matricesPacket, 0, &viewProj.data, 8, 0); + // packet2_utils_vu_add_unpack_data(matricesPacket, 0, &viewProj.data, 8, 0); + packet2_utils_vu_add_unpack_data(matricesPacket, 0, localScreen, 8, 0); packet2_utils_vu_add_end_tag(matricesPacket); dma_channel_wait(DMA_CHANNEL_VIF1, 0); dma_channel_send_packet2(matricesPacket, DMA_CHANNEL_VIF1, 1); From ea6d5215d1b0da693ccd0c377c045fad7421a653 Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 13 Dec 2020 00:39:32 +0100 Subject: [PATCH 8/8] fixed funcs declaration --- src/engine/utils/math.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/utils/math.cpp b/src/engine/utils/math.cpp index c273467..5c4846e 100644 --- a/src/engine/utils/math.cpp +++ b/src/engine/utils/math.cpp @@ -100,7 +100,7 @@ void manyVec3ToNative(VECTOR *o_result, Vector3 *t_vec, int t_amount, float t_fo vec3ToNative(o_result[i], t_vec[i], t_fourthVal); } -static float asin(float x) +float Math::asin(float x) { float r; asm volatile( @@ -159,7 +159,7 @@ static float asin(float x) return r; } -static float mod(float x, float y) +float Math::mod(float x, float y) { /* * Portable fmod(x,y) implementation for systems