Skip to content

Commit

Permalink
ogc: implement locking textures, add more texture formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mardy authored and WinterMute committed Jan 14, 2024
1 parent 98b9798 commit 67aaabf
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 92 deletions.
43 changes: 35 additions & 8 deletions src/render/ogc/SDL_render_ogc.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "../../video/ogc/SDL_ogcvideo.h"

#include <malloc.h>
#include <ogc/cache.h>
#include <ogc/gx.h>
#include <ogc/video.h>

Expand All @@ -47,7 +48,10 @@ typedef struct
typedef struct
{
void *texels;
void *pixels;
SDL_Rect pixels_rect;
u16 pitch;
u16 pixels_pitch;
u8 format;
u8 needed_stages; // Normally 1, set to 2 for palettized formats
} OGC_TextureData;
Expand Down Expand Up @@ -125,24 +129,45 @@ static int OGC_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
static int OGC_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, void **pixels, int *pitch)
{
// TODO
OGC_TextureData *ogc_tex = texture->driverdata;

ogc_tex->pixels = SDL_malloc(rect->w * rect->h * SDL_BYTESPERPIXEL(texture->format));
ogc_tex->pixels_pitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
ogc_tex->pixels_rect = *rect;
*pixels = ogc_tex->pixels;
*pitch = ogc_tex->pixels_pitch;
return 0;
}

static void OGC_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
// TODO
OGC_TextureData *ogc_tex = texture->driverdata;
u32 texture_size;

OGC_pixels_to_texture(ogc_tex->pixels, texture->format,
&ogc_tex->pixels_rect, ogc_tex->pixels_pitch,
ogc_tex->texels, texture->w);
/* It would be more effective if we updated only the changed range here,
* but the complexity is probably not worth the effort. */
texture_size = GX_GetTexBufferSize(texture->w, texture->h, ogc_tex->format,
GX_FALSE, 0);
DCStoreRange(ogc_tex->texels, texture_size);
GX_InvalidateTexAll();


if (ogc_tex->pixels) {
SDL_free(ogc_tex->pixels);
ogc_tex->pixels = NULL;
}
}

static int OGC_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, const void *pixels, int pitch)
{
OGC_TextureData *ogc_tex = texture->driverdata;
u8 format;

// TODO: take rect into account
OGC_pixels_to_texture((void*)pixels, texture->format, texture->w, texture->h,
pitch, ogc_tex->texels, &format);
OGC_pixels_to_texture((void*)pixels, texture->format, rect,
pitch, ogc_tex->texels, texture->w);

return 0;
}
Expand Down Expand Up @@ -287,7 +312,6 @@ static int OGC_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL

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

float v_aspect = viewport->h / 2.0;
Expand Down Expand Up @@ -589,10 +613,13 @@ SDL_RenderDriver OGC_RenderDriver = {
.info = {
.name = "ogc",
.flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE,
.num_texture_formats = 2,
.num_texture_formats = 5,
.texture_formats = {
[0] = SDL_PIXELFORMAT_RGB565,
[1] = SDL_PIXELFORMAT_RGBA8888,
[2] = SDL_PIXELFORMAT_ARGB8888,
[3] = SDL_PIXELFORMAT_RGB24,
[4] = SDL_PIXELFORMAT_XRGB8888,
// TODO: add more
},
.max_texture_width = 1024,
Expand Down
14 changes: 11 additions & 3 deletions src/video/ogc/SDL_ogcframebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "../SDL_sysvideo.h"
#include "SDL_ogcframebuffer_c.h"
#include "SDL_ogcgxcommon.h"
#include "SDL_ogcpixels.h"
#include "SDL_ogcvideo.h"

#include <malloc.h>
Expand Down Expand Up @@ -95,11 +96,18 @@ int SDL_OGC_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, v
int SDL_OGC_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
{
SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
u32 texture_size;
u8 gx_format;

OGC_prepare_texels(windowdata->pixels, window->w, window->h, window->surface->pitch,
windowdata->surface_format,
windowdata->texels, &gx_format);
gx_format = OGC_texture_format_from_SDL(windowdata->surface_format);
for (int i = 0; i < numrects; i++) {
OGC_pixels_to_texture(windowdata->pixels, windowdata->surface_format, &rects[i],
window->surface->pitch, windowdata->texels, window->w);
}
texture_size = GX_GetTexBufferSize(window->w, window->h, gx_format,
GX_FALSE, 0);
DCStoreRange(windowdata->texels, texture_size);
GX_InvalidateTexAll();
OGC_load_texture(windowdata->texels, window->w, window->h, gx_format);
draw_screen_rect(window);
GX_DrawDone();
Expand Down
12 changes: 0 additions & 12 deletions src/video/ogc/SDL_ogcgxcommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,6 @@ void OGC_draw_init(int w, int h, int h_aspect, int v_aspect)
GX_InvVtxCache(); // update vertex cache
}

void OGC_prepare_texels(void *pixels, int w, int h, int pitch, Uint32 format,
void *texels, u8 *gx_format)
{
u32 texture_size;

OGC_pixels_to_texture(pixels, format, w, h, pitch,
texels, gx_format);
texture_size = GX_GetTexBufferSize(w, h, *gx_format, GX_FALSE, 0);
DCStoreRange(texels, texture_size);
GX_InvalidateTexAll();
}

void OGC_load_texture(void *texels, int w, int h, u8 format)
{
GXTexObj texobj_a, texobj_b;
Expand Down
2 changes: 0 additions & 2 deletions src/video/ogc/SDL_ogcgxcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

void OGC_draw_init(int w, int h, int h_aspect, int v_aspect);
void OGC_set_viewport(int x, int y, int w, int h, float h_aspect, float v_aspect);
void OGC_prepare_texels(void *pixels, int w, int h, int pitch, Uint32 format,
void *texels, u8 *gx_format);
void OGC_load_texture(void *texels, int w, int h, u8 gx_format);

#endif /* SDL_ogcgxcommon_h_ */
Expand Down
Loading

0 comments on commit 67aaabf

Please sign in to comment.