-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfx_psp.cpp
1877 lines (1574 loc) · 55.2 KB
/
gfx_psp.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
/*
* Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
*
* (c) Copyright 1996 - 2001 Gary Henderson ([email protected]) and
* Jerremy Koot ([email protected])
*
* Super FX C emulator code
* (c) Copyright 1997 - 1999 Ivar ([email protected]) and
* Gary Henderson.
* Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
*
* DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
* C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
* C4 C code (c) Copyright 2001 Gary Henderson ([email protected]).
*
* DOS port code contains the works of other authors. See headers in
* individual files.
*
* Snes9x homepage: http://www.snes9x.com
*
* Permission to use, copy, modify and distribute Snes9x in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* Snes9x is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Snes9x or software derived from Snes9x.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so everyone can benefit from the modifications
* in future versions.
*
* Super NES and Super Nintendo Entertainment System are trademarks of
* Nintendo Co., Limited and its subsidiary companies.
*/
#include "snes9x.h"
#include "memmap.h"
#include "ppu.h"
#include "cpuexec.h"
#include "display.h"
#include "gfx.h"
#include "apu.h"
#include "cheats.h"
#include "tile_psp.h"
#define M7 19
#define M8 19
void ComputeClipWindows ();
extern int os9x_BG0,os9x_BG1,os9x_BG2,os9x_BG3,os9x_OBJ,os9x_easy,os9x_fastsprite;
extern int os9x_render;
int add_sub_mode;
int last_palette;
int current_bitshift;
int render_timestamp;
//extern u16 __attribute__((aligned(16))) clut256[256*3];
extern u16* clut256;
u16* clut;
u32 fixedcol16;
u32 fixedcol;
int rendering_beware_fix16;
// for OBJ & BGS
struct Vertex *vertices[3];
struct Vertex *vertices_ptr[3];
struct Vertex *vertices_end[3];
// for Blending
struct Vertex *_vertices;
struct Vertex *_vertices_ptr;
struct VertexCol *_verticesCol;
struct VertexCol *_verticesCol_ptr;
struct VertexCol *_verticesCol_end;
s32 realZ1,realZ2;
extern void S9xSetupOBJ(void);
extern uint8 BitShifts[8][4];
extern uint8 TileShifts[8][4];
extern uint8 PaletteShifts[8][4];
extern uint8 PaletteMasks[8][4];
extern uint8 Depths[8][4];
extern uint8 BGSizes [2];
extern NormalTileRendererPSP DrawTilePtr;
extern ClippedTileRendererPSP DrawClippedTilePtr;
extern NormalTileRendererPSP DrawHiResTilePtr;
extern ClippedTileRendererPSP DrawHiResClippedTilePtr;
extern LargePixelRendererPSP DrawLargePixelPtr;
extern struct SBG BG;
extern struct SLineData LineData[240];
extern struct SLineMatrixData LineMatrixData [240];
extern uint8 Mode7Depths [2];
#define ON_MAIN(N) \
(GFX.r212c & (1 << (N)) && \
!(PPU.BG_Forced & (1 << (N))))
#define SUB_OR_ADD(N) \
(GFX.r2131 & (1 << (N)))
#define ON_SUB(N) \
((GFX.r2130 & 0x30) != 0x30 && \
(GFX.r2130 & 2) && \
(GFX.r212d & (1 << N)) && \
!(PPU.BG_Forced & (1 << (N))))
#define ANYTHING_ON_SUB \
((GFX.r2130 & 0x30) != 0x30 && \
(GFX.r2130 & 2) && \
(GFX.r212d & 0x1f))
#define ADD_OR_SUB_ON_ANYTHING \
(GFX.r2131 & 0x3f)
#define BLACK BUILD_PIXEL(0,0,0)
void pspDrawTile16 (uint32 Tile, s32 x,s32 y, uint32 StartLine,
uint32 LineCount);
void pspDrawClippedTile16 (uint32 Tile, s32 x,s32 y,
uint32 StartPixel, uint32 Width,
uint32 StartLine, uint32 LineCount);
void pspDrawHiResTile16 (uint32 Tile, s32 x,s32 y, uint32 StartLine,
uint32 LineCount);
void pspDrawHiResClippedTile16 (uint32 Tile, s32 x,s32 y,
uint32 StartPixel, uint32 Width,
uint32 StartLine, uint32 LineCount);
void pspDrawLargePixel16 (uint32 Tile, s32 x,s32 y,
uint32 StartPixel, uint32 Pixels,
uint32 StartLine, uint32 LineCount);
inline void pspSelectAddSubMode() {
if (GFX.r2131 & 0x80) {
if (GFX.r2131 & 0x40) {
if (GFX.r2130 & 2) {
add_sub_mode=1;
//debug_log("tile sub 1_2");
} else {
add_sub_mode=2;
// Fixed colour substraction
//debug_log("Fixed colour substraction");
}
} else {
add_sub_mode=3;
//debug_log("tile sub");
}
} else {
if (GFX.r2131 & 0x40) {
if (GFX.r2130 & 2) {
add_sub_mode=4;
//debug_log("tile add1_2");
} else {
add_sub_mode=5;
// Fixed colour addition
//debug_log("Fixed colour addition");
}
} else {
add_sub_mode=6;
//debug_log("tile add");
}
}
}
void initRendering(){
sceGuStart(GU_DIRECT,list);
//rendering options
sceGuEnable(GU_DEPTH_TEST);
sceGuDepthFunc(GU_GREATER);
sceGuEnable(GU_ALPHA_TEST);
sceGuAlphaFunc(GU_EQUAL,0,0x1);
sceGuEnable(GU_SCISSOR_TEST);
sceGuEnable(GU_TEXTURE_2D);
sceGuTexFilter(GU_NEAREST,GU_NEAREST);
sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGBA);
sceGuTexScale(1.0f/512.0f,1.0f/512.0f);
sceGuTexOffset(0,0);
sceGuClutMode(GU_PSM_5551,0,255,0);
last_palette=0; //start in non directcolour mode, coz first things to be rendered are s
clut = (u16*)NO_CPU_CACHE(&clut256[0]);
memcpy(clut,&IPPU.ScreenColors[0],256*2);
if (rendering_beware_fix16)
for (int i=0;i<256;i++) {
if (clut[i]==fixedcol16) clut[i]^=1<<10; //swap blue 1st bit
}
clut[0]|=0x8000;
sceGuClutLoad(256/16,clut);
}
void endRendering(){
sceGuFinish();
sceGuSync(0,0);
}
void waitRendering() {
/*sceGuFinish();
sceGuSync(0,0);
sceGuStart(GU_DIRECT,list);
*/
}
void pspDrawFASTOBJS (bool8 OnMain, uint8 D, uint8 drawmode)
{
//uint32 O;
s32 Xs,Ys;
int i;
uint32 BaseTile, Tile;
BG.BitShift = 4;
BG.TileShift = 5;
BG.TileAddress = PPU.OBJNameBase;
BG.StartPalette = 128;
BG.PaletteShift = 4;
BG.PaletteMask = 7;
current_bitshift=TILE_4BIT;
BG.Buffer = tile_texture[TILE_4BIT];//IPPU.TileCache8 [TILE_4BIT];
BG.Buffered = IPPU.TileCached [TILE_4BIT];
BG.NameSelect = PPU.OBJNameSelect;
BG.DirectColourMode = false;
/********************************************************/
/****************PSP STUFF*******************************/
/********************************************************/
int num_vert=0;//8192;//0;
int I=0;
for (int S = GFX.OBJList [I++]; S >= 0; S = GFX.OBJList [I++]) {
int VPos = GFX.VPositions [S];
int Size = GFX.Sizes[S];
int TileInc = 1;
int Offset;
if (VPos + Size <= (int) GFX.StartY || VPos > (int) GFX.EndY) continue;
num_vert+=(Size/8)*(Size/8);
}
if (!(vertices[current_bitshift]=(struct Vertex*)sceGuGetMemory(num_vert*2* sizeof(struct Vertex)))){
debug_log("not enough vertices!");
return;
}
vertices_ptr[current_bitshift]=vertices[current_bitshift];
vertices_end[current_bitshift]=vertices_ptr[current_bitshift]+num_vert*2;
/********************************************************/
/********************************************************/
GFX.Z1 = (D + 2);
realZ1=(int)(GFX.Z1);
I=0;
for (int S = GFX.OBJList [I++]; S >= 0; S = GFX.OBJList [I++]) {
int VPos = GFX.VPositions [S];
int Size = GFX.Sizes[S];
int TileInc = 1;
int Offset;
if (VPos + Size <= (int) GFX.StartY || VPos > (int) GFX.EndY)
continue;
//drawing sub/added sprites => palette has to be 4-7
if (OnMain){
if ((drawmode==1) && (PPU.OBJ [S].Palette < 4) ) continue;
//drawing not sub/added sprites => palette has to be 4-7 or no add/sub in effect for OBJ
if ((drawmode==2) && (PPU.OBJ [S].Palette >= 4) && SUB_OR_ADD(4)) continue;
}
BaseTile = PPU.OBJ[S].Name | (PPU.OBJ[S].Palette << 10);
if (PPU.OBJ[S].HFlip) {
BaseTile += ((Size >> 3) - 1) | H_FLIP;
TileInc = -1;
}
if (PPU.OBJ[S].VFlip) BaseTile |= V_FLIP;
int clipcount = GFX.pCurrentClip->Count [4];
if (!clipcount) clipcount = 1;
GFX.Z2 = ((PPU.OBJ[S].Priority + 1) * 4 + D);
realZ2=(int)(GFX.Z2);//+I-1;
for (int clip = 0; clip < clipcount; clip++){
int Left;
int Right;
if (!GFX.pCurrentClip->Count [4]){
Left = 0;
Right = 256;
}else{
Left = GFX.pCurrentClip->Left [clip][4];
Right = GFX.pCurrentClip->Right [clip][4];
}
if (Right <= Left || PPU.OBJ[S].HPos + Size <= Left || PPU.OBJ[S].HPos >= Right) continue;
for (int Y = 0; Y < Size; Y += 8) {
if (VPos + Y + 7 >= (int) GFX.StartY && VPos + Y <= (int) GFX.EndY){
int StartLine;
int TileLine;
int LineCount;
int Last;
if ((StartLine = VPos + Y) < (int) GFX.StartY) {
StartLine = GFX.StartY - StartLine;
LineCount = 8 - StartLine;
}else{
StartLine = 0;
LineCount = 8;
}
if ((Last = VPos + Y + 7 - GFX.EndY) > 0)
if ((LineCount -= Last) <= 0) break;
TileLine = StartLine;// << 3;
Ys = (VPos + Y + StartLine);
if (!PPU.OBJ[S].VFlip) Tile = BaseTile + (Y << 1);
else Tile = BaseTile + ((Size - Y - 8) << 1);
int Middle = Size >> 3;
if (PPU.OBJ[S].HPos < Left) {
Tile += ((Left - PPU.OBJ[S].HPos) >> 3) * TileInc;
Middle -= (Left - PPU.OBJ[S].HPos) >> 3;
Xs = Left ;
if ((Offset = (Left - PPU.OBJ[S].HPos) & 7)) {
Xs -= Offset ;
int W = 8 - Offset;
int Width = Right - Left;
if (W > Width) W = Width;
pspDrawClippedTile16 (Tile, Xs,Ys, Offset, W,TileLine, LineCount);
if (W >= Width) continue;
Tile += TileInc;
Middle--;
Xs += 8 ;
}
} else Xs = PPU.OBJ[S].HPos ;
if (PPU.OBJ[S].HPos + Size >= Right) {
Middle -= ((PPU.OBJ[S].HPos + Size + 7) - Right) >> 3;
Offset = (Right - (PPU.OBJ[S].HPos + Size)) & 7;
}else Offset = 0;
for (int X = 0; X < Middle; X++, Xs += 8 , Tile += TileInc){
pspDrawTile16 (Tile, Xs,Ys, TileLine, LineCount);
}
if (Offset){
pspDrawClippedTile16 (Tile, Xs,Ys, 0, Offset,TileLine, LineCount);
}
}
}
}
}
if (vertices_ptr[current_bitshift]-vertices[current_bitshift]) {
sceGuTexImage(0,512,512,512,tile_texture[current_bitshift]);
sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,(vertices_ptr[current_bitshift]-vertices[current_bitshift]),0,vertices[current_bitshift]);
}
}
void pspDrawOBJS (bool8 OnMain, uint8 D, uint8 drawmode) {
//uint32 O;
s32 Xs,Ys;
int speZ;
uint32 BaseTile, Tile;
BG.BitShift = 4;
current_bitshift=TILE_4BIT;
BG.TileShift = 5;
BG.TileAddress = PPU.OBJNameBase;
BG.StartPalette = 128;
BG.PaletteShift = 4;
BG.PaletteMask = 7;
BG.Buffer = tile_texture[TILE_4BIT];//IPPU.TileCache8 [TILE_4BIT];
BG.Buffered = IPPU.TileCached [TILE_4BIT];
BG.NameSelect = PPU.OBJNameSelect;
BG.DirectColourMode = false;
/********************************************************/
/****************PSP STUFF*******************************/
/********************************************************/
int num_vert=0;//8192;//0;
int I=0;
for (int S = GFX.OBJList [I++]; S >= 0; S = GFX.OBJList [I++]) {
int VPos = GFX.VPositions [S];
int Size = GFX.Sizes[S];
if (VPos + Size <= (int) GFX.StartY || VPos > (int) GFX.EndY)
continue;
//drawing sub/added sprites => palette has to be 4-7
num_vert+=(Size/8)*(Size/8);
}
if (!(vertices[current_bitshift]=(struct Vertex*)sceGuGetMemory(num_vert*2* sizeof(struct Vertex)))){
debug_log("not enough vertices!");
return;
}
vertices_ptr[current_bitshift]=vertices[current_bitshift];
vertices_end[current_bitshift]=vertices_ptr[current_bitshift]+num_vert*2;
/********************************************************/
/********************************************************/
GFX.Z1 = (D + 2);
realZ1=(int)(GFX.Z1);
I=0;
for (int S = GFX.OBJList [I++]; S >= 0; S = GFX.OBJList [I++]) {
int VPos = GFX.VPositions [S];
int Size = GFX.Sizes[S];
int TileInc = 1;
int Offset;
if (VPos + Size <= (int) GFX.StartY || VPos > (int) GFX.EndY)
continue;
//drawing sub/added sprites => palette has to be 4-7
speZ=0;
if (OnMain){
if ((drawmode==1) && (PPU.OBJ [S].Palette < 4) ) speZ=1;
//drawing not sub/added sprites => palette has to be 4-7 or no add/sub in effect for OBJ
if ((drawmode==2) && (PPU.OBJ [S].Palette >= 4) && SUB_OR_ADD(4)) speZ=1;
}
BaseTile = PPU.OBJ[S].Name | (PPU.OBJ[S].Palette << 10);
if (PPU.OBJ[S].HFlip) {
BaseTile += ((Size >> 3) - 1) | H_FLIP;
TileInc = -1;
}
if (PPU.OBJ[S].VFlip) BaseTile |= V_FLIP;
int clipcount = GFX.pCurrentClip->Count [4];
if (!clipcount) clipcount = 1;
GFX.Z2 = ((PPU.OBJ[S].Priority + 1) * 4 + D);
if (!speZ) realZ2=(int)(GFX.Z2);//+I-1;
else realZ2=0;
for (int clip = 0; clip < clipcount; clip++){
int Left;
int Right;
if (!GFX.pCurrentClip->Count [4]){
Left = 0;
Right = 256;
}else{
Left = GFX.pCurrentClip->Left [clip][4];
Right = GFX.pCurrentClip->Right [clip][4];
}
if (Right <= Left || PPU.OBJ[S].HPos + Size <= Left || PPU.OBJ[S].HPos >= Right) continue;
for (int Y = 0; Y < Size; Y += 8) {
if (VPos + Y + 7 >= (int) GFX.StartY && VPos + Y <= (int) GFX.EndY) {
int StartLine;
int TileLine;
int LineCount;
int Last;
if ((StartLine = VPos + Y) < (int) GFX.StartY) {
StartLine = GFX.StartY - StartLine;
LineCount = 8 - StartLine;
} else {
StartLine = 0;
LineCount = 8;
}
if ((Last = VPos + Y + 7 - GFX.EndY) > 0)
if ((LineCount -= Last) <= 0) break;
TileLine = StartLine;// << 3;
Ys = (VPos + Y + StartLine);
if (!PPU.OBJ[S].VFlip) Tile = BaseTile + (Y << 1);
else Tile = BaseTile + ((Size - Y - 8) << 1);
int Middle = Size >> 3;
if (PPU.OBJ[S].HPos < Left) {
Tile += ((Left - PPU.OBJ[S].HPos) >> 3) * TileInc;
Middle -= (Left - PPU.OBJ[S].HPos) >> 3;
Xs = Left ;
if ((Offset = (Left - PPU.OBJ[S].HPos) & 7)) {
Xs -= Offset ;
int W = 8 - Offset;
int Width = Right - Left;
if (W > Width) W = Width;
pspDrawClippedTile16 (Tile, Xs,Ys, Offset, W,TileLine, LineCount);
if (W >= Width) continue;
Tile += TileInc;
Middle--;
Xs += 8 ;
}
} else Xs = PPU.OBJ[S].HPos ;
if (PPU.OBJ[S].HPos + Size >= Right) {
Middle -= ((PPU.OBJ[S].HPos + Size + 7) - Right) >> 3;
Offset = (Right - (PPU.OBJ[S].HPos + Size)) & 7;
}else Offset = 0;
for (int X = 0; X < Middle; X++, Xs += 8 , Tile += TileInc){
pspDrawTile16 (Tile, Xs,Ys, TileLine, LineCount);
}
if (Offset){
pspDrawClippedTile16 (Tile, Xs,Ys, 0, Offset,TileLine, LineCount);
}
}
}
}
}
/*{
int i=0;
char str[32];
tile_cache_t *p=tile_cache_first_free[TILE_4BIT];
while (p) {i++;p=p->next;}
sprintf(str,"cached %d free %d/%d",tile_cached[TILE_4BIT],i,i+tile_cached[TILE_4BIT]);
info(32,12,str);
}*/
if (vertices_ptr[current_bitshift]-vertices[current_bitshift]) {
sceGuEnable(GU_STENCIL_TEST);//stencil
sceGuClearStencil(0);
sceGuClear(GU_STENCIL_BUFFER_BIT);
sceGuStencilOp( GU_KEEP, GU_INCR, GU_INCR);
sceGuStencilFunc(GU_EQUAL,0,0xFFFFFFFF);
sceGuTexImage(0,512,512,512,tile_texture[current_bitshift]);
sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,(vertices_ptr[current_bitshift]-vertices[current_bitshift]),0,vertices[current_bitshift]);
}
sceGuDisable(GU_STENCIL_TEST);//stencil
}
void pspDrawBackgroundMosaic (uint32 BGMode, uint32 bg, uint8 Z1, uint8 Z2) {
uint32 Tile;
uint16 *SC0;
uint16 *SC1;
uint16 *SC2;
uint16 *SC3;
uint8 depths [2] = {Z1, Z2};
if (last_palette!=BG.DirectColourMode){
//DirectColourMode has changed, so we have to reload palette
waitRendering();
last_palette=BG.DirectColourMode;
clut = (u16*)NO_CPU_CACHE(&clut256[0]);
memcpy(clut,GFX.ScreenColors,256*2);
if (rendering_beware_fix16)
for (int i=0;i<256;i++) {
if (clut[i]==fixedcol16) clut[i]^=1<<10; //swap blue 1st bit
}
clut[0]|=0x8000;
sceGuClutLoad(256/16,clut);
}
//bg is 32x30 tile but needs more in case of clipping or scroll changing every line
int num_vert=((GFX.EndY-GFX.StartY+1)+2)*(256/PPU.Mosaic+6);
if (!(_verticesCol=(struct VertexCol*)sceGuGetMemory(num_vert*2* sizeof(struct VertexCol)))){
debug_log("not enough vertices!");
return;
}
_verticesCol_ptr=_verticesCol;
_verticesCol_end=_verticesCol_ptr+num_vert*2;
/***********************************************************/
/***********************************************************/
if (BGMode == 0) BG.StartPalette = bg << 5;
else BG.StartPalette = 0;
SC0 = (uint16 *) &VRAM[PPU.BG[bg].SCBase << 1];
if (PPU.BG[bg].SCSize & 1) SC1 = SC0 + 1024;
else SC1 = SC0;
if((SC1-(unsigned short*)VRAM)>0x10000) SC1-=0x10000;
if (PPU.BG[bg].SCSize & 2) SC2 = SC1 + 1024;
else SC2 = SC0;
if((SC2-(unsigned short*)VRAM)>0x10000) SC2-=0x10000;
if (PPU.BG[bg].SCSize & 1) SC3 = SC2 + 1024;
else SC3 = SC2;
if((SC3-(unsigned short*)VRAM)>0x10000) SC3-=0x10000;
uint32 Lines;
uint32 OffsetMask;
uint32 OffsetShift;
if (BG.TileSize == 16) {
OffsetMask = 0x3ff;
OffsetShift = 4;
} else {
OffsetMask = 0x1ff;
OffsetShift = 3;
}
for (uint32 Y = GFX.StartY; Y <= GFX.EndY; Y += Lines) {
uint32 VOffset = LineData [Y].BG[bg].VOffset;
uint32 HOffset = LineData [Y].BG[bg].HOffset;
uint32 MosaicOffset = Y % PPU.Mosaic;
for (Lines = 1; Lines < PPU.Mosaic - MosaicOffset; Lines++)
if ((VOffset != LineData [Y + Lines].BG[bg].VOffset) || (HOffset != LineData [Y + Lines].BG[bg].HOffset)) break;
uint32 MosaicLine = VOffset + Y - MosaicOffset;
if (Y + Lines > GFX.EndY) Lines = GFX.EndY + 1 - Y;
uint32 VirtAlign = (MosaicLine & 7);// << 3;
uint16 *b1;
uint16 *b2;
uint32 ScreenLine = MosaicLine >> OffsetShift;
uint32 Rem16 = MosaicLine & 15;
if (ScreenLine & 0x20) b1 = SC2, b2 = SC3;
else b1 = SC0, b2 = SC1;
b1 += (ScreenLine & 0x1f) << 5;
b2 += (ScreenLine & 0x1f) << 5;
uint16 *t;
uint32 Left = 0;
uint32 Right = 256;
uint32 ClipCount = GFX.pCurrentClip->Count [bg];
uint32 HPos = HOffset;
uint32 PixWidth = PPU.Mosaic;
if (!ClipCount) ClipCount = 1;
for (uint32 clip = 0; clip < ClipCount; clip++) {
if (GFX.pCurrentClip->Count [bg]) {
Left = GFX.pCurrentClip->Left [clip][bg];
Right = GFX.pCurrentClip->Right [clip][bg];
uint32 r = Left % PPU.Mosaic;
HPos = HOffset + Left;
PixWidth = PPU.Mosaic - r;
}
//uint32 s = Y * GFX.PPL + Left * GFX.PixSize;
s32 Xt=Left;
s32 Yt=Y;
for (uint32 x = Left; x < Right; x += PixWidth, /*s += PixWidth * GFX.PixSize*/Xt+=PixWidth,HPos += PixWidth, PixWidth = PPU.Mosaic) {
uint32 Quot = (HPos & OffsetMask) >> 3;
if (x + PixWidth >= Right) PixWidth = Right - x;
if (BG.TileSize == 8) {
if (Quot > 31) t = b2 + (Quot & 0x1f);
else t = b1 + Quot;
} else {
if (Quot > 63) t = b2 + ((Quot >> 1) & 0x1f);
else t = b1 + (Quot >> 1);
}
Tile = READ_2BYTES (t);
GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
realZ1=(int)GFX.Z1;realZ2=(int)GFX.Z2;
// pspDraw tile...
if (BG.TileSize != 8) {
if (Tile & H_FLIP) {
// Horizontal flip, but what about vertical flip ?
if (Tile & V_FLIP) {
// Both horzontal & vertical flip
if (Rem16 < 8) {
pspDrawLargePixel16 (Tile + 17 - (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines);
} else {
pspDrawLargePixel16 (Tile + 1 - (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines);
}
} else {
// Horizontal flip only
if (Rem16 > 7) {
pspDrawLargePixel16 (Tile + 17 - (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines);
} else {
pspDrawLargePixel16 (Tile + 1 - (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines);
}
}
} else {
// No horizontal flip, but is there a vertical flip ?
if (Tile & V_FLIP) {
// Vertical flip only
if (Rem16 < 8) {
pspDrawLargePixel16 (Tile + 16 + (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines);
} else {
pspDrawLargePixel16 (Tile + (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines);
}
} else {
// Normal unflipped
if (Rem16 > 7) {
pspDrawLargePixel16 (Tile + 16 + (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines);
} else {
pspDrawLargePixel16 (Tile + (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines);
}
}
}
} else pspDrawLargePixel16 (Tile, Xt,Yt, HPos & 7, PixWidth,VirtAlign, Lines);
}
}
}
//render
if (_verticesCol_ptr-_verticesCol) {
sceGuTexImage(0,512,512,512,tile_texture[current_bitshift]);
sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT/*GU_COLOR_5551*/|GU_VERTEX_16BIT|GU_TRANSFORM_2D,(_verticesCol_ptr-_verticesCol),0,_verticesCol);
}
}
void pspDrawBackgroundMode5 (uint32 /* BGMODE */, uint32 bg, uint8 Z1, uint8 Z2) {
int i;
GFX.Pitch = GFX.RealPitch;
GFX.PPL = GFX.PPLx2 >> 1;
GFX.PixSize = 1;
uint8 depths [2] = {Z1, Z2};
uint32 Tile;
uint16 *SC0;
uint16 *SC1;
uint16 *SC2;
uint16 *SC3;
uint32 Width;
if (last_palette!=BG.DirectColourMode){
//DirectColourMode has changed, so we have to reload palette
waitRendering();
last_palette=BG.DirectColourMode;
clut = (u16*)NO_CPU_CACHE(&clut256[0]);
memcpy(clut,GFX.ScreenColors,256*2);
if (rendering_beware_fix16)
for (int i=0;i<256;i++) {
if (clut[i]==fixedcol16) clut[i]^=1<<10; //swap blue 1st bit
}
clut[0]|=0x8000;
sceGuClutLoad(256/16,clut);
}
//bg is 32x30 tile but needs more in case of clipping or scroll changing every line
int num_vert=((GFX.EndY-GFX.StartY+1)+2)*(256/8+6);
if (!(vertices[current_bitshift]=(struct Vertex*)sceGuGetMemory(num_vert*2* sizeof(struct Vertex)))){
debug_log("not enough vertices!");
return;
}
vertices_ptr[current_bitshift]=vertices[current_bitshift];
vertices_end[current_bitshift]=vertices_ptr[current_bitshift]+num_vert*2;
/********************************************************/
/********************************************************/
BG.StartPalette = 0;
SC0 = (uint16 *) &VRAM[PPU.BG[bg].SCBase << 1];
if ((PPU.BG[bg].SCSize & 1)) SC1 = SC0 + 1024;
else SC1 = SC0;
if((SC1-(unsigned short*)VRAM)>0x10000) SC1=(uint16*)&VRAM[(((uint8*)SC1)-VRAM)%0x10000];
if ((PPU.BG[bg].SCSize & 2)) SC2 = SC1 + 1024;
else SC2 = SC0;
if((SC2-(unsigned short*)VRAM)>0x10000) SC2=(uint16*)&VRAM[(((uint8*)SC2)-VRAM)%0x10000];
if ((PPU.BG[bg].SCSize & 1)) SC3 = SC2 + 1024;
else SC3 = SC2;
if((SC3-(unsigned short*)VRAM)>0x10000) SC3=(uint16*)&VRAM[(((uint8*)SC3)-VRAM)%0x10000];
int Lines;
int VOffsetMask;
int VOffsetShift;
if (BG.TileSize == 16) {
VOffsetMask = 0x3ff;
VOffsetShift = 4;
} else {
VOffsetMask = 0x1ff;
VOffsetShift = 3;
}
int endy = GFX.EndY;
for (int Y = GFX.StartY; Y <= endy; Y += Lines) {
int y = Y;
uint32 VOffset = LineData [y].BG[bg].VOffset;
uint32 HOffset = LineData [y].BG[bg].HOffset;
int VirtAlign = (Y + VOffset) & 7;
for (Lines = 1; Lines < 8 - VirtAlign; Lines++)
if ((VOffset != LineData [y + Lines].BG[bg].VOffset) || (HOffset != LineData [y + Lines].BG[bg].HOffset)) break;
HOffset <<= 1;
if (Y + Lines > endy) Lines = endy + 1 - Y;
// VirtAlign <<= 3;
int ScreenLine = (VOffset + Y) >> VOffsetShift;
int t1;
int t2;
if (((VOffset + Y) & 15) > 7) {
t1 = 16;
t2 = 0;
} else {
t1 = 0;
t2 = 16;
}
uint16 *b1;
uint16 *b2;
if (ScreenLine & 0x20) b1 = SC2, b2 = SC3;
else b1 = SC0, b2 = SC1;
b1 += (ScreenLine & 0x1f) << 5;
b2 += (ScreenLine & 0x1f) << 5;
int clipcount = GFX.pCurrentClip->Count [bg];
if (!clipcount) clipcount = 1;
for (int clip = 0; clip < clipcount; clip++) {
int Left;
int Right;
if (!GFX.pCurrentClip->Count [bg]) {
Left = 0;
Right = 512;
} else {
Left = GFX.pCurrentClip->Left [clip][bg]* 2;
Right = GFX.pCurrentClip->Right [clip][bg] * 2;
if (Right <= Left) continue;
}
//uint32 s = (Left>>1) * GFX.PixSize + Y * 256;//GFX.PPL;
s32 Xt=Left>>1;
s32 Yt=Y;
uint32 HPos = (HOffset + Left * GFX.PixSize) & 0x3ff;
uint32 Quot = HPos >> 3;
uint32 Count = 0;
uint16 *t;
if (Quot > 63) t = b2 + ((Quot >> 1) & 0x1f);
else t = b1 + (Quot >> 1);
Width = Right - Left;
// Left hand edge clipped tile
if (HPos & 7) {
int Offset = (HPos & 7);
Count = 8 - Offset;
if (Count > Width) Count = Width;
//s -= Offset>>1;
Xt-=Offset>>1;
Tile = READ_2BYTES (t);
GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
realZ1=(int)GFX.Z1;realZ2=(int)GFX.Z2;
if (BG.TileSize == 8) {
if (!(Tile & H_FLIP)) {
// Normal, unflipped
pspDrawHiResClippedTile16 (Tile + (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines);
} else {
// H flip
pspDrawHiResClippedTile16 (Tile + 1 - (Quot & 1),Xt,Yt, Offset, Count, VirtAlign, Lines);
}
} else {
if (!(Tile & (V_FLIP | H_FLIP))) {
// Normal, unflipped
pspDrawHiResClippedTile16 (Tile + t1 + (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines);
} else
if (Tile & H_FLIP) {
if (Tile & V_FLIP) {
// H & V flip
pspDrawHiResClippedTile16 (Tile + t2 + 1 - (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines);
} else {
// H flip only
pspDrawHiResClippedTile16 (Tile + t1 + 1 - (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines);
}
} else {
// V flip only
pspDrawHiResClippedTile16 (Tile + t2 + (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines);
}
}
t += Quot & 1;
if (Quot == 63) t = b2;
else if (Quot == 127) t = b1;
Quot++;
//s += 4;
Xt+=4;
}
// Middle, unclipped tiles
Count = Width - Count;
int Middle = Count >> 3;
Count &= 7;
for (int C = Middle; C > 0; Xt+=4/*s += 4*/, Quot++, C--) {
Tile = READ_2BYTES(t);
GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
realZ1=(int)GFX.Z1;realZ2=(int)GFX.Z2;
if (BG.TileSize == 8) {
if (!(Tile & H_FLIP)) {
// Normal, unflipped
pspDrawHiResTile16 (Tile + (Quot & 1),Xt,Yt, VirtAlign, Lines);
} else {
// H flip
pspDrawHiResTile16 (Tile + 1 - (Quot & 1), Xt,Yt, VirtAlign, Lines);
}
} else {
if (!(Tile & (V_FLIP | H_FLIP))) {
// Normal, unflipped
pspDrawHiResTile16 (Tile + t1 + (Quot & 1),Xt,Yt, VirtAlign, Lines);
} else if (Tile & H_FLIP) {
if (Tile & V_FLIP) {
// H & V flip
pspDrawHiResTile16 (Tile + t2 + 1 - (Quot & 1), Xt,Yt, VirtAlign, Lines);
} else {
// H flip only
pspDrawHiResTile16 (Tile + t1 + 1 - (Quot & 1), Xt,Yt, VirtAlign, Lines);
}
} else {
// V flip only
pspDrawHiResTile16 (Tile + t2 + (Quot & 1), Xt,Yt, VirtAlign, Lines);
}
}
t += Quot & 1;
if (Quot == 63) t = b2;
else if (Quot == 127) t = b1;
}
// Right-hand edge clipped tiles
if (Count) {
Tile = READ_2BYTES(t);
GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
realZ1=(int)GFX.Z1;realZ2=(int)GFX.Z2;
if (BG.TileSize == 8) {
if (!(Tile & H_FLIP)) {
// Normal, unflipped
pspDrawHiResClippedTile16 (Tile + (Quot & 1), Xt,Yt, 0, Count, VirtAlign, Lines);
} else {
// H flip
pspDrawHiResClippedTile16 (Tile + 1 - (Quot & 1), Xt,Yt, 0, Count, VirtAlign, Lines);
}
} else {
if (!(Tile & (V_FLIP | H_FLIP))) {
// Normal, unflipped
pspDrawHiResClippedTile16 (Tile + t1 + (Quot & 1), Xt,Yt, 0, Count, VirtAlign, Lines);
} else if (Tile & H_FLIP) {
if (Tile & V_FLIP) {
// H & V flip
pspDrawHiResClippedTile16 (Tile + t2 + 1 - (Quot & 1), Xt,Yt, 0, Count, VirtAlign, Lines);
} else {
// H flip only
pspDrawHiResClippedTile16 (Tile + t1 + 1 - (Quot & 1), Xt,Yt, 0, Count, VirtAlign, Lines);
}
} else {
// V flip only
pspDrawHiResClippedTile16 (Tile + t2 + (Quot & 1), Xt,Yt, 0, Count, VirtAlign, Lines);
}
}
}