From f9b7cd7985e843f73215f99713d0c3bcdbd28526 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Fri, 19 Feb 2021 21:28:31 +0000 Subject: [PATCH] Add a custom extension to query and defragment texture memory --- GL/private.h | 4 ++++ GL/state.c | 11 ++++++++++- GL/texture.c | 32 ++++++++++++++++++++++++++++++-- Makefile | 1 + include/glkos.h | 7 +++++++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/GL/private.h b/GL/private.h index dad060b..cf6f789 100644 --- a/GL/private.h +++ b/GL/private.h @@ -374,6 +374,10 @@ void _glKosThrowError(GLenum error, const char *function); void _glKosPrintError(); GLubyte _glKosHasError(); +GLuint _glFreeTextureMemory(); +GLuint _glUsedTextureMemory(); +GLuint _glFreeContiguousTextureMemory(); + #define PVR_VERTEX_BUF_SIZE 2560 * 256 #define MAX_TEXTURE_UNITS 2 #define MAX_LIGHTS 8 diff --git a/GL/state.c b/GL/state.c index 8ac4719..4d17060 100644 --- a/GL/state.c +++ b/GL/state.c @@ -696,6 +696,15 @@ void APIENTRY glGetIntegerv(GLenum pname, GLint *params) { params[i] = COMPRESSED_FORMATS[i]; } } break; + case GL_FREE_TEXTURE_MEMORY_KOS: + *params = _glFreeTextureMemory(); + break; + case GL_USED_TEXTURE_MEMORY_KOS: + *params = _glUsedTextureMemory(); + break; + case GL_FREE_CONTIGUOUS_TEXTURE_MEMORY_KOS: + *params = _glFreeContiguousTextureMemory(); + break; default: _glKosThrowError(GL_INVALID_ENUM, __func__); _glKosPrintError(); @@ -715,7 +724,7 @@ const GLubyte *glGetString(GLenum name) { return (const GLubyte*) "1.2 (partial) - GLdc 1.1"; case GL_EXTENSIONS: - return (const GLubyte*) "GL_ARB_framebuffer_object, GL_ARB_multitexture, GL_ARB_texture_rg, GL_EXT_paletted_texture, GL_EXT_shared_texture_palette, GL_KOS_multiple_shared_palette, GL_ARB_vertex_array_bgra, GL_ARB_vertex_type_2_10_10_10_rev"; + return (const GLubyte*) "GL_ARB_framebuffer_object, GL_ARB_multitexture, GL_ARB_texture_rg, GL_EXT_paletted_texture, GL_EXT_shared_texture_palette, GL_KOS_multiple_shared_palette, GL_ARB_vertex_array_bgra, GL_ARB_vertex_type_2_10_10_10_rev, GL_KOS_texture_memory_management"; } return (const GLubyte*) "GL_KOS_ERROR: ENUM Unsupported\n"; diff --git a/GL/texture.c b/GL/texture.c index 30b3d54..cd8c811 100644 --- a/GL/texture.c +++ b/GL/texture.c @@ -1065,7 +1065,7 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat, active->height != height || active->color != pvr_format) { /* changed - free old texture memory */ - pvr_mem_free(active->data); + yalloc_free(YALLOC_BASE, active->data); active->data = NULL; active->mipmap = 0; active->mipmapCount = 0; @@ -1104,7 +1104,7 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat, /* If we're uploading a mipmap level, we need to allocate the full amount of space */ _glAllocateSpaceForMipmaps(active); } else { - active->data = pvr_mem_malloc(active->baseDataSize); + active->data = yalloc_alloc(YALLOC_BASE, active->baseDataSize); } assert(active->data); @@ -1544,3 +1544,31 @@ GLAPI void APIENTRY glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height _GL_UNUSED(pixels); assert(0 && "Not Implemented"); } + +GLuint _glFreeTextureMemory() { + return yalloc_count_free(YALLOC_BASE); +} + +GLuint _glUsedTextureMemory() { + return YALLOC_SIZE - _glFreeTextureMemory(); +} + +GLuint _glFreeContiguousTextureMemory() { + return yalloc_count_continuous(YALLOC_BASE); +} + +GLAPI GLvoid APIENTRY glDefragmentTextureMemory_KOS(void) { + yalloc_defrag_start(YALLOC_BASE); + + GLuint id; + + /* Replace all texture pointers */ + for(id = 0; id < MAX_TEXTURE_COUNT; id++){ + if(glIsTexture(id)){ + TextureObject* txr = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, id); + txr->data = yalloc_defrag_address(YALLOC_BASE, txr->data); + } + } + + yalloc_defrag_commit(YALLOC_BASE); +} diff --git a/Makefile b/Makefile index 143c240..0fa72f6 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ TARGET = libGLdc.a OBJS = GL/draw.o GL/flush.o GL/framebuffer.o GL/immediate.o GL/lighting.o GL/state.o GL/texture.o GL/glu.o GL/version.h OBJS += GL/matrix.o GL/fog.o GL/error.o GL/clip.o containers/stack.o containers/named_array.o containers/aligned_vector.o GL/profiler.o +OBJS += GL/yalloc/yalloc.o SUBDIRS = diff --git a/include/glkos.h b/include/glkos.h index 4f2aaf8..dd290bb 100644 --- a/include/glkos.h +++ b/include/glkos.h @@ -111,5 +111,12 @@ GLAPI void APIENTRY glKosSwapBuffers(); /* Pass to glTexParameteri to set the shared bank */ #define GL_SHARED_TEXTURE_BANK_KOS 0xEF00 +/* Memory allocation extension (GL_KOS_texture_memory_management) */ +GLAPI GLvoid APIENTRY glDefragmentTextureMemory_KOS(void); + +#define GL_FREE_TEXTURE_MEMORY_KOS 0xEF01 +#define GL_USED_TEXTURE_MEMORY_KOS 0xEF02 +#define GL_FREE_CONTIGUOUS_TEXTURE_MEMORY_KOS 0xEF03 + __END_DECLS