-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathSDL_render_psp.c
1400 lines (1181 loc) · 42.4 KB
/
SDL_render_psp.c
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_VIDEO_RENDER_PSP
#include "../SDL_sysrender.h"
#include "SDL_render_psp_c.h"
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include <pspgum.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <pspge.h>
#include <stdarg.h>
#include <stdlib.h>
#include <vram.h>
// PSP renderer implementation, based on the PGE
static unsigned int __attribute__((aligned(16))) DisplayList[262144];
#define COL5650(r, g, b, a) ((r >> 3) | ((g >> 2) << 5) | ((b >> 3) << 11))
#define COL5551(r, g, b, a) ((r >> 3) | ((g >> 3) << 5) | ((b >> 3) << 10) | (a > 0 ? 0x7000 : 0))
#define COL4444(r, g, b, a) ((r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8) | ((a >> 4) << 12))
#define COL8888(r, g, b, a) ((r) | ((g) << 8) | ((b) << 16) | ((a) << 24))
/**
* Holds psp specific texture data
*
* Part of a hot-list of textures that are used as render targets
* When short of vram we spill Least-Recently-Used render targets to system memory
*/
typedef struct PSP_TextureData
{
void *data; /**< Image data. */
unsigned int size; /**< Size of data in bytes. */
unsigned int width; /**< Image width. */
unsigned int height; /**< Image height. */
unsigned int textureWidth; /**< Texture width (power of two). */
unsigned int textureHeight; /**< Texture height (power of two). */
unsigned int bits; /**< Image bits per pixel. */
unsigned int format; /**< Image format - one of ::pgePixelFormat. */
unsigned int pitch;
bool swizzled; /**< Is image swizzled. */
struct PSP_TextureData *prevhotw; /**< More recently used render target */
struct PSP_TextureData *nexthotw; /**< Less recently used render target */
} PSP_TextureData;
typedef struct
{
SDL_BlendMode mode;
unsigned int color;
int shadeModel;
SDL_Texture *texture;
} PSP_BlendState;
typedef struct
{
unsigned int color;
} PSP_DrawStateCache;
typedef struct
{
void *frontbuffer; /**< main screen buffer */
void *backbuffer; /**< buffer presented to display */
SDL_Texture *boundTarget; /**< currently bound rendertarget */
bool initialized; /**< is driver initialized */
bool displayListAvail; /**< is the display list already initialized for this frame */
unsigned int psm; /**< format of the display buffers */
unsigned int bpp; /**< bits per pixel of the main display */
bool vsync; /**< whether we do vsync */
PSP_BlendState blendState; /**< current blend mode */
PSP_TextureData *most_recent_target; /**< start of render target LRU double linked list */
PSP_TextureData *least_recent_target; /**< end of the LRU list */
bool vblank_not_reached; /**< whether vblank wasn't reached */
} PSP_RenderData;
typedef struct
{
float x, y, z;
} VertV;
typedef struct
{
float u, v;
float x, y, z;
} VertTV;
typedef struct
{
SDL_Color col;
float x, y, z;
} VertCV;
typedef struct
{
float u, v;
SDL_Color col;
float x, y, z;
} VertTCV;
#define radToDeg(x) ((x)*180.f / SDL_PI_F)
#define degToRad(x) ((x)*SDL_PI_F / 180.f)
static float MathAbs(float x)
{
float result;
__asm__ volatile(
"mtv %1, S000\n"
"vabs.s S000, S000\n"
"mfv %0, S000\n"
: "=r"(result)
: "r"(x));
return result;
}
static void MathSincos(float r, float *s, float *c)
{
__asm__ volatile(
"mtv %2, S002\n"
"vcst.s S003, VFPU_2_PI\n"
"vmul.s S002, S002, S003\n"
"vrot.p C000, S002, [s, c]\n"
"mfv %0, S000\n"
"mfv %1, S001\n"
: "=r"(*s), "=r"(*c)
: "r"(r));
}
static void Swap(float *a, float *b)
{
float n = *a;
*a = *b;
*b = n;
}
static inline int InVram(void *data)
{
return data < (void *)0x04200000;
}
// Return next power of 2
static int TextureNextPow2(unsigned int w)
{
unsigned int n = 2;
if (w == 0) {
return 0;
}
while (w > n) {
n <<= 1;
}
return n;
}
static void psp_on_vblank(u32 sub, PSP_RenderData *data)
{
if (data) {
data->vblank_not_reached = false;
}
}
static int PixelFormatToPSPFMT(SDL_PixelFormat format)
{
switch (format) {
case SDL_PIXELFORMAT_BGR565:
return GU_PSM_5650;
case SDL_PIXELFORMAT_ABGR1555:
return GU_PSM_5551;
case SDL_PIXELFORMAT_ABGR4444:
return GU_PSM_4444;
case SDL_PIXELFORMAT_ABGR8888:
return GU_PSM_8888;
default:
return GU_PSM_8888;
}
}
/// SECTION render target LRU management
static void LRUTargetRelink(PSP_TextureData *psp_texture)
{
if (psp_texture->prevhotw) {
psp_texture->prevhotw->nexthotw = psp_texture->nexthotw;
}
if (psp_texture->nexthotw) {
psp_texture->nexthotw->prevhotw = psp_texture->prevhotw;
}
}
static void LRUTargetPushFront(PSP_RenderData *data, PSP_TextureData *psp_texture)
{
psp_texture->nexthotw = data->most_recent_target;
if (data->most_recent_target) {
data->most_recent_target->prevhotw = psp_texture;
}
data->most_recent_target = psp_texture;
if (!data->least_recent_target) {
data->least_recent_target = psp_texture;
}
}
static void LRUTargetRemove(PSP_RenderData *data, PSP_TextureData *psp_texture)
{
LRUTargetRelink(psp_texture);
if (data->most_recent_target == psp_texture) {
data->most_recent_target = psp_texture->nexthotw;
}
if (data->least_recent_target == psp_texture) {
data->least_recent_target = psp_texture->prevhotw;
}
psp_texture->prevhotw = NULL;
psp_texture->nexthotw = NULL;
}
static void LRUTargetBringFront(PSP_RenderData *data, PSP_TextureData *psp_texture)
{
if (data->most_recent_target == psp_texture) {
return; // nothing to do
}
LRUTargetRemove(data, psp_texture);
LRUTargetPushFront(data, psp_texture);
}
static void TextureStorageFree(void *storage)
{
if (InVram(storage)) {
vfree(storage);
} else {
SDL_free(storage);
}
}
static bool TextureSwizzle(PSP_TextureData *psp_texture, void *dst)
{
int bytewidth, height;
int rowblocks, rowblocksadd;
int i, j;
unsigned int blockaddress = 0;
unsigned int *src = NULL;
unsigned char *data = NULL;
if (psp_texture->swizzled) {
return true;
}
bytewidth = psp_texture->textureWidth * (psp_texture->bits >> 3);
height = psp_texture->size / bytewidth;
rowblocks = (bytewidth >> 4);
rowblocksadd = (rowblocks - 1) << 7;
src = (unsigned int *)psp_texture->data;
data = dst;
if (!data) {
data = SDL_malloc(psp_texture->size);
}
if (!data) {
return false;
}
for (j = 0; j < height; j++, blockaddress += 16) {
unsigned int *block;
block = (unsigned int *)&data[blockaddress];
for (i = 0; i < rowblocks; i++) {
*block++ = *src++;
*block++ = *src++;
*block++ = *src++;
*block++ = *src++;
block += 28;
}
if ((j & 0x7) == 0x7) {
blockaddress += rowblocksadd;
}
}
TextureStorageFree(psp_texture->data);
psp_texture->data = data;
psp_texture->swizzled = true;
sceKernelDcacheWritebackRange(psp_texture->data, psp_texture->size);
return true;
}
static bool TextureUnswizzle(PSP_TextureData *psp_texture, void *dst)
{
int bytewidth, height;
int widthblocks, heightblocks;
int dstpitch, dstrow;
int blockx, blocky;
int j;
unsigned int *src = NULL;
unsigned char *data = NULL;
unsigned char *ydst = NULL;
if (!psp_texture->swizzled) {
return true;
}
bytewidth = psp_texture->textureWidth * (psp_texture->bits >> 3);
height = psp_texture->size / bytewidth;
widthblocks = bytewidth / 16;
heightblocks = height / 8;
dstpitch = (bytewidth - 16) / 4;
dstrow = bytewidth * 8;
src = (unsigned int *)psp_texture->data;
data = dst;
if (!data) {
data = SDL_malloc(psp_texture->size);
}
if (!data) {
return false;
}
ydst = (unsigned char *)data;
for (blocky = 0; blocky < heightblocks; ++blocky) {
unsigned char *xdst = ydst;
for (blockx = 0; blockx < widthblocks; ++blockx) {
unsigned int *block;
block = (unsigned int *)xdst;
for (j = 0; j < 8; ++j) {
*(block++) = *(src++);
*(block++) = *(src++);
*(block++) = *(src++);
*(block++) = *(src++);
block += dstpitch;
}
xdst += 16;
}
ydst += dstrow;
}
TextureStorageFree(psp_texture->data);
psp_texture->data = data;
psp_texture->swizzled = false;
sceKernelDcacheWritebackRange(psp_texture->data, psp_texture->size);
return true;
}
static bool TextureSpillToSram(PSP_RenderData *data, PSP_TextureData *psp_texture)
{
// Assumes the texture is in VRAM
if (psp_texture->swizzled) {
// Texture was swizzled in vram, just copy to system memory
void *sdata = SDL_malloc(psp_texture->size);
if (!sdata) {
return false;
}
SDL_memcpy(sdata, psp_texture->data, psp_texture->size);
vfree(psp_texture->data);
psp_texture->data = sdata;
return true;
} else {
return TextureSwizzle(psp_texture, NULL); // Will realloc in sysram
}
}
static bool TexturePromoteToVram(PSP_RenderData *data, PSP_TextureData *psp_texture, bool target)
{
// Assumes texture in sram and a large enough continuous block in vram
void *tdata = vramalloc(psp_texture->size);
if (psp_texture->swizzled && target) {
return TextureUnswizzle(psp_texture, tdata);
} else {
SDL_memcpy(tdata, psp_texture->data, psp_texture->size);
SDL_free(psp_texture->data);
psp_texture->data = tdata;
return true;
}
}
static bool TextureSpillLRU(PSP_RenderData *data, size_t wanted)
{
PSP_TextureData *lru = data->least_recent_target;
if (lru) {
if (!TextureSpillToSram(data, lru)) {
return false;
}
LRUTargetRemove(data, lru);
} else {
// Asked to spill but there nothing to spill
return SDL_SetError("Could not spill more VRAM to system memory. VRAM : %dKB,(%dKB), wanted %dKB", vmemavail() / 1024, vlargestblock() / 1024, wanted / 1024);
}
return true;
}
static bool TextureSpillTargetsForSpace(PSP_RenderData *data, size_t size)
{
while (vlargestblock() < size) {
if (!TextureSpillLRU(data, size)) {
return false;
}
}
return true;
}
static bool TextureBindAsTarget(PSP_RenderData *data, PSP_TextureData *psp_texture)
{
unsigned int dstFormat;
if (!InVram(psp_texture->data)) {
// Bring back the texture in vram
if (!TextureSpillTargetsForSpace(data, psp_texture->size)) {
return false;
}
if (!TexturePromoteToVram(data, psp_texture, true)) {
return false;
}
}
LRUTargetBringFront(data, psp_texture);
sceGuDrawBufferList(psp_texture->format, vrelptr(psp_texture->data), psp_texture->textureWidth);
// Stencil alpha dst hack
dstFormat = psp_texture->format;
if (dstFormat == GU_PSM_5551) {
sceGuEnable(GU_STENCIL_TEST);
sceGuStencilOp(GU_REPLACE, GU_REPLACE, GU_REPLACE);
sceGuStencilFunc(GU_GEQUAL, 0xff, 0xff);
sceGuEnable(GU_ALPHA_TEST);
sceGuAlphaFunc(GU_GREATER, 0x00, 0xff);
} else {
sceGuDisable(GU_STENCIL_TEST);
sceGuDisable(GU_ALPHA_TEST);
}
return true;
}
static void PSP_WindowEvent(SDL_Renderer *renderer, const SDL_WindowEvent *event)
{
}
static bool PSP_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_PropertiesID create_props)
{
PSP_RenderData *data = renderer->internal;
PSP_TextureData *psp_texture = (PSP_TextureData *)SDL_calloc(1, sizeof(*psp_texture));
if (!psp_texture) {
return false;
}
psp_texture->swizzled = false;
psp_texture->width = texture->w;
psp_texture->height = texture->h;
psp_texture->textureHeight = TextureNextPow2(texture->h);
psp_texture->textureWidth = TextureNextPow2(texture->w);
psp_texture->format = PixelFormatToPSPFMT(texture->format);
switch (psp_texture->format) {
case GU_PSM_5650:
case GU_PSM_5551:
case GU_PSM_4444:
psp_texture->bits = 16;
break;
case GU_PSM_8888:
psp_texture->bits = 32;
break;
default:
SDL_free(psp_texture);
return false;
}
psp_texture->pitch = psp_texture->textureWidth * SDL_BYTESPERPIXEL(texture->format);
psp_texture->size = psp_texture->textureHeight * psp_texture->pitch;
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
if (!TextureSpillTargetsForSpace(renderer->internal, psp_texture->size)) {
SDL_free(psp_texture);
return false;
}
psp_texture->data = vramalloc(psp_texture->size);
if (psp_texture->data) {
LRUTargetPushFront(data, psp_texture);
}
} else {
psp_texture->data = SDL_calloc(1, psp_texture->size);
}
if (!psp_texture->data) {
SDL_free(psp_texture);
return false;
}
texture->internal = psp_texture;
return true;
}
static bool TextureShouldSwizzle(PSP_TextureData *psp_texture, SDL_Texture *texture)
{
return !((texture->access == SDL_TEXTUREACCESS_TARGET) && InVram(psp_texture->data)) && texture->access != SDL_TEXTUREACCESS_STREAMING && (texture->w >= 16 || texture->h >= 16);
}
static void TextureActivate(SDL_Texture *texture)
{
PSP_TextureData *psp_texture = (PSP_TextureData *)texture->internal;
int scaleMode = (texture->scaleMode == SDL_SCALEMODE_NEAREST) ? GU_NEAREST : GU_LINEAR;
// Swizzling is useless with small textures.
if (TextureShouldSwizzle(psp_texture, texture)) {
TextureSwizzle(psp_texture, NULL);
}
sceGuTexWrap(GU_REPEAT, GU_REPEAT);
sceGuTexMode(psp_texture->format, 0, 0, psp_texture->swizzled);
sceGuTexFilter(scaleMode, scaleMode); // GU_NEAREST good for tile-map
// GU_LINEAR good for scaling
sceGuTexImage(0, psp_texture->textureWidth, psp_texture->textureHeight, psp_texture->textureWidth, psp_texture->data);
}
static bool PSP_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, void **pixels, int *pitch);
static bool PSP_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, const void *pixels, int pitch)
{
/* PSP_TextureData *psp_texture = (PSP_TextureData *) texture->internal; */
const Uint8 *src;
Uint8 *dst;
int row, length, dpitch;
src = pixels;
PSP_LockTexture(renderer, texture, rect, (void **)&dst, &dpitch);
length = rect->w * SDL_BYTESPERPIXEL(texture->format);
if (length == pitch && length == dpitch) {
SDL_memcpy(dst, src, length * rect->h);
} else {
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += dpitch;
}
}
sceKernelDcacheWritebackAll();
return true;
}
static bool PSP_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, void **pixels, int *pitch)
{
PSP_TextureData *psp_texture = (PSP_TextureData *)texture->internal;
*pixels =
(void *)((Uint8 *)psp_texture->data + rect->y * psp_texture->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = psp_texture->pitch;
return true;
}
static void PSP_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
PSP_TextureData *psp_texture = (PSP_TextureData *)texture->internal;
SDL_Rect rect;
// We do whole texture updates, at least for now
rect.x = 0;
rect.y = 0;
rect.w = texture->w;
rect.h = texture->h;
PSP_UpdateTexture(renderer, texture, &rect, psp_texture->data, psp_texture->pitch);
}
static void PSP_SetTextureScaleMode(SDL_Renderer *renderer, SDL_Texture *texture, SDL_ScaleMode scaleMode)
{
// Nothing to do because TextureActivate takes care of it
}
static bool PSP_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
{
return true;
}
static bool PSP_QueueNoOp(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
return true; // nothing to do in this backend.
}
static bool PSP_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
{
VertV *verts = (VertV *)SDL_AllocateRenderVertices(renderer, count * sizeof(VertV), 4, &cmd->data.draw.first);
int i;
if (!verts) {
return false;
}
cmd->data.draw.count = count;
for (i = 0; i < count; i++, verts++, points++) {
verts->x = points->x;
verts->y = points->y;
verts->z = 0.0f;
}
return true;
}
static bool PSP_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const float *xy, int xy_stride, const SDL_FColor *color, int color_stride, const float *uv, int uv_stride,
int num_vertices, const void *indices, int num_indices, int size_indices,
float scale_x, float scale_y)
{
int i;
int count = indices ? num_indices : num_vertices;
const float color_scale = cmd->data.draw.color_scale;
cmd->data.draw.count = count;
size_indices = indices ? size_indices : 0;
if (!texture) {
VertCV *verts;
verts = (VertCV *)SDL_AllocateRenderVertices(renderer, count * sizeof(VertCV), 4, &cmd->data.draw.first);
if (!verts) {
return false;
}
for (i = 0; i < count; i++) {
int j;
float *xy_;
SDL_FColor *col_;
if (size_indices == 4) {
j = ((const Uint32 *)indices)[i];
} else if (size_indices == 2) {
j = ((const Uint16 *)indices)[i];
} else if (size_indices == 1) {
j = ((const Uint8 *)indices)[i];
} else {
j = i;
}
xy_ = (float *)((char *)xy + j * xy_stride);
col_ = (SDL_FColor *)((char *)color + j * color_stride);
verts->x = xy_[0] * scale_x;
verts->y = xy_[1] * scale_y;
verts->z = 0;
verts->col.r = (Uint8)SDL_roundf(SDL_clamp(col_->r * color_scale, 0.0f, 1.0f) * 255.0f);
verts->col.g = (Uint8)SDL_roundf(SDL_clamp(col_->g * color_scale, 0.0f, 1.0f) * 255.0f);
verts->col.b = (Uint8)SDL_roundf(SDL_clamp(col_->b * color_scale, 0.0f, 1.0f) * 255.0f);
verts->col.a = (Uint8)SDL_roundf(SDL_clamp(col_->a, 0.0f, 1.0f) * 255.0f);
verts++;
}
} else {
PSP_TextureData *psp_texture = (PSP_TextureData *)texture->internal;
VertTCV *verts;
verts = (VertTCV *)SDL_AllocateRenderVertices(renderer, count * sizeof(VertTCV), 4, &cmd->data.draw.first);
if (!verts) {
return false;
}
for (i = 0; i < count; i++) {
int j;
float *xy_;
SDL_FColor *col_;
float *uv_;
if (size_indices == 4) {
j = ((const Uint32 *)indices)[i];
} else if (size_indices == 2) {
j = ((const Uint16 *)indices)[i];
} else if (size_indices == 1) {
j = ((const Uint8 *)indices)[i];
} else {
j = i;
}
xy_ = (float *)((char *)xy + j * xy_stride);
col_ = (SDL_FColor *)((char *)color + j * color_stride);
uv_ = (float *)((char *)uv + j * uv_stride);
verts->x = xy_[0] * scale_x;
verts->y = xy_[1] * scale_y;
verts->z = 0;
verts->col.r = (Uint8)SDL_roundf(SDL_clamp(col_->r * color_scale, 0.0f, 1.0f) * 255.0f);
verts->col.g = (Uint8)SDL_roundf(SDL_clamp(col_->g * color_scale, 0.0f, 1.0f) * 255.0f);
verts->col.b = (Uint8)SDL_roundf(SDL_clamp(col_->b * color_scale, 0.0f, 1.0f) * 255.0f);
verts->col.a = (Uint8)SDL_roundf(SDL_clamp(col_->a, 0.0f, 1.0f) * 255.0f);
verts->u = uv_[0] * psp_texture->textureWidth;
verts->v = uv_[1] * psp_texture->textureHeight;
verts++;
}
}
return true;
}
static bool PSP_QueueFillRects(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FRect *rects, int count)
{
VertV *verts = (VertV *)SDL_AllocateRenderVertices(renderer, count * 2 * sizeof(VertV), 4, &cmd->data.draw.first);
int i;
if (!verts) {
return false;
}
cmd->data.draw.count = count;
for (i = 0; i < count; i++, rects++) {
verts->x = rects->x;
verts->y = rects->y;
verts->z = 0.0f;
verts++;
verts->x = rects->x + rects->w + 0.5f;
verts->y = rects->y + rects->h + 0.5f;
verts->z = 0.0f;
verts++;
}
return true;
}
static bool PSP_QueueCopy(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_FRect *srcrect, const SDL_FRect *dstrect)
{
VertTV *verts;
const float x = dstrect->x;
const float y = dstrect->y;
const float width = dstrect->w;
const float height = dstrect->h;
const float u0 = srcrect->x;
const float v0 = srcrect->y;
const float u1 = srcrect->x + srcrect->w;
const float v1 = srcrect->y + srcrect->h;
if ((MathAbs(u1) - MathAbs(u0)) < 64.0f) {
verts = (VertTV *)SDL_AllocateRenderVertices(renderer, 2 * sizeof(VertTV), 4, &cmd->data.draw.first);
if (!verts) {
return false;
}
cmd->data.draw.count = 1;
verts->u = u0;
verts->v = v0;
verts->x = x;
verts->y = y;
verts->z = 0;
verts++;
verts->u = u1;
verts->v = v1;
verts->x = x + width;
verts->y = y + height;
verts->z = 0;
verts++;
} else {
float start, end;
float curU = u0;
float curX = x;
const float endX = x + width;
const float slice = 64.0f;
const size_t count = (size_t)SDL_ceilf(width / slice);
size_t i;
float ustep = (u1 - u0) / width * slice;
if (ustep < 0.0f) {
ustep = -ustep;
}
cmd->data.draw.count = count;
verts = (VertTV *)SDL_AllocateRenderVertices(renderer, count * 2 * sizeof(VertTV), 4, &cmd->data.draw.first);
if (!verts) {
return false;
}
for (i = 0, start = 0, end = width; i < count; i++, start += slice) {
const float polyWidth = ((curX + slice) > endX) ? (endX - curX) : slice;
const float sourceWidth = ((curU + ustep) > u1) ? (u1 - curU) : ustep;
SDL_assert(start < end);
verts->u = curU;
verts->v = v0;
verts->x = curX;
verts->y = y;
verts->z = 0;
verts++;
curU += sourceWidth;
curX += polyWidth;
verts->u = curU;
verts->v = v1;
verts->x = curX;
verts->y = (y + height);
verts->z = 0;
verts++;
}
}
return true;
}
static bool PSP_QueueCopyEx(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_FRect *srcrect, const SDL_FRect *dstrect,
const double angle, const SDL_FPoint *center, const SDL_FlipMode flip, float scale_x, float scale_y)
{
VertTV *verts = (VertTV *)SDL_AllocateRenderVertices(renderer, 4 * sizeof(VertTV), 4, &cmd->data.draw.first);
const float centerx = center->x;
const float centery = center->y;
const float x = dstrect->x + centerx;
const float y = dstrect->y + centery;
const float width = dstrect->w - centerx;
const float height = dstrect->h - centery;
float s, c;
float cw1, sw1, ch1, sh1, cw2, sw2, ch2, sh2;
float u0 = srcrect->x;
float v0 = srcrect->y;
float u1 = srcrect->x + srcrect->w;
float v1 = srcrect->y + srcrect->h;
if (!verts) {
return false;
}
cmd->data.draw.count = 1;
MathSincos(degToRad((float)(360 - angle)), &s, &c);
cw1 = c * -centerx;
sw1 = s * -centerx;
ch1 = c * -centery;
sh1 = s * -centery;
cw2 = c * width;
sw2 = s * width;
ch2 = c * height;
sh2 = s * height;
if (flip & SDL_FLIP_VERTICAL) {
Swap(&v0, &v1);
}
if (flip & SDL_FLIP_HORIZONTAL) {
Swap(&u0, &u1);
}
verts->u = u0;
verts->v = v0;
verts->x = x + cw1 + sh1;
verts->y = y - sw1 + ch1;
verts->z = 0;
verts++;
verts->u = u0;
verts->v = v1;
verts->x = x + cw1 + sh2;
verts->y = y - sw1 + ch2;
verts->z = 0;
verts++;
verts->u = u1;
verts->v = v1;
verts->x = x + cw2 + sh2;
verts->y = y - sw2 + ch2;
verts->z = 0;
verts++;
verts->u = u1;
verts->v = v0;
verts->x = x + cw2 + sh1;
verts->y = y - sw2 + ch1;
verts->z = 0;
if (scale_x != 1.0f || scale_y != 1.0f) {
verts->x *= scale_x;
verts->y *= scale_y;
verts--;
verts->x *= scale_x;
verts->y *= scale_y;
verts--;
verts->x *= scale_x;
verts->y *= scale_y;
verts--;
verts->x *= scale_x;
verts->y *= scale_y;
}
return true;
}
static void ResetBlendState(PSP_BlendState *state)
{
sceGuColor(0xffffffff);
state->color = 0xffffffff;
state->mode = SDL_BLENDMODE_INVALID;
state->texture = NULL;
sceGuDisable(GU_TEXTURE_2D);
sceGuShadeModel(GU_SMOOTH);
state->shadeModel = GU_SMOOTH;
}
static void StartDrawing(SDL_Renderer *renderer)
{
PSP_RenderData *data = (PSP_RenderData *)renderer->internal;
// Check if we need to start GU displaylist
if (!data->displayListAvail) {
sceGuStart(GU_DIRECT, DisplayList);
data->displayListAvail = true;
// ResetBlendState(&data->blendState);
}
// Check if we need a draw buffer change
if (renderer->target != data->boundTarget) {
SDL_Texture *texture = renderer->target;
if (texture) {
PSP_TextureData *psp_texture = (PSP_TextureData *)texture->internal;
// Set target, registering LRU
TextureBindAsTarget(data, psp_texture);
} else {
// Set target back to screen
sceGuDrawBufferList(data->psm, vrelptr(data->frontbuffer), PSP_FRAME_BUFFER_WIDTH);
}
data->boundTarget = texture;
}
}
static void PSP_SetBlendState(PSP_RenderData *data, PSP_BlendState *state)
{
PSP_BlendState *current = &data->blendState;
if (state->mode != current->mode) {
switch (state->mode) {
case SDL_BLENDMODE_NONE:
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
sceGuDisable(GU_BLEND);
break;
case SDL_BLENDMODE_BLEND:
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
sceGuEnable(GU_BLEND);
break;
case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA);
sceGuBlendFunc(GU_ADD, GU_FIX, GU_ONE_MINUS_SRC_ALPHA, 0x00FFFFFF, 0 );
sceGuEnable(GU_BLEND);
break;
case SDL_BLENDMODE_ADD:
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_FIX, 0, 0x00FFFFFF);
sceGuEnable(GU_BLEND);
break;
case SDL_BLENDMODE_ADD_PREMULTIPLIED:
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);