24
24
25
25
#include "../SDL_sysrender.h"
26
26
#include "SDL_hints.h"
27
+ #include "SDL_render_psp.h"
27
28
28
29
#include <pspgu.h>
29
30
#include <pspintrman.h>
30
- #include "SDL_render_psp.h"
31
+ #include <vram.h>
32
+ #include <psputils.h>
33
+ #include <pspdisplay.h>
31
34
32
35
static unsigned int __attribute__((aligned (16 ))) list [262144 ];
33
36
@@ -41,6 +44,7 @@ typedef struct
41
44
{
42
45
void * frontbuffer ; /**< main screen buffer */
43
46
void * backbuffer ; /**< buffer presented to display */
47
+ uint8_t drawBufferFormat ; /**< GU_PSM_8888 or GU_PSM_5650 or GU_PSM_4444 */
44
48
uint64_t drawColor ;
45
49
PSP_BlendInfo blendInfo ; /**< current blend info */
46
50
uint8_t vsync ; /* 0 (Disabled), 1 (Enabled), 2 (Dynamic) */
@@ -90,25 +94,6 @@ typedef struct
90
94
int height ;
91
95
} SliceSize ;
92
96
93
- typedef enum
94
- {
95
- SLICE_PIXEL_BITS_32 ,
96
- SLICE_PIXEL_BITS_16 ,
97
- SLICE_PIXEL_BITS_COUNT
98
- } SlicePixelBits ;
99
-
100
- #define SLICE_VALUES_COUNT 3
101
- typedef struct
102
- {
103
- SlicePixelBits pixelBits ;
104
- SliceSize sizes [SLICE_VALUES_COUNT ];
105
- SliceSize condition ;
106
- } SliceInfo ;
107
-
108
- static SliceInfo sliceInfo [SLICE_PIXEL_BITS_COUNT ] = {
109
- { SLICE_PIXEL_BITS_32 , { { 128 , 16 }, { 64 , 32 }, { 32 , 64 } }, { 32 , 16 } },
110
- { SLICE_PIXEL_BITS_16 , { { 128 , 32 }, { 64 , 64 }, { 32 , 128 } }, { 32 , 32 } }
111
- };
112
97
113
98
int SDL_PSP_RenderGetProp (SDL_Renderer * r , enum SDL_PSP_RenderProps which , void * * out )
114
99
{
@@ -127,7 +112,6 @@ int SDL_PSP_RenderGetProp(SDL_Renderer *r, enum SDL_PSP_RenderProps which, void*
127
112
}
128
113
return -1 ;
129
114
}
130
-
131
115
static int vsync_sema_id = 0 ;
132
116
133
117
/* PRIVATE METHODS */
@@ -203,94 +187,69 @@ static inline int calculateNextPow2(int value)
203
187
return i ;
204
188
}
205
189
206
- static inline int calculateBestSliceSizeForTexture (SDL_Texture * texture , SliceSize * uvSize , SliceSize * sliceSize , SliceSize * sliceDimension ) {
207
- int i ;
208
- uint8_t horizontalSlices , verticalSlices ;
209
- int pixelBits = 0 ;
210
- int pixelSize = SDL_BYTESPERPIXEL (texture -> format );
211
- SliceInfo * sliceInfoPtr = NULL ;
212
- SliceSize * foundSlizeSize = NULL ;
213
-
214
- switch (pixelSize ) {
215
- case 4 :
216
- sliceInfoPtr = & sliceInfo [SLICE_PIXEL_BITS_32 ];
217
- break ;
218
- case 2 :
219
- sliceInfoPtr = & sliceInfo [SLICE_PIXEL_BITS_16 ];
220
- break ;
221
- default :
222
- return -1 ;
223
- }
224
-
225
- if (sliceInfoPtr -> condition .width > uvSize -> width && sliceInfoPtr -> condition .height > uvSize -> height ) {
226
- sliceSize -> width = uvSize -> width ;
227
- sliceSize -> height = uvSize -> height ;
228
- sliceDimension -> width = 1 ;
229
- sliceDimension -> height = 1 ;
230
- return 0 ;
231
- }
190
+ static inline int calculateBestSliceSizeForSprite (SDL_Renderer * renderer , const SDL_FRect * dstrect , SliceSize * sliceSize , SliceSize * sliceDimension ) {
191
+ PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
232
192
233
- if (uvSize -> width >= uvSize -> height ) {
234
- for (i = 0 ; i < SLICE_VALUES_COUNT ; i ++ ) {
235
- if (uvSize -> width >= sliceInfoPtr -> sizes [i ].width ) {
236
- foundSlizeSize = & sliceInfoPtr -> sizes [i ];
237
- break ;
238
- }
239
- }
240
- } else {
241
- for (i = SLICE_VALUES_COUNT - 1 ; i >= 0 ; i -- ) {
242
- if (uvSize -> height >= sliceInfoPtr -> sizes [i ].height ) {
243
- foundSlizeSize = & sliceInfoPtr -> sizes [i ];
244
- break ;
245
- }
246
- }
247
- }
193
+ // We split in blocks of (64 x destiny height) when 16 bits per color
194
+ // or (32 x destiny height) when 32 bits per color
248
195
249
- if (foundSlizeSize == NULL )
196
+ switch (data -> drawBufferFormat ) {
197
+ case GU_PSM_5650 :
198
+ case GU_PSM_5551 :
199
+ case GU_PSM_4444 :
200
+ sliceSize -> width = 64 ;
201
+ break ;
202
+ case GU_PSM_8888 :
203
+ sliceSize -> width = 32 ;
204
+ break ;
205
+ default :
250
206
return -1 ;
207
+ }
208
+ sliceSize -> height = dstrect -> h ;
251
209
252
- sliceSize -> width = foundSlizeSize -> width ;
253
- sliceSize -> height = foundSlizeSize -> height ;
254
- sliceDimension -> width = ((uvSize -> width % foundSlizeSize -> width == 0 ) ? 0 : 1 ) + (uvSize -> width / foundSlizeSize -> width );
255
- sliceDimension -> height = ((uvSize -> height % foundSlizeSize -> height == 0 ) ? 0 : 1 ) + (uvSize -> height / foundSlizeSize -> height );
210
+ sliceDimension -> width = SDL_ceilf (dstrect -> w / sliceSize -> width );
211
+ sliceDimension -> height = SDL_ceilf (dstrect -> h / sliceSize -> height );
256
212
257
213
return 0 ;
258
214
}
259
215
260
216
static inline void fillSpriteVertices (VertTV * vertices , SliceSize * dimensions , SliceSize * sliceSize ,
261
217
const SDL_Rect * srcrect , const SDL_FRect * dstrect ) {
262
218
int i , j ;
263
- int remainingWidth = srcrect -> w % sliceSize -> width ;
264
- int remainingHeight = srcrect -> h % sliceSize -> height ;
219
+ int remainingWidth = ( int ) dstrect -> w % sliceSize -> width ;
220
+ int remainingHeight = ( int ) dstrect -> h % sliceSize -> height ;
265
221
int hasRemainingWidth = remainingWidth > 0 ;
266
222
int hasRemainingHeight = remainingHeight > 0 ;
267
- float dstrectRateWidth = (float )(abs (dstrect -> w - dimensions -> width )) / (float )(abs (srcrect -> w - dimensions -> width ));
268
- float dstrectRateHeight = (float )(abs (dstrect -> h - dimensions -> height )) / (float )(abs (srcrect -> h - dimensions -> height ));
223
+ float srcrectRateWidth = (float )(abs (srcrect -> w - dimensions -> width )) / (float )(abs (dstrect -> w - dimensions -> width ));
224
+ float srcrectRateHeight = (float )(abs (srcrect -> h - dimensions -> height )) / (float )(abs (dstrect -> h - dimensions -> height ));
225
+ float srcWidth = sliceSize -> width * srcrectRateWidth ;
226
+ float srcHeight = sliceSize -> height * srcrectRateHeight ;
227
+ float remainingSrcWidth = remainingWidth * srcrectRateWidth ;
228
+ float remainingSrcHeight = remainingHeight * srcrectRateHeight ;
269
229
270
- int verticesCount = dimensions -> width * dimensions -> height * 2 ;
271
230
for (i = 0 ; i < dimensions -> width ; i ++ ) {
272
231
for (j = 0 ; j < dimensions -> height ; j ++ ) {
273
232
uint8_t currentIndex = (i * dimensions -> height + j ) * 2 ;
274
- vertices [currentIndex ].u = srcrect -> x + i * sliceSize -> width ;
275
- vertices [currentIndex ].v = srcrect -> y + j * sliceSize -> height ;
276
- vertices [currentIndex ].x = dstrect -> x + i * sliceSize -> width * dstrectRateWidth ;
277
- vertices [currentIndex ].y = dstrect -> y + j * sliceSize -> height * dstrectRateHeight ;
233
+ vertices [currentIndex ].u = srcrect -> x + i * srcWidth ;
234
+ vertices [currentIndex ].v = srcrect -> y + j * srcHeight ;
235
+ vertices [currentIndex ].x = dstrect -> x + i * sliceSize -> width ;
236
+ vertices [currentIndex ].y = dstrect -> y + j * sliceSize -> height ;
278
237
vertices [currentIndex ].z = 0 ;
279
238
280
239
if (i == dimensions -> width - 1 && hasRemainingWidth ) {
281
- vertices [currentIndex + 1 ].u = srcrect -> x + i * sliceSize -> width + remainingWidth ;
282
- vertices [currentIndex + 1 ].x = dstrect -> x + i * ( sliceSize -> width * dstrectRateWidth ) + remainingWidth * dstrectRateWidth ;
240
+ vertices [currentIndex + 1 ].u = vertices [ currentIndex ]. u + remainingSrcWidth ;
241
+ vertices [currentIndex + 1 ].x = vertices [ currentIndex ]. x + remainingWidth ;
283
242
} else {
284
- vertices [currentIndex + 1 ].u = ( srcrect -> x + ( i + 1 ) * sliceSize -> width ) ;
285
- vertices [currentIndex + 1 ].x = dstrect -> x + ( i + 1 ) * sliceSize -> width * dstrectRateWidth ;
243
+ vertices [currentIndex + 1 ].u = vertices [ currentIndex ]. u + srcWidth ;
244
+ vertices [currentIndex + 1 ].x = vertices [ currentIndex ]. x + sliceSize -> width ;
286
245
}
287
246
288
247
if (j == dimensions -> height - 1 && hasRemainingHeight ) {
289
- vertices [currentIndex + 1 ].v = srcrect -> y + j * sliceSize -> height + remainingHeight ;
290
- vertices [currentIndex + 1 ].y = dstrect -> y + j * sliceSize -> height * dstrectRateHeight + remainingHeight * dstrectRateHeight ;
248
+ vertices [currentIndex + 1 ].v = vertices [ currentIndex ]. v + remainingSrcHeight ;
249
+ vertices [currentIndex + 1 ].y = vertices [ currentIndex ]. y + remainingHeight ;
291
250
} else {
292
- vertices [currentIndex + 1 ].v = ( srcrect -> y + ( j + 1 ) * sliceSize -> height ) ;
293
- vertices [currentIndex + 1 ].y = dstrect -> y + ( j + 1 ) * sliceSize -> height * dstrectRateHeight ;
251
+ vertices [currentIndex + 1 ].v = vertices [ currentIndex ]. v + srcHeight ;
252
+ vertices [currentIndex + 1 ].y = vertices [ currentIndex ]. y + sliceSize -> height ;
294
253
}
295
254
296
255
vertices [currentIndex + 1 ].z = 0 ;
@@ -344,7 +303,6 @@ static int PSP_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
344
303
static void PSP_UnlockTexture (SDL_Renderer * renderer , SDL_Texture * texture )
345
304
{
346
305
PSP_Texture * psp_texture = (PSP_Texture * )texture -> driverdata ;
347
- PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
348
306
349
307
sceKernelDcacheWritebackRange (psp_texture -> data , psp_texture -> size );
350
308
}
@@ -396,7 +354,6 @@ static int PSP_QueueSetViewport(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
396
354
397
355
static int PSP_QueueDrawPoints (SDL_Renderer * renderer , SDL_RenderCommand * cmd , const SDL_FPoint * points , int count )
398
356
{
399
- PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
400
357
VertV * verts = (VertV * )SDL_AllocateRenderVertices (renderer , count * sizeof (VertV ), 4 , & cmd -> data .draw .first );
401
358
int i ;
402
359
@@ -422,7 +379,6 @@ static int PSP_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL
422
379
{
423
380
int i ;
424
381
int count = indices ? num_indices : num_vertices ;
425
- PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
426
382
427
383
cmd -> data .draw .count = count ;
428
384
size_indices = indices ? size_indices : 0 ;
@@ -535,25 +491,14 @@ static int PSP_QueueCopy(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Tex
535
491
{
536
492
VertTV * verts ;
537
493
uint8_t verticesCount ;
538
- const float x = dstrect -> x ;
539
- const float y = dstrect -> y ;
540
- const float width = dstrect -> w ;
541
- const float height = dstrect -> h ;
542
494
543
- const float u0 = srcrect -> x ;
544
- const float v0 = srcrect -> y ;
545
- const float u1 = srcrect -> x + srcrect -> w ;
546
- const float v1 = srcrect -> y + srcrect -> h ;
547
-
548
- SliceSize sliceSize , sliceDimension , uvSize ;
495
+ SliceSize sliceSize , sliceDimension ;
549
496
550
497
// In this function texture must be created
551
498
if (!texture )
552
499
return -1 ;
553
500
554
- uvSize .width = abs (u1 - u0 );
555
- uvSize .height = abs (v1 - v0 );
556
- if (calculateBestSliceSizeForTexture (texture , & uvSize , & sliceSize , & sliceDimension ))
501
+ if (calculateBestSliceSizeForSprite (renderer , dstrect , & sliceSize , & sliceDimension ))
557
502
return -1 ;
558
503
559
504
verticesCount = sliceDimension .width * sliceDimension .height * 2 ;
@@ -570,7 +515,6 @@ static int PSP_QueueCopy(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Tex
570
515
571
516
static int PSP_RenderSetViewPort (SDL_Renderer * renderer , SDL_RenderCommand * cmd )
572
517
{
573
- PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
574
518
const SDL_Rect * viewport = & cmd -> data .viewport .rect ;
575
519
576
520
sceGuOffset (2048 - (viewport -> w >> 1 ), 2048 - (viewport -> h >> 1 ));
@@ -635,8 +579,6 @@ static void PSP_SetBlendMode(PSP_RenderData *data, PSP_BlendInfo blendInfo)
635
579
636
580
static inline int PSP_RenderSetClipRect (SDL_Renderer * renderer , SDL_RenderCommand * cmd )
637
581
{
638
- PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
639
-
640
582
const SDL_Rect * rect = & cmd -> data .cliprect .rect ;
641
583
642
584
if (cmd -> data .cliprect .enabled ) {
@@ -653,8 +595,6 @@ static inline int PSP_RenderSetDrawColor(SDL_Renderer *renderer, SDL_RenderComma
653
595
{
654
596
uint8_t colorR , colorG , colorB , colorA ;
655
597
656
- PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
657
-
658
598
colorR = cmd -> data .color .r ;
659
599
colorG = cmd -> data .color .g ;
660
600
colorB = cmd -> data .color .b ;
@@ -668,8 +608,6 @@ static inline int PSP_RenderClear(SDL_Renderer *renderer, SDL_RenderCommand *cmd
668
608
{
669
609
uint8_t colorR , colorG , colorB , colorA ;
670
610
671
- PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
672
-
673
611
colorR = cmd -> data .color .r ;
674
612
colorG = cmd -> data .color .g ;
675
613
colorB = cmd -> data .color .b ;
@@ -763,6 +701,7 @@ static inline int PSP_RenderPoints(SDL_Renderer *renderer, void *vertices, SDL_R
763
701
static inline int PSP_RenderCopy (SDL_Renderer * renderer , void * vertices , SDL_RenderCommand * cmd )
764
702
{
765
703
PSP_RenderData * data = (PSP_RenderData * )renderer -> driverdata ;
704
+ PSP_Texture * psp_tex = (PSP_Texture * )cmd -> data .draw .texture -> driverdata ;
766
705
const size_t count = cmd -> data .draw .count ;
767
706
const VertTV * verts = (VertTV * )(vertices + cmd -> data .draw .first );
768
707
PSP_BlendInfo blendInfo = {
@@ -772,8 +711,6 @@ static inline int PSP_RenderCopy(SDL_Renderer *renderer, void *vertices, SDL_Ren
772
711
773
712
PSP_SetBlendMode (data , blendInfo );
774
713
775
- PSP_Texture * psp_tex = (PSP_Texture * )cmd -> data .draw .texture -> driverdata ;
776
-
777
714
sceGuTexMode (psp_tex -> format , 0 , 0 , GU_FALSE );
778
715
sceGuTexImage (0 , psp_tex -> textureWidth , psp_tex -> textureHeight , psp_tex -> width , psp_tex -> data );
779
716
sceGuTexFilter (psp_tex -> filter , psp_tex -> filter );
@@ -948,15 +885,18 @@ static int PSP_CreateRenderer(SDL_Renderer *renderer, SDL_Window *window, Uint32
948
885
// flush cache so that no stray data remains
949
886
sceKernelDcacheWritebackAll ();
950
887
888
+ // Forcing for now using GU_PSM_4444
889
+ data -> drawBufferFormat = GU_PSM_4444 ;
890
+
951
891
/* Specific GU init */
952
- bufferSize = getMemorySize (PSP_FRAME_BUFFER_WIDTH , PSP_SCREEN_HEIGHT , GU_PSM_4444 );
892
+ bufferSize = getMemorySize (PSP_FRAME_BUFFER_WIDTH , PSP_SCREEN_HEIGHT , data -> drawBufferFormat );
953
893
doublebuffer = vramalloc (bufferSize * 2 );
954
894
data -> backbuffer = doublebuffer ;
955
895
data -> frontbuffer = ((uint8_t * )doublebuffer ) + bufferSize ;
956
896
957
897
sceGuInit ();
958
898
sceGuStart (GU_DIRECT , list );
959
- sceGuDrawBuffer (GU_PSM_4444 , vrelptr (data -> frontbuffer ), PSP_FRAME_BUFFER_WIDTH );
899
+ sceGuDrawBuffer (data -> drawBufferFormat , vrelptr (data -> frontbuffer ), PSP_FRAME_BUFFER_WIDTH );
960
900
sceGuDispBuffer (PSP_SCREEN_WIDTH , PSP_SCREEN_HEIGHT , vrelptr (data -> backbuffer ), PSP_FRAME_BUFFER_WIDTH );
961
901
962
902
sceGuOffset (2048 - (PSP_SCREEN_WIDTH >> 1 ), 2048 - (PSP_SCREEN_HEIGHT >> 1 ));
0 commit comments