forked from notaz/gpsp
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathvideo.cc
2339 lines (2032 loc) · 83.6 KB
/
video.cc
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
/* gameplaySP
*
* Copyright (C) 2006 Exophase <[email protected]>
* Copyright (C) 2023 David Guillen Fandos <[email protected]>
*
* This program 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 2 of
* the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
extern "C" {
#include "common.h"
}
u16* gba_screen_pixels = NULL;
#define get_screen_pixels() gba_screen_pixels
#define get_screen_pitch() GBA_SCREEN_PITCH
typedef struct {
u16 attr0, attr1, attr2, attr3;
} t_oam;
typedef struct {
u16 pad0[3];
u16 dx;
u16 pad1[3];
u16 dmx;
u16 pad2[3];
u16 dy;
u16 pad3[3];
u16 dmy;
} t_affp;
typedef void (* bitmap_render_function)(
u32 start, u32 end, void *dest_ptr, const u16 *pal);
typedef void (* tile_render_function)(
u32 layer, u32 start, u32 end, void *dest_ptr, const u16 *pal);
typedef void (*render_function_u16)(
u32 start, u32 end, u16 *scanline, u32 enable_flags);
typedef void (*render_function_u32)(
u32 start, u32 end, u32 *scanline, u32 enable_flags);
typedef void (*window_render_function)(u16 *scanline, u32 start, u32 end);
static void render_scanline_conditional(
u32 start, u32 end, u16 *scanline, u32 enable_flags = 0x3F);
typedef struct
{
bitmap_render_function blit_render;
bitmap_render_function scale_render;
bitmap_render_function affine_render;
} bitmap_layer_render_struct;
typedef struct
{
render_function_u16 fullcolor;
render_function_u16 indexed_u16;
render_function_u32 indexed_u32;
render_function_u32 stacked;
} layer_render_struct;
// Object blending modes
#define OBJ_MOD_NORMAL 0
#define OBJ_MOD_SEMITRAN 1
#define OBJ_MOD_WINDOW 2
#define OBJ_MOD_INVALID 3
// BLDCNT color effect modes
#define COL_EFFECT_NONE 0x0
#define COL_EFFECT_BLEND 0x1
#define COL_EFFECT_BRIGHT 0x2
#define COL_EFFECT_DARK 0x3
// Background render modes
#define RENDER_NORMAL 0
#define RENDER_COL16 1
#define RENDER_COL32 2
#define RENDER_ALPHA 3
// Byte lengths of complete tiles and tile rows in 4bpp and 8bpp.
#define tile_width_4bpp 4
#define tile_size_4bpp 32
#define tile_width_8bpp 8
#define tile_size_8bpp 64
// Sprite rendering cycles
#define REND_CYC_MAX 32768 /* Theoretical max is 17920 */
#define REND_CYC_SCANLINE 1210
#define REND_CYC_REDUCED 954
// Generate bit mask (bits 9th and 10th) with information about the pixel
// status (1st and/or 2nd target) for later blending.
static inline u16 color_flags(u32 layer) {
u32 bldcnt = read_ioreg(REG_BLDCNT);
return (
((bldcnt >> layer) & 0x01) | // 1st target
((bldcnt >> (layer + 7)) & 0x02) // 2nd target
) << 9;
}
static const u32 map_widths[] = { 256, 512, 256, 512 };
typedef enum
{
FULLCOLOR, // Regular rendering, output a 16 bit color
INDXCOLOR, // Rendering to indexed color, so we can later apply dark/bright
STCKCOLOR, // Stacks two indexed pixels (+flags) to apply blending
PIXCOPY // Special mode used for sprites, to allow for obj-window drawing
} rendtype;
s32 affine_reference_x[2];
s32 affine_reference_y[2];
static inline s32 signext28(u32 value)
{
s32 ret = (s32)(value << 4);
return ret >> 4;
}
void video_reload_counters()
{
/* This happens every Vblank */
affine_reference_x[0] = signext28(read_ioreg32(REG_BG2X_L));
affine_reference_y[0] = signext28(read_ioreg32(REG_BG2Y_L));
affine_reference_x[1] = signext28(read_ioreg32(REG_BG3X_L));
affine_reference_y[1] = signext28(read_ioreg32(REG_BG3Y_L));
}
// Renders non-affine tiled background layer.
// Will process a full or partial tile (start and end within 0..8) and draw
// it in either 8 or 4 bpp mode. Honors vertical and horizontal flip.
// tile contains the tile info (contains tile index, flip bits, pal info)
// hflip causes the tile pixels lookup to be reversed (from MSB to LSB
// If isbase is not set, color 0 is interpreted as transparent, otherwise
// we are drawing the base layer, so palette[0] is used (backdrop).
template<typename dtype, rendtype rdtype, bool is8bpp, bool isbase, bool hflip>
static inline void rend_part_tile_Nbpp(u32 bg_comb, u32 px_comb,
dtype *dest_ptr, u32 start, u32 end, u16 tile,
const u8 *tile_base, int vertical_pixel_flip, const u16 *paltbl
) {
// Seek to the specified tile, using the tile number and size.
// tile_base already points to the right tile-line vertical offset
const u8 *tile_ptr = &tile_base[(tile & 0x3FF) * (is8bpp ? 64 : 32)];
u16 bgcolor = paltbl[0];
// On vertical flip, apply the mirror offset
if (tile & 0x800)
tile_ptr += vertical_pixel_flip;
if (is8bpp) {
// Each byte is a color, mapped to a palete. 8 bytes can be read as 64bit
for (u32 i = start; i < end; i++, dest_ptr++) {
// Honor hflip by selecting bytes in the correct order
u32 sel = hflip ? (7-i) : i;
u8 pval = tile_ptr[sel];
// Alhpa mode stacks previous value (unless rendering the first layer)
if (pval) {
if (rdtype == FULLCOLOR)
*dest_ptr = paltbl[pval];
else if (rdtype == INDXCOLOR)
*dest_ptr = pval | px_comb; // Add combine flags
else if (rdtype == STCKCOLOR)
// Stack pixels on top of the pixel value and combine flags
*dest_ptr = pval | px_comb | ((isbase ? bg_comb : *dest_ptr) << 16);
}
else if (isbase) {
if (rdtype == FULLCOLOR)
*dest_ptr = bgcolor;
else
*dest_ptr = 0 | bg_comb; // Add combine flags
}
}
} else {
// In 4bpp mode, the tile[15..12] bits contain the sub-palette number.
u16 tilepal = (tile >> 12) << 4;
u16 pxflg = px_comb | tilepal;
const u16 *subpal = &paltbl[tilepal];
// Read packed pixel data, skip start pixels
u32 tilepix = eswap32(*(u32*)tile_ptr);
if (hflip) tilepix <<= (start * 4);
else tilepix >>= (start * 4);
// Only 32 bits (8 pixels * 4 bits)
for (u32 i = start; i < end; i++, dest_ptr++) {
u8 pval = hflip ? tilepix >> 28 : tilepix & 0xF;
if (pval) {
if (rdtype == FULLCOLOR)
*dest_ptr = subpal[pval];
else if (rdtype == INDXCOLOR)
*dest_ptr = pxflg | pval;
else if (rdtype == STCKCOLOR) // Stack pixels
*dest_ptr = pxflg | pval | ((isbase ? bg_comb : *dest_ptr) << 16);
}
else if (isbase) {
if (rdtype == FULLCOLOR)
*dest_ptr = bgcolor;
else
*dest_ptr = 0 | bg_comb;
}
// Advance to next packed data
if (hflip) tilepix <<= 4;
else tilepix >>= 4;
}
}
}
// Same as above, but optimized for full tiles. Skip comments here.
template<typename dtype, rendtype rdtype, bool is8bpp, bool isbase, bool hflip>
static inline void render_tile_Nbpp(
u32 bg_comb, u32 px_comb, dtype *dest_ptr, u16 tile,
const u8 *tile_base, int vertical_pixel_flip, const u16 *paltbl
) {
const u8 *tile_ptr = &tile_base[(tile & 0x3FF) * (is8bpp ? 64 : 32)];
u16 bgcolor = paltbl[0];
if (tile & 0x800)
tile_ptr += vertical_pixel_flip;
if (is8bpp) {
for (u32 j = 0; j < 2; j++) {
u32 tilepix = eswap32(((u32*)tile_ptr)[hflip ? 1-j : j]);
if (tilepix) {
for (u32 i = 0; i < 4; i++, dest_ptr++) {
u8 pval = hflip ? (tilepix >> (24 - i*8)) : (tilepix >> (i*8));
if (pval) {
if (rdtype == FULLCOLOR)
*dest_ptr = paltbl[pval];
else if (rdtype == INDXCOLOR)
*dest_ptr = pval | px_comb; // Add combine flags
else if (rdtype == STCKCOLOR)
*dest_ptr = pval | px_comb | ((isbase ? bg_comb : *dest_ptr) << 16);
}
else if (isbase) {
*dest_ptr = (rdtype == FULLCOLOR) ? bgcolor : 0 | bg_comb;
}
}
} else {
for (u32 i = 0; i < 4; i++, dest_ptr++)
if (isbase)
*dest_ptr = (rdtype == FULLCOLOR) ? bgcolor : 0 | bg_comb;
}
}
} else {
u32 tilepix = eswap32(*(u32*)tile_ptr);
if (tilepix) { // We can skip it all if the row is transparent
u16 tilepal = (tile >> 12) << 4;
u16 pxflg = px_comb | tilepal;
const u16 *subpal = &paltbl[tilepal];
for (u32 i = 0; i < 8; i++, dest_ptr++) {
u8 pval = (hflip ? (tilepix >> ((7-i)*4)) : (tilepix >> (i*4))) & 0xF;
if (pval) {
if (rdtype == FULLCOLOR)
*dest_ptr = subpal[pval];
else if (rdtype == INDXCOLOR)
*dest_ptr = pxflg | pval;
else if (rdtype == STCKCOLOR)
*dest_ptr = pxflg | pval | ((isbase ? bg_comb : *dest_ptr) << 16);
}
else if (isbase) {
*dest_ptr = (rdtype == FULLCOLOR) ? bgcolor : 0 | bg_comb;
}
}
} else if (isbase) {
// In this case we simply fill the pixels with background pixels
for (u32 i = 0; i < 8; i++, dest_ptr++)
*dest_ptr = (rdtype == FULLCOLOR) ? bgcolor : 0 | bg_comb;
}
}
}
template<typename stype, rendtype rdtype, bool isbase, bool is8bpp>
static void render_scanline_text_fast(u32 layer,
u32 start, u32 end, void *scanline, const u16 * paltbl)
{
u32 bg_control = read_ioreg(REG_BGxCNT(layer));
u16 vcount = read_ioreg(REG_VCOUNT);
u32 map_size = (bg_control >> 14) & 0x03;
u32 map_width = map_widths[map_size];
u32 hoffset = (start + read_ioreg(REG_BGxHOFS(layer))) % 512;
u32 voffset = (vcount + read_ioreg(REG_BGxVOFS(layer))) % 512;
stype *dest_ptr = ((stype*)scanline) + start;
// Calculate combine masks. These store 2 bits of info: 1st and 2nd target.
// If set, the current pixel belongs to a layer that is 1st or 2nd target.
u32 bg_comb = color_flags(5), px_comb = color_flags(layer);
// Background map data is in vram, at an offset specified in 2K blocks.
// (each map data block is 32x32 tiles, at 16bpp, so 2KB)
u32 base_block = (bg_control >> 8) & 0x1F;
u16 *map_base = (u16 *)&vram[base_block * 2048];
u16 *map_ptr, *second_ptr;
end -= start;
// Skip the top one/two block(s) if using the bottom half
if ((map_size & 0x02) && (voffset >= 256))
map_base += ((map_width / 8) * 32);
// Skip the top tiles within the block
map_base += (((voffset % 256) / 8) * 32);
// we might need to render from two charblocks, store a second pointer.
second_ptr = map_ptr = map_base;
if(map_size & 0x01) { // If background is 512 pixels wide
if(hoffset >= 256) {
// If we are rendering the right block, skip a whole charblock
hoffset -= 256;
map_ptr += (32 * 32);
} else {
// If we are rendering the left block, we might overrun into the right
second_ptr += (32 * 32);
}
} else {
hoffset %= 256; // Background is 256 pixels wide
}
// Skip the left blocks within the block
map_ptr += hoffset / 8;
// Render a single scanline of text tiles
u32 tilewidth = is8bpp ? tile_width_8bpp : tile_width_4bpp;
u32 vert_pix_offset = (voffset % 8) * tilewidth;
// Calculate the pixel offset between a line and its "flipped" mirror.
// The values can be {56, 40, 24, 8, -8, -24, -40, -56}
s32 vflip_off = is8bpp ?
tile_size_8bpp - 2*vert_pix_offset - tile_width_8bpp :
tile_size_4bpp - 2*vert_pix_offset - tile_width_4bpp;
// The tilemap base is selected via bgcnt (16KiB chunks)
u32 tilecntrl = (bg_control >> 2) & 0x03;
// Account for the base offset plus the tile vertical offset
u8 *tile_base = &vram[tilecntrl * 16*1024 + vert_pix_offset];
// Number of pixels available until the end of the tile block
u32 pixel_run = 256 - hoffset;
u32 tile_hoff = hoffset % 8;
u32 partial_hcnt = 8 - tile_hoff;
if (tile_hoff) {
// First partial tile, only right side is visible.
u32 todraw = MIN(end, partial_hcnt); // [1..7]
u32 stop = tile_hoff + todraw; // Usually 8, unless short run.
u16 tile = eswap16(*map_ptr++);
if (tile & 0x400) // Tile horizontal flip
rend_part_tile_Nbpp<stype, rdtype, is8bpp, isbase, true>(
bg_comb, px_comb, dest_ptr, tile_hoff, stop, tile, tile_base, vflip_off, paltbl);
else
rend_part_tile_Nbpp<stype, rdtype, is8bpp, isbase, false>(
bg_comb, px_comb, dest_ptr, tile_hoff, stop, tile, tile_base, vflip_off, paltbl);
dest_ptr += todraw;
end -= todraw;
pixel_run -= todraw;
}
if (!end)
return;
// Now render full tiles
u32 todraw = MIN(end, pixel_run) / 8;
for (u32 i = 0; i < todraw; i++, dest_ptr += 8) {
u16 tile = eswap16(*map_ptr++);
if (tile & 0x400) // Tile horizontal flip
render_tile_Nbpp<stype, rdtype, is8bpp, isbase, true>(
bg_comb, px_comb, dest_ptr, tile, tile_base, vflip_off, paltbl);
else
render_tile_Nbpp<stype, rdtype, is8bpp, isbase, false>(
bg_comb, px_comb, dest_ptr, tile, tile_base, vflip_off, paltbl);
}
end -= todraw * 8;
pixel_run -= todraw * 8;
if (!end)
return;
// Switch to the next char block if we ran out of tiles
if (!pixel_run)
map_ptr = second_ptr;
todraw = end / 8;
for (u32 i = 0; i < todraw; i++, dest_ptr += 8) {
u16 tile = eswap16(*map_ptr++);
if (tile & 0x400) // Tile horizontal flip
render_tile_Nbpp<stype, rdtype, is8bpp, isbase, true>(
bg_comb, px_comb, dest_ptr, tile, tile_base, vflip_off, paltbl);
else
render_tile_Nbpp<stype, rdtype, is8bpp, isbase, false>(
bg_comb, px_comb, dest_ptr, tile, tile_base, vflip_off, paltbl);
}
end -= todraw * 8;
// Finalize the tile rendering the left side of it (from 0 up to "end").
if (end) {
u16 tile = eswap16(*map_ptr++);
if (tile & 0x400) // Tile horizontal flip
rend_part_tile_Nbpp<stype, rdtype, is8bpp, isbase, true>(
bg_comb, px_comb, dest_ptr, 0, end, tile, tile_base, vflip_off, paltbl);
else
rend_part_tile_Nbpp<stype, rdtype, is8bpp, isbase, false>(
bg_comb, px_comb, dest_ptr, 0, end, tile, tile_base, vflip_off, paltbl);
}
}
// A slow version of the above function that allows for mosaic effects
template<typename stype, rendtype rdtype, bool isbase, bool is8bpp>
static void render_scanline_text_mosaic(u32 layer,
u32 start, u32 end, void *scanline, const u16 * paltbl)
{
u32 bg_control = read_ioreg(REG_BGxCNT(layer));
const u32 mosh = (read_ioreg(REG_MOSAIC) & 0xF) + 1;
const u32 mosv = ((read_ioreg(REG_MOSAIC) >> 4) & 0xF) + 1;
u16 vcount = read_ioreg(REG_VCOUNT);
u32 map_size = (bg_control >> 14) & 0x03;
u32 map_width = map_widths[map_size];
u32 hoffset = (start + read_ioreg(REG_BGxHOFS(layer))) % 512;
u16 vmosoff = vcount - vcount % mosv;
u32 voffset = (vmosoff + read_ioreg(REG_BGxVOFS(layer))) % 512;
stype *dest_ptr = ((stype*)scanline) + start;
u32 bg_comb = color_flags(5), px_comb = color_flags(layer);
u32 base_block = (bg_control >> 8) & 0x1F;
u16 *map_base = (u16 *)&vram[base_block * 2048];
u16 *map_ptr, *second_ptr;
if ((map_size & 0x02) && (voffset >= 256))
map_base += ((map_width / 8) * 32);
map_base += (((voffset % 256) / 8) * 32);
second_ptr = map_ptr = map_base;
if(map_size & 0x01) { // If background is 512 pixels wide
if(hoffset >= 256) {
// If we are rendering the right block, skip a whole charblock
hoffset -= 256;
map_ptr += (32 * 32);
} else {
// If we are rendering the left block, we might overrun into the right
second_ptr += (32 * 32);
}
} else {
hoffset %= 256; // Background is 256 pixels wide
}
// Skip the left blocks within the block
map_ptr += hoffset / 8;
// Render a single scanline of text tiles
u32 tilewidth = is8bpp ? tile_width_8bpp : tile_width_4bpp;
u32 vert_pix_offset = (voffset % 8) * tilewidth;
// Calculate the pixel offset between a line and its "flipped" mirror.
// The values can be {56, 40, 24, 8, -8, -24, -40, -56}
s32 vflip_off = is8bpp ?
tile_size_8bpp - 2*vert_pix_offset - tile_width_8bpp :
tile_size_4bpp - 2*vert_pix_offset - tile_width_4bpp;
// The tilemap base is selected via bgcnt (16KiB chunks)
u32 tilecntrl = (bg_control >> 2) & 0x03;
// Account for the base offset plus the tile vertical offset
u8 *tile_base = &vram[tilecntrl * 16*1024 + vert_pix_offset];
u16 bgcolor = paltbl[0];
// Iterate pixel by pixel, loading data every N pixels to honor mosaic effect
u8 pval = 0;
for (u32 i = 0; start < end; start++, i++, dest_ptr++) {
u16 tile = eswap16(*map_ptr);
if (!(i % mosh)) {
const u8 *tile_ptr = &tile_base[(tile & 0x3FF) * (is8bpp ? 64 : 32)];
bool hflip = (tile & 0x400);
if (tile & 0x800)
tile_ptr += vflip_off;
// Load byte or nibble with pixel data.
if (is8bpp) {
if (hflip)
pval = tile_ptr[7 - hoffset % 8];
else
pval = tile_ptr[hoffset % 8];
} else {
if (hflip)
pval = (tile_ptr[(7 - hoffset % 8) >> 1] >> (((hoffset & 1) ^ 1) * 4)) & 0xF;
else
pval = (tile_ptr[(hoffset % 8) >> 1] >> ((hoffset & 1) * 4)) & 0xF;
}
}
if (is8bpp) {
if (pval) {
if (rdtype == FULLCOLOR)
*dest_ptr = paltbl[pval];
else if (rdtype == INDXCOLOR)
*dest_ptr = pval | px_comb; // Add combine flags
else if (rdtype == STCKCOLOR)
*dest_ptr = pval | px_comb | ((isbase ? bg_comb : *dest_ptr) << 16);
}
else if (isbase) {
*dest_ptr = (rdtype == FULLCOLOR) ? bgcolor : 0 | bg_comb;
}
} else {
u16 tilepal = (tile >> 12) << 4;
u16 pxflg = px_comb | tilepal;
const u16 *subpal = &paltbl[tilepal];
if (pval) {
if (rdtype == FULLCOLOR)
*dest_ptr = subpal[pval];
else if (rdtype == INDXCOLOR)
*dest_ptr = pxflg | pval;
else if (rdtype == STCKCOLOR)
*dest_ptr = pxflg | pval | ((isbase ? bg_comb : *dest_ptr) << 16);
}
else if (isbase) {
*dest_ptr = (rdtype == FULLCOLOR) ? bgcolor : 0 | bg_comb;
}
}
// Need to continue from the next charblock
hoffset++;
if (hoffset % 8 == 0)
map_ptr++;
if (hoffset >= 256) {
hoffset = 0;
map_ptr = second_ptr;
}
}
}
template<typename stype, rendtype rdtype, bool isbase>
static void render_scanline_text(u32 layer,
u32 start, u32 end, void *scanline, const u16 * paltbl)
{
// Tile mode has 4 and 8 bpp modes.
u32 bg_control = read_ioreg(REG_BGxCNT(layer));
bool is8bpp = (read_ioreg(REG_BGxCNT(layer)) & 0x80);
const u32 mosamount = read_ioreg(REG_MOSAIC) & 0xFF;
bool has_mosaic = (bg_control & 0x40) && (mosamount != 0);
if (has_mosaic) {
if (is8bpp)
render_scanline_text_mosaic<stype, rdtype, isbase, true>(
layer, start, end, scanline, paltbl);
else
render_scanline_text_mosaic<stype, rdtype, isbase, false>(
layer, start, end, scanline, paltbl);
} else {
if (is8bpp)
render_scanline_text_fast<stype, rdtype, isbase, true>(
layer, start, end, scanline, paltbl);
else
render_scanline_text_fast<stype, rdtype, isbase, false>(
layer, start, end, scanline, paltbl);
}
}
static inline u8 lookup_pix_8bpp(
u32 px, u32 py, const u8 *tile_base, const u8 *map_base, u32 map_size
) {
// Pitch represents the log2(number of tiles per row) (from 16 to 128)
u32 map_pitch = map_size + 4;
// Given coords (px,py) in the background space, find the tile.
u32 mapoff = (px / 8) + ((py / 8) << map_pitch);
// Each tile is 8x8, so 64 bytes each.
const u8 *tile_ptr = &tile_base[map_base[mapoff] * tile_size_8bpp];
// Read the 8bit color within the tile.
return tile_ptr[(px % 8) + ((py % 8) * 8)];
}
template<typename dsttype, rendtype rdtype, bool isbase>
static inline void rend_pix_8bpp(
dsttype *dest_ptr, u8 pval, u32 bg_comb, u32 px_comb, const u16 *pal
) {
// Alhpa mode stacks previous value (unless rendering the first layer)
if (pval) {
if (rdtype == FULLCOLOR)
*dest_ptr = pal[pval];
else if (rdtype == INDXCOLOR)
*dest_ptr = pval | px_comb; // Add combine flags
else if (rdtype == STCKCOLOR)
// Stack pixels. If base, stack the base pixel.
*dest_ptr = pval | px_comb | ((isbase ? bg_comb : *dest_ptr) << 16);
}
else if (isbase) {
// Transparent pixel, but we are base layer, so render background.
if (rdtype == FULLCOLOR)
*dest_ptr = pal[0];
else
*dest_ptr = 0 | bg_comb; // Just backdrop color and combine flags
}
}
template<typename dsttype, rendtype rdtype>
static inline void render_bdrop_pixel_8bpp(
dsttype *dest_ptr, u32 bg_comb, u16 bgcol) {
// Alhpa mode stacks previous value (unless rendering the first layer)
if (rdtype == FULLCOLOR)
*dest_ptr = bgcol;
else
*dest_ptr = 0 | bg_comb;
}
typedef void (*affine_render_function) (
u32 layer, u32 start, u32 cnt, const u8 *map_base,
u32 map_size, const u8 *tile_base, void *dst_ptr,
const u16* pal);
// Affine background rendering logic.
// wrap extends the background infinitely, otherwise transparent/backdrop fill
// rotate indicates if there's any rotation (optimized version for no-rotation)
// mosaic applies to horizontal mosaic (vertical is adjusted via affine ref)
template <typename dtype, rendtype rdtype,
bool isbase, bool wrap, bool rotate, bool mosaic>
static inline void render_affine_background(
u32 layer, u32 start, u32 cnt, const u8 *map_base,
u32 map_size, const u8 *tile_base, void *dst_ptr_raw,
const u16* pal) {
dtype *dst_ptr = (dtype*)dst_ptr_raw;
// Backdrop and current layer combine bits.
u32 bg_comb = color_flags(5);
u32 px_comb = color_flags(layer);
s32 dx = (s16)read_ioreg(REG_BGxPA(layer));
s32 dy = (s16)read_ioreg(REG_BGxPC(layer));
s32 source_x = affine_reference_x[layer - 2] + (start * dx);
s32 source_y = affine_reference_y[layer - 2] + (start * dy);
// Maps are squared, four sizes available (128x128 to 1024x1024)
u32 width_height = 128 << map_size;
// Horizontal mosaic effect.
const u32 mosh = (mosaic ? (read_ioreg(REG_MOSAIC)) & 0xF : 0) + 1;
if (wrap) {
// In wrap mode the entire space is covered, since it "wraps" at the edges
u8 pval = 0;
if (rotate) {
for (u32 i = 0; cnt; i++, cnt--) {
u32 pix_x = (u32)(source_x >> 8) & (width_height-1);
u32 pix_y = (u32)(source_y >> 8) & (width_height-1);
// Lookup pixel and draw it (only every Nth if mosaic is on)
if (!mosaic || !(i % mosh))
pval = lookup_pix_8bpp(pix_x, pix_y, tile_base, map_base, map_size);
rend_pix_8bpp<dtype, rdtype, isbase>(dst_ptr++, pval, bg_comb, px_comb, pal);
source_x += dx; source_y += dy; // Move to the next pixel
}
} else {
// Y coordinate stays contant across the walk.
const u32 pix_y = (u32)(source_y >> 8) & (width_height-1);
for (u32 i = 0; cnt; i++, cnt--) {
u32 pix_x = (u32)(source_x >> 8) & (width_height-1);
if (!mosaic || !(i % mosh))
pval = lookup_pix_8bpp(pix_x, pix_y, tile_base, map_base, map_size);
rend_pix_8bpp<dtype, rdtype, isbase>(dst_ptr++, pval, bg_comb, px_comb, pal);
source_x += dx; // Only moving in the X direction.
}
}
} else {
u16 bgcol = pal[0];
if (rotate) {
// Draw backdrop pixels if necessary until we reach the background edge.
while (cnt) {
// Draw backdrop pixels if they lie outside of the background.
u32 pix_x = (u32)(source_x >> 8), pix_y = (u32)(source_y >> 8);
// Stop once we find a pixel that is actually *inside* the map.
if (pix_x < width_height && pix_y < width_height)
break;
// Draw a backdrop pixel if we are the base layer.
if (isbase)
render_bdrop_pixel_8bpp<dtype, rdtype>(dst_ptr, bg_comb, bgcol);
dst_ptr++;
source_x += dx; source_y += dy;
cnt--;
}
// Draw background pixels by looking them up in the map
u8 pval = 0;
for (u32 i = 0; cnt; i++, cnt--) {
u32 pix_x = (u32)(source_x >> 8), pix_y = (u32)(source_y >> 8);
// Check if we run out of background pixels, stop drawing.
if (pix_x >= width_height || pix_y >= width_height)
break;
// Lookup pixel and draw it.
if (!mosaic || !(i % mosh))
pval = lookup_pix_8bpp(pix_x, pix_y, tile_base, map_base, map_size);
rend_pix_8bpp<dtype, rdtype, isbase>(dst_ptr++, pval, bg_comb, px_comb, pal);
// Move to the next pixel, update coords accordingly
source_x += dx; source_y += dy;
}
} else {
// Specialized version for scaled-only backgrounds
u8 pval = 0;
const u32 pix_y = (u32)(source_y >> 8);
if (pix_y < width_height) { // Check if within Y-coord range
// Draw/find till left edge
while (cnt) {
u32 pix_x = (u32)(source_x >> 8);
if (pix_x < width_height)
break;
if (isbase)
render_bdrop_pixel_8bpp<dtype, rdtype>(dst_ptr, bg_comb, bgcol);
dst_ptr++;
source_x += dx;
cnt--;
}
// Draw actual background
for (u32 i = 0; cnt; i++, cnt--) {
u32 pix_x = (u32)(source_x >> 8);
if (pix_x >= width_height)
break;
if (!mosaic || !(i % mosh))
pval = lookup_pix_8bpp(pix_x, pix_y, tile_base, map_base, map_size);
rend_pix_8bpp<dtype, rdtype, isbase>(dst_ptr++, pval, bg_comb, px_comb, pal);
source_x += dx;
}
}
}
// Complete the line on the right, if we ran out over the bg edge.
// Only necessary for the base layer, otherwise we can safely finish.
if (isbase)
while (cnt--)
render_bdrop_pixel_8bpp<dtype, rdtype>(dst_ptr++, bg_comb, bgcol);
}
}
// Renders affine backgrounds. These differ substantially from non-affine
// ones. Tile maps are byte arrays (instead of 16 bit), limiting the map to
// 256 different tiles (with no flip bits and just one single 256 color pal).
// Optimize for common cases: wrap/non-wrap, scaling/rotation.
template<typename dsttype, rendtype rdtype, bool isbase>
static void render_scanline_affine(u32 layer,
u32 start, u32 end, void *scanline, const u16 *pal)
{
u32 bg_control = read_ioreg(REG_BGxCNT(layer));
u32 map_size = (bg_control >> 14) & 0x03;
// Char block base pointer
u32 base_block = (bg_control >> 8) & 0x1F;
u8 *map_base = &vram[base_block * 2048];
// The tilemap base is selected via bgcnt (16KiB chunks)
u32 tilecntrl = (bg_control >> 2) & 0x03;
u8 *tile_base = &vram[tilecntrl * 16*1024];
dsttype *dest_ptr = ((dsttype*)scanline) + start;
const u32 mosamount = read_ioreg(REG_MOSAIC) & 0xFF;
bool has_mosaic = (bg_control & 0x40) && (mosamount != 0);
bool has_rotation = read_ioreg(REG_BGxPC(layer)) != 0;
bool has_wrap = (bg_control >> 13) & 1;
// Number of pixels to render
u32 cnt = end - start;
// Four specialized versions for faster rendering on specific cases like
// scaling only or non-wrapped backgrounds.
u32 fidx = (has_wrap ? 0x4 : 0) |
(has_rotation ? 0x2 : 0) |
(has_mosaic ? 0x1 : 0);
static const affine_render_function rdfns[8] = {
render_affine_background<dsttype, rdtype, isbase, false, false, false>,
render_affine_background<dsttype, rdtype, isbase, false, false, true>,
render_affine_background<dsttype, rdtype, isbase, false, true, false>,
render_affine_background<dsttype, rdtype, isbase, false, true, true>,
render_affine_background<dsttype, rdtype, isbase, true, false, false>,
render_affine_background<dsttype, rdtype, isbase, true, false, true>,
render_affine_background<dsttype, rdtype, isbase, true, true, false>,
render_affine_background<dsttype, rdtype, isbase, true, true, true>,
};
rdfns[fidx](layer, start, cnt, map_base, map_size, tile_base, dest_ptr, pal);
}
template<rendtype rdmode, typename buftype, unsigned mode, typename pixfmt>
static inline void bitmap_pixel_write(
buftype *dst_ptr, pixfmt val, const u16 * palptr, u16 px_attr
) {
if (mode != 4)
*dst_ptr = convert_palette(val); // Direct color, u16 bitmap
else if (val) {
if (rdmode == FULLCOLOR)
*dst_ptr = palptr[val];
else if (rdmode == INDXCOLOR)
*dst_ptr = val | px_attr; // Add combine flags
else if (rdmode == STCKCOLOR)
*dst_ptr = val | px_attr | ((*dst_ptr) << 16); // Stack pixels
}
}
typedef enum
{
BLIT, // The bitmap has no scaling nor rotation on the X axis
SCALED, // The bitmap features some scaling (on the X axis) but no rotation
ROTATED // Bitmap has rotation (and perhaps scaling too)
} bm_rendmode;
// Renders a bitmap honoring the pixel mode and any affine transformations.
// There's optimized versions for bitmaps without scaling / rotation.
template<rendtype rdtype, typename dsttype, // Rendering target type and format
unsigned mode,
typename pixfmt, // Bitmap source pixel format (8/16)
unsigned width, unsigned height, // Bitmap size (not screen!)
bm_rendmode rdmode, // Rendering mode optimization.
bool mosaic> // Whether mosaic effect is used.
static inline void render_scanline_bitmap(
u32 start, u32 end, void *scanline, const u16 * palptr
) {
s32 dx = (s16)read_ioreg(REG_BG2PA);
s32 dy = (s16)read_ioreg(REG_BG2PC);
s32 source_x = affine_reference_x[0] + (start * dx); // Always BG2
s32 source_y = affine_reference_y[0] + (start * dy);
// Premature abort render optimization if bitmap out of Y coordinate.
if ((rdmode != ROTATED) && ((u32)(source_y >> 8)) >= height)
return;
// Modes 4 and 5 feature double buffering.
bool second_frame = (mode >= 4) && (read_ioreg(REG_DISPCNT) & 0x10);
pixfmt *src_ptr = (pixfmt*)&vram[second_frame ? 0xA000 : 0x0000];
dsttype *dst_ptr = ((dsttype*)scanline) + start;
u16 px_attr = color_flags(2); // Always BG2
const u32 mosh = (mosaic ? (read_ioreg(REG_MOSAIC)) & 0xF : 0) + 1;
if (rdmode == BLIT) {
// We just blit pixels (copy) from buffer to buffer.
const u32 pixel_y = (u32)(source_y >> 8);
if (source_x < 0) {
// The bitmap starts somewhere after "start", skip those pixels.
u32 delta = (-source_x + 255) >> 8;
dst_ptr += delta;
start += delta;
source_x = 0;
}
u32 pixel_x = (u32)(source_x >> 8);
u32 pixcnt = MIN(end - start, width - pixel_x);
pixfmt *valptr = &src_ptr[pixel_x + (pixel_y * width)];
pixfmt val = 0;
for (u32 i = 0; pixcnt; i++, pixcnt--, valptr++) {
// Pretty much pixel copier
if (!mosaic || !(i % mosh))
val = sizeof(pixfmt) == 2 ? eswap16(*valptr) : *valptr;
bitmap_pixel_write<rdtype, dsttype, mode, pixfmt>(dst_ptr++, val, palptr, px_attr);
}
}
else if (rdmode == SCALED) {
// Similarly to above, but now we need to sample pixels instead.
const u32 pixel_y = (u32)(source_y >> 8);
// Find the "inside" of the bitmap
while (start < end) {
u32 pixel_x = (u32)(source_x >> 8);
if (pixel_x < width)
break;
source_x += dx;
start++;
dst_ptr++;
}
u32 cnt = end - start;
pixfmt val = 0;
for (u32 i = 0; cnt; i++, cnt--) {
u32 pixel_x = (u32)(source_x >> 8);
if (pixel_x >= width)
break; // We reached the end of the bitmap
if (!mosaic || !(i % mosh)) {
pixfmt *valptr = &src_ptr[pixel_x + (pixel_y * width)];
val = sizeof(pixfmt) == 2 ? eswap16(*valptr) : *valptr;
}
bitmap_pixel_write<rdtype, dsttype, mode, pixfmt>(dst_ptr++, val, palptr, px_attr);
source_x += dx;
}
} else {
// Look for the first pixel to be drawn.
while (start < end) {
u32 pixel_x = (u32)(source_x >> 8), pixel_y = (u32)(source_y >> 8);
if (pixel_x < width && pixel_y < height)
break;
start++;
dst_ptr++;
source_x += dx; source_y += dy;
}
pixfmt val = 0;
for (u32 i = 0; start < end; start++) {
u32 pixel_x = (u32)(source_x >> 8), pixel_y = (u32)(source_y >> 8);
// Check if we run out of background pixels, stop drawing.
if (pixel_x >= width || pixel_y >= height)
break;
// Lookup pixel and draw it.
if (!mosaic || !(i % mosh)) {
pixfmt *valptr = &src_ptr[pixel_x + (pixel_y * width)];
val = sizeof(pixfmt) == 2 ? eswap16(*valptr) : *valptr;
}
bitmap_pixel_write<rdtype, dsttype, mode, pixfmt>(dst_ptr++, val, palptr, px_attr);
// Move to the next pixel, update coords accordingly
source_x += dx;
source_y += dy;
}
}
}
// Object/Sprite rendering logic
static const u8 obj_dim_table[3][4][2] = {
{ {8, 8}, {16, 16}, {32, 32}, {64, 64} },
{ {16, 8}, {32, 8}, {32, 16}, {64, 32} },
{ {8, 16}, {8, 32}, {16, 32}, {32, 64} }
};
static u8 obj_priority_list[5][160][128];
static u8 obj_priority_count[5][160];
static u8 obj_alpha_count[160];
typedef struct {
s32 obj_x, obj_y;
s32 obj_w, obj_h;
u32 attr1, attr2;
bool is_double;
} t_sprite;
// Renders a tile row (8 pixels) for a regular (non-affine) object/sprite.
// tile_offset points to the VRAM offset where the data lives.
template<typename dsttype, rendtype rdtype, bool is8bpp, bool hflip>
static inline void render_obj_part_tile_Nbpp(
u32 px_comb, dsttype *dest_ptr, u32 start, u32 end,
u32 tile_offset, u16 palette, const u16 *pal
) {
// Note that the last VRAM bank wrap around, hence the offset aliasing
const u8* tile_ptr = &vram[0x10000 + (tile_offset & 0x7FFF)];
u32 px_attr = px_comb | palette | 0x100; // Combine flags + high palette bit
if (is8bpp) {
// Each byte is a color, mapped to a palete.
for (u32 i = start; i < end; i++, dest_ptr++) {
// Honor hflip by selecting bytes in the correct order
u32 sel = hflip ? (7-i) : i;
u8 pval = tile_ptr[sel];
// Alhpa mode stacks previous value
if (pval) {
if (rdtype == FULLCOLOR)
*dest_ptr = pal[pval];
else if (rdtype == INDXCOLOR)
*dest_ptr = pval | px_attr; // Add combine flags
else if (rdtype == STCKCOLOR) {
// Stack pixels on top of the pixel value and combine flags
// We do not stack OBJ on OBJ, rather overwrite the previous object
if (*dest_ptr & 0x100)
*dest_ptr = pval | px_attr | ((*dest_ptr) & 0xFFFF0000);
else
*dest_ptr = pval | px_attr | ((*dest_ptr) << 16);