Spill render targets to ram on PSP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
diff --git a/src/render/psp/SDL_render_psp.c b/src/render/psp/SDL_render_psp.c
index 8dc40d9..28e01c0 100644
--- a/src/render/psp/SDL_render_psp.c
+++ b/src/render/psp/SDL_render_psp.c
@@ -74,8 +74,8 @@ typedef struct PSP_TextureData
unsigned int format; /**< Image format - one of ::pgePixelFormat. */
unsigned int pitch;
SDL_bool swizzled; /**< Is image swizzled. */
- struct PSP_TextureData* prevhot; /**< More recently used render target */
- struct PSP_TextureData* nexthot; /**< Less recently used render target */
+ struct PSP_TextureData* prevhotw; /**< More recently used render target */
+ struct PSP_TextureData* nexthotw; /**< Less recently used render target */
} PSP_TextureData;
@@ -165,6 +165,12 @@ Swap(float *a, float *b)
*b = n;
}
+static inline int
+InVram(void* data)
+{
+ return data < 0x04200000;
+}
+
/* Return next power of 2 */
static int
TextureNextPow2(unsigned int w)
@@ -203,35 +209,70 @@ PixelFormatToPSPFMT(Uint32 format)
}
}
+///SECTION render target LRU management
static void
-StartDrawing(SDL_Renderer * renderer)
-{
- PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;
+LRUTargetRelink(PSP_TextureData* psp_texture) {
+ if(psp_texture->prevhotw) {
+ psp_texture->prevhotw->nexthotw = psp_texture->nexthotw;
+ }
+ if(psp_texture->nexthotw) {
+ psp_texture->nexthotw->prevhotw = psp_texture->prevhotw;
+ }
+}
- // Check if we need to start GU displaylist
- if(!data->displayListAvail) {
- sceGuStart(GU_DIRECT, DisplayList);
- data->displayListAvail = SDL_TRUE;
+static void
+LRUTargetFixTail(PSP_RenderData* data, PSP_TextureData* psp_texture) {
+ if(data->least_recent_target == psp_texture) {
+ data->least_recent_target = psp_texture->prevhotw;
+ } else if(!data->least_recent_target) {
+ data->least_recent_target = psp_texture;
}
+}
- // Check if we need a draw buffer change
- if(renderer->target != data->boundTarget) {
- SDL_Texture* texture = renderer->target;
- if(texture) {
- PSP_TextureData* psp_texture = (PSP_TextureData*) texture->driverdata;
- // Set target back to screen
- sceGuDrawBufferList(psp_texture->format, vrelptr(psp_texture->data), psp_texture->textureWidth);
- } else {
- // Set target back to screen
- sceGuDrawBufferList(data->psm, vrelptr(data->frontbuffer), PSP_FRAME_BUFFER_WIDTH);
- }
- data->boundTarget = texture;
+static void
+LRUTargetPushFront(PSP_RenderData* data, PSP_TextureData* psp_texture) {
+ psp_texture->nexthotw = data->most_recent_target;
+ if(data->most_recent_target) {
+ data->most_recent_target->prevhotw = psp_texture;
+ }
+ LRUTargetFixTail(data, psp_texture);
+}
+
+static void
+LRUTargetBringFront(PSP_RenderData* data, PSP_TextureData* psp_texture) {
+ if(data->most_recent_target == psp_texture) {
+ return; //nothing to do
+ }
+ LRUTargetRelink(psp_texture);
+ psp_texture->prevhotw = NULL;
+ psp_texture->nexthotw = NULL;
+ LRUTargetPushFront(data, psp_texture);
+}
+
+static void
+LRUTargetRemove(PSP_RenderData* data, PSP_TextureData* psp_texture) {
+ LRUTargetRelink(psp_texture);
+ if(data->most_recent_target == psp_texture) {
+ data->most_recent_target = psp_texture->nexthotw;
}
+ if(data->least_recent_target == psp_texture) {
+ data->least_recent_target = psp_texture->prevhotw;
+ }
+ psp_texture->prevhotw = NULL;
+ psp_texture->nexthotw = NULL;
}
+static void
+TextureStorageFree(void* storage) {
+ if(InVram(storage)) {
+ vfree(storage);
+ } else {
+ SDL_free(storage);
+ }
+}
static int
-TextureSwizzle(PSP_TextureData *psp_texture)
+TextureSwizzle(PSP_TextureData *psp_texture, void* dst)
{
int bytewidth, height;
int rowblocks, rowblocksadd;
@@ -251,7 +292,14 @@ TextureSwizzle(PSP_TextureData *psp_texture)
src = (unsigned int*) psp_texture->data;
- data = SDL_malloc(psp_texture->size);
+ data = dst;
+ if(!data) {
+ data = SDL_malloc(psp_texture->size);
+ }
+
+ if(!data) {
+ return SDL_OutOfMemory();
+ }
for(j = 0; j < height; j++, blockaddress += 16)
{
@@ -272,7 +320,7 @@ TextureSwizzle(PSP_TextureData *psp_texture)
blockaddress += rowblocksadd;
}
- SDL_free(psp_texture->data);
+ TextureStorageFree(psp_texture->data);
psp_texture->data = data;
psp_texture->swizzled = SDL_TRUE;
@@ -281,7 +329,7 @@ TextureSwizzle(PSP_TextureData *psp_texture)
}
static int
-TextureUnswizzle(PSP_TextureData *psp_texture)
+TextureUnswizzle(PSP_TextureData *psp_texture, void* dst)
{
int bytewidth, height;
int widthblocks, heightblocks;
@@ -306,10 +354,14 @@ TextureUnswizzle(PSP_TextureData *psp_texture)
src = (unsigned int*) psp_texture->data;
- data = SDL_malloc(psp_texture->size);
+ data = dst;
+
+ if(!data) {
+ data = SDL_malloc(psp_texture->size);
+ }
if(!data)
- return 0;
+ return SDL_OutOfMemory();
ydst = (unsigned char *)data;
@@ -338,7 +390,7 @@ TextureUnswizzle(PSP_TextureData *psp_texture)
ydst += dstrow;
}
- SDL_free(psp_texture->data);
+ TextureStorageFree(psp_texture->data);
psp_texture->data = data;
@@ -348,6 +400,80 @@ TextureUnswizzle(PSP_TextureData *psp_texture)
return 1;
}
+static int
+TextureSpillToSram(PSP_RenderData* data, PSP_TextureData* psp_texture)
+{
+ // Assumes the texture is in VRAM
+ if(psp_texture->swizzled) {
+ //Texture was swizzled in vram, just copy to system memory
+ void* data = SDL_malloc(psp_texture->size);
+ if(!data) {
+ return SDL_OutOfMemory();
+ }
+
+ SDL_memcpy(data, psp_texture->data, psp_texture->size);
+ vfree(psp_texture->data);
+ psp_texture->data = data;
+ return 0;
+ } else {
+ return TextureSwizzle(psp_texture, NULL); //Will realloc in sysram
+ }
+}
+
+static int
+TexturePromoteToVram(PSP_RenderData* data, PSP_TextureData* psp_texture, SDL_bool target)
+{
+ // Assumes texture in sram and a large enough continuous block in vram
+ void* tdata = valloc(psp_texture->size);
+ if(psp_texture->swizzled && target) {
+ return TextureUnswizzle(psp_texture, tdata);
+ } else {
+ SDL_memcpy(tdata, psp_texture->data, psp_texture->size);
+ SDL_free(psp_texture->data);
+ psp_texture->data = tdata;
+ return 0;
+ }
+}
+
+static int
+TextureSpillLRU(PSP_RenderData* data) {
+ PSP_TextureData* lru = data->least_recent_target;
+ if(lru) {
+ if(TextureSpillToSram(data, lru) < 0) {
+ return -1;
+ }
+ LRUTargetRemove(data, lru);
+ }
+ return 0;
+}
+
+static int
+TextureSpillTargetsForSpace(PSP_RenderData* data, size_t size)
+{
+ while(vlargestblock() < size) {
+ if(TextureSpillLRU(data) < 0) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static int
+TextureBindAsTarget(PSP_RenderData* data, PSP_TextureData* psp_texture) {
+ if(!InVram(psp_texture->data)) {
+ // Bring back the texture in vram
+ if(TextureSpillTargetsForSpace(data, psp_texture->size) < 0) {
+ return -1;
+ }
+ if(TexturePromoteToVram(data, psp_texture, SDL_TRUE) < 0) {
+ return -1;
+ }
+ }
+ LRUTargetBringFront(data, psp_texture);
+ sceGuDrawBufferList(psp_texture->format, vrelptr(psp_texture->data), psp_texture->textureWidth);
+ return 0;
+}
+
static void
PSP_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
@@ -357,7 +483,7 @@ PSP_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
static int
PSP_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
-/* PSP_RenderData *renderdata = (PSP_RenderData *) renderer->driverdata; */
+ PSP_RenderData *data = renderer->driverdata;
PSP_TextureData* psp_texture = (PSP_TextureData*) SDL_calloc(1, sizeof(*psp_texture));
if(!psp_texture)
@@ -389,7 +515,13 @@ PSP_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
psp_texture->pitch = psp_texture->textureWidth * SDL_BYTESPERPIXEL(texture->format);
psp_texture->size = psp_texture->textureHeight*psp_texture->pitch;
if(texture->access & SDL_TEXTUREACCESS_TARGET) {
+ if(TextureSpillTargetsForSpace(renderer->driverdata, psp_texture->size) < 0){
+ return -1;
+ }
psp_texture->data = valloc(psp_texture->size);
+ if(psp_texture->data) {
+ LRUTargetPushFront(data, psp_texture);
+ }
} else {
psp_texture->data = SDL_calloc(1, psp_texture->size);
}
@@ -426,7 +558,7 @@ TextureActivate(SDL_Texture * texture)
/* Swizzling is useless with small textures. */
if (TextureShouldSwizzle(psp_texture, texture))
{
- TextureSwizzle(psp_texture);
+ TextureSwizzle(psp_texture, NULL);
}
sceGuEnable(GU_TEXTURE_2D);
@@ -812,6 +944,34 @@ PSP_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * t
return 0;
}
+
+static void
+StartDrawing(SDL_Renderer * renderer)
+{
+ PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;
+
+ // Check if we need to start GU displaylist
+ if(!data->displayListAvail) {
+ sceGuStart(GU_DIRECT, DisplayList);
+ data->displayListAvail = SDL_TRUE;
+ }
+
+ // Check if we need a draw buffer change
+ if(renderer->target != data->boundTarget) {
+ SDL_Texture* texture = renderer->target;
+ if(texture) {
+ PSP_TextureData* psp_texture = (PSP_TextureData*) texture->driverdata;
+ // Set target, registering LRU
+ TextureBindAsTarget(data, psp_texture);
+ } else {
+ // Set target back to screen
+ sceGuDrawBufferList(data->psm, vrelptr(data->frontbuffer), PSP_FRAME_BUFFER_WIDTH);
+ }
+ data->boundTarget = texture;
+ }
+}
+
+
static void
PSP_SetBlendMode(SDL_Renderer * renderer, int blendMode)
{
@@ -1072,11 +1232,7 @@ PSP_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
if(psp_texture == 0)
return;
- if(InVram(psp_texture->data)) {
- vfree(psp_texture->data);
- } else {
- SDL_free(psp_texture->data);
- }
+ TextureStorageFree(psp_texture->data);
SDL_free(psp_texture);
texture->driverdata = NULL;
}