-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpu_3d.cpp
1860 lines (1605 loc) · 54.4 KB
/
gpu_3d.cpp
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019-2020 Hydr8gon
This file is part of NooDS.
NooDS is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NooDS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with NooDS. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstring>
#include "gpu_3d.h"
#include "core.h"
Gpu3D::Gpu3D(Core *core): core(core)
{
// Set the parameter counts
paramCounts[0x10] = 1;
paramCounts[0x11] = 0;
paramCounts[0x12] = 1;
paramCounts[0x13] = 1;
paramCounts[0x14] = 1;
paramCounts[0x15] = 0;
paramCounts[0x16] = 16;
paramCounts[0x17] = 12;
paramCounts[0x18] = 16;
paramCounts[0x19] = 12;
paramCounts[0x1A] = 9;
paramCounts[0x1B] = 3;
paramCounts[0x1C] = 3;
paramCounts[0x20] = 1;
paramCounts[0x21] = 1;
paramCounts[0x22] = 1;
paramCounts[0x23] = 2;
paramCounts[0x24] = 1;
paramCounts[0x25] = 1;
paramCounts[0x26] = 1;
paramCounts[0x27] = 1;
paramCounts[0x28] = 1;
paramCounts[0x29] = 1;
paramCounts[0x2A] = 1;
paramCounts[0x2B] = 1;
paramCounts[0x30] = 1;
paramCounts[0x31] = 1;
paramCounts[0x32] = 1;
paramCounts[0x33] = 1;
paramCounts[0x34] = 32;
paramCounts[0x40] = 1;
paramCounts[0x41] = 0;
paramCounts[0x50] = 1;
paramCounts[0x60] = 1;
paramCounts[0x70] = 3;
paramCounts[0x71] = 2;
paramCounts[0x72] = 1;
}
uint32_t Gpu3D::rgb5ToRgb6(uint16_t color)
{
// Convert an RGB5 value to an RGB6 value (the way the 3D engine does it)
uint8_t r = ((color >> 0) & 0x1F) * 2; if (r > 0) r++;
uint8_t g = ((color >> 5) & 0x1F) * 2; if (g > 0) g++;
uint8_t b = ((color >> 10) & 0x1F) * 2; if (b > 0) b++;
return (b << 12) | (g << 6) | r;
}
void Gpu3D::runCycle()
{
// Fetch the next geometry command
Entry entry = pipe.front();
pipe.pop();
// Execute the geometry command
switch (entry.command)
{
case 0x10: mtxModeCmd(entry.param); break; // MTX_MODE
case 0x11: mtxPushCmd(); break; // MTX_PUSH
case 0x12: mtxPopCmd(entry.param); break; // MTX_POP
case 0x13: mtxStoreCmd(entry.param); break; // MTX_STORE
case 0x14: mtxRestoreCmd(entry.param); break; // MTX_RESTORE
case 0x15: mtxIdentityCmd(); break; // MTX_IDENTITY
case 0x16: mtxLoad44Cmd(entry.param); break; // MTX_LOAD_4x4
case 0x17: mtxLoad43Cmd(entry.param); break; // MTX_LOAD_4x3
case 0x18: mtxMult44Cmd(entry.param); break; // MTX_MULT_4x4
case 0x19: mtxMult43Cmd(entry.param); break; // MTX_MULT_4x3
case 0x1A: mtxMult33Cmd(entry.param); break; // MTX_MULT_3x3
case 0x1B: mtxScaleCmd(entry.param); break; // MTX_SCALE
case 0x1C: mtxTransCmd(entry.param); break; // MTX_TRANS
case 0x20: colorCmd(entry.param); break; // COLOR
case 0x21: normalCmd(entry.param); break; // NORMAL
case 0x22: texCoordCmd(entry.param); break; // TEXCOORD
case 0x23: vtx16Cmd(entry.param); break; // VTX_16
case 0x24: vtx10Cmd(entry.param); break; // VTX_10
case 0x25: vtxXYCmd(entry.param); break; // VTX_XY
case 0x26: vtxXZCmd(entry.param); break; // VTX_XZ
case 0x27: vtxYZCmd(entry.param); break; // VTX_YZ
case 0x28: vtxDiffCmd(entry.param); break; // VTX_DIFF
case 0x29: polygonAttrCmd(entry.param); break; // POLYGON_ATTR
case 0x2A: texImageParamCmd(entry.param); break; // TEXIMAGE_PARAM
case 0x2B: plttBaseCmd(entry.param); break; // PLTT_BASE
case 0x30: difAmbCmd(entry.param); break; // DIF_AMB
case 0x31: speEmiCmd(entry.param); break; // SPE_EMI
case 0x32: lightVectorCmd(entry.param); break; // LIGHT_VECTOR
case 0x33: lightColorCmd(entry.param); break; // LIGHT_COLOR
case 0x34: shininessCmd(entry.param); break; // SHININESS
case 0x40: beginVtxsCmd(entry.param); break; // BEGIN_VTXS
case 0x41: break; // END_VTXS
case 0x50: swapBuffersCmd(entry.param); break; // SWAP_BUFFERS
case 0x60: viewportCmd(entry.param); break; // VIEWPORT
case 0x70: boxTestCmd(entry.param); break; // BOX_TEST
case 0x71: posTestCmd(entry.param); break; // POS_TEST
case 0x72: vecTestCmd(entry.param); break; // VEC_TEST
default:
{
printf("Unknown GXFIFO command: 0x%X\n", entry.command);
break;
}
}
// Keep track of how many parameters have been sent
paramCount++;
if (paramCount >= paramCounts[entry.command])
paramCount = 0;
// Move 2 FIFO entries into the PIPE if it runs half empty
if (pipe.size() < 3)
{
for (int i = 0; i < ((fifo.size() > 2) ? 2 : fifo.size()); i++)
{
pipe.push(fifo.front());
fifo.pop();
}
}
// Update the FIFO status
gxStat = (gxStat & ~0x00001F00) | (coordinatePtr << 8); // Coordinate stack pointer
gxStat = (gxStat & ~0x00002000) | (projectionPtr << 13); // Projection stack pointer
gxStat = (gxStat & ~0x01FF0000) | (fifo.size() << 16); // FIFO entries
if (fifo.size() == 0) gxStat |= BIT(26); // Empty
if (pipe.size() == 0) gxStat &= ~BIT(27); // Commands not executing
// If the FIFO becomes less than half full, trigger GXFIFO DMA transfers
// If the FIFO is already less than half full when a DMA starts, it will automatically activate
if (fifo.size() < 128 && !(gxStat & BIT(25)))
{
gxStat |= BIT(25);
core->dma[0].trigger(7);
}
// Send a GXFIFO interrupt if enabled
switch ((gxStat & 0xC0000000) >> 30)
{
case 1: if (gxStat & BIT(25)) core->interpreter[0].sendInterrupt(21); break;
case 2: if (gxStat & BIT(26)) core->interpreter[0].sendInterrupt(21); break;
}
}
void Gpu3D::swapBuffers()
{
// Process the vertices
for (int i = 0; i < vertexCountIn; i++)
{
if (verticesIn[i].w != 0)
{
// Normalize and scale the vertices to the viewport
// X coordinates are 9-bit and Y coordinates are 8-bit; invalid viewports can cause wraparound
// Z coordinates (and depth values in general) are 24-bit
verticesIn[i].x = (( (int64_t)verticesIn[i].x + verticesIn[i].w) * viewportWidth / (verticesIn[i].w * 2) + viewportX) & 0x1FF;
verticesIn[i].y = ((-(int64_t)verticesIn[i].y + verticesIn[i].w) * viewportHeight / (verticesIn[i].w * 2) + viewportY) & 0xFF;
verticesIn[i].z = (((((int64_t)verticesIn[i].z << 14) / verticesIn[i].w) + 0x3FFF) << 9) & 0xFFFFFF;
}
else
{
// The W coordinate is invalid, so not much can be done
verticesIn[i].x = 0;
verticesIn[i].y = 0;
verticesIn[i].z = 0;
}
}
// Determine each polygon's W-shift value to be used for reducing (or expanding) W values to 16 bits
for (int i = 0; i < polygonCountIn; i++)
{
_Polygon *p = &polygonsIn[i];
// Reduce precision in 4-bit increments until all W values fit in the 16-bit range
for (int j = 0; j < p->size; j++)
{
while (((uint32_t)p->vertices[j].w >> p->wShift) > 0xFFFF)
p->wShift += 4;
}
// If precision wasn't reduced, increase it in 4-bit increments until a W value no longer fits in the 16-bit range
if (p->wShift == 0)
{
while (true)
{
for (int j = 0; j < p->size; j++)
{
if (p->vertices[j].w == 0 || ((uint32_t)p->vertices[j].w << -(p->wShift - 4)) > 0xFFFF)
goto out;
}
p->wShift -= 4;
}
out:;
}
}
// Swap the vertex buffers
SWAP(verticesOut, verticesIn);
vertexCountOut = vertexCountIn;
vertexCountIn = 0;
vertexCount = 0;
// Swap the polygon buffers
SWAP(polygonsOut, polygonsIn);
polygonCountOut = polygonCountIn;
polygonCountIn = 0;
// Unhalt the geometry engine
halted = false;
core->gpu.invalidate3D();
}
Matrix Gpu3D::multiply(Matrix *mtx1, Matrix *mtx2)
{
Matrix matrix;
// Multiply 2 matrices
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
int64_t value = 0;
for (int i = 0; i < 4; i++) value += (int64_t)mtx1->data[y * 4 + i] * mtx2->data[i * 4 + x];
matrix.data[y * 4 + x] = value >> 12;
}
}
return matrix;
}
Vertex Gpu3D::multiply(Vertex *vtx, Matrix *mtx)
{
Vertex vertex = *vtx;
// Multiply a vertex with a matrix
vertex.x = ((int64_t)vtx->x * mtx->data[0] + (int64_t)vtx->y * mtx->data[4] + (int64_t)vtx->z * mtx->data[8] + (int64_t)vtx->w * mtx->data[12]) >> 12;
vertex.y = ((int64_t)vtx->x * mtx->data[1] + (int64_t)vtx->y * mtx->data[5] + (int64_t)vtx->z * mtx->data[9] + (int64_t)vtx->w * mtx->data[13]) >> 12;
vertex.z = ((int64_t)vtx->x * mtx->data[2] + (int64_t)vtx->y * mtx->data[6] + (int64_t)vtx->z * mtx->data[10] + (int64_t)vtx->w * mtx->data[14]) >> 12;
vertex.w = ((int64_t)vtx->x * mtx->data[3] + (int64_t)vtx->y * mtx->data[7] + (int64_t)vtx->z * mtx->data[11] + (int64_t)vtx->w * mtx->data[15]) >> 12;
return vertex;
}
int32_t Gpu3D::multiply(Vertex *vec1, Vertex *vec2)
{
// Multiply 2 vectors
return ((int64_t)vec1->x * vec2->x + (int64_t)vec1->y * vec2->y + (int64_t)vec1->z * vec2->z) >> 12;
}
void Gpu3D::addVertex()
{
if (vertexCountIn >= 6144) return;
// Set the new vertex
verticesIn[vertexCountIn] = savedVertex;
verticesIn[vertexCountIn].w = 1 << 12;
// Transform the texture coordinates
if (textureCoordMode == 3)
{
// Get the texture matrix with the texture coordinates
Matrix matrix = texture;
matrix.data[12] = (int32_t)s << 12;
matrix.data[13] = (int32_t)t << 12;
// Multiply the vertex with the texture matrix
Vertex vertex = multiply(&verticesIn[vertexCountIn], &matrix);
// Save the transformed coordinates
verticesIn[vertexCountIn].s = vertex.x >> 12;
verticesIn[vertexCountIn].t = vertex.y >> 12;
}
// Update the clip matrix if necessary
if (clipDirty)
{
clip = multiply(&coordinate, &projection);
clipDirty = false;
}
// Transform the vertex
verticesIn[vertexCountIn] = multiply(&verticesIn[vertexCountIn], &clip);
// Move to the next vertex
vertexCountIn++;
vertexCount++;
// Move to the next polygon if one has been completed
switch (polygonType)
{
case 0: if (vertexCount % 3 == 0) addPolygon(); break; // Separate triangles
case 1: if (vertexCount % 4 == 0) addPolygon(); break; // Separate quads
case 2: if (vertexCount >= 3) addPolygon(); break; // Triangle strips
case 3: if (vertexCount >= 4 && vertexCount % 2 == 0) addPolygon(); break; // Quad strips
}
}
void Gpu3D::addPolygon()
{
if (polygonCountIn >= 2048) return;
// Set the polygon vertex information
int size = 3 + (polygonType & 1);
savedPolygon.size = size;
savedPolygon.vertices = &verticesIn[vertexCountIn - size];
// Save a copy of the unclipped vertices
Vertex unclipped[10];
memcpy(unclipped, savedPolygon.vertices, size * sizeof(Vertex));
// Rearrange quad strip vertices to be counter-clockwise
if (polygonType == 3)
{
Vertex vertex = unclipped[2];
unclipped[2] = unclipped[3];
unclipped[3] = vertex;
}
// Clip the polygon
Vertex clipped[10];
bool clip = clipPolygon(unclipped, clipped, &savedPolygon.size);
// Calculate the cross product of the normalized polygon vertices to determine orientation
int64_t cross = 0;
if (savedPolygon.size >= 3)
{
cross = (((int64_t)clipped[1].x << 12) / clipped[1].w - ((int64_t)clipped[0].x << 12) / clipped[0].w) *
(((int64_t)clipped[2].y << 12) / clipped[2].w - ((int64_t)clipped[0].y << 12) / clipped[0].w) -
(((int64_t)clipped[1].y << 12) / clipped[1].w - ((int64_t)clipped[0].y << 12) / clipped[0].w) *
(((int64_t)clipped[2].x << 12) / clipped[2].w - ((int64_t)clipped[0].x << 12) / clipped[0].w);
}
// Every other triangle strip polygon is stored clockwise instead of counter-clockwise
// Keep track of this, and reverse the cross product of clockwise polygons to accomodate
if (polygonType == 2)
{
if (clockwise) cross = -cross;
clockwise = !clockwise;
}
// Discard polygons that are outside of the view area or should be culled
if (savedPolygon.size == 0 || (!renderFront && cross > 0) || (!renderBack && cross < 0))
{
switch (polygonType)
{
case 0: case 1: // Separate polygons
{
// Discard the vertices
vertexCountIn -= size;
return;
}
case 2: // Triangle strips
{
if (vertexCount == 3) // First triangle in the strip
{
// Discard the first vertex, but keep the other 2 for the next triangle
verticesIn[vertexCountIn - 3] = verticesIn[vertexCountIn - 2];
verticesIn[vertexCountIn - 2] = verticesIn[vertexCountIn - 1];
vertexCountIn--;
vertexCount--;
}
else if (vertexCountIn < 6144)
{
// End the previous strip, and start a new one with the last 2 vertices
verticesIn[vertexCountIn - 0] = verticesIn[vertexCountIn - 1];
verticesIn[vertexCountIn - 1] = verticesIn[vertexCountIn - 2];
vertexCountIn++;
vertexCount = 2;
}
return;
}
case 3: // Quad strips
{
if (vertexCount == 4) // First quad in the strip
{
// Discard the first 2 vertices, but keep the other 2 for the next quad
verticesIn[vertexCountIn - 4] = verticesIn[vertexCountIn - 2];
verticesIn[vertexCountIn - 3] = verticesIn[vertexCountIn - 1];
vertexCountIn -= 2;
vertexCount -= 2;
}
else
{
// End the previous strip, and start a new one with the last 2 vertices
vertexCount = 2;
}
return;
}
}
}
// Update the vertices of clipped polygons
if (clip)
{
switch (polygonType)
{
case 0: case 1: // Separate polygons
{
// Remove the unclipped vertices
vertexCountIn -= size;
// Add the clipped vertices
for (int i = 0; i < savedPolygon.size; i++)
{
if (vertexCountIn >= 6144) return;
verticesIn[vertexCountIn] = clipped[i];
vertexCountIn++;
}
break;
}
case 2: // Triangle strips
{
// Remove the unclipped vertices
vertexCountIn -= (vertexCount == 3) ? 3 : 1;
savedPolygon.vertices = &verticesIn[vertexCountIn];
// Add the clipped vertices
for (int i = 0; i < savedPolygon.size; i++)
{
if (vertexCountIn >= 6144) return;
verticesIn[vertexCountIn] = clipped[i];
vertexCountIn++;
}
// End the previous strip, and start a new one with the last 2 vertices
for (int i = 0; i < 2; i++)
{
if (vertexCountIn >= 6144) return;
verticesIn[vertexCountIn] = unclipped[1 + i];
vertexCountIn++;
}
vertexCount = 2;
break;
}
case 3: // Quad strips
{
// Remove the unclipped vertices
vertexCountIn -= (vertexCount == 4) ? 4 : 2;
savedPolygon.vertices = &verticesIn[vertexCountIn];
// Add the clipped vertices
for (int i = 0; i < savedPolygon.size; i++)
{
if (vertexCountIn >= 6144) return;
verticesIn[vertexCountIn] = clipped[i];
vertexCountIn++;
}
// End the previous strip, and start a new one with the last 2 vertices
for (int i = 0; i < 2; i++)
{
if (vertexCountIn >= 6144) return;
verticesIn[vertexCountIn] = unclipped[3 - i];
vertexCountIn++;
}
vertexCount = 2;
break;
}
}
}
// Set the new polygon
polygonsIn[polygonCountIn] = savedPolygon;
polygonsIn[polygonCountIn].crossed = (polygonType == 3 && !clip);
polygonsIn[polygonCountIn].paletteAddr <<= 4 - (savedPolygon.textureFmt == 2);
// Move to the next polygon
polygonCountIn++;
}
Vertex Gpu3D::intersection(Vertex *vtx1, Vertex *vtx2, int32_t val1, int32_t val2)
{
Vertex vertex;
// Calculate the interpolation coefficients
int64_t d1 = val1 + vtx1->w;
int64_t d2 = val2 + vtx2->w;
if (d2 == d1) return *vtx1;
// Interpolate the vertex coordinates
vertex.x = ((d2 * vtx1->x) - (d1 * vtx2->x)) / (d2 - d1);
vertex.y = ((d2 * vtx1->y) - (d1 * vtx2->y)) / (d2 - d1);
vertex.z = ((d2 * vtx1->z) - (d1 * vtx2->z)) / (d2 - d1);
vertex.w = ((d2 * vtx1->w) - (d1 * vtx2->w)) / (d2 - d1);
vertex.s = ((d2 * vtx1->s) - (d1 * vtx2->s)) / (d2 - d1);
vertex.t = ((d2 * vtx1->t) - (d1 * vtx2->t)) / (d2 - d1);
// Interpolate the vertex color
uint8_t r = ((d2 * ((vtx1->color >> 0) & 0x3F)) - (d1 * ((vtx2->color >> 0) & 0x3F))) / (d2 - d1);
uint8_t g = ((d2 * ((vtx1->color >> 6) & 0x3F)) - (d1 * ((vtx2->color >> 6) & 0x3F))) / (d2 - d1);
uint8_t b = ((d2 * ((vtx1->color >> 12) & 0x3F)) - (d1 * ((vtx2->color >> 12) & 0x3F))) / (d2 - d1);
vertex.color = (vtx1->color & 0xFC0000) | (b << 12) | (g << 6) | r;
return vertex;
}
bool Gpu3D::clipPolygon(Vertex *unclipped, Vertex *clipped, int *size)
{
bool clip = false;
// Save a copy of the original unclipped vertices
Vertex original[4];
memcpy(original, unclipped, 4 * sizeof(Vertex));
// Clip a polygon using the Sutherland-Hodgman algorithm
for (int i = 0; i < 6; i++)
{
int oldSize = *size;
*size = 0;
for (int j = 0; j < oldSize; j++)
{
// Get the unclipped vertices
Vertex *current = &unclipped[j];
Vertex *previous = &unclipped[(j - 1 + oldSize) % oldSize];
// Choose which coordinates to check based on the current side being clipped against
int32_t currentVal, previousVal;
switch (i)
{
case 0: currentVal = current->x; previousVal = previous->x; break;
case 1: currentVal = -current->x; previousVal = -previous->x; break;
case 2: currentVal = current->y; previousVal = previous->y; break;
case 3: currentVal = -current->y; previousVal = -previous->y; break;
case 4: currentVal = current->z; previousVal = previous->z; break;
case 5: currentVal = -current->z; previousVal = -previous->z; break;
}
// Add the clipped vertices
if (currentVal >= -current->w) // Current vertex in bounds
{
if (previousVal < -previous->w) // Previous vertex not in bounds
{
clipped[(*size)++] = intersection(current, previous, currentVal, previousVal);
clip = true;
}
clipped[(*size)++] = *current;
}
else if (previousVal >= -previous->w) // Previous vertex in bounds
{
clipped[(*size)++] = intersection(current, previous, currentVal, previousVal);
clip = true;
}
}
// Copy the new vertices to unclipped so they'll be used in the next iteration
if (i < 5) memcpy(unclipped, clipped, *size * sizeof(Vertex));
}
// Restore the original unclipped vertices
memcpy(unclipped, original, 4 * sizeof(Vertex));
return clip;
}
void Gpu3D::mtxModeCmd(uint32_t param)
{
// Set the matrix mode
matrixMode = param & 0x00000003;
}
void Gpu3D::mtxPushCmd()
{
// Push the current matrix onto a stack
switch (matrixMode)
{
case 0: // Projection stack
{
if (projectionPtr < 1)
{
// Push to the single projection stack slot and increment the pointer
projectionStack = projection;
projectionPtr++;
}
else
{
// Indicate a matrix stack overflow error
gxStat |= BIT(15);
}
break;
}
case 1: case 2: // Coordinate and directional stacks
{
// Indicate a matrix stack overflow error
// Even though the 31st slot exists, it still causes an overflow error
if (coordinatePtr >= 30) gxStat |= BIT(15);
// Push to the current coordinate and directional stack slots and increment the pointer
if (coordinatePtr < 31)
{
coordinateStack[coordinatePtr] = coordinate;
directionStack[coordinatePtr] = direction;
coordinatePtr++;
}
break;
}
case 3: // Texture stack
{
// Push to the single texture stack slot
textureStack = texture;
break;
}
}
}
void Gpu3D::mtxPopCmd(uint32_t param)
{
// Pop a matrix from a stack
switch (matrixMode)
{
case 0: // Projection stack
{
if (projectionPtr > 0)
{
// Pop from the single projection stack slot and decrement the pointer
projectionPtr--;
projection = projectionStack;
clipDirty = true;
}
else
{
// Indicate a matrix stack underflow error
gxStat |= BIT(15);
}
break;
}
case 1: case 2: // Coordinate and directional stacks
{
// Calculate the stack address to pop from
int address = coordinatePtr - (((param & BIT(5)) ? 0xFFFFFFC0 : 0) | (param & 0x0000003F));
// Indicate a matrix stack underflow or overflow error
// Even though the 31st slot exists, it still causes an overflow error
if (address < 0 || address >= 30) gxStat |= BIT(15);
// Pop from the current coordinate and directional stack slots and update the pointer
if (address >= 0 && address < 31)
{
coordinate = coordinateStack[address];
direction = directionStack[address];
coordinatePtr = address;
clipDirty = true;
}
break;
}
case 3: // Texture stack
{
// Pop from the single texture stack slot
texture = textureStack;
break;
}
}
}
void Gpu3D::mtxStoreCmd(uint32_t param)
{
// Store a matrix to the stack
switch (matrixMode)
{
case 0: // Projection stack
{
// Store to the single projection stack slot
projectionStack = projection;
break;
}
case 1: case 2: // Coordinate and directional stacks
{
// Get the stack address to store to
int address = param & 0x0000001F;
// Indicate a matrix stack overflow error
// Even though the 31st slot exists, it still causes an overflow error
if (address == 31) gxStat |= BIT(15);
// Store to the current coordinate and directional stack slots
coordinateStack[address] = coordinate;
directionStack[address] = direction;
break;
}
case 3: // Texture stack
{
// Store to the single texture stack slot
textureStack = texture;
break;
}
}
}
void Gpu3D::mtxRestoreCmd(uint32_t param)
{
// Restore a matrix from the stack
switch (matrixMode)
{
case 0: // Projection stack
{
// Restore from the single projection stack slot
projection = projectionStack;
clipDirty = true;
break;
}
case 1: case 2: // Coordinate and directional stacks
{
// Get the stack address to store to
int address = param & 0x0000001F;
// Indicate a matrix stack overflow error
// Even though the 31st slot exists, it still causes an overflow error
if (address == 31) gxStat |= BIT(15);
// Restore from the current coordinate and directional stack slots
coordinate = coordinateStack[address];
direction = directionStack[address];
clipDirty = true;
break;
}
case 3: // Texture stack
{
// Restore from the single texture stack slot
texture = textureStack;
break;
}
}
}
void Gpu3D::mtxIdentityCmd()
{
// Set a matrix to the identity matrix
switch (matrixMode)
{
case 0: // Projection stack
{
projection = Matrix();
clipDirty = true;
break;
}
case 1: // Coordinate stack
{
coordinate = Matrix();
clipDirty = true;
break;
}
case 2: // Coordinate and directional stacks
{
coordinate = Matrix();
direction = Matrix();
clipDirty = true;
break;
}
case 3: // Texture stack
{
texture = Matrix();
break;
}
}
}
void Gpu3D::mtxLoad44Cmd(uint32_t param)
{
// Store the paramaters to the temporary matrix
temp.data[paramCount] = (int32_t)param;
if (paramCount < 15) return;
// Set a 4x4 matrix
switch (matrixMode)
{
case 0: // Projection stack
{
projection = temp;
clipDirty = true;
break;
}
case 1: // Coordinate stack
{
coordinate = temp;
clipDirty = true;
break;
}
case 2: // Coordinate and directional stacks
{
coordinate = temp;
direction = temp;
clipDirty = true;
break;
}
case 3: // Texture stack
{
texture = temp;
break;
}
}
}
void Gpu3D::mtxLoad43Cmd(uint32_t param)
{
// Store the paramaters to the temporary matrix
if (paramCount == 0) temp = Matrix();
temp.data[(paramCount / 3) * 4 + paramCount % 3] = (int32_t)param;
if (paramCount < 11) return;
// Set a 4x3 matrix
switch (matrixMode)
{
case 0: // Projection stack
{
projection = temp;
clipDirty = true;
break;
}
case 1: // Coordinate stack
{
coordinate = temp;
clipDirty = true;
break;
}
case 2: // Coordinate and directional stacks
{
coordinate = temp;
direction = temp;
clipDirty = true;
break;
}
case 3: // Texture stack
{
texture = temp;
break;
}
}
}
void Gpu3D::mtxMult44Cmd(uint32_t param)
{
// Store the paramaters to the temporary matrix
temp.data[paramCount] = (int32_t)param;
if (paramCount < 15) return;
// Multiply a matrix by a 4x4 matrix
switch (matrixMode)
{
case 0: // Projection stack
{
projection = multiply(&temp, &projection);
clipDirty = true;
break;
}
case 1: // Coordinate stack
{
coordinate = multiply(&temp, &coordinate);
clipDirty = true;
break;
}
case 2: // Coordinate and directional stacks
{
coordinate = multiply(&temp, &coordinate);
direction = multiply(&temp, &direction);
clipDirty = true;
break;
}
case 3: // Texture stack
{
texture = multiply(&temp, &texture);
break;
}
}
}
void Gpu3D::mtxMult43Cmd(uint32_t param)
{
// Store the paramaters to the temporary matrix
if (paramCount == 0) temp = Matrix();
temp.data[(paramCount / 3) * 4 + paramCount % 3] = (int32_t)param;
if (paramCount < 11) return;
// Multiply a matrix by a 4x3 matrix
switch (matrixMode)
{
case 0: // Projection stack
{
projection = multiply(&temp, &projection);
clipDirty = true;
break;
}
case 1: // Coordinate stack
{
coordinate = multiply(&temp, &coordinate);
clipDirty = true;
break;
}
case 2: // Coordinate and directional stacks
{
coordinate = multiply(&temp, &coordinate);
direction = multiply(&temp, &direction);
clipDirty = true;
break;
}
case 3: // Texture stack
{
texture = multiply(&temp, &texture);
break;
}
}
}
void Gpu3D::mtxMult33Cmd(uint32_t param)
{
// Store the paramaters to the temporary matrix
if (paramCount == 0) temp = Matrix();
temp.data[(paramCount / 3) * 4 + paramCount % 3] = (int32_t)param;
if (paramCount < 8) return;
// Multiply a matrix by a 3x3 matrix
switch (matrixMode)
{
case 0: // Projection stack
{
projection = multiply(&temp, &projection);
clipDirty = true;
break;
}
case 1: // Coordinate stack
{
coordinate = multiply(&temp, &coordinate);
clipDirty = true;
break;
}
case 2: // Coordinate and directional stacks
{
coordinate = multiply(&temp, &coordinate);
direction = multiply(&temp, &direction);
clipDirty = true;
break;
}
case 3: // Texture stack
{
texture = multiply(&temp, &texture);
break;
}
}
}
void Gpu3D::mtxScaleCmd(uint32_t param)
{
// Store the paramaters to the temporary matrix