Handle calling glBindTexture on an unknown name

This commit is contained in:
Luke Benstead 2019-02-27 20:45:53 +00:00
parent e34f7e6368
commit 111d4ca83e
3 changed files with 44 additions and 12 deletions

View File

@ -170,12 +170,7 @@ GLboolean APIENTRY glIsTexture(GLuint texture) {
return (named_array_used(&TEXTURE_OBJECTS, texture)) ? GL_TRUE : GL_FALSE;
}
void APIENTRY glGenTextures(GLsizei n, GLuint *textures) {
TRACE();
while(n--) {
GLuint id = 0;
TextureObject* txr = (TextureObject*) named_array_alloc(&TEXTURE_OBJECTS, &id);
static void _glInitializeTextureObject(TextureObject* txr, unsigned int id) {
txr->index = id;
txr->width = txr->height = 0;
txr->mipmap = 0;
@ -188,6 +183,15 @@ void APIENTRY glGenTextures(GLsizei n, GLuint *textures) {
txr->palette = NULL;
txr->isCompressed = GL_FALSE;
txr->isPaletted = GL_FALSE;
}
void APIENTRY glGenTextures(GLsizei n, GLuint *textures) {
TRACE();
while(n--) {
GLuint id = 0;
TextureObject* txr = (TextureObject*) named_array_alloc(&TEXTURE_OBJECTS, &id);
_glInitializeTextureObject(txr, id);
*textures = id;
@ -236,6 +240,13 @@ void APIENTRY glBindTexture(GLenum target, GLuint texture) {
return;
}
/* If this didn't come from glGenTextures, then we should initialize the
* texture the first time it's bound */
if(!named_array_used(&TEXTURE_OBJECTS, texture)) {
TextureObject* txr = named_array_reserve(&TEXTURE_OBJECTS, texture);
_glInitializeTextureObject(txr, texture);
}
if(texture) {
TEXTURE_UNITS[ACTIVE_TEXTURE] = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, texture);
} else {

View File

@ -1,6 +1,7 @@
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#ifndef __APPLE__
#include <malloc.h>
@ -57,6 +58,23 @@ void* named_array_alloc(NamedArray* array, unsigned int* new_id) {
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 - j) / 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 - 1) * 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) {
new_id--;

View File

@ -15,7 +15,10 @@ typedef struct {
void named_array_init(NamedArray* array, unsigned int element_size, unsigned int max_elements);
char named_array_used(NamedArray* array, unsigned int id);
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);