Skip to content

Commit

Permalink
renders: dynamic allocation of PVS/PHS buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
0lvin committed Dec 22, 2024
1 parent de20289 commit 54e32cb
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 27 deletions.
34 changes: 29 additions & 5 deletions src/client/refresh/gl1/gl1_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

#include "header/local.h"

static YQ2_ALIGNAS_TYPE(int) byte mod_novis[MAX_MAP_LEAFS / 8];
static byte *mod_novis = NULL;
static size_t mod_novis_len = 0;

static model_t mod_known[MAX_MOD_KNOWN];
static int mod_numknown = 0;
Expand Down Expand Up @@ -65,16 +66,21 @@ Mod_HasFreeSpace(void)
const byte *
Mod_ClusterPVS(int cluster, const model_t *model)
{
if (!mod_novis)
{
Com_Error(ERR_DROP, "%s: incrorrect init of PVS/PHS", __func__);
}

if (!model->vis)
{
Mod_DecompressVis(NULL, mod_novis, NULL,
(model->vis->numclusters + 7) >> 3);
(model->numclusters + 7) >> 3);
return mod_novis;
}

if (cluster == -1)
{
memset(mod_novis, 0, sizeof(mod_novis));
memset(mod_novis, 0, (model->numclusters + 7) >> 3);
return mod_novis;
}

Expand All @@ -86,7 +92,7 @@ Mod_ClusterPVS(int cluster, const model_t *model)
Mod_DecompressVis((byte *)model->vis +
model->vis->bitofs[cluster][DVIS_PVS], mod_novis,
(byte *)model->vis + model->numvisibility,
(model->vis->numclusters + 7) >> 3);
(model->numclusters + 7) >> 3);
return mod_novis;
}

Expand Down Expand Up @@ -131,7 +137,8 @@ void
Mod_Init(void)
{
mod_max = 0;
memset(mod_novis, 0xff, sizeof(mod_novis));
mod_novis = NULL;
mod_novis_len = 0;
}

static void
Expand Down Expand Up @@ -389,6 +396,15 @@ Mod_LoadBrushModel(model_t *mod, const void *buffer, int modfilelen)
Com_Error(ERR_DROP, "%s: Map %s has incorrect number of clusters %d != %d",
__func__, mod->name, mod->numclusters, mod->vis->numclusters);
}

if ((mod->numleafs > mod_novis_len) || !mod_novis)
{
/* reallocate buffers for PVS/PHS buffers*/
mod_novis_len = (mod->numleafs + 63) & ~63;
mod_novis = malloc(mod_novis_len / 8);
Com_Printf("Allocated " YQ2_COM_PRIdS " bit leafs of PVS/PHS buffer\n",
mod_novis_len);
}
}

/* Temporary solution, need to use load file dirrectly */
Expand Down Expand Up @@ -601,6 +617,14 @@ Mod_FreeAll(void)
Mod_Free(&mod_known[i]);
}
}

/* Free PVS buffer */
if (mod_novis)
{
free(mod_novis);
mod_novis = NULL;
}
mod_novis_len = 0;
}

/*
Expand Down
34 changes: 29 additions & 5 deletions src/client/refresh/gl3/gl3_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

#include "header/local.h"

static YQ2_ALIGNAS_TYPE(int) byte mod_novis[MAX_MAP_LEAFS / 8];
static byte *mod_novis = NULL;
static size_t mod_novis_len = 0;

static gl3model_t mod_known[MAX_MOD_KNOWN];
static int mod_numknown = 0;
Expand Down Expand Up @@ -66,16 +67,21 @@ Mod_HasFreeSpace(void)
const byte *
GL3_Mod_ClusterPVS(int cluster, const gl3model_t *model)
{
if (!mod_novis)
{
Com_Error(ERR_DROP, "%s: incrorrect init of PVS/PHS", __func__);
}

if (!model->vis)
{
Mod_DecompressVis(NULL, mod_novis, NULL,
(model->vis->numclusters + 7) >> 3);
(model->numclusters + 7) >> 3);
return mod_novis;
}

if (cluster == -1)
{
memset(mod_novis, 0, sizeof(mod_novis));
memset(mod_novis, 0, (model->numclusters + 7) >> 3);
return mod_novis;
}

Expand All @@ -87,7 +93,7 @@ GL3_Mod_ClusterPVS(int cluster, const gl3model_t *model)
Mod_DecompressVis((byte *)model->vis +
model->vis->bitofs[cluster][DVIS_PVS], mod_novis,
(byte *)model->vis + model->numvisibility,
(model->vis->numclusters + 7) >> 3);
(model->numclusters + 7) >> 3);
return mod_novis;
}

Expand Down Expand Up @@ -132,7 +138,8 @@ void
GL3_Mod_Init(void)
{
mod_max = 0;
memset(mod_novis, 0xff, sizeof(mod_novis));
mod_novis = NULL;
mod_novis_len = 0;
}

static void
Expand Down Expand Up @@ -390,6 +397,15 @@ Mod_LoadBrushModel(gl3model_t *mod, const void *buffer, int modfilelen)
Com_Error(ERR_DROP, "%s: Map %s has incorrect number of clusters %d != %d",
__func__, mod->name, mod->numclusters, mod->vis->numclusters);
}

if ((mod->numleafs > mod_novis_len) || !mod_novis)
{
/* reallocate buffers for PVS/PHS buffers*/
mod_novis_len = (mod->numleafs + 63) & ~63;
mod_novis = malloc(mod_novis_len / 8);
Com_Printf("Allocated " YQ2_COM_PRIdS " bit leafs of PVS/PHS buffer\n",
mod_novis_len);
}
}

/* Temporary solution, need to use load file dirrectly */
Expand Down Expand Up @@ -602,6 +618,14 @@ GL3_Mod_FreeAll(void)
Mod_Free(&mod_known[i]);
}
}

/* Free PVS buffer */
if (mod_novis)
{
free(mod_novis);
mod_novis = NULL;
}
mod_novis_len = 0;
}

/*
Expand Down
34 changes: 29 additions & 5 deletions src/client/refresh/gl4/gl4_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

#include "header/local.h"

YQ2_ALIGNAS_TYPE(int) static byte mod_novis[MAX_MAP_LEAFS / 8];
static byte *mod_novis = NULL;
static size_t mod_novis_len = 0;

static gl4model_t mod_known[MAX_MOD_KNOWN];
static int mod_numknown = 0;
Expand Down Expand Up @@ -66,16 +67,21 @@ Mod_HasFreeSpace(void)
const byte *
GL4_Mod_ClusterPVS(int cluster, const gl4model_t *model)
{
if (!mod_novis)
{
Com_Error(ERR_DROP, "%s: incrorrect init of PVS/PHS", __func__);
}

if (!model->vis)
{
Mod_DecompressVis(NULL, mod_novis, NULL,
(model->vis->numclusters + 7) >> 3);
(model->numclusters + 7) >> 3);
return mod_novis;
}

if (cluster == -1)
{
memset(mod_novis, 0, sizeof(mod_novis));
memset(mod_novis, 0, (model->numclusters + 7) >> 3);
return mod_novis;
}

Expand All @@ -87,7 +93,7 @@ GL4_Mod_ClusterPVS(int cluster, const gl4model_t *model)
Mod_DecompressVis((byte *)model->vis +
model->vis->bitofs[cluster][DVIS_PVS], mod_novis,
(byte *)model->vis + model->numvisibility,
(model->vis->numclusters + 7) >> 3);
(model->numclusters + 7) >> 3);
return mod_novis;
}

Expand Down Expand Up @@ -132,7 +138,8 @@ void
GL4_Mod_Init(void)
{
mod_max = 0;
memset(mod_novis, 0xff, sizeof(mod_novis));
mod_novis = NULL;
mod_novis_len = 0;
}

static void
Expand Down Expand Up @@ -390,6 +397,15 @@ Mod_LoadBrushModel(gl4model_t *mod, const void *buffer, int modfilelen)
Com_Error(ERR_DROP, "%s: Map %s has incorrect number of clusters %d != %d",
__func__, mod->name, mod->numclusters, mod->vis->numclusters);
}

if ((mod->numleafs > mod_novis_len) || !mod_novis)
{
/* reallocate buffers for PVS/PHS buffers*/
mod_novis_len = (mod->numleafs + 63) & ~63;
mod_novis = malloc(mod_novis_len / 8);
Com_Printf("Allocated " YQ2_COM_PRIdS " bit leafs of PVS/PHS buffer\n",
mod_novis_len);
}
}

/* Temporary solution, need to use load file dirrectly */
Expand Down Expand Up @@ -602,6 +618,14 @@ GL4_Mod_FreeAll(void)
Mod_Free(&mod_known[i]);
}
}

/* Free PVS buffer */
if (mod_novis)
{
free(mod_novis);
mod_novis = NULL;
}
mod_novis_len = 0;
}

/*
Expand Down
35 changes: 30 additions & 5 deletions src/client/refresh/soft/sw_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
#include <limits.h>
#include "header/local.h"

static YQ2_ALIGNAS_TYPE(int) byte mod_novis[MAX_MAP_LEAFS / 8];
static byte *mod_novis = NULL;
static size_t mod_novis_len = 0;

static model_t mod_known[MAX_MOD_KNOWN];
static int mod_numknown = 0;
Expand Down Expand Up @@ -69,16 +70,21 @@ Mod_HasFreeSpace(void)
const byte *
Mod_ClusterPVS(int cluster, const model_t *model)
{
if (!mod_novis)
{
Com_Error(ERR_DROP, "%s: incrorrect init of PVS/PHS", __func__);
}

if (!model->vis)
{
Mod_DecompressVis(NULL, mod_novis, NULL,
(model->vis->numclusters + 7) >> 3);
(model->numclusters + 7) >> 3);
return mod_novis;
}

if (cluster == -1)
{
memset(mod_novis, 0, sizeof(mod_novis));
memset(mod_novis, 0, (model->numclusters + 7) >> 3);
return mod_novis;
}

Expand All @@ -90,7 +96,7 @@ Mod_ClusterPVS(int cluster, const model_t *model)
Mod_DecompressVis((byte *)model->vis +
model->vis->bitofs[cluster][DVIS_PVS], mod_novis,
(byte *)model->vis + model->numvisibility,
(model->vis->numclusters + 7) >> 3);
(model->numclusters + 7) >> 3);
return mod_novis;
}

Expand Down Expand Up @@ -135,7 +141,8 @@ void
Mod_Init(void)
{
mod_max = 0;
memset(mod_novis, 0xff, sizeof(mod_novis));
mod_novis = NULL;
mod_novis_len = 0;
}

static void
Expand Down Expand Up @@ -395,6 +402,16 @@ Mod_LoadBrushModel(model_t *mod, const void *buffer, int modfilelen)
Com_Error(ERR_DROP, "%s: Map %s has incorrect number of clusters %d != %d",
__func__, mod->name, mod->numclusters, mod->vis->numclusters);
}

if ((mod->numleafs > mod_novis_len) || !mod_novis)
{
/* reallocate buffers for PVS/PHS buffers*/
mod_novis_len = (mod->numleafs + 63) & ~63;
mod_novis = malloc(mod_novis_len / 8);
Com_Printf("Allocated " YQ2_COM_PRIdS " bit leafs of PVS/PHS buffer\n",
mod_novis_len);
}

R_InitSkyBox(mod);
}

Expand Down Expand Up @@ -608,6 +625,14 @@ Mod_FreeAll(void)
Mod_Free(&mod_known[i]);
}
}

/* Free PVS buffer */
if (mod_novis)
{
free(mod_novis);
mod_novis = NULL;
}
mod_novis_len = 0;
}

/*
Expand Down
Loading

0 comments on commit 54e32cb

Please sign in to comment.