-
Notifications
You must be signed in to change notification settings - Fork 30
/
gfx_psp_fix.cpp
2725 lines (2363 loc) · 81.6 KB
/
gfx_psp_fix.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;
extern int add_sub_mode;
extern int last_palette;
extern int current_bitshift;
extern int render_timestamp;
//extern u16 __attribute__((aligned(16))) clut256[256*3];
extern u16* clut256;
extern u16* clut;
extern u32 fixedcol16;
extern u32 fixedcol;
extern int rendering_beware_fix16;
// for OBJ & BGS
extern struct Vertex *vertices[3];
extern struct Vertex *vertices_ptr[3];
extern struct Vertex *vertices_end[3];
// for Blending
extern struct Vertex *_vertices;
extern struct Vertex *_vertices_ptr;
extern struct VertexCol *_verticesCol;
extern struct VertexCol *_verticesCol_ptr;
extern 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];
ClipDataFix* pCurrentClipFix;
#define ON_MAIN(N) \
(GPUPack.GFX.r212c & (1 << (N)) && \
!(PPUPack.PPU.BG_Forced & (1 << (N))))
#define SUB_OR_ADD(N) \
(GPUPack.GFX.r2131 & (1 << (N)))
#define ON_SUB(N) \
((GPUPack.GFX.r2130 & 0x30) != 0x30 && \
(GPUPack.GFX.r2130 & 2) && \
(GPUPack.GFX.r212d & (1 << N)) && \
!(PPUPack.PPU.BG_Forced & (1 << (N))))
#define ANYTHING_ON_SUB \
((GPUPack.GFX.r2130 & 0x30) != 0x30 && \
(GPUPack.GFX.r2130 & 2) && \
(GPUPack.GFX.r212d & 0x1f))
#define ADD_OR_SUB_ON_ANYTHING \
(GPUPack.GFX.r2131 & 0x3f)
#define BLACK BUILD_PIXEL(0,0,0)
void pspDrawTile16 (uint32 Tile, s32 x,s32 y, uint32 StartLine,
uint32 LineCount,short realZ2);
void pspDrawClippedTile16 (uint32 Tile, s32 x,s32 y,
uint32 StartPixel, uint32 Width,
uint32 StartLine, uint32 LineCount,short realZ2);
void pspDrawHiResTile16 (uint32 Tile, s32 x,s32 y, uint32 StartLine,
uint32 LineCount,short realZ2);
void pspDrawHiResClippedTile16 (uint32 Tile, s32 x,s32 y,
uint32 StartPixel, uint32 Width,
uint32 StartLine, uint32 LineCount,short realZ2);
void pspDrawLargePixel16 (uint32 Tile, s32 x,s32 y,
uint32 StartPixel, uint32 Pixels,
uint32 StartLine, uint32 LineCount,short realZ2);
inline void pspSelectAddSubMode() {
if (GPUPack.GFX.r2131 & 0x80) {
if (GPUPack.GFX.r2131 & 0x40) {
if (GPUPack.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 (GPUPack.GFX.r2131 & 0x40) {
if (GPUPack.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");
}
}
}
#define pspDrawTile16_order pspDrawTile16
#define pspDrawClippedTile16_order pspDrawClippedTile16
void initRenderingFix(){
sceGuStart(GU_DIRECT,list);
//rendering options
sceGuEnable(GU_DEPTH_TEST);
sceGuDepthFunc(GU_GREATER);
sceGuEnable(GU_ALPHA_TEST);
sceGuAlphaFunc(GU_EQUAL,0,0x1);
sceGuEnable(GU_TEXTURE_2D);
sceGuTexFilter(GU_NEAREST,GU_NEAREST);
sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGBA);
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 pspDrawOBJS (bool8 OnMain, uint8 D, uint8 drawmode);
void endRenderingFix(){
sceGuFinish();
sceGuSync(0,0);
}
void waitRenderingFix() {
/*sceGuFinish();
sceGuSync(0,0);
sceGuStart(GU_DIRECT,list);
*/
}
void pspDrawFASTOBJSFix (bool8 OnMain, uint8 D, uint8 drawmode)
{
//uint32 O;
s32 Xs,Ys;
uint32 BaseTile, Tile;
GPUPack.BG.BitShift = 4;
GPUPack.BG.TileShift = 5;
GPUPack.BG.TileAddress = PPUPack.PPU.OBJNameBase;
GPUPack.BG.StartPalette = 128;
GPUPack.BG.PaletteShift = 4;
GPUPack.BG.PaletteMask = 7;
current_bitshift=TILE_4BIT;
GPUPack.BG.Buffer = tile_texture[TILE_4BIT];//IPPU.TileCache8 [TILE_4BIT];
GPUPack.BG.Buffered = IPPU.TileCached [TILE_4BIT];
GPUPack.BG.NameSelect = PPUPack.PPU.OBJNameSelect;
GPUPack.BG.DirectColourMode = false;
/********************************************************/
/****************PSP STUFF*******************************/
/********************************************************/
int num_vert=0;//8192;//0;
int I=0;
for (int S = GPUPack.GFX.OBJList [I++]; S >= 0; S = GPUPack.GFX.OBJList [I++]) {
int VPos = GPUPack.GFX.VPositions [S];
int Size = GPUPack.GFX.Sizes[S];
if (VPos + Size <= (int) GPUPack.GFX.StartY || VPos > (int) GPUPack.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;
/********************************************************/
/********************************************************/
GPUPack.GFX.Z1 = (D + 2);
//realZ1=(int)(GPUPack.GFX.Z1);
I=0;
for (int S = GPUPack.GFX.OBJList [I++]; S >= 0; S = GPUPack.GFX.OBJList [I++]) {
int VPos = GPUPack.GFX.VPositions [S];
int Size = GPUPack.GFX.Sizes[S];
int TileInc = 1;
int Offset;
if (VPos + Size <= (int) GPUPack.GFX.StartY || VPos > (int) GPUPack.GFX.EndY)
continue;
//drawing sub/added sprites => palette has to be 4-7
if (OnMain){
if ((drawmode==1) && (PPUPack.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) && (PPUPack.PPU.OBJ [S].Palette >= 4) && SUB_OR_ADD(4)) continue;
}
BaseTile = PPUPack.PPU.OBJ[S].Name | (PPUPack.PPU.OBJ[S].Palette << 10);
if (PPUPack.PPU.OBJ[S].HFlip) {
BaseTile += ((Size >> 3) - 1) | H_FLIP;
TileInc = -1;
}
if (PPUPack.PPU.OBJ[S].VFlip) BaseTile |= V_FLIP;
int clipcount = GPUPack.GFX.pCurrentClip->Count [4];
if (!clipcount) clipcount = 1;
GPUPack.GFX.Z2 = ((PPUPack.PPU.OBJ[S].Priority + 1) * 4 + D);
short realZ2=(short)(GPUPack.GFX.Z2);//+I-1;
for (int clip = 0; clip < clipcount; clip++){
int Left;
int Right;
if (!GPUPack.GFX.pCurrentClip->Count [4]){
Left = 0;
Right = 256;
}else{
Left = GPUPack.GFX.pCurrentClip->Left [clip][4];
Right = GPUPack.GFX.pCurrentClip->Right [clip][4];
}
if (Right <= Left || PPUPack.PPU.OBJ[S].HPos + Size <= Left || PPUPack.PPU.OBJ[S].HPos >= Right) continue;
for (int Y = 0; Y < Size; Y += 8) {
if (VPos + Y + 7 >= (int) GPUPack.GFX.StartY && VPos + Y <= (int) GPUPack.GFX.EndY){
int StartLine;
int TileLine;
int LineCount;
int Last;
if ((StartLine = VPos + Y) < (int) GPUPack.GFX.StartY) {
StartLine = GPUPack.GFX.StartY - StartLine;
LineCount = 8 - StartLine;
}else{
StartLine = 0;
LineCount = 8;
}
if ((Last = VPos + Y + 7 - GPUPack.GFX.EndY) > 0)
if ((LineCount -= Last) <= 0) break;
TileLine = StartLine;// << 3;
Ys = (VPos + Y + StartLine);
if (!PPUPack.PPU.OBJ[S].VFlip) Tile = BaseTile + (Y << 1);
else Tile = BaseTile + ((Size - Y - 8) << 1);
int Middle = Size >> 3;
if (PPUPack.PPU.OBJ[S].HPos < Left) {
Tile += ((Left - PPUPack.PPU.OBJ[S].HPos) >> 3) * TileInc;
Middle -= (Left - PPUPack.PPU.OBJ[S].HPos) >> 3;
Xs = Left ;
if ((Offset = (Left - PPUPack.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,realZ2);
if (W >= Width) continue;
Tile += TileInc;
Middle--;
Xs += 8 ;
}
} else Xs = PPUPack.PPU.OBJ[S].HPos ;
if (PPUPack.PPU.OBJ[S].HPos + Size >= Right) {
Middle -= ((PPUPack.PPU.OBJ[S].HPos + Size + 7) - Right) >> 3;
Offset = (Right - (PPUPack.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,realZ2);
}
if (Offset){
pspDrawClippedTile16 (Tile, Xs,Ys, 0, Offset,TileLine, LineCount,realZ2);
}
}
}
}
}
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 pspDrawOBJSFix (bool8 OnMain, uint8 D, uint8 drawmode) {
//uint32 O;
s32 Xs,Ys;
int speZ;
uint32 BaseTile, Tile;
GPUPack.BG.BitShift = 4;
current_bitshift=TILE_4BIT;
GPUPack.BG.TileShift = 5;
GPUPack.BG.TileAddress = PPUPack.PPU.OBJNameBase;
GPUPack.BG.StartPalette = 128;
GPUPack.BG.PaletteShift = 4;
GPUPack.BG.PaletteMask = 7;
GPUPack.BG.Buffer = tile_texture[TILE_4BIT];//IPPU.TileCache8 [TILE_4BIT];
GPUPack.BG.Buffered = IPPU.TileCached [TILE_4BIT];
GPUPack.BG.NameSelect = PPUPack.PPU.OBJNameSelect;
GPUPack.BG.DirectColourMode = false;
/********************************************************/
/****************PSP STUFF*******************************/
/********************************************************/
int num_vert=0;//8192;//0;
int I=0;
for (int S = GPUPack.GFX.OBJList [I++]; S >= 0; S = GPUPack.GFX.OBJList [I++]) {
int VPos = GPUPack.GFX.VPositions [S];
int Size = GPUPack.GFX.Sizes[S];
if (VPos + Size <= (int) GPUPack.GFX.StartY || VPos > (int) GPUPack.GFX.EndY)
continue;
//drawing sub/added sprites => palette has to be 4-7
num_vert+=(Size/8)*(Size/8);
}
num_vert += 33*pCurrentClipFix->GroupCount [4];
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;
/********************************************************/
/********************************************************/
GPUPack.GFX.Z1 = (D + 2);
//realZ1=(int)(GPUPack.GFX.Z1);
I=0;
for (int S = GPUPack.GFX.OBJList [I++]; S >= 0; S = GPUPack.GFX.OBJList [I++]) {
int VPos = GPUPack.GFX.VPositions [S];
int Size = GPUPack.GFX.Sizes[S];
int TileInc = 1;
int Offset;
for (uint32 g = 0; g < pCurrentClipFix->GroupCount [4]; g++)
{
if (VPos + Size <= (int) pCurrentClipFix->Start[4][g] || VPos > (int) pCurrentClipFix->End[4][g])
continue;
//drawing sub/added sprites => palette has to be 4-7
speZ=0;
if (OnMain)
{
if ((drawmode==1) && (PPUPack.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
else if ((drawmode==2) && (PPUPack.PPU.OBJ [S].Palette >= 4) && SUB_OR_ADD(4)) speZ=1;
}
BaseTile = PPUPack.PPU.OBJ[S].Name | (PPUPack.PPU.OBJ[S].Palette << 10);
if (PPUPack.PPU.OBJ[S].HFlip)
{
BaseTile += ((Size >> 3) - 1) | H_FLIP;
TileInc = -1;
}
if (PPUPack.PPU.OBJ[S].VFlip) BaseTile |= V_FLIP;
int clipcount = pCurrentClipFix->Count [4][g];
if (!clipcount) clipcount = 1;
GPUPack.GFX.Z2 = ((PPUPack.PPU.OBJ[S].Priority + 1) * 4 + D);
short realZ2;
if (!speZ) realZ2=(short)(GPUPack.GFX.Z2);//+I-1;
else realZ2=0;
for (int clip = 0; clip < clipcount; clip++)
{
int Left;
int Right;
if(pCurrentClipFix->Count [4][g]==0)
{
Left = 0;
Right = 256;
}else
{
Left = pCurrentClipFix->Left [clip][4][g];
Right = pCurrentClipFix->Right [clip][4][g];
}
if (Right <= Left || PPUPack.PPU.OBJ[S].HPos + Size <= Left || PPUPack.PPU.OBJ[S].HPos >= Right) continue;
for (int Y = 0; Y < Size; Y += 8)
{
if (VPos + Y + 7 >= (int) pCurrentClipFix->Start[4][g] && VPos + Y <= (int) pCurrentClipFix->End[4][g])
{
int StartLine;
int TileLine;
int LineCount;
int Last;
if ((StartLine = VPos + Y) < (int) pCurrentClipFix->Start[4][g])
{
StartLine = pCurrentClipFix->Start[4][g] - StartLine;
LineCount = 8 - StartLine;
} else
{
StartLine = 0;
LineCount = 8;
}
if ((Last = VPos + Y + 7 - pCurrentClipFix->End[4][g]) > 0)
if ((LineCount -= Last) <= 0) break;
TileLine = StartLine;// << 3;
Ys = (VPos + Y + StartLine);
if (!PPUPack.PPU.OBJ[S].VFlip) Tile = BaseTile + (Y << 1);
else Tile = BaseTile + ((Size - Y - 8) << 1);
int Middle = Size >> 3;
if (PPUPack.PPU.OBJ[S].HPos < Left)
{
Tile += ((Left - PPUPack.PPU.OBJ[S].HPos) >> 3) * TileInc;
Middle -= (Left - PPUPack.PPU.OBJ[S].HPos) >> 3;
Xs = Left ;
if ((Offset = (Left - PPUPack.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,realZ2);
if (W >= Width) continue;
Tile += TileInc;
Middle--;
Xs += 8 ;
}
}
else Xs = PPUPack.PPU.OBJ[S].HPos ;
if (PPUPack.PPU.OBJ[S].HPos + Size >= Right)
{
Middle -= ((PPUPack.PPU.OBJ[S].HPos + Size + 7) - Right) >> 3;
Offset = (Right - (PPUPack.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,realZ2);
}
if (Offset)
{
pspDrawClippedTile16 (Tile, Xs,Ys, 0, Offset,TileLine, LineCount,realZ2);
}
}
}
}
}
}
/*{
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 pspDrawBackgroundMosaicFix (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!=GPUPack.BG.DirectColourMode){
//DirectColourMode has changed, so we have to reload palette
waitRenderingFix();
last_palette=GPUPack.BG.DirectColourMode;
clut = (u16*)NO_CPU_CACHE(&clut256[0]);
memcpy(clut,GPUPack.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=((GPUPack.GFX.EndY-GPUPack.GFX.StartY+1)+2+pCurrentClipFix->GroupCount [bg])*(256/8+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) GPUPack.BG.StartPalette = bg << 5;
else GPUPack.BG.StartPalette = 0;
SC0 = (uint16 *) &VRAM[PPUPack.PPU.BG[bg].SCBase << 1];
if (PPUPack.PPU.BG[bg].SCSize & 1) SC1 = SC0 + 1024;
else SC1 = SC0;
if((SC1-(unsigned short*)VRAM)>0x10000) SC1-=0x10000;
if (PPUPack.PPU.BG[bg].SCSize & 2) SC2 = SC1 + 1024;
else SC2 = SC0;
if((SC2-(unsigned short*)VRAM)>0x10000) SC2-=0x10000;
if (PPUPack.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 (GPUPack.BG.TileSize == 16) {
OffsetMask = 0x3ff;
OffsetShift = 4;
} else {
OffsetMask = 0x1ff;
OffsetShift = 3;
}
uint32 Y = GPUPack.GFX.StartY;
for (uint32 g = 0; g < pCurrentClipFix->GroupCount [bg]; g++)
{
Y=pCurrentClipFix->Start [bg][g];
for (; Y <= GPUPack.GFX.EndY && Y <= pCurrentClipFix->End [bg][g]; Y += Lines)
{
uint32 VOffset = LineData [Y].BG[bg].VOffset;
uint32 HOffset = LineData [Y].BG[bg].HOffset;
uint32 MosaicOffset = Y % PPUPack.PPU.Mosaic;
for (Lines = 1; Lines < PPUPack.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 > GPUPack.GFX.EndY) Lines = GPUPack.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 = pCurrentClipFix->Count [bg][g];
uint32 HPos = HOffset;
uint32 PixWidth = PPUPack.PPU.Mosaic;
if (!ClipCount) ClipCount = 1;
for (uint32 clip = 0; clip < ClipCount; clip++)
{
if (pCurrentClipFix->Count [bg][g])
{
Left = pCurrentClipFix->Left [clip][bg][g];
Right = pCurrentClipFix->Right [clip][bg][g];
uint32 r = Left % PPUPack.PPU.Mosaic;
HPos = HOffset + Left;
PixWidth = PPUPack.PPU.Mosaic - r;
}
//uint32 s = Y * GPUPack.GFX.PPL + Left * GPUPack.GFX.PixSize;
s32 Xt=Left;
s32 Yt=Y;
for (uint32 x = Left; x < Right; x += PixWidth, /*s += PixWidth * GPUPack.GFX.PixSize*/Xt+=PixWidth,HPos += PixWidth, PixWidth = PPUPack.PPU.Mosaic)
{
uint32 Quot = (HPos & OffsetMask) >> 3;
if (x + PixWidth >= Right) PixWidth = Right - x;
if (GPUPack.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);
GPUPack.GFX.Z1 = GPUPack.GFX.Z2 = depths [(Tile & 0x2000) >> 13];
//realZ1=(int)GPUPack.GFX.Z1;realZ2=(int)GPUPack.GFX.Z2;
short realZ2=(short)GPUPack.GFX.Z2;
// pspDraw tile...
if (GPUPack.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,realZ2);
} else {
pspDrawLargePixel16 (Tile + 1 - (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines,realZ2);
}
} else {
// Horizontal flip only
if (Rem16 > 7) {
pspDrawLargePixel16 (Tile + 17 - (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines,realZ2);
} else {
pspDrawLargePixel16 (Tile + 1 - (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines,realZ2);
}
}
} 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,realZ2);
} else {
pspDrawLargePixel16 (Tile + (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines,realZ2);
}
} else
{
// Normal unflipped
if (Rem16 > 7) {
pspDrawLargePixel16 (Tile + 16 + (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines,realZ2);
} else {
pspDrawLargePixel16 (Tile + (Quot & 1), Xt,Yt,HPos & 7, PixWidth,VirtAlign, Lines,realZ2);
}
}
}
} else pspDrawLargePixel16 (Tile, Xt,Yt, HPos & 7, PixWidth,VirtAlign, Lines,realZ2);
}
}
}
}
//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 pspDrawBackgroundMode5Fix (uint32 /* BGMODE */, uint32 bg, uint8 Z1, uint8 Z2) {
GPUPack.GFX.Pitch = GPUPack.GFX.RealPitch;
GPUPack.GFX.PPL = GPUPack.GFX.PPLx2 >> 1;
uint8 depths [2] = {Z1, Z2};
uint32 Tile;
uint16 *SC0;
uint16 *SC1;
uint16 *SC2;
uint16 *SC3;
uint32 Width;
if (last_palette!=GPUPack.BG.DirectColourMode){
//DirectColourMode has changed, so we have to reload palette
waitRenderingFix();
last_palette=GPUPack.BG.DirectColourMode;
clut = (u16*)NO_CPU_CACHE(&clut256[0]);
memcpy(clut,GPUPack.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=((GPUPack.GFX.EndY-GPUPack.GFX.StartY+1)+2+pCurrentClipFix->GroupCount [bg])*(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;
/********************************************************/
/********************************************************/
GPUPack.BG.StartPalette = 0;
SC0 = (uint16 *) &VRAM[PPUPack.PPU.BG[bg].SCBase << 1];
if ((PPUPack.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 ((PPUPack.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 ((PPUPack.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;
uint32 endy = GPUPack.GFX.EndY;
uint32 Y = GPUPack.GFX.StartY;
for (uint32 g = 0; g < pCurrentClipFix->GroupCount [bg]; g++)
{
Y=pCurrentClipFix->Start [bg][g];
for (; Y <= GPUPack.GFX.EndY && Y <= pCurrentClipFix->End [bg][g]; 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) >> (GPUPack.BG.TileSize == 16 ? 4 : 3);
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 = pCurrentClipFix->Count [bg][g];
if (!clipcount) clipcount = 1;
for (int clip = 0; clip < clipcount; clip++)
{
int Left;
int Right;
if (pCurrentClipFix->Count [bg][g]==0)
{
Left = 0;
Right = 512;
} else
{
Left = pCurrentClipFix->Left [clip][bg][g]* 2;
Right = pCurrentClipFix->Right [clip][bg][g] * 2;
if (Right <= Left) continue;
}
//uint32 s = (Left>>1) * GPUPack.GFX.PixSize + Y * 256;//GPUPack.GFX.PPL;
s32 Xt=Left>>1;
s32 Yt=Y;
uint32 HPos = (HOffset + Left * 1) & 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);
GPUPack.GFX.Z1 = GPUPack.GFX.Z2 = depths [(Tile & 0x2000) >> 13];
//realZ1=(int)GPUPack.GFX.Z1;realZ2=(int)GPUPack.GFX.Z2;
short realZ2=(short)GPUPack.GFX.Z2;
if (GPUPack.BG.TileSize == 8)
{
if (!(Tile & H_FLIP))
{
// Normal, unflipped
pspDrawHiResClippedTile16 (Tile + (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines,realZ2);
} else
{
// H flip
pspDrawHiResClippedTile16 (Tile + 1 - (Quot & 1),Xt,Yt, Offset, Count, VirtAlign, Lines,realZ2);
}
} else
{
if (!(Tile & (V_FLIP | H_FLIP)))
{
// Normal, unflipped
pspDrawHiResClippedTile16 (Tile + t1 + (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines,realZ2);
} else if (Tile & H_FLIP)
{
if (Tile & V_FLIP)
{
// H & V flip
pspDrawHiResClippedTile16 (Tile + t2 + 1 - (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines,realZ2);
} else
{
// H flip only
pspDrawHiResClippedTile16 (Tile + t1 + 1 - (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines,realZ2);
}
} else
{
// V flip only
pspDrawHiResClippedTile16 (Tile + t2 + (Quot & 1), Xt,Yt, Offset, Count, VirtAlign, Lines,realZ2);
}
}
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);
GPUPack.GFX.Z1 = GPUPack.GFX.Z2 = depths [(Tile & 0x2000) >> 13];
//realZ1=(int)GPUPack.GFX.Z1;realZ2=(int)GPUPack.GFX.Z2;
short realZ2=(short)GPUPack.GFX.Z2;
if (GPUPack.BG.TileSize == 8)
{
if (!(Tile & H_FLIP))
{
// Normal, unflipped
pspDrawHiResTile16 (Tile + (Quot & 1),Xt,Yt, VirtAlign, Lines,realZ2);
} else
{
// H flip
pspDrawHiResTile16 (Tile + 1 - (Quot & 1), Xt,Yt, VirtAlign, Lines,realZ2);
}
} else
{
if (!(Tile & (V_FLIP | H_FLIP)))
{
// Normal, unflipped
pspDrawHiResTile16 (Tile + t1 + (Quot & 1),Xt,Yt, VirtAlign, Lines,realZ2);
} else if (Tile & H_FLIP)