From 2080c337d77277b68668dbe3bd2cdc96be1428e5 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 11 May 2012 21:53:16 +0900 Subject: [PATCH] Avoid using malloc/free every model/frame. --- include/r_internal.h | 2 +- libs/video/renderer/glsl/glsl_iqm.c | 8 +++----- libs/video/renderer/r_light.c | 11 +++++------ 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/include/r_internal.h b/include/r_internal.h index d19512bd1..3d5e69206 100644 --- a/include/r_internal.h +++ b/include/r_internal.h @@ -78,7 +78,7 @@ void R_EnqueueEntity (struct entity_s *ent); entity_t *R_AllocEntity (void); void R_FreeAllEntities (void); -dlight_t **R_FindNearLights (const vec3_t pos, int count); +void R_FindNearLights (const vec3_t pos, int count, dlight_t **lights); dlight_t *R_AllocDlight (int key); void R_DecayLights (double frametime); void R_ClearDlights (void); diff --git a/libs/video/renderer/glsl/glsl_iqm.c b/libs/video/renderer/glsl/glsl_iqm.c index 79d7d5177..db6de02df 100644 --- a/libs/video/renderer/glsl/glsl_iqm.c +++ b/libs/video/renderer/glsl/glsl_iqm.c @@ -195,14 +195,14 @@ glsl_R_DrawIQM (void) entity_t *ent = currententity; model_t *model = ent->model; iqm_t *iqm = (iqm_t *) model->aliashdr; - dlight_t **lights; + dlight_t *lights[MAX_IQM_LIGHTS]; int i; vec_t norm_mat[9]; mat4_t mvp_mat; float blend; iqmframe_t *frame; - lights = R_FindNearLights (ent->origin, MAX_IQM_LIGHTS); + R_FindNearLights (ent->origin, MAX_IQM_LIGHTS, lights); // we need only the rotation for normals. VectorCopy (ent->transform + 0, norm_mat + 0); @@ -212,7 +212,7 @@ glsl_R_DrawIQM (void) blend = R_IQMGetLerpedFrames (ent, iqm); - frame = malloc (iqm->num_joints * sizeof (iqmframe_t)); + frame = Hunk_TempAlloc (iqm->num_joints * sizeof (iqmframe_t)); for (i = 0; i < iqm->num_joints; i++) { iqmframe_t *f1 = &iqm->frames[ent->pose1][i]; iqmframe_t *f2 = &iqm->frames[ent->pose2][i]; @@ -249,8 +249,6 @@ glsl_R_DrawIQM (void) GL_UNSIGNED_SHORT, iqm->elements + 3 * iqm->meshes[i].first_triangle); } - free (frame); - free (lights); } // All iqm models are drawn in a batch, so avoid thrashing the gl state diff --git a/libs/video/renderer/r_light.c b/libs/video/renderer/r_light.c index fd48ac558..2d51b03a9 100644 --- a/libs/video/renderer/r_light.c +++ b/libs/video/renderer/r_light.c @@ -49,11 +49,10 @@ vec3_t ambientcolor; unsigned int r_maxdlights; -dlight_t ** -R_FindNearLights (const vec3_t pos, int count) +void +R_FindNearLights (const vec3_t pos, int count, dlight_t **lights) { - dlight_t **lights = calloc (count, sizeof (dlight_t *)); - float *scores = calloc (count, sizeof (float)); + float *scores = alloca (count * sizeof (float)); float score; dlight_t *dl; unsigned i; @@ -100,8 +99,8 @@ R_FindNearLights (const vec3_t pos, int count) } } } - free (scores); - return lights; + for (j = num; j < count; j++) + lights[j] = 0; } void