/
GPU.cpp
8429 lines (7187 loc) · 324 KB
/
GPU.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 (C) 2006 yopyop
Copyright (C) 2006-2007 Theo Berkau
Copyright (C) 2007 shash
Copyright (C) 2008-2017 DeSmuME team
This file 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 file 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 the this software. If not, see <http://www.gnu.org/licenses/>.
*/
#include "GPU.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include "common.h"
#include "MMU.h"
#include "FIFO.h"
#include "debug.h"
#include "render3D.h"
#include "registers.h"
#include "gfx3d.h"
#include "debug.h"
#include "NDSSystem.h"
#include "readwrite.h"
#include "matrix.h"
#include "emufile.h"
#ifdef FASTBUILD
#undef FORCEINLINE
#define FORCEINLINE
//compilation speed hack (cuts time exactly in half by cutting out permutations)
#define DISABLE_MOSAIC
#define DISABLE_COLOREFFECTDISABLEHINT
#endif
//instantiate static instance
u16 GPUEngineBase::_brightnessUpTable555[17][0x8000];
FragmentColor GPUEngineBase::_brightnessUpTable666[17][0x8000];
FragmentColor GPUEngineBase::_brightnessUpTable888[17][0x8000];
u16 GPUEngineBase::_brightnessDownTable555[17][0x8000];
FragmentColor GPUEngineBase::_brightnessDownTable666[17][0x8000];
FragmentColor GPUEngineBase::_brightnessDownTable888[17][0x8000];
u8 GPUEngineBase::_blendTable555[17][17][32][32];
GPUEngineBase::MosaicLookup GPUEngineBase::_mosaicLookup;
GPUSubsystem *GPU = NULL;
static size_t _gpuLargestDstLineCount = 1;
static size_t _gpuVRAMBlockOffset = GPU_VRAM_BLOCK_LINES * GPU_FRAMEBUFFER_NATIVE_WIDTH;
static u16 *_gpuDstToSrcIndex = NULL; // Key: Destination pixel index / Value: Source pixel index
static u8 *_gpuDstToSrcSSSE3_u8_8e = NULL;
static u8 *_gpuDstToSrcSSSE3_u8_16e = NULL;
static u8 *_gpuDstToSrcSSSE3_u16_8e = NULL;
static CACHE_ALIGN size_t _gpuDstPitchCount[GPU_FRAMEBUFFER_NATIVE_WIDTH]; // Key: Source pixel index in x-dimension / Value: Number of x-dimension destination pixels for the source pixel
static CACHE_ALIGN size_t _gpuDstPitchIndex[GPU_FRAMEBUFFER_NATIVE_WIDTH]; // Key: Source pixel index in x-dimension / Value: First destination pixel that maps to the source pixel
static CACHE_ALIGN size_t _gpuDstLineCount[GPU_FRAMEBUFFER_NATIVE_HEIGHT]; // Key: Source line index / Value: Number of destination lines for the source line
static CACHE_ALIGN size_t _gpuDstLineIndex[GPU_FRAMEBUFFER_NATIVE_HEIGHT]; // Key: Source line index / Value: First destination line that maps to the source line
static CACHE_ALIGN size_t _gpuCaptureLineCount[GPU_VRAM_BLOCK_LINES + 1]; // Key: Source line index / Value: Number of destination lines for the source line
static CACHE_ALIGN size_t _gpuCaptureLineIndex[GPU_VRAM_BLOCK_LINES + 1]; // Key: Source line index / Value: First destination line that maps to the source line
const CACHE_ALIGN SpriteSize GPUEngineBase::_sprSizeTab[4][4] = {
{{8, 8}, {16, 8}, {8, 16}, {8, 8}},
{{16, 16}, {32, 8}, {8, 32}, {8, 8}},
{{32, 32}, {32, 16}, {16, 32}, {8, 8}},
{{64, 64}, {64, 32}, {32, 64}, {8, 8}},
};
const CACHE_ALIGN BGType GPUEngineBase::_mode2type[8][4] = {
{BGType_Text, BGType_Text, BGType_Text, BGType_Text},
{BGType_Text, BGType_Text, BGType_Text, BGType_Affine},
{BGType_Text, BGType_Text, BGType_Affine, BGType_Affine},
{BGType_Text, BGType_Text, BGType_Text, BGType_AffineExt},
{BGType_Text, BGType_Text, BGType_Affine, BGType_AffineExt},
{BGType_Text, BGType_Text, BGType_AffineExt, BGType_AffineExt},
{BGType_Invalid, BGType_Invalid, BGType_Large8bpp, BGType_Invalid},
{BGType_Invalid, BGType_Invalid, BGType_Invalid, BGType_Invalid}
};
//dont ever think of changing these to bits because you could avoid the multiplies in the main tile blitter.
//it doesnt really help any
const CACHE_ALIGN BGLayerSize GPUEngineBase::_BGLayerSizeLUT[8][4] = {
{{0, 0}, {0, 0}, {0, 0}, {0, 0}}, //Invalid
{{256,256}, {512,256}, {256,512}, {512,512}}, //text
{{128,128}, {256,256}, {512,512}, {1024,1024}}, //affine
{{512,1024}, {1024,512}, {0,0}, {0,0}}, //large 8bpp
{{0, 0}, {0, 0}, {0, 0}, {0, 0}}, //affine ext (to be elaborated with another value)
{{128,128}, {256,256}, {512,512}, {1024,1024}}, //affine ext 256x16
{{128,128}, {256,256}, {512,256}, {512,512}}, //affine ext 256x1
{{128,128}, {256,256}, {512,256}, {512,512}}, //affine ext direct
};
static FORCEINLINE void ExpandLine8(u8 *__restrict dst, const u8 *__restrict src, size_t dstLength)
{
#ifdef ENABLE_SSSE3
const bool isIntegerScale = ((dstLength % GPU_FRAMEBUFFER_NATIVE_WIDTH) == 0);
if (isIntegerScale)
{
const size_t scale = dstLength / GPU_FRAMEBUFFER_NATIVE_WIDTH;
for (size_t srcX = 0, dstX = 0; srcX < GPU_FRAMEBUFFER_NATIVE_WIDTH; srcX+=16, dstX+=(scale*16))
{
const __m128i src_vec128 = _mm_load_si128((__m128i *)(src + srcX));
for (size_t s = 0; s < scale; s++)
{
const __m128i ssse3idx_u8 = _mm_load_si128((__m128i *)(_gpuDstToSrcSSSE3_u8_16e + (s * 16)));
_mm_store_si128( (__m128i *)(dst + dstX + (s * 16)), _mm_shuffle_epi8(src_vec128, ssse3idx_u8) );
}
}
}
else
#endif
{
for (size_t x = 0; x < GPU_FRAMEBUFFER_NATIVE_WIDTH; x++)
{
for (size_t p = 0; p < _gpuDstPitchCount[x]; p++)
{
dst[_gpuDstPitchIndex[x] + p] = src[x];
}
}
}
}
/*****************************************************************************/
// BACKGROUND RENDERING -ROTOSCALE-
/*****************************************************************************/
FORCEINLINE void rot_tiled_8bit_entry(const s32 auxX, const s32 auxY, const int lg, const u32 map, const u32 tile, const u16 *__restrict pal, u8 &outIndex, u16 &outColor)
{
const u16 tileindex = *(u8*)MMU_gpu_map(map + ((auxX>>3) + (auxY>>3) * (lg>>3)));
const u16 x = auxX & 0x0007;
const u16 y = auxY & 0x0007;
outIndex = *(u8*)MMU_gpu_map(tile + ((tileindex<<6)+(y<<3)+x));
outColor = LE_TO_LOCAL_16(pal[outIndex]);
}
template<bool EXTPAL>
FORCEINLINE void rot_tiled_16bit_entry(const s32 auxX, const s32 auxY, const int lg, const u32 map, const u32 tile, const u16 *__restrict pal, u8 &outIndex, u16 &outColor)
{
TILEENTRY tileentry;
tileentry.val = LE_TO_LOCAL_16( *(u16 *)MMU_gpu_map(map + (((auxX>>3) + (auxY>>3) * (lg>>3))<<1)) );
const u16 x = ((tileentry.bits.HFlip) ? 7 - (auxX) : (auxX)) & 0x0007;
const u16 y = ((tileentry.bits.VFlip) ? 7 - (auxY) : (auxY)) & 0x0007;
outIndex = *(u8*)MMU_gpu_map(tile + ((tileentry.bits.TileNum<<6)+(y<<3)+x));
outColor = LE_TO_LOCAL_16(pal[(outIndex + (EXTPAL ? (tileentry.bits.Palette<<8) : 0))]);
}
FORCEINLINE void rot_256_map(const s32 auxX, const s32 auxY, const int lg, const u32 map, const u32 tile, const u16 *__restrict pal, u8 &outIndex, u16 &outColor)
{
outIndex = *(u8*)MMU_gpu_map(map + ((auxX + auxY * lg)));
outColor = LE_TO_LOCAL_16(pal[outIndex]);
}
FORCEINLINE void rot_BMP_map(const s32 auxX, const s32 auxY, const int lg, const u32 map, const u32 tile, const u16 *__restrict pal, u8 &outIndex, u16 &outColor)
{
outColor = LE_TO_LOCAL_16( *(u16 *)MMU_gpu_map(map + ((auxX + auxY * lg) << 1)) );
outIndex = ((outColor & 0x8000) == 0) ? 0 : 1;
}
void gpu_savestate(EMUFILE* os)
{
const NDSDisplayInfo &dispInfo = GPU->GetDisplayInfo();
const GPUEngineA *mainEngine = GPU->GetEngineMain();
const GPUEngineB *subEngine = GPU->GetEngineSub();
//version
write32le(1,os);
os->fwrite((u8 *)dispInfo.masterCustomBuffer, GPU_FRAMEBUFFER_NATIVE_WIDTH * GPU_FRAMEBUFFER_NATIVE_HEIGHT * sizeof(u16) * 2);
write32le(mainEngine->savedBG2X.value, os);
write32le(mainEngine->savedBG2Y.value, os);
write32le(mainEngine->savedBG3X.value, os);
write32le(mainEngine->savedBG3Y.value, os);
write32le(subEngine->savedBG2X.value, os);
write32le(subEngine->savedBG2Y.value, os);
write32le(subEngine->savedBG3X.value, os);
write32le(subEngine->savedBG3Y.value, os);
}
bool gpu_loadstate(EMUFILE* is, int size)
{
const NDSDisplayInfo &dispInfo = GPU->GetDisplayInfo();
GPUEngineA *mainEngine = GPU->GetEngineMain();
GPUEngineB *subEngine = GPU->GetEngineSub();
//read version
u32 version;
//sigh.. shouldve used a new version number
if (size == GPU_FRAMEBUFFER_NATIVE_WIDTH * GPU_FRAMEBUFFER_NATIVE_HEIGHT * sizeof(u16) * 2)
{
version = 0;
}
else if (size == 0x30024)
{
read32le(&version,is);
version = 1;
}
else
{
if(read32le(&version,is) != 1) return false;
}
if (version > 1) return false;
is->fread((u8 *)dispInfo.masterCustomBuffer, GPU_FRAMEBUFFER_NATIVE_WIDTH * GPU_FRAMEBUFFER_NATIVE_HEIGHT * sizeof(u16) * 2);
if (version == 1)
{
read32le((u32 *)&mainEngine->savedBG2X, is);
read32le((u32 *)&mainEngine->savedBG2Y, is);
read32le((u32 *)&mainEngine->savedBG3X, is);
read32le((u32 *)&mainEngine->savedBG3Y, is);
read32le((u32 *)&subEngine->savedBG2X, is);
read32le((u32 *)&subEngine->savedBG2Y, is);
read32le((u32 *)&subEngine->savedBG3X, is);
read32le((u32 *)&subEngine->savedBG3Y, is);
//removed per nitsuja feedback. anyway, this same thing will happen almost immediately in gpu line=0
//mainEngine->refreshAffineStartRegs(-1,-1);
//subEngine->refreshAffineStartRegs(-1,-1);
}
mainEngine->ParseAllRegisters();
subEngine->ParseAllRegisters();
return !is->fail();
}
/*****************************************************************************/
// INITIALIZATION
/*****************************************************************************/
void GPUEngineBase::_InitLUTs()
{
static bool didInit = false;
if (didInit)
{
return;
}
/*
NOTE: gbatek (in the reference above) seems to expect 6bit values
per component, but as desmume works with 5bit per component,
we use 31 as top, instead of 63. Testing it on a few games,
using 63 seems to give severe color wraping, and 31 works
nicely, so for now we'll just that, until proven wrong.
i have seen pics of pokemon ranger getting white with 31, with 63 it is nice.
it could be pb of alpha or blending or...
MightyMax> created a test NDS to check how the brightness values work,
and 31 seems to be correct. FactorEx is a override for max brighten/darken
See: http://mightymax.org/gfx_test_brightness.nds
The Pokemon Problem could be a problem with 8/32 bit writes not recognized yet,
i'll add that so you can check back.
*/
for (u16 i = 0; i <= 16; i++)
{
for (u16 j = 0x0000; j < 0x8000; j++)
{
COLOR cur;
cur.val = j;
cur.bits.red = (cur.bits.red + ((31 - cur.bits.red) * i / 16));
cur.bits.green = (cur.bits.green + ((31 - cur.bits.green) * i / 16));
cur.bits.blue = (cur.bits.blue + ((31 - cur.bits.blue) * i / 16));
cur.bits.alpha = 0;
GPUEngineBase::_brightnessUpTable555[i][j] = cur.val;
GPUEngineBase::_brightnessUpTable666[i][j].color = COLOR555TO666(cur.val);
GPUEngineBase::_brightnessUpTable888[i][j].color = COLOR555TO888(cur.val);
cur.val = j;
cur.bits.red = (cur.bits.red - (cur.bits.red * i / 16));
cur.bits.green = (cur.bits.green - (cur.bits.green * i / 16));
cur.bits.blue = (cur.bits.blue - (cur.bits.blue * i / 16));
cur.bits.alpha = 0;
GPUEngineBase::_brightnessDownTable555[i][j] = cur.val;
GPUEngineBase::_brightnessDownTable666[i][j].color = COLOR555TO666(cur.val);
GPUEngineBase::_brightnessDownTable888[i][j].color = COLOR555TO888(cur.val);
}
}
for(int c0=0;c0<=31;c0++)
for(int c1=0;c1<=31;c1++)
for(int eva=0;eva<=16;eva++)
for(int evb=0;evb<=16;evb++)
{
int blend = ((c0 * eva) + (c1 * evb) ) / 16;
int final = std::min<int>(31,blend);
GPUEngineBase::_blendTable555[eva][evb][c0][c1] = final;
}
didInit = true;
}
GPUEngineBase::GPUEngineBase()
{
_IORegisterMap = NULL;
_paletteOBJ = NULL;
_BGLayer[GPULayerID_BG0].layerID = GPULayerID_BG0;
_BGLayer[GPULayerID_BG1].layerID = GPULayerID_BG1;
_BGLayer[GPULayerID_BG2].layerID = GPULayerID_BG2;
_BGLayer[GPULayerID_BG3].layerID = GPULayerID_BG3;
_BGLayer[GPULayerID_BG0].extPaletteSlot = GPULayerID_BG0;
_BGLayer[GPULayerID_BG1].extPaletteSlot = GPULayerID_BG1;
_BGLayer[GPULayerID_BG2].extPaletteSlot = GPULayerID_BG2;
_BGLayer[GPULayerID_BG3].extPaletteSlot = GPULayerID_BG3;
_BGLayer[GPULayerID_BG0].extPalette = NULL;
_BGLayer[GPULayerID_BG1].extPalette = NULL;
_BGLayer[GPULayerID_BG2].extPalette = NULL;
_BGLayer[GPULayerID_BG3].extPalette = NULL;
_InitLUTs();
_internalRenderLineTargetCustom = NULL;
_renderLineLayerIDCustom = NULL;
_bgLayerIndexCustom = NULL;
_bgLayerColorCustom = NULL;
_didPassWindowTestCustomMasterPtr = NULL;
_didPassWindowTestCustom[GPULayerID_BG0] = NULL;
_didPassWindowTestCustom[GPULayerID_BG1] = NULL;
_didPassWindowTestCustom[GPULayerID_BG2] = NULL;
_didPassWindowTestCustom[GPULayerID_BG3] = NULL;
_didPassWindowTestCustom[GPULayerID_OBJ] = NULL;
_enableColorEffectCustomMasterPtr = NULL;
_enableColorEffectCustom[GPULayerID_BG0] = NULL;
_enableColorEffectCustom[GPULayerID_BG1] = NULL;
_enableColorEffectCustom[GPULayerID_BG2] = NULL;
_enableColorEffectCustom[GPULayerID_BG3] = NULL;
_enableColorEffectCustom[GPULayerID_OBJ] = NULL;
}
GPUEngineBase::~GPUEngineBase()
{
free_aligned(this->_internalRenderLineTargetCustom);
this->_internalRenderLineTargetCustom = NULL;
free_aligned(this->_renderLineLayerIDCustom);
this->_renderLineLayerIDCustom = NULL;
free_aligned(this->_bgLayerIndexCustom);
this->_bgLayerIndexCustom = NULL;
free_aligned(this->_bgLayerColorCustom);
this->_bgLayerColorCustom = NULL;
free_aligned(this->_didPassWindowTestCustomMasterPtr);
this->_didPassWindowTestCustomMasterPtr = NULL;
this->_didPassWindowTestCustom[GPULayerID_BG0] = NULL;
this->_didPassWindowTestCustom[GPULayerID_BG1] = NULL;
this->_didPassWindowTestCustom[GPULayerID_BG2] = NULL;
this->_didPassWindowTestCustom[GPULayerID_BG3] = NULL;
this->_didPassWindowTestCustom[GPULayerID_OBJ] = NULL;
this->_enableColorEffectCustomMasterPtr = NULL;
this->_enableColorEffectCustom[GPULayerID_BG0] = NULL;
this->_enableColorEffectCustom[GPULayerID_BG1] = NULL;
this->_enableColorEffectCustom[GPULayerID_BG2] = NULL;
this->_enableColorEffectCustom[GPULayerID_BG3] = NULL;
this->_enableColorEffectCustom[GPULayerID_OBJ] = NULL;
}
void GPUEngineBase::_Reset_Base()
{
const NDSDisplayInfo &dispInfo = GPU->GetDisplayInfo();
memset(this->_sprColor, 0, sizeof(this->_sprColor));
memset(this->_sprAlpha, 0, sizeof(this->_sprAlpha));
memset(this->_sprType, OBJMode_Normal, sizeof(this->_sprType));
memset(this->_sprPrio, 0x7F, sizeof(this->_sprPrio));
memset(this->_sprNum, 0, sizeof(this->_sprNum));
memset(this->_didPassWindowTestNative, 1, 5 * GPU_FRAMEBUFFER_NATIVE_WIDTH * sizeof(u8));
memset(this->_enableColorEffectNative, 1, 5 * GPU_FRAMEBUFFER_NATIVE_WIDTH * sizeof(u8));
memset(this->_didPassWindowTestCustomMasterPtr, 1, 10 * dispInfo.customWidth * sizeof(u8));
memset(this->_h_win[0], 0, sizeof(this->_h_win[0]));
memset(this->_h_win[1], 0, sizeof(this->_h_win[1]));
memset(&this->_mosaicColors, 0, sizeof(MosaicColor));
memset(this->_itemsForPriority, 0, sizeof(this->_itemsForPriority));
memset(this->_internalRenderLineTargetNative, 0, GPU_FRAMEBUFFER_NATIVE_WIDTH * sizeof(FragmentColor));
if (this->_internalRenderLineTargetCustom != NULL)
{
memset(this->_internalRenderLineTargetCustom, 0, dispInfo.customWidth * _gpuLargestDstLineCount * dispInfo.pixelBytes);
}
if (this->_renderLineLayerIDCustom != NULL)
{
memset(this->_renderLineLayerIDCustom, 0, dispInfo.customWidth * _gpuLargestDstLineCount * 4 * sizeof(u8));
}
this->_enableLayer[GPULayerID_BG0] = false;
this->_enableLayer[GPULayerID_BG1] = false;
this->_enableLayer[GPULayerID_BG2] = false;
this->_enableLayer[GPULayerID_BG3] = false;
this->_enableLayer[GPULayerID_OBJ] = false;
this->_isAnyBGLayerEnabled = false;
this->_BGLayer[GPULayerID_BG0].BGnCNT = this->_IORegisterMap->BG0CNT;
this->_BGLayer[GPULayerID_BG1].BGnCNT = this->_IORegisterMap->BG1CNT;
this->_BGLayer[GPULayerID_BG2].BGnCNT = this->_IORegisterMap->BG2CNT;
this->_BGLayer[GPULayerID_BG3].BGnCNT = this->_IORegisterMap->BG3CNT;
this->_BGLayer[GPULayerID_BG0].size = GPUEngineBase::_BGLayerSizeLUT[BGType_Affine][1];
this->_BGLayer[GPULayerID_BG1].size = GPUEngineBase::_BGLayerSizeLUT[BGType_Affine][1];
this->_BGLayer[GPULayerID_BG2].size = GPUEngineBase::_BGLayerSizeLUT[BGType_Affine][1];
this->_BGLayer[GPULayerID_BG3].size = GPUEngineBase::_BGLayerSizeLUT[BGType_Affine][1];
this->_BGLayer[GPULayerID_BG0].baseType = BGType_Invalid;
this->_BGLayer[GPULayerID_BG1].baseType = BGType_Invalid;
this->_BGLayer[GPULayerID_BG2].baseType = BGType_Invalid;
this->_BGLayer[GPULayerID_BG3].baseType = BGType_Invalid;
this->_BGLayer[GPULayerID_BG0].type = BGType_Invalid;
this->_BGLayer[GPULayerID_BG1].type = BGType_Invalid;
this->_BGLayer[GPULayerID_BG2].type = BGType_Invalid;
this->_BGLayer[GPULayerID_BG3].type = BGType_Invalid;
this->_BGLayer[GPULayerID_BG0].priority = 0;
this->_BGLayer[GPULayerID_BG1].priority = 0;
this->_BGLayer[GPULayerID_BG2].priority = 0;
this->_BGLayer[GPULayerID_BG3].priority = 0;
this->_BGLayer[GPULayerID_BG0].isVisible = false;
this->_BGLayer[GPULayerID_BG1].isVisible = false;
this->_BGLayer[GPULayerID_BG2].isVisible = false;
this->_BGLayer[GPULayerID_BG3].isVisible = false;
this->_BGLayer[GPULayerID_BG0].isMosaic = false;
this->_BGLayer[GPULayerID_BG1].isMosaic = false;
this->_BGLayer[GPULayerID_BG2].isMosaic = false;
this->_BGLayer[GPULayerID_BG3].isMosaic = false;
this->_BGLayer[GPULayerID_BG0].isDisplayWrapped = false;
this->_BGLayer[GPULayerID_BG1].isDisplayWrapped = false;
this->_BGLayer[GPULayerID_BG2].isDisplayWrapped = false;
this->_BGLayer[GPULayerID_BG3].isDisplayWrapped = false;
this->_BGLayer[GPULayerID_BG0].extPaletteSlot = GPULayerID_BG0;
this->_BGLayer[GPULayerID_BG1].extPaletteSlot = GPULayerID_BG1;
this->_BGLayer[GPULayerID_BG0].extPalette = (u16 **)&MMU.ExtPal[this->_engineID][GPULayerID_BG0];
this->_BGLayer[GPULayerID_BG1].extPalette = (u16 **)&MMU.ExtPal[this->_engineID][GPULayerID_BG1];
this->_BGLayer[GPULayerID_BG2].extPalette = (u16 **)&MMU.ExtPal[this->_engineID][GPULayerID_BG2];
this->_BGLayer[GPULayerID_BG3].extPalette = (u16 **)&MMU.ExtPal[this->_engineID][GPULayerID_BG3];
this->_needUpdateWINH[0] = true;
this->_needUpdateWINH[1] = true;
this->vramBlockOBJIndex = VRAM_NO_3D_USAGE;
this->vramBlockOBJAddress = this->_sprMem;
this->nativeLineRenderCount = GPU_FRAMEBUFFER_NATIVE_HEIGHT;
this->nativeLineOutputCount = GPU_FRAMEBUFFER_NATIVE_HEIGHT;
for (size_t l = 0; l < GPU_FRAMEBUFFER_NATIVE_HEIGHT; l++)
{
this->isLineRenderNative[l] = true;
this->isLineOutputNative[l] = true;
}
GPUEngineRenderState &renderState = this->_currentRenderState;
renderState.displayOutputMode = GPUDisplayMode_Off;
renderState.selectedLayerID = GPULayerID_BG0;
renderState.selectedBGLayer = &this->_BGLayer[GPULayerID_BG0];
renderState.backdropColor16 = LE_TO_LOCAL_16(this->_paletteBG[0]) & 0x7FFF;
renderState.colorEffect = (ColorEffect)this->_IORegisterMap->BLDCNT.ColorEffect;
renderState.blendEVA = 0;
renderState.blendEVB = 0;
renderState.blendEVY = 0;
renderState.masterBrightnessMode = GPUMasterBrightMode_Disable;
renderState.masterBrightnessIntensity = 0;
renderState.masterBrightnessIsFullIntensity = false;
renderState.masterBrightnessIsMaxOrMin = true;
renderState.blendTable555 = (TBlendTable *)&GPUEngineBase::_blendTable555[renderState.blendEVA][renderState.blendEVB][0][0];
renderState.brightnessUpTable555 = &GPUEngineBase::_brightnessUpTable555[renderState.blendEVY][0];
renderState.brightnessUpTable666 = &GPUEngineBase::_brightnessUpTable666[renderState.blendEVY][0];
renderState.brightnessUpTable888 = &GPUEngineBase::_brightnessUpTable888[renderState.blendEVY][0];
renderState.brightnessDownTable555 = &GPUEngineBase::_brightnessDownTable555[renderState.blendEVY][0];
renderState.brightnessDownTable666 = &GPUEngineBase::_brightnessDownTable666[renderState.blendEVY][0];
renderState.brightnessDownTable888 = &GPUEngineBase::_brightnessDownTable888[renderState.blendEVY][0];
renderState.srcBlendEnable[GPULayerID_BG0] = false;
renderState.srcBlendEnable[GPULayerID_BG1] = false;
renderState.srcBlendEnable[GPULayerID_BG2] = false;
renderState.srcBlendEnable[GPULayerID_BG3] = false;
renderState.srcBlendEnable[GPULayerID_OBJ] = false;
renderState.srcBlendEnable[GPULayerID_Backdrop] = false;
renderState.dstBlendEnable[GPULayerID_BG0] = false;
renderState.dstBlendEnable[GPULayerID_BG1] = false;
renderState.dstBlendEnable[GPULayerID_BG2] = false;
renderState.dstBlendEnable[GPULayerID_BG3] = false;
renderState.dstBlendEnable[GPULayerID_OBJ] = false;
renderState.dstBlendEnable[GPULayerID_Backdrop] = false;
renderState.dstAnyBlendEnable = false;
#ifdef ENABLE_SSE2
renderState.srcBlendEnable_SSE2[GPULayerID_BG0] = _mm_setzero_si128();
renderState.srcBlendEnable_SSE2[GPULayerID_BG1] = _mm_setzero_si128();
renderState.srcBlendEnable_SSE2[GPULayerID_BG2] = _mm_setzero_si128();
renderState.srcBlendEnable_SSE2[GPULayerID_BG3] = _mm_setzero_si128();
renderState.srcBlendEnable_SSE2[GPULayerID_OBJ] = _mm_setzero_si128();
renderState.srcBlendEnable_SSE2[GPULayerID_Backdrop] = _mm_setzero_si128();
#ifdef ENABLE_SSSE3
renderState.dstBlendEnable_SSSE3 = _mm_setzero_si128();
#else
renderState.dstBlendEnable_SSE2[GPULayerID_BG0] = _mm_setzero_si128();
renderState.dstBlendEnable_SSE2[GPULayerID_BG1] = _mm_setzero_si128();
renderState.dstBlendEnable_SSE2[GPULayerID_BG2] = _mm_setzero_si128();
renderState.dstBlendEnable_SSE2[GPULayerID_BG3] = _mm_setzero_si128();
renderState.dstBlendEnable_SSE2[GPULayerID_OBJ] = _mm_setzero_si128();
renderState.dstBlendEnable_SSE2[GPULayerID_Backdrop] = _mm_setzero_si128();
#endif
#endif
renderState.WIN0_enable[GPULayerID_BG0] = 0;
renderState.WIN0_enable[GPULayerID_BG1] = 0;
renderState.WIN0_enable[GPULayerID_BG2] = 0;
renderState.WIN0_enable[GPULayerID_BG3] = 0;
renderState.WIN0_enable[GPULayerID_OBJ] = 0;
renderState.WIN0_enable[WINDOWCONTROL_EFFECTFLAG] = 0;
renderState.WIN1_enable[GPULayerID_BG0] = 0;
renderState.WIN1_enable[GPULayerID_BG1] = 0;
renderState.WIN1_enable[GPULayerID_BG2] = 0;
renderState.WIN1_enable[GPULayerID_BG3] = 0;
renderState.WIN1_enable[GPULayerID_OBJ] = 0;
renderState.WIN1_enable[WINDOWCONTROL_EFFECTFLAG] = 0;
renderState.WINOUT_enable[GPULayerID_BG0] = 0;
renderState.WINOUT_enable[GPULayerID_BG1] = 0;
renderState.WINOUT_enable[GPULayerID_BG2] = 0;
renderState.WINOUT_enable[GPULayerID_BG3] = 0;
renderState.WINOUT_enable[GPULayerID_OBJ] = 0;
renderState.WINOUT_enable[WINDOWCONTROL_EFFECTFLAG] = 0;
renderState.WINOBJ_enable[GPULayerID_BG0] = 0;
renderState.WINOBJ_enable[GPULayerID_BG1] = 0;
renderState.WINOBJ_enable[GPULayerID_BG2] = 0;
renderState.WINOBJ_enable[GPULayerID_BG3] = 0;
renderState.WINOBJ_enable[GPULayerID_OBJ] = 0;
renderState.WINOBJ_enable[WINDOWCONTROL_EFFECTFLAG] = 0;
#if defined(ENABLE_SSE2)
renderState.WIN0_enable_SSE2[GPULayerID_BG0] = _mm_setzero_si128();
renderState.WIN0_enable_SSE2[GPULayerID_BG1] = _mm_setzero_si128();
renderState.WIN0_enable_SSE2[GPULayerID_BG2] = _mm_setzero_si128();
renderState.WIN0_enable_SSE2[GPULayerID_BG3] = _mm_setzero_si128();
renderState.WIN0_enable_SSE2[GPULayerID_OBJ] = _mm_setzero_si128();
renderState.WIN0_enable_SSE2[WINDOWCONTROL_EFFECTFLAG] = _mm_setzero_si128();
renderState.WIN1_enable_SSE2[GPULayerID_BG0] = _mm_setzero_si128();
renderState.WIN1_enable_SSE2[GPULayerID_BG1] = _mm_setzero_si128();
renderState.WIN1_enable_SSE2[GPULayerID_BG2] = _mm_setzero_si128();
renderState.WIN1_enable_SSE2[GPULayerID_BG3] = _mm_setzero_si128();
renderState.WIN1_enable_SSE2[GPULayerID_OBJ] = _mm_setzero_si128();
renderState.WIN1_enable_SSE2[WINDOWCONTROL_EFFECTFLAG] = _mm_setzero_si128();
renderState.WINOUT_enable_SSE2[GPULayerID_BG0] = _mm_setzero_si128();
renderState.WINOUT_enable_SSE2[GPULayerID_BG1] = _mm_setzero_si128();
renderState.WINOUT_enable_SSE2[GPULayerID_BG2] = _mm_setzero_si128();
renderState.WINOUT_enable_SSE2[GPULayerID_BG3] = _mm_setzero_si128();
renderState.WINOUT_enable_SSE2[GPULayerID_OBJ] = _mm_setzero_si128();
renderState.WINOUT_enable_SSE2[WINDOWCONTROL_EFFECTFLAG] = _mm_setzero_si128();
renderState.WINOBJ_enable_SSE2[GPULayerID_BG0] = _mm_setzero_si128();
renderState.WINOBJ_enable_SSE2[GPULayerID_BG1] = _mm_setzero_si128();
renderState.WINOBJ_enable_SSE2[GPULayerID_BG2] = _mm_setzero_si128();
renderState.WINOBJ_enable_SSE2[GPULayerID_BG3] = _mm_setzero_si128();
renderState.WINOBJ_enable_SSE2[GPULayerID_OBJ] = _mm_setzero_si128();
renderState.WINOBJ_enable_SSE2[WINDOWCONTROL_EFFECTFLAG] = _mm_setzero_si128();
#endif
renderState.WIN0_ENABLED = false;
renderState.WIN1_ENABLED = false;
renderState.WINOBJ_ENABLED = false;
renderState.isAnyWindowEnabled = false;
renderState.mosaicWidthBG = this->_mosaicLookup.table[0];
renderState.mosaicHeightBG = this->_mosaicLookup.table[0];
renderState.mosaicWidthOBJ = this->_mosaicLookup.table[0];
renderState.mosaicHeightOBJ = this->_mosaicLookup.table[0];
renderState.isBGMosaicSet = false;
renderState.isOBJMosaicSet = false;
renderState.spriteRenderMode = SpriteRenderMode_Sprite1D;
renderState.spriteBoundary = 0;
renderState.spriteBMPBoundary = 0;
this->savedBG2X.value = 0;
this->savedBG2Y.value = 0;
this->savedBG3X.value = 0;
this->savedBG3Y.value = 0;
this->renderedWidth = GPU_FRAMEBUFFER_NATIVE_WIDTH;
this->renderedHeight = GPU_FRAMEBUFFER_NATIVE_HEIGHT;
this->renderedBuffer = this->nativeBuffer;
for (size_t line = 0; line < GPU_FRAMEBUFFER_NATIVE_HEIGHT; line++)
{
this->_currentCompositorInfo[line].renderState = renderState;
}
}
void GPUEngineBase::Reset()
{
this->_Reset_Base();
}
void GPUEngineBase::_ResortBGLayers()
{
const IOREG_DISPCNT &DISPCNT = this->_IORegisterMap->DISPCNT;
int i, prio;
itemsForPriority_t *item;
// we don't need to check for windows here...
// if we tick boxes, invisible layers become invisible & vice versa
#define OP ^ !
// if we untick boxes, layers become invisible
//#define OP &&
this->_enableLayer[GPULayerID_BG0] = CommonSettings.dispLayers[this->_engineID][GPULayerID_BG0] OP(this->_BGLayer[GPULayerID_BG0].isVisible);
this->_enableLayer[GPULayerID_BG1] = CommonSettings.dispLayers[this->_engineID][GPULayerID_BG1] OP(this->_BGLayer[GPULayerID_BG1].isVisible);
this->_enableLayer[GPULayerID_BG2] = CommonSettings.dispLayers[this->_engineID][GPULayerID_BG2] OP(this->_BGLayer[GPULayerID_BG2].isVisible);
this->_enableLayer[GPULayerID_BG3] = CommonSettings.dispLayers[this->_engineID][GPULayerID_BG3] OP(this->_BGLayer[GPULayerID_BG3].isVisible);
this->_enableLayer[GPULayerID_OBJ] = CommonSettings.dispLayers[this->_engineID][GPULayerID_OBJ] OP(DISPCNT.OBJ_Enable);
this->_isAnyBGLayerEnabled = this->_enableLayer[GPULayerID_BG0] || this->_enableLayer[GPULayerID_BG1] || this->_enableLayer[GPULayerID_BG2] || this->_enableLayer[GPULayerID_BG3];
// KISS ! lower priority first, if same then lower num
for (i = 0; i < NB_PRIORITIES; i++)
{
item = &(this->_itemsForPriority[i]);
item->nbBGs = 0;
item->nbPixelsX = 0;
}
for (i = NB_BG; i > 0; )
{
i--;
if (!this->_enableLayer[i]) continue;
prio = this->_BGLayer[i].priority;
item = &(this->_itemsForPriority[prio]);
item->BGs[item->nbBGs]=i;
item->nbBGs++;
}
#if 0
//debug
for (i = 0; i < NB_PRIORITIES; i++)
{
item = &(this->_itemsForPriority[i]);
printf("%d : ", i);
for (j=0; j<NB_PRIORITIES; j++)
{
if (j < item->nbBGs)
printf("BG%d ", item->BGs[j]);
else
printf("... ", item->BGs[j]);
}
}
printf("\n");
#endif
}
FORCEINLINE u16 GPUEngineBase::_ColorEffectBlend(const u16 colA, const u16 colB, const u16 blendEVA, const u16 blendEVB)
{
u16 ra = colA & 0x001F;
u16 ga = (colA >> 5) & 0x001F;
u16 ba = (colA >> 10) & 0x001F;
u16 rb = colB & 0x001F;
u16 gb = (colB >> 5) & 0x001F;
u16 bb = (colB >> 10) & 0x001F;
ra = ( (ra * blendEVA) + (rb * blendEVB) ) / 16;
ga = ( (ga * blendEVA) + (gb * blendEVB) ) / 16;
ba = ( (ba * blendEVA) + (bb * blendEVB) ) / 16;
ra = (ra > 31) ? 31 : ra;
ga = (ga > 31) ? 31 : ga;
ba = (ba > 31) ? 31 : ba;
return ra | (ga << 5) | (ba << 10);
}
template <NDSColorFormat COLORFORMAT>
FORCEINLINE FragmentColor GPUEngineBase::_ColorEffectBlend(const FragmentColor colA, const FragmentColor colB, const u16 blendEVA, const u16 blendEVB)
{
FragmentColor outColor;
u16 r16 = ( (colA.r * blendEVA) + (colB.r * blendEVB) ) / 16;
u16 g16 = ( (colA.g * blendEVA) + (colB.g * blendEVB) ) / 16;
u16 b16 = ( (colA.b * blendEVA) + (colB.b * blendEVB) ) / 16;
if (COLORFORMAT == NDSColorFormat_BGR666_Rev)
{
outColor.r = (r16 > 63) ? 63 : r16;
outColor.g = (g16 > 63) ? 63 : g16;
outColor.b = (b16 > 63) ? 63 : b16;
}
else if (COLORFORMAT == NDSColorFormat_BGR888_Rev)
{
outColor.r = (r16 > 255) ? 255 : r16;
outColor.g = (g16 > 255) ? 255 : g16;
outColor.b = (b16 > 255) ? 255 : b16;
}
outColor.a = 0;
return outColor;
}
FORCEINLINE u16 GPUEngineBase::_ColorEffectBlend(const u16 colA, const u16 colB, const TBlendTable *blendTable)
{
const u8 r = (*blendTable)[ colA & 0x1F][ colB & 0x1F];
const u8 g = (*blendTable)[(colA >> 5) & 0x1F][(colB >> 5) & 0x1F];
const u8 b = (*blendTable)[(colA >> 10) & 0x1F][(colB >> 10) & 0x1F];
return r | (g << 5) | (b << 10);
}
FORCEINLINE u16 GPUEngineBase::_ColorEffectBlend3D(const FragmentColor colA, const u16 colB)
{
const u16 alpha = colA.a + 1;
COLOR c2;
COLOR cfinal;
c2.val = colB;
cfinal.bits.red = ((colA.r * alpha) + ((c2.bits.red << 1) * (32 - alpha))) >> 6;
cfinal.bits.green = ((colA.g * alpha) + ((c2.bits.green << 1) * (32 - alpha))) >> 6;
cfinal.bits.blue = ((colA.b * alpha) + ((c2.bits.blue << 1) * (32 - alpha))) >> 6;
cfinal.bits.alpha = 0;
return cfinal.val;
}
template <NDSColorFormat COLORFORMATB>
FORCEINLINE FragmentColor GPUEngineBase::_ColorEffectBlend3D(const FragmentColor colA, const FragmentColor colB)
{
FragmentColor blendedColor;
const u16 alpha = colA.a + 1;
if (COLORFORMATB == NDSColorFormat_BGR666_Rev)
{
blendedColor.r = ((colA.r * alpha) + (colB.r * (32 - alpha))) >> 5;
blendedColor.g = ((colA.g * alpha) + (colB.g * (32 - alpha))) >> 5;
blendedColor.b = ((colA.b * alpha) + (colB.b * (32 - alpha))) >> 5;
}
else if (COLORFORMATB == NDSColorFormat_BGR888_Rev)
{
blendedColor.r = ((colA.r * alpha) + (colB.r * (256 - alpha))) >> 8;
blendedColor.g = ((colA.g * alpha) + (colB.g * (256 - alpha))) >> 8;
blendedColor.b = ((colA.b * alpha) + (colB.b * (256 - alpha))) >> 8;
}
blendedColor.a = 0;
return blendedColor;
}
FORCEINLINE u16 GPUEngineBase::_ColorEffectIncreaseBrightness(const u16 col, const u16 blendEVY)
{
u16 r = col & 0x001F;
u16 g = (col >> 5) & 0x001F;
u16 b = (col >> 10) & 0x001F;
r = (r + ((31 - r) * blendEVY / 16));
g = (g + ((31 - g) * blendEVY / 16));
b = (b + ((31 - b) * blendEVY / 16));
return r | (g << 5) | (b << 10);
}
template <NDSColorFormat COLORFORMAT>
FORCEINLINE FragmentColor GPUEngineBase::_ColorEffectIncreaseBrightness(const FragmentColor col, const u16 blendEVY)
{
FragmentColor newColor;
newColor.color = 0;
u32 r = col.r;
u32 g = col.g;
u32 b = col.b;
if (COLORFORMAT == NDSColorFormat_BGR666_Rev)
{
newColor.r = (r + ((63 - r) * blendEVY / 16));
newColor.g = (g + ((63 - g) * blendEVY / 16));
newColor.b = (b + ((63 - b) * blendEVY / 16));
}
else if (COLORFORMAT == NDSColorFormat_BGR888_Rev)
{
newColor.r = (r + ((255 - r) * blendEVY / 16));
newColor.g = (g + ((255 - g) * blendEVY / 16));
newColor.b = (b + ((255 - b) * blendEVY / 16));
}
return newColor;
}
FORCEINLINE u16 GPUEngineBase::_ColorEffectDecreaseBrightness(const u16 col, const u16 blendEVY)
{
u16 r = col & 0x001F;
u16 g = (col >> 5) & 0x001F;
u16 b = (col >> 10) & 0x001F;
r = (r - (r * blendEVY / 16));
g = (g - (g * blendEVY / 16));
b = (b - (b * blendEVY / 16));
return r | (g << 5) | (b << 10);
}
FORCEINLINE FragmentColor GPUEngineBase::_ColorEffectDecreaseBrightness(const FragmentColor col, const u16 blendEVY)
{
FragmentColor newColor;
newColor.color = 0;
u32 r = col.r;
u32 g = col.g;
u32 b = col.b;
newColor.r = (r - (r * blendEVY / 16));
newColor.g = (g - (g * blendEVY / 16));
newColor.b = (b - (b * blendEVY / 16));
return newColor;
}
#ifdef ENABLE_SSE2
template <NDSColorFormat COLORFORMAT>
FORCEINLINE __m128i GPUEngineBase::_ColorEffectIncreaseBrightness(const __m128i &col, const __m128i &blendEVY)
{
if (COLORFORMAT == NDSColorFormat_BGR555_Rev)
{
__m128i r_vec128 = _mm_and_si128( col, _mm_set1_epi16(0x001F) );
__m128i g_vec128 = _mm_and_si128( _mm_srli_epi16(col, 5), _mm_set1_epi16(0x001F) );
__m128i b_vec128 = _mm_and_si128( _mm_srli_epi16(col, 10), _mm_set1_epi16(0x001F) );
r_vec128 = _mm_add_epi16( r_vec128, _mm_srli_epi16(_mm_mullo_epi16(_mm_sub_epi16(_mm_set1_epi16(31), r_vec128), blendEVY), 4) );
g_vec128 = _mm_add_epi16( g_vec128, _mm_srli_epi16(_mm_mullo_epi16(_mm_sub_epi16(_mm_set1_epi16(31), g_vec128), blendEVY), 4) );
b_vec128 = _mm_add_epi16( b_vec128, _mm_srli_epi16(_mm_mullo_epi16(_mm_sub_epi16(_mm_set1_epi16(31), b_vec128), blendEVY), 4) );
return _mm_or_si128(r_vec128, _mm_or_si128( _mm_slli_epi16(g_vec128, 5), _mm_slli_epi16(b_vec128, 10)) );
}
else
{
__m128i rgbLo = _mm_unpacklo_epi8(col, _mm_setzero_si128());
__m128i rgbHi = _mm_unpackhi_epi8(col, _mm_setzero_si128());
rgbLo = _mm_add_epi16( rgbLo, _mm_srli_epi16(_mm_mullo_epi16(_mm_sub_epi16(_mm_set1_epi16((COLORFORMAT == NDSColorFormat_BGR666_Rev) ? 63 : 255), rgbLo), blendEVY), 4) );
rgbHi = _mm_add_epi16( rgbHi, _mm_srli_epi16(_mm_mullo_epi16(_mm_sub_epi16(_mm_set1_epi16((COLORFORMAT == NDSColorFormat_BGR666_Rev) ? 63 : 255), rgbHi), blendEVY), 4) );
return _mm_and_si128( _mm_packus_epi16(rgbLo, rgbHi), _mm_set1_epi32(0x00FFFFFF) );
}
}
template <NDSColorFormat COLORFORMAT>
FORCEINLINE __m128i GPUEngineBase::_ColorEffectDecreaseBrightness(const __m128i &col, const __m128i &blendEVY)
{
if (COLORFORMAT == NDSColorFormat_BGR555_Rev)
{
__m128i r_vec128 = _mm_and_si128( col, _mm_set1_epi16(0x001F) );
__m128i g_vec128 = _mm_and_si128( _mm_srli_epi16(col, 5), _mm_set1_epi16(0x001F) );
__m128i b_vec128 = _mm_and_si128( _mm_srli_epi16(col, 10), _mm_set1_epi16(0x001F) );
r_vec128 = _mm_sub_epi16( r_vec128, _mm_srli_epi16(_mm_mullo_epi16(r_vec128, blendEVY), 4) );
g_vec128 = _mm_sub_epi16( g_vec128, _mm_srli_epi16(_mm_mullo_epi16(g_vec128, blendEVY), 4) );
b_vec128 = _mm_sub_epi16( b_vec128, _mm_srli_epi16(_mm_mullo_epi16(b_vec128, blendEVY), 4) );
return _mm_or_si128(r_vec128, _mm_or_si128( _mm_slli_epi16(g_vec128, 5), _mm_slli_epi16(b_vec128, 10)) );
}
else
{
__m128i rgbLo = _mm_unpacklo_epi8(col, _mm_setzero_si128());
__m128i rgbHi = _mm_unpackhi_epi8(col, _mm_setzero_si128());
rgbLo = _mm_sub_epi16( rgbLo, _mm_srli_epi16(_mm_mullo_epi16(rgbLo, blendEVY), 4) );
rgbHi = _mm_sub_epi16( rgbHi, _mm_srli_epi16(_mm_mullo_epi16(rgbHi, blendEVY), 4) );
return _mm_and_si128( _mm_packus_epi16(rgbLo, rgbHi), _mm_set1_epi32(0x00FFFFFF) );
}
}
template <NDSColorFormat COLORFORMAT>
FORCEINLINE __m128i GPUEngineBase::_ColorEffectBlend(const __m128i &colA, const __m128i &colB, const __m128i &blendEVA, const __m128i &blendEVB)
{
#ifdef ENABLE_SSSE3
__m128i blendAB = _mm_or_si128(blendEVA, _mm_slli_epi16(blendEVB, 8));
#endif
if (COLORFORMAT == NDSColorFormat_BGR555_Rev)
{
__m128i ra;
__m128i ga;
__m128i ba;
__m128i colorBitMask = _mm_set1_epi16(0x001F);
#ifdef ENABLE_SSSE3
ra = _mm_or_si128( _mm_and_si128( colA, colorBitMask), _mm_and_si128(_mm_slli_epi16(colB, 8), _mm_set1_epi16(0x1F00)) );
ga = _mm_or_si128( _mm_and_si128(_mm_srli_epi16(colA, 5), colorBitMask), _mm_and_si128(_mm_slli_epi16(colB, 3), _mm_set1_epi16(0x1F00)) );
ba = _mm_or_si128( _mm_and_si128(_mm_srli_epi16(colA, 10), colorBitMask), _mm_and_si128(_mm_srli_epi16(colB, 2), _mm_set1_epi16(0x1F00)) );
ra = _mm_maddubs_epi16(ra, blendAB);
ga = _mm_maddubs_epi16(ga, blendAB);
ba = _mm_maddubs_epi16(ba, blendAB);
#else
ra = _mm_and_si128( colA, colorBitMask);
ga = _mm_and_si128(_mm_srli_epi16(colA, 5), colorBitMask);
ba = _mm_and_si128(_mm_srli_epi16(colA, 10), colorBitMask);
__m128i rb = _mm_and_si128( colB, colorBitMask);
__m128i gb = _mm_and_si128(_mm_srli_epi16(colB, 5), colorBitMask);
__m128i bb = _mm_and_si128(_mm_srli_epi16(colB, 10), colorBitMask);
ra = _mm_add_epi16( _mm_mullo_epi16(ra, blendEVA), _mm_mullo_epi16(rb, blendEVB) );
ga = _mm_add_epi16( _mm_mullo_epi16(ga, blendEVA), _mm_mullo_epi16(gb, blendEVB) );
ba = _mm_add_epi16( _mm_mullo_epi16(ba, blendEVA), _mm_mullo_epi16(bb, blendEVB) );
#endif
ra = _mm_srli_epi16(ra, 4);
ga = _mm_srli_epi16(ga, 4);
ba = _mm_srli_epi16(ba, 4);
ra = _mm_min_epi16(ra, colorBitMask);
ga = _mm_min_epi16(ga, colorBitMask);
ba = _mm_min_epi16(ba, colorBitMask);
return _mm_or_si128(ra, _mm_or_si128( _mm_slli_epi16(ga, 5), _mm_slli_epi16(ba, 10)) );
}
else
{
__m128i outColorLo;
__m128i outColorHi;
__m128i outColor;
#ifdef ENABLE_SSSE3
outColorLo = _mm_unpacklo_epi8(colA, colB);
outColorHi = _mm_unpackhi_epi8(colA, colB);
outColorLo = _mm_maddubs_epi16(outColorLo, blendAB);
outColorHi = _mm_maddubs_epi16(outColorHi, blendAB);
#else
__m128i colALo = _mm_unpacklo_epi8(colA, _mm_setzero_si128());
__m128i colAHi = _mm_unpackhi_epi8(colA, _mm_setzero_si128());
__m128i colBLo = _mm_unpacklo_epi8(colB, _mm_setzero_si128());
__m128i colBHi = _mm_unpackhi_epi8(colB, _mm_setzero_si128());
outColorLo = _mm_add_epi16( _mm_mullo_epi16(colALo, blendEVA), _mm_mullo_epi16(colBLo, blendEVB) );
outColorHi = _mm_add_epi16( _mm_mullo_epi16(colAHi, blendEVA), _mm_mullo_epi16(colBHi, blendEVB) );
#endif
outColorLo = _mm_srli_epi16(outColorLo, 4);
outColorHi = _mm_srli_epi16(outColorHi, 4);
outColor = _mm_packus_epi16(outColorLo, outColorHi);
// When the color format is 888, the packuswb instruction will naturally clamp
// the color component values to 255. However, when the color format is 666, the
// color component values must be clamped to 63. In this case, we must call pminub
// to do the clamp.
if (COLORFORMAT == NDSColorFormat_BGR666_Rev)
{
outColor = _mm_min_epu8(outColor, _mm_set1_epi8(63));
}
outColor = _mm_and_si128(outColor, _mm_set1_epi32(0x00FFFFFF));
return outColor;
}
}
template <NDSColorFormat COLORFORMATB>
FORCEINLINE __m128i GPUEngineBase::_ColorEffectBlend3D(const __m128i &colA_Lo, const __m128i &colA_Hi, const __m128i &colB)
{
if (COLORFORMATB == NDSColorFormat_BGR555_Rev)
{
// If the color format of B is 555, then the colA_Hi parameter is required.
// The color format of A is assumed to be RGB666.