From 6d2c47bda16fa938570adbe40c9ed03c95f9a6b6 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Thu, 21 Feb 2019 21:58:31 +0000 Subject: [PATCH] Implement GL_EXT_shared_texture_palette --- GL/private.h | 3 +++ GL/state.c | 18 ++++++++++++- GL/texture.c | 71 ++++++++++++++++++++++++++++++++----------------- include/glext.h | 2 ++ 4 files changed, 69 insertions(+), 25 deletions(-) diff --git a/GL/private.h b/GL/private.h index e0b9eef..c496e19 100644 --- a/GL/private.h +++ b/GL/private.h @@ -139,6 +139,9 @@ TextureObject* getBoundTexture(); GLubyte _glGetActiveTexture(); GLuint _glGetActiveClientTexture(); +GLboolean _glIsSharedTexturePaletteEnabled(); +void _glApplyColorTable(); + GLboolean isBlendingEnabled(); GLboolean _glIsMipmapComplete(const TextureObject* obj); GLubyte* _glGetMipmapLocation(TextureObject* obj, GLuint level); diff --git a/GL/state.c b/GL/state.c index 7ce4863..96c0bf2 100644 --- a/GL/state.c +++ b/GL/state.c @@ -27,6 +27,13 @@ static GLenum FRONT_FACE = GL_CCW; static GLboolean CULLING_ENABLED = GL_FALSE; static GLboolean COLOR_MATERIAL_ENABLED = GL_FALSE; +/* Is the shared texture palette enabled? */ +static GLboolean SHARED_PALETTE_ENABLED = GL_FALSE; + +GLboolean _glIsSharedTexturePaletteEnabled() { + return SHARED_PALETTE_ENABLED; +} + static int _calc_pvr_face_culling() { if(!CULLING_ENABLED) { return PVR_CULLING_NONE; @@ -214,6 +221,9 @@ void _glUpdatePVRTextureContext(pvr_poly_cxt_t* context, GLshort textureUnit) { } else { context->txr.enable = PVR_TEXTURE_DISABLE; } + + /* Apply the texture palette if necessary */ + _glApplyColorTable(); } static GLboolean LIGHTING_ENABLED = GL_FALSE; @@ -291,6 +301,9 @@ GLAPI void APIENTRY glEnable(GLenum cap) { case GL_COLOR_MATERIAL: COLOR_MATERIAL_ENABLED = GL_TRUE; break; + case GL_SHARED_TEXTURE_PALETTE_EXT: + SHARED_PALETTE_ENABLED = GL_TRUE; + break; case GL_LIGHT0: case GL_LIGHT1: case GL_LIGHT2: @@ -338,6 +351,9 @@ GLAPI void APIENTRY glDisable(GLenum cap) { case GL_COLOR_MATERIAL: COLOR_MATERIAL_ENABLED = GL_FALSE; break; + case GL_SHARED_TEXTURE_PALETTE_EXT: + SHARED_PALETTE_ENABLED = GL_FALSE; + break; case GL_LIGHT0: case GL_LIGHT1: case GL_LIGHT2: @@ -601,7 +617,7 @@ const GLbyte *glGetString(GLenum name) { return "GLdc 1.x"; case GL_EXTENSIONS: - return "GL_ARB_framebuffer_object, GL_ARB_multitexture, GL_ARB_texture_rg, GL_EXT_paletted_texture"; + return "GL_ARB_framebuffer_object, GL_ARB_multitexture, GL_ARB_texture_rg, GL_EXT_paletted_texture, GL_EXT_shared_texture_palette"; } return "GL_KOS_ERROR: ENUM Unsupported\n"; diff --git a/GL/texture.c b/GL/texture.c index cd028d3..d0219e4 100644 --- a/GL/texture.c +++ b/GL/texture.c @@ -17,26 +17,43 @@ static TextureObject* TEXTURE_UNITS[MAX_TEXTURE_UNITS] = {NULL, NULL}; static NamedArray TEXTURE_OBJECTS; static GLubyte ACTIVE_TEXTURE = 0; +static TexturePalette* SHARED_PALETTE = NULL; + static GLuint _determinePVRFormat(GLint internalFormat, GLenum type); #define PACK_ARGB8888(a,r,g,b) ( ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF) ) -static void _glApplyColorTable() { - TextureObject* active = getBoundTexture(); +void _glApplyColorTable() { + /* + * FIXME: + * + * - Different palette formats (GL_RGB -> PVR_PAL_RGB565) + * - Store the active palette, don't resubmit eah time + */ - if(!active) { - return; //? Unload the palette? Make White? - } + TexturePalette* src = NULL; - if(!active->palette || !active->palette->data) { - return; + if(_glIsSharedTexturePaletteEnabled()) { + src = SHARED_PALETTE; + } else { + TextureObject* active = getBoundTexture(); + + if(!active) { + return; //? Unload the palette? Make White? + } + + if(!active->palette || !active->palette->data) { + return; + } + + src = active->palette; } pvr_set_pal_format(PVR_PAL_ARGB8888); GLushort i = 0; - for(; i < active->palette->width; ++i) { - GLubyte* entry = &active->palette->data[i * 4]; + for(; i < src->width; ++i) { + GLubyte* entry = &src->data[i * 4]; pvr_set_pal_entry(i, PACK_ARGB8888(entry[3], entry[1], entry[2], entry[0])); } } @@ -117,6 +134,8 @@ static GLuint _glGetMipmapDataSize(TextureObject* obj) { GLubyte _glKosInitTextures() { named_array_init(&TEXTURE_OBJECTS, sizeof(TextureObject), 256); + + SHARED_PALETTE = (TexturePalette*) malloc(sizeof(TexturePalette)); return 1; } @@ -218,9 +237,6 @@ void APIENTRY glBindTexture(GLenum target, GLuint texture) { if(texture) { TEXTURE_UNITS[ACTIVE_TEXTURE] = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, texture); - - // If this is a paletted texture, we need to reapply the color table - _glApplyColorTable(); } else { TEXTURE_UNITS[ACTIVE_TEXTURE] = NULL; } @@ -947,20 +963,30 @@ GLAPI void APIENTRY glColorTableEXT(GLenum target, GLenum internalFormat, GLsize return; } - TextureObject* active = getBoundTexture(); - if(active->palette) { - free(active->palette->data); - active->palette->data = NULL; + TexturePalette* palette = NULL; + + if(target == GL_SHARED_TEXTURE_PALETTE_EXT) { + palette = SHARED_PALETTE; } else { - active->palette = (TexturePalette*) malloc(sizeof(TexturePalette)); + TextureObject* active = getBoundTexture(); + if(!active->palette) { + active->palette = (TexturePalette*) malloc(sizeof(TexturePalette)); + } + + palette = active->palette; } - active->palette->data = (GLubyte*) malloc(width * 4); - active->palette->format = format; - active->palette->width = width; + if(target) { + free(palette->data); + palette->data = NULL; + } + + palette->data = (GLubyte*) malloc(width * 4); + palette->format = format; + palette->width = width; GLubyte* src = (GLubyte*) data; - GLubyte* dst = (GLubyte*) active->palette->data; + GLubyte* dst = (GLubyte*) palette->data; /* Transform and copy the source palette to the texture */ GLushort i = 0; @@ -970,9 +996,6 @@ GLAPI void APIENTRY glColorTableEXT(GLenum target, GLenum internalFormat, GLsize src += sourceStride; dst += 4; } - - /* Apply the palette immediately, we'll also do this when binding the texture */ - _glApplyColorTable(); } GLAPI void APIENTRY glColorSubTableEXT(GLenum target, GLsizei start, GLsizei count, GLenum format, GLenum type, const GLvoid *data) { diff --git a/include/glext.h b/include/glext.h index 739f333..af64ff2 100644 --- a/include/glext.h +++ b/include/glext.h @@ -153,6 +153,8 @@ GLAPI GLboolean APIENTRY glIsFramebufferEXT(GLuint framebuffer); #define GL_TEXTURE_INDEX_SIZE_EXT 0x80ED +#define GL_SHARED_TEXTURE_PALETTE_EXT 0x81FB + GLAPI void APIENTRY glColorTableEXT(GLenum target, GLenum internalFormat, GLsizei width, GLenum format, GLenum type, const GLvoid *data); GLAPI void APIENTRY glColorSubTableEXT(GLenum target, GLsizei start, GLsizei count, GLenum format, GLenum type, const GLvoid *data); GLAPI void APIENTRY glGetColorTableEXT(GLenum target, GLenum format, GLenum type, GLvoid *data);