summaryrefslogtreecommitdiff
path: root/libs/JSystem/J3DGraphBase/J3DTransform.cpp
blob: f0cdca374322377354b0c76bb55fa01ca780455f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//
// Generated By: dol2asm
// Translation Unit: J3DTransform
//

#include "JSystem/J3DGraphBase/J3DTransform.h"
#include "JSystem/JMath/JMATrigonometric.h"
#include "dol2asm.h"
#include "dolphin/base/PPCArch.h"

//
// Forward References:
//

extern "C" void __MTGQR7__FUl();
extern "C" void J3DGQRSetup7__FUlUlUlUl();
extern "C" void J3DCalcBBoardMtx__FPA4_f();
extern "C" void J3DCalcYBBoardMtx__FPA4_f();
extern "C" void J3DPSCalcInverseTranspose__FPA4_fPA3_f();
extern "C" void J3DGetTranslateRotateMtx__FRC16J3DTransformInfoPA4_f();
extern "C" void J3DGetTranslateRotateMtx__FsssfffPA4_f();
extern "C" void J3DGetTextureMtx__FRC17J3DTextureSRTInfoRC3VecPA4_f();
extern "C" void J3DGetTextureMtxOld__FRC17J3DTextureSRTInfoRC3VecPA4_f();
extern "C" void J3DGetTextureMtxMaya__FRC17J3DTextureSRTInfoPA4_f();
extern "C" void J3DGetTextureMtxMayaOld__FRC17J3DTextureSRTInfoPA4_f();
extern "C" void J3DScaleNrmMtx__FPA4_fRC3Vec();
extern "C" void J3DScaleNrmMtx33__FPA3_fRC3Vec();
extern "C" void J3DMtxProjConcat__FPA4_fPA4_fPA4_f();
extern "C" void J3DPSMtxArrayConcat__FPA4_fPA4_fPA4_fUl();
extern "C" extern f32 PSMulUnit01[2];

//
// External References:
//

extern "C" u8 sincosTable___5JMath[65536];

//
// Declarations:
//

/* 80311630-80311638 -00001 0008+00 0/0 0/0 0/0 .text            __MTGQR7__FUl */
void __MTGQR7(register u32 v) {
    // clang-format off
	asm {
	    mtspr GQR7, v
    }
    // clang-format on
}

/* 80311638-80311670 30BF78 0038+00 0/0 2/2 0/0 .text            J3DGQRSetup7__FUlUlUlUl */
void J3DGQRSetup7(u32 r0, u32 r1, u32 r2, u32 r3) {
    u32 v = (((r0 << 8) + r1) << 16) | ((r2 << 8) + r3);
    __MTGQR7(v);
}

/* ############################################################################################## */
/* 80456378-8045637C 004978 0004+00 6/6 0/0 0/0 .sdata2          @435 */
SECTION_SDATA2 static u8 lit_435[4] = {
    0x00,
    0x00,
    0x00,
    0x00,
};

/* 80311670-80311760 30BFB0 00F0+00 0/0 2/2 0/0 .text            J3DCalcBBoardMtx__FPA4_f */
// this uses a non-standard sqrtf, not sure why or how its supposed to be setup
#ifdef NONMATCHING
static inline f32 sqrtf2(f32 x) {
    if (x > 0.0f) {
        f32 guess = (f32)__frsqrte(x);
        return (f32)(x * guess);
    }
    return x;
}

void J3DCalcBBoardMtx(Mtx mtx) {
    f32 x = (mtx[0][0] * mtx[0][0]) + (mtx[1][0] * mtx[1][0]) + (mtx[2][0] * mtx[2][0]);
    f32 y = (mtx[0][1] * mtx[0][1]) + (mtx[1][1] * mtx[1][1]) + (mtx[2][1] * mtx[2][1]);
    f32 z = (mtx[0][2] * mtx[0][2]) + (mtx[1][2] * mtx[1][2]) + (mtx[2][2] * mtx[2][2]);

    if (x > 0.0f) {
        x *= sqrtf2(x);
    }
    if (y > 0.0f) {
        y *= sqrtf2(y);
    }
    if (z > 0.0f) {
        z *= sqrtf2(z);
    }

    mtx[0][0] = x;
    mtx[0][1] = 0.0f;
    mtx[0][2] = 0.0f;

    mtx[1][0] = 0.0f;
    mtx[1][1] = y;
    mtx[1][2] = 0.0f;

    mtx[2][0] = 0.0f;
    mtx[2][1] = 0.0f;
    mtx[2][2] = z;
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DCalcBBoardMtx(f32 (*param_0)[4]) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DCalcBBoardMtx__FPA4_f.s"
}
#pragma pop
#endif

/* ############################################################################################## */
/* 803A1E30-803A1E50 02E490 0020+00 0/0 1/1 0/0 .rodata          j3dDefaultTransformInfo */
extern J3DTransformInfo const j3dDefaultTransformInfo = {
    {1.0f, 1.0f, 1.0f}, {0, 0, 0}, {0.0f, 0.0f, 0.0f}};

/* 803A1E50-803A1E5C 02E4B0 000C+00 0/0 1/1 0/0 .rodata          j3dDefaultScale */
extern Vec const j3dDefaultScale = {1.0f, 1.0f, 1.0f};

/* 803A1E5C-803A1E8C 02E4BC 0030+00 0/0 8/8 7/7 .rodata          j3dDefaultMtx */
extern Mtx const j3dDefaultMtx = {
    {1.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f, 0.0f}};

/* 803A1E8C-803A1E98 02E4EC 000C+00 1/1 0/0 0/0 .rodata          @443 */
SECTION_RODATA static u8 const lit_443[12] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
COMPILER_STRIP_GATE(0x803A1E8C, &lit_443);

/* 80311760-8031189C 30C0A0 013C+00 0/0 2/2 0/0 .text            J3DCalcYBBoardMtx__FPA4_f */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DCalcYBBoardMtx(f32 (*param_0)[4]) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DCalcYBBoardMtx__FPA4_f.s"
}
#pragma pop

/* 8031189C-80311964 30C1DC 00C8+00 0/0 6/6 0/0 .text J3DPSCalcInverseTranspose__FPA4_fPA3_f */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DPSCalcInverseTranspose(f32 (*param_0)[4], f32 (*param_1)[3]) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DPSCalcInverseTranspose__FPA4_fPA3_f.s"
}
#pragma pop

/* 80311964-80311A24 30C2A4 00C0+00 0/0 2/2 2/2 .text
 * J3DGetTranslateRotateMtx__FRC16J3DTransformInfoPA4_f         */
void J3DGetTranslateRotateMtx(const J3DTransformInfo& tx, Mtx dst) {
    f32 sx = JMASSin(tx.mRotation.x), cx = JMASCos(tx.mRotation.x);
    f32 sy = JMASSin(tx.mRotation.y), cy = JMASCos(tx.mRotation.y);
    f32 sz = JMASSin(tx.mRotation.z), cz = JMASCos(tx.mRotation.z);

    dst[2][0] = -sy;
    dst[0][0] = cz * cy;
    dst[1][0] = sz * cy;
    dst[2][1] = cy * sx;
    dst[2][2] = cy * cx;

    f32 cxsz = cx * sz;
    f32 sxcz = sx * cz;
    dst[0][1] = sxcz * sy - cxsz;
    dst[1][2] = cxsz * sy - sxcz;

    f32 sxsz = sx * sz;
    f32 cxcz = cx * cz;
    dst[0][2] = cxcz * sy + sxsz;
    dst[1][1] = sxsz * sy + cxcz;

    dst[0][3] = tx.mTranslate.x;
    dst[1][3] = tx.mTranslate.y;
    dst[2][3] = tx.mTranslate.z;
}

/* 80311A24-80311ACC 30C364 00A8+00 0/0 1/1 0/0 .text J3DGetTranslateRotateMtx__FsssfffPA4_f */
void J3DGetTranslateRotateMtx(s16 rx, s16 ry, s16 rz, f32 tx, f32 ty, f32 tz, Mtx dst) {
    f32 sx = JMASSin(rx), cx = JMASCos(rx);
    f32 sy = JMASSin(ry), cy = JMASCos(ry);
    f32 sz = JMASSin(rz), cz = JMASCos(rz);

    dst[2][0] = -sy;
    dst[0][0] = cz * cy;
    dst[1][0] = sz * cy;
    dst[2][1] = cy * sx;
    dst[2][2] = cy * cx;

    f32 cxsz = cx * sz;
    f32 sxcz = sx * cz;
    dst[0][1] = sxcz * sy - cxsz;
    dst[1][2] = cxsz * sy - sxcz;

    f32 sxsz = sx * sz;
    f32 cxcz = cx * cz;
    dst[0][2] = cxcz * sy + sxsz;
    dst[1][1] = sxsz * sy + cxcz;

    dst[0][3] = tx;
    dst[1][3] = ty;
    dst[2][3] = tz;
}

/* ############################################################################################## */
/* 8045637C-80456380 00497C 0004+00 4/4 0/0 0/0 .sdata2          @526 */
SECTION_SDATA2 static f32 lit_526 = 1.0f;

/* 80311ACC-80311B80 30C40C 00B4+00 0/0 3/3 0/0 .text
 * J3DGetTextureMtx__FRC17J3DTextureSRTInfoRC3VecPA4_f          */
// matches with literals
#ifdef NONMATCHING
void J3DGetTextureMtx(const J3DTextureSRTInfo& srt, const Vec& center, Mtx dst) {
    f32 sr = JMASSin(srt.mRotation), cr = JMASCos(srt.mRotation);

    f32 cx = srt.mScaleX * cr;
    f32 sx = srt.mScaleX * sr;
    f32 sy = srt.mScaleY * sr;
    f32 cy = srt.mScaleY * cr;

    dst[0][0] = cx;
    dst[0][1] = -sx;
    dst[0][2] = (-cx * center.x + sx * center.y) + center.x + srt.mTranslationX;

    dst[1][0] = sy;
    dst[1][1] = cy;
    dst[1][2] = (-sy * center.x - cy * center.y) + center.y + srt.mTranslationY;

    dst[2][3] = 0.0f;
    dst[2][1] = 0.0f;
    dst[2][0] = 0.0f;
    dst[1][3] = 0.0f;
    dst[0][3] = 0.0f;
    dst[2][2] = 1.0f;
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DGetTextureMtx(J3DTextureSRTInfo const& param_0, Vec const& param_1, f32 (*param_2)[4]) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DGetTextureMtx__FRC17J3DTextureSRTInfoRC3VecPA4_f.s"
}
#pragma pop
#endif

/* 80311B80-80311C34 30C4C0 00B4+00 0/0 3/3 0/0 .text
 * J3DGetTextureMtxOld__FRC17J3DTextureSRTInfoRC3VecPA4_f       */
// matches with literals
#ifdef NONMATCHING
void J3DGetTextureMtxOld(const J3DTextureSRTInfo& srt, const Vec& center, Mtx dst) {
    f32 sr = JMASSin(srt.mRotation), cr = JMASCos(srt.mRotation);

    f32 cx = srt.mScaleX * cr;
    f32 sx = srt.mScaleX * sr;
    f32 sy = srt.mScaleY * sr;
    f32 cy = srt.mScaleY * cr;

    dst[0][0] = cx;
    dst[0][1] = -sx;
    dst[0][3] = (-cx * center.x + sx * center.y) + center.x + srt.mTranslationX;

    dst[1][0] = sy;
    dst[1][1] = cy;
    dst[1][3] = (-sy * center.x - cy * center.y) + center.y + srt.mTranslationY;

    dst[2][3] = 0.0f;
    dst[2][1] = 0.0f;
    dst[2][0] = 0.0f;
    dst[1][2] = 0.0f;
    dst[0][2] = 0.0f;
    dst[2][2] = 1.0f;
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DGetTextureMtxOld(J3DTextureSRTInfo const& param_0, Vec const& param_1,
                             f32 (*param_2)[4]) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DGetTextureMtxOld__FRC17J3DTextureSRTInfoRC3VecPA4_f.s"
}
#pragma pop
#endif

/* ############################################################################################## */
/* 80456380-80456388 004980 0004+04 2/2 0/0 0/0 .sdata2          @557 */
SECTION_SDATA2 static f32 lit_557[1 + 1 /* padding */] = {
    0.5f,
    /* padding */
    0.0f,
};

/* 80311C34-80311CE4 30C574 00B0+00 0/0 3/3 0/0 .text
 * J3DGetTextureMtxMaya__FRC17J3DTextureSRTInfoPA4_f            */
// matches with literals
#ifdef NONMATCHING
void J3DGetTextureMtxMaya(const J3DTextureSRTInfo& srt, Mtx dst) {
    f32 sr = JMASSin(srt.mRotation), cr = JMASCos(srt.mRotation);
    f32 tx = srt.mTranslationX - 0.5f;
    f32 ty = srt.mTranslationY - 0.5f;

    dst[0][0] = srt.mScaleX * cr;
    dst[0][1] = srt.mScaleY * sr;
    dst[0][2] = tx * cr - sr * (ty + srt.mScaleY) + 0.5f;

    dst[1][0] = -srt.mScaleX * sr;
    dst[1][1] = srt.mScaleY * cr;
    dst[1][2] = -tx * sr - cr * (ty + srt.mScaleY) + 0.5f;

    dst[2][3] = 0.0f;
    dst[2][1] = 0.0f;
    dst[2][0] = 0.0f;
    dst[1][3] = 0.0f;
    dst[0][3] = 0.0f;
    dst[2][2] = 1.0f;
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DGetTextureMtxMaya(J3DTextureSRTInfo const& param_0, f32 (*param_1)[4]) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DGetTextureMtxMaya__FRC17J3DTextureSRTInfoPA4_f.s"
}
#pragma pop
#endif

/* 80311CE4-80311D94 30C624 00B0+00 0/0 3/3 0/0 .text
 * J3DGetTextureMtxMayaOld__FRC17J3DTextureSRTInfoPA4_f         */
// matches with literals
#ifdef NONMATCHING
void J3DGetTextureMtxMayaOld(const J3DTextureSRTInfo& srt, Mtx dst) {
    f32 sr = JMASSin(srt.mRotation), cr = JMASCos(srt.mRotation);
    f32 tx = srt.mTranslationX - 0.5f;
    f32 ty = srt.mTranslationY - 0.5f;

    dst[0][0] = srt.mScaleX * cr;
    dst[0][1] = srt.mScaleY * sr;
    dst[0][3] = tx * cr - sr * (ty + srt.mScaleY) + 0.5f;

    dst[1][0] = -srt.mScaleX * sr;
    dst[1][1] = srt.mScaleY * cr;
    dst[1][3] = -tx * sr - cr * (ty + srt.mScaleY) + 0.5f;

    dst[2][3] = 0.0f;
    dst[2][1] = 0.0f;
    dst[2][0] = 0.0f;
    dst[1][2] = 0.0f;
    dst[0][2] = 0.0f;
    dst[2][2] = 1.0f;
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DGetTextureMtxMayaOld(J3DTextureSRTInfo const& param_0, f32 (*param_1)[4]) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DGetTextureMtxMayaOld__FRC17J3DTextureSRTInfoPA4_f.s"
}
#pragma pop
#endif

/* 80311D94-80311DF8 30C6D4 0064+00 0/0 2/2 0/0 .text            J3DScaleNrmMtx__FPA4_fRC3Vec */
#ifdef NONMATCHING
void J3DScaleNrmMtx(register Mtx mtx, const register Vec& scl) {
    register f32 mtx_xy, mtx_z_, scl_xy, scl_z_;

    asm {
        /* Row 0 */
        psq_l  scl_xy, 0(scl), 0, 0
        psq_l  mtx_xy, 0(mtx), 0, 0
        lfs    scl_z_, 8(scl)
        lfs    mtx_z_, 8(mtx)
        ps_mul f4, mtx_xy, scl_xy
        psq_st f4, 0(mtx), 0, 0
        fmuls  f4, mtx_z_, scl_z_
        stfs   f4, 8(mtx)

        /* Row 1 */
        psq_l  scl_xy, 0(scl), 0, 0
        psq_l  mtx_xy, 16(mtx), 0, 0
        lfs    scl_z_, 8(scl)
        lfs    mtx_z_, 24(mtx)
        ps_mul f4, mtx_xy, scl_xy
        psq_st f4, 16(mtx), 0, 0
        fmuls  f4, mtx_z_, scl_z_
        stfs   f4, 24(mtx)

        /* Row 2 */
        psq_l  scl_xy, 0(scl), 0, 0
        psq_l  mtx_xy, 32(mtx), 0, 0
        lfs    scl_z_, 8(scl)
        lfs    mtx_z_, 40(mtx)
        ps_mul f4, mtx_xy, scl_xy
        psq_st f4, 32(mtx), 0, 0
        fmuls  f4, mtx_z_, scl_z_
        stfs   f4, 40(mtx)
    }
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DScaleNrmMtx(f32 (*param_0)[4], Vec const& param_1) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DScaleNrmMtx__FPA4_fRC3Vec.s"
}
#pragma pop
#endif

/* 80311DF8-80311E4C 30C738 0054+00 0/0 5/5 0/0 .text            J3DScaleNrmMtx33__FPA3_fRC3Vec */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DScaleNrmMtx33(f32 (*param_0)[3], Vec const& param_1) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DScaleNrmMtx33__FPA3_fRC3Vec.s"
}
#pragma pop

/* 80311E4C-80311F70 30C78C 0124+00 0/0 3/3 0/0 .text            J3DMtxProjConcat__FPA4_fPA4_fPA4_f
 */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DMtxProjConcat(f32 (*param_0)[4], f32 (*param_1)[4], f32 (*param_2)[4]) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DMtxProjConcat__FPA4_fPA4_fPA4_f.s"
}
#pragma pop

/* ############################################################################################## */
/* 80450958-80450960 0003D8 0008+00 1/1 0/0 0/0 .sdata           Unit01 */
SECTION_SDATA static u8 Unit01[8] = {
    0x00, 0x00, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00,
};

/* 80311F70-8031204C 30C8B0 00DC+00 0/0 1/1 0/0 .text J3DPSMtxArrayConcat__FPA4_fPA4_fPA4_fUl */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void J3DPSMtxArrayConcat(f32 (*param_0)[4], f32 (*param_1)[4], f32 (*param_2)[4], u32 param_3) {
    nofralloc
#include "asm/JSystem/J3DGraphBase/J3DTransform/J3DPSMtxArrayConcat__FPA4_fPA4_fPA4_fUl.s"
}
#pragma pop

/* ############################################################################################## */
/* 803CD8F8-803CD900 02AA18 0008+00 0/0 2/2 0/0 .data            PSMulUnit01 */
SECTION_DATA extern f32 PSMulUnit01[2] = {
    0.0f,
    -1.0f,
};