- Refine the Cache_Get callback, to use a void * instead of a char *

- Switch alias models over to Cache_Get/Cache_Release
This commit is contained in:
Adam Olsen 2001-08-31 12:22:45 +00:00
parent f54ef94fd0
commit f1a016c8a5
13 changed files with 82 additions and 68 deletions

View file

@ -120,12 +120,12 @@ void Hunk_Check (void);
struct cache_user_s; struct cache_user_s;
typedef void * (*cache_allocator_t) (struct cache_user_s *c, int size, const char *name); typedef void * (*cache_allocator_t) (struct cache_user_s *c, int size, const char *name);
typedef void (*cache_loader_t) (struct cache_user_s *cache, cache_allocator_t allocator); typedef void (*cache_loader_t) (void *object, cache_allocator_t allocator);
typedef struct cache_user_s typedef struct cache_user_s
{ {
void *data; void *data;
char *filename; void *object;
cache_loader_t loader; cache_loader_t loader;
} cache_user_t; } cache_user_t;
@ -143,7 +143,7 @@ void *Cache_Alloc (cache_user_t *c, int size, const char *name);
void Cache_Report (void); void Cache_Report (void);
void Cache_Add (cache_user_t *c, const char *filename, cache_loader_t loader); void Cache_Add (cache_user_t *c, void *object, cache_loader_t loader);
void Cache_Remove (cache_user_t *c); void Cache_Remove (cache_user_t *c);
void *Cache_Get (cache_user_t *c); void *Cache_Get (cache_user_t *c);
void *Cache_TryGet (cache_user_t *c); void *Cache_TryGet (cache_user_t *c);

View file

@ -61,7 +61,7 @@ void SND_Update_ (void);
sfx_t *SND_PrecacheSound (const char *name); sfx_t *SND_PrecacheSound (const char *name);
void SND_ClearBuffer (void); void SND_ClearBuffer (void);
void SND_PaintChannels (int endtime); void SND_PaintChannels (int endtime);
void SND_CallbackLoad (struct cache_user_s *cache, cache_allocator_t allocator); void SND_CallbackLoad (void *object, cache_allocator_t allocator);
void SND_Init_Cvars (); void SND_Init_Cvars ();
@ -330,7 +330,7 @@ SND_FindName (const char *name)
sfx = &known_sfx[i]; sfx = &known_sfx[i];
strcpy (sfx->name, name); strcpy (sfx->name, name);
Cache_Add (&sfx->cache, name, SND_CallbackLoad); Cache_Add (&sfx->cache, sfx, SND_CallbackLoad);
num_sfx++; num_sfx++;

View file

@ -47,7 +47,7 @@ int cache_full_cycle;
byte *SND_Alloc (int size); byte *SND_Alloc (int size);
wavinfo_t SND_GetWavinfo (const char *name, byte * wav, int wavlength); wavinfo_t SND_GetWavinfo (const char *name, byte * wav, int wavlength);
sfxcache_t *SND_LoadSound (cache_user_t *cache, const char *name, cache_allocator_t allocator); sfxcache_t *SND_LoadSound (sfx_t *sfx, cache_allocator_t allocator);
void void
@ -157,7 +157,7 @@ SND_ResampleSfx (sfxcache_t *sc, int inrate, int inwidth, byte * data)
//============================================================================= //=============================================================================
sfxcache_t * sfxcache_t *
SND_LoadSound (cache_user_t *cache, const char *name, cache_allocator_t allocator) SND_LoadSound (sfx_t *sfx, cache_allocator_t allocator)
{ {
char namebuffer[256]; char namebuffer[256];
byte *data; byte *data;
@ -169,7 +169,7 @@ SND_LoadSound (cache_user_t *cache, const char *name, cache_allocator_t allocato
// load it in // load it in
strcpy (namebuffer, "sound/"); strcpy (namebuffer, "sound/");
strncat (namebuffer, name, sizeof (namebuffer) - strlen (namebuffer)); strncat (namebuffer, sfx->name, sizeof (namebuffer) - strlen (namebuffer));
data = COM_LoadStackFile (namebuffer, stackbuf, sizeof (stackbuf)); data = COM_LoadStackFile (namebuffer, stackbuf, sizeof (stackbuf));
@ -178,9 +178,9 @@ SND_LoadSound (cache_user_t *cache, const char *name, cache_allocator_t allocato
return NULL; return NULL;
} }
info = SND_GetWavinfo (name, data, com_filesize); info = SND_GetWavinfo (sfx->name, data, com_filesize);
if (info.channels != 1) { if (info.channels != 1) {
Con_Printf ("%s is a stereo sample\n", name); Con_Printf ("%s is a stereo sample\n", sfx->name);
return NULL; return NULL;
} }
@ -193,7 +193,7 @@ SND_LoadSound (cache_user_t *cache, const char *name, cache_allocator_t allocato
len = len * 2 * info.channels; len = len * 2 * info.channels;
} }
sc = allocator (cache, len + sizeof (sfxcache_t), name); sc = allocator (&sfx->cache, len + sizeof (sfxcache_t), sfx->name);
if (!sc) if (!sc)
return NULL; return NULL;
@ -210,9 +210,9 @@ SND_LoadSound (cache_user_t *cache, const char *name, cache_allocator_t allocato
} }
void void
SND_CallbackLoad (struct cache_user_s *cache, cache_allocator_t allocator) SND_CallbackLoad (void *object, cache_allocator_t allocator)
{ {
SND_LoadSound (cache, cache->filename, allocator); SND_LoadSound (object, allocator);
} }
/* WAV loading */ /* WAV loading */

View file

@ -356,7 +356,7 @@ GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr, void *_m, int _s)
qboolean do_cache = false; qboolean do_cache = false;
aliasmodel = m; aliasmodel = m;
paliashdr = hdr; // (aliashdr_t *)Mod_Extradata (m); paliashdr = hdr;
if (gl_mesh_cache->int_val if (gl_mesh_cache->int_val
&& gl_mesh_cache->int_val <= paliashdr->mdl.numtris) { && gl_mesh_cache->int_val <= paliashdr->mdl.numtris) {

View file

@ -80,7 +80,7 @@ void *Mod_LoadAliasGroup (void *pin, maliasframedesc_t *frame);
//========================================================================= //=========================================================================
void void
Mod_LoadAliasModel (model_t *mod, void *buffer) Mod_LoadAliasModel (model_t *mod, void *buffer, cache_allocator_t allocator)
{ {
int i, j; int i, j;
mdl_t *pinmodel, *pmodel; mdl_t *pinmodel, *pmodel;
@ -94,6 +94,7 @@ Mod_LoadAliasModel (model_t *mod, void *buffer)
byte *p; byte *p;
int len; int len;
unsigned short crc; unsigned short crc;
void *mem;
CRC_Init (&crc); CRC_Init (&crc);
for (len = com_filesize, p = buffer; len; len--, p++) for (len = com_filesize, p = buffer; len; len--, p++)
@ -218,10 +219,10 @@ Mod_LoadAliasModel (model_t *mod, void *buffer)
end = Hunk_LowMark (); end = Hunk_LowMark ();
total = end - start; total = end - start;
Cache_Alloc (&mod->cache, total, loadname); mem = allocator (&mod->cache, total, loadname);
if (!mod->cache.data) if (!mem)
return; return; // FIXME: shouldn't this call Hunk_FreeToLowMark too?!?!
memcpy (mod->cache.data, pheader, total); memcpy (mem, pheader, total);
Hunk_FreeToLowMark (start); Hunk_FreeToLowMark (start);
} }

View file

@ -48,9 +48,11 @@
#include "QF/sys.h" #include "QF/sys.h"
#include "QF/vfs.h" #include "QF/vfs.h"
void Mod_LoadAliasModel (model_t *mod, void *buf); void Mod_LoadAliasModel (model_t *mod, void *buf, cache_allocator_t allocator);
void Mod_LoadSpriteModel (model_t *mod, void *buf); void Mod_LoadSpriteModel (model_t *mod, void *buf);
void Mod_LoadBrushModel (model_t *mod, void *buf); void Mod_LoadBrushModel (model_t *mod, void *buf);
model_t *Mod_RealLoadModel (model_t *mod, qboolean crash, cache_allocator_t allocator);
void Mod_CallbackLoad (void *object, cache_allocator_t allocator);
model_t *loadmodel; model_t *loadmodel;
char loadname[32]; // for hunk tags char loadname[32]; // for hunk tags
@ -143,6 +145,7 @@ Mod_FindName (const char *name)
strcpy (mod->name, name); strcpy (mod->name, name);
mod->needload = true; mod->needload = true;
mod_numknown++; mod_numknown++;
Cache_Add (&mod->cache, mod, Mod_CallbackLoad);
} }
return mod; return mod;
@ -156,19 +159,21 @@ Mod_FindName (const char *name)
model_t * model_t *
Mod_LoadModel (model_t *mod, qboolean crash) Mod_LoadModel (model_t *mod, qboolean crash)
{ {
void *d;
unsigned int *buf;
byte stackbuf[1024]; // avoid dirtying the cache heap
if (!mod->needload) { if (!mod->needload) {
if (mod->type == mod_alias) { if (mod->type == mod_alias) {
d = Cache_Check (&mod->cache); if (Cache_Check (&mod->cache))
if (d)
return mod; return mod;
} else } else
return mod; // not cached at all return mod; // not cached at all
} }
return Mod_RealLoadModel (mod, crash, Cache_Alloc);
}
model_t *
Mod_RealLoadModel (model_t *mod, qboolean crash, cache_allocator_t allocator)
{
unsigned int *buf;
byte stackbuf[1024]; // avoid dirtying the cache heap
// load the file // load the file
buf = buf =
(unsigned int *) COM_LoadStackFile (mod->name, stackbuf, (unsigned int *) COM_LoadStackFile (mod->name, stackbuf,
@ -192,7 +197,7 @@ Mod_LoadModel (model_t *mod, qboolean crash)
switch (LittleLong (*(unsigned int *) buf)) { switch (LittleLong (*(unsigned int *) buf)) {
case IDPOLYHEADER: case IDPOLYHEADER:
Mod_LoadAliasModel (mod, buf); Mod_LoadAliasModel (mod, buf, allocator);
break; break;
case IDSPRITEHEADER: case IDSPRITEHEADER:
@ -207,6 +212,20 @@ Mod_LoadModel (model_t *mod, qboolean crash)
return mod; return mod;
} }
/*
Mod_CallbackLoad
Callback used to load model automatically
*/
void
Mod_CallbackLoad (void *object, cache_allocator_t allocator)
{
if (((model_t *)object)->type != mod_alias)
Sys_Error ("Mod_CallbackLoad for non-alias model? FIXME!\n");
// FIXME: do we want crash set to true?
Mod_RealLoadModel (object, true, allocator);
}
/* /*
Mod_ForName Mod_ForName
@ -223,27 +242,6 @@ Mod_ForName (const char *name, qboolean crash)
return Mod_LoadModel (mod, crash); return Mod_LoadModel (mod, crash);
} }
/*
Mod_Extradata
Caches the data if needed
*/
void *
Mod_Extradata (model_t *mod)
{
void *r;
r = Cache_Check (&mod->cache);
if (r)
return r;
Mod_LoadModel (mod, true);
if (!mod->cache.data)
Sys_Error ("Mod_Extradata: caching failed");
return mod->cache.data;
}
void void
Mod_TouchModel (const char *name) Mod_TouchModel (const char *name)
{ {

View file

@ -963,16 +963,14 @@ Cache_Profile (void)
} }
void void
Cache_Add (cache_user_t *c, const char *filename, cache_loader_t loader) Cache_Add (cache_user_t *c, void *object, cache_loader_t loader)
{ {
CACHE_WRITE_LOCK; CACHE_WRITE_LOCK;
if (c->data || c->filename || c->loader) if (c->data || c->object || c->loader)
Sys_Error ("Cache_Add: cache item already exists!\n"); Sys_Error ("Cache_Add: cache item already exists!\n");
c->filename = strdup (filename); c->object = object;
if (!c->filename)
Sys_Error ("Cache_Add: strdup failed!\n");
c->loader = loader; c->loader = loader;
// c->loader (c, Cache_RealAlloc); // for debugging // c->loader (c, Cache_RealAlloc); // for debugging
@ -985,14 +983,13 @@ Cache_Remove (cache_user_t *c)
{ {
CACHE_WRITE_LOCK; CACHE_WRITE_LOCK;
if (!c->filename || !c->loader) if (!c->object || !c->loader)
Sys_Error ("Cache_Remove: already removed!\n"); Sys_Error ("Cache_Remove: already removed!\n");
if (Cache_RealCheck (c)) if (Cache_RealCheck (c))
Cache_RealFree (c); Cache_RealFree (c);
free (c->filename); c->object = 0;
c->filename = 0;
c->loader = 0; c->loader = 0;
CACHE_WRITE_UNLOCK; CACHE_WRITE_UNLOCK;
@ -1006,7 +1003,7 @@ Cache_TryGet (cache_user_t *c)
mem = Cache_RealCheck (c); mem = Cache_RealCheck (c);
if (!mem) { if (!mem) {
c->loader (c, Cache_RealAlloc); c->loader (c->object, Cache_RealAlloc);
mem = Cache_RealCheck (c); mem = Cache_RealCheck (c);
} }
if (mem) if (mem)

View file

@ -624,7 +624,7 @@ R_DrawAliasModel (entity_t *e, qboolean cull)
VectorNormalize (shadevector); VectorNormalize (shadevector);
// locate the proper data // locate the proper data
paliashdr = (aliashdr_t *) Mod_Extradata (currententity->model); paliashdr = Cache_Get (&currententity->model->cache);
c_alias_polys += paliashdr->mdl.numtris; c_alias_polys += paliashdr->mdl.numtris;
// draw all the triangles // draw all the triangles
@ -714,6 +714,8 @@ R_DrawAliasModel (entity_t *e, qboolean cull)
qfglEnable (GL_TEXTURE_2D); qfglEnable (GL_TEXTURE_2D);
qfglPopMatrix (); qfglPopMatrix ();
} }
Cache_Release (&currententity->model->cache);
} }
/* /*

View file

@ -207,7 +207,7 @@ Skin_Do_Translation_Model (model_t *model, int skinnum, int slot, skin_t *skin)
if (model->type != mod_alias) // only translate skins on alias models if (model->type != mod_alias) // only translate skins on alias models
return; return;
paliashdr = (aliashdr_t *) Mod_Extradata (model); paliashdr = Cache_Get (&model->cache);
if (skinnum < 0 if (skinnum < 0
|| skinnum >= paliashdr->mdl.numskins) { || skinnum >= paliashdr->mdl.numskins) {
Con_Printf ("(%d): Invalid player skin #%d\n", slot, Con_Printf ("(%d): Invalid player skin #%d\n", slot,
@ -222,6 +222,8 @@ Skin_Do_Translation_Model (model_t *model, int skinnum, int slot, skin_t *skin)
inheight = paliashdr->mdl.skinheight; inheight = paliashdr->mdl.skinheight;
build_skin (texnum, original, inwidth, inheight, inwidth, inheight, false); build_skin (texnum, original, inwidth, inheight, inwidth, inheight, false);
Cache_Release (&model->cache);
} }
void void

View file

@ -113,7 +113,7 @@ R_AliasCheckBBox (void)
// expand, rotate, and translate points into worldspace // expand, rotate, and translate points into worldspace
currententity->trivial_accept = 0; currententity->trivial_accept = 0;
pmodel = currententity->model; pmodel = currententity->model;
pahdr = Mod_Extradata (pmodel); pahdr = Cache_Get (&pmodel->cache);
pmdl = (mdl_t *) ((byte *) pahdr + pahdr->model); pmdl = (mdl_t *) ((byte *) pahdr + pahdr->model);
R_AliasSetUpTransform (0); R_AliasSetUpTransform (0);
@ -166,6 +166,7 @@ R_AliasCheckBBox (void)
} }
if (zfullyclipped) { if (zfullyclipped) {
Cache_Release (&pmodel->cache);
return false; // everything was near-z-clipped return false; // everything was near-z-clipped
} }
@ -226,8 +227,10 @@ R_AliasCheckBBox (void)
allclip &= flags; allclip &= flags;
} }
if (allclip) if (allclip) {
Cache_Release (&pmodel->cache);
return false; // trivial reject off one side return false; // trivial reject off one side
}
currententity->trivial_accept = !anyclip & !zclipped; currententity->trivial_accept = !anyclip & !zclipped;
@ -237,6 +240,7 @@ R_AliasCheckBBox (void)
} }
} }
Cache_Release (&pmodel->cache);
return true; return true;
} }
@ -664,7 +668,7 @@ R_AliasDrawModel (alight_t *plighting)
(((long) &finalverts[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1)); (((long) &finalverts[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
pauxverts = &auxverts[0]; pauxverts = &auxverts[0];
paliashdr = (aliashdr_t *) Mod_Extradata (currententity->model); paliashdr = Cache_Get (&currententity->model->cache);
pmdl = (mdl_t *) ((byte *) paliashdr + paliashdr->model); pmdl = (mdl_t *) ((byte *) paliashdr + paliashdr->model);
R_AliasSetupSkin (); R_AliasSetupSkin ();
@ -697,4 +701,6 @@ R_AliasDrawModel (alight_t *plighting)
R_AliasPrepareUnclippedPoints (); R_AliasPrepareUnclippedPoints ();
else else
R_AliasPreparePoints (); R_AliasPreparePoints ();
Cache_Release (&currententity->model->cache);
} }

View file

@ -113,7 +113,7 @@ R_AliasCheckBBox (void)
// expand, rotate, and translate points into worldspace // expand, rotate, and translate points into worldspace
currententity->trivial_accept = 0; currententity->trivial_accept = 0;
pmodel = currententity->model; pmodel = currententity->model;
pahdr = Mod_Extradata (pmodel); pahdr = Cache_Get (&pmodel->cache);
pmdl = (mdl_t *) ((byte *) pahdr + pahdr->model); pmdl = (mdl_t *) ((byte *) pahdr + pahdr->model);
R_AliasSetUpTransform (0); R_AliasSetUpTransform (0);
@ -166,6 +166,7 @@ R_AliasCheckBBox (void)
} }
if (zfullyclipped) { if (zfullyclipped) {
Cache_Release (&pmodel->cache);
return false; // everything was near-z-clipped return false; // everything was near-z-clipped
} }
@ -226,8 +227,10 @@ R_AliasCheckBBox (void)
allclip &= flags; allclip &= flags;
} }
if (allclip) if (allclip) {
Cache_Release (&pmodel->cache);
return false; // trivial reject off one side return false; // trivial reject off one side
}
currententity->trivial_accept = !anyclip & !zclipped; currententity->trivial_accept = !anyclip & !zclipped;
@ -237,6 +240,7 @@ R_AliasCheckBBox (void)
} }
} }
Cache_Release (&pmodel->cache);
return true; return true;
} }
@ -651,7 +655,7 @@ R_AliasDrawModel (alight_t *plighting)
(((long) &finalverts[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1)); (((long) &finalverts[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
pauxverts = &auxverts[0]; pauxverts = &auxverts[0];
paliashdr = (aliashdr_t *) Mod_Extradata (currententity->model); paliashdr = Cache_Get (&currententity->model->cache);
pmdl = (mdl_t *) ((byte *) paliashdr + paliashdr->model); pmdl = (mdl_t *) ((byte *) paliashdr + paliashdr->model);
R_AliasSetupSkin (); R_AliasSetupSkin ();
@ -683,4 +687,6 @@ R_AliasDrawModel (alight_t *plighting)
R_AliasPrepareUnclippedPoints (); R_AliasPrepareUnclippedPoints ();
else else
R_AliasPreparePoints (); R_AliasPreparePoints ();
Cache_Release (&currententity->model->cache);
} }

View file

@ -1373,12 +1373,13 @@ PrintFrameName (model_t *m, int frame)
aliashdr_t *hdr; aliashdr_t *hdr;
maliasframedesc_t *pframedesc; maliasframedesc_t *pframedesc;
hdr = (aliashdr_t *) Mod_Extradata (m); hdr = Cache_TryGet (&m->cache);
if (!hdr) if (!hdr)
return; return;
pframedesc = &hdr->frames[frame]; pframedesc = &hdr->frames[frame];
Con_Printf ("frame %i: %s\n", frame, pframedesc->name); Con_Printf ("frame %i: %s\n", frame, pframedesc->name);
Cache_Release (&m->cache);
} }
/* /*

View file

@ -296,13 +296,14 @@ Model_NextDownload (void)
info_key = emodel_name; info_key = emodel_name;
if (info_key && cl_model_crcs->int_val) { if (info_key && cl_model_crcs->int_val) {
aliashdr_t *ahdr = (aliashdr_t *) Mod_Extradata aliashdr_t *ahdr = Cache_Get
(cl.model_precache[i]); (&cl.model_precache[i]->cache);
Info_SetValueForKey (cls.userinfo, info_key, va ("%d", ahdr->crc), Info_SetValueForKey (cls.userinfo, info_key, va ("%d", ahdr->crc),
MAX_INFO_STRING, 0); MAX_INFO_STRING, 0);
MSG_WriteByte (&cls.netchan.message, clc_stringcmd); MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
SZ_Print (&cls.netchan.message, va ("setinfo %s %d", info_key, SZ_Print (&cls.netchan.message, va ("setinfo %s %d", info_key,
ahdr->crc)); ahdr->crc));
Cache_Release (&cl.model_precache[i]->cache);
} }
} }