aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/smd/smd_cel.s
blob: 8a39f01b056f50b55ef64f7a8b4c3194922e11e2 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# PSn00bSDK .SMD model parser library
# (C) 2019-2023 Lameguy64, spicyjpeg - MPL licensed

.set noreorder

.include "gtereg.inc"
.include "inline_s.inc"
.include "smd/smd_s.inc"

.section .text.smdSetCelTex, "ax", @progbits
.global smdSetCelTex
.type smdSetCelTex, @function

smdSetCelTex:
	# a0 - TPage value
	# a1 - CLUT value
	la		$v0, _smd_cel_tpage
	andi	$a0, 0xffff
	sll		$a1, 16
	or		$a0, $a1
	jr		$ra
	sw		$a0, 0($v0)

.section .text.smdSetCelParam, "ax", @progbits
.global smdSetCelParam
.type smdSetCelParam, @function

smdSetCelParam:
	# a0 - Shading texture U offset
	# a1 - Shading texture V offset
	# a2 - Shading primitive color
	andi	$a1, 0xff
	sll		$a1, 8
	andi	$a0, 0xff
	or		$a0, $a1
	la		$v0, _smd_cel_param
	sw		$a0, 0($v0)
	la		$v0, _smd_cel_col
	lui		$v1, 0x0200
	or		$a3, $v1
	jr		$ra
	sw		$a2, 0($v0)

.section .text.smdSortModelCel, "ax", @progbits
.global smdSortModelCel
.type smdSortModelCel, @function

smdSortModelCel:
	# a0 - Pointer SC_OT structure
	# a1 - Pointer to next primitive
	# a2 - Pointer to SMD data address
	# v0 - New pointer of primitive buffer (return)

	addiu	$sp, -16
	sw		$s0, 0($sp)
	sw		$s1, 4($sp)
	sw		$s2, 8($sp)
	sw		$s3, 12($sp)

	la		$v0, _sc_clip
	lw		$t8, 0($v0)
	lw		$t9, 4($v0)

	lw		$t0, OT_LEN($a0)
	lw		$a0, OT_ADDR($a0)
	lw		$t1, SMD_HEAD_PVERTS($a2)
	lw		$t2, SMD_HEAD_PNORMS($a2)
	lw		$t3, SMD_HEAD_PPRIMS($a2)

.sort_loop:

	nop
	lw		$a3, 0($t3)				# Get primitive ID word
	move	$t4, $t3

	beqz	$a3, .exit				# Check if terminator (just zero)
	addiu	$t4, 4

	lhu		$t5, 0( $t4 )			# Load vertices
	lhu		$t6, 2( $t4 )
	lhu		$t7, 4( $t4 )
	sll		$t5, 3
	sll		$t6, 3
	sll		$t7, 3
	addu	$t5, $t1
	addu	$t6, $t1
	addu	$t7, $t1
	lwc2	C2_VXY0, 0( $t5 )
	lwc2	C2_VZ0 , 4( $t5 )
	lwc2	C2_VXY1, 0( $t6 )
	lwc2	C2_VZ1 , 4( $t6 )
	lwc2	C2_VXY2, 0( $t7 )
	lwc2	C2_VZ2 , 4( $t7 )

	srl		$v1, $a3, 24			# Get primitive size
	addu	$t3, $v1				# Step main pointer to next primitive

	RTPT

	cfc2	$v0, C2_FLAG			# Get GTE flag value
	nop

	bltz	$v0, .skip_prim			# Skip primitive if Z overflow
	andi	$v0, $a3, 0x3

	NCLIP							# Backface culling

	srl		$v1, $a3, 12
	andi	$v1, 1

	bnez	$v1, .no_culling
	nop

	mfc2	$v1, C2_MAC0
	nop
	bltz	$v1, .skip_prim
	nop

.no_culling:

	beq		$v0, 0x1, .prim_tri		# If primitive is a triangle
	nop
	beq		$v0, 0x2, .prim_quad	# If primitive is a quad
	nop

	b		.skip_prim
	nop

## Triangles

.prim_tri:							# Triangle processing

	addiu	$t4, 8					# Advance from indices

	AVSZ3							# Calculate average Z

	srl		$v0, $t0, 16			# Get Z divisor from OT_LEN value
	andi	$v0, 0xff

	mfc2	$t5, C2_OTZ				# Get AVSZ3 result

	sra		$v1, $t0, 24			# Get Z offset from OT_LEN value

	srl		$t5, $v0				# Apply divisor and offset
	sub		$t5, $v1

	blez	$t5, .skip_prim			# Skip primitive if less than zero
	andi	$v1, $t0, 0xffff
	bge		$t5, $v1, .skip_prim	# Skip primitive if greater than OT length
	sll		$t5, 2
	addu	$t5, $a0				# Append OTZ to OT address

	ClipTestTri

	and		$v0, $s0, $s1			# v0 & v1
	beqz	$v0, .do_draw
	and		$v0, $s1, $s2			# v1 & v2
	beqz	$v0, .do_draw
	and		$v0, $s2, $s0			# v2 & v0
	beqz	$v0, .do_draw
	nop
	b		.skip_prim
	nop

.do_draw:


	srl		$v0, $a3, 2					# Lighting enabled?
	andi	$v0, 0x3
	bnez	$v0, .F3_light
	nop

	andi	$v0, $a3, 0x10				# Gouraud shaded
	bnez	$v0, .F3_gouraud
	nop

	andi	$v0, $a3, 0x20				# Textured triangle
	bnez	$v0, .F3_textured
	nop

	lw		$v0, 0( $t4 )				# Flat color, no lighting
	lui		$v1, 0x2000
	or		$v0, $v1

	b		.sort_F3_pri
	sw		$v0, POLYF3_rgbc( $a1 )

.F3_gouraud:

	lw		$v0, 0($t4)
	lw		$v1, 4($t4)
	.set noat
	lui		$at, 0x3000
	or		$v0, $at
	.set at
	sw		$v0, POLYG3_rgbc0($a1)
	lw		$v0, 8($t4)
	sw		$v1, POLYG3_rgbc1($a1)
	b		.sort_G3_pri
	sw		$v0, POLYG3_rgbc2($a1)

.F3_textured:

	lw		$v0, 0( $t4 )				# Flat color, no lighting
	lui		$v1, 0x2400
	or		$v0, $v1
	sw		$v0, POLYFT3_rgbc( $a1 )
	addiu	$t4, 4

	lhu		$v0, 0( $t4 )				# Load texture coordinates
	lhu		$v1, 2( $t4 )
	sh		$v0, POLYFT3_uv0( $a1 )
	lhu		$v0, 4( $t4 )
	sh		$v1, POLYFT3_uv1( $a1 )
	sh		$v0, POLYFT3_uv2( $a1 )

	lw		$v0, 8( $t4 )				# Tpage + CLUT
	nop
	andi	$v1, $v0, 0xffff
	sh		$v1, POLYFT3_tpage( $a1 )
	srl		$v0, 16

	b		.sort_FT3_pri
	sh		$v0, POLYFT3_clut( $a1 )

.F3_light:

	lhu		$v0, 0( $t4 )				# Load normal 0

	srl		$v1, $a3, 2
	andi	$v1, $v1, 0x3

	sll		$v0, 3
	addu	$v0, $t2
	lwc2	C2_VXY0, 0( $v0 )
	lwc2	C2_VZ0 , 4( $v0 )

	beq		$v1, 0x2, .F3_light_smt
	nop

	lw		$v0, 4( $t4 )
	lui		$v1, 0x2000
	or		$v0, $v1
	mtc2	$v0, C2_RGB

	addiu	$t4, 8
	nop

	NCCS

	andi	$v0, $a3, 0x20				# Textured triangle
	bnez	$v0, .F3_light_tex
	nop

	swc2	C2_RGB2, POLYF3_rgbc( $a1 )

	b		.sort_F3_pri
	nop

.F3_light_tex:

	lhu		$v0, 0( $t4 )				# Load texture coordinates
	lhu		$v1, 2( $t4 )
	sh		$v0, POLYFT3_uv0( $a1 )
	lhu		$v0, 4( $t4 )
	sh		$v1, POLYFT3_uv1( $a1 )
	sh		$v0, POLYFT3_uv2( $a1 )

	lw		$v1, 8( $t4 )
	nop
	andi	$v0, $v1, 0xffff
	sh		$v0, POLYFT3_tpage( $a1 )
	srl		$v0, $v1, 16
	sh		$v0, POLYFT3_clut( $a1 )

	mfc2	$v0, C2_RGB2
	lui		$v1, 0x2400
	or		$v0, $v1

	b		.sort_FT3_pri
	sw		$v0, POLYFT3_rgbc( $a1 )

.F3_light_smt:

	lhu		$v0, 2($t4)			# Load normals 1 and 2
	lhu		$v1, 4($t4)
	sll		$v0, 3
	sll		$v1, 3
	addu	$v0, $t2
	addu	$v1, $t2
	lwc2	C2_VXY1, 0($v0)
	lwc2	C2_VZ1 , 4($v0)
	#la		$v0, _smd_cel_col
	#lw		$v0, 0($v0)
	lwc2	C2_VXY2, 0($v1)
	lwc2	C2_VZ2 , 4($v1)
	mtc2	$v0, C2_RGB

	swc2	C2_SXY0, POLYFT3_xy0($a1)
	swc2	C2_SXY1, POLYFT3_xy1($a1)
	swc2	C2_SXY2, POLYFT3_xy2($a1)

	la		$v0, _smd_cel_tpage		# Load cel shader TPage and CLUT values
	lw		$v0, 0($v0)

	NCT

	andi	$v1, $v0, 0xffff
	sh		$v1, POLYFT3_tpage($a1)
	srl		$v1, $v0, 16
	sh		$v1, POLYFT3_clut($a1)

	# Usable regs: v0, v1, at, t6, t7

	.set noat

	la		$at, _smd_cel_param			# Load cel shader parameters
	lhu		$at, 0($at)

	mfc2	$t7, C2_RGB0				# Get first shaded color
	andi	$v1, $at, 0xff				# Get U divisor value
	andi	$t7, 0xffff					# Only keep R and G colors

	andi	$v0, $t7, 0xff				# U0
	srl		$v0, $v1
	sb		$v0, POLYFT3_uv0($a1)
	srl		$v0, $t7, 8					# V0
	srl		$v1, $at, 8
	srl		$v0, $v1
	sb		$v0, POLYFT3_uv0+1($a1)

	mfc2	$t7, C2_RGB1
	andi	$v1, $at, 0xff
	andi	$t7, 0xffff
	andi	$v0, $t7, 0xff				# U1
	srl		$v0, $v1
	sb		$v0, POLYFT3_uv1($a1)
	srl		$v0, $t7, 8					# V1
	srl		$v1, $at, 8
	srl		$v0, $v1
	sb		$v0, POLYFT3_uv1+1($a1)

	mfc2	$t7, C2_RGB2
	andi	$v1, $at, 0xff
	andi	$t7, 0xffff
	andi	$v0, $t7, 0xff				# U2
	srl		$v0, $v1
	sb		$v0, POLYFT3_uv2($a1)
	srl		$v0, $t7, 8					# V2
	srl		$v1, $at, 8
	srl		$v0, $v1
	sb		$v0, POLYFT3_uv2+1($a1)

	la		$v0, _smd_cel_col
	lw		$v0, 0($v0)
	lui		$v1, 0x2600
	or		$v0, $v1
	sw		$v0, POLYFT3_rgbc($a1)

	lw		$t7, 8($t4)
	addiu	$t4, 12

	lui		$v1, 0x0700
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$v1, $a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $v1
	sw		$v0, 0($t5)

	lui		$v0, 0x8000
	or		$a1, $v0
	addiu	$a1, POLYFT3_len

	.set at

	andi	$v0, $a3, 0x20				# Textured triangle
	bnez	$v0, .F3_light_tex_smt
	nop

	#swc2	C2_RGB0, POLYG3_rgbc0( $a1 )
	#swc2	C2_RGB1, POLYG3_rgbc1( $a1 )
	#swc2	C2_RGB2, POLYG3_rgbc2( $a1 )

	lui		$v0, 0x2000
	or		$t7, $v0
	b		.sort_F3_pri
	sw		$t7, POLYF3_rgbc($a1)

.F3_light_tex_smt:

	lhu		$v0, 0( $t4 )				# Load texture coordinates
	lhu		$v1, 2( $t4 )
	sh		$v0, POLYFT3_uv0( $a1 )
	lhu		$v0, 4( $t4 )
	sh		$v1, POLYFT3_uv1( $a1 )
	sh		$v0, POLYFT3_uv2( $a1 )

	lw		$v1, 8( $t4 )
	nop
	andi	$v0, $v1, 0xffff
	sh		$v0, POLYFT3_tpage( $a1 )
	srl		$v0, $v1, 16
	sh		$v0, POLYFT3_clut( $a1 )

	lui		$v1, 0x2400
	or		$t7, $v1

	b		.sort_FT3_pri
	sw		$t7, POLYFT3_rgbc( $a1 )

.sort_F3_pri:

	swc2	C2_SXY0, POLYF3_xy0($a1)
	swc2	C2_SXY1, POLYF3_xy1($a1)
	swc2	C2_SXY2, POLYF3_xy2($a1)

	la		$v0, _smd_tpage_base
	lhu		$v0, 0($v0)
	srl		$v1, $a3, 6				# Get blend mode
	andi	$v1, 0x3
	sll		$v1, 5
	or		$v0, $v1
	lui		$v1, 0xe100
	or		$v0, $v1
	sw		$v0, POLYF3_tpage($a1)	# Store TPage

	.set noat

	lui		$v1, 0x0500
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $a1
	sw		$v0, 0($t5)

	.set at

	lui		$v0, 0x8000
	or		$a1, $v0

	b		.sort_loop
	addiu	$a1, POLYF3_len

.sort_FT3_pri:

	swc2	C2_SXY0, POLYFT3_xy0( $a1 )
	swc2	C2_SXY1, POLYFT3_xy1( $a1 )
	swc2	C2_SXY2, POLYFT3_xy2( $a1 )

	.set noat

	lui		$v1, 0x0700
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $a1
	sw		$v0, 0($t5)

	.set at

	lui		$v0, 0x8000
	or		$a1, $v0

	b		.sort_loop
	addiu	$a1, POLYFT3_len

.sort_G3_pri:

	swc2	C2_SXY0, POLYG3_xy0( $a1 )
	swc2	C2_SXY1, POLYG3_xy1( $a1 )
	swc2	C2_SXY2, POLYG3_xy2( $a1 )

	la		$v0, _smd_tpage_base
	lhu		$v0, 0($v0)
	srl		$v1, $a3, 6				# Get blend mode
	andi	$v1, 0x3
	sll		$v1, 5
	or		$v0, $v1
	lui		$v1, 0xe100
	or		$v0, $v1
	sw		$v0, POLYG3_tpage($a1)	# Store TPage

	.set noat

	lui		$v1, 0x0700
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $a1
	sw		$v0, 0($t5)

	.set at

	lui		$v0, 0x8000
	or		$a1, $v0

	b		.sort_loop
	addiu	$a1, POLYG3_len

.sort_GT3_pri:

	swc2	C2_SXY0, POLYGT3_xy0( $a1 )
	swc2	C2_SXY1, POLYGT3_xy1( $a1 )
	swc2	C2_SXY2, POLYGT3_xy2( $a1 )

	.set noat

	lui		$v1, 0x0900
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $a1
	sw		$v0, 0($t5)

	.set at

	lui		$v0, 0x8000
	or		$a1, $v0

	b		.sort_loop
	addiu	$a1, POLYGT3_len

## Quads

.prim_quad:							# Quad processing

	mfc2	$t6, C2_SXY0			# Retrieve first projected vertex

	lhu		$t5, 6( $t4 )			# Project the last vertex
	addiu	$t4, 8
	sll		$t5, 3
	addu	$t5, $t1
	lwc2	C2_VXY0, 0( $t5 )
	lwc2	C2_VZ0 , 4( $t5 )

	nRTPS

	cfc2	$v1, C2_FLAG			# Get GTE flag value

	srl		$v0, $t0, 16			# Get Z divisor from OT_LEN value

	bltz	$v1, .skip_prim
	nop

	AVSZ4

	andi	$v0, 0xff

	mfc2	$t5, C2_OTZ

	sra		$v1, $t0, 24				# Get Z offset from OT_LEN value

	srl		$t5, $v0					# Apply divisor and offset
	sub		$t5, $v1

	blez	$t5, .skip_prim				# Skip primitive if less than zero
	andi	$v1, $t0, 0xffff
	bge		$t5, $v1, .skip_prim		# Skip primitive if greater than OT length
	sll		$t5, 2
	addu	$t5, $a0					# Append OTZ to OT address

	# no touch:
	# a0, a1, a2, a3, t0, t1, t2, t3, t4, t5(ot), t6(sxy0)

	ClipTestQuad

	and		$v0, $s0, $s1				# v0 & v1
	beqz	$v0, .do_draw_q
	and		$v0, $s1, $s2				# v1 & v2
	beqz	$v0, .do_draw_q
	and		$v0, $s2, $s3				# v2 & v3
	beqz	$v0, .do_draw_q
	and		$v0, $s3, $s0				# v3 & v0
	beqz	$v0, .do_draw_q
	and		$v0, $s0, $s2				# v0 & v2
	beqz	$v0, .do_draw_q
	and		$v0, $s1, $s3				# v1 & v3
	beqz	$v0, .do_draw_q
	nop
	b		.skip_prim
	nop

.do_draw_q:

	srl		$v0, $a3, 2					# Lighting enabled?
	andi	$v0, 0x3
	bnez	$v0, .F4_light
	nop

	andi	$v0, $a3, 0x10				# Gouraud quad
	bnez	$v0, .F4_gouraud
	nop

	andi	$v0, $a3, 0x20				# Textured quad
	bnez	$v0, .F4_textured
	nop

	lw		$v0, 0($t4)
	lui		$v1, 0x2800
	or		$v0, $v1

	b		.sort_F4_pri
	sw		$v0, POLYF4_rgbc($a1)

.F4_gouraud:

	lw		$v0, 0($t4)
	lw		$v1, 4($t4)
	.set noat
	lui		$at, 0x3800
	or		$v0, $at
	.set at
	sw		$v0, POLYG4_rgbc0($a1)
	lw		$v0, 8($t4)
	sw		$v1, POLYG4_rgbc1($a1)
	lw		$v1, 12($t4)
	sw		$v0, POLYG4_rgbc2($a1)
	b		.sort_G4_pri
	sw		$v1, POLYG4_rgbc3($a1)

.F4_textured:

	lw		$v0, 0($t4)
	lui		$v1, 0x2c00
	or		$v0, $v1
	sw		$v0, POLYFT4_rgbc( $a1 )
	addiu	$t4, 4

	lhu		$v0, 0($t4)					# Load texture coordinates
	lhu		$v1, 2($t4)
	sh		$v0, POLYFT4_uv0( $a1 )
	lhu		$v0, 4( $t4 )
	sh		$v1, POLYFT4_uv1( $a1 )
	lhu		$v1, 6( $t4 )
	sh		$v0, POLYFT4_uv2( $a1 )
	sh		$v1, POLYFT4_uv3( $a1 )

	lw		$v1, 8( $t4 )
	nop
	andi	$v0, $v1, 0xffff
	sh		$v0, POLYFT4_tpage( $a1 )
	srl		$v0, $v1, 16

	b		.sort_FT4_pri
	sh		$v0, POLYFT4_clut($a1)

.F4_light:

	lhu		$v0, 0( $t4 )				# Load normal 0

	srl		$v1, $a3, 2
	andi	$v1, $v1, 0x3

	sll		$v0, 3
	addu	$v0, $t2
	lwc2	C2_VXY0, 0( $v0 )
	lwc2	C2_VZ0 , 4( $v0 )

	beq		$v1, 0x2, .F4_light_smt
	nop

	lw		$v0, 4( $t4 )
	lui		$v1, 0x2800
	or		$v0, $v1
	mtc2	$v0, C2_RGB

	addiu	$t4, 8
	nop

	NCS

	andi	$v0, $a3, 0x20				# Textured triangle
	bnez	$v0, .F4_light_tex
	nop

	swc2	C2_RGB2, POLYF4_rgbc( $a1 )

	b		.sort_F4_pri
	nop

.F4_light_tex:

	lhu		$v0, 0( $t4 )				# Load texture coordinates
	lhu		$v1, 2( $t4 )
	sh		$v0, POLYFT4_uv0( $a1 )
	lhu		$v0, 4( $t4 )
	sh		$v1, POLYFT4_uv1( $a1 )
	lhu		$v1, 6( $t4 )
	sh		$v0, POLYFT4_uv2( $a1 )
	sh		$v1, POLYFT4_uv3( $a1 )

	lw		$v1, 8( $t4 )
	nop
	andi	$v0, $v1, 0xffff
	sh		$v0, POLYFT4_tpage( $a1 )
	srl		$v0, $v1, 16
	sh		$v0, POLYFT4_clut( $a1 )

	mfc2	$v0, C2_RGB2
	lui		$v1, 0x2c00
	or		$v0, $v1

	b		.sort_FT4_pri
	nop
	sw		$v0, POLYFT4_rgbc( $a1 )

.F4_light_smt:

	lhu		$v0, 2( $t4 )			# Load normals 1 and 2
	lhu		$v1, 4( $t4 )
	sll		$v0, 3
	sll		$v1, 3
	addu	$v0, $t2
	addu	$v1, $t2
	lwc2	C2_VXY1, 0( $v0 )
	lwc2	C2_VZ1 , 4( $v0 )
	lwc2	C2_VXY2, 0( $v1 )
	lwc2	C2_VZ2 , 4( $v1 )

	sw		$t6, POLYFT4_xy0($a1)
	swc2	C2_SXY0, POLYFT4_xy1($a1)
	swc2	C2_SXY1, POLYFT4_xy2($a1)
	swc2	C2_SXY2, POLYFT4_xy3($a1)

	la		$v0, _smd_cel_tpage		# Load cel shader TPage and CLUT values
	lw		$v0, 0($v0)

	NCT

	andi	$v1, $v0, 0xffff
	sh		$v1, POLYFT4_tpage($a1)
	srl		$v1, $v0, 16
	sh		$v1, POLYFT4_clut($a1)

	# Usable regs: v0, v1, at, t7

	.set noat

	la		$at, _smd_cel_param			# Load cel shader parameters
	lhu		$at, 0($at)

	mfc2	$t7, C2_RGB0
	andi	$v1, $at, 0xff				# Get U divisor value
	andi	$t7, 0xffff					# Only keep R and G colors

	andi	$v0, $t7, 0xff				# U0
	srl		$v0, $v1
	sb		$v0, POLYFT4_uv0($a1)
	srl		$v0, $t7, 8					# V0
	srl		$v1, $at, 8
	srl		$v0, $v1
	sb		$v0, POLYFT4_uv0+1($a1)

	mfc2	$t7, C2_RGB1
	andi	$v1, $at, 0xff
	andi	$t7, 0xffff
	andi	$v0, $t7, 0xff				# U1
	srl		$v0, $v1
	sb		$v0, POLYFT4_uv1($a1)
	srl		$v0, $t7, 8					# V1
	srl		$v1, $at, 8
	srl		$v0, $v1
	sb		$v0, POLYFT4_uv1+1($a1)

	mfc2	$t7, C2_RGB2
	andi	$v1, $at, 0xff
	andi	$t7, 0xffff
	andi	$v0, $t7, 0xff				# U2
	srl		$v0, $v1
	sb		$v0, POLYFT4_uv2($a1)
	srl		$v0, $t7, 8					# V2
	srl		$v1, $at, 8
	srl		$v0, $v1
	sb		$v0, POLYFT4_uv2+1($a1)

	la		$v0, _smd_cel_col
	lw		$v0, 0($v0)
	lui		$v1, 0x2E00
	or		$v0, $v1
	sw		$v0, POLYFT4_rgbc($a1)

	lw		$t7, 8($t4)

	lhu		$v0, 6($t4)			# Load normal 3
	addiu	$t4, 12
	sll		$v0, 3
	addu	$v0, $t2
	lwc2	C2_VXY0, 0( $v0 )
	lwc2	C2_VZ0 , 4( $v0 )

	nNCS

	mfc2	$s0, C2_RGB2
	andi	$v1, $at, 0xff
	andi	$s0, 0xffff
	andi	$v0, $s0, 0xff				# U3
	srl		$v0, $v1
	sb		$v0, POLYFT4_uv3($a1)
	srl		$v0, $s0, 8					# V3
	srl		$v1, $at, 8
	srl		$v0, $v1
	sb		$v0, POLYFT4_uv3+1($a1)

	lui		$v1, 0x0900
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$v1, $a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $v1
	sw		$v0, 0($t5)

	lui		$v0, 0x8000
	or		$a1, $v0
	addiu	$a1, POLYFT4_len

	.set at

	andi	$v0, $a3, 0x20				# Textured quad
	bnez	$v0, .F4_light_tex_smt
	nop

	lui		$v0, 0x2800
	or		$t7, $v0
	b		.sort_F4_pri
	sw		$t7, POLYF4_rgbc($a1)

.F4_light_tex_smt:

	lhu		$v0, 0($t4)				# Load texture coordinates
	lhu		$v1, 2($t4)
	sh		$v0, POLYFT4_uv0($a1)
	lhu		$v0, 4($t4)
	sh		$v1, POLYFT4_uv1($a1)
	lhu		$v1, 6($t4)
	sh		$v0, POLYFT4_uv2($a1)
	sh		$v1, POLYFT4_uv3($a1)

	lw		$v1, 8($t4)

	lui		$v0, 0x2E00
	or		$t7, $v0
	sw		$t7, POLYFT4_rgbc($a1)

	andi	$v0, $v1, 0xffff
	sh		$v0, POLYFT4_tpage($a1)
	srl		$v0, $v1, 16

	b		.sort_FT4_pri
	sh		$v0, POLYFT4_clut($a1)

.sort_F4_pri:

	sw		$t6, POLYF4_xy0($a1)
	swc2	C2_SXY0, POLYF4_xy1($a1)
	swc2	C2_SXY1, POLYF4_xy2($a1)
	swc2	C2_SXY2, POLYF4_xy3($a1)

	la		$v0, _smd_tpage_base
	lhu		$v0, 0($v0)
	srl		$v1, $a3, 6				# Get blend mode
	andi	$v1, 0x3
	sll		$v1, 5
	or		$v0, $v1
	lui		$v1, 0xe100
	or		$v0, $v1
	sw		$v0, POLYF4_tpage($a1)	# Store TPage

	.set noat

	lui		$v1, 0x0600
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $a1
	sw		$v0, 0($t5)

	.set at

	lui		$v0, 0x8000
	or		$a1, $v0

	b		.sort_loop
	addiu	$a1, POLYF4_len

.sort_FT4_pri:

	sw		$t6, POLYFT4_xy0($a1)
	swc2	C2_SXY0, POLYFT4_xy1($a1)
	swc2	C2_SXY1, POLYFT4_xy2($a1)
	swc2	C2_SXY2, POLYFT4_xy3($a1)

	.set noat

	lui		$v1, 0x0900
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $a1
	sw		$v0, 0($t5)

	.set at

	lui		$v0, 0x8000
	or		$a1, $v0

	b		.sort_loop
	addiu	$a1, POLYFT4_len

.sort_G4_pri:

	sw		$t6, POLYG4_xy0($a1)
	swc2	C2_SXY0, POLYG4_xy1($a1)
	swc2	C2_SXY1, POLYG4_xy2($a1)
	swc2	C2_SXY2, POLYG4_xy3($a1)

	la		$v0, _smd_tpage_base
	lhu		$v0, 0($v0)
	srl		$v1, $a3, 6				# Get blend mode
	andi	$v1, 0x3
	sll		$v1, 5
	or		$v0, $v1
	lui		$v1, 0xe100
	or		$v0, $v1
	sw		$v0, POLYG4_tpage($a1)	# Store TPage

	.set noat

	lui		$v1, 0x0900
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $a1
	sw		$v0, 0($t5)

	.set at

	lui		$v0, 0x8000
	or		$a1, $v0

	b		.sort_loop
	addiu	$a1, POLYG4_len

.sort_GT4_pri:

	sw		$t6, POLYGT4_xy0($a1)
	swc2	C2_SXY0, POLYGT4_xy1($a1)
	swc2	C2_SXY1, POLYGT4_xy2($a1)
	swc2	C2_SXY2, POLYGT4_xy3($a1)

	.set noat

	lui		$v1, 0x0c00
	lw		$v0, 0($t5)
	lui		$at, 0xff00
	and		$v1, $at
	lui		$at, 0x00ff
	or		$at, 0xffff
	and		$v0, $at
	or		$v1, $v0
	sw		$v1, 0($a1)
	lw		$v0, 0($t5)
	and		$a1, $at
	lui		$at, 0xff00
	and		$v0, $at
	or		$v0, $a1
	sw		$v0, 0($t5)

	.set at

	lui		$v0, 0x8000
	or		$a1, $v0

	b		.sort_loop
	addiu	$a1, POLYGT4_len

.skip_prim:

	b		.sort_loop
	nop

.exit:

	lw		$s0, 0( $sp )
	lw		$s1, 4( $sp )
	lw		$s2, 8( $sp )
	lw		$s3, 12( $sp )
	addiu	$sp, 16
	jr		$ra
	move	$v0, $a1

.section .bss._smd_cel_col, "w"
.comm _smd_cel_col, 4, 4	# STP shading polygon color

.section .bss._smd_cel_param, "w"
.comm _smd_cel_param, 4, 4	# U divisor, V divisor, shading clip

.section .bss._smd_cel_tpage, "w"
.comm _smd_cel_tpage, 4, 4	# CEL shader texture page & CLUT