From b7d0b7976b8fe28b9bef9460697d1f412a58612a Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 2 Apr 2024 16:08:24 -0500 Subject: [PATCH] Support ApplyMatrix --- .../src/Overlay/GTETest/gte_test.cpp | 2 +- .../Overlay/GTETest/include/GTE_Sprite.hpp | 17 +++++++-------- include/PSX/GTE/gte.hpp | 13 +++++++++++- include/PSX/GTE/gte_instruction.hpp | 21 +++++++++++++++++++ src/Library/src/GTE/gte.cpp | 10 +++++++++ 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/examples/PoolBox/application/src/Overlay/GTETest/gte_test.cpp b/examples/PoolBox/application/src/Overlay/GTETest/gte_test.cpp index 34600e59..4ea9ef1a 100644 --- a/examples/PoolBox/application/src/Overlay/GTETest/gte_test.cpp +++ b/examples/PoolBox/application/src/Overlay/GTETest/gte_test.cpp @@ -34,7 +34,7 @@ namespace GTETest { GTE::set_trans_matrix(matrix); GTE::set_rot_matrix(matrix); - doener_fish.apply(); + doener_fish.apply(matrix); rotation += 5_DEG; return false; } diff --git a/examples/PoolBox/application/src/Overlay/GTETest/include/GTE_Sprite.hpp b/examples/PoolBox/application/src/Overlay/GTETest/include/GTE_Sprite.hpp index 5373bf71..41a793e7 100644 --- a/examples/PoolBox/application/src/Overlay/GTETest/include/GTE_Sprite.hpp +++ b/examples/PoolBox/application/src/Overlay/GTETest/include/GTE_Sprite.hpp @@ -17,20 +17,19 @@ namespace GTETest { }; } - void apply() { - static const auto apply_to = [](GTE::SVECTOR vector) -> GPU::Vertex { - GTE::VECTOR output; + void apply(const GTE::MATRIX& matrix) { + static const auto apply_to = [](const GTE::MATRIX& matrix, GTE::SVECTOR vector) -> GPU::Vertex { + GTE::SVECTOR output; int32_t flag; - GTE::rot_trans(vector, output, flag); - return output.to(); + return GTE::apply_matrix(matrix, vector, output).to(); }; const auto& area = this->area; - this->display.vertex0 = apply_to(GTE::SVECTOR::from(area.get_top_left())); - this->display.vertex1 = apply_to(GTE::SVECTOR::from(area.get_top_right())); - this->display.vertex2 = apply_to(GTE::SVECTOR::from(area.get_bottom_left())); - this->display.vertex3 = apply_to(GTE::SVECTOR::from(area.get_bottom_right())); + this->display.vertex0 = apply_to(matrix, GTE::SVECTOR::from(area.get_top_left())); + this->display.vertex1 = apply_to(matrix, GTE::SVECTOR::from(area.get_top_right())); + this->display.vertex2 = apply_to(matrix, GTE::SVECTOR::from(area.get_bottom_left())); + this->display.vertex3 = apply_to(matrix, GTE::SVECTOR::from(area.get_bottom_right())); } void render() { diff --git a/include/PSX/GTE/gte.hpp b/include/PSX/GTE/gte.hpp index 359dd6d6..2bedda8d 100644 --- a/include/PSX/GTE/gte.hpp +++ b/include/PSX/GTE/gte.hpp @@ -89,7 +89,18 @@ namespace JabyEngine { __asm__ volatile("cfc2 $12, $5" :: "r"(&matrix) : "$12", "$13", "$14"); __asm__ volatile("sw $13, 24(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); __asm__ volatile("sw $12, 20(%0)" :: "r"(&matrix) : "$12", "$13", "$14"); - } + } + + /* + ApplyMatrix + m0: Matrix to apply + v0: Vector to apply to + v1: Result + returns: result + + Applies the matrix to the vector + */ + SVECTOR& apply_matrix(const MATRIX& m0, const SVECTOR& v0, SVECTOR& v1); /* MulMatrix0 diff --git a/include/PSX/GTE/gte_instruction.hpp b/include/PSX/GTE/gte_instruction.hpp index d21dc3df..0cc51473 100644 --- a/include/PSX/GTE/gte_instruction.hpp +++ b/include/PSX/GTE/gte_instruction.hpp @@ -55,6 +55,17 @@ namespace JabyEngine { __asm__ volatile("swc2 $27, 8(%0)" :: "r"(&out_vector) : "memory"); } + // Modify to store in VERTEX? + // Store SVECTOR from 16 bit universal register + static __always_inline void stsv(SVECTOR& out_vector) { + __asm__ volatile("mfc2 $12, $9" :: "r"(&out_vector) : "$12", "$13", "$14", "memory"); + __asm__ volatile("mfc2 $13, $10" :: "r"(&out_vector) : "$12", "$13", "$14", "memory"); + __asm__ volatile("mfc2 $14, $11" :: "r"(&out_vector) : "$12", "$13", "$14", "memory"); + __asm__ volatile("sh $12, 0(%0)" :: "r"(&out_vector) : "$12", "$13", "$14", "memory"); + __asm__ volatile("sh $13, 2(%0)" :: "r"(&out_vector) : "$12", "$13", "$14", "memory"); + __asm__ volatile("sh $14, 4(%0)" :: "r"(&out_vector) : "$12", "$13", "$14", "memory"); + } + /* Kernel of RotTrans (Transfer vector)+(Rotation Matrix)*(vertex register 0) @@ -65,6 +76,16 @@ namespace JabyEngine { __asm__ volatile("cop2 0x0480012"); } + /* + Variation of gte_rt + (Rotation Matrix)*(vertex register 0). + */ + static __always_inline void rtv0() { + __asm__ volatile("nop;"); + __asm__ volatile("nop;"); + __asm__ volatile("cop2 0x0486012;"); + } + /* Variation of gte_rt (Rotation Matrix)*(16 bit universal vector) diff --git a/src/Library/src/GTE/gte.cpp b/src/Library/src/GTE/gte.cpp index 1abdd1ed..48571278 100644 --- a/src/Library/src/GTE/gte.cpp +++ b/src/Library/src/GTE/gte.cpp @@ -43,6 +43,16 @@ namespace JabyEngine { static MATRIX Stack[StackSize]; static MATRIX* FreeStackEntry = Stack; + SVECTOR& apply_matrix(const MATRIX& m0, const SVECTOR& v0, SVECTOR& v1) { + // TODO: Do we want to push here? + set_rot_matrix(m0); + + JabyEngine::GTE::ldv0(v0); + JabyEngine::GTE::rtv0(); + JabyEngine::GTE::stsv(v1); + return v1; + } + MATRIX& multiply_matrix(const MATRIX& m0, const MATRIX& m1, MATRIX& result) { set_rot_matrix(m0);