Skip to content

Commit

Permalink
ref: gl: try to match texture slot and texture object handle because …
Browse files Browse the repository at this point in the history
…of buggy games
  • Loading branch information
a1batross committed Jun 15, 2024
1 parent b41591a commit 260edc2
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions ref/gl/gl_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1392,40 +1392,54 @@ GL_AllocTexture
*/
static gl_texture_t *GL_AllocTexture( const char *name, texFlags_t flags )
{
gl_texture_t *tex;
uint i;

// find a free texture_t slot
for( i = 0, tex = gl_textures; i < gl_numTextures; i++, tex++ )
if( !tex->name[0] ) break;
const qboolean skyboxhack = FBitSet( flags, TF_SKYSIDE ) && glConfig.context != CONTEXT_TYPE_GL_CORE;
gl_texture_t *tex = NULL;
GLuint texnum = 1;

if( i == gl_numTextures )
if( !skyboxhack )
{
if( gl_numTextures == MAX_TEXTURES )
gEngfuncs.Host_Error( "GL_AllocTexture: MAX_TEXTURES limit exceeds\n" );
gl_numTextures++;
// keep generating new texture names to avoid collision with predefined skybox objects
do
{
pglGenTextures( 1, &texnum );
}
while( texnum >= SKYBOX_BASE_NUM && texnum <= SKYBOX_BASE_NUM + SKYBOX_MAX_SIDES );
}
else texnum = tr.skyboxbasenum;

tex = &gl_textures[i];

// copy initial params
Q_strncpy( tex->name, name, sizeof( tex->name ));

if( FBitSet( flags, TF_SKYSIDE ) && glConfig.context != CONTEXT_TYPE_GL_CORE )
tex->texnum = tr.skyboxbasenum++;
else
// try to match texture slot and texture handle because of buggy games
if( texnum >= MAX_TEXTURES || gl_textures[texnum].texnum != 0 )
{
// keep generating new texture names to avoid collision with predefined skybox objects
do
// find a free texture_t slot
uint i;

for( i = 0; i < MAX_TEXTURES; i++ )
{
pglGenTextures( 1, &tex->texnum );
if( gl_textures[i].texnum )
continue;

tex = &gl_textures[i];
break;
}
while( tex->texnum >= SKYBOX_BASE_NUM &&
tex->texnum <= SKYBOX_BASE_NUM + SKYBOX_MAX_SIDES );
}
else tex = &gl_textures[texnum];

if( tex == NULL )
{
gEngfuncs.Host_Error( "%s: MAX_TEXTURES limit exceeds\n", __func__ );
return NULL;
}

// copy initial params
Q_strncpy( tex->name, name, sizeof( tex->name ));
tex->texnum = texnum;
tex->flags = flags;

// increase counter
gl_numTextures = Q_max(( tex - gl_textures ) + 1, gl_numTextures );
if( skyboxhack )
tr.skyboxbasenum++;

// add to hash table
tex->hashValue = COM_HashKey( name, TEXTURES_HASH_SIZE );
tex->nextHash = gl_texturesHashTable[tex->hashValue];
Expand Down

0 comments on commit 260edc2

Please sign in to comment.