GL1 multitexture, refactored memory allocation

It was missing error checking and cleanup at shutdown.
Also, reset state when needed. Fixes "death by laser" in boss1 map.
This commit is contained in:
Jaime Moreira 2024-04-24 19:43:24 -04:00
parent ba71af2af8
commit b81e910929
2 changed files with 45 additions and 21 deletions

View file

@ -31,13 +31,48 @@ extern gllightmapstate_t gl_lms;
void R_SetCacheState(msurface_t *surf);
void R_BuildLightMap(msurface_t *surf, byte *dest, int stride);
void
LM_FreeLightmapBuffers(void)
{
for (int i=0; i<MAX_LIGHTMAPS; i++)
{
if (gl_lms.lightmap_buffer[i])
{
free(gl_lms.lightmap_buffer[i]);
}
gl_lms.lightmap_buffer[i] = NULL;
}
}
static void
LM_AllocLightmapBuffer(int buffer, qboolean clean)
{
static const unsigned int lightmap_size =
BLOCK_WIDTH * BLOCK_HEIGHT * LIGHTMAP_BYTES;
if (!gl_lms.lightmap_buffer[buffer])
{
gl_lms.lightmap_buffer[buffer] = malloc (lightmap_size);
}
if (!gl_lms.lightmap_buffer[buffer])
{
ri.Sys_Error(ERR_FATAL, "Could not allocate lightmap buffer %d\n",
buffer);
}
if (clean)
{
memset (gl_lms.lightmap_buffer[buffer], 0, lightmap_size);
}
}
void
LM_InitBlock(void)
{
memset(gl_lms.allocated, 0, sizeof(gl_lms.allocated));
if (gl_config.multitexture && !gl_lms.lightmap_buffer[gl_lms.current_lightmap_texture]) {
gl_lms.lightmap_buffer[gl_lms.current_lightmap_texture] = malloc (BLOCK_WIDTH*BLOCK_HEIGHT*LIGHTMAP_BYTES);
if (gl_config.multitexture)
{
LM_AllocLightmapBuffer(gl_lms.current_lightmap_texture, false);
}
}
@ -239,21 +274,11 @@ LM_CreateSurfaceLightmap(msurface_t *surf)
void
LM_BeginBuildingLightmaps(model_t *m)
{
static const unsigned int lightmap_size = BLOCK_WIDTH*BLOCK_HEIGHT*LIGHTMAP_BYTES;
static lightstyle_t lightstyles[MAX_LIGHTSTYLES];
int i;
memset(gl_lms.allocated, 0, sizeof(gl_lms.allocated));
// free lightmap update buffers
for (i=0; i<MAX_LIGHTMAPS; i++)
{
if (gl_lms.lightmap_buffer[i])
{
free(gl_lms.lightmap_buffer[i]);
}
gl_lms.lightmap_buffer[i] = NULL;
}
LM_FreeLightmapBuffers();
r_framecount = 1; /* no dlightcache */
@ -280,18 +305,12 @@ LM_BeginBuildingLightmaps(model_t *m)
if (gl_config.multitexture)
{
// alloc lightmap update buffer if needed
if (!gl_lms.lightmap_buffer[gl_lms.current_lightmap_texture]) {
gl_lms.lightmap_buffer[gl_lms.current_lightmap_texture] = malloc (lightmap_size);
}
LM_AllocLightmapBuffer(gl_lms.current_lightmap_texture, false);
return;
}
// dynamic lightmap for classic rendering path (no multitexture)
if (!gl_lms.lightmap_buffer[0]) {
gl_lms.lightmap_buffer[0] = malloc (lightmap_size);
memset (gl_lms.lightmap_buffer[0], 0, lightmap_size);
}
LM_AllocLightmapBuffer(0, true);
/* initialize the dynamic lightmap texture */
R_Bind(gl_state.lightmap_textures + 0);

View file

@ -144,6 +144,8 @@ cvar_t *gl1_stereo_convergence;
refimport_t ri;
void LM_FreeLightmapBuffers(void);
void
R_RotateForEntity(entity_t *e)
{
@ -261,6 +263,7 @@ R_DrawNullModel(entity_t *currententity)
R_LightPoint(currententity, currententity->origin, shadelight);
}
R_EnableMultitexture(false);
glPushMatrix();
R_RotateForEntity(currententity);
@ -1654,6 +1657,7 @@ RI_Shutdown(void)
ri.Cmd_RemoveCommand("imagelist");
ri.Cmd_RemoveCommand("gl_strings");
LM_FreeLightmapBuffers();
Mod_FreeAll();
R_ShutdownImages();
@ -1909,6 +1913,7 @@ R_DrawBeam(entity_t *e)
VectorAdd(start_points[i], direction, end_points[i]);
}
R_EnableMultitexture(false);
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);