dynamically allocate dynamic lights

master
an 2019-11-25 22:00:40 -05:00
parent ef6a1262c5
commit a1bd4a0862
6 changed files with 61 additions and 50 deletions

View File

@ -48,15 +48,19 @@ cvar_t m_side = {"m_side", "0.8", CVAR_ARCHIVE};
cvar_t cl_maxpitch = {"cl_maxpitch", "90", CVAR_ARCHIVE}; //johnfitz -- variable pitch clamping cvar_t cl_maxpitch = {"cl_maxpitch", "90", CVAR_ARCHIVE}; //johnfitz -- variable pitch clamping
cvar_t cl_minpitch = {"cl_minpitch", "-90", CVAR_ARCHIVE}; //johnfitz -- variable pitch clamping cvar_t cl_minpitch = {"cl_minpitch", "-90", CVAR_ARCHIVE}; //johnfitz -- variable pitch clamping
cvar_t cl_maxdlights = {"cl_maxdlights", "128", CVAR_ARCHIVE};
client_static_t cls; client_static_t cls;
client_state_t cl; client_state_t cl;
// FIXME: put these on hunk? // FIXME: put these on hunk?
entity_t cl_static_entities[MAX_STATIC_ENTITIES]; entity_t cl_static_entities[MAX_STATIC_ENTITIES];
lightstyle_t cl_lightstyle[MAX_LIGHTSTYLES]; lightstyle_t cl_lightstyle[MAX_LIGHTSTYLES];
dlight_t cl_dlights[MAX_DLIGHTS];
entity_t *cl_entities; //johnfitz -- was a static array, now on hunk entity_t *cl_entities; //johnfitz -- was a static array, now on hunk
int32_t cl_max_edicts; //johnfitz -- only changes when new map loads int32_t cl_max_edicts; //johnfitz -- only changes when new map loads
dlight_t *cl_dlights;
int32_t cl_max_dlights; //johnfitz -- only changes when new map loads
int32_t cl_numvisedicts; int32_t cl_numvisedicts;
entity_t *cl_visedicts[MAX_VISEDICTS]; entity_t *cl_visedicts[MAX_VISEDICTS];
@ -74,21 +78,23 @@ void CL_ClearState(void)
if(!sv.active) if(!sv.active)
Host_ClearMemory(); Host_ClearMemory();
// wipe the entire cl structure // wipe the entire cl structure
memset(&cl, 0, sizeof(cl)); memset(&cl, 0, sizeof(cl));
SZ_Clear(&cls.message); SZ_Clear(&cls.message);
// clear other arrays // clear other arrays
memset(cl_dlights, 0, sizeof(cl_dlights));
memset(cl_lightstyle, 0, sizeof(cl_lightstyle)); memset(cl_lightstyle, 0, sizeof(cl_lightstyle));
memset(cl_temp_entities, 0, sizeof(cl_temp_entities)); memset(cl_temp_entities, 0, sizeof(cl_temp_entities));
memset(cl_beams, 0, sizeof(cl_beams)); memset(cl_beams, 0, sizeof(cl_beams));
//johnfitz -- cl_entities is now dynamically allocated //johnfitz -- cl_entities is now dynamically allocated
cl_max_edicts = CLAMP(MIN_EDICTS, (int32_t)max_edicts.value, MAX_EDICTS); cl_max_edicts = CLAMP(MIN_EDICTS, (int32_t)max_edicts.value, MAX_EDICTS);
cl_entities = (entity_t *) Hunk_AllocName(cl_max_edicts * sizeof(entity_t), "cl_entities"); cl_entities = Hunk_AllocName(cl_max_edicts * sizeof(entity_t), "cl_entities");
//johnfitz //johnfitz
cl_max_dlights = CLAMP(MIN_DLIGHTS, (int32_t)cl_maxdlights.value, MAX_DLIGHTS);
cl_dlights = Hunk_AllocName(cl_max_dlights * sizeof(*cl_dlights), "cl_dlights");
} }
/* /*
@ -288,7 +294,7 @@ dlight_t *CL_AllocDlight(int32_t key)
if(key) if(key)
{ {
dl = cl_dlights; dl = cl_dlights;
for(i = 0 ; i < MAX_DLIGHTS ; i++, dl++) for(i = 0 ; i < cl_max_dlights ; i++, dl++)
{ {
if(dl->key == key) if(dl->key == key)
{ {
@ -302,7 +308,7 @@ dlight_t *CL_AllocDlight(int32_t key)
// then look for anything else // then look for anything else
dl = cl_dlights; dl = cl_dlights;
for(i = 0 ; i < MAX_DLIGHTS ; i++, dl++) for(i = 0 ; i < cl_max_dlights ; i++, dl++)
{ {
if(dl->die < cl.time) if(dl->die < cl.time)
{ {
@ -336,7 +342,7 @@ void CL_DecayLights(void)
time = cl.time - cl.oldtime; time = cl.time - cl.oldtime;
dl = cl_dlights; dl = cl_dlights;
for(i = 0 ; i < MAX_DLIGHTS ; i++, dl++) for(i = 0 ; i < cl_max_dlights ; i++, dl++)
{ {
if(dl->die < cl.time || !dl->radius) if(dl->die < cl.time || !dl->radius)
continue; continue;
@ -650,11 +656,11 @@ int32_t CL_ReadFromServer(void)
dev_peakstats.beams = q_max(num_beams, dev_peakstats.beams); dev_peakstats.beams = q_max(num_beams, dev_peakstats.beams);
//dlights //dlights
for(i = 0, l = cl_dlights ; i < MAX_DLIGHTS ; i++, l++) for(i = 0, l = cl_dlights ; i < cl_max_dlights ; i++, l++)
if(l->die >= cl.time && l->radius) if(l->die >= cl.time && l->radius)
num_dlights++; num_dlights++;
if(num_dlights > 32 && dev_peakstats.dlights <= 32) if(num_dlights > 32 && dev_peakstats.dlights <= 32)
Con_DWarning("%" PRIi32 " dlights exceeded standard limit of 32 (max = %" PRIi32 ").\n", num_dlights, MAX_DLIGHTS); Con_DWarning("%" PRIi32 " dlights exceeded standard limit of 32 (max = %" PRIi32 ").\n", num_dlights, cl_max_dlights);
dev_stats.dlights = num_dlights; dev_stats.dlights = num_dlights;
dev_peakstats.dlights = q_max(num_dlights, dev_peakstats.dlights); dev_peakstats.dlights = q_max(num_dlights, dev_peakstats.dlights);
@ -807,6 +813,8 @@ void CL_Init(void)
Cvar_RegisterVariable(&cl_maxpitch); //johnfitz -- variable pitch clamping Cvar_RegisterVariable(&cl_maxpitch); //johnfitz -- variable pitch clamping
Cvar_RegisterVariable(&cl_minpitch); //johnfitz -- variable pitch clamping Cvar_RegisterVariable(&cl_minpitch); //johnfitz -- variable pitch clamping
Cvar_RegisterVariable(&cl_maxdlights);
Cmd_AddCommand("entities", CL_PrintEntities_f); Cmd_AddCommand("entities", CL_PrintEntities_f);
Cmd_AddCommand("disconnect", CL_Disconnect_f); Cmd_AddCommand("disconnect", CL_Disconnect_f);
Cmd_AddCommand("record", CL_Record_f); Cmd_AddCommand("record", CL_Record_f);

View File

@ -61,22 +61,23 @@ typedef struct
// client_state_t should hold all pieces of the client state // client_state_t should hold all pieces of the client state
// //
#define SIGNONS 4 // signon messages to receive before connected #define SIGNONS 4 // signon messages to receive before connected
#define MAX_DLIGHTS 64 //johnfitz -- was 32 #define MIN_DLIGHTS 32
#define MAX_DLIGHTS 4096
typedef struct typedef struct
{ {
vec3_t origin; vec3_t origin;
float radius; float radius;
float die; // stop lighting after this time float die; // stop lighting after this time
float decay; // drop this each second float decay; // drop this each second
float minlight; // don't add when contributing less float minlight; // don't add when contributing less
int32_t key; int32_t key;
vec3_t color; //johnfitz -- lit support via lordhavoc vec3_t color; // johnfitz -- lit support via lordhavoc
} dlight_t; } dlight_t;
#define MAX_BEAMS 32 //johnfitz -- was 24 #define MAX_BEAMS 32 // johnfitz -- was 24
typedef struct typedef struct
{ {
int32_t entity; int32_t entity;
@ -273,16 +274,18 @@ extern cvar_t m_side;
extern client_state_t cl; extern client_state_t cl;
// FIXME, allocate dynamically // FIXME, allocate dynamically
extern entity_t cl_static_entities[MAX_STATIC_ENTITIES]; extern entity_t cl_static_entities[MAX_STATIC_ENTITIES];
extern lightstyle_t cl_lightstyle[MAX_LIGHTSTYLES]; extern lightstyle_t cl_lightstyle[MAX_LIGHTSTYLES];
extern dlight_t cl_dlights[MAX_DLIGHTS]; extern entity_t cl_temp_entities[MAX_TEMP_ENTITIES];
extern entity_t cl_temp_entities[MAX_TEMP_ENTITIES]; extern beam_t cl_beams[MAX_BEAMS];
extern beam_t cl_beams[MAX_BEAMS]; extern entity_t *cl_visedicts[MAX_VISEDICTS];
extern entity_t *cl_visedicts[MAX_VISEDICTS]; extern int32_t cl_numvisedicts;
extern int32_t cl_numvisedicts;
extern entity_t *cl_entities; //johnfitz -- was a static array, now on hunk extern entity_t *cl_entities; //johnfitz -- was a static array, now on hunk
extern int32_t cl_max_edicts; //johnfitz -- only changes when new map loads extern int32_t cl_max_edicts; //johnfitz -- only changes when new map loads
extern dlight_t *cl_dlights;
extern int32_t cl_max_dlights;
//============================================================================= //=============================================================================

View File

@ -139,7 +139,7 @@ void R_RenderDlights(void)
glBlendFunc(GL_ONE, GL_ONE); glBlendFunc(GL_ONE, GL_ONE);
l = cl_dlights; l = cl_dlights;
for(i = 0 ; i < MAX_DLIGHTS ; i++, l++) for(i = 0 ; i < cl_max_dlights ; i++, l++)
{ {
if(l->die < cl.time || !l->radius) if(l->die < cl.time || !l->radius)
continue; continue;
@ -251,7 +251,7 @@ void R_PushDlights(void)
// advanced yet for this frame // advanced yet for this frame
l = cl_dlights; l = cl_dlights;
for(i = 0 ; i < MAX_DLIGHTS ; i++, l++) for(i = 0 ; i < cl_max_dlights ; i++, l++)
{ {
if(l->die < cl.time || !l->radius) if(l->die < cl.time || !l->radius)
continue; continue;

View File

@ -553,7 +553,7 @@ void R_SetupAliasLighting(entity_t *e)
R_LightPoint(e->origin); R_LightPoint(e->origin);
//add dlights //add dlights
for(i = 0 ; i < MAX_DLIGHTS ; i++) for(i = 0 ; i < cl_max_dlights ; i++)
{ {
if(cl_dlights[i].die >= cl.time) if(cl_dlights[i].die >= cl.time)
{ {

View File

@ -545,7 +545,7 @@ void R_DrawBrushModel(entity_t *e)
// instanced model // instanced model
if(clmodel->firstmodelsurface != 0 && !gl_flashblend.value) if(clmodel->firstmodelsurface != 0 && !gl_flashblend.value)
{ {
for(k = 0 ; k < MAX_DLIGHTS ; k++) for(k = 0 ; k < cl_max_dlights ; k++)
{ {
if((cl_dlights[k].die < cl.time) || if((cl_dlights[k].die < cl.time) ||
(!cl_dlights[k].radius)) (!cl_dlights[k].radius))
@ -1065,7 +1065,7 @@ void R_AddDynamicLights(msurface_t *surf)
tmax = (surf->extents[1] >> 4) + 1; tmax = (surf->extents[1] >> 4) + 1;
tex = surf->texinfo; tex = surf->texinfo;
for(lnum = 0 ; lnum < MAX_DLIGHTS ; lnum++) for(lnum = 0 ; lnum < cl_max_dlights ; lnum++)
{ {
if(!(surf->dlightbits[lnum >> 5] & (1U << (lnum & 31)))) if(!(surf->dlightbits[lnum >> 5] & (1U << (lnum & 31))))
continue; // not lit by this light continue; // not lit by this light

View File

@ -288,30 +288,30 @@ void Z_Print(memzone_t *zone)
//============================================================================ //============================================================================
#define HUNK_SENTINAL 0x1df001ed #define HUNK_SENTINEL 0x1df001ed
#define HUNKNAME_LEN 24 #define HUNKNAME_LEN 24
typedef struct typedef struct
{ {
int32_t sentinal; int32_t sentinel;
int32_t size; // including sizeof(hunk_t), -1 = not allocated int32_t size; // including sizeof(hunk_t), -1 = not allocated
char name[HUNKNAME_LEN]; char name[HUNKNAME_LEN];
} hunk_t; } hunk_t;
byte *hunk_base; byte *hunk_base;
int32_t hunk_size; int32_t hunk_size;
int32_t hunk_low_used; int32_t hunk_low_used;
int32_t hunk_high_used; int32_t hunk_high_used;
bool hunk_tempactive; bool hunk_tempactive;
int32_t hunk_tempmark; int32_t hunk_tempmark;
/* /*
============== ==============
Hunk_Check Hunk_Check
Run consistancy and sentinal trahing checks Run consistancy and sentinel trahing checks
============== ==============
*/ */
void Hunk_Check(void) void Hunk_Check(void)
@ -320,8 +320,8 @@ void Hunk_Check(void)
for(h = (hunk_t *)hunk_base ; (byte *)h != hunk_base + hunk_low_used ;) for(h = (hunk_t *)hunk_base ; (byte *)h != hunk_base + hunk_low_used ;)
{ {
if(h->sentinal != HUNK_SENTINAL) if(h->sentinel != HUNK_SENTINEL)
Sys_Error("Hunk_Check: trahsed sentinal"); Sys_Error("Hunk_Check: trahsed sentinel");
if(h->size < (int32_t) sizeof(hunk_t) || h->size + (byte *)h - hunk_base > hunk_size) if(h->size < (int32_t) sizeof(hunk_t) || h->size + (byte *)h - hunk_base > hunk_size)
Sys_Error("Hunk_Check: bad size"); Sys_Error("Hunk_Check: bad size");
h = (hunk_t *)((byte *)h + h->size); h = (hunk_t *)((byte *)h + h->size);
@ -377,8 +377,8 @@ void Hunk_Print(bool all)
// //
// run consistancy checks // run consistancy checks
// //
if(h->sentinal != HUNK_SENTINAL) if(h->sentinel != HUNK_SENTINEL)
Sys_Error("Hunk_Check: trahsed sentinal"); Sys_Error("Hunk_Check: trahsed sentinel");
if(h->size < (int32_t) sizeof(hunk_t) || h->size + (byte *)h - hunk_base > hunk_size) if(h->size < (int32_t) sizeof(hunk_t) || h->size + (byte *)h - hunk_base > hunk_size)
Sys_Error("Hunk_Check: bad size"); Sys_Error("Hunk_Check: bad size");
@ -453,7 +453,7 @@ void *Hunk_AllocName(int32_t size, const char *name)
memset(h, 0, size); memset(h, 0, size);
h->size = size; h->size = size;
h->sentinal = HUNK_SENTINAL; h->sentinel = HUNK_SENTINEL;
q_strlcpy(h->name, name, HUNKNAME_LEN); q_strlcpy(h->name, name, HUNKNAME_LEN);
return (void *)(h + 1); return (void *)(h + 1);
@ -544,7 +544,7 @@ void *Hunk_HighAllocName(int32_t size, const char *name)
memset(h, 0, size); memset(h, 0, size);
h->size = size; h->size = size;
h->sentinal = HUNK_SENTINAL; h->sentinel = HUNK_SENTINEL;
q_strlcpy(h->name, name, HUNKNAME_LEN); q_strlcpy(h->name, name, HUNKNAME_LEN);
return (void *)(h + 1); return (void *)(h + 1);