render: Move PSP backend to new interface. SDL-ryan-batching-renderer
authorRyan C. Gordon <icculus@icculus.org>
Thu, 04 Oct 2018 16:11:43 -0400
branchSDL-ryan-batching-renderer
changeset 122894425d03dffd6
parent 12288 005e1d1bcf05
child 12290 a82837d659d6
render: Move PSP backend to new interface.

I have no idea if this works (or if it ever worked, having now examined this
code), as I have no way to compile or test this.

If it's broken, send patches. :)
src/render/psp/SDL_render_psp.c
     1.1 --- a/src/render/psp/SDL_render_psp.c	Wed Oct 03 23:37:29 2018 -0400
     1.2 +++ b/src/render/psp/SDL_render_psp.c	Thu Oct 04 16:11:43 2018 -0400
     1.3 @@ -23,6 +23,7 @@
     1.4  #if SDL_VIDEO_RENDER_PSP
     1.5  
     1.6  #include "SDL_hints.h"
     1.7 +#include "SDL_assert.h"
     1.8  #include "../SDL_sysrender.h"
     1.9  
    1.10  #include <pspkernel.h>
    1.11 @@ -42,74 +43,6 @@
    1.12  
    1.13  /* PSP renderer implementation, based on the PGE  */
    1.14  
    1.15 -
    1.16 -extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
    1.17 -
    1.18 -
    1.19 -static SDL_Renderer *PSP_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.20 -static void PSP_WindowEvent(SDL_Renderer * renderer,
    1.21 -                             const SDL_WindowEvent *event);
    1.22 -static int PSP_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.23 -static int PSP_SetTextureColorMod(SDL_Renderer * renderer,
    1.24 -                                   SDL_Texture * texture);
    1.25 -static int PSP_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.26 -                              const SDL_Rect * rect, const void *pixels,
    1.27 -                              int pitch);
    1.28 -static int PSP_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.29 -                            const SDL_Rect * rect, void **pixels, int *pitch);
    1.30 -static void PSP_UnlockTexture(SDL_Renderer * renderer,
    1.31 -                               SDL_Texture * texture);
    1.32 -static int PSP_SetRenderTarget(SDL_Renderer * renderer,
    1.33 -                                 SDL_Texture * texture);
    1.34 -static int PSP_UpdateViewport(SDL_Renderer * renderer);
    1.35 -static int PSP_RenderClear(SDL_Renderer * renderer);
    1.36 -static int PSP_RenderDrawPoints(SDL_Renderer * renderer,
    1.37 -                                 const SDL_FPoint * points, int count);
    1.38 -static int PSP_RenderDrawLines(SDL_Renderer * renderer,
    1.39 -                                const SDL_FPoint * points, int count);
    1.40 -static int PSP_RenderFillRects(SDL_Renderer * renderer,
    1.41 -                                const SDL_FRect * rects, int count);
    1.42 -static int PSP_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    1.43 -                           const SDL_Rect * srcrect,
    1.44 -                           const SDL_FRect * dstrect);
    1.45 -static int PSP_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.46 -                    Uint32 pixel_format, void * pixels, int pitch);
    1.47 -static int PSP_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
    1.48 -                         const SDL_Rect * srcrect, const SDL_FRect * dstrect,
    1.49 -                         const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
    1.50 -static void PSP_RenderPresent(SDL_Renderer * renderer);
    1.51 -static void PSP_DestroyTexture(SDL_Renderer * renderer,
    1.52 -                                SDL_Texture * texture);
    1.53 -static void PSP_DestroyRenderer(SDL_Renderer * renderer);
    1.54 -
    1.55 -/*
    1.56 -SDL_RenderDriver PSP_RenderDriver = {
    1.57 -    PSP_CreateRenderer,
    1.58 -    {
    1.59 -     "PSP",
    1.60 -     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
    1.61 -     1,
    1.62 -     {SDL_PIXELFORMAT_ABGR8888},
    1.63 -     0,
    1.64 -     0}
    1.65 -};
    1.66 -*/
    1.67 -SDL_RenderDriver PSP_RenderDriver = {
    1.68 -    .CreateRenderer = PSP_CreateRenderer,
    1.69 -    .info = {
    1.70 -        .name = "PSP",
    1.71 -        .flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE,
    1.72 -        .num_texture_formats = 4,
    1.73 -        .texture_formats = { [0] = SDL_PIXELFORMAT_BGR565,
    1.74 -                                                 [1] = SDL_PIXELFORMAT_ABGR1555,
    1.75 -                                                 [2] = SDL_PIXELFORMAT_ABGR4444,
    1.76 -                                                 [3] = SDL_PIXELFORMAT_ABGR8888,
    1.77 -        },
    1.78 -        .max_texture_width = 512,
    1.79 -        .max_texture_height = 512,
    1.80 -     }
    1.81 -};
    1.82 -
    1.83  #define PSP_SCREEN_WIDTH    480
    1.84  #define PSP_SCREEN_HEIGHT   272
    1.85  
    1.86 @@ -169,6 +102,42 @@
    1.87  
    1.88  } VertTV;
    1.89  
    1.90 +#define PI   3.14159265358979f
    1.91 +
    1.92 +#define radToDeg(x) ((x)*180.f/PI)
    1.93 +#define degToRad(x) ((x)*PI/180.f)
    1.94 +
    1.95 +float MathAbs(float x)
    1.96 +{
    1.97 +    float result;
    1.98 +
    1.99 +    __asm__ volatile (
   1.100 +        "mtv      %1, S000\n"
   1.101 +        "vabs.s   S000, S000\n"
   1.102 +        "mfv      %0, S000\n"
   1.103 +    : "=r"(result) : "r"(x));
   1.104 +
   1.105 +    return result;
   1.106 +}
   1.107 +
   1.108 +void MathSincos(float r, float *s, float *c)
   1.109 +{
   1.110 +    __asm__ volatile (
   1.111 +        "mtv      %2, S002\n"
   1.112 +        "vcst.s   S003, VFPU_2_PI\n"
   1.113 +        "vmul.s   S002, S002, S003\n"
   1.114 +        "vrot.p   C000, S002, [s, c]\n"
   1.115 +        "mfv      %0, S000\n"
   1.116 +        "mfv      %1, S001\n"
   1.117 +    : "=r"(*s), "=r"(*c): "r"(r));
   1.118 +}
   1.119 +
   1.120 +void Swap(float *a, float *b)
   1.121 +{
   1.122 +    float n=*a;
   1.123 +    *a = *b;
   1.124 +    *b = n;
   1.125 +}
   1.126  
   1.127  /* Return next power of 2 */
   1.128  static int
   1.129 @@ -326,123 +295,9 @@
   1.130      return 1;
   1.131  }
   1.132  
   1.133 -SDL_Renderer *
   1.134 -PSP_CreateRenderer(SDL_Window * window, Uint32 flags)
   1.135 -{
   1.136 -
   1.137 -    SDL_Renderer *renderer;
   1.138 -    PSP_RenderData *data;
   1.139 -        int pixelformat;
   1.140 -    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   1.141 -    if (!renderer) {
   1.142 -        SDL_OutOfMemory();
   1.143 -        return NULL;
   1.144 -    }
   1.145 -
   1.146 -    data = (PSP_RenderData *) SDL_calloc(1, sizeof(*data));
   1.147 -    if (!data) {
   1.148 -        PSP_DestroyRenderer(renderer);
   1.149 -        SDL_OutOfMemory();
   1.150 -        return NULL;
   1.151 -    }
   1.152 -
   1.153 -
   1.154 -    renderer->WindowEvent = PSP_WindowEvent;
   1.155 -    renderer->CreateTexture = PSP_CreateTexture;
   1.156 -    renderer->SetTextureColorMod = PSP_SetTextureColorMod;
   1.157 -    renderer->UpdateTexture = PSP_UpdateTexture;
   1.158 -    renderer->LockTexture = PSP_LockTexture;
   1.159 -    renderer->UnlockTexture = PSP_UnlockTexture;
   1.160 -    renderer->SetRenderTarget = PSP_SetRenderTarget;
   1.161 -    renderer->UpdateViewport = PSP_UpdateViewport;
   1.162 -    renderer->RenderClear = PSP_RenderClear;
   1.163 -    renderer->RenderDrawPoints = PSP_RenderDrawPoints;
   1.164 -    renderer->RenderDrawLines = PSP_RenderDrawLines;
   1.165 -    renderer->RenderFillRects = PSP_RenderFillRects;
   1.166 -    renderer->RenderCopy = PSP_RenderCopy;
   1.167 -    renderer->RenderReadPixels = PSP_RenderReadPixels;
   1.168 -    renderer->RenderCopyEx = PSP_RenderCopyEx;
   1.169 -    renderer->RenderPresent = PSP_RenderPresent;
   1.170 -    renderer->DestroyTexture = PSP_DestroyTexture;
   1.171 -    renderer->DestroyRenderer = PSP_DestroyRenderer;
   1.172 -    renderer->info = PSP_RenderDriver.info;
   1.173 -    renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
   1.174 -    renderer->driverdata = data;
   1.175 -    renderer->window = window;
   1.176 -
   1.177 -    if (data->initialized != SDL_FALSE)
   1.178 -        return 0;
   1.179 -    data->initialized = SDL_TRUE;
   1.180 -
   1.181 -    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   1.182 -        data->vsync = SDL_TRUE;
   1.183 -    } else {
   1.184 -        data->vsync = SDL_FALSE;
   1.185 -    }
   1.186 -
   1.187 -    pixelformat=PixelFormatToPSPFMT(SDL_GetWindowPixelFormat(window));
   1.188 -    switch(pixelformat)
   1.189 -    {
   1.190 -        case GU_PSM_4444:
   1.191 -        case GU_PSM_5650:
   1.192 -        case GU_PSM_5551:
   1.193 -            data->frontbuffer = (unsigned int *)(PSP_FRAME_BUFFER_SIZE<<1);
   1.194 -            data->backbuffer =  (unsigned int *)(0);
   1.195 -            data->bpp = 2;
   1.196 -            data->psm = pixelformat;
   1.197 -            break;
   1.198 -        default:
   1.199 -            data->frontbuffer = (unsigned int *)(PSP_FRAME_BUFFER_SIZE<<2);
   1.200 -            data->backbuffer =  (unsigned int *)(0);
   1.201 -            data->bpp = 4;
   1.202 -            data->psm = GU_PSM_8888;
   1.203 -            break;
   1.204 -    }
   1.205 -
   1.206 -    sceGuInit();
   1.207 -    /* setup GU */
   1.208 -    sceGuStart(GU_DIRECT, DisplayList);
   1.209 -    sceGuDrawBuffer(data->psm, data->frontbuffer, PSP_FRAME_BUFFER_WIDTH);
   1.210 -    sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT, data->backbuffer, PSP_FRAME_BUFFER_WIDTH);
   1.211 -
   1.212 -
   1.213 -    sceGuOffset(2048 - (PSP_SCREEN_WIDTH>>1), 2048 - (PSP_SCREEN_HEIGHT>>1));
   1.214 -    sceGuViewport(2048, 2048, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
   1.215 -
   1.216 -    data->frontbuffer = vabsptr(data->frontbuffer);
   1.217 -    data->backbuffer = vabsptr(data->backbuffer);
   1.218 -
   1.219 -    /* Scissoring */
   1.220 -    sceGuScissor(0, 0, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
   1.221 -    sceGuEnable(GU_SCISSOR_TEST);
   1.222 -
   1.223 -    /* Backface culling */
   1.224 -    sceGuFrontFace(GU_CCW);
   1.225 -    sceGuEnable(GU_CULL_FACE);
   1.226 -
   1.227 -    /* Texturing */
   1.228 -    sceGuEnable(GU_TEXTURE_2D);
   1.229 -    sceGuShadeModel(GU_SMOOTH);
   1.230 -    sceGuTexWrap(GU_REPEAT, GU_REPEAT);
   1.231 -
   1.232 -    /* Blending */
   1.233 -    sceGuEnable(GU_BLEND);
   1.234 -    sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
   1.235 -
   1.236 -    sceGuTexFilter(GU_LINEAR,GU_LINEAR);
   1.237 -
   1.238 -    sceGuFinish();
   1.239 -    sceGuSync(0,0);
   1.240 -    sceDisplayWaitVblankStartCB();
   1.241 -    sceGuDisplay(GU_TRUE);
   1.242 -
   1.243 -    return renderer;
   1.244 -}
   1.245 -
   1.246  static void
   1.247  PSP_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
   1.248  {
   1.249 -
   1.250  }
   1.251  
   1.252  
   1.253 @@ -576,17 +431,221 @@
   1.254  static int
   1.255  PSP_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
   1.256  {
   1.257 +    return 0;
   1.258 +}
   1.259 +
   1.260 +static int
   1.261 +PSP_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
   1.262 +{
   1.263 +    return 0;  /* nothing to do in this backend. */
   1.264 +}
   1.265 +
   1.266 +static int
   1.267 +PSP_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
   1.268 +{
   1.269 +    VertV *verts = (VertV *) SDL_AllocateRenderVertices(renderer, count * sizeof (VertV), 4, &cmd->data.draw.first);
   1.270 +    size_t i;
   1.271 +
   1.272 +    if (!verts) {
   1.273 +        return -1;
   1.274 +    }
   1.275 +
   1.276 +    cmd->data.draw.count = count;
   1.277 +
   1.278 +    for (i = 0; i < count; i++, verts++, points++) {
   1.279 +        verts->x = points->x;
   1.280 +        verts->y = points->y;
   1.281 +        verts->z = 0.0f;
   1.282 +    }
   1.283  
   1.284      return 0;
   1.285  }
   1.286  
   1.287  static int
   1.288 -PSP_UpdateViewport(SDL_Renderer * renderer)
   1.289 +PSP_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
   1.290  {
   1.291 +    VertV *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 2 * sizeof (VertV), 4, &cmd->data.draw.first);
   1.292 +    size_t i;
   1.293 +
   1.294 +    if (!verts) {
   1.295 +        return -1;
   1.296 +    }
   1.297 +
   1.298 +    cmd->data.draw.count = count;
   1.299 +    for (i = 0; i < count; i++, rects++) {
   1.300 +        const SDL_FRect *rect = &rects[i];
   1.301 +        verts->x = rect->x;
   1.302 +        verts->y = rect->y;
   1.303 +        verts->z = 0.0f;
   1.304 +        verts++;
   1.305 +
   1.306 +        verts->x = rect->x + rect->w;
   1.307 +        verts->y = rect->y + rect->h;
   1.308 +        verts->z = 0.0f;
   1.309 +        verts++;
   1.310 +    }
   1.311  
   1.312      return 0;
   1.313  }
   1.314  
   1.315 +static int
   1.316 +PSP_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
   1.317 +             const SDL_Rect * srcrect, const SDL_FRect * dstrect)
   1.318 +{
   1.319 +    VertTV *verts;
   1.320 +    const float x = dstrect->x;
   1.321 +    const float y = dstrect->y;
   1.322 +    const float width = dstrect->w;
   1.323 +    const float height = dstrect->h;
   1.324 +
   1.325 +    const float u0 = srcrect->x;
   1.326 +    const float v0 = srcrect->y;
   1.327 +    const float u1 = srcrect->x + srcrect->w;
   1.328 +    const float v1 = srcrect->y + srcrect->h;
   1.329 +
   1.330 +    if((MathAbs(u1) - MathAbs(u0)) < 64.0f)
   1.331 +    {
   1.332 +        verts = (VertTV *) SDL_AllocateRenderVertices(renderer, 2 * sizeof (VertTV), 4, &cmd->data.draw.first);
   1.333 +        if (!verts) {
   1.334 +            return -1;
   1.335 +        }
   1.336 +
   1.337 +        cmd->data.draw.count = 1;
   1.338 +
   1.339 +        verts->u = u0;
   1.340 +        verts->v = v0;
   1.341 +        verts->x = x;
   1.342 +        verts->y = y;
   1.343 +        verts->z = 0;
   1.344 +        verts++;
   1.345 +
   1.346 +        verts->u = u1;
   1.347 +        verts->v = v1;
   1.348 +        verts->x = x + width;
   1.349 +        verts->y = y + height;
   1.350 +        verts->z = 0;
   1.351 +        verts++;
   1.352 +    }
   1.353 +    else
   1.354 +    {
   1.355 +        float start, end;
   1.356 +        float curU = u0;
   1.357 +        float curX = x;
   1.358 +        const float endX = x + width;
   1.359 +        const float slice = 64.0f;
   1.360 +        const size_t count = SDL_ceilf(width / slice);
   1.361 +        size_t i;
   1.362 +        float ustep = (u1 - u0)/width * slice;
   1.363 +
   1.364 +        if(ustep < 0.0f)
   1.365 +            ustep = -ustep;
   1.366 +
   1.367 +        cmd->data.draw.count = count;
   1.368 +
   1.369 +        verts = (VertTV *) SDL_AllocateRenderVertices(renderer, count * sizeof (VertTV), 4, &cmd->data.draw.first);
   1.370 +        if (!verts) {
   1.371 +            return -1;
   1.372 +        }
   1.373 +
   1.374 +
   1.375 +        for(i = 0, start = 0, end = width; i < count; i++, start += slice)
   1.376 +        {
   1.377 +            const float polyWidth = ((curX + slice) > endX) ? (endX - curX) : slice;
   1.378 +            const float sourceWidth = ((curU + ustep) > u1) ? (u1 - curU) : ustep;
   1.379 +
   1.380 +            SDL_assert(start < end);
   1.381 +
   1.382 +            verts->u = curU;
   1.383 +            verts->v = v0;
   1.384 +            verts->x = curX;
   1.385 +            verts->y = y;
   1.386 +            verts->z = 0;
   1.387 +
   1.388 +            curU += sourceWidth;
   1.389 +            curX += polyWidth;
   1.390 +
   1.391 +            verts->u = curU;
   1.392 +            verts->v = v1;
   1.393 +            verts->x = curX;
   1.394 +            verts->y = (y + height);
   1.395 +            verts->z = 0;
   1.396 +        }
   1.397 +    }
   1.398 +
   1.399 +    return 0;
   1.400 +}
   1.401 +
   1.402 +static int
   1.403 +PSP_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
   1.404 +               const SDL_Rect * srcrect, const SDL_FRect * dstrect,
   1.405 +               const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
   1.406 +{
   1.407 +    VertTV *verts = (VertTV *) SDL_AllocateRenderVertices(renderer, 4 * sizeof (VertTV), 4, &cmd->data.draw.first);
   1.408 +    const float centerx = center->x;
   1.409 +    const float centery = center->y;
   1.410 +    const float x = dstrect->x + centerx;
   1.411 +    const float y = dstrect->y + centery;
   1.412 +    const float width = dstrect->w - centerx;
   1.413 +    const float height = dstrect->h - centery;
   1.414 +    float s, c;
   1.415 +
   1.416 +    float u0 = srcrect->x;
   1.417 +    float v0 = srcrect->y;
   1.418 +    float u1 = srcrect->x + srcrect->w;
   1.419 +    float v1 = srcrect->y + srcrect->h;
   1.420 +
   1.421 +
   1.422 +    if (!verts) {
   1.423 +        return -1;
   1.424 +    }
   1.425 +
   1.426 +    cmd->data.draw.count = 1;
   1.427 +
   1.428 +    MathSincos(degToRad(angle), &s, &c);
   1.429 +
   1.430 +    const float cw = c * width;
   1.431 +    const float sw = s * width;
   1.432 +    const float ch = c * height;
   1.433 +    const float sh = s * height;
   1.434 +
   1.435 +    if (flip & SDL_FLIP_VERTICAL) {
   1.436 +        Swap(&v0, &v1);
   1.437 +    }
   1.438 +
   1.439 +    if (flip & SDL_FLIP_HORIZONTAL) {
   1.440 +        Swap(&u0, &u1);
   1.441 +    }
   1.442 +
   1.443 +    verts->u = u0;
   1.444 +    verts->v = v0;
   1.445 +    verts->x = x - cw + sh;
   1.446 +    verts->y = y - sw - ch;
   1.447 +    verts->z = 0;
   1.448 +    verts++;
   1.449 +
   1.450 +    verts->u = u0;
   1.451 +    verts->v = v1;
   1.452 +    verts->x = x - cw - sh;
   1.453 +    verts->y = y - sw + ch;
   1.454 +    verts->z = 0;
   1.455 +    verts++;
   1.456 +
   1.457 +    verts->u = u1;
   1.458 +    verts->v = v1;
   1.459 +    verts->x = x + cw - sh;
   1.460 +    verts->y = y + sw + ch;
   1.461 +    verts->z = 0;
   1.462 +    verts++;
   1.463 +
   1.464 +    verts->u = u1;
   1.465 +    verts->v = v0;
   1.466 +    verts->x = x + cw + sh;
   1.467 +    verts->y = y + sw - ch;
   1.468 +    verts->z = 0;
   1.469 +    verts++;
   1.470 +
   1.471 +    return 0;
   1.472 +}
   1.473  
   1.474  static void
   1.475  PSP_SetBlendMode(SDL_Renderer * renderer, int blendMode)
   1.476 @@ -618,341 +677,183 @@
   1.477      }
   1.478  }
   1.479  
   1.480 +static int
   1.481 +PSP_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
   1.482 +{
   1.483 +    PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata;
   1.484 +    size_t i;
   1.485  
   1.486 +    StartDrawing(renderer);
   1.487  
   1.488 -static int
   1.489 -PSP_RenderClear(SDL_Renderer * renderer)
   1.490 -{
   1.491 -    /* start list */
   1.492 -    StartDrawing(renderer);
   1.493 -    int color = renderer->a << 24 | renderer->b << 16 | renderer->g << 8 | renderer->r;
   1.494 -    sceGuClearColor(color);
   1.495 -    sceGuClearDepth(0);
   1.496 -    sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT|GU_FAST_CLEAR_BIT);
   1.497 +    /* note that before the renderer interface change, this would do extrememly small
   1.498 +       batches with sceGuGetMemory()--a few vertices at a time--and it's not clear that
   1.499 +       this won't fail if you try to push 100,000 draw calls in a single batch.
   1.500 +       I don't know what the limits on PSP hardware are. It might be useful to have
   1.501 +       rendering backends report a reasonable maximum, so the higher level can flush
   1.502 +       if we appear to be exceeding that. */
   1.503 +    Uint8 *gpumem = (Uint8 *) sceGuGetMemory(vertsize);
   1.504 +    if (!gpumem) {
   1.505 +        return SDL_SetError("Couldn't obtain a %d-byte vertex buffer!", (int) vertsize);
   1.506 +    }
   1.507 +    SDL_memcpy(gpumem, vertices, vertsize);
   1.508  
   1.509 -    return 0;
   1.510 -}
   1.511 +    while (cmd) {
   1.512 +        switch (cmd->command) {
   1.513 +            case SDL_RENDERCMD_SETDRAWCOLOR: {
   1.514 +                break;  /* !!! FIXME: we could cache drawstate like color */
   1.515 +            }
   1.516  
   1.517 -static int
   1.518 -PSP_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
   1.519 -                      int count)
   1.520 -{
   1.521 -    int color = renderer->a << 24 | renderer->b << 16 | renderer->g << 8 | renderer->r;
   1.522 -    int i;
   1.523 -    StartDrawing(renderer);
   1.524 -    VertV* vertices = (VertV*)sceGuGetMemory(count*sizeof(VertV));
   1.525 +            case SDL_RENDERCMD_SETVIEWPORT: {
   1.526 +                SDL_Rect *viewport = &data->drawstate.viewport;
   1.527 +                if (SDL_memcmp(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect)) != 0) {
   1.528 +                    SDL_memcpy(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect));
   1.529 +                    data->drawstate.viewport_dirty = SDL_TRUE;
   1.530 +                }
   1.531 +                break;
   1.532 +            }
   1.533  
   1.534 -    for (i = 0; i < count; ++i) {
   1.535 -            vertices[i].x = points[i].x;
   1.536 -            vertices[i].y = points[i].y;
   1.537 -            vertices[i].z = 0.0f;
   1.538 -    }
   1.539 -    sceGuDisable(GU_TEXTURE_2D);
   1.540 -    sceGuColor(color);
   1.541 -    sceGuShadeModel(GU_FLAT);
   1.542 -    sceGuDrawArray(GU_POINTS, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, vertices);
   1.543 -    sceGuShadeModel(GU_SMOOTH);
   1.544 -    sceGuEnable(GU_TEXTURE_2D);
   1.545 +            case SDL_RENDERCMD_SETCLIPRECT: {
   1.546 +                const SDL_Rect *rect = &cmd->data.cliprect.rect;
   1.547 +                if (data->drawstate.cliprect_enabled != cmd->data.cliprect.enabled) {
   1.548 +                    data->drawstate.cliprect_enabled = cmd->data.cliprect.enabled;
   1.549 +                    data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
   1.550 +                }
   1.551 +                if (SDL_memcmp(&data->drawstate.cliprect, rect, sizeof (SDL_Rect)) != 0) {
   1.552 +                    SDL_memcpy(&data->drawstate.cliprect, rect, sizeof (SDL_Rect));
   1.553 +                    data->drawstate.cliprect_dirty = SDL_TRUE;
   1.554 +                }
   1.555 +                break;
   1.556 +            }
   1.557  
   1.558 -    return 0;
   1.559 -}
   1.560 +            case SDL_RENDERCMD_CLEAR: {
   1.561 +                const Uint8 r = cmd->data.color.r;
   1.562 +                const Uint8 g = cmd->data.color.g;
   1.563 +                const Uint8 b = cmd->data.color.b;
   1.564 +                const Uint8 a = cmd->data.color.a;
   1.565 +                const Uint32 color = ((a << 24) | (b << 16) | (g << 8) | r);
   1.566 +                /* !!! FIXME: we could cache drawstate like clear color */
   1.567 +                sceGuClearColor(color);
   1.568 +                sceGuClearDepth(0);
   1.569 +                sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT|GU_FAST_CLEAR_BIT);
   1.570 +                break;
   1.571 +            }
   1.572  
   1.573 -static int
   1.574 -PSP_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
   1.575 -                     int count)
   1.576 -{
   1.577 -    int color = renderer->a << 24 | renderer->b << 16 | renderer->g << 8 | renderer->r;
   1.578 -    int i;
   1.579 -    StartDrawing(renderer);
   1.580 -    VertV* vertices = (VertV*)sceGuGetMemory(count*sizeof(VertV));
   1.581 +            case SDL_RENDERCMD_DRAW_POINTS: {
   1.582 +                const size_t count = cmd->data.draw.count;
   1.583 +                const VertV *verts = (VertV *) (gpumem + cmd->data.draw.first);
   1.584 +                const Uint8 r = cmd->data.color.r;
   1.585 +                const Uint8 g = cmd->data.color.g;
   1.586 +                const Uint8 b = cmd->data.color.b;
   1.587 +                const Uint8 a = cmd->data.color.a;
   1.588 +                const Uint32 color = ((a << 24) | (b << 16) | (g << 8) | r);
   1.589 +                /* !!! FIXME: we could cache draw state like color, texturing, etc */
   1.590 +                sceGuColor(color);
   1.591 +                sceGuDisable(GU_TEXTURE_2D);
   1.592 +                sceGuShadeModel(GU_FLAT);
   1.593 +                sceGuDrawArray(GU_POINTS, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, verts);
   1.594 +                sceGuShadeModel(GU_SMOOTH);
   1.595 +                sceGuEnable(GU_TEXTURE_2D);
   1.596 +                break;
   1.597 +            }
   1.598  
   1.599 -    for (i = 0; i < count; ++i) {
   1.600 -            vertices[i].x = points[i].x;
   1.601 -            vertices[i].y = points[i].y;
   1.602 -            vertices[i].z = 0.0f;
   1.603 +            case SDL_RENDERCMD_DRAW_LINES: {
   1.604 +                const size_t count = cmd->data.draw.count;
   1.605 +                const VertV *verts = (VertV *) (gpumem + cmd->data.draw.first);
   1.606 +                const Uint8 r = cmd->data.color.r;
   1.607 +                const Uint8 g = cmd->data.color.g;
   1.608 +                const Uint8 b = cmd->data.color.b;
   1.609 +                const Uint8 a = cmd->data.color.a;
   1.610 +                const Uint32 color = ((a << 24) | (b << 16) | (g << 8) | r);
   1.611 +                /* !!! FIXME: we could cache draw state like color, texturing, etc */
   1.612 +                sceGuColor(color);
   1.613 +                sceGuDisable(GU_TEXTURE_2D);
   1.614 +                sceGuShadeModel(GU_FLAT);
   1.615 +                sceGuDrawArray(GU_LINE_STRIP, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, verts);
   1.616 +                sceGuShadeModel(GU_SMOOTH);
   1.617 +                sceGuEnable(GU_TEXTURE_2D);
   1.618 +                break;
   1.619 +            }
   1.620 +
   1.621 +            case SDL_RENDERCMD_FILL_RECTS: {
   1.622 +                const size_t count = cmd->data.draw.count;
   1.623 +                const VertV *verts = (VertV *) (gpumem + cmd->data.draw.first);
   1.624 +                const Uint8 r = cmd->data.color.r;
   1.625 +                const Uint8 g = cmd->data.color.g;
   1.626 +                const Uint8 b = cmd->data.color.b;
   1.627 +                const Uint8 a = cmd->data.color.a;
   1.628 +                const Uint32 color = ((a << 24) | (b << 16) | (g << 8) | r);
   1.629 +                /* !!! FIXME: we could cache draw state like color, texturing, etc */
   1.630 +                sceGuColor(color);
   1.631 +                sceGuDisable(GU_TEXTURE_2D);
   1.632 +                sceGuShadeModel(GU_FLAT);
   1.633 +                sceGuDrawArray(GU_SPRITES, GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2 * count, 0, verts);
   1.634 +                sceGuShadeModel(GU_SMOOTH);
   1.635 +                sceGuEnable(GU_TEXTURE_2D);
   1.636 +                break;
   1.637 +            }
   1.638 +
   1.639 +            case SDL_RENDERCMD_COPY: {
   1.640 +                const size_t count = cmd->data.draw.count;
   1.641 +                const VertTV *verts = (VertTV *) (gpumem + cmd->data.draw.first);
   1.642 +                const Uint8 alpha = cmd->data.draw.a;
   1.643 +                TextureActivate(cmd->data.draw.texture);
   1.644 +                PSP_SetBlendMode(renderer, cmd->data.draw.blend);
   1.645 +
   1.646 +                if(alpha != 255) {  /* !!! FIXME: is this right? */
   1.647 +                    sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
   1.648 +                    sceGuColor(GU_RGBA(255, 255, 255, alpha));
   1.649 +                } else {
   1.650 +                    sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
   1.651 +                    sceGuColor(0xFFFFFFFF);
   1.652 +                }
   1.653 +
   1.654 +                sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2 * count, 0, verts);
   1.655 +
   1.656 +                if(alpha != 255) {
   1.657 +                    sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
   1.658 +                }
   1.659 +                break;
   1.660 +            }
   1.661 +
   1.662 +            case SDL_RENDERCMD_COPY_EX: {
   1.663 +                const VertTV *verts = (VertTV *) (gpumem + cmd->data.draw.first);
   1.664 +                const Uint8 alpha = cmd->data.draw.a;
   1.665 +                TextureActivate(cmd->data.draw.texture);
   1.666 +                PSP_SetBlendMode(renderer, cmd->data.draw.blend);
   1.667 +
   1.668 +                if(alpha != 255) {  /* !!! FIXME: is this right? */
   1.669 +                    sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
   1.670 +                    sceGuColor(GU_RGBA(255, 255, 255, alpha));
   1.671 +                } else {
   1.672 +                    sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
   1.673 +                    sceGuColor(0xFFFFFFFF);
   1.674 +                }
   1.675 +
   1.676 +                sceGuDrawArray(GU_TRIANGLE_FAN, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 4, 0, verts);
   1.677 +
   1.678 +                if(alpha != 255) {
   1.679 +                    sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
   1.680 +                }
   1.681 +                break;
   1.682 +            }
   1.683 +
   1.684 +            case SDL_RENDERCMD_NO_OP:
   1.685 +                break;
   1.686 +        }
   1.687 +
   1.688 +        cmd = cmd->next;
   1.689      }
   1.690  
   1.691 -    sceGuDisable(GU_TEXTURE_2D);
   1.692 -    sceGuColor(color);
   1.693 -    sceGuShadeModel(GU_FLAT);
   1.694 -    sceGuDrawArray(GU_LINE_STRIP, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, vertices);
   1.695 -    sceGuShadeModel(GU_SMOOTH);
   1.696 -    sceGuEnable(GU_TEXTURE_2D);
   1.697 -
   1.698 -    return 0;
   1.699 -}
   1.700 -
   1.701 -static int
   1.702 -PSP_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects,
   1.703 -                     int count)
   1.704 -{
   1.705 -    int color = renderer->a << 24 | renderer->b << 16 | renderer->g << 8 | renderer->r;
   1.706 -    int i;
   1.707 -    StartDrawing(renderer);
   1.708 -
   1.709 -    for (i = 0; i < count; ++i) {
   1.710 -        const SDL_FRect *rect = &rects[i];
   1.711 -        VertV* vertices = (VertV*)sceGuGetMemory((sizeof(VertV)<<1));
   1.712 -        vertices[0].x = rect->x;
   1.713 -        vertices[0].y = rect->y;
   1.714 -        vertices[0].z = 0.0f;
   1.715 -
   1.716 -        vertices[1].x = rect->x + rect->w;
   1.717 -        vertices[1].y = rect->y + rect->h;
   1.718 -        vertices[1].z = 0.0f;
   1.719 -
   1.720 -        sceGuDisable(GU_TEXTURE_2D);
   1.721 -        sceGuColor(color);
   1.722 -        sceGuShadeModel(GU_FLAT);
   1.723 -        sceGuDrawArray(GU_SPRITES, GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2, 0, vertices);
   1.724 -        sceGuShadeModel(GU_SMOOTH);
   1.725 -        sceGuEnable(GU_TEXTURE_2D);
   1.726 -    }
   1.727 -
   1.728 -    return 0;
   1.729 -}
   1.730 -
   1.731 -
   1.732 -#define PI   3.14159265358979f
   1.733 -
   1.734 -#define radToDeg(x) ((x)*180.f/PI)
   1.735 -#define degToRad(x) ((x)*PI/180.f)
   1.736 -
   1.737 -float MathAbs(float x)
   1.738 -{
   1.739 -    float result;
   1.740 -
   1.741 -    __asm__ volatile (
   1.742 -        "mtv      %1, S000\n"
   1.743 -        "vabs.s   S000, S000\n"
   1.744 -        "mfv      %0, S000\n"
   1.745 -    : "=r"(result) : "r"(x));
   1.746 -
   1.747 -    return result;
   1.748 -}
   1.749 -
   1.750 -void MathSincos(float r, float *s, float *c)
   1.751 -{
   1.752 -    __asm__ volatile (
   1.753 -        "mtv      %2, S002\n"
   1.754 -        "vcst.s   S003, VFPU_2_PI\n"
   1.755 -        "vmul.s   S002, S002, S003\n"
   1.756 -        "vrot.p   C000, S002, [s, c]\n"
   1.757 -        "mfv      %0, S000\n"
   1.758 -        "mfv      %1, S001\n"
   1.759 -    : "=r"(*s), "=r"(*c): "r"(r));
   1.760 -}
   1.761 -
   1.762 -void Swap(float *a, float *b)
   1.763 -{
   1.764 -    float n=*a;
   1.765 -    *a = *b;
   1.766 -    *b = n;
   1.767 -}
   1.768 -
   1.769 -static int
   1.770 -PSP_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.771 -                const SDL_Rect * srcrect, const SDL_FRect * dstrect)
   1.772 -{
   1.773 -    float x, y, width, height;
   1.774 -    float u0, v0, u1, v1;
   1.775 -    unsigned char alpha;
   1.776 -
   1.777 -    x = dstrect->x;
   1.778 -    y = dstrect->y;
   1.779 -    width = dstrect->w;
   1.780 -    height = dstrect->h;
   1.781 -
   1.782 -    u0 = srcrect->x;
   1.783 -    v0 = srcrect->y;
   1.784 -    u1 = srcrect->x + srcrect->w;
   1.785 -    v1 = srcrect->y + srcrect->h;
   1.786 -
   1.787 -    alpha = texture->a;
   1.788 -
   1.789 -    StartDrawing(renderer);
   1.790 -    TextureActivate(texture);
   1.791 -    PSP_SetBlendMode(renderer, renderer->blendMode);
   1.792 -
   1.793 -    if(alpha != 255)
   1.794 -    {
   1.795 -        sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
   1.796 -        sceGuColor(GU_RGBA(255, 255, 255, alpha));
   1.797 -    }else{
   1.798 -        sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
   1.799 -        sceGuColor(0xFFFFFFFF);
   1.800 -    }
   1.801 -
   1.802 -    if((MathAbs(u1) - MathAbs(u0)) < 64.0f)
   1.803 -    {
   1.804 -        VertTV* vertices = (VertTV*)sceGuGetMemory((sizeof(VertTV))<<1);
   1.805 -
   1.806 -        vertices[0].u = u0;
   1.807 -        vertices[0].v = v0;
   1.808 -        vertices[0].x = x;
   1.809 -        vertices[0].y = y;
   1.810 -        vertices[0].z = 0;
   1.811 -
   1.812 -        vertices[1].u = u1;
   1.813 -        vertices[1].v = v1;
   1.814 -        vertices[1].x = x + width;
   1.815 -        vertices[1].y = y + height;
   1.816 -        vertices[1].z = 0;
   1.817 -
   1.818 -        sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2, 0, vertices);
   1.819 -    }
   1.820 -    else
   1.821 -    {
   1.822 -        float start, end;
   1.823 -        float curU = u0;
   1.824 -        float curX = x;
   1.825 -        float endX = x + width;
   1.826 -        float slice = 64.0f;
   1.827 -        float ustep = (u1 - u0)/width * slice;
   1.828 -
   1.829 -        if(ustep < 0.0f)
   1.830 -            ustep = -ustep;
   1.831 -
   1.832 -        for(start = 0, end = width; start < end; start += slice)
   1.833 -        {
   1.834 -            VertTV* vertices = (VertTV*)sceGuGetMemory((sizeof(VertTV))<<1);
   1.835 -
   1.836 -            float polyWidth = ((curX + slice) > endX) ? (endX - curX) : slice;
   1.837 -            float sourceWidth = ((curU + ustep) > u1) ? (u1 - curU) : ustep;
   1.838 -
   1.839 -            vertices[0].u = curU;
   1.840 -            vertices[0].v = v0;
   1.841 -            vertices[0].x = curX;
   1.842 -            vertices[0].y = y;
   1.843 -            vertices[0].z = 0;
   1.844 -
   1.845 -            curU += sourceWidth;
   1.846 -            curX += polyWidth;
   1.847 -
   1.848 -            vertices[1].u = curU;
   1.849 -            vertices[1].v = v1;
   1.850 -            vertices[1].x = curX;
   1.851 -            vertices[1].y = (y + height);
   1.852 -            vertices[1].z = 0;
   1.853 -
   1.854 -            sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2, 0, vertices);
   1.855 -        }
   1.856 -    }
   1.857 -
   1.858 -    if(alpha != 255)
   1.859 -        sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
   1.860      return 0;
   1.861  }
   1.862  
   1.863  static int
   1.864  PSP_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   1.865                      Uint32 pixel_format, void * pixels, int pitch)
   1.866 -
   1.867  {
   1.868      return SDL_Unsupported();
   1.869  }
   1.870  
   1.871 -
   1.872 -static int
   1.873 -PSP_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
   1.874 -                const SDL_Rect * srcrect, const SDL_FRect * dstrect,
   1.875 -                const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
   1.876 -{
   1.877 -    float x, y, width, height;
   1.878 -    float u0, v0, u1, v1;
   1.879 -    unsigned char alpha;
   1.880 -    float centerx, centery;
   1.881 -
   1.882 -    x = dstrect->x;
   1.883 -    y = dstrect->y;
   1.884 -    width = dstrect->w;
   1.885 -    height = dstrect->h;
   1.886 -
   1.887 -    u0 = srcrect->x;
   1.888 -    v0 = srcrect->y;
   1.889 -    u1 = srcrect->x + srcrect->w;
   1.890 -    v1 = srcrect->y + srcrect->h;
   1.891 -
   1.892 -    centerx = center->x;
   1.893 -    centery = center->y;
   1.894 -
   1.895 -    alpha = texture->a;
   1.896 -
   1.897 -    StartDrawing(renderer);
   1.898 -    TextureActivate(texture);
   1.899 -    PSP_SetBlendMode(renderer, renderer->blendMode);
   1.900 -
   1.901 -    if(alpha != 255)
   1.902 -    {
   1.903 -        sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
   1.904 -        sceGuColor(GU_RGBA(255, 255, 255, alpha));
   1.905 -    }else{
   1.906 -        sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
   1.907 -        sceGuColor(0xFFFFFFFF);
   1.908 -    }
   1.909 -
   1.910 -/*      x += width * 0.5f; */
   1.911 -/*      y += height * 0.5f; */
   1.912 -    x += centerx;
   1.913 -    y += centery;
   1.914 -
   1.915 -    float c, s;
   1.916 -
   1.917 -    MathSincos(degToRad(angle), &s, &c);
   1.918 -
   1.919 -/*      width *= 0.5f; */
   1.920 -/*      height *= 0.5f; */
   1.921 -    width  -= centerx;
   1.922 -    height -= centery;
   1.923 -
   1.924 -
   1.925 -    float cw = c*width;
   1.926 -    float sw = s*width;
   1.927 -    float ch = c*height;
   1.928 -    float sh = s*height;
   1.929 -
   1.930 -    VertTV* vertices = (VertTV*)sceGuGetMemory(sizeof(VertTV)<<2);
   1.931 -
   1.932 -    vertices[0].u = u0;
   1.933 -    vertices[0].v = v0;
   1.934 -    vertices[0].x = x - cw + sh;
   1.935 -    vertices[0].y = y - sw - ch;
   1.936 -    vertices[0].z = 0;
   1.937 -
   1.938 -    vertices[1].u = u0;
   1.939 -    vertices[1].v = v1;
   1.940 -    vertices[1].x = x - cw - sh;
   1.941 -    vertices[1].y = y - sw + ch;
   1.942 -    vertices[1].z = 0;
   1.943 -
   1.944 -    vertices[2].u = u1;
   1.945 -    vertices[2].v = v1;
   1.946 -    vertices[2].x = x + cw - sh;
   1.947 -    vertices[2].y = y + sw + ch;
   1.948 -    vertices[2].z = 0;
   1.949 -
   1.950 -    vertices[3].u = u1;
   1.951 -    vertices[3].v = v0;
   1.952 -    vertices[3].x = x + cw + sh;
   1.953 -    vertices[3].y = y + sw - ch;
   1.954 -    vertices[3].z = 0;
   1.955 -
   1.956 -    if (flip & SDL_FLIP_VERTICAL) {
   1.957 -                Swap(&vertices[0].v, &vertices[2].v);
   1.958 -                Swap(&vertices[1].v, &vertices[3].v);
   1.959 -    }
   1.960 -    if (flip & SDL_FLIP_HORIZONTAL) {
   1.961 -                Swap(&vertices[0].u, &vertices[2].u);
   1.962 -                Swap(&vertices[1].u, &vertices[3].u);
   1.963 -    }
   1.964 -
   1.965 -    sceGuDrawArray(GU_TRIANGLE_FAN, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 4, 0, vertices);
   1.966 -
   1.967 -    if(alpha != 255)
   1.968 -        sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
   1.969 -    return 0;
   1.970 -}
   1.971 -
   1.972  static void
   1.973  PSP_RenderPresent(SDL_Renderer * renderer)
   1.974  {
   1.975 @@ -1010,6 +911,136 @@
   1.976      SDL_free(renderer);
   1.977  }
   1.978  
   1.979 +SDL_Renderer *
   1.980 +PSP_CreateRenderer(SDL_Window * window, Uint32 flags)
   1.981 +{
   1.982 +
   1.983 +    SDL_Renderer *renderer;
   1.984 +    PSP_RenderData *data;
   1.985 +        int pixelformat;
   1.986 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   1.987 +    if (!renderer) {
   1.988 +        SDL_OutOfMemory();
   1.989 +        return NULL;
   1.990 +    }
   1.991 +
   1.992 +    data = (PSP_RenderData *) SDL_calloc(1, sizeof(*data));
   1.993 +    if (!data) {
   1.994 +        PSP_DestroyRenderer(renderer);
   1.995 +        SDL_OutOfMemory();
   1.996 +        return NULL;
   1.997 +    }
   1.998 +
   1.999 +
  1.1000 +    renderer->WindowEvent = PSP_WindowEvent;
  1.1001 +    renderer->CreateTexture = PSP_CreateTexture;
  1.1002 +    renderer->SetTextureColorMod = PSP_SetTextureColorMod;
  1.1003 +    renderer->UpdateTexture = PSP_UpdateTexture;
  1.1004 +    renderer->LockTexture = PSP_LockTexture;
  1.1005 +    renderer->UnlockTexture = PSP_UnlockTexture;
  1.1006 +    renderer->SetRenderTarget = PSP_SetRenderTarget;
  1.1007 +    renderer->QueueSetViewport = PSP_QueueSetViewport;
  1.1008 +    renderer->QueueSetDrawColor = PSP_QueueSetViewport;  /* SetViewport and SetDrawColor are (currently) no-ops. */
  1.1009 +    renderer->QueueDrawPoints = PSP_QueueDrawPoints;
  1.1010 +    renderer->QueueDrawLines = PSP_QueueDrawPoints;  /* lines and points queue vertices the same way. */
  1.1011 +    renderer->QueueFillRects = PSP_QueueFillRects;
  1.1012 +    renderer->QueueCopy = PSP_QueueCopy;
  1.1013 +    renderer->QueueCopyEx = PSP_QueueCopyEx;
  1.1014 +    renderer->RunCommandQueue = PSP_RunCommandQueue;
  1.1015 +    renderer->RenderReadPixels = PSP_RenderReadPixels;
  1.1016 +    renderer->RenderPresent = PSP_RenderPresent;
  1.1017 +    renderer->DestroyTexture = PSP_DestroyTexture;
  1.1018 +    renderer->DestroyRenderer = PSP_DestroyRenderer;
  1.1019 +    renderer->info = PSP_RenderDriver.info;
  1.1020 +    renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
  1.1021 +    renderer->driverdata = data;
  1.1022 +    renderer->window = window;
  1.1023 +
  1.1024 +    if (data->initialized != SDL_FALSE)
  1.1025 +        return 0;
  1.1026 +    data->initialized = SDL_TRUE;
  1.1027 +
  1.1028 +    if (flags & SDL_RENDERER_PRESENTVSYNC) {
  1.1029 +        data->vsync = SDL_TRUE;
  1.1030 +    } else {
  1.1031 +        data->vsync = SDL_FALSE;
  1.1032 +    }
  1.1033 +
  1.1034 +    pixelformat=PixelFormatToPSPFMT(SDL_GetWindowPixelFormat(window));
  1.1035 +    switch(pixelformat)
  1.1036 +    {
  1.1037 +        case GU_PSM_4444:
  1.1038 +        case GU_PSM_5650:
  1.1039 +        case GU_PSM_5551:
  1.1040 +            data->frontbuffer = (unsigned int *)(PSP_FRAME_BUFFER_SIZE<<1);
  1.1041 +            data->backbuffer =  (unsigned int *)(0);
  1.1042 +            data->bpp = 2;
  1.1043 +            data->psm = pixelformat;
  1.1044 +            break;
  1.1045 +        default:
  1.1046 +            data->frontbuffer = (unsigned int *)(PSP_FRAME_BUFFER_SIZE<<2);
  1.1047 +            data->backbuffer =  (unsigned int *)(0);
  1.1048 +            data->bpp = 4;
  1.1049 +            data->psm = GU_PSM_8888;
  1.1050 +            break;
  1.1051 +    }
  1.1052 +
  1.1053 +    sceGuInit();
  1.1054 +    /* setup GU */
  1.1055 +    sceGuStart(GU_DIRECT, DisplayList);
  1.1056 +    sceGuDrawBuffer(data->psm, data->frontbuffer, PSP_FRAME_BUFFER_WIDTH);
  1.1057 +    sceGuDispBuffer(PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT, data->backbuffer, PSP_FRAME_BUFFER_WIDTH);
  1.1058 +
  1.1059 +
  1.1060 +    sceGuOffset(2048 - (PSP_SCREEN_WIDTH>>1), 2048 - (PSP_SCREEN_HEIGHT>>1));
  1.1061 +    sceGuViewport(2048, 2048, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
  1.1062 +
  1.1063 +    data->frontbuffer = vabsptr(data->frontbuffer);
  1.1064 +    data->backbuffer = vabsptr(data->backbuffer);
  1.1065 +
  1.1066 +    /* Scissoring */
  1.1067 +    sceGuScissor(0, 0, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT);
  1.1068 +    sceGuEnable(GU_SCISSOR_TEST);
  1.1069 +
  1.1070 +    /* Backface culling */
  1.1071 +    sceGuFrontFace(GU_CCW);
  1.1072 +    sceGuEnable(GU_CULL_FACE);
  1.1073 +
  1.1074 +    /* Texturing */
  1.1075 +    sceGuEnable(GU_TEXTURE_2D);
  1.1076 +    sceGuShadeModel(GU_SMOOTH);
  1.1077 +    sceGuTexWrap(GU_REPEAT, GU_REPEAT);
  1.1078 +
  1.1079 +    /* Blending */
  1.1080 +    sceGuEnable(GU_BLEND);
  1.1081 +    sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  1.1082 +
  1.1083 +    sceGuTexFilter(GU_LINEAR,GU_LINEAR);
  1.1084 +
  1.1085 +    sceGuFinish();
  1.1086 +    sceGuSync(0,0);
  1.1087 +    sceDisplayWaitVblankStartCB();
  1.1088 +    sceGuDisplay(GU_TRUE);
  1.1089 +
  1.1090 +    return renderer;
  1.1091 +}
  1.1092 +
  1.1093 +SDL_RenderDriver PSP_RenderDriver = {
  1.1094 +    .CreateRenderer = PSP_CreateRenderer,
  1.1095 +    .info = {
  1.1096 +        .name = "PSP",
  1.1097 +        .flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE,
  1.1098 +        .num_texture_formats = 4,
  1.1099 +        .texture_formats = { [0] = SDL_PIXELFORMAT_BGR565,
  1.1100 +                                                 [1] = SDL_PIXELFORMAT_ABGR1555,
  1.1101 +                                                 [2] = SDL_PIXELFORMAT_ABGR4444,
  1.1102 +                                                 [3] = SDL_PIXELFORMAT_ABGR8888,
  1.1103 +        },
  1.1104 +        .max_texture_width = 512,
  1.1105 +        .max_texture_height = 512,
  1.1106 +     }
  1.1107 +};
  1.1108 +
  1.1109  #endif /* SDL_VIDEO_RENDER_PSP */
  1.1110  
  1.1111  /* vi: set ts=4 sw=4 expandtab: */