From fff5887126d552f91a424edda773ff43d9fd5504 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 27 Jan 2024 23:01:21 +1100 Subject: [PATCH] Dreamcast: Try to simplify some things, doesn't fix issues though --- misc/dreamcast/Makefile | 2 +- third_party/gldc/include/gldc.h | 24 +------- third_party/gldc/src/flush.c | 34 ++++------- third_party/gldc/src/named_array.c | 90 ------------------------------ third_party/gldc/src/named_array.h | 38 ------------- third_party/gldc/src/private.h | 1 - third_party/gldc/src/texture.c | 81 +++++++++++++++++---------- 7 files changed, 65 insertions(+), 205 deletions(-) delete mode 100644 third_party/gldc/src/named_array.c delete mode 100644 third_party/gldc/src/named_array.h diff --git a/misc/dreamcast/Makefile b/misc/dreamcast/Makefile index 7d9f58f88..46446abdc 100644 --- a/misc/dreamcast/Makefile +++ b/misc/dreamcast/Makefile @@ -52,7 +52,7 @@ $(TARGET).iso: $(TARGET)-scr.bin mkdir -p ISO_FILES/maps mkdir -p ISO_FILES/texpacks mkdir -p ISO_FILES/texturecache - cp $(CC_TEXTURES) ISO_FILES/texpacks/classicube.zip + cp $(CC_TEXTURES) ISO_FILES/texpacks/default.zip cp misc/dreamcast/IP.BIN IP.BIN mkisofs -G IP.BIN -C 0,11702 -J -l -r -o $(TARGET).iso ISO_FILES # genisoimage -V ClassiCube -G IP.BIN -joliet -rock -l -o $(TARGET).iso ISO_FILES diff --git a/third_party/gldc/include/gldc.h b/third_party/gldc/include/gldc.h index a527fdfee..52cd6a7d5 100644 --- a/third_party/gldc/include/gldc.h +++ b/third_party/gldc/include/gldc.h @@ -215,6 +215,7 @@ GLAPI void glAlphaFunc(GLenum func, GLclampf ref); /* Initialize the GL pipeline. GL will initialize the PVR. */ GLAPI void glKosInit(); +GLAPI void glKosSwapBuffers(); typedef struct { /* If GL_TRUE, enables pvr autosorting, this *will* break glDepthFunc/glDepthTest */ @@ -222,30 +223,9 @@ typedef struct { /* If GL_TRUE, enables the PVR FSAA */ GLboolean fsaa_enabled; - - /* Initial capacity of each of the OP, TR and PT lists in vertices */ - GLuint initial_op_capacity; - GLuint initial_tr_capacity; - GLuint initial_pt_capacity; - } GLdcConfig; -GLAPI void glKosInitConfig(GLdcConfig* config); - -/* Usage: - * - * GLdcConfig config; - * glKosInitConfig(&config); - * - * config.autosort_enabled = GL_TRUE; - * - * glKosInitEx(&config); - */ -GLAPI void glKosInitEx(GLdcConfig* config); -GLAPI void glKosSwapBuffers(); -\ - /* Memory allocation extension (GL_KOS_texture_memory_management) */ GLAPI void glDefragmentTextureMemory_KOS(void); @@ -255,4 +235,4 @@ GLAPI void glDefragmentTextureMemory_KOS(void); __END_DECLS -#endif /* !__GL_GL_H */ +#endif /* !__GL_GL_H */ \ No newline at end of file diff --git a/third_party/gldc/src/flush.c b/third_party/gldc/src/flush.c index 8c96a7e31..4ac9e59f8 100644 --- a/third_party/gldc/src/flush.c +++ b/third_party/gldc/src/flush.c @@ -16,22 +16,15 @@ PolyList TR_LIST; GLboolean AUTOSORT_ENABLED = GL_FALSE; - -void APIENTRY glKosInitConfig(GLdcConfig* config) { - config->autosort_enabled = GL_FALSE; - config->fsaa_enabled = GL_FALSE; - - config->initial_op_capacity = 1024 * 3; - config->initial_pt_capacity = 512 * 3; - config->initial_tr_capacity = 1024 * 3; -} - -void APIENTRY glKosInitEx(GLdcConfig* config) { +void APIENTRY glKosInit() { TRACE(); + + GLdcConfig config; + config.autosort_enabled = GL_FALSE; + config.fsaa_enabled = GL_FALSE; - InitGPU(config->autosort_enabled, config->fsaa_enabled); - - AUTOSORT_ENABLED = config->autosort_enabled; + InitGPU(config.autosort_enabled, config.fsaa_enabled); + AUTOSORT_ENABLED = config.autosort_enabled; _glInitSubmissionTarget(); _glInitMatrices(); @@ -47,16 +40,11 @@ void APIENTRY glKosInitEx(GLdcConfig* config) { aligned_vector_init(&PT_LIST.vector, sizeof(Vertex)); aligned_vector_init(&TR_LIST.vector, sizeof(Vertex)); - aligned_vector_reserve(&OP_LIST.vector, config->initial_op_capacity); - aligned_vector_reserve(&PT_LIST.vector, config->initial_pt_capacity); - aligned_vector_reserve(&TR_LIST.vector, config->initial_tr_capacity); + aligned_vector_reserve(&OP_LIST.vector, 1024 * 3); + aligned_vector_reserve(&PT_LIST.vector, 512 * 3); + aligned_vector_reserve(&TR_LIST.vector, 1024 * 3); } -void APIENTRY glKosInit() { - GLdcConfig config; - glKosInitConfig(&config); - glKosInitEx(&config); -} void APIENTRY glKosSwapBuffers() { TRACE(); @@ -87,4 +75,4 @@ void APIENTRY glKosSwapBuffers() { aligned_vector_clear(&TR_LIST.vector); _glApplyScissor(true); -} +} \ No newline at end of file diff --git a/third_party/gldc/src/named_array.c b/third_party/gldc/src/named_array.c deleted file mode 100644 index 4f6fad34c..000000000 --- a/third_party/gldc/src/named_array.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#include -#include - -#ifndef __APPLE__ -#include -#else -/* Linux + Kos define this, OSX does not, so just use malloc there */ -#define memalign(x, size) malloc((size)) -#endif - -#include "named_array.h" - -void named_array_init(NamedArray* array, unsigned int element_size, unsigned int max_elements) { - array->element_size = element_size; - array->max_element_count = max_elements; - - array->marker_count = (unsigned char)((max_elements+8-1)/8); - -#ifdef _arch_dreamcast - // Use 32-bit aligned memory on the Dreamcast - array->elements = (unsigned char*) memalign(0x20, element_size * max_elements); - array->used_markers = (unsigned char*) memalign(0x20, array->marker_count); -#else - array->elements = (unsigned char*) malloc(element_size * max_elements); - array->used_markers = (unsigned char*) malloc(array->marker_count); -#endif - memset(array->used_markers, 0, sizeof(unsigned char) * array->marker_count); -} - -void* named_array_alloc(NamedArray* array, unsigned int* new_id) { - unsigned int i = 0, j = 0; - for(i = 0; i < array->marker_count; ++i) { - for(j = 0; j < 8; ++j) { - unsigned int id = (i * 8) + j; - if(!named_array_used(array, id)) { - array->used_markers[i] |= (unsigned char) 1 << j; - *new_id = id; - unsigned char* ptr = &array->elements[id * array->element_size]; - memset(ptr, 0, array->element_size); - return ptr; - } - } - } - - return NULL; -} - -void* named_array_reserve(NamedArray* array, unsigned int id) { - if(!named_array_used(array, id)) { - unsigned int j = (id % 8); - unsigned int i = id / 8; - - assert(!named_array_used(array, id)); - array->used_markers[i] |= (unsigned char) 1 << j; - assert(named_array_used(array, id)); - - unsigned char* ptr = &array->elements[id * array->element_size]; - memset(ptr, 0, array->element_size); - return ptr; - } - - return named_array_get(array, id); -} - -void named_array_release(NamedArray* array, unsigned int new_id) { - unsigned int i = new_id / 8; - unsigned int j = new_id % 8; - array->used_markers[i] &= (unsigned char) ~(1 << j); -} - -void* named_array_get(NamedArray* array, unsigned int id) { - if(!named_array_used(array, id)) { - return NULL; - } - - return &array->elements[id * array->element_size]; -} - -void named_array_cleanup(NamedArray* array) { - free(array->elements); - free(array->used_markers); - array->elements = NULL; - array->used_markers = NULL; - array->element_size = array->max_element_count = 0; - array->marker_count = 0; -} - diff --git a/third_party/gldc/src/named_array.h b/third_party/gldc/src/named_array.h deleted file mode 100644 index d6be27632..000000000 --- a/third_party/gldc/src/named_array.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#ifndef NAMED_ARRAY_H -#define NAMED_ARRAY_H - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - unsigned int element_size; - unsigned int max_element_count; - unsigned char* elements; - unsigned char* used_markers; - unsigned char marker_count; -} NamedArray; - -void named_array_init(NamedArray* array, unsigned int element_size, unsigned int max_elements); -static inline char named_array_used(NamedArray* array, unsigned int id) { - const unsigned int i = id / 8; - const unsigned int j = id % 8; - - unsigned char v = array->used_markers[i] & (unsigned char) (1 << j); - return !!(v); -} - -void* named_array_alloc(NamedArray* array, unsigned int* new_id); -void* named_array_reserve(NamedArray* array, unsigned int id); - -void named_array_release(NamedArray* array, unsigned int new_id); -void* named_array_get(NamedArray* array, unsigned int id); -void named_array_cleanup(NamedArray* array); - -#ifdef __cplusplus -} -#endif - -#endif // NAMED_ARRAY_H diff --git a/third_party/gldc/src/private.h b/third_party/gldc/src/private.h index a912f5742..18af99409 100644 --- a/third_party/gldc/src/private.h +++ b/third_party/gldc/src/private.h @@ -11,7 +11,6 @@ #include "../include/gldc.h" #include "aligned_vector.h" -#include "named_array.h" #define MAX_TEXTURE_COUNT 768 diff --git a/third_party/gldc/src/texture.c b/third_party/gldc/src/texture.c index 7f363ccfd..bc887d483 100644 --- a/third_party/gldc/src/texture.c +++ b/third_party/gldc/src/texture.c @@ -6,7 +6,6 @@ #include #include "platform.h" - #include "yalloc/yalloc.h" /* We always leave this amount of vram unallocated to prevent @@ -14,7 +13,42 @@ #define PVR_MEM_BUFFER_SIZE (64 * 1024) TextureObject* TEXTURE_ACTIVE = NULL; -static NamedArray TEXTURE_OBJECTS; +static TextureObject TEXTURE_LIST[MAX_TEXTURE_COUNT]; +static unsigned char TEXTURE_USED[MAX_TEXTURE_COUNT / 8]; + +static char texture_id_map_used(unsigned int id) { + unsigned int i = id / 8; + unsigned int j = id % 8; + + unsigned char v = TEXTURE_USED[i] & (unsigned char) (1 << j); + return !!(v); +} + +static void texture_id_map_reserve(unsigned int id) { + unsigned int j = (id % 8); + unsigned int i = id / 8; + + TEXTURE_USED[i] |= (unsigned char) 1 << j; +} + +static void texture_id_map_release(unsigned int id) { + unsigned int i = id / 8; + unsigned int j = id % 8; + TEXTURE_USED[i] &= (unsigned char) ~(1 << j); +} + +unsigned int texture_id_map_alloc(void) { + unsigned int id; + + for(id = 0; id < MAX_TEXTURE_COUNT; ++id) { + if(!texture_id_map_used(id)) { + texture_id_map_reserve(id); + return id; + } + } + return 0; +} + static void* YALLOC_BASE = NULL; static size_t YALLOC_SIZE = 0; @@ -48,13 +82,12 @@ static void _glInitializeTextureObject(TextureObject* txr, unsigned int id) { } GLubyte _glInitTextures() { - named_array_init(&TEXTURE_OBJECTS, sizeof(TextureObject), MAX_TEXTURE_COUNT); - + memset(TEXTURE_USED, 0, sizeof(TEXTURE_USED)); // Reserve zero so that it is never given to anyone as an ID! - named_array_reserve(&TEXTURE_OBJECTS, 0); + texture_id_map_reserve(0); // Initialize zero as an actual texture object though because apparently it is! - TextureObject* default_tex = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, 0); + TextureObject* default_tex = &TEXTURE_LIST[0]; _glInitializeTextureObject(default_tex, 0); TEXTURE_ACTIVE = default_tex; @@ -68,25 +101,19 @@ GLubyte _glInitTextures() { #endif yalloc_init(YALLOC_BASE, YALLOC_SIZE); - - gl_assert(TEXTURE_OBJECTS.element_size > 0); return 1; } GLuint APIENTRY gldcGenTexture(void) { TRACE(); - gl_assert(TEXTURE_OBJECTS.element_size > 0); - - GLuint id = 0; - TextureObject* txr = (TextureObject*) named_array_alloc(&TEXTURE_OBJECTS, &id); - gl_assert(txr); + GLuint id = texture_id_map_alloc(); gl_assert(id); // Generated IDs must never be zero - + + TextureObject* txr = &TEXTURE_LIST[id]; _glInitializeTextureObject(txr, id); gl_assert(txr->index == id); - gl_assert(TEXTURE_OBJECTS.element_size > 0); return id; } @@ -94,21 +121,18 @@ GLuint APIENTRY gldcGenTexture(void) { void APIENTRY gldcDeleteTexture(GLuint id) { TRACE(); - gl_assert(TEXTURE_OBJECTS.element_size > 0); - if(id == 0) { /* Zero is the "default texture" and we never allow deletion of it */ return; } - TextureObject* txr = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, id); - - if(txr) { + if(texture_id_map_used(id)) { + TextureObject* txr = &TEXTURE_LIST[id]; gl_assert(txr->index == id); if(txr == TEXTURE_ACTIVE) { // Reset to the default texture - TEXTURE_ACTIVE = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, 0); + TEXTURE_ACTIVE = &TEXTURE_LIST[0]; } if(txr->data) { @@ -116,22 +140,19 @@ void APIENTRY gldcDeleteTexture(GLuint id) { txr->data = NULL; } - named_array_release(&TEXTURE_OBJECTS, id); + texture_id_map_release(id); } - - gl_assert(TEXTURE_OBJECTS.element_size > 0); } void APIENTRY gldcBindTexture(GLuint id) { TRACE(); - TextureObject* txr = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, id); + gl_assert(texture_id_map_used(id)); + TextureObject* txr = &TEXTURE_LIST[id]; TEXTURE_ACTIVE = txr; gl_assert(TEXTURE_ACTIVE->index == id); - gl_assert(TEXTURE_OBJECTS.element_size > 0); - STATE_DIRTY = GL_TRUE; } @@ -266,12 +287,12 @@ GLAPI GLvoid APIENTRY glDefragmentTextureMemory_KOS(void) { /* Replace all texture pointers */ for(id = 0; id < MAX_TEXTURE_COUNT; id++){ - TextureObject* txr = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, id); - if(txr){ + if(texture_id_map_used(id)){ + TextureObject* txr = &TEXTURE_LIST[id]; gl_assert(txr->index == id); txr->data = yalloc_defrag_address(YALLOC_BASE, txr->data); } } yalloc_defrag_commit(YALLOC_BASE); -} +} \ No newline at end of file