ref: ad3df4d5903c43c9ae12e2d3fb7176f074d84eec
parent: c8272c514c3737b079cffc44a6e0c8eea8f5e7d8
author: David <gek@katherine>
date: Sun Mar 7 16:47:45 EST 2021
Improved performance by reducing runtime overhead of textures
--- a/SDL_Examples/Makefile
+++ b/SDL_Examples/Makefile
@@ -12,7 +12,7 @@
#SDL_LIBS= -lmingw32 -lSDLmain -lSDL
-#SDL_MIXERLIBS= -lSDL_mixer -lmad -logg -lmikmod -logg
+#SDL_MIXERLIBS= -lSDL_mixer -lmad -logg -lmikmod -logg -DPLAY_MUSIC
SDL_MIXERLIBS=
all: $(ALL_T)
clean:
--- a/include/zfeatures.h
+++ b/include/zfeatures.h
@@ -7,6 +7,7 @@
//Disabling this has slight performance gains.
#define TGL_FEATURE_ERROR_CHECK 0
//Strict out-of-memory checking. All OpenGL function calls are invalidated (ALL OF THEM) if a GL_OUT_OF_MEMORY error occurs.
+//This means that TinyGL has to constantly check all gl_malloc() attempts for errors and the state of the error state variable.
//The checks slow down the renderer so it is not recommended , but
//it's in the GL spec that this should occur.
#define TGL_FEATURE_STRICT_OOM_CHECKS 0
@@ -15,7 +16,8 @@
//a replacement gl_malloc(), gl_zalloc(), and gl_free() in memory.c
#define TGL_FEATURE_CUSTOM_MALLOC 0
-//Use Fast Inverse Square Root. Toggleable because it's actually slower on some systems, i've heard.
+//Use Fast Inverse Square Root. Toggleable because it's actually slower,
+//And because some systems may have float types which are incompatible with it.
#define TGL_FEATURE_FISR 1
//Clientside Arrays
#define TGL_FEATURE_ARRAYS 1
--- a/src/init.c
+++ b/src/init.c
@@ -40,12 +40,12 @@
t = s->texture_hash_table[i];
while (t) {
GLTexture** ht;
- GLImage* im;
- GLint inner_i;
+ //GLImage* im;
+ //GLint inner_i;
// t = find_texture(c, h);
if (t->prev == NULL) {
- ht = &c->shared_state.texture_hash_table[t->handle % TEXTURE_HASH_TABLE_SIZE];
+ ht = &c->shared_state.texture_hash_table[t->handle & TEXTURE_HASH_TABLE_MASK];
*ht = t->next;
} else {
t->prev->next = t->next;
@@ -54,11 +54,11 @@
if (t->next != NULL)
t->next->prev = t->prev;
- for (inner_i = 0; inner_i < MAX_TEXTURE_LEVELS; inner_i++) {
- im = &t->images[inner_i];
- if (im->pixmap != NULL)
- gl_free(im->pixmap);
- }
+ //for (inner_i = 0; inner_i < MAX_TEXTURE_LEVELS; inner_i++) {
+ // im = &t->images[inner_i];
+ // if (im->pixmap != NULL)
+ // gl_free(im->pixmap);
+ //}
gl_free(t);
t = n;
}
--- a/src/texture.c
+++ b/src/texture.c
@@ -6,7 +6,7 @@
static GLTexture* find_texture(GLContext* c, GLint h) {
GLTexture* t;
- t = c->shared_state.texture_hash_table[h % TEXTURE_HASH_TABLE_SIZE];
+ t = c->shared_state.texture_hash_table[h & TEXTURE_HASH_TABLE_MASK];
while (t != NULL) {
if (t->handle == h)
return t;
@@ -65,12 +65,10 @@
static void free_texture(GLContext* c, GLint h) {
GLTexture *t, **ht;
- GLImage* im;
- GLint i;
t = find_texture(c, h);
if (t->prev == NULL) {
- ht = &c->shared_state.texture_hash_table[t->handle % TEXTURE_HASH_TABLE_SIZE];
+ ht = &c->shared_state.texture_hash_table[t->handle & TEXTURE_HASH_TABLE_MASK];
*ht = t->next;
} else {
t->prev->next = t->next;
@@ -78,11 +76,11 @@
if (t->next != NULL)
t->next->prev = t->prev;
- for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
- im = &t->images[i];
- if (im->pixmap != NULL)
- gl_free(im->pixmap);
- }
+// for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
+// im = &t->images[i];
+ //if (im->pixmap != NULL)
+ // gl_free(im->pixmap);
+// }
gl_free(t);
}
@@ -101,7 +99,7 @@
gl_fatal_error("GL_OUT_OF_MEMORY");
#endif
- ht = &c->shared_state.texture_hash_table[h % TEXTURE_HASH_TABLE_SIZE];
+ ht = &c->shared_state.texture_hash_table[h & TEXTURE_HASH_TABLE_MASK];
t->next = *ht;
t->prev = NULL;
@@ -146,6 +144,7 @@
GLTexture* t;
#include "error_check.h"
for (i = 0; i < n; i++) {
+
t = find_texture(c, textures[i]);
if (t != NULL && t != 0) {
if (t == c->current_texture) {
@@ -241,15 +240,10 @@
return;
#endif
}
- PIXEL* data = gl_malloc(TGL_FEATURE_TEXTURE_DIM * TGL_FEATURE_TEXTURE_DIM * sizeof(PIXEL)); //GUARDED
- if(!data){
-#if TGL_FEATURE_ERROR_CHECK == 1
-#define ERROR_FLAG GL_OUT_OF_MEMORY
-#include "error_check.h"
-#else
- gl_fatal_error("GL_OUT_OF_MEMORY");
-#endif
- }
+ GLImage* im = &c->current_texture->images[level];
+ PIXEL* data = c->current_texture->images[level].pixmap;
+ im->xsize = TGL_FEATURE_TEXTURE_DIM;
+ im->ysize = TGL_FEATURE_TEXTURE_DIM;
//sample the buffer.
//TODO implement the scaling and stuff that the GL spec says it should have.
for(GLint j = 0; j < h; j++)
@@ -265,13 +259,6 @@
data[i+j*w] = c->zb->pbuf[ ((i+x)%(c->zb->xsize))
+ ((j+y)%(c->zb->ysize))*(c->zb->xsize)];
}
- //TODO: Load this into a texture.
- GLImage* im = &c->current_texture->images[level];
- im->xsize = TGL_FEATURE_TEXTURE_DIM;
- im->ysize = TGL_FEATURE_TEXTURE_DIM;
- if (im->pixmap != NULL) gl_free(im->pixmap);
- im->pixmap = data;
- //if(data)gl_free(data);
}
void glopTexImage1D(GLContext* c, GLParam* p){
@@ -324,42 +311,16 @@
im = &c->current_texture->images[level];
im->xsize = width;
im->ysize = height;
- if (im->pixmap != NULL) gl_free(im->pixmap);
+ //if (im->pixmap != NULL) gl_free(im->pixmap);
#if TGL_FEATURE_RENDER_BITS == 32
- im->pixmap = gl_malloc(width * height * 4); //GUARDED
- if (im->pixmap) {
- gl_convertRGB_to_8A8R8G8B(im->pixmap, pixels1, width, height);
- }else { //failed malloc of im->pixmap, glopteximage1d
-#if TGL_FEATURE_ERROR_CHECK == 1
-#define ERROR_FLAG GL_OUT_OF_MEMORY
-#include "error_check.h"
-#else
- gl_fatal_error("GL_OUT_OF_MEMORY");
-#endif
- }
+ gl_convertRGB_to_8A8R8G8B(im->pixmap, pixels1, width, height);
#elif TGL_FEATURE_RENDER_BITS == 16
- im->pixmap = gl_malloc(width * height * 2);//GUARDED
- if (im->pixmap) {
- gl_convertRGB_to_5R6G5B(im->pixmap, pixels1, width, height);
- }else {
-#if TGL_FEATURE_ERROR_CHECK == 1
-#define ERROR_FLAG GL_OUT_OF_MEMORY
-#include "error_check.h"
+ gl_convertRGB_to_5R6G5B(im->pixmap, pixels1, width, height);
#else
- gl_fatal_error("GL_OUT_OF_MEMORY");
+#error bad TGL_FEATURE_RENDER_BITS
#endif
- }
-
-#else
-#error TODO
-#endif
if (do_free)
gl_free(pixels1);
-
-
-
-
-
}
void glopTexImage2D(GLContext* c, GLParam* p) {
GLint target = p[1].i;
@@ -411,34 +372,12 @@
im = &c->current_texture->images[level];
im->xsize = width;
im->ysize = height;
- if (im->pixmap != NULL) gl_free(im->pixmap);
#if TGL_FEATURE_RENDER_BITS == 32
- im->pixmap = gl_malloc(width * height * 4);//GUARDED
- if (im->pixmap) {
gl_convertRGB_to_8A8R8G8B(im->pixmap, pixels1, width, height);
- }else {
-#if TGL_FEATURE_ERROR_CHECK == 1
-#define ERROR_FLAG GL_OUT_OF_MEMORY
-#include "error_check.h"
-#else
- gl_fatal_error("GL_OUT_OF_MEMORY");
-#endif
- }
#elif TGL_FEATURE_RENDER_BITS == 16
- im->pixmap = gl_malloc(width * height * 2);//GUARDED
- if (im->pixmap) {
gl_convertRGB_to_5R6G5B(im->pixmap, pixels1, width, height);
- }else {
-#if TGL_FEATURE_ERROR_CHECK == 1
-#define ERROR_FLAG GL_OUT_OF_MEMORY
-#include "error_check.h"
#else
- gl_fatal_error("GL_OUT_OF_MEMORY");
-#endif
- }
-
-#else
-#error TODO
+#error Bad TGL_FEATURE_RENDER_BITS
#endif
if (do_free)
gl_free(pixels1);
--- a/src/zgl.h
+++ b/src/zgl.h
@@ -140,7 +140,7 @@
} GLVertex;
typedef struct GLImage {
- void* pixmap;
+ PIXEL pixmap[TGL_FEATURE_TEXTURE_DIM * TGL_FEATURE_TEXTURE_DIM];
GLint xsize, ysize;
} GLImage;
@@ -147,6 +147,7 @@
/* textures */
#define TEXTURE_HASH_TABLE_SIZE 256
+#define TEXTURE_HASH_TABLE_MASK 255
typedef struct GLTexture {
GLImage images[MAX_TEXTURE_LEVELS];
GLint handle;