SDL_render_psp.c 48 KB

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