sdl2-sys 0.38.0

Raw SDL2 bindings for Rust, used internally rust-sdl2
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"

#if SDL_VIDEO_RENDER_PS2

#include "../SDL_sysrender.h"
#include "SDL_hints.h"

#include <kernel.h>
#include <malloc.h>
#include <gsKit.h>
#include <dmaKit.h>
#include <gsToolkit.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
#include <gsInline.h>
#pragma GCC diagnostic pop

/* turn black GS Screen */
#define GS_BLACK GS_SETREG_RGBA(0x00, 0x00, 0x00, 0x80)
/* Size of Persistent drawbuffer (Single Buffered) */
#define RENDER_QUEUE_PER_POOLSIZE 1024 * 256 // 256K of persistent renderqueue
/* Size of Oneshot drawbuffer (Double Buffered, so it uses this size * 2) */
#define RENDER_QUEUE_OS_POOLSIZE 1024 * 1024 * 2 // 2048K of oneshot renderqueue

typedef struct
{
    GSGLOBAL *gsGlobal;
    uint64_t drawColor;
    int32_t vsync_callback_id;
    uint8_t vsync; /* 0 (Disabled), 1 (Enabled), 2 (Dynamic) */
} PS2_RenderData;


static int vsync_sema_id = 0;

/* PRIVATE METHODS */
static int vsync_handler()
{
   iSignalSema(vsync_sema_id);

   ExitHandler();
   return 0;
}

/* Copy of gsKit_sync_flip, but without the 'flip' */
static void gsKit_sync(GSGLOBAL *gsGlobal)
{
   if (!gsGlobal->FirstFrame) WaitSema(vsync_sema_id);
   while (PollSema(vsync_sema_id) >= 0)
   	;
}

/* Copy of gsKit_sync_flip, but without the 'sync' */
static void gsKit_flip(GSGLOBAL *gsGlobal)
{
   if (!gsGlobal->FirstFrame)
   {
      if (gsGlobal->DoubleBuffering == GS_SETTING_ON)
      {
         GS_SET_DISPFB2( gsGlobal->ScreenBuffer[
               gsGlobal->ActiveBuffer & 1] / 8192,
               gsGlobal->Width / 64, gsGlobal->PSM, 0, 0 );

         gsGlobal->ActiveBuffer ^= 1;
      }

   }

   gsKit_setactive(gsGlobal);
}

static int PixelFormatToPS2PSM(Uint32 format)
{
    switch (format) {
    case SDL_PIXELFORMAT_ABGR1555:
        return GS_PSM_CT16;
    default:
        return GS_PSM_CT32;
    }
}

static void
PS2_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{

}

static int
PS2_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
    GSTEXTURE* ps2_tex = (GSTEXTURE*) SDL_calloc(1, sizeof(GSTEXTURE));

    if (!ps2_tex)
        return SDL_OutOfMemory();

    ps2_tex->Width = texture->w;
    ps2_tex->Height = texture->h;
    ps2_tex->PSM = PixelFormatToPS2PSM(texture->format);
    ps2_tex->Mem = memalign(128, gsKit_texture_size_ee(ps2_tex->Width, ps2_tex->Height, ps2_tex->PSM));

    if (!ps2_tex->Mem)
    {
        SDL_free(ps2_tex);
        return SDL_OutOfMemory();
    }

    texture->driverdata = ps2_tex;

    return 0;
}

static int
PS2_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
                 const SDL_Rect * rect, void **pixels, int *pitch)
{
    GSTEXTURE *ps2_texture = (GSTEXTURE *) texture->driverdata;

    *pixels =
        (void *) ((Uint8 *) ps2_texture->Mem + rect->y * ps2_texture->Width * SDL_BYTESPERPIXEL(texture->format) +
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
    *pitch = ps2_texture->Width * SDL_BYTESPERPIXEL(texture->format);
    return 0;
}

static void
PS2_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
    GSTEXTURE *ps2_texture = (GSTEXTURE *) texture->driverdata;
    PS2_RenderData *data = (PS2_RenderData *) renderer->driverdata;

    gsKit_TexManager_invalidate(data->gsGlobal, ps2_texture);
}

static int
PS2_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
                   const SDL_Rect * rect, const void *pixels, int pitch)
{
    const Uint8 *src;
    Uint8 *dst;
    int row, length,dpitch;
    src = pixels;

    PS2_LockTexture(renderer, texture, rect, (void **)&dst, &dpitch);
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
    if (length == pitch && length == dpitch) {
        SDL_memcpy(dst, src, length*rect->h);
    } else {
        for (row = 0; row < rect->h; ++row) {
            SDL_memcpy(dst, src, length);
            src += pitch;
            dst += dpitch;
        }
    }

    PS2_UnlockTexture(renderer, texture);

    return 0;
}

static void
PS2_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture, SDL_ScaleMode scaleMode)
{
    GSTEXTURE *ps2_texture = (GSTEXTURE *) texture->driverdata;
    /*
     set texture filtering according to scaleMode
     suported hint values are nearest (0, default) or linear (1)
     gskit scale mode is either GS_FILTER_NEAREST (good for tile-map)
     or GS_FILTER_LINEAR (good for scaling)
     */
    uint32_t gsKitScaleMode = (scaleMode == SDL_ScaleModeNearest
                        ? GS_FILTER_NEAREST
                        : GS_FILTER_LINEAR);
    ps2_texture->Filter = gsKitScaleMode;
}

static int
PS2_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
{
    return 0;
}

static int
PS2_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
{
    return 0;  /* nothing to do in this backend. */
}

static int
PS2_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
{
    PS2_RenderData *data = (PS2_RenderData *) renderer->driverdata;
    GSPRIMPOINT *vertices = (GSPRIMPOINT *) SDL_AllocateRenderVertices(renderer, count * sizeof (GSPRIMPOINT), 4, &cmd->data.draw.first);
    uint8_t colorR, colorG, colorB, colorA;
    gs_rgbaq rgbaq;
    int i;

    if (!vertices) {
        return -1;
    }

    cmd->data.draw.count = count;

    colorR = cmd->data.draw.r >> 1;
    colorG = cmd->data.draw.g >> 1;
    colorB = cmd->data.draw.b >> 1;
    colorA = cmd->data.draw.a >> 1;
    rgbaq = color_to_RGBAQ(colorR, colorG, colorB, colorA, 0.0f);

    for (i = 0; i < count; i++, vertices++, points++) {
        vertices->xyz2 = vertex_to_XYZ2(data->gsGlobal, points->x, points->y, 0);
        vertices->rgbaq = rgbaq;
    }
    return 0;
}

static int
PS2_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
        const float *xy, int xy_stride, const SDL_Color *color, int color_stride, const float *uv, int uv_stride,
        int num_vertices, const void *indices, int num_indices, int size_indices,
        float scale_x, float scale_y)
{
    int i;
    int count = indices ? num_indices : num_vertices;
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;

    cmd->data.draw.count = count;
    size_indices = indices ? size_indices : 0;

    if (texture) {
        GSPRIMSTQPOINT *vertices = (GSPRIMSTQPOINT *) SDL_AllocateRenderVertices(renderer, count * sizeof (GSPRIMSTQPOINT), 4, &cmd->data.draw.first);
        
        if (!vertices) {
            return -1;
        }

        for (i = 0; i < count; i++) {
            int j;
            float *xy_;
            float *uv_;
            SDL_Color col_;
            if (size_indices == 4) {
                j = ((const Uint32 *)indices)[i];
            } else if (size_indices == 2) {
                j = ((const Uint16 *)indices)[i];
            } else if (size_indices == 1) {
                j = ((const Uint8 *)indices)[i];
            } else {
                j = i;
            }

            xy_ = (float *)((char*)xy + j * xy_stride);
            col_ = *(SDL_Color *)((char*)color + j * color_stride);
            uv_ = (float *)((char*)uv + j * uv_stride);

            vertices->xyz2 = vertex_to_XYZ2(data->gsGlobal, xy_[0] * scale_x, xy_[1] * scale_y, 0);
            vertices->stq = vertex_to_STQ(uv_[0], uv_[1]);
            vertices->rgbaq = color_to_RGBAQ(col_.r >> 1, col_.g >> 1, col_.b >> 1, col_.a >> 1, 1.0f);

            vertices++;
        }
        
    } else {
        GSPRIMPOINT *vertices = (GSPRIMPOINT *) SDL_AllocateRenderVertices(renderer, count * sizeof (GSPRIMPOINT), 4, &cmd->data.draw.first);

        if (!vertices) {
            return -1;
        }

        for (i = 0; i < count; i++) {
            int j;
            float *xy_;
            SDL_Color col_;
            if (size_indices == 4) {
                j = ((const Uint32 *)indices)[i];
            } else if (size_indices == 2) {
                j = ((const Uint16 *)indices)[i];
            } else if (size_indices == 1) {
                j = ((const Uint8 *)indices)[i];
            } else {
                j = i;
            }

            xy_ = (float *)((char*)xy + j * xy_stride);
            col_ = *(SDL_Color *)((char*)color + j * color_stride);

            vertices->xyz2 = vertex_to_XYZ2(data->gsGlobal, xy_[0] * scale_x, xy_[1] * scale_y, 0);
            vertices->rgbaq = color_to_RGBAQ(col_.r >> 1, col_.g >> 1, col_.b >> 1, col_.a >> 1, 0.0f);

            vertices++;
        }
    }

    return 0;
}

static int
PS2_RenderSetViewPort(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;
    const SDL_Rect *viewport = &cmd->data.viewport.rect;

    gsKit_set_display_offset(data->gsGlobal, viewport->x, viewport->y);
    gsKit_set_scissor(data->gsGlobal, GS_SETREG_SCISSOR(viewport->x, viewport->y, viewport->w, viewport->h));

    return 0;
}

static int
PS2_RenderSetClipRect(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;

    const SDL_Rect *rect = &cmd->data.cliprect.rect;

    if(cmd->data.cliprect.enabled){
        gsKit_set_scissor(data->gsGlobal, GS_SETREG_SCISSOR(rect->x, rect->y, rect->w, rect->h));
    } else {
        gsKit_set_scissor(data->gsGlobal, GS_SCISSOR_RESET);
    }

    return 0;
}


static int
PS2_RenderSetDrawColor(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
    int colorR, colorG, colorB, colorA;

    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;
    
    colorR = (cmd->data.color.r) >> 1;
    colorG = (cmd->data.color.g) >> 1;
    colorB = (cmd->data.color.b) >> 1;
    colorA = (cmd->data.color.a) >> 1;
    data->drawColor = GS_SETREG_RGBAQ(colorR, colorG, colorB, colorA, 0x00);
    return 0;
}

static int
PS2_RenderClear(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
    int colorR, colorG, colorB, colorA;

    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;
    
    colorR = (cmd->data.color.r) >> 1;
    colorG = (cmd->data.color.g) >> 1;
    colorB = (cmd->data.color.b) >> 1;
    colorA = (cmd->data.color.a) >> 1;
    gsKit_clear(data->gsGlobal, GS_SETREG_RGBAQ(colorR, colorG, colorB, colorA, 0x00));
    
    return 0;
}

static void
PS2_SetBlendMode(PS2_RenderData *data, int blendMode)
{
    #define A_COLOR_SOURCE 0
    #define A_COLOR_DEST 1
    #define A_COLOR_NULL 2
    #define A_ALPHA_SOURCE 0
    #define A_ALPHA_DEST 1
    #define A_ALPHA_FIX 2

    switch (blendMode)
    {
        case SDL_BLENDMODE_NONE: {
            data->gsGlobal->PrimAlphaEnable = GS_SETTING_OFF;
            break;
        }
        case SDL_BLENDMODE_BLEND:{
            gsKit_set_primalpha(data->gsGlobal, GS_SETREG_ALPHA(A_COLOR_SOURCE, A_COLOR_DEST, A_ALPHA_SOURCE, A_COLOR_DEST, 0), 0);
            data->gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
            break;
        }
        case SDL_BLENDMODE_ADD: {
            gsKit_set_primalpha(data->gsGlobal, GS_SETREG_ALPHA(A_COLOR_SOURCE, A_COLOR_NULL, A_ALPHA_FIX, A_COLOR_DEST, 0x80), 0);
            data->gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
            break;
        }
        case SDL_BLENDMODE_MUL: 
        case SDL_BLENDMODE_MOD: {
            /* We don't fully support MOD and MUL, however this is the best we can do */
            gsKit_set_primalpha(data->gsGlobal, GS_SETREG_ALPHA(A_COLOR_DEST, A_COLOR_NULL, A_ALPHA_SOURCE, A_COLOR_SOURCE, 0x80), 0);
            data->gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
            break;
        }
    }
}

static int
PS2_RenderGeometry(SDL_Renderer *renderer, void *vertices, SDL_RenderCommand *cmd)
{
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;
    const size_t count = cmd->data.draw.count;

    PS2_SetBlendMode(data, cmd->data.draw.blend);

    if (cmd->data.draw.texture) {
        const GSPRIMSTQPOINT *verts = (GSPRIMSTQPOINT *) (vertices + cmd->data.draw.first);
        GSTEXTURE *ps2_tex = (GSTEXTURE *) cmd->data.draw.texture->driverdata;

        gsKit_TexManager_bind(data->gsGlobal, ps2_tex);
        gsKit_prim_list_triangle_goraud_texture_stq_3d(data->gsGlobal, ps2_tex, count, verts);
    } else {
        const GSPRIMPOINT *verts = (GSPRIMPOINT *) (vertices + cmd->data.draw.first);
        gsKit_prim_list_triangle_gouraud_3d(data->gsGlobal, count, verts);
    } 
    
    return 0;
}


int
PS2_RenderLines(SDL_Renderer *renderer, void *vertices, SDL_RenderCommand * cmd)
{
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;
    const size_t count = cmd->data.draw.count;
    const GSPRIMPOINT *verts = (GSPRIMPOINT *) (vertices + cmd->data.draw.first);

    PS2_SetBlendMode(data, cmd->data.draw.blend);
    gsKit_prim_list_line_goraud_3d(data->gsGlobal, count, verts);

    /* We're done! */
    return 0;
}

int
PS2_RenderPoints(SDL_Renderer *renderer, void *vertices, SDL_RenderCommand * cmd)
{
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;
    const size_t count = cmd->data.draw.count;
    const GSPRIMPOINT *verts = (GSPRIMPOINT *) (vertices + cmd->data.draw.first);

    PS2_SetBlendMode(data, cmd->data.draw.blend);
    gsKit_prim_list_points(data->gsGlobal, count, verts);

    /* We're done! */
    return 0;
}

static int
PS2_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
{
    while (cmd) {
        switch (cmd->command) {
            case SDL_RENDERCMD_SETVIEWPORT: {
                PS2_RenderSetViewPort(renderer, cmd);
                break;
            }
            case SDL_RENDERCMD_SETCLIPRECT: {
                PS2_RenderSetClipRect(renderer, cmd);
                break;
            }
            case SDL_RENDERCMD_SETDRAWCOLOR: {
                PS2_RenderSetDrawColor(renderer, cmd);
                break;
            }
            case SDL_RENDERCMD_CLEAR: {
                PS2_RenderClear(renderer, cmd);
                break;
            }
            case SDL_RENDERCMD_DRAW_POINTS: {
                PS2_RenderPoints(renderer, vertices, cmd);
                break;
            }
            case SDL_RENDERCMD_DRAW_LINES: {
                PS2_RenderLines(renderer, vertices, cmd);
                break;
            }
            case SDL_RENDERCMD_FILL_RECTS: /* unused */
                break;
            case SDL_RENDERCMD_COPY: /* unused */
                break;
            case SDL_RENDERCMD_COPY_EX: /* unused */
                break;
            case SDL_RENDERCMD_GEOMETRY: {
                PS2_RenderGeometry(renderer, vertices, cmd);
                break;
            }
            case SDL_RENDERCMD_NO_OP: 
                break;
        }
        cmd = cmd->next;
    }
    return 0;
}

static int
PS2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
                    Uint32 format, void * pixels, int pitch)
{
    return SDL_Unsupported();
}

static int
PS2_RenderPresent(SDL_Renderer * renderer)
{
    PS2_RenderData *data = (PS2_RenderData *) renderer->driverdata;

    if (data->gsGlobal->DoubleBuffering == GS_SETTING_OFF) {
		if (data->vsync == 2) // Dynamic
            gsKit_sync(data->gsGlobal);
        else if (data->vsync == 1)
            gsKit_vsync_wait();
		gsKit_queue_exec(data->gsGlobal);
    } else {
		gsKit_queue_exec(data->gsGlobal);
		gsKit_finish();
		if (data->vsync == 2) // Dynamic
            gsKit_sync(data->gsGlobal);
        else if (data->vsync == 1)
            gsKit_vsync_wait();
		gsKit_flip(data->gsGlobal);
	}
	gsKit_TexManager_nextFrame(data->gsGlobal);
    gsKit_clear(data->gsGlobal, GS_BLACK);
    return 0;
}

static void
PS2_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
    GSTEXTURE *ps2_texture = (GSTEXTURE *) texture->driverdata;
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;

    if (data == NULL)
        return;

    if(ps2_texture == NULL)
        return;

    // Free from vram
    gsKit_TexManager_free(data->gsGlobal, ps2_texture);
    
    SDL_free(ps2_texture->Mem);
    SDL_free(ps2_texture);
    texture->driverdata = NULL;
}

static void
PS2_DestroyRenderer(SDL_Renderer * renderer)
{
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;

    if (data) {
        gsKit_clear(data->gsGlobal, GS_BLACK);
        gsKit_vram_clear(data->gsGlobal);
        gsKit_deinit_global(data->gsGlobal);
        gsKit_remove_vsync_handler(data->vsync_callback_id);

        SDL_free(data);
    }

    if (vsync_sema_id >= 0)
        DeleteSema(vsync_sema_id);

    SDL_free(renderer);
}

static int
PS2_SetVSync(SDL_Renderer * renderer, const int vsync)
{
    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;
    SDL_bool dynamicVsync = SDL_GetHintBoolean(SDL_HINT_PS2_DYNAMIC_VSYNC, SDL_FALSE);
    data->vsync = vsync ? (dynamicVsync ? 2 : 1) : 0;
    return 0;
}

static SDL_Renderer *
PS2_CreateRenderer(SDL_Window * window, Uint32 flags)
{
    SDL_Renderer *renderer;
    PS2_RenderData *data;
    GSGLOBAL *gsGlobal;
    ee_sema_t sema;
    SDL_bool dynamicVsync;

    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    if (!renderer) {
        SDL_OutOfMemory();
        return NULL;
    }

    data = (PS2_RenderData *) SDL_calloc(1, sizeof(*data));
    if (!data) {
        PS2_DestroyRenderer(renderer);
        SDL_OutOfMemory();
        return NULL;
    }

    /* Specific gsKit init */
    sema.init_count = 0;
    sema.max_count = 1;
    sema.option = 0;
    vsync_sema_id = CreateSema(&sema);
    
    gsGlobal = gsKit_init_global_custom(RENDER_QUEUE_OS_POOLSIZE, RENDER_QUEUE_PER_POOLSIZE);

	gsGlobal->Mode = GS_MODE_NTSC;
    gsGlobal->Height = 448;

	gsGlobal->PSM  = GS_PSM_CT24;
	gsGlobal->PSMZ = GS_PSMZ_16S;
	gsGlobal->ZBuffering = GS_SETTING_OFF;
	gsGlobal->DoubleBuffering = GS_SETTING_ON;
	gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
	gsGlobal->Dithering = GS_SETTING_OFF;

	gsKit_set_primalpha(gsGlobal, GS_SETREG_ALPHA(0, 1, 0, 1, 0), 0);

	dmaKit_init(D_CTRL_RELE_OFF, D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC, D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF);
	dmaKit_chan_init(DMA_CHANNEL_GIF);

	gsKit_set_clamp(gsGlobal, GS_CMODE_REPEAT);

	gsKit_vram_clear(gsGlobal);

	gsKit_init_screen(gsGlobal);

	gsKit_TexManager_init(gsGlobal);

	data->vsync_callback_id = gsKit_add_vsync_handler(vsync_handler);

	gsKit_mode_switch(gsGlobal, GS_ONESHOT);

    gsKit_clear(gsGlobal, GS_BLACK);

    data->gsGlobal = gsGlobal;
    dynamicVsync = SDL_GetHintBoolean(SDL_HINT_PS2_DYNAMIC_VSYNC, SDL_FALSE);
    data->vsync = flags & SDL_RENDERER_PRESENTVSYNC ? (dynamicVsync ? 2 : 1) : 0;

    renderer->WindowEvent = PS2_WindowEvent;
    renderer->CreateTexture = PS2_CreateTexture;
    renderer->UpdateTexture = PS2_UpdateTexture;
    renderer->LockTexture = PS2_LockTexture;
    renderer->UnlockTexture = PS2_UnlockTexture;
    renderer->SetTextureScaleMode = PS2_SetTextureScaleMode;
    renderer->SetRenderTarget = PS2_SetRenderTarget;
    renderer->QueueSetViewport = PS2_QueueSetViewport;
    renderer->QueueSetDrawColor = PS2_QueueSetViewport;
    renderer->QueueDrawPoints = PS2_QueueDrawPoints;
    renderer->QueueDrawLines = PS2_QueueDrawPoints;
    renderer->QueueGeometry = PS2_QueueGeometry;
    renderer->RunCommandQueue = PS2_RunCommandQueue;
    renderer->RenderReadPixels = PS2_RenderReadPixels;
    renderer->RenderPresent = PS2_RenderPresent;
    renderer->DestroyTexture = PS2_DestroyTexture;
    renderer->DestroyRenderer = PS2_DestroyRenderer;
    renderer->SetVSync = PS2_SetVSync;
    renderer->info = PS2_RenderDriver.info;
    renderer->driverdata = data;
    renderer->window = window;

    return renderer;
}

SDL_RenderDriver PS2_RenderDriver = {
    .CreateRenderer = PS2_CreateRenderer,
    .info = {
        .name = "PS2 gsKit",
        .flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE,
        .num_texture_formats = 2,
        .texture_formats = { 
            [0] = SDL_PIXELFORMAT_ABGR1555,
            [1] = SDL_PIXELFORMAT_ABGR8888,
        },
        .max_texture_width = 1024,
        .max_texture_height = 1024,
    }
};

#endif /* SDL_VIDEO_RENDER_PS2 */

/* vi: set ts=4 sw=4 expandtab: */