forked from melonDS-emu/melonDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPU2D_Soft.cpp
2176 lines (1830 loc) · 60.2 KB
/
GPU2D_Soft.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2016-2024 melonDS team
This file is part of melonDS.
melonDS is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#include "GPU2D_Soft.h"
#include "GPU.h"
#include "GPU3D.h"
namespace melonDS
{
namespace GPU2D
{
SoftRenderer::SoftRenderer(melonDS::GPU& gpu)
: Renderer2D(), GPU(gpu)
{
// mosaic table is initialized at compile-time
}
u32 SoftRenderer::ColorComposite(int i, u32 val1, u32 val2) const
{
u32 coloreffect = 0;
u32 eva, evb;
u32 flag1 = val1 >> 24;
u32 flag2 = val2 >> 24;
u32 blendCnt = CurUnit->BlendCnt;
u32 target2;
if (flag2 & 0x80) target2 = 0x1000;
else if (flag2 & 0x40) target2 = 0x0100;
else target2 = flag2 << 8;
if ((flag1 & 0x80) && (blendCnt & target2))
{
// sprite blending
coloreffect = 1;
if (flag1 & 0x40)
{
eva = flag1 & 0x1F;
evb = 16 - eva;
}
else
{
eva = CurUnit->EVA;
evb = CurUnit->EVB;
}
}
else if ((flag1 & 0x40) && (blendCnt & target2))
{
// 3D layer blending
coloreffect = 4;
}
else
{
if (flag1 & 0x80) flag1 = 0x10;
else if (flag1 & 0x40) flag1 = 0x01;
if ((blendCnt & flag1) && (WindowMask[i] & 0x20))
{
coloreffect = (blendCnt >> 6) & 0x3;
if (coloreffect == 1)
{
if (blendCnt & target2)
{
eva = CurUnit->EVA;
evb = CurUnit->EVB;
}
else
coloreffect = 0;
}
}
}
switch (coloreffect)
{
case 0: return val1;
case 1: return ColorBlend4(val1, val2, eva, evb);
case 2: return ColorBrightnessUp(val1, CurUnit->EVY, 0x8);
case 3: return ColorBrightnessDown(val1, CurUnit->EVY, 0x7);
case 4: return ColorBlend5(val1, val2);
}
return val1;
}
void SoftRenderer::DrawScanline(u32 line, Unit* unit)
{
CurUnit = unit;
int stride = GPU.GPU3D.IsRendererAccelerated() ? (256*3 + 1) : 256;
u32* dst = &Framebuffer[CurUnit->Num][stride * line];
int n3dline = line;
line = GPU.VCount;
if (CurUnit->Num == 0)
{
auto bgDirty = GPU.VRAMDirty_ABG.DeriveState(GPU.VRAMMap_ABG, GPU);
GPU.MakeVRAMFlat_ABGCoherent(bgDirty);
auto bgExtPalDirty = GPU.VRAMDirty_ABGExtPal.DeriveState(GPU.VRAMMap_ABGExtPal, GPU);
GPU.MakeVRAMFlat_ABGExtPalCoherent(bgExtPalDirty);
auto objExtPalDirty = GPU.VRAMDirty_AOBJExtPal.DeriveState(&GPU.VRAMMap_AOBJExtPal, GPU);
GPU.MakeVRAMFlat_AOBJExtPalCoherent(objExtPalDirty);
}
else
{
auto bgDirty = GPU.VRAMDirty_BBG.DeriveState(GPU.VRAMMap_BBG, GPU);
GPU.MakeVRAMFlat_BBGCoherent(bgDirty);
auto bgExtPalDirty = GPU.VRAMDirty_BBGExtPal.DeriveState(GPU.VRAMMap_BBGExtPal, GPU);
GPU.MakeVRAMFlat_BBGExtPalCoherent(bgExtPalDirty);
auto objExtPalDirty = GPU.VRAMDirty_BOBJExtPal.DeriveState(&GPU.VRAMMap_BOBJExtPal, GPU);
GPU.MakeVRAMFlat_BOBJExtPalCoherent(objExtPalDirty);
}
bool forceblank = false;
// scanlines that end up outside of the GPU drawing range
// (as a result of writing to VCount) are filled white
if (line > 192) forceblank = true;
// GPU B can be completely disabled by POWCNT1
// oddly that's not the case for GPU A
if (CurUnit->Num && !CurUnit->Enabled) forceblank = true;
if (line == 0 && CurUnit->CaptureCnt & (1 << 31) && !forceblank)
CurUnit->CaptureLatch = true;
if (CurUnit->Num == 0)
{
if (!GPU.GPU3D.IsRendererAccelerated())
_3DLine = GPU.GPU3D.GetLine(n3dline);
else if (CurUnit->CaptureLatch && (((CurUnit->CaptureCnt >> 29) & 0x3) != 1))
{
_3DLine = GPU.GPU3D.GetLine(n3dline);
//GPU3D::GLRenderer::PrepareCaptureFrame();
}
}
if (forceblank)
{
for (int i = 0; i < 256; i++)
dst[i] = 0xFFFFFFFF;
if (GPU.GPU3D.IsRendererAccelerated())
{
dst[256*3] = 0;
}
return;
}
u32 dispmode = CurUnit->DispCnt >> 16;
dispmode &= (CurUnit->Num ? 0x1 : 0x3);
// always render regular graphics
DrawScanline_BGOBJ(line);
CurUnit->UpdateMosaicCounters(line);
switch (dispmode)
{
case 0: // screen off
{
for (int i = 0; i < 256; i++)
dst[i] = 0x003F3F3F;
}
break;
case 1: // regular display
{
int i = 0;
for (; i < (stride & ~1); i+=2)
*(u64*)&dst[i] = *(u64*)&BGOBJLine[i];
}
break;
case 2: // VRAM display
{
u32 vrambank = (CurUnit->DispCnt >> 18) & 0x3;
if (GPU.VRAMMap_LCDC & (1<<vrambank))
{
u16* vram = (u16*)GPU.VRAM[vrambank];
vram = &vram[line * 256];
for (int i = 0; i < 256; i++)
{
u16 color = vram[i];
u8 r = (color & 0x001F) << 1;
u8 g = (color & 0x03E0) >> 4;
u8 b = (color & 0x7C00) >> 9;
dst[i] = r | (g << 8) | (b << 16);
}
}
else
{
for (int i = 0; i < 256; i++)
{
dst[i] = 0;
}
}
}
break;
case 3: // FIFO display
{
for (int i = 0; i < 256; i++)
{
u16 color = CurUnit->DispFIFOBuffer[i];
u8 r = (color & 0x001F) << 1;
u8 g = (color & 0x03E0) >> 4;
u8 b = (color & 0x7C00) >> 9;
dst[i] = r | (g << 8) | (b << 16);
}
}
break;
}
// capture
if ((CurUnit->Num == 0) && CurUnit->CaptureLatch)
{
u32 capwidth, capheight;
switch ((CurUnit->CaptureCnt >> 20) & 0x3)
{
case 0: capwidth = 128; capheight = 128; break;
case 1: capwidth = 256; capheight = 64; break;
case 2: capwidth = 256; capheight = 128; break;
case 3: capwidth = 256; capheight = 192; break;
}
if (line < capheight)
DoCapture(line, capwidth);
}
u32 masterBrightness = CurUnit->MasterBrightness;
if (GPU.GPU3D.IsRendererAccelerated())
{
u32 xpos = GPU.GPU3D.GetRenderXPos();
dst[256*3] = masterBrightness |
(CurUnit->DispCnt & 0x30000) |
(xpos << 24) | ((xpos & 0x100) << 15);
return;
}
// master brightness
if (dispmode != 0)
{
if ((masterBrightness >> 14) == 1)
{
// up
u32 factor = masterBrightness & 0x1F;
if (factor > 16) factor = 16;
for (int i = 0; i < 256; i++)
{
dst[i] = ColorBrightnessUp(dst[i], factor, 0x0);
}
}
else if ((masterBrightness >> 14) == 2)
{
// down
u32 factor = masterBrightness & 0x1F;
if (factor > 16) factor = 16;
for (int i = 0; i < 256; i++)
{
dst[i] = ColorBrightnessDown(dst[i], factor, 0xF);
}
}
}
// convert to 32-bit BGRA
// note: 32-bit RGBA would be more straightforward, but
// BGRA seems to be more compatible (Direct2D soft, cairo...)
for (int i = 0; i < 256; i+=2)
{
u64 c = *(u64*)&dst[i];
u64 r = (c << 18) & 0xFC000000FC0000;
u64 g = (c << 2) & 0xFC000000FC00;
u64 b = (c >> 14) & 0xFC000000FC;
c = r | g | b;
*(u64*)&dst[i] = c | ((c & 0x00C0C0C000C0C0C0) >> 6) | 0xFF000000FF000000;
}
}
void SoftRenderer::VBlankEnd(Unit* unitA, Unit* unitB)
{
#ifdef OGLRENDERER_ENABLED
if (Renderer3D& renderer3d = GPU.GPU3D.GetCurrentRenderer(); renderer3d.Accelerated)
{
if ((unitA->CaptureCnt & (1<<31)) && (((unitA->CaptureCnt >> 29) & 0x3) != 1))
{
renderer3d.PrepareCaptureFrame();
}
}
#endif
}
void SoftRenderer::DoCapture(u32 line, u32 width)
{
u32 captureCnt = CurUnit->CaptureCnt;
u32 dstvram = (captureCnt >> 16) & 0x3;
// TODO: confirm this
// it should work like VRAM display mode, which requires VRAM to be mapped to LCDC
if (!(GPU.VRAMMap_LCDC & (1<<dstvram)))
return;
u16* dst = (u16*)GPU.VRAM[dstvram];
u32 dstaddr = (((captureCnt >> 18) & 0x3) << 14) + (line * width);
// TODO: handle 3D in GPU3D::CurrentRenderer->Accelerated mode!!
u32* srcA;
if (captureCnt & (1<<24))
{
srcA = _3DLine;
}
else
{
srcA = BGOBJLine;
if (GPU.GPU3D.IsRendererAccelerated())
{
// in GPU3D::CurrentRenderer->Accelerated mode, compositing is normally done on the GPU
// but when doing display capture, we do need the composited output
// so we do it here
for (int i = 0; i < 256; i++)
{
u32 val1 = BGOBJLine[i];
u32 val2 = BGOBJLine[256+i];
u32 val3 = BGOBJLine[512+i];
u32 compmode = (val3 >> 24) & 0xF;
if (compmode == 4)
{
// 3D on top, blending
u32 _3dval = _3DLine[i];
if ((_3dval >> 24) > 0)
val1 = ColorBlend5(_3dval, val1);
else
val1 = val2;
}
else if (compmode == 1)
{
// 3D on bottom, blending
u32 _3dval = _3DLine[i];
if ((_3dval >> 24) > 0)
{
u32 eva = (val3 >> 8) & 0x1F;
u32 evb = (val3 >> 16) & 0x1F;
val1 = ColorBlend4(val1, _3dval, eva, evb);
}
else
val1 = val2;
}
else if (compmode <= 3)
{
// 3D on top, normal/fade
u32 _3dval = _3DLine[i];
if ((_3dval >> 24) > 0)
{
u32 evy = (val3 >> 8) & 0x1F;
val1 = _3dval;
if (compmode == 2) val1 = ColorBrightnessUp(val1, evy, 0x8);
else if (compmode == 3) val1 = ColorBrightnessDown(val1, evy, 0x7);
}
else
val1 = val2;
}
BGOBJLine[i] = val1;
}
}
}
u16* srcB = NULL;
u32 srcBaddr = line * 256;
if (captureCnt & (1<<25))
{
srcB = &CurUnit->DispFIFOBuffer[0];
srcBaddr = 0;
}
else
{
u32 srcvram = (CurUnit->DispCnt >> 18) & 0x3;
if (GPU.VRAMMap_LCDC & (1<<srcvram))
srcB = (u16*)GPU.VRAM[srcvram];
if (((CurUnit->DispCnt >> 16) & 0x3) != 2)
srcBaddr += ((captureCnt >> 26) & 0x3) << 14;
}
dstaddr &= 0xFFFF;
srcBaddr &= 0xFFFF;
static_assert(VRAMDirtyGranularity == 512);
GPU.VRAMDirty[dstvram][(dstaddr * 2) / VRAMDirtyGranularity] = true;
switch ((captureCnt >> 29) & 0x3)
{
case 0: // source A
{
for (u32 i = 0; i < width; i++)
{
u32 val = srcA[i];
// TODO: check what happens when alpha=0
u32 r = (val >> 1) & 0x1F;
u32 g = (val >> 9) & 0x1F;
u32 b = (val >> 17) & 0x1F;
u32 a = ((val >> 24) != 0) ? 0x8000 : 0;
dst[dstaddr] = r | (g << 5) | (b << 10) | a;
dstaddr = (dstaddr + 1) & 0xFFFF;
}
}
break;
case 1: // source B
{
if (srcB)
{
for (u32 i = 0; i < width; i++)
{
dst[dstaddr] = srcB[srcBaddr];
srcBaddr = (srcBaddr + 1) & 0xFFFF;
dstaddr = (dstaddr + 1) & 0xFFFF;
}
}
else
{
for (u32 i = 0; i < width; i++)
{
dst[dstaddr] = 0;
dstaddr = (dstaddr + 1) & 0xFFFF;
}
}
}
break;
case 2: // sources A+B
case 3:
{
u32 eva = captureCnt & 0x1F;
u32 evb = (captureCnt >> 8) & 0x1F;
// checkme
if (eva > 16) eva = 16;
if (evb > 16) evb = 16;
if (srcB)
{
for (u32 i = 0; i < width; i++)
{
u32 val = srcA[i];
// TODO: check what happens when alpha=0
u32 rA = (val >> 1) & 0x1F;
u32 gA = (val >> 9) & 0x1F;
u32 bA = (val >> 17) & 0x1F;
u32 aA = ((val >> 24) != 0) ? 1 : 0;
val = srcB[srcBaddr];
u32 rB = val & 0x1F;
u32 gB = (val >> 5) & 0x1F;
u32 bB = (val >> 10) & 0x1F;
u32 aB = val >> 15;
u32 rD = ((rA * aA * eva) + (rB * aB * evb) + 8) >> 4;
u32 gD = ((gA * aA * eva) + (gB * aB * evb) + 8) >> 4;
u32 bD = ((bA * aA * eva) + (bB * aB * evb) + 8) >> 4;
u32 aD = (eva>0 ? aA : 0) | (evb>0 ? aB : 0);
if (rD > 0x1F) rD = 0x1F;
if (gD > 0x1F) gD = 0x1F;
if (bD > 0x1F) bD = 0x1F;
dst[dstaddr] = rD | (gD << 5) | (bD << 10) | (aD << 15);
srcBaddr = (srcBaddr + 1) & 0xFFFF;
dstaddr = (dstaddr + 1) & 0xFFFF;
}
}
else
{
for (u32 i = 0; i < width; i++)
{
u32 val = srcA[i];
// TODO: check what happens when alpha=0
u32 rA = (val >> 1) & 0x1F;
u32 gA = (val >> 9) & 0x1F;
u32 bA = (val >> 17) & 0x1F;
u32 aA = ((val >> 24) != 0) ? 1 : 0;
u32 rD = ((rA * aA * eva) + 8) >> 4;
u32 gD = ((gA * aA * eva) + 8) >> 4;
u32 bD = ((bA * aA * eva) + 8) >> 4;
u32 aD = (eva>0 ? aA : 0);
dst[dstaddr] = rD | (gD << 5) | (bD << 10) | (aD << 15);
dstaddr = (dstaddr + 1) & 0xFFFF;
}
}
}
break;
}
}
#define DoDrawBG(type, line, num) \
do \
{ \
if ((bgCnt[num] & 0x0040) && (CurUnit->BGMosaicSize[0] > 0)) \
{ \
if (GPU.GPU3D.IsRendererAccelerated()) DrawBG_##type<true, DrawPixel_Accel>(line, num); \
else DrawBG_##type<true, DrawPixel_Normal>(line, num); \
} \
else \
{ \
if (GPU.GPU3D.IsRendererAccelerated()) DrawBG_##type<false, DrawPixel_Accel>(line, num); \
else DrawBG_##type<false, DrawPixel_Normal>(line, num); \
} \
} while (false)
#define DoDrawBG_Large(line) \
do \
{ \
if ((bgCnt[2] & 0x0040) && (CurUnit->BGMosaicSize[0] > 0)) \
{ \
if (GPU.GPU3D.IsRendererAccelerated()) DrawBG_Large<true, DrawPixel_Accel>(line); \
else DrawBG_Large<true, DrawPixel_Normal>(line); \
} \
else \
{ \
if (GPU.GPU3D.IsRendererAccelerated()) DrawBG_Large<false, DrawPixel_Accel>(line); \
else DrawBG_Large<false, DrawPixel_Normal>(line); \
} \
} while (false)
#define DoInterleaveSprites(prio) \
if (GPU.GPU3D.IsRendererAccelerated()) InterleaveSprites<DrawPixel_Accel>(prio); else InterleaveSprites<DrawPixel_Normal>(prio);
template<u32 bgmode>
void SoftRenderer::DrawScanlineBGMode(u32 line)
{
u32 dispCnt = CurUnit->DispCnt;
u16* bgCnt = CurUnit->BGCnt;
for (int i = 3; i >= 0; i--)
{
if ((bgCnt[3] & 0x3) == i)
{
if (dispCnt & 0x0800)
{
if (bgmode >= 3)
DoDrawBG(Extended, line, 3);
else if (bgmode >= 1)
DoDrawBG(Affine, line, 3);
else
DoDrawBG(Text, line, 3);
}
}
if ((bgCnt[2] & 0x3) == i)
{
if (dispCnt & 0x0400)
{
if (bgmode == 5)
DoDrawBG(Extended, line, 2);
else if (bgmode == 4 || bgmode == 2)
DoDrawBG(Affine, line, 2);
else
DoDrawBG(Text, line, 2);
}
}
if ((bgCnt[1] & 0x3) == i)
{
if (dispCnt & 0x0200)
{
DoDrawBG(Text, line, 1);
}
}
if ((bgCnt[0] & 0x3) == i)
{
if (dispCnt & 0x0100)
{
if (!CurUnit->Num && (dispCnt & 0x8))
DrawBG_3D();
else
DoDrawBG(Text, line, 0);
}
}
if ((dispCnt & 0x1000) && NumSprites[CurUnit->Num])
{
DoInterleaveSprites(0x40000 | (i<<16));
}
}
}
void SoftRenderer::DrawScanlineBGMode6(u32 line)
{
u32 dispCnt = CurUnit->DispCnt;
u16* bgCnt = CurUnit->BGCnt;
for (int i = 3; i >= 0; i--)
{
if ((bgCnt[2] & 0x3) == i)
{
if (dispCnt & 0x0400)
{
DoDrawBG_Large(line);
}
}
if ((bgCnt[0] & 0x3) == i)
{
if (dispCnt & 0x0100)
{
if ((!CurUnit->Num) && (dispCnt & 0x8))
DrawBG_3D();
}
}
if ((dispCnt & 0x1000) && NumSprites[CurUnit->Num])
{
DoInterleaveSprites(0x40000 | (i<<16))
}
}
}
void SoftRenderer::DrawScanlineBGMode7(u32 line)
{
u32 dispCnt = CurUnit->DispCnt;
u16* bgCnt = CurUnit->BGCnt;
// mode 7 only has text-mode BG0 and BG1
for (int i = 3; i >= 0; i--)
{
if ((bgCnt[1] & 0x3) == i)
{
if (dispCnt & 0x0200)
{
DoDrawBG(Text, line, 1);
}
}
if ((bgCnt[0] & 0x3) == i)
{
if (dispCnt & 0x0100)
{
if (!CurUnit->Num && (dispCnt & 0x8))
DrawBG_3D();
else
DoDrawBG(Text, line, 0);
}
}
if ((dispCnt & 0x1000) && NumSprites[CurUnit->Num])
{
DoInterleaveSprites(0x40000 | (i<<16))
}
}
}
void SoftRenderer::DrawScanline_BGOBJ(u32 line)
{
// forced blank disables BG/OBJ compositing
if (CurUnit->DispCnt & (1<<7))
{
for (int i = 0; i < 256; i++)
BGOBJLine[i] = 0xFF3F3F3F;
return;
}
u64 backdrop;
if (CurUnit->Num) backdrop = *(u16*)&GPU.Palette[0x400];
else backdrop = *(u16*)&GPU.Palette[0];
{
u8 r = (backdrop & 0x001F) << 1;
u8 g = (backdrop & 0x03E0) >> 4;
u8 b = (backdrop & 0x7C00) >> 9;
backdrop = r | (g << 8) | (b << 16) | 0x20000000;
backdrop |= (backdrop << 32);
for (int i = 0; i < 256; i+=2)
*(u64*)&BGOBJLine[i] = backdrop;
}
if (CurUnit->DispCnt & 0xE000)
CurUnit->CalculateWindowMask(line, WindowMask, OBJWindow[CurUnit->Num]);
else
memset(WindowMask, 0xFF, 256);
ApplySpriteMosaicX();
CurBGXMosaicTable = MosaicTable[CurUnit->BGMosaicSize[0]].data();
switch (CurUnit->DispCnt & 0x7)
{
case 0: DrawScanlineBGMode<0>(line); break;
case 1: DrawScanlineBGMode<1>(line); break;
case 2: DrawScanlineBGMode<2>(line); break;
case 3: DrawScanlineBGMode<3>(line); break;
case 4: DrawScanlineBGMode<4>(line); break;
case 5: DrawScanlineBGMode<5>(line); break;
case 6: DrawScanlineBGMode6(line); break;
case 7: DrawScanlineBGMode7(line); break;
}
// color special effects
// can likely be optimized
if (!GPU.GPU3D.IsRendererAccelerated())
{
for (int i = 0; i < 256; i++)
{
u32 val1 = BGOBJLine[i];
u32 val2 = BGOBJLine[256+i];
BGOBJLine[i] = ColorComposite(i, val1, val2);
}
}
else
{
if (CurUnit->Num == 0)
{
for (int i = 0; i < 256; i++)
{
u32 val1 = BGOBJLine[i];
u32 val2 = BGOBJLine[256+i];
u32 val3 = BGOBJLine[512+i];
u32 flag1 = val1 >> 24;
u32 flag2 = val2 >> 24;
u32 bldcnteffect = (CurUnit->BlendCnt >> 6) & 0x3;
u32 target1;
if (flag1 & 0x80) target1 = 0x0010;
else if (flag1 & 0x40) target1 = 0x0001;
else target1 = flag1;
u32 target2;
if (flag2 & 0x80) target2 = 0x1000;
else if (flag2 & 0x40) target2 = 0x0100;
else target2 = flag2 << 8;
if (((flag1 & 0xC0) == 0x40) && (CurUnit->BlendCnt & target2))
{
// 3D on top, blending
BGOBJLine[i] = val2;
BGOBJLine[256+i] = ColorComposite(i, val2, val3);
BGOBJLine[512+i] = 0x04000000;
}
else if ((flag1 & 0xC0) == 0x40)
{
// 3D on top, normal/fade
if (bldcnteffect == 1) bldcnteffect = 0;
if (!(CurUnit->BlendCnt & 0x0001)) bldcnteffect = 0;
if (!(WindowMask[i] & 0x20)) bldcnteffect = 0;
BGOBJLine[i] = val2;
BGOBJLine[256+i] = ColorComposite(i, val2, val3);
BGOBJLine[512+i] = (bldcnteffect << 24) | (CurUnit->EVY << 8);
}
else if (((flag2 & 0xC0) == 0x40) && ((CurUnit->BlendCnt & 0x01C0) == 0x0140))
{
// 3D on bottom, blending
u32 eva, evb;
if ((flag1 & 0xC0) == 0xC0)
{
eva = flag1 & 0x1F;
evb = 16 - eva;
}
else if (((CurUnit->BlendCnt & target1) && (WindowMask[i] & 0x20)) ||
((flag1 & 0xC0) == 0x80))
{
eva = CurUnit->EVA;
evb = CurUnit->EVB;
}
else
bldcnteffect = 7;
BGOBJLine[i] = val1;
BGOBJLine[256+i] = ColorComposite(i, val1, val3);
BGOBJLine[512+i] = (bldcnteffect << 24) | (CurUnit->EVB << 16) | (CurUnit->EVA << 8);
}
else
{
// no potential 3D pixel involved
BGOBJLine[i] = ColorComposite(i, val1, val2);
BGOBJLine[256+i] = 0;
BGOBJLine[512+i] = 0x07000000;
}
}
}
else
{
for (int i = 0; i < 256; i++)
{
u32 val1 = BGOBJLine[i];
u32 val2 = BGOBJLine[256+i];
BGOBJLine[i] = ColorComposite(i, val1, val2);
BGOBJLine[256+i] = 0;
BGOBJLine[512+i] = 0x07000000;
}
}
}
if (CurUnit->BGMosaicY >= CurUnit->BGMosaicYMax)
{
CurUnit->BGMosaicY = 0;
CurUnit->BGMosaicYMax = CurUnit->BGMosaicSize[1];
}
else
CurUnit->BGMosaicY++;
/*if (OBJMosaicY >= OBJMosaicYMax)
{
OBJMosaicY = 0;
OBJMosaicYMax = OBJMosaicSize[1];
}
else
OBJMosaicY++;*/
}
void SoftRenderer::DrawPixel_Normal(u32* dst, u16 color, u32 flag)
{
u8 r = (color & 0x001F) << 1;
u8 g = (color & 0x03E0) >> 4;
u8 b = (color & 0x7C00) >> 9;
//g |= ((color & 0x8000) >> 15);
*(dst+256) = *dst;
*dst = r | (g << 8) | (b << 16) | flag;
}
void SoftRenderer::DrawPixel_Accel(u32* dst, u16 color, u32 flag)
{
u8 r = (color & 0x001F) << 1;
u8 g = (color & 0x03E0) >> 4;
u8 b = (color & 0x7C00) >> 9;
*(dst+512) = *(dst+256);
*(dst+256) = *dst;
*dst = r | (g << 8) | (b << 16) | flag;
}
void SoftRenderer::DrawBG_3D()
{
int i = 0;
if (GPU.GPU3D.IsRendererAccelerated())
{
for (i = 0; i < 256; i++)
{
if (!(WindowMask[i] & 0x01)) continue;
BGOBJLine[i+512] = BGOBJLine[i+256];
BGOBJLine[i+256] = BGOBJLine[i];
BGOBJLine[i] = 0x40000000; // 3D-layer placeholder
}
}
else
{
for (i = 0; i < 256; i++)
{
u32 c = _3DLine[i];
if ((c >> 24) == 0) continue;
if (!(WindowMask[i] & 0x01)) continue;
BGOBJLine[i+256] = BGOBJLine[i];
BGOBJLine[i] = c | 0x40000000;
}
}
}
template<bool mosaic, SoftRenderer::DrawPixel drawPixel>
void SoftRenderer::DrawBG_Text(u32 line, u32 bgnum)
{
// workaround for backgrounds missing on aarch64 with lto build
asm volatile ("" : : : "memory");
u16 bgcnt = CurUnit->BGCnt[bgnum];
u32 tilesetaddr, tilemapaddr;
u16* pal;
u32 extpal, extpalslot;
u16 xoff = CurUnit->BGXPos[bgnum];
u16 yoff = CurUnit->BGYPos[bgnum] + line;
if (bgcnt & 0x0040)
{
// vertical mosaic
yoff -= CurUnit->BGMosaicY;
}
u32 widexmask = (bgcnt & 0x4000) ? 0x100 : 0;
extpal = (CurUnit->DispCnt & 0x40000000);
if (extpal) extpalslot = ((bgnum<2) && (bgcnt&0x2000)) ? (2+bgnum) : bgnum;
u8* bgvram;
u32 bgvrammask;
CurUnit->GetBGVRAM(bgvram, bgvrammask);
if (CurUnit->Num)
{
tilesetaddr = ((bgcnt & 0x003C) << 12);
tilemapaddr = ((bgcnt & 0x1F00) << 3);
pal = (u16*)&GPU.Palette[0x400];
}
else
{
tilesetaddr = ((CurUnit->DispCnt & 0x07000000) >> 8) + ((bgcnt & 0x003C) << 12);
tilemapaddr = ((CurUnit->DispCnt & 0x38000000) >> 11) + ((bgcnt & 0x1F00) << 3);
pal = (u16*)&GPU.Palette[0];
}
// adjust Y position in tilemap
if (bgcnt & 0x8000)
{
tilemapaddr += ((yoff & 0x1F8) << 3);
if (bgcnt & 0x4000)
tilemapaddr += ((yoff & 0x100) << 3);
}
else
tilemapaddr += ((yoff & 0xF8) << 3);
u16 curtile;
u16* curpal;
u32 pixelsaddr;
u8 color;
u32 lastxpos;
if (bgcnt & 0x0080)
{
// 256-color
// preload shit as needed
if ((xoff & 0x7) || mosaic)
{
curtile = *(u16*)&bgvram[(tilemapaddr + ((xoff & 0xF8) >> 2) + ((xoff & widexmask) << 3)) & bgvrammask];
if (extpal) curpal = CurUnit->GetBGExtPal(extpalslot, curtile>>12);
else curpal = pal;
pixelsaddr = tilesetaddr + ((curtile & 0x03FF) << 6)
+ (((curtile & 0x0800) ? (7-(yoff&0x7)) : (yoff&0x7)) << 3);
}
if (mosaic) lastxpos = xoff;
for (int i = 0; i < 256; i++)
{
u32 xpos;
if (mosaic) xpos = xoff - CurBGXMosaicTable[i];
else xpos = xoff;
if ((!mosaic && (!(xpos & 0x7))) ||
(mosaic && ((xpos >> 3) != (lastxpos >> 3))))
{