Skip to content

Commit

Permalink
Refactor|ResourceSystem: Use a QHash for raw textures and return a QL…
Browse files Browse the repository at this point in the history
…ist of instance pointers
  • Loading branch information
danij-deng committed Dec 11, 2013
1 parent 8f5c5f1 commit 67d35c4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 84 deletions.
14 changes: 11 additions & 3 deletions doomsday/client/include/resource/rawtexture.h
Expand Up @@ -22,19 +22,27 @@
#define DENG_RESOURCE_RAWTEXTURE

#include "dd_share.h" // For lumpnum_t
#include <de/str.h>
#include <de/String>

/**
* A rawtex is a lump raw graphic that has been prepared for render.
*/
struct rawtex_t
{
ddstring_t name; ///< Percent-encoded.
de::String name; ///< Percent-encoded.
lumpnum_t lumpNum;
DGLuint tex; /// Name of the associated DGL texture.
short width, height;
byte masked;
rawtex_t *next;

rawtex_t(de::String name, lumpnum_t lumpNum)
: name(name)
, lumpNum(lumpNum)
, tex(0)
, width(0)
, height(0)
, masked(0)
{}
};

#endif // DENG_RESOURCE_RAWTEXTURE
5 changes: 2 additions & 3 deletions doomsday/client/include/resource/resourcesystem.h
Expand Up @@ -482,10 +482,9 @@ class ResourceSystem : public de::System
rawtex_t *declareRawTexture(lumpnum_t lumpNum);

/**
* Returns a NULL-terminated array of pointers to all the rawtexs.
* The array must be freed with Z_Free.
* Returns a list of pointers to all the raw textures in the collection.
*/
rawtex_t **collectRawTextures(int *count = 0);
QList<rawtex_t *> collectRawTextures() const;

#ifdef __CLIENT__
/**
Expand Down
22 changes: 8 additions & 14 deletions doomsday/client/src/gl/gl_texmanager.cpp
Expand Up @@ -279,7 +279,7 @@ static res::Source loadRaw(image_t &image, rawtex_t const &raw)
// First try an external resource.
try
{
String foundPath = App_FileSystem().findPath(de::Uri("Patches", Path(Str_Text(&raw.name))),
String foundPath = App_FileSystem().findPath(de::Uri("Patches", Path(raw.name)),
RLF_DEFAULT, App_ResourceClass(RC_GRAPHIC));
// Ensure the found path is absolute.
foundPath = App_BasePath() / foundPath;
Expand Down Expand Up @@ -371,34 +371,28 @@ GLuint GL_PrepareRawTexture(rawtex_t &raw)

void GL_SetRawTexturesMinFilter(int newMinFilter)
{
rawtex_t **rawTexs = App_ResourceSystem().collectRawTextures();
for(rawtex_t **ptr = rawTexs; *ptr; ptr++)
foreach(rawtex_t *raw, App_ResourceSystem().collectRawTextures())
{
rawtex_t *r = *ptr;
if(r->tex) // Is the texture loaded?
if(raw->tex) // Is the texture loaded?
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();

glBindTexture(GL_TEXTURE_2D, r->tex);
glBindTexture(GL_TEXTURE_2D, raw->tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, newMinFilter);
}
}
Z_Free(rawTexs);
}

void GL_ReleaseTexturesForRawImages()
{
rawtex_t **rawTexs = App_ResourceSystem().collectRawTextures();
for(rawtex_t **ptr = rawTexs; *ptr; ptr++)
foreach(rawtex_t *raw, App_ResourceSystem().collectRawTextures())
{
rawtex_t *r = (*ptr);
if(r->tex)
if(raw->tex)
{
glDeleteTextures(1, (GLuint const *) &r->tex);
r->tex = 0;
glDeleteTextures(1, (GLuint const *) &raw->tex);
raw->tex = 0;
}
}
Z_Free(rawTexs);
LOG_MSG("All GL textures for RawTextures deleted.");
}
94 changes: 30 additions & 64 deletions doomsday/client/src/resource/resourcesystem.cpp
Expand Up @@ -196,15 +196,8 @@ DENG2_PIMPL(ResourceSystem)
/// All texture instances in the system (from all schemes).
AllTextures textures;

static int const RAWTEX_HASH_SIZE = 128;
struct RawTexHash {
rawtex_t *first;
};
RawTexHash rawtexhash[RAWTEX_HASH_SIZE];

inline RawTexHash &rawTextureHash(lumpnum_t lumpNum) {
return rawtexhash[((unsigned) lumpNum) & (RAWTEX_HASH_SIZE - 1)];
}
typedef QHash<lumpnum_t, rawtex_t *> RawTextureHash;
RawTextureHash rawTexHash;

/// System subspace schemes containing the manifests/resources.
MaterialSchemes materialSchemes;
Expand Down Expand Up @@ -338,8 +331,6 @@ DENG2_PIMPL(ResourceSystem)
, modelRepository(0)
#endif
{
zap(rawtexhash);

LOG_AS("ResourceSystem");
resClasses.append(new ResourceClass("RC_PACKAGE", "Packages"));
resClasses.append(new ResourceClass("RC_DEFINITION", "Defs"));
Expand Down Expand Up @@ -386,6 +377,9 @@ DENG2_PIMPL(ResourceSystem)
#endif
self.clearAllTextureSchemes();
clearTextureManifests();
#ifdef __CLIENT__
clearAllRawTextures();
#endif

#ifdef __CLIENT__
self.purgeCacheQueue();
Expand Down Expand Up @@ -466,6 +460,12 @@ DENG2_PIMPL(ResourceSystem)
newScheme->audienceForManifestDefined += this;
}

void clearAllRawTextures()
{
qDeleteAll(rawTexHash);
rawTexHash.clear();
}

#ifdef __CLIENT__
void clearFontManifests()
{
Expand Down Expand Up @@ -688,7 +688,7 @@ DENG2_PIMPL(ResourceSystem)
if(!textureSpecInUse(*spec))
{
it.remove();
delete &spec;
delete spec;
numPruned += 1;
}
}
Expand Down Expand Up @@ -2471,10 +2471,10 @@ rawtex_t *ResourceSystem::rawTexture(lumpnum_t lumpNum)
return 0;
}

for(rawtex_t *i = d->rawTextureHash(lumpNum).first; i; i = i->next)
Instance::RawTextureHash::iterator found = d->rawTexHash.find(lumpNum);
if(found != d->rawTexHash.end())
{
if(i->lumpNum == lumpNum)
return i;
return found.value();
}
return 0;
}
Expand All @@ -2484,66 +2484,32 @@ rawtex_t *ResourceSystem::declareRawTexture(lumpnum_t lumpNum)
LOG_AS("ResourceSystem::rawTexture");
if(-1 == lumpNum || lumpNum >= F_LumpCount())
{
LOG_DEBUG("LumpNum #%i out of bounds (%i), returning 0.") << lumpNum << F_LumpCount();
LOG_DEBUG("LumpNum #%i out of range %s, returning 0.")
<< lumpNum << Rangeui(0, F_LumpCount()).asText();
return 0;
}

// Check if this lumpNum has already been loaded as a rawtex.
rawtex_t *r = rawTexture(lumpNum);
if(r) return r;

// Hmm, this is an entirely new rawtex.
r = (rawtex_t *) Z_Calloc(sizeof(*r), PU_REFRESHRAW, 0);
F_FileName(Str_Init(&r->name), Str_Text(F_LumpName(lumpNum)));
r->lumpNum = lumpNum;

// Link to the hash.
Instance::RawTexHash &hash = d->rawTextureHash(lumpNum);
r->next = hash.first;
hash.first = r;

return r;
}

rawtex_t **ResourceSystem::collectRawTextures(int *count)
{
// First count the number of patchtexs.
int num = 0;
for(int i = 0; i < Instance::RAWTEX_HASH_SIZE; ++i)
for(rawtex_t *r = d->rawtexhash[i].first; r; r = r->next)
{
num++;
}

// Tell this to the caller.
if(count) *count = num;

// Allocate the array, plus one for the terminator.
rawtex_t **list = (rawtex_t **) Z_Malloc(sizeof(**list) * (num + 1), PU_APPSTATIC, NULL);

// Collect the pointers.
num = 0;
for(int i = 0; i < Instance::RAWTEX_HASH_SIZE; ++i)
for(rawtex_t *r = d->rawtexhash[i].first; r; r = r->next)
// Has this raw texture already been declared?
rawtex_t *raw = rawTexture(lumpNum);
if(!raw)
{
list[num++] = r;
// An entirely new raw texture.
String const &name = App_FileSystem().nameIndex().lump(lumpNum).name();
raw = new rawtex_t(name, lumpNum);
d->rawTexHash.insert(lumpNum, raw);
}

// Terminate.
list[num] = NULL;
return raw;
}

return list;
QList<rawtex_t *> ResourceSystem::collectRawTextures() const
{
return d->rawTexHash.values();
}

void ResourceSystem::initRawTextures()
{
for(int i = 0; i < Instance::RAWTEX_HASH_SIZE; ++i)
for(rawtex_t *rawTex = d->rawtexhash[i].first; rawTex; rawTex = rawTex->next)
{
Str_Free(&rawTex->name);
}
Z_FreeTags(PU_REFRESHRAW, PU_REFRESHRAW);
zap(d->rawtexhash);
d->clearAllRawTextures();
}

MaterialScheme &ResourceSystem::materialScheme(String name) const
Expand Down

0 comments on commit 67d35c4

Please sign in to comment.