-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderSub.c
More file actions
2241 lines (1986 loc) · 87 KB
/
renderSub.c
File metadata and controls
2241 lines (1986 loc) · 87 KB
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
//subrender.c
//This file compiled separately
#include <SL_DEF.H>
#include "def.h"
#include "mymath.h"
#include "render.h"
#include "draw.h"
#include "dspm.h"
unsigned short texIDs_cut_from_texID[225][4] = {
{30, 31, 32, 33}, // +++
{30, 32, 0, 0}, // -++
{31, 33, 0, 0}, // -++
{30, 31, 0, 0}, // |++
{32, 33, 0, 0}, // |++
{34, 38, 0, 0}, // --+
{32, 39, 0, 0}, // --+
{31, 36, 0, 0}, // --+
{37, 41, 0, 0}, // --+
{42, 44, 0, 0}, // ||+
{43, 45, 0, 0}, // ||+
{46, 48, 0, 0}, // ||+
{47, 49, 0, 0}, // ||+
{50, 58, 0, 0}, // ---
{51, 59, 0, 0}, // ---
{52, 60, 0, 0}, // ---
{53, 61, 0, 0}, // ---
{54, 62, 0, 0}, // ---
{55, 63, 0, 0}, // ---
{56, 64, 0, 0}, // ---
{57, 65, 0, 0}, // ---
{66, 70, 0, 0}, // |||
{67, 71, 0, 0}, // |||
{68, 72, 0, 0}, // |||
{69, 73, 0, 0}, // |||
{74, 78, 0, 0}, // |||
{75, 79, 0, 0}, // |||
{76, 80, 0, 0}, // |||
{77, 81, 0, 0}, // |||
{82, 83, 86, 87}, // ++
{84, 85, 88, 89}, // ++
{90, 91, 94, 95}, // ++
{92, 93, 96, 97}, // ++
{82, 86, 0, 0}, // -+
{83, 87, 0, 0}, // -+
{84, 88, 0, 0}, // -+
{85, 89, 0, 0}, // -+
{90, 94, 0, 0}, // -+
{91, 95, 0, 0}, // -+
{92, 96, 0, 0}, // -+
{93, 97, 0, 0}, // -+
{82, 83, 0, 0}, // |+
{86, 87, 0, 0}, // |+
{84, 85, 0, 0}, // |+
{88, 89, 0, 0}, // |+
{90, 91, 0, 0}, // |+
{94, 95, 0, 0}, // |+
{92, 93, 0, 0}, // |+
{96, 97, 0, 0}, // |+
{98, 99, 0, 0}, // --
{100, 101, 0, 0}, // --
{102, 103, 0, 0}, // --
{104, 105, 0, 0}, // --
{106, 107, 0, 0}, // --
{108, 109, 0, 0}, // --
{110, 111, 0, 0}, // --
{112, 113, 0, 0}, // --
{114, 115, 0, 0}, // --
{116, 117, 0, 0}, // --
{118, 119, 0, 0}, // --
{120, 121, 0, 0}, // --
{122, 123, 0, 0}, // --
{124, 125, 0, 0}, // --
{126, 127, 0, 0}, // --
{128, 129, 0, 0}, // --
{130, 132, 0, 0}, // ||
{131, 133, 0, 0}, // ||
{138, 140, 0, 0}, // ||
{139, 141, 0, 0}, // ||
{134, 136, 0, 0}, // ||
{135, 137, 0, 0}, // ||
{142, 144, 0, 0}, // ||
{143, 145, 0, 0}, // ||
{146, 148, 0, 0}, // ||
{147, 149, 0, 0}, // ||
{154, 156, 0, 0}, // ||
{155, 157, 0, 0}, // ||
{150, 152, 0, 0}, // ||
{151, 153, 0, 0}, // ||
{158, 160, 0, 0}, // ||
{159, 161, 0, 0}, // ||
{162, 166, 163, 167}, // +
{170, 174, 171, 175}, // +
{178, 182, 179, 183}, // +
{186, 190, 187, 191}, // +
{164, 168, 165, 169}, // +
{172, 176, 173, 177}, // +
{180, 184, 181, 185}, // +
{188, 192, 189, 193}, // +
{194, 198, 195, 199}, // +
{202, 206, 203, 207}, // +
{210, 214, 211, 215}, // +
{218, 222, 219, 223}, // +
{196, 200, 197, 201}, // +
{204, 208, 205, 209}, // +
{212, 216, 213, 217}, // +
{220, 224, 221, 225}, // +
{162, 163, 0, 0}, // -
{164, 165, 0, 0}, // -
{166, 167, 0, 0}, // -
{168, 169, 0, 0}, // -
{170, 171, 0, 0}, // -
{172, 173, 0, 0}, // -
{174, 175, 0, 0}, // -
{176, 177, 0, 0}, // -
{178, 179, 0, 0}, // -
{180, 181, 0, 0}, // -
{182, 183, 0, 0}, // -
{184, 185, 0, 0}, // -
{186, 187, 0, 0}, // -
{188, 189, 0, 0}, // -
{190, 191, 0, 0}, // -
{192, 193, 0, 0}, // -
{194, 195, 0, 0}, // -
{196, 197, 0, 0}, // -
{198, 199, 0, 0}, // -
{200, 201, 0, 0}, // -
{202, 203, 0, 0}, // -
{204, 205, 0, 0}, // -
{206, 207, 0, 0}, // -
{208, 209, 0, 0}, // -
{210, 211, 0, 0}, // -
{212, 213, 0, 0}, // -
{214, 215, 0, 0}, // -
{216, 217, 0, 0}, // -
{218, 219, 0, 0}, // -
{220, 221, 0, 0}, // -
{222, 223, 0, 0}, // -
{224, 225, 0, 0}, // -
{162, 166, 0, 0}, // |
{163, 167, 0, 0}, // |
{170, 174, 0, 0}, // |
{171, 175, 0, 0}, // |
{178, 182, 0, 0}, // |
{179, 183, 0, 0}, // |
{186, 190, 0, 0}, // |
{187, 191, 0, 0}, // |
{164, 168, 0, 0}, // |
{165, 169, 0, 0}, // |
{172, 176, 0, 0}, // |
{173, 177, 0, 0}, // |
{180, 184, 0, 0}, // |
{181, 185, 0, 0}, // |
{188, 192, 0, 0}, // |
{189, 193, 0, 0}, // |
{194, 198, 0, 0}, // |
{195, 199, 0, 0}, // |
{202, 206, 0, 0}, // |
{203, 207, 0, 0}, // |
{210, 214, 0, 0}, // |
{211, 215, 0, 0}, // |
{218, 222, 0, 0}, // |
{219, 223, 0, 0}, // |
{196, 200, 0, 0}, // |
{197, 201, 0, 0}, // |
{204, 208, 0, 0}, // |
{205, 209, 0, 0}, // |
{212, 216, 0, 0}, // |
{213, 217, 0, 0}, // |
{220, 224, 0, 0}, // |
{221, 225, 0, 0} // |
//(remaining values do not subdivide)
};
/*
Portal information:
Polygon ID of the next portal.
If it is 255, this polygon is not a portal.
If it is 254, this is the last portal.
Plane information:
Information about the scale and subdivision rules of the plane.
0-1: First subdivision rule
2-3: Second subdivision rule
4-5: Third subdivision rule
6-7: ???
*/
#define SUBDIVIDE_X (1) // |
#define SUBDIVIDE_Y (2) // -
#define SUBDIVIDE_XY (3) // +
#define SUBDIVIDE_3XY (0x3F) // +++
#define SUBDIVIDE_1Y2XY (0x3E) // -++
#define SUBDIVIDE_1X2XY (0x3D) // |++
#define SUBDIVIDE_2Y1XY (0x3A) // --+
#define SUBDIVIDE_2X1XY (0x35) // ||+
#define SUBDIVIDE_3Y (0x2A) // ---
#define SUBDIVIDE_3X (0x15) // |||
#define SUBDIVIDE_2XY (0xF) // ++
#define SUBDIVIDE_1Y1XY (0xE) // -+
#define SUBDIVIDE_1X1XY (0xD) // |+
#define SUBDIVIDE_2Y (0xA) // --
#define SUBDIVIDE_2X (0x5) // ||
#define UV_CUT_COUNT (224)
#define MAX_IN_TILE (128)
int sub_transform_buffer[MAX_SSH2_ENTITY_VERTICES][4];
vertex_t screen_transform_buffer[MAX_SSH2_ENTITY_VERTICES];
//Realistically, this could go up to 4^6. That's a lot of RAM.
//Even this being set at 1024 is quite a lot of RAM used.
// 12kb pt buffer + 8kb poly buffer + 2kb tex buffer // 22kb
int subdivided_points[MAX_IN_TILE][4];
short subdivided_polygons[MAX_IN_TILE][4]; //4 Vertex IDs of the subdivided_points
short used_textures[MAX_IN_TILE];
short sub_poly_cnt = 0;
short sub_vert_cnt = 0;
short tile_rules[4] = {0, 0, 0, SUBDIVIDE_XY};
short plane_rules[4] = {0, 0, 0, SUBDIVIDE_XY};
short texture_rules[4] = {16, 16, 16, 0};
// big performance nob.
int z_rules[4] = {512<<16, 256<<16, 128<<16, 0};
int clip_settings[7] = {TV_HALF_WIDTH, -TV_HALF_WIDTH, TV_HALF_HEIGHT, -TV_HALF_WIDTH, SUBDIVISION_NEAR_PLANE, 0, 0};
// 0 4 8 12 16 20,24
typedef struct {
FIXED * ptv[4]; //ptv[0] is byte 0, ptv[1] is byte 4, ptv[2] is byte 8, ptv[3] is byte 12
short * poly_a; // byte 16
short * poly_b; // byte 20
short * poly_c; // byte 24
short * poly_d; // byte 28
} _subdivision_settings;
void subdivide_xy(_subdivision_settings * set)
{
//////////////////////////////////////////////////////////////////
// Subdivide by all rules / Subdivide polygon into four new quads
//Turn 4 points into 9 points
//Make the 4 new polygons
//////////////////////////////////////////////////////////////////
/*
0A 1A | 0B 1B
A B
3A 2A | 3B 2B
0D 1D | 0C 1C
C D
3D 2D | 3C 2C
*/
// Initial Conditions
set->poly_a[0] = set->poly_a[0];
set->poly_b[1] = set->poly_a[1];
set->poly_c[3] = set->poly_a[3];
set->poly_d[2] = set->poly_a[2];
// Center
//
int * live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[0][X] + set->ptv[1][X] +
set->ptv[2][X] + set->ptv[3][X])>>2;
live_vt[Y] = (set->ptv[0][Y] + set->ptv[1][Y] +
set->ptv[2][Y] + set->ptv[3][Y])>>2;
live_vt[Z] = (set->ptv[0][Z] + set->ptv[1][Z] +
set->ptv[2][Z] + set->ptv[3][Z])>>2;
set->poly_a[2] = sub_vert_cnt;
set->poly_b[3] = sub_vert_cnt;
set->poly_c[1] = sub_vert_cnt;
set->poly_d[0] = sub_vert_cnt;
sub_vert_cnt++;
// 0 -> 1
//Hm, in the process of making center, we do add 0+1. That could be an optimization -- if it were not for the shifts.
live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[0][X] + set->ptv[1][X])>>1;
live_vt[Y] = (set->ptv[0][Y] + set->ptv[1][Y])>>1;
live_vt[Z] = (set->ptv[0][Z] + set->ptv[1][Z])>>1;
set->poly_a[1] = sub_vert_cnt;
set->poly_b[0] = sub_vert_cnt;
sub_vert_cnt++;
// 1 -> 2
live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[2][X] + set->ptv[1][X])>>1;
live_vt[Y] = (set->ptv[2][Y] + set->ptv[1][Y])>>1;
live_vt[Z] = (set->ptv[2][Z] + set->ptv[1][Z])>>1;
set->poly_b[2] = sub_vert_cnt;
set->poly_d[1] = sub_vert_cnt;
sub_vert_cnt++;
// 3 -> 2
live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[2][X] + set->ptv[3][X])>>1;
live_vt[Y] = (set->ptv[2][Y] + set->ptv[3][Y])>>1;
live_vt[Z] = (set->ptv[2][Z] + set->ptv[3][Z])>>1;
set->poly_c[2] = sub_vert_cnt;
set->poly_d[3] = sub_vert_cnt;
sub_vert_cnt++;
// 3 -> 0
live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[0][X] + set->ptv[3][X])>>1;
live_vt[Y] = (set->ptv[0][Y] + set->ptv[3][Y])>>1;
live_vt[Z] = (set->ptv[0][Z] + set->ptv[3][Z])>>1;
set->poly_a[3] = sub_vert_cnt;
set->poly_c[0] = sub_vert_cnt;
sub_vert_cnt++;
sub_poly_cnt += 3; //Only add 3, as there was already 1 polygon. It was split into four.
}
void subdivide_y(_subdivision_settings * set)
{
//////////////////////////////////////////////////////////////////
// Subdivide between the edges 0->1 and 3->2 (""Vertically"")
// (Splits the polygon such that new vertices are created between 0->3 and 1->2)
//Turn 4 points into 6 points
//Make the 2 new polygons
//////////////////////////////////////////////////////////////////
/*
0A 1A
A
3A--------------------------2A
0B--------------------------1B
B
3B 2B
*/
//Initial Conditions
set->poly_a[0] = set->poly_a[0];
set->poly_a[1] = set->poly_a[1];
set->poly_b[2] = set->poly_a[2];
set->poly_b[3] = set->poly_a[3];
// 1 -> 2
int * live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[2][X] + set->ptv[1][X])>>1;
live_vt[Y] = (set->ptv[2][Y] + set->ptv[1][Y])>>1;
live_vt[Z] = (set->ptv[2][Z] + set->ptv[1][Z])>>1;
set->poly_a[2] = sub_vert_cnt;
set->poly_b[1] = sub_vert_cnt;
sub_vert_cnt++;
// 3 -> 0
live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[0][X] + set->ptv[3][X])>>1;
live_vt[Y] = (set->ptv[0][Y] + set->ptv[3][Y])>>1;
live_vt[Z] = (set->ptv[0][Z] + set->ptv[3][Z])>>1;
set->poly_a[3] = sub_vert_cnt;
set->poly_b[0] = sub_vert_cnt;
sub_vert_cnt++;
sub_poly_cnt += 1; //Only add 1, as there was already 1 polygon. It was split in two.
}
void subdivide_x(_subdivision_settings * set)
{
//////////////////////////////////////////////////////////////////
// Subdivide between the edges 0->3 and 1->2 (""Horizontally"")
// (Splits the polygon such that new vertices are created between 0->1 and 3->2)
//Turn 4 points into 6 points
//Make the 2 new polygons
//////////////////////////////////////////////////////////////////
/*
0A 1A | 0B 1B
|
A | B
|
3A 2A | 3B 2B
*/
// Initial Conditions
set->poly_a[0] = set->poly_a[0];
set->poly_a[3] = set->poly_a[3];
set->poly_b[1] = set->poly_a[1];
set->poly_b[2] = set->poly_a[2];
// 0 -> 1
int * live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[0][X] + set->ptv[1][X])>>1;
live_vt[Y] = (set->ptv[0][Y] + set->ptv[1][Y])>>1;
live_vt[Z] = (set->ptv[0][Z] + set->ptv[1][Z])>>1;
set->poly_a[1] = sub_vert_cnt;
set->poly_b[0] = sub_vert_cnt;
sub_vert_cnt++;
// 3 -> 2
live_vt = &subdivided_points[sub_vert_cnt][0];
live_vt[X] = (set->ptv[2][X] + set->ptv[3][X])>>1;
live_vt[Y] = (set->ptv[2][Y] + set->ptv[3][Y])>>1;
live_vt[Z] = (set->ptv[2][Z] + set->ptv[3][Z])>>1;
set->poly_a[2] = sub_vert_cnt;
set->poly_b[3] = sub_vert_cnt;
sub_vert_cnt++;
sub_poly_cnt += 1; //Only add 1, as there was already 1 polygon. It was split in two.
}
void subdivide_plane(short overwritten_polygon, short num_divisions, short total_divisions)
{
//"Load" the original points (code shortening operation)
FIXED * ptv[4];
static char new_rule;
static _subdivision_settings sub;
ptv[0] = &subdivided_points[subdivided_polygons[overwritten_polygon][0]][X];
ptv[1] = &subdivided_points[subdivided_polygons[overwritten_polygon][1]][X];
ptv[2] = &subdivided_points[subdivided_polygons[overwritten_polygon][2]][X];
ptv[3] = &subdivided_points[subdivided_polygons[overwritten_polygon][3]][X];
new_rule = plane_rules[total_divisions];
if((num_divisions <= 0 || plane_rules[total_divisions] == 0))
{
return;
}
//Because "sub" is a structure of localized memory common to all subdivided planes,
//we have to store this data separately from the subdivision parameters to prevent data corruption on recursive calls.
//This definitely blows some stack space.
short semaphore_poly_a = overwritten_polygon;
short semaphore_poly_b = sub_poly_cnt;
short semaphore_poly_c = sub_poly_cnt+1;
short semaphore_poly_d = sub_poly_cnt+2;
sub.ptv[0] = ptv[0];
sub.ptv[1] = ptv[1];
sub.ptv[2] = ptv[2];
sub.ptv[3] = ptv[3];
sub.poly_a = &subdivided_polygons[semaphore_poly_a][0];
sub.poly_b = &subdivided_polygons[semaphore_poly_b][0];
sub.poly_c = &subdivided_polygons[semaphore_poly_c][0];
sub.poly_d = &subdivided_polygons[semaphore_poly_d][0];
///////////////////////////////////////////
// Recursively subdivide the polygon.
///////////////////////////////////////////
switch(new_rule)
{
case(SUBDIVIDE_XY):
subdivide_xy(&sub);
subdivide_plane(semaphore_poly_a, num_divisions-1, total_divisions+1);
subdivide_plane(semaphore_poly_b, num_divisions-1, total_divisions+1);
subdivide_plane(semaphore_poly_c, num_divisions-1, total_divisions+1);
subdivide_plane(semaphore_poly_d, num_divisions-1, total_divisions+1);
break;
case(SUBDIVIDE_Y):
subdivide_y(&sub);
subdivide_plane(semaphore_poly_a, num_divisions-1, total_divisions+1);
subdivide_plane(semaphore_poly_b, num_divisions-1, total_divisions+1);
break;
case(SUBDIVIDE_X):
subdivide_x(&sub);
subdivide_plane(semaphore_poly_a, num_divisions-1, total_divisions+1);
subdivide_plane(semaphore_poly_b, num_divisions-1, total_divisions+1);
break;
default:
break;
}
}
void * preprocess_planes_to_tiles_for_sector(_sector * sct, void * workAddress)
{
//What we are going to do:
//Subdivide every plane in the original mesh, as stored in the mesh, to the subdivision buffers.
//When the plane is finished subdividing, we will dump the vertex and polygon buffers to
//sct->tltbl and sct->tvtbl with the vertex count for each plane being added to sct->nbTileVert and tile count to sct->nbTile
entity_t * ent = sct->ent;
GVPLY * mesh = ent->pol;
sct->nbTile = 0;
sct->nbTileVert = 0;
POINT * tile_vert_buf = (POINT *)dirty_buf;
_quad * tile_poly_buf = (_quad *)dirtier_buf;
static int tNew = 0;
static int tvNew = 0;
static int tPlane = 0;
tPlane = 0;
tNew = 0;
tvNew = 0;
workAddress = align_4(workAddress);
sct->altbl = (unsigned short *)workAddress;
//In this case, we are doing this in model-space.
for(unsigned int i = 0; i < sct->nbPolygon; i++)
{
sub_vert_cnt = 0;
sub_poly_cnt = 0;
int alias = sct->pltbl[i];
int base_tvNew = tvNew;
plane_rules[0] = mesh->attbl[alias].plane_information & 0x3;
plane_rules[1] = (mesh->attbl[alias].plane_information>>2) & 0x3;
plane_rules[2] = (mesh->attbl[alias].plane_information>>4) & 0x3;
plane_rules[3] = 0;
//We have some special things to do...
//We have to put the vertices of the polygon into:
//subdivided polygons and subdivided vertices buffers.
for(int k = 0; k < 4; k++)
{
subdivided_points[k][X] = mesh->pntbl[mesh->pltbl[alias].vertices[k]][X];
subdivided_points[k][Y] = mesh->pntbl[mesh->pltbl[alias].vertices[k]][Y];
subdivided_points[k][Z] = mesh->pntbl[mesh->pltbl[alias].vertices[k]][Z];
subdivided_polygons[0][k] = k;
}
sub_vert_cnt += 4;
sub_poly_cnt += 1;
subdivide_plane(0, 3, 0);
//After this, the subdivided plane/polygon buffers should be full of tile data for this plane.
//These points are in model-space / mesh-space.
//We first want to dump these to the dirty buf in LWRAM before checking them in to the sector's list in HWRAM.
for(int l = 0; l < sub_vert_cnt; l++)
{
tile_vert_buf[tvNew][X] = subdivided_points[l][X];
tile_vert_buf[tvNew][Y] = subdivided_points[l][Y];
tile_vert_buf[tvNew][Z] = subdivided_points[l][Z];
tvNew++;
}
for(int l = 0; l < sub_poly_cnt; l++)
{
tile_poly_buf[tNew].vertices[0] = subdivided_polygons[l][0] + base_tvNew;
tile_poly_buf[tNew].vertices[1] = subdivided_polygons[l][1] + base_tvNew;
tile_poly_buf[tNew].vertices[2] = subdivided_polygons[l][2] + base_tvNew;
tile_poly_buf[tNew].vertices[3] = subdivided_polygons[l][3] + base_tvNew;
sct->altbl[tNew] = alias;
tNew++;
}
//With tvNew and tNew, we now have a count of the number of polygons and planes made so far IN TOTAL.
}
sct->nbTileVert = tvNew;
//To account for the size of the alias table, we have to push workAddress forward.
workAddress += tNew * sizeof(short);
//nbTile table : stores the number of tiles per plane
sct->nbTile = (unsigned short *)workAddress;
//Set forward by short * number of planes
workAddress += sct->nbPolygon * sizeof(unsigned short);
//plStart table : stores first tile ID # in each plane
sct->plStart = (unsigned short *)workAddress;
//Set forward by short * number of planes
workAddress += sct->nbPolygon * sizeof(unsigned short);
//Align address
workAddress = align_4(workAddress);
//Set address of tile polygon table
sct->tltbl = (_quad *)workAddress;
//We know its size already; there are no duplicate polygons.
workAddress += tNew * sizeof(_quad);
//Find & write the number of tiles per polygon
for(unsigned int i = 0; i < sct->nbPolygon; i++)
{
tPlane = 0;
int first_tile = -1;
for(int t = 0; t < tNew; t++)
{
if(sct->altbl[t] == sct->pltbl[i])
{
first_tile = (first_tile == -1) ? t : first_tile;
tPlane++;
}
}
sct->plStart[i] = first_tile;
sct->nbTile[i] = tPlane;
}
//Write a copy of an unmodified (no duplicates removed) tile table
for(int i = 0; i < tNew; i++)
{
for(int k = 0; k < 4; k++)
{
sct->tltbl[i].vertices[k] = tile_poly_buf[i].vertices[k];
}
}
//Align address for tile vertex table
workAddress = align_4(workAddress);
//Set address of tile vertex table
sct->tvtbl = (POINT *)workAddress;
tvNew = 0;
for(unsigned int i = 0; i < sct->nbTileVert; i++)
{
//First, check all vertices to see if this is a duplicate or not.
//Throw out the low order bits when doing this. Just cuz.
//In case of something found as a duplicate, it is marked with zero. This indicates a duplicate.
//We won't be adding this.
if(tile_vert_buf[i][X] == 0 && tile_vert_buf[i][Y] == 0 && tile_vert_buf[i][Z] == 0) continue;
for(unsigned int d = 0; d < sct->nbTileVert; d++)
{
//Don't mark the same vertex as a duplicate.
if(d == i) continue;
//In case of being marked with zero, it would have been a duplicate. Do not check it.
if(tile_vert_buf[d][X] == 0 && tile_vert_buf[d][Y] == 0 && tile_vert_buf[d][Z] == 0) continue;
if((tile_vert_buf[i][X]>>16) == (tile_vert_buf[d][X]>>16) &&
(tile_vert_buf[i][Y]>>16) == (tile_vert_buf[d][Y]>>16) &&
(tile_vert_buf[i][Z]>>16) == (tile_vert_buf[d][Z]>>16))
{
//Okay, we have a duplicate. i is the same vertex as d.
//First thing we have to do is go through every polygon and find ones which use the index d.
//Replace that index with i.
for(int p = 0; p < tNew; p++)
{
for(int k = 0; k < 4; k++)
{
if(tile_poly_buf[p].vertices[k] == d) tile_poly_buf[p].vertices[k] = i;
}
}
//Now we must denote the duplicate vertex so it is not added again.
//We will do this by zeroing it out.
tile_vert_buf[d][X] = 0;
tile_vert_buf[d][Y] = 0;
tile_vert_buf[d][Z] = 0;
}
}
for(int k = 0; k < 3; k++)
{
sct->tvtbl[tvNew][k] = tile_vert_buf[i][k];
}
//Add to the new duplicate-removed vertex count.
tvNew++;
}
sct->nbTileVert = tvNew;
workAddress += sct->nbTileVert * sizeof(POINT);
//At this point, we need to allocate memory for the sector's screen transform region and view transform region.
sct->viewspace_tvtbl = (void*)workAddress;
workAddress += sct->nbTileVert * sizeof(vertex_t);
sct->scrnspace_tvtbl = (void*)workAddress;
workAddress += sct->nbTileVert * sizeof(vertex_t);
//Now that we have removed duplicates from the vertex list, the vertex list size has changed.
//Doing so has unpredictably changed the index of each vertex in the list, meaning the polygon list is now invalid.
//We have allocated a copy of the original list to sct->tltbl and have the duplicates-removed list at tile_poly_buf.
//We also have the original in-order list in tile_vert_buf with duplicates marked zero.
//What we must do is check every polygon in tile_poly_buf to find the original values of its verices in tile_vert_buf.
//We must then find that vertex in sct->tvtbl, and change the index of the polygon in sct->tltbl to index vertex at sct->tvtbl.
POINT overt = {0,0,0};
for(int i = 0; i < tNew; i++)
{
for(int k = 0; k < 4; k++)
{
overt[X] = tile_vert_buf[tile_poly_buf[i].vertices[k]][X];
overt[Y] = tile_vert_buf[tile_poly_buf[i].vertices[k]][Y];
overt[Z] = tile_vert_buf[tile_poly_buf[i].vertices[k]][Z];
for(unsigned int v = 0; v < sct->nbTileVert; v++)
{
if(overt[X] == sct->tvtbl[v][X] && overt[Y] == sct->tvtbl[v][Y] && overt[Z] == sct->tvtbl[v][Z])
{
//We've found, through process of elimination, the matching vertex.
//We want the new table in sct->tltbl to index this vertex.
sct->tltbl[i].vertices[k] = v;
}
}
}
}
return align_4(workAddress);
}
void subdivide_tile(short overwritten_polygon, short num_divisions, short total_divisions, short rootTex)
{
//"Load" the original points (code shortening operation)
int new_rule;
static _subdivision_settings sub;
short * semaphore_poly_a = &subdivided_polygons[overwritten_polygon][0];
sub.ptv[0] = &subdivided_points[semaphore_poly_a[0]][X];
sub.ptv[1] = &subdivided_points[semaphore_poly_a[1]][X];
sub.ptv[2] = &subdivided_points[semaphore_poly_a[2]][X];
sub.ptv[3] = &subdivided_points[semaphore_poly_a[3]][X];
if(sub.ptv[0][Z] < 0 && sub.ptv[1][Z] < 0 && sub.ptv[2][Z] < 0 && sub.ptv[3][Z] < 0) return;
new_rule = tile_rules[total_divisions];
rootTex = rootTex-1;
used_textures[overwritten_polygon] = rootTex;
//////////////////////////////////////////////////////////////////
// Quick check: If we are subdividing a polygon above the z level, stop further subdivision.
// This is mostly useful in cases where a large polygon is being recursively subdivided and parts of it may be far away.
//////////////////////////////////////////////////////////////////
int polygon_minimum = JO_MIN(JO_MIN(sub.ptv[0][Z], sub.ptv[1][Z]), JO_MIN(sub.ptv[2][Z], sub.ptv[3][Z]));
///////////
// Don't try and add an exception to let things draw closer to the screen,
// even as untextured polygons.
// There's no solution that is compatible with the way the screen transform math works.
// You'd need to draw things completely differently.
if(num_divisions <= 0 || tile_rules[total_divisions] == 0 || polygon_minimum > z_rules[total_divisions])
{
return;
}
//Because "sub" is a structure of localized memory common to all subdivided planes,
//we have to store this data separately from the subdivision parameters to prevent data corruption on recursive calls.
//This definitely blows some stack space.
short semaphore_poly_b = sub_poly_cnt;
short semaphore_poly_c = sub_poly_cnt+1;
short semaphore_poly_d = sub_poly_cnt+2;
sub.poly_a = semaphore_poly_a;
sub.poly_b = &subdivided_polygons[semaphore_poly_b][0];
sub.poly_c = &subdivided_polygons[semaphore_poly_c][0];
sub.poly_d = &subdivided_polygons[semaphore_poly_d][0];
// mov @(R0, set);
// R0 being a byte offset, and "set" being an absolute address
// subdivided_polygons is 8 bytes per entry, so <<3
// subdivided_points is 16 bytes per entry
// BENCHMARKED: The assembly IS faster, but it is micro-faster; like 1 or 2% faster; maybe not faster (or slower).
// (to be clear I seriously don't expect to do better than the compiler at such simple tasks)
// Some more potential optimizations is to get rid of the middleman struct "sub"
switch(new_rule)
{
case(SUBDIVIDE_XY):
// subdivide_xy(&sub);
asm(
"mov.l @(16,%[set]),r2;" //Move &poly_a to r2
"mov.l @(20,%[set]),r3;" //Move &poly_b to r3
"mov.l @(24,%[set]),r4;" //Move &poly_c to r4
"mov.l @(28,%[set]),r5;" //Move &poly_d to r5
"mov.w @(2,r2),r0;"
"mov.w r0,@(2,r3);" //Copy poly_a[1] to poly_b[1]
"mov.w @(4,r2),r0;"
"mov.w r0,@(4,r5);" //Copy poly_a[2] to poly_c[2]
"mov.w @(6,r2),r0;"
"mov.w r0,@(6,r4);" //Copy poly_a[3] to poly_d[3]
"mov.w @%[pnt_cnt],r0;" //Copy sub_vert_cnt to r0
"mov r0,r1;"
"shll2 r1;"
"shll2 r1;" //Perform sub_vert_cnt<<2 in r1
"add %[sub_pnt],r1;" //Add subdivided_points to r1 to get address of: subdivided_points[sub_vert_cnt] in r1
"mov.l @%[set],r6;" //Move ptv[0] to r6
"mov.l @r6,r7;"
"mov.l @(4,r6),r8;"
"mov.l @(8,r6),r9;" //Move ptv[0][xyz] to r7,r8,r9
"mov.l @(4,%[set]),r6;" //Move ptv[1] to r6
"mov.l @r6,r10;"
"add r10,r7;" //ptv[0][X] + ptv[1][X]
"mov.l @(4,r6),r10;"
"add r10,r8;" //ptv[0][Y] + ptv[1][Y]
"mov.l @(8,r6),r10;"
"add r10,r9;" //ptv[0][Z] + ptv[1][Z] -- this code will repeat for ptv[2] and ptv[3] to be added.
"mov.l @(8,%[set]),r6;" //Move ptv[2] to r6
"mov.l @r6,r10;"
"add r10,r7;"
"mov.l @(4,r6),r10;"
"add r10,r8;"
"mov.l @(8,r6),r10;"
"add r10,r9;"
"mov.l @(12,%[set]),r6;" //Move ptv[3] to r6
"mov.l @r6,r10;"
"add r10,r7;"
"mov.l @(4,r6),r10;"
"add r10,r8;"
"mov.l @(8,r6),r10;"
"add r10,r9;" //ptv[0,1,2,3] have been added together
"shar r7;"
"shar r7;"
"shar r8;"
"shar r8;"
"shar r9;"
"shar r9;" //Shifts each of xyz right by two (arithmetic shift)
"mov.l r7,@r1;"
"mov.l r8,@(4,r1);"
"mov.l r9,@(8,r1);" //Set this point to subdivided_points[sub_vert_cnt]
"add #16,r1;" //Increment the pointer in subdivided_points
"mov.w r0,@(4,r2);" //Set poly_a[2] = sub_vert_cnt (in r0)
"mov.w r0,@(6,r3);" //Set poly_b[3] = sub_vert_cnt
"mov.w r0,@(2,r4);" //Set poly_c[1] = sub_vert_cnt
"mov.w r0,@r5;" //Set poly_d[0] = sub_vert_cnt
"add #1,r0;" //End calculating new vertex, add 1 to vertex count
"mov.l @%[set],r6;" //Move ptv[0] to r6 (for (ptv[0]+ptv[1])>>1
"mov.l @r6,r7;"
"mov.l @(4,r6),r8;"
"mov.l @(8,r6),r9;" //Move ptv[0][xyz] to r7,r8,r9
"mov.l @(4,%[set]),r6;" //Move ptv[1] to r6
"mov.l @r6,r10;"
"add r10,r7;" //ptv[0][X] + ptv[1][X]
"mov.l @(4,r6),r10;"
"add r10,r8;" //ptv[0][Y] + ptv[1][Y]
"mov.l @(8,r6),r10;"
"add r10,r9;" //ptv[0][Z] + ptv[1][Z] -- this code segment is only ptv[0]+ptv[1].
"shar r7;"
"shar r8;"
"shar r9;"
"mov.l r7,@r1;"
"mov.l r8,@(4,r1);"
"mov.l r9,@(8,r1);" //Set this point to subdivided_points[sub_vert_cnt]
"add #16,r1;" //Increment the pointer in subdivided_points
"mov.w r0,@(2,r2);" //Set poly_a[1] = sub_vert_cnt (in r0)
"mov.w r0,@r3;" //Set poly_b[0] = sub_vert_cnt
"add #1,r0;" //End calculating new vertex, add 1 to vertex count
"mov.l @(4,%[set]),r6;" //Move ptv[1] to r6 (for (ptv[2]+ptv[1])>>1
"mov.l @r6,r7;"
"mov.l @(4,r6),r8;"
"mov.l @(8,r6),r9;" //Move ptv[1][xyz] to r7,r8,r9
"mov.l @(8,%[set]),r6;" //Move ptv[2] to r6
"mov.l @r6,r10;"
"add r10,r7;" //ptv[2][X] + ptv[1][X]
"mov.l @(4,r6),r10;"
"add r10,r8;" //ptv[2][Y] + ptv[1][Y]
"mov.l @(8,r6),r10;"
"add r10,r9;" //ptv[2][Z] + ptv[1][Z] -- this code segment is only ptv[0]+ptv[1].
"shar r7;"
"shar r8;"
"shar r9;"
"mov.l r7,@r1;"
"mov.l r8,@(4,r1);"
"mov.l r9,@(8,r1);" //Set this point to subdivided_points[sub_vert_cnt]
"add #16,r1;" //Increment the pointer in subdivided_points
"mov.w r0,@(4,r3);" //Set poly_b[2] = sub_vert_cnt (in r0)
"mov.w r0,@(2,r5);" //Set poly_d[1] = sub_vert_cnt
"add #1,r0;" //End calculating new vertex, add 1 to vertex count
"mov.l @(8,%[set]),r6;" //Move ptv[2] to r6 (for (ptv[2]+ptv[3])>>1
"mov.l @r6,r7;"
"mov.l @(4,r6),r8;"
"mov.l @(8,r6),r9;" //Move ptv[3][xyz] to r7,r8,r9
"mov.l @(12,%[set]),r6;" //Move ptv[3] to r6
"mov.l @r6,r10;"
"add r10,r7;" //ptv[2][X] + ptv[3][X]
"mov.l @(4,r6),r10;"
"add r10,r8;" //ptv[2][Y] + ptv[3][Y]
"mov.l @(8,r6),r10;"
"add r10,r9;" //ptv[2][Z] + ptv[3][Z] -- this code segment is only ptv[0]+ptv[1].
"shar r7;"
"shar r8;"
"shar r9;"
"mov.l r7,@r1;"
"mov.l r8,@(4,r1);"
"mov.l r9,@(8,r1);" //Set this point to subdivided_points[sub_vert_cnt]
"add #16,r1;" //Increment the pointer in subdivided_points
"mov.w r0,@(4,r4);" //Set poly_c[2] = sub_vert_cnt (in r0)
"mov.w r0,@(6,r5);" //Set poly_d[3] = sub_vert_cnt
"add #1,r0;" //End calculating new vertex, add 1 to vertex count
"mov.l @%[set],r6;" //Move ptv[0] to r6 (for (ptv[0]+ptv[3])>>1
"mov.l @r6,r7;"
"mov.l @(4,r6),r8;"
"mov.l @(8,r6),r9;" //Move ptv[3][xyz] to r7,r8,r9
"mov.l @(12,%[set]),r6;" //Move ptv[3] to r6
"mov.l @r6,r10;"
"add r10,r7;" //ptv[0][X] + ptv[3][X]
"mov.l @(4,r6),r10;"
"add r10,r8;" //ptv[0][Y] + ptv[3][Y]
"mov.l @(8,r6),r10;"
"add r10,r9;" //ptv[0][Z] + ptv[3][Z] -- this code segment is only ptv[0]+ptv[1].
"shar r7;"
"shar r8;"
"shar r9;"
"mov.l r7,@r1;"
"mov.l r8,@(4,r1);"
"mov.l r9,@(8,r1);" //Set this point to subdivided_points[sub_vert_cnt]
"add #16,r1;" //Increment the pointer in subdivided_points
"mov.w r0,@(6,r2);" //Set poly_a[3] = sub_vert_cnt (in r0)
"mov.w r0,@r4;" //Set poly_c[0] = sub_vert_cnt
"add #1,r0;" //End calculating new vertex, add 1 to vertex count
"mov.w r0,@%[pnt_cnt];" //Move the new sub_vert_cnt (in r0) to the address of sub_vert_cnt
"mov.w @%[ply_cnt],r0;" //Move ply_cnt to r0
"add #3,r0;" //We added three polygons in this process; increase the polygon count by 3.
"mov.w r0,@%[ply_cnt];"
: //OUT
: [set] "p" (&sub), [sub_pnt] "p" (subdivided_points), [pnt_cnt] "p" (&sub_vert_cnt), [ply_cnt] "p" (&sub_poly_cnt) //IN
: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10" //CLOBBERS
);
break;
case(SUBDIVIDE_Y):
// subdivide_y(&sub);
asm(
"mov.l @(16,%[set]),r3;" //poly_a's pointer to r3
"mov.l @(20,%[set]),r4;" //poly_b's pointer to r4
"mov.w @(4,r3),r0;" //Copy poly_a[2] to r0 (the syntax here doesn't line up with the manual)
"mov.w r0,@(4,r4);" //Copy r0 to poly_b[2] (GCC expects the literal byte offset, and converts it)
"mov.w @(6,r3),r0;" //Copy poly_a[3] to r0 (the manual states the offset is x1/x2/x4 for mov.b/w/l)
"mov.w r0,@(6,r4);" //Copy r0 to poly_b[3]
"mov.w @%[pnt_cnt],r0;" //Copy sub_vert_cnt to r0
"mov.w r0,@(4,r3);" //Copy sub_vert_cnt in r0 to poly_a[2]
"mov.w r0,@(2,r4);" //copy sub_vert_cnt in r0 to poly_b[1]
"mov r0, r1;" //copy sub_vert_cnt in r0 to r1
"shll2 r1;"
"shll2 r1;" // sub_vert_cnt<<2 in r1
"add %[sub_pnt],r1;" //makes pointer to sub_pnt[sub_vert_cnt] in r1
"mov.l @(4,%[set]),r2;" //copy set->ptv[1] to r2
"mov.l @r2,r5;"
"mov.l @(4,r2),r6;"
"mov.l @(8,r2),r7;" //copy set->ptv[1][xyz] to r5,r6,r7
"mov.l @(8,%[set]),r2;" //copy set->ptv[2] to r2
"mov.l @r2,r8;" //set->ptv[2][X] in r8
"add r8,r5;"
"shar r5;" //(set->ptv[1][X] + set->ptv[2][X]) >> 1
"mov.l @(4,r2),r8;"//set->ptv[2][Y] in r8
"add r8,r6;"
"shar r6;" //(set->ptv[1][Y] + set->ptv[2][Y]) >> 1
"mov.l @(8,r2),r8;"//set->ptv[2][Z] in r8
"add r8,r7;"
"shar r7;" //(set->ptv[1][Z] + set->ptv[2][Z]) >> 1
"mov.l r5,@r1;"
"mov.l r6,@(4,r1);"
"mov.l r7,@(8,r1);" //copies (ptv[1][xyz]+ptv[2][xyz])>>1 to sub_pnt[sub_vert_cnt][xyz]
"add #1,r0;" //add 1 to sub_vert_cnt in r0
"mov.w r0,@(6,r3);" //poly_a[3] = sub_vert_cnt+1
"mov.w r0,@r4;" //poly_b[0] = sub_vert_cnt+1
"add #16,r1;" //add 16 to r1 to reach the next array entry
"mov.l @(12,%[set]),r2;" //move set->ptv[3] to r2
"mov.l @r2,r5;"
"mov.l @(4,r2),r6;"
"mov.l @(8,r2),r7;" //move set->ptv[3][xyz] to r5,r7,r7
"mov.l @%[set],r2;" //move set->ptv[0] to r2
"mov.l @r2,r8;" //set->ptv[0][X] in r8
"add r8,r5;"
"shar r5;" //(set->ptv[0][X] + set->ptv[3][X]) >> 1
"mov.l @(4,r2),r8;"//set->ptv[0][Y] in r8
"add r8,r6;"
"shar r6;" //(set->ptv[0][Y] + set->ptv[3][Y]) >> 1
"mov.l @(8,r2),r8;"//set->ptv[0][Z] in r8
"add r8,r7;"
"shar r7;" //(set->ptv[0][Z] + set->ptv[3][Z]) >> 1
"mov.l r5,@r1;"
"mov.l r6,@(4,r1);"
"mov.l r7,@(8,r1);" //copies (ptv[0][xyz]+ptv[3][xyz])>>1 to sub_pnt[tgt_pnt+1][xyz]
"add #1,r0;" //add 1 to sub_vert_cnt in r0
"mov.w r0,@%[pnt_cnt];" //move sub_vert_cnt+2 to sub_vert_cnt
"mov.w @%[ply_cnt],r0;" //move sub_poly_cnt to r0
"add #1,r0;" //add 1 to sub_poly_cnt
"mov.w r0,@%[ply_cnt];" //move sub_poly_cnt+1 to sub_poly_cnt
: //OUT
: [set] "p" (&sub), [sub_pnt] "p" (subdivided_points), [pnt_cnt] "p" (&sub_vert_cnt), [ply_cnt] "p" (&sub_poly_cnt) //IN
: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8" //CLOBBERS
);
break;
case(SUBDIVIDE_X):
// subdivide_x(&sub);
asm(
"mov.l @(16,%[set]),r3;" //poly_a's pointer to r3
"mov.l @(20,%[set]),r4;" //poly_b's pointer to r4
"mov.w @(2,r3),r0;" //Copy poly_a[1] to r0 (the syntax here doesn't line up with the manual)
"mov.w r0,@(2,r4);" //Copy r0 to poly_b[1] (GCC expects the literal byte offset, and converts it)
"mov.w @(4,r3),r0;" //Copy poly_a[2] to r0 (the manual states the offset is x1/x2/x4 for mov.b/w/l)
"mov.w r0,@(4,r4);" //Copy r0 to poly_b[2]
"mov.w @%[pnt_cnt],r0;" //Copy sub_vert_cnt to r0
"mov.w r0,@(2,r3);" //Copy sub_vert_cnt in r0 to poly_a[1]
"mov.w r0,@r4;" //copy sub_vert_cnt in r0 to poly_b[0]
"mov r0, r1;" //copy sub_vert_cnt in r0 to r1
"shll2 r1;"
"shll2 r1;" // sub_vert_cnt<<2 in r1
"add %[sub_pnt],r1;" //makes pointer to sub_pnt[sub_vert_cnt] in r1
"mov.l @(4,%[set]),r2;" //copy set->ptv[1] to r2
"mov.l @r2,r5;"
"mov.l @(4,r2),r6;"
"mov.l @(8,r2),r7;" //copy set->ptv[1][xyz] to r5,r6,r7
"mov.l @%[set],r2;" //copy set->ptv[0] to r2
"mov.l @r2,r8;" //set->ptv[0][X] in r8
"add r8,r5;"
"shar r5;" //(set->ptv[1][X] + set->ptv[0][X]) >> 1
"mov.l @(4,r2),r8;"//set->ptv[0][Y] in r8
"add r8,r6;"
"shar r6;" //(set->ptv[1][Y] + set->ptv[0][Y]) >> 1
"mov.l @(8,r2),r8;"//set->ptv[0][Z] in r8
"add r8,r7;"
"shar r7;" //(set->ptv[1][Z] + set->ptv[0][Z]) >> 1
"mov.l r5,@r1;"
"mov.l r6,@(4,r1);"
"mov.l r7,@(8,r1);" //copies (ptv[0][xyz]+ptv[1][xyz])>>1 to sub_pnt[sub_vert_cnt][xyz]
"add #1,r0;" //add 1 to sub_vert_cnt in r0