SDL_render_psp.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if SDL_VIDEO_RENDER_PSP
  20. #include "../SDL_sysrender.h"
  21. #include <pspkernel.h>
  22. #include <pspdisplay.h>
  23. #include <pspgu.h>
  24. #include <pspgum.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include <pspge.h>
  29. #include <stdarg.h>
  30. #include <stdlib.h>
  31. #include <vram.h>
  32. /* PSP renderer implementation, based on the PGE */
  33. #define PSP_SCREEN_WIDTH 480
  34. #define PSP_SCREEN_HEIGHT 272
  35. #define PSP_FRAME_BUFFER_WIDTH 512
  36. #define PSP_FRAME_BUFFER_SIZE (PSP_FRAME_BUFFER_WIDTH * PSP_SCREEN_HEIGHT)
  37. static unsigned int __attribute__((aligned(16))) DisplayList[262144];
  38. #define COL5650(r, g, b, a) ((r >> 3) | ((g >> 2) << 5) | ((b >> 3) << 11))
  39. #define COL5551(r, g, b, a) ((r >> 3) | ((g >> 3) << 5) | ((b >> 3) << 10) | (a > 0 ? 0x7000 : 0))
  40. #define COL4444(r, g, b, a) ((r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8) | ((a >> 4) << 12))
  41. #define COL8888(r, g, b, a) ((r) | ((g) << 8) | ((b) << 16) | ((a) << 24))
  42. /**
  43. * Holds psp specific texture data
  44. *
  45. * Part of a hot-list of textures that are used as render targets
  46. * When short of vram we spill Least-Recently-Used render targets to system memory
  47. */
  48. typedef struct PSP_TextureData
  49. {
  50. void *data; /**< Image data. */
  51. unsigned int size; /**< Size of data in bytes. */
  52. unsigned int width; /**< Image width. */
  53. unsigned int height; /**< Image height. */
  54. unsigned int textureWidth; /**< Texture width (power of two). */
  55. unsigned int textureHeight; /**< Texture height (power of two). */
  56. unsigned int bits; /**< Image bits per pixel. */
  57. unsigned int format; /**< Image format - one of ::pgePixelFormat. */
  58. unsigned int pitch;
  59. SDL_bool swizzled; /**< Is image swizzled. */
  60. struct PSP_TextureData *prevhotw; /**< More recently used render target */
  61. struct PSP_TextureData *nexthotw; /**< Less recently used render target */
  62. } PSP_TextureData;
  63. typedef struct
  64. {
  65. SDL_BlendMode mode;
  66. unsigned int color;
  67. int shadeModel;
  68. SDL_Texture *texture;
  69. } PSP_BlendState;
  70. typedef struct
  71. {
  72. void *frontbuffer; /**< main screen buffer */
  73. void *backbuffer; /**< buffer presented to display */
  74. SDL_Texture *boundTarget; /**< currently bound rendertarget */
  75. SDL_bool initialized; /**< is driver initialized */
  76. SDL_bool displayListAvail; /**< is the display list already initialized for this frame */
  77. unsigned int psm; /**< format of the display buffers */
  78. unsigned int bpp; /**< bits per pixel of the main display */
  79. SDL_bool vsync; /**< whether we do vsync */
  80. PSP_BlendState blendState; /**< current blend mode */
  81. PSP_TextureData *most_recent_target; /**< start of render target LRU double linked list */
  82. PSP_TextureData *least_recent_target; /**< end of the LRU list */
  83. SDL_bool vblank_not_reached; /**< whether vblank wasn't reached */
  84. } PSP_RenderData;
  85. typedef struct
  86. {
  87. float x, y, z;
  88. } VertV;
  89. typedef struct
  90. {
  91. float u, v;
  92. float x, y, z;
  93. } VertTV;
  94. typedef struct
  95. {
  96. SDL_Color col;
  97. float x, y, z;
  98. } VertCV;
  99. typedef struct
  100. {
  101. float u, v;
  102. SDL_Color col;
  103. float x, y, z;
  104. } VertTCV;
  105. #define PI 3.14159265358979f
  106. #define radToDeg(x) ((x)*180.f / PI)
  107. #define degToRad(x) ((x)*PI / 180.f)
  108. static float MathAbs(float x)
  109. {
  110. float result;
  111. __asm__ volatile(
  112. "mtv %1, S000\n"
  113. "vabs.s S000, S000\n"
  114. "mfv %0, S000\n"
  115. : "=r"(result)
  116. : "r"(x));
  117. return result;
  118. }
  119. static void MathSincos(float r, float *s, float *c)
  120. {
  121. __asm__ volatile(
  122. "mtv %2, S002\n"
  123. "vcst.s S003, VFPU_2_PI\n"
  124. "vmul.s S002, S002, S003\n"
  125. "vrot.p C000, S002, [s, c]\n"
  126. "mfv %0, S000\n"
  127. "mfv %1, S001\n"
  128. : "=r"(*s), "=r"(*c)
  129. : "r"(r));
  130. }
  131. static void Swap(float *a, float *b)
  132. {
  133. float n = *a;
  134. *a = *b;
  135. *b = n;
  136. }
  137. static inline int InVram(void *data)
  138. {
  139. return data < (void *)0x04200000;
  140. }
  141. /* Return next power of 2 */
  142. static int TextureNextPow2(unsigned int w)
  143. {
  144. unsigned int n = 2;
  145. if (w == 0) {
  146. return 0;
  147. }
  148. while (w > n) {
  149. n <<= 1;
  150. }
  151. return n;
  152. }
  153. static void psp_on_vblank(u32 sub, PSP_RenderData *data)
  154. {
  155. if (data) {
  156. data->vblank_not_reached = SDL_FALSE;
  157. }
  158. }
  159. static int PixelFormatToPSPFMT(Uint32 format)
  160. {
  161. switch (format) {
  162. case SDL_PIXELFORMAT_BGR565:
  163. return GU_PSM_5650;
  164. case SDL_PIXELFORMAT_ABGR1555:
  165. return GU_PSM_5551;
  166. case SDL_PIXELFORMAT_ABGR4444:
  167. return GU_PSM_4444;
  168. case SDL_PIXELFORMAT_ABGR8888:
  169. return GU_PSM_8888;
  170. default:
  171. return GU_PSM_8888;
  172. }
  173. }
  174. /// SECTION render target LRU management
  175. static void LRUTargetRelink(PSP_TextureData *psp_texture)
  176. {
  177. if (psp_texture->prevhotw) {
  178. psp_texture->prevhotw->nexthotw = psp_texture->nexthotw;
  179. }
  180. if (psp_texture->nexthotw) {
  181. psp_texture->nexthotw->prevhotw = psp_texture->prevhotw;
  182. }
  183. }
  184. static void LRUTargetPushFront(PSP_RenderData *data, PSP_TextureData *psp_texture)
  185. {
  186. psp_texture->nexthotw = data->most_recent_target;
  187. if (data->most_recent_target) {
  188. data->most_recent_target->prevhotw = psp_texture;
  189. }
  190. data->most_recent_target = psp_texture;
  191. if (!data->least_recent_target) {
  192. data->least_recent_target = psp_texture;
  193. }
  194. }
  195. static void LRUTargetRemove(PSP_RenderData *data, PSP_TextureData *psp_texture)
  196. {
  197. LRUTargetRelink(psp_texture);
  198. if (data->most_recent_target == psp_texture) {
  199. data->most_recent_target = psp_texture->nexthotw;
  200. }
  201. if (data->least_recent_target == psp_texture) {
  202. data->least_recent_target = psp_texture->prevhotw;
  203. }
  204. psp_texture->prevhotw = NULL;
  205. psp_texture->nexthotw = NULL;
  206. }
  207. static void LRUTargetBringFront(PSP_RenderData *data, PSP_TextureData *psp_texture)
  208. {
  209. if (data->most_recent_target == psp_texture) {
  210. return; // nothing to do
  211. }
  212. LRUTargetRemove(data, psp_texture);
  213. LRUTargetPushFront(data, psp_texture);
  214. }
  215. static void TextureStorageFree(void *storage)
  216. {
  217. if (InVram(storage)) {
  218. vfree(storage);
  219. } else {
  220. SDL_free(storage);
  221. }
  222. }
  223. static int TextureSwizzle(PSP_TextureData *psp_texture, void *dst)
  224. {
  225. int bytewidth, height;
  226. int rowblocks, rowblocksadd;
  227. int i, j;
  228. unsigned int blockaddress = 0;
  229. unsigned int *src = NULL;
  230. unsigned char *data = NULL;
  231. if (psp_texture->swizzled) {
  232. return 1;
  233. }
  234. bytewidth = psp_texture->textureWidth * (psp_texture->bits >> 3);
  235. height = psp_texture->size / bytewidth;
  236. rowblocks = (bytewidth >> 4);
  237. rowblocksadd = (rowblocks - 1) << 7;
  238. src = (unsigned int *)psp_texture->data;
  239. data = dst;
  240. if (data == NULL) {
  241. data = SDL_malloc(psp_texture->size);
  242. }
  243. if (data == NULL) {
  244. return SDL_OutOfMemory();
  245. }
  246. for (j = 0; j < height; j++, blockaddress += 16) {
  247. unsigned int *block;
  248. block = (unsigned int *)&data[blockaddress];
  249. for (i = 0; i < rowblocks; i++) {
  250. *block++ = *src++;
  251. *block++ = *src++;
  252. *block++ = *src++;
  253. *block++ = *src++;
  254. block += 28;
  255. }
  256. if ((j & 0x7) == 0x7) {
  257. blockaddress += rowblocksadd;
  258. }
  259. }
  260. TextureStorageFree(psp_texture->data);
  261. psp_texture->data = data;
  262. psp_texture->swizzled = SDL_TRUE;
  263. sceKernelDcacheWritebackRange(psp_texture->data, psp_texture->size);
  264. return 1;
  265. }
  266. static int TextureUnswizzle(PSP_TextureData *psp_texture, void *dst)
  267. {
  268. int bytewidth, height;
  269. int widthblocks, heightblocks;
  270. int dstpitch, dstrow;
  271. int blockx, blocky;
  272. int j;
  273. unsigned int *src = NULL;
  274. unsigned char *data = NULL;
  275. unsigned char *ydst = NULL;
  276. if (!psp_texture->swizzled) {
  277. return 1;
  278. }
  279. bytewidth = psp_texture->textureWidth * (psp_texture->bits >> 3);
  280. height = psp_texture->size / bytewidth;
  281. widthblocks = bytewidth / 16;
  282. heightblocks = height / 8;
  283. dstpitch = (bytewidth - 16) / 4;
  284. dstrow = bytewidth * 8;
  285. src = (unsigned int *)psp_texture->data;
  286. data = dst;
  287. if (data == NULL) {
  288. data = SDL_malloc(psp_texture->size);
  289. }
  290. if (data == NULL) {
  291. return SDL_OutOfMemory();
  292. }
  293. ydst = (unsigned char *)data;
  294. for (blocky = 0; blocky < heightblocks; ++blocky) {
  295. unsigned char *xdst = ydst;
  296. for (blockx = 0; blockx < widthblocks; ++blockx) {
  297. unsigned int *block;
  298. block = (unsigned int *)xdst;
  299. for (j = 0; j < 8; ++j) {
  300. *(block++) = *(src++);
  301. *(block++) = *(src++);
  302. *(block++) = *(src++);
  303. *(block++) = *(src++);
  304. block += dstpitch;
  305. }
  306. xdst += 16;
  307. }
  308. ydst += dstrow;
  309. }
  310. TextureStorageFree(psp_texture->data);
  311. psp_texture->data = data;
  312. psp_texture->swizzled = SDL_FALSE;
  313. sceKernelDcacheWritebackRange(psp_texture->data, psp_texture->size);
  314. return 1;
  315. }
  316. static int TextureSpillToSram(PSP_RenderData *data, PSP_TextureData *psp_texture)
  317. {
  318. // Assumes the texture is in VRAM
  319. if (psp_texture->swizzled) {
  320. // Texture was swizzled in vram, just copy to system memory
  321. void *sdata = SDL_malloc(psp_texture->size);
  322. if (sdata == NULL) {
  323. return SDL_OutOfMemory();
  324. }
  325. SDL_memcpy(sdata, psp_texture->data, psp_texture->size);
  326. vfree(psp_texture->data);
  327. psp_texture->data = sdata;
  328. return 0;
  329. } else {
  330. return TextureSwizzle(psp_texture, NULL); // Will realloc in sysram
  331. }
  332. }
  333. static int TexturePromoteToVram(PSP_RenderData *data, PSP_TextureData *psp_texture, SDL_bool target)
  334. {
  335. // Assumes texture in sram and a large enough continuous block in vram
  336. void *tdata = vramalloc(psp_texture->size);
  337. if (psp_texture->swizzled && target) {
  338. return TextureUnswizzle(psp_texture, tdata);
  339. } else {
  340. SDL_memcpy(tdata, psp_texture->data, psp_texture->size);
  341. SDL_free(psp_texture->data);
  342. psp_texture->data = tdata;
  343. return 0;
  344. }
  345. }
  346. static int TextureSpillLRU(PSP_RenderData *data, size_t wanted)
  347. {
  348. PSP_TextureData *lru = data->least_recent_target;
  349. if (lru) {
  350. if (TextureSpillToSram(data, lru) < 0) {
  351. return -1;
  352. }
  353. LRUTargetRemove(data, lru);
  354. } else {
  355. // Asked to spill but there nothing to spill
  356. return SDL_SetError("Could not spill more VRAM to system memory. VRAM : %dKB,(%dKB), wanted %dKB", vmemavail() / 1024, vlargestblock() / 1024, wanted / 1024);
  357. }
  358. return 0;
  359. }
  360. static int TextureSpillTargetsForSpace(PSP_RenderData *data, size_t size)
  361. {
  362. while (vlargestblock() < size) {
  363. if (TextureSpillLRU(data, size) < 0) {
  364. return -1;
  365. }
  366. }
  367. return 0;
  368. }
  369. static int TextureBindAsTarget(PSP_RenderData *data, PSP_TextureData *psp_texture)
  370. {
  371. unsigned int dstFormat;
  372. if (!InVram(psp_texture->data)) {
  373. // Bring back the texture in vram
  374. if (TextureSpillTargetsForSpace(data, psp_texture->size) < 0) {
  375. return -1;
  376. }
  377. if (TexturePromoteToVram(data, psp_texture, SDL_TRUE) < 0) {
  378. return -1;
  379. }
  380. }
  381. LRUTargetBringFront(data, psp_texture);
  382. sceGuDrawBufferList(psp_texture->format, vrelptr(psp_texture->data), psp_texture->textureWidth);
  383. // Stencil alpha dst hack
  384. dstFormat = psp_texture->format;
  385. if (dstFormat == GU_PSM_5551) {
  386. sceGuEnable(GU_STENCIL_TEST);
  387. sceGuStencilOp(GU_REPLACE, GU_REPLACE, GU_REPLACE);
  388. sceGuStencilFunc(GU_GEQUAL, 0xff, 0xff);
  389. sceGuEnable(GU_ALPHA_TEST);
  390. sceGuAlphaFunc(GU_GREATER, 0x00, 0xff);
  391. } else {
  392. sceGuDisable(GU_STENCIL_TEST);
  393. sceGuDisable(GU_ALPHA_TEST);
  394. }
  395. return 0;
  396. }
  397. static void PSP_WindowEvent(SDL_Renderer *renderer, const SDL_WindowEvent *event)
  398. {
  399. }
  400. static int PSP_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  401. {
  402. PSP_RenderData *data = renderer->driverdata;
  403. PSP_TextureData *psp_texture = (PSP_TextureData *)SDL_calloc(1, sizeof(*psp_texture));
  404. if (psp_texture == NULL) {
  405. return SDL_OutOfMemory();
  406. }
  407. psp_texture->swizzled = SDL_FALSE;
  408. psp_texture->width = texture->w;
  409. psp_texture->height = texture->h;
  410. psp_texture->textureHeight = TextureNextPow2(texture->h);
  411. psp_texture->textureWidth = TextureNextPow2(texture->w);
  412. psp_texture->format = PixelFormatToPSPFMT(texture->format);
  413. switch (psp_texture->format) {
  414. case GU_PSM_5650:
  415. case GU_PSM_5551:
  416. case GU_PSM_4444:
  417. psp_texture->bits = 16;
  418. break;
  419. case GU_PSM_8888:
  420. psp_texture->bits = 32;
  421. break;
  422. default:
  423. SDL_free(psp_texture);
  424. return -1;
  425. }
  426. psp_texture->pitch = psp_texture->textureWidth * SDL_BYTESPERPIXEL(texture->format);
  427. psp_texture->size = psp_texture->textureHeight * psp_texture->pitch;
  428. if (texture->access & SDL_TEXTUREACCESS_TARGET) {
  429. if (TextureSpillTargetsForSpace(renderer->driverdata, psp_texture->size) < 0) {
  430. SDL_free(psp_texture);
  431. return -1;
  432. }
  433. psp_texture->data = vramalloc(psp_texture->size);
  434. if (psp_texture->data) {
  435. LRUTargetPushFront(data, psp_texture);
  436. }
  437. } else {
  438. psp_texture->data = SDL_calloc(1, psp_texture->size);
  439. }
  440. if (!psp_texture->data) {
  441. SDL_free(psp_texture);
  442. return SDL_OutOfMemory();
  443. }
  444. texture->driverdata = psp_texture;
  445. return 0;
  446. }
  447. static int TextureShouldSwizzle(PSP_TextureData *psp_texture, SDL_Texture *texture)
  448. {
  449. return !((texture->access == SDL_TEXTUREACCESS_TARGET) && InVram(psp_texture->data)) && texture->access != SDL_TEXTUREACCESS_STREAMING && (texture->w >= 16 || texture->h >= 16);
  450. }
  451. static void TextureActivate(SDL_Texture *texture)
  452. {
  453. PSP_TextureData *psp_texture = (PSP_TextureData *)texture->driverdata;
  454. int scaleMode = (texture->scaleMode == SDL_SCALEMODE_NEAREST) ? GU_NEAREST : GU_LINEAR;
  455. /* Swizzling is useless with small textures. */
  456. if (TextureShouldSwizzle(psp_texture, texture)) {
  457. TextureSwizzle(psp_texture, NULL);
  458. }
  459. sceGuTexWrap(GU_REPEAT, GU_REPEAT);
  460. sceGuTexMode(psp_texture->format, 0, 0, psp_texture->swizzled);
  461. sceGuTexFilter(scaleMode, scaleMode); /* GU_NEAREST good for tile-map */
  462. /* GU_LINEAR good for scaling */
  463. sceGuTexImage(0, psp_texture->textureWidth, psp_texture->textureHeight, psp_texture->textureWidth, psp_texture->data);
  464. }
  465. static int PSP_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
  466. const SDL_Rect *rect, void **pixels, int *pitch);
  467. static int PSP_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
  468. const SDL_Rect *rect, const void *pixels, int pitch)
  469. {
  470. /* PSP_TextureData *psp_texture = (PSP_TextureData *) texture->driverdata; */
  471. const Uint8 *src;
  472. Uint8 *dst;
  473. int row, length, dpitch;
  474. src = pixels;
  475. PSP_LockTexture(renderer, texture, rect, (void **)&dst, &dpitch);
  476. length = rect->w * SDL_BYTESPERPIXEL(texture->format);
  477. if (length == pitch && length == dpitch) {
  478. SDL_memcpy(dst, src, length * rect->h);
  479. } else {
  480. for (row = 0; row < rect->h; ++row) {
  481. SDL_memcpy(dst, src, length);
  482. src += pitch;
  483. dst += dpitch;
  484. }
  485. }
  486. sceKernelDcacheWritebackAll();
  487. return 0;
  488. }
  489. static int PSP_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
  490. const SDL_Rect *rect, void **pixels, int *pitch)
  491. {
  492. PSP_TextureData *psp_texture = (PSP_TextureData *)texture->driverdata;
  493. *pixels =
  494. (void *)((Uint8 *)psp_texture->data + rect->y * psp_texture->pitch +
  495. rect->x * SDL_BYTESPERPIXEL(texture->format));
  496. *pitch = psp_texture->pitch;
  497. return 0;
  498. }
  499. static void PSP_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  500. {
  501. PSP_TextureData *psp_texture = (PSP_TextureData *)texture->driverdata;
  502. SDL_Rect rect;
  503. /* We do whole texture updates, at least for now */
  504. rect.x = 0;
  505. rect.y = 0;
  506. rect.w = texture->w;
  507. rect.h = texture->h;
  508. PSP_UpdateTexture(renderer, texture, &rect, psp_texture->data, psp_texture->pitch);
  509. }
  510. static void PSP_SetTextureScaleMode(SDL_Renderer *renderer, SDL_Texture *texture, SDL_ScaleMode scaleMode)
  511. {
  512. /* Nothing to do because TextureActivate takes care of it */
  513. }
  514. static int PSP_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  515. {
  516. return 0;
  517. }
  518. static int PSP_QueueSetViewport(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
  519. {
  520. return 0; /* nothing to do in this backend. */
  521. }
  522. static int PSP_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
  523. {
  524. VertV *verts = (VertV *)SDL_AllocateRenderVertices(renderer, count * sizeof(VertV), 4, &cmd->data.draw.first);
  525. int i;
  526. if (verts == NULL) {
  527. return -1;
  528. }
  529. cmd->data.draw.count = count;
  530. for (i = 0; i < count; i++, verts++, points++) {
  531. verts->x = points->x;
  532. verts->y = points->y;
  533. verts->z = 0.0f;
  534. }
  535. return 0;
  536. }
  537. static int PSP_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  538. const float *xy, int xy_stride, const SDL_Color *color, int color_stride, const float *uv, int uv_stride,
  539. int num_vertices, const void *indices, int num_indices, int size_indices,
  540. float scale_x, float scale_y)
  541. {
  542. int i;
  543. int count = indices ? num_indices : num_vertices;
  544. cmd->data.draw.count = count;
  545. size_indices = indices ? size_indices : 0;
  546. if (texture == NULL) {
  547. VertCV *verts;
  548. verts = (VertCV *)SDL_AllocateRenderVertices(renderer, count * sizeof(VertCV), 4, &cmd->data.draw.first);
  549. if (verts == NULL) {
  550. return -1;
  551. }
  552. for (i = 0; i < count; i++) {
  553. int j;
  554. float *xy_;
  555. SDL_Color col_;
  556. if (size_indices == 4) {
  557. j = ((const Uint32 *)indices)[i];
  558. } else if (size_indices == 2) {
  559. j = ((const Uint16 *)indices)[i];
  560. } else if (size_indices == 1) {
  561. j = ((const Uint8 *)indices)[i];
  562. } else {
  563. j = i;
  564. }
  565. xy_ = (float *)((char *)xy + j * xy_stride);
  566. col_ = *(SDL_Color *)((char *)color + j * color_stride);
  567. verts->x = xy_[0] * scale_x;
  568. verts->y = xy_[1] * scale_y;
  569. verts->z = 0;
  570. verts->col = col_;
  571. verts++;
  572. }
  573. } else {
  574. PSP_TextureData *psp_texture = (PSP_TextureData *)texture->driverdata;
  575. VertTCV *verts;
  576. verts = (VertTCV *)SDL_AllocateRenderVertices(renderer, count * sizeof(VertTCV), 4, &cmd->data.draw.first);
  577. if (verts == NULL) {
  578. return -1;
  579. }
  580. for (i = 0; i < count; i++) {
  581. int j;
  582. float *xy_;
  583. SDL_Color col_;
  584. float *uv_;
  585. if (size_indices == 4) {
  586. j = ((const Uint32 *)indices)[i];
  587. } else if (size_indices == 2) {
  588. j = ((const Uint16 *)indices)[i];
  589. } else if (size_indices == 1) {
  590. j = ((const Uint8 *)indices)[i];
  591. } else {
  592. j = i;
  593. }
  594. xy_ = (float *)((char *)xy + j * xy_stride);
  595. col_ = *(SDL_Color *)((char *)color + j * color_stride);
  596. uv_ = (float *)((char *)uv + j * uv_stride);
  597. verts->x = xy_[0] * scale_x;
  598. verts->y = xy_[1] * scale_y;
  599. verts->z = 0;
  600. verts->col = col_;
  601. verts->u = uv_[0] * psp_texture->textureWidth;
  602. verts->v = uv_[1] * psp_texture->textureHeight;
  603. verts++;
  604. }
  605. }
  606. return 0;
  607. }
  608. static int PSP_QueueFillRects(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FRect *rects, int count)
  609. {
  610. VertV *verts = (VertV *)SDL_AllocateRenderVertices(renderer, count * 2 * sizeof(VertV), 4, &cmd->data.draw.first);
  611. int i;
  612. if (verts == NULL) {
  613. return -1;
  614. }
  615. cmd->data.draw.count = count;
  616. for (i = 0; i < count; i++, rects++) {
  617. verts->x = rects->x;
  618. verts->y = rects->y;
  619. verts->z = 0.0f;
  620. verts++;
  621. verts->x = rects->x + rects->w + 0.5f;
  622. verts->y = rects->y + rects->h + 0.5f;
  623. verts->z = 0.0f;
  624. verts++;
  625. }
  626. return 0;
  627. }
  628. static int PSP_QueueCopy(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  629. const SDL_Rect *srcrect, const SDL_FRect *dstrect)
  630. {
  631. VertTV *verts;
  632. const float x = dstrect->x;
  633. const float y = dstrect->y;
  634. const float width = dstrect->w;
  635. const float height = dstrect->h;
  636. const float u0 = srcrect->x;
  637. const float v0 = srcrect->y;
  638. const float u1 = srcrect->x + srcrect->w;
  639. const float v1 = srcrect->y + srcrect->h;
  640. if ((MathAbs(u1) - MathAbs(u0)) < 64.0f) {
  641. verts = (VertTV *)SDL_AllocateRenderVertices(renderer, 2 * sizeof(VertTV), 4, &cmd->data.draw.first);
  642. if (verts == NULL) {
  643. return -1;
  644. }
  645. cmd->data.draw.count = 1;
  646. verts->u = u0;
  647. verts->v = v0;
  648. verts->x = x;
  649. verts->y = y;
  650. verts->z = 0;
  651. verts++;
  652. verts->u = u1;
  653. verts->v = v1;
  654. verts->x = x + width;
  655. verts->y = y + height;
  656. verts->z = 0;
  657. verts++;
  658. } else {
  659. float start, end;
  660. float curU = u0;
  661. float curX = x;
  662. const float endX = x + width;
  663. const float slice = 64.0f;
  664. const size_t count = SDL_ceilf(width / slice);
  665. size_t i;
  666. float ustep = (u1 - u0) / width * slice;
  667. if (ustep < 0.0f) {
  668. ustep = -ustep;
  669. }
  670. cmd->data.draw.count = count;
  671. verts = (VertTV *)SDL_AllocateRenderVertices(renderer, count * 2 * sizeof(VertTV), 4, &cmd->data.draw.first);
  672. if (verts == NULL) {
  673. return -1;
  674. }
  675. for (i = 0, start = 0, end = width; i < count; i++, start += slice) {
  676. const float polyWidth = ((curX + slice) > endX) ? (endX - curX) : slice;
  677. const float sourceWidth = ((curU + ustep) > u1) ? (u1 - curU) : ustep;
  678. SDL_assert(start < end);
  679. verts->u = curU;
  680. verts->v = v0;
  681. verts->x = curX;
  682. verts->y = y;
  683. verts->z = 0;
  684. verts++;
  685. curU += sourceWidth;
  686. curX += polyWidth;
  687. verts->u = curU;
  688. verts->v = v1;
  689. verts->x = curX;
  690. verts->y = (y + height);
  691. verts->z = 0;
  692. verts++;
  693. }
  694. }
  695. return 0;
  696. }
  697. static int PSP_QueueCopyEx(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  698. const SDL_Rect *srcrect, const SDL_FRect *dstrect,
  699. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, float scale_x, float scale_y)
  700. {
  701. VertTV *verts = (VertTV *)SDL_AllocateRenderVertices(renderer, 4 * sizeof(VertTV), 4, &cmd->data.draw.first);
  702. const float centerx = center->x;
  703. const float centery = center->y;
  704. const float x = dstrect->x + centerx;
  705. const float y = dstrect->y + centery;
  706. const float width = dstrect->w - centerx;
  707. const float height = dstrect->h - centery;
  708. float s, c;
  709. float cw1, sw1, ch1, sh1, cw2, sw2, ch2, sh2;
  710. float u0 = srcrect->x;
  711. float v0 = srcrect->y;
  712. float u1 = srcrect->x + srcrect->w;
  713. float v1 = srcrect->y + srcrect->h;
  714. if (verts == NULL) {
  715. return -1;
  716. }
  717. cmd->data.draw.count = 1;
  718. MathSincos(degToRad(360 - angle), &s, &c);
  719. cw1 = c * -centerx;
  720. sw1 = s * -centerx;
  721. ch1 = c * -centery;
  722. sh1 = s * -centery;
  723. cw2 = c * width;
  724. sw2 = s * width;
  725. ch2 = c * height;
  726. sh2 = s * height;
  727. if (flip & SDL_FLIP_VERTICAL) {
  728. Swap(&v0, &v1);
  729. }
  730. if (flip & SDL_FLIP_HORIZONTAL) {
  731. Swap(&u0, &u1);
  732. }
  733. verts->u = u0;
  734. verts->v = v0;
  735. verts->x = x + cw1 + sh1;
  736. verts->y = y - sw1 + ch1;
  737. verts->z = 0;
  738. verts++;
  739. verts->u = u0;
  740. verts->v = v1;
  741. verts->x = x + cw1 + sh2;
  742. verts->y = y - sw1 + ch2;
  743. verts->z = 0;
  744. verts++;
  745. verts->u = u1;
  746. verts->v = v1;
  747. verts->x = x + cw2 + sh2;
  748. verts->y = y - sw2 + ch2;
  749. verts->z = 0;
  750. verts++;
  751. verts->u = u1;
  752. verts->v = v0;
  753. verts->x = x + cw2 + sh1;
  754. verts->y = y - sw2 + ch1;
  755. verts->z = 0;
  756. if (scale_x != 1.0f || scale_y != 1.0f) {
  757. verts->x *= scale_x;
  758. verts->y *= scale_y;
  759. verts--;
  760. verts->x *= scale_x;
  761. verts->y *= scale_y;
  762. verts--;
  763. verts->x *= scale_x;
  764. verts->y *= scale_y;
  765. verts--;
  766. verts->x *= scale_x;
  767. verts->y *= scale_y;
  768. }
  769. return 0;
  770. }
  771. static void ResetBlendState(PSP_BlendState *state)
  772. {
  773. sceGuColor(0xffffffff);
  774. state->color = 0xffffffff;
  775. state->mode = SDL_BLENDMODE_INVALID;
  776. state->texture = NULL;
  777. sceGuDisable(GU_TEXTURE_2D);
  778. sceGuShadeModel(GU_SMOOTH);
  779. state->shadeModel = GU_SMOOTH;
  780. }
  781. static void StartDrawing(SDL_Renderer *renderer)
  782. {
  783. PSP_RenderData *data = (PSP_RenderData *)renderer->driverdata;
  784. // Check if we need to start GU displaylist
  785. if (!data->displayListAvail) {
  786. sceGuStart(GU_DIRECT, DisplayList);
  787. data->displayListAvail = SDL_TRUE;
  788. // ResetBlendState(&data->blendState);
  789. }
  790. // Check if we need a draw buffer change
  791. if (renderer->target != data->boundTarget) {
  792. SDL_Texture *texture = renderer->target;
  793. if (texture) {
  794. PSP_TextureData *psp_texture = (PSP_TextureData *)texture->driverdata;
  795. // Set target, registering LRU
  796. TextureBindAsTarget(data, psp_texture);
  797. } else {
  798. // Set target back to screen
  799. sceGuDrawBufferList(data->psm, vrelptr(data->frontbuffer), PSP_FRAME_BUFFER_WIDTH);
  800. }
  801. data->boundTarget = texture;
  802. }
  803. }
  804. static void PSP_SetBlendState(PSP_RenderData *data, PSP_BlendState *state)
  805. {
  806. PSP_BlendState *current = &data->blendState;
  807. if (state->mode != current->mode) {
  808. switch (state->mode) {
  809. case SDL_BLENDMODE_NONE:
  810. sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
  811. sceGuDisable(GU_BLEND);
  812. break;
  813. case SDL_BLENDMODE_BLEND:
  814. sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
  815. sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  816. sceGuEnable(GU_BLEND);
  817. break;
  818. case SDL_BLENDMODE_ADD:
  819. sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
  820. sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_FIX, 0, 0x00FFFFFF);
  821. sceGuEnable(GU_BLEND);
  822. break;
  823. case SDL_BLENDMODE_MOD:
  824. sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
  825. sceGuBlendFunc(GU_ADD, GU_FIX, GU_SRC_COLOR, 0, 0);
  826. sceGuEnable(GU_BLEND);
  827. break;
  828. case SDL_BLENDMODE_MUL:
  829. sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
  830. sceGuBlendFunc(GU_ADD, GU_DST_COLOR, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  831. sceGuEnable(GU_BLEND);
  832. break;
  833. case SDL_BLENDMODE_INVALID:
  834. break;
  835. }
  836. }
  837. if (state->color != current->color) {
  838. sceGuColor(state->color);
  839. }
  840. if (state->shadeModel != current->shadeModel) {
  841. sceGuShadeModel(state->shadeModel);
  842. }
  843. if (state->texture != current->texture) {
  844. if (state->texture != NULL) {
  845. TextureActivate(state->texture);
  846. sceGuEnable(GU_TEXTURE_2D);
  847. } else {
  848. sceGuDisable(GU_TEXTURE_2D);
  849. }
  850. }
  851. *current = *state;
  852. }
  853. static int PSP_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
  854. {
  855. PSP_RenderData *data = (PSP_RenderData *)renderer->driverdata;
  856. Uint8 *gpumem = NULL;
  857. StartDrawing(renderer);
  858. /* note that before the renderer interface change, this would do extrememly small
  859. batches with sceGuGetMemory()--a few vertices at a time--and it's not clear that
  860. this won't fail if you try to push 100,000 draw calls in a single batch.
  861. I don't know what the limits on PSP hardware are. It might be useful to have
  862. rendering backends report a reasonable maximum, so the higher level can flush
  863. if we appear to be exceeding that. */
  864. gpumem = (Uint8 *)sceGuGetMemory(vertsize);
  865. if (gpumem == NULL) {
  866. return SDL_SetError("Couldn't obtain a %d-byte vertex buffer!", (int)vertsize);
  867. }
  868. SDL_memcpy(gpumem, vertices, vertsize);
  869. while (cmd) {
  870. switch (cmd->command) {
  871. case SDL_RENDERCMD_SETDRAWCOLOR:
  872. {
  873. break; /* !!! FIXME: we could cache drawstate like color */
  874. }
  875. case SDL_RENDERCMD_SETVIEWPORT:
  876. {
  877. SDL_Rect *viewport = &cmd->data.viewport.rect;
  878. sceGuOffset(2048 - (viewport->w >> 1), 2048 - (viewport->h >> 1));
  879. sceGuViewport(2048, 2048, viewport->w, viewport->h);
  880. sceGuScissor(viewport->x, viewport->y, viewport->w, viewport->h);
  881. break;
  882. }
  883. case SDL_RENDERCMD_SETCLIPRECT:
  884. {
  885. const SDL_Rect *rect = &cmd->data.cliprect.rect;
  886. if (cmd->data.cliprect.enabled) {
  887. sceGuEnable(GU_SCISSOR_TEST);
  888. sceGuScissor(rect->x, rect->y, rect->w, rect->h);
  889. } else {
  890. sceGuDisable(GU_SCISSOR_TEST);
  891. }
  892. break;
  893. }
  894. case SDL_RENDERCMD_CLEAR:
  895. {
  896. const Uint8 r = cmd->data.color.r;
  897. const Uint8 g = cmd->data.color.g;
  898. const Uint8 b = cmd->data.color.b;
  899. const Uint8 a = cmd->data.color.a;
  900. sceGuClearColor(GU_RGBA(r, g, b, a));
  901. sceGuClearStencil(a);
  902. sceGuClear(GU_COLOR_BUFFER_BIT | GU_STENCIL_BUFFER_BIT);
  903. break;
  904. }
  905. case SDL_RENDERCMD_DRAW_POINTS:
  906. {
  907. const size_t count = cmd->data.draw.count;
  908. const VertV *verts = (VertV *)(gpumem + cmd->data.draw.first);
  909. const Uint8 r = cmd->data.draw.r;
  910. const Uint8 g = cmd->data.draw.g;
  911. const Uint8 b = cmd->data.draw.b;
  912. const Uint8 a = cmd->data.draw.a;
  913. PSP_BlendState state = {
  914. .color = GU_RGBA(r, g, b, a),
  915. .texture = NULL,
  916. .mode = cmd->data.draw.blend,
  917. .shadeModel = GU_FLAT
  918. };
  919. PSP_SetBlendState(data, &state);
  920. sceGuDrawArray(GU_POINTS, GU_VERTEX_32BITF | GU_TRANSFORM_2D, count, 0, verts);
  921. break;
  922. }
  923. case SDL_RENDERCMD_DRAW_LINES:
  924. {
  925. const size_t count = cmd->data.draw.count;
  926. const VertV *verts = (VertV *)(gpumem + cmd->data.draw.first);
  927. const Uint8 r = cmd->data.draw.r;
  928. const Uint8 g = cmd->data.draw.g;
  929. const Uint8 b = cmd->data.draw.b;
  930. const Uint8 a = cmd->data.draw.a;
  931. PSP_BlendState state = {
  932. .color = GU_RGBA(r, g, b, a),
  933. .texture = NULL,
  934. .mode = cmd->data.draw.blend,
  935. .shadeModel = GU_FLAT
  936. };
  937. PSP_SetBlendState(data, &state);
  938. sceGuDrawArray(GU_LINE_STRIP, GU_VERTEX_32BITF | GU_TRANSFORM_2D, count, 0, verts);
  939. break;
  940. }
  941. case SDL_RENDERCMD_FILL_RECTS:
  942. {
  943. const size_t count = cmd->data.draw.count;
  944. const VertV *verts = (VertV *)(gpumem + cmd->data.draw.first);
  945. const Uint8 r = cmd->data.draw.r;
  946. const Uint8 g = cmd->data.draw.g;
  947. const Uint8 b = cmd->data.draw.b;
  948. const Uint8 a = cmd->data.draw.a;
  949. PSP_BlendState state = {
  950. .color = GU_RGBA(r, g, b, a),
  951. .texture = NULL,
  952. .mode = cmd->data.draw.blend,
  953. .shadeModel = GU_FLAT
  954. };
  955. PSP_SetBlendState(data, &state);
  956. sceGuDrawArray(GU_SPRITES, GU_VERTEX_32BITF | GU_TRANSFORM_2D, 2 * count, 0, verts);
  957. break;
  958. }
  959. case SDL_RENDERCMD_COPY:
  960. {
  961. const size_t count = cmd->data.draw.count;
  962. const VertTV *verts = (VertTV *)(gpumem + cmd->data.draw.first);
  963. const Uint8 a = cmd->data.draw.a;
  964. const Uint8 r = cmd->data.draw.r;
  965. const Uint8 g = cmd->data.draw.g;
  966. const Uint8 b = cmd->data.draw.b;
  967. PSP_BlendState state = {
  968. .color = GU_RGBA(r, g, b, a),
  969. .texture = cmd->data.draw.texture,
  970. .mode = cmd->data.draw.blend,
  971. .shadeModel = GU_SMOOTH
  972. };
  973. PSP_SetBlendState(data, &state);
  974. sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_2D, 2 * count, 0, verts);
  975. break;
  976. }
  977. case SDL_RENDERCMD_COPY_EX:
  978. {
  979. const VertTV *verts = (VertTV *)(gpumem + cmd->data.draw.first);
  980. const Uint8 a = cmd->data.draw.a;
  981. const Uint8 r = cmd->data.draw.r;
  982. const Uint8 g = cmd->data.draw.g;
  983. const Uint8 b = cmd->data.draw.b;
  984. PSP_BlendState state = {
  985. .color = GU_RGBA(r, g, b, a),
  986. .texture = cmd->data.draw.texture,
  987. .mode = cmd->data.draw.blend,
  988. .shadeModel = GU_SMOOTH
  989. };
  990. PSP_SetBlendState(data, &state);
  991. sceGuDrawArray(GU_TRIANGLE_FAN, GU_TEXTURE_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_2D, 4, 0, verts);
  992. break;
  993. }
  994. case SDL_RENDERCMD_GEOMETRY:
  995. {
  996. const size_t count = cmd->data.draw.count;
  997. if (cmd->data.draw.texture == NULL) {
  998. const VertCV *verts = (VertCV *)(gpumem + cmd->data.draw.first);
  999. sceGuDisable(GU_TEXTURE_2D);
  1000. /* In GU_SMOOTH mode */
  1001. sceGuDrawArray(GU_TRIANGLES, GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_2D, count, 0, verts);
  1002. sceGuEnable(GU_TEXTURE_2D);
  1003. } else {
  1004. const VertTCV *verts = (VertTCV *)(gpumem + cmd->data.draw.first);
  1005. const Uint8 a = cmd->data.draw.a;
  1006. const Uint8 r = cmd->data.draw.r;
  1007. const Uint8 g = cmd->data.draw.g;
  1008. const Uint8 b = cmd->data.draw.b;
  1009. PSP_BlendState state = {
  1010. .color = GU_RGBA(r, g, b, a),
  1011. .texture = NULL,
  1012. .mode = cmd->data.draw.blend,
  1013. .shadeModel = GU_FLAT
  1014. };
  1015. TextureActivate(cmd->data.draw.texture);
  1016. PSP_SetBlendState(data, &state);
  1017. sceGuDrawArray(GU_TRIANGLES, GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_2D, count, 0, verts);
  1018. }
  1019. break;
  1020. }
  1021. case SDL_RENDERCMD_NO_OP:
  1022. break;
  1023. }
  1024. cmd = cmd->next;
  1025. }
  1026. return 0;
  1027. }
  1028. static int PSP_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
  1029. Uint32 pixel_format, void *pixels, int pitch)
  1030. {
  1031. return SDL_Unsupported();
  1032. }
  1033. static int PSP_RenderPresent(SDL_Renderer *renderer)
  1034. {
  1035. PSP_RenderData *data = (PSP_RenderData *)renderer->driverdata;
  1036. if (!data->displayListAvail) {
  1037. return -1;
  1038. }
  1039. data->displayListAvail = SDL_FALSE;
  1040. sceGuFinish();
  1041. sceGuSync(0, 0);
  1042. if ((data->vsync) && (data->vblank_not_reached)) {
  1043. sceDisplayWaitVblankStart();
  1044. }
  1045. data->vblank_not_reached = SDL_TRUE;
  1046. data->backbuffer = data->frontbuffer;
  1047. data->frontbuffer = vabsptr(sceGuSwapBuffers());
  1048. return 0;
  1049. }
  1050. static void PSP_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  1051. {
  1052. PSP_RenderData *renderdata = (PSP_RenderData *)renderer->driverdata;
  1053. PSP_TextureData *psp_texture = (PSP_TextureData *)texture->driverdata;
  1054. if (renderdata == NULL) {
  1055. return;
  1056. }
  1057. if (psp_texture == NULL) {
  1058. return;
  1059. }
  1060. LRUTargetRemove(renderdata, psp_texture);
  1061. TextureStorageFree(psp_texture->data);
  1062. SDL_free(psp_texture);
  1063. texture->driverdata = NULL;
  1064. }
  1065. static void PSP_DestroyRenderer(SDL_Renderer *renderer)
  1066. {
  1067. PSP_RenderData *data = (PSP_RenderData *)renderer->driverdata;
  1068. if (data) {
  1069. if (!data->initialized) {
  1070. return;
  1071. }
  1072. StartDrawing(renderer);
  1073. sceKernelDisableSubIntr(PSP_VBLANK_INT, 0);
  1074. sceKernelReleaseSubIntrHandler(PSP_VBLANK_INT, 0);
  1075. sceDisplayWaitVblankStart();
  1076. sceGuDisplay(GU_FALSE);
  1077. sceGuTerm();
  1078. vfree(data->backbuffer);
  1079. vfree(data->frontbuffer);
  1080. data->initialized = SDL_FALSE;
  1081. data->displayListAvail = SDL_FALSE;
  1082. SDL_free(data);
  1083. }
  1084. SDL_free(renderer);
  1085. }
  1086. static int PSP_SetVSync(SDL_Renderer *renderer, const int vsync)
  1087. {
  1088. PSP_RenderData *data = renderer->driverdata;
  1089. data->vsync = vsync;
  1090. return 0;
  1091. }
  1092. SDL_Renderer *
  1093. PSP_CreateRenderer(SDL_Window *window, Uint32 flags)
  1094. {
  1095. SDL_Renderer *renderer;
  1096. PSP_RenderData *data;
  1097. int pixelformat;
  1098. void *doublebuffer = NULL;
  1099. renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(*renderer));
  1100. if (renderer == NULL) {
  1101. SDL_OutOfMemory();
  1102. return NULL;
  1103. }
  1104. data = (PSP_RenderData *)SDL_calloc(1, sizeof(*data));
  1105. if (data == NULL) {
  1106. PSP_DestroyRenderer(renderer);
  1107. SDL_OutOfMemory();
  1108. return NULL;
  1109. }
  1110. renderer->WindowEvent = PSP_WindowEvent;
  1111. renderer->CreateTexture = PSP_CreateTexture;
  1112. renderer->UpdateTexture = PSP_UpdateTexture;
  1113. renderer->LockTexture = PSP_LockTexture;
  1114. renderer->UnlockTexture = PSP_UnlockTexture;
  1115. renderer->SetTextureScaleMode = PSP_SetTextureScaleMode;
  1116. renderer->SetRenderTarget = PSP_SetRenderTarget;
  1117. renderer->QueueSetViewport = PSP_QueueSetViewport;
  1118. renderer->QueueSetDrawColor = PSP_QueueSetViewport; /* SetViewport and SetDrawColor are (currently) no-ops. */
  1119. renderer->QueueDrawPoints = PSP_QueueDrawPoints;
  1120. renderer->QueueDrawLines = PSP_QueueDrawPoints; /* lines and points queue vertices the same way. */
  1121. renderer->QueueGeometry = PSP_QueueGeometry;
  1122. renderer->QueueFillRects = PSP_QueueFillRects;
  1123. renderer->QueueCopy = PSP_QueueCopy;
  1124. renderer->QueueCopyEx = PSP_QueueCopyEx;
  1125. renderer->RunCommandQueue = PSP_RunCommandQueue;
  1126. renderer->RenderReadPixels = PSP_RenderReadPixels;
  1127. renderer->RenderPresent = PSP_RenderPresent;
  1128. renderer->DestroyTexture = PSP_DestroyTexture;
  1129. renderer->DestroyRenderer = PSP_DestroyRenderer;
  1130. renderer->SetVSync = PSP_SetVSync;
  1131. renderer->info = PSP_RenderDriver.info;
  1132. renderer->info.flags = SDL_RENDERER_ACCELERATED;
  1133. renderer->driverdata = data;
  1134. renderer->window = window;
  1135. data->initialized = SDL_TRUE;
  1136. data->most_recent_target = NULL;
  1137. data->least_recent_target = NULL;
  1138. if (flags & SDL_RENDERER_PRESENTVSYNC) {
  1139. data->vsync = SDL_TRUE;
  1140. } else {
  1141. data->vsync = SDL_FALSE;
  1142. }
  1143. pixelformat = PixelFormatToPSPFMT(SDL_GetWindowPixelFormat(window));
  1144. switch (pixelformat) {
  1145. case GU_PSM_4444:
  1146. case GU_PSM_5650:
  1147. case GU_PSM_5551:
  1148. data->bpp = 2;
  1149. data->psm = pixelformat;
  1150. break;
  1151. default:
  1152. data->bpp = 4;
  1153. data->psm = GU_PSM_8888;
  1154. break;
  1155. }
  1156. doublebuffer = vramalloc(PSP_FRAME_BUFFER_SIZE * data->bpp * 2);
  1157. data->backbuffer = doublebuffer;
  1158. data->frontbuffer = ((uint8_t *)doublebuffer) + PSP_FRAME_BUFFER_SIZE * data->bpp;
  1159. sceGuInit();
  1160. /* setup GU */
  1161. sceGuStart(GU_DIRECT, DisplayList);
  1162. sceGuDrawBuffer(data->psm, vrelptr(data->frontbuffer), PSP_FRAME_BUFFER_WIDTH);
  1163. sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT, vrelptr(data->backbuffer), PSP_FRAME_BUFFER_WIDTH);
  1164. sceGuOffset(2048 - (PSP_SCREEN_WIDTH >> 1), 2048 - (PSP_SCREEN_HEIGHT >> 1));
  1165. sceGuViewport(2048, 2048, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
  1166. sceGuDisable(GU_DEPTH_TEST);
  1167. /* Scissoring */
  1168. sceGuScissor(0, 0, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
  1169. sceGuEnable(GU_SCISSOR_TEST);
  1170. /* Backface culling */
  1171. /*
  1172. FIXME: Culling probably un-needed ? It can conflict with SDL_RENDERCMD_GEOMETRY
  1173. sceGuFrontFace(GU_CCW);
  1174. sceGuEnable(GU_CULL_FACE);
  1175. */
  1176. // Setup initial blend state
  1177. ResetBlendState(&data->blendState);
  1178. sceGuFinish();
  1179. sceGuSync(0, 0);
  1180. sceDisplayWaitVblankStartCB();
  1181. sceGuDisplay(GU_TRUE);
  1182. /* Improve performance when VSYC is enabled and it is not reaching the 60 FPS */
  1183. data->vblank_not_reached = SDL_TRUE;
  1184. sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0, psp_on_vblank, data);
  1185. sceKernelEnableSubIntr(PSP_VBLANK_INT, 0);
  1186. return renderer;
  1187. }
  1188. SDL_RenderDriver PSP_RenderDriver = {
  1189. .CreateRenderer = PSP_CreateRenderer,
  1190. .info = {
  1191. .name = "PSP",
  1192. .flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
  1193. .num_texture_formats = 4,
  1194. .texture_formats = {
  1195. [0] = SDL_PIXELFORMAT_BGR565,
  1196. [1] = SDL_PIXELFORMAT_ABGR1555,
  1197. [2] = SDL_PIXELFORMAT_ABGR4444,
  1198. [3] = SDL_PIXELFORMAT_ABGR8888,
  1199. },
  1200. .max_texture_width = 512,
  1201. .max_texture_height = 512,
  1202. }
  1203. };
  1204. #endif /* SDL_VIDEO_RENDER_PSP */