Skip to content

Commit 26dbbec

Browse files
committed
decompile CalculateTrianglePlane
1 parent 64d289c commit 26dbbec

File tree

9 files changed

+72
-19
lines changed

9 files changed

+72
-19
lines changed

include/ctr/coll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,6 @@ typedef struct CollDCache
104104
#define DCACHE_COLL (*(CollDCache*) 0x1f800000)
105105

106106
void COLL_ProjectPointToEdge(SVec3* out, const SVec3* v1, const SVec3* v2, const SVec3* point);
107+
void COLL_CalculateTrianglePlane(const CollDCache* cache, CollVertex* v1, const CollVertex* v2, const CollVertex* v3);
107108
void COLL_LoadVerticeData(CollDCache* cache);
108109
s32 COLL_BarycentricTest(TestVertex* t, const CollVertex* v1, const CollVertex* v2, const CollVertex* v3);

include/ctr/gte.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ typedef enum GTE_MAC
4343
GTE_MAC_3, /* s32 */
4444
} GTE_MAC;
4545

46-
typedef enum GTE_INTERPOLATE
46+
typedef enum GTE_CALC
4747
{
48-
GTE_INTERPOLATE_INT,
49-
GTE_INTERPOLATE_FLOATING_POINT,
50-
} GTE_INTERPOLATE;
48+
GTE_CALC_INT,
49+
GTE_CALC_FLOATING_POINT,
50+
} GTE_CALC;
5151

5252
/* HELPERS */
5353
#define _CAT(a, b) a##b
@@ -75,12 +75,15 @@ typedef enum GTE_INTERPOLATE
7575
#define _gte_readMac_GTE_MAC_3(out) gte_stlvnl2(out)
7676
#define _gte_readMac_GTE_VECTOR_MAC(out) gte_stlvnl(out)
7777
#define _gte_loadIR_GTE_IR_0(in) gte_lddp(in)
78-
#define _gte_dotProduct_GTE_ROW_INDEX_0(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_1(out)
79-
#define _gte_dotProduct_GTE_ROW_INDEX_1(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_2(out)
80-
#define _gte_dotProduct_GTE_ROW_INDEX_2(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_3(out)
78+
#define _gte_dotProduct_GTE_ROW_INDEX_0_GTE_CALC_INT(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_1(out)
79+
#define _gte_dotProduct_GTE_ROW_INDEX_1_GTE_CALC_INT(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_2(out)
80+
#define _gte_dotProduct_GTE_ROW_INDEX_2_GTE_CALC_INT(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_3(out)
81+
#define _gte_dotProduct_GTE_ROW_INDEX_0_GTE_CALC_FLOATING_POINT(out, matrixType, vecType) gte_mvmva(1, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_1(out)
82+
#define _gte_dotProduct_GTE_ROW_INDEX_1_GTE_CALC_FLOATING_POINT(out, matrixType, vecType) gte_mvmva(1, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_2(out)
83+
#define _gte_dotProduct_GTE_ROW_INDEX_2_GTE_CALC_FLOATING_POINT(out, matrixType, vecType) gte_mvmva(1, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_3(out)
8184
#define _gte_mulMatrixVec(out, matrixType, vecType, shift) gte_mvmva(shift, matrixType, vecType, 3, 0); _gte_readMac_GTE_VECTOR_MAC(out)
82-
#define _gte_interpolate_GTE_INTERPOLATE_INT() gte_gpl0()
83-
#define _gte_interpolate_GTE_INTERPOLATE_FLOATING_POINT() gte_gpl12()
85+
#define _gte_interpolate_GTE_CALC_INT() gte_gpl0()
86+
#define _gte_interpolate_GTE_CALC_FLOATING_POINT() gte_gpl12()
8487
#define _gte_leadingZeroes(out, in) __asm__ volatile ( \
8588
"mtc2 %1, $30;" \
8689
"nop;" \
@@ -98,6 +101,7 @@ typedef enum GTE_INTERPOLATE
98101
#define gte_loadVec(v, vecType) CAT(_gte_loadVec_, vecType)(v)
99102
#define gte_loadRowMatrix(v, rowIndex, matrixType) CAT3(_gte_loadSVecMatrix_, matrixType, _##rowIndex)(v)
100103
#define gte_mulMatrixVec(out, matrixType, vecType) _gte_mulMatrixVec(out, matrixType, vecType, 1)
101-
#define gte_dotProduct(out, rowIndex, matrixType, vecType) CAT(_gte_dotProduct_, rowIndex)(out, matrixType, vecType)
104+
#define gte_dotProduct(out, rowIndex, matrixType, vecType, calcType) CAT3(_gte_dotProduct_, rowIndex, _##calcType)(out, matrixType, vecType)
102105
#define gte_leadingZeroes(out, in) _gte_leadingZeroes(out, in)
103-
#define gte_interpolate(out, interpolationType) CAT(_gte_interpolate_, interpolationType)(); _gte_readMac_GTE_VECTOR_MAC(out)
106+
#define gte_interpolate(out, calcType) CAT(_gte_interpolate_, calcType)(); _gte_readMac_GTE_VECTOR_MAC(out)
107+
#define gte_crossProduct(out, matrix, vector) gte_SetRotMatrix(matrix); gte_loadSVec(vector, GTE_VECTOR_IR); gte_op0(); gte_readMac(out, GTE_VECTOR_MAC)

include/ctr/nd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
#include <ctr/coll.h>
77

88
void ND_LOAD_XnfFile(char* filename, u32 address, char* dummy);
9+
void ND_LOAD_InitCD();
10+
911
s32 ND_SquareRoot0_stub(s32 n);
1012
int ND_printf(const char* format, ...);
1113
int ND_sprintf(const char* outStr, const char* format, ...);
12-
void ND_LOAD_InitCD();
1314

1415
/* MATH */
1516
s32 ND_MATH_Sin(u32 angle);
@@ -29,5 +30,6 @@ u32 ND_RNG_Random(RNGSeed* seed);
2930

3031
/* COLL */
3132
void ND_COLL_ProjectPointToEdge(SVec3* out, const SVec3* v1, const SVec3* v2, const SVec3* point);
33+
void ND_COLL_CalculateTrianglePlane(const CollDCache* cache, CollVertex* v1, const CollVertex* v2, const CollVertex* v3);
3234
void ND_COLL_LoadVerticeData(CollDCache* cache);
3335
s32 ND_COLL_BarycentricTest(TestVertex* t, const CollVertex* v1, const CollVertex* v2, const CollVertex* v3);

include/ctr/test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ force_inline void FlushCache()
6666

6767
#ifdef TEST_COLL_IMPL
6868
void TEST_COLL_ProjectPointToEdge(const SVec3* v1, const SVec3* v2, const SVec3* point, const SVec3* ret);
69+
void TEST_COLL_CalculateTrianglePlane(const CollDCache* cache, CollVertex* v1, const CollVertex* v2, const CollVertex* v3, const CollVertex* ret);
6970
void TEST_COLL_LoadVerticeData(CollDCache* cache);
7071
void TEST_COLL_BarycentricTest(TestVertex* t, const CollVertex* v1, const CollVertex* v2, const CollVertex* v3, const SVec3* pos, s32 ret);
7172
#else

rewrite/src/exe/coll.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void COLL_ProjectPointToEdge(SVec3* out, const SVec3* v1, const SVec3* v2, const
1414
gte_SetRotMatrix(m.m);
1515
gte_loadSVec(edge.v, GTE_VECTOR_0);
1616
s32 edgeDot, pointDot;
17-
gte_dotProduct(&edgeDot, GTE_ROW_INDEX_0, GTE_MATRIX_ROT, GTE_VECTOR_0);
17+
gte_dotProduct(&edgeDot, GTE_ROW_INDEX_0, GTE_MATRIX_ROT, GTE_VECTOR_0, GTE_CALC_INT);
1818
gte_readMac(&pointDot, GTE_MAC_2);
1919
s32 leadingZeroes;
2020
gte_leadingZeroes(&leadingZeroes, pointDot);
@@ -31,19 +31,51 @@ void COLL_ProjectPointToEdge(SVec3* out, const SVec3* v1, const SVec3* v2, const
3131
gte_loadVec(V1.v, GTE_VECTOR_MAC);
3232

3333
Vec3 coords;
34-
gte_interpolate(coords.v, GTE_INTERPOLATE_FLOATING_POINT);
34+
gte_interpolate(coords.v, GTE_CALC_FLOATING_POINT);
3535
out->x = coords.x;
3636
out->y = coords.y;
3737
out->z = coords.z;
3838
TEST_COLL_ProjectPointToEdge(v1, v2, point, out);
3939
/* This is a hand written assembly function that breaks the ABI,
40-
and some callers expect the argument registers to be untouched*/
40+
and some callers expect the argument registers to be untouched */
4141
__asm__ volatile("move $a0, %0" : : "r"((u32)out));
4242
__asm__ volatile("move $a1, %0" : : "r"((u32)v1));
4343
__asm__ volatile("move $a2, %0" : : "r"((u32)v2));
4444
__asm__ volatile("move $a3, %0" : : "r"((u32)point));
4545
}
4646

47+
/* Address: 0x8001f2dc */
48+
void COLL_CalculateTrianglePlane(const CollDCache* cache, CollVertex* v1, const CollVertex* v2, const CollVertex* v3)
49+
{
50+
#ifdef TEST_COLL_IMPL
51+
CollVertex input = *v1;
52+
#endif
53+
Vec3 cross;
54+
const Matrix m = { .m[0][0] = v3->pos.x - v1->pos.x, .m[1][1] = v3->pos.y - v1->pos.y, .m[2][2] = v3->pos.z - v1->pos.z };
55+
const SVec3 v = { .x = v2->pos.x - v1->pos.x, .y = v2->pos.y - v1->pos.y, .z = v2->pos.z - v1->pos.z };
56+
gte_crossProduct(cross.v, m.m, v.v);
57+
v1->triNormal.x = ((cross.x >> cache->lodShift) * cache->normalScale) >> cache->normalBitshift;
58+
v1->triNormal.y = ((cross.y >> cache->lodShift) * cache->normalScale) >> cache->normalBitshift;
59+
v1->triNormal.z = ((cross.z >> cache->lodShift) * cache->normalScale) >> cache->normalBitshift;
60+
61+
s32 dot;
62+
gte_loadRowMatrix(v1->pos.v, GTE_ROW_INDEX_0, GTE_MATRIX_ROT);
63+
gte_loadSVec(v1->triNormal.v, GTE_VECTOR_IR);
64+
gte_dotProduct(&dot, GTE_ROW_INDEX_0, GTE_MATRIX_ROT, GTE_VECTOR_IR, GTE_CALC_FLOATING_POINT);
65+
v1->planeDist = dot >> 1;
66+
67+
Vec3 absNormal = { .x = abs(v1->triNormal.x), .y = abs(v1->triNormal.y), .z = abs(v1->triNormal.z) };
68+
const s32 magnitude = max(max(absNormal.x, absNormal.y), absNormal.z);
69+
if (magnitude == absNormal.x) { v1->normalDominantAxis = AXIS_X; }
70+
else if (magnitude == absNormal.y) { v1->normalDominantAxis = AXIS_Y; }
71+
else { v1->normalDominantAxis = AXIS_Z; }
72+
TEST_COLL_CalculateTrianglePlane(cache, &input, v2, v3, v1);
73+
/* This is a hand written assembly function that breaks the ABI,
74+
and some callers expect the argument registers to be untouched */
75+
__asm__ volatile("move $a0, %0" : : "r"((u32)cache));
76+
__asm__ volatile("move $t9, %0" : : "r"((u32)cache->currQuadblock));
77+
}
78+
4779
/* Address: 0x8001f7f0 */
4880
void COLL_LoadVerticeData(CollDCache* cache)
4981
{
@@ -60,7 +92,7 @@ void COLL_LoadVerticeData(CollDCache* cache)
6092
cache->quadblockFourthIndex = quadblock->index[3];
6193
TEST_COLL_LoadVerticeData(cache);
6294
/* This is a hand written assembly function that breaks the ABI,
63-
and some callers expect the argument registers to be untouched*/
95+
and some callers expect the argument registers to be untouched */
6496
__asm__ volatile("move $a0, %0" : : "r"((u32)cache));
6597
__asm__ volatile("move $t9, %0" : : "r"((u32)quadblock));
6698
}

rewrite/src/exe/math.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ s32 MATH_VectorLength(const SVec3* vector)
8282
gte_loadRowMatrix(vector, GTE_ROW_INDEX_0, GTE_MATRIX_ROT);
8383
gte_loadSVec(vector, GTE_VECTOR_0);
8484
s32 lengthSquared;
85-
gte_dotProduct(&lengthSquared, GTE_ROW_INDEX_0, GTE_MATRIX_ROT, GTE_VECTOR_0);
85+
gte_dotProduct(&lengthSquared, GTE_ROW_INDEX_0, GTE_MATRIX_ROT, GTE_VECTOR_0, GTE_CALC_INT);
8686
const s32 len = ND_SquareRoot0_stub(lengthSquared);
8787
TEST_MATH_VectorLength(vector, len);
8888
return len;

rewrite/src/tests/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ FunctionPatch s_functions[] =
3232
TEST_FUNC(COLL_ProjectPointToEdge),
3333
TEST_FUNC(COLL_BarycentricTest),
3434
TEST_FUNC(COLL_LoadVerticeData),
35+
TEST_FUNC(COLL_CalculateTrianglePlane),
3536
};
3637

3738
void LoadTestPatches()

rewrite/src/tests/test_coll.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ void TEST_COLL_ProjectPointToEdge(const SVec3* v1, const SVec3* v2, const SVec3*
1313
PatchFunction_End(index);
1414
}
1515

16+
void TEST_COLL_CalculateTrianglePlane(const CollDCache* cache, CollVertex* v1, const CollVertex* v2, const CollVertex* v3, const CollVertex* ret)
17+
{
18+
const u32 index = PatchFunction_Beg((u32*)(&ND_COLL_CalculateTrianglePlane));
19+
typedef void (*Func)(const CollDCache* cache, CollVertex* v1, const CollVertex* v2, const CollVertex* v3);
20+
Func func = (Func) TEST_WRAPPER;
21+
func(cache, v1, v2, v3);
22+
PrintSVectorDiff("COLL_CalculateTrianglePlane", &v1->triNormal, &ret->triNormal);
23+
if (v1->planeDist != ret->planeDist) { ND_printf("[COLL_CalculateTrianglePlane] Test Failed:\nDist: %d\nResult: %d\n", v1->planeDist, ret->planeDist); }
24+
if (v1->normalDominantAxis != ret->normalDominantAxis) { ND_printf("[COLL_CalculateTrianglePlane] Test Failed:\nAxis: %d\nResult: %d\n", v1->normalDominantAxis, ret->normalDominantAxis); }
25+
PatchFunction_End(index);
26+
}
27+
1628
void TEST_COLL_LoadVerticeData(CollDCache* cache)
1729
{
1830
CollVertex vertices[NUM_VERTICES_QUADBLOCK];

symbols/gcc-syms-rewrite.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ND_COLL_SearchBSP_CallbackQUADBLK = 0x8001eb0c;
6363
ND_COLL_SearchBSP_CallbackPARAM = 0x8001ebec;
6464
ND_COLL_ProjectPointToEdge = 0x8001ede4;
6565
ND_COLL_FIXED_TRIANGL_TestPoint = 0x8001ef50;
66-
ND_COLL_FIXED_TRIANGL_GetNormVec = 0x8001f2dc;
66+
ND_COLL_CalculateTrianglePlane = 0x8001f2dc;
6767
ND_COLL_FIXED_QUADBLK_TestTriangles = 0x8001f41c;
6868
ND_COLL_FIXED_BSPLEAF_TestQuadblocks = 0x8001f5f0;
6969
ND_COLL_FIXED_QUADBLK_GetNormVecs_LoLOD = 0x8001f67c;
@@ -851,7 +851,7 @@ ND_ThTick_SetAndExec = 0x800716ec;
851851
ND_ThTick_Set = 0x80071704;
852852
ND_RotTrans = 0x8007170c;
853853
ND_ratan2 = 0x8007173c;
854-
ND_memset = 0x800718bc;
854+
memset = 0x800718bc;
855855
ND_strcmp = 0x800718cc;
856856
ND_SetTransMatrix = 0x800718dc;
857857
ND_SetRotMatrix = 0x800718fc;

0 commit comments

Comments
 (0)