From d6a1636b4d3d52313181fd5d8436db3873db5768 Mon Sep 17 00:00:00 2001 From: danij Date: Sun, 8 Dec 2013 23:36:06 +0000 Subject: [PATCH] Refactor|Resources: ResourceSystem has ownership of raw textures --- doomsday/client/client.pro | 1 - doomsday/client/include/resource/rawtexture.h | 26 +--- .../client/include/resource/resourcesystem.h | 18 +++ doomsday/client/src/dd_main.cpp | 3 +- doomsday/client/src/gl/gl_main.cpp | 2 +- doomsday/client/src/gl/gl_texmanager.cpp | 4 +- doomsday/client/src/resource/rawtexture.cpp | 125 ------------------ .../client/src/resource/resourcesystem.cpp | 97 ++++++++++++++ doomsday/client/src/ui/finaleinterpreter.cpp | 6 +- doomsday/client/src/ui/ui2_main.cpp | 2 +- 10 files changed, 126 insertions(+), 158 deletions(-) delete mode 100644 doomsday/client/src/resource/rawtexture.cpp diff --git a/doomsday/client/client.pro b/doomsday/client/client.pro index 73f7142d61..bfdb750bb2 100644 --- a/doomsday/client/client.pro +++ b/doomsday/client/client.pro @@ -715,7 +715,6 @@ SOURCES += \ src/resource/patch.cpp \ src/resource/patchname.cpp \ src/resource/pcx.cpp \ - src/resource/rawtexture.cpp \ src/resource/resourcesystem.cpp \ src/resource/sprite.cpp \ src/resource/texture.cpp \ diff --git a/doomsday/client/include/resource/rawtexture.h b/doomsday/client/include/resource/rawtexture.h index d639dc5940..87da47cf2e 100644 --- a/doomsday/client/include/resource/rawtexture.h +++ b/doomsday/client/include/resource/rawtexture.h @@ -18,8 +18,8 @@ * 02110-1301 USA */ -#ifndef LIBDENG_RESOURCE_RAWTEXTURE -#define LIBDENG_RESOURCE_RAWTEXTURE +#ifndef DENG_RESOURCE_RAWTEXTURE +#define DENG_RESOURCE_RAWTEXTURE #include "dd_share.h" // For lumpnum_t #include @@ -37,24 +37,4 @@ struct rawtex_t rawtex_t *next; }; -void R_InitRawTexs(); -void R_UpdateRawTexs(); - -/** - * Returns a rawtex_t for the given lump if one already exists; otherwise @c 0. - */ -rawtex_t *R_FindRawTex(lumpnum_t lumpNum); - -/** - * Get a rawtex_t data structure for a raw texture specified with a WAD lump - * number. Allocates a new rawtex_t if it hasn't been loaded yet. - */ -rawtex_t *R_GetRawTex(lumpnum_t lumpNum); - -/** - * Returns a NULL-terminated array of pointers to all the rawtexs. - * The array must be freed with Z_Free. - */ -rawtex_t **R_CollectRawTexs(int *count = 0); - -#endif // LIBDENG_RESOURCE_RAWTEXTURE +#endif // DENG_RESOURCE_RAWTEXTURE diff --git a/doomsday/client/include/resource/resourcesystem.h b/doomsday/client/include/resource/resourcesystem.h index 3c9322e5ea..38b1d214a6 100644 --- a/doomsday/client/include/resource/resourcesystem.h +++ b/doomsday/client/include/resource/resourcesystem.h @@ -463,6 +463,23 @@ class ResourceSystem : public de::System patchid_t declarePatch(de::String encodedName); + /** + * Returns a rawtex_t for the given lump if one already exists; otherwise @c 0. + */ + rawtex_t *rawTexture(lumpnum_t lumpNum); + + /** + * Get a rawtex_t data structure for a raw texture specified with a WAD lump + * number. Allocates a new rawtex_t if it hasn't been loaded yet. + */ + 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. + */ + rawtex_t **collectRawTextures(int *count = 0); + #ifdef __CLIENT__ /** * Determines if a manifest exists for a resource on @a path. @@ -843,6 +860,7 @@ class ResourceSystem : public de::System public: /// @todo Should be private: void initCompositeTextures(); void initFlatTextures(); + void initRawTextures(); void initSpriteTextures(); void initSystemTextures(); diff --git a/doomsday/client/src/dd_main.cpp b/doomsday/client/src/dd_main.cpp index f9ba58bca2..8dc5b90a5a 100644 --- a/doomsday/client/src/dd_main.cpp +++ b/doomsday/client/src/dd_main.cpp @@ -2014,7 +2014,6 @@ static int DD_StartupWorker(void * /*context*/) UI_LoadFonts(); #endif R_InitTranslationTables(); - R_InitRawTexs(); R_InitSvgs(); #ifdef __CLIENT__ R_InitViewWindow(); @@ -2104,7 +2103,7 @@ static int DD_UpdateEngineStateWorker(void *context) // Re-read definitions. Def_Read(); - R_UpdateRawTexs(); + App_ResourceSystem().initRawTextures(); App_ResourceSystem().initSprites(); // Fully reinitialize sprites. #ifdef __CLIENT__ App_ResourceSystem().initModels(); // Defs might've changed. diff --git a/doomsday/client/src/gl/gl_main.cpp b/doomsday/client/src/gl/gl_main.cpp index c822c94aed..ca94cb4814 100644 --- a/doomsday/client/src/gl/gl_main.cpp +++ b/doomsday/client/src/gl/gl_main.cpp @@ -999,7 +999,7 @@ void GL_SetPSprite(Material *mat, int tClass, int tMap) void GL_SetRawImage(lumpnum_t lumpNum, gl::Wrapping wrapS, gl::Wrapping wrapT) { - if(rawtex_t *rawTex = R_GetRawTex(lumpNum)) + if(rawtex_t *rawTex = App_ResourceSystem().declareRawTexture(lumpNum)) { GL_BindTextureUnmanaged(GL_PrepareRawTexture(*rawTex), wrapS, wrapT, (filterUI ? gl::Linear : gl::Nearest)); diff --git a/doomsday/client/src/gl/gl_texmanager.cpp b/doomsday/client/src/gl/gl_texmanager.cpp index 83e8d00b1c..8324817bd7 100644 --- a/doomsday/client/src/gl/gl_texmanager.cpp +++ b/doomsday/client/src/gl/gl_texmanager.cpp @@ -369,7 +369,7 @@ GLuint GL_PrepareRawTexture(rawtex_t &raw) void GL_SetRawTexturesMinFilter(int newMinFilter) { - rawtex_t **rawTexs = R_CollectRawTexs(); + rawtex_t **rawTexs = App_ResourceSystem().collectRawTextures(); for(rawtex_t **ptr = rawTexs; *ptr; ptr++) { rawtex_t *r = *ptr; @@ -387,7 +387,7 @@ void GL_SetRawTexturesMinFilter(int newMinFilter) void GL_ReleaseTexturesForRawImages() { - rawtex_t **rawTexs = R_CollectRawTexs(); + rawtex_t **rawTexs = App_ResourceSystem().collectRawTextures(); for(rawtex_t **ptr = rawTexs; *ptr; ptr++) { rawtex_t *r = (*ptr); diff --git a/doomsday/client/src/resource/rawtexture.cpp b/doomsday/client/src/resource/rawtexture.cpp deleted file mode 100644 index e1f74d1411..0000000000 --- a/doomsday/client/src/resource/rawtexture.cpp +++ /dev/null @@ -1,125 +0,0 @@ -/** @file rawtexture.cpp Raw Texture - * - * @authors Copyright © 2003-2013 Jaakko Keränen - * @authors Copyright © 2005-2013 Daniel Swanson - * - * @par License - * GPL: http://www.gnu.org/licenses/gpl.html - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. This program is distributed in the hope that it - * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General - * Public License for more details. You should have received a copy of the GNU - * General Public License along with this program; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA - */ - -#include "de_base.h" -#include "de_filesys.h" -#include - -#include "resource/rawtexture.h" - -#define RAWTEX_HASH_SIZE 128 -#define RAWTEX_HASH(x) (rawtexhash + (((unsigned) x) & (RAWTEX_HASH_SIZE - 1))) - -struct RawTexHash -{ - rawtex_t *first; -}; - -static RawTexHash rawtexhash[RAWTEX_HASH_SIZE]; - -rawtex_t **R_CollectRawTexs(int *count) -{ - // First count the number of patchtexs. - int num = 0; - for(int i = 0; i < RAWTEX_HASH_SIZE; ++i) - for(rawtex_t *r = 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 < RAWTEX_HASH_SIZE; ++i) - for(rawtex_t *r = rawtexhash[i].first; r; r = r->next) - { - list[num++] = r; - } - - // Terminate. - list[num] = NULL; - - return list; -} - -rawtex_t *R_FindRawTex(lumpnum_t lumpNum) -{ - LOG_AS("R_FindRawTex"); - if(-1 == lumpNum || lumpNum >= F_LumpCount()) - { - LOG_DEBUG("LumpNum #%i out of bounds (%i), returning 0.") << lumpNum << F_LumpCount(); - return 0; - } - - for(rawtex_t *i = RAWTEX_HASH(lumpNum)->first; i; i = i->next) - { - if(i->lumpNum == lumpNum) - return i; - } - return 0; -} - -rawtex_t *R_GetRawTex(lumpnum_t lumpNum) -{ - LOG_AS("R_GetRawTex"); - if(-1 == lumpNum || lumpNum >= F_LumpCount()) - { - LOG_DEBUG("LumpNum #%i out of bounds (%i), returning 0.") << lumpNum << F_LumpCount(); - return 0; - } - - // Check if this lumpNum has already been loaded as a rawtex. - rawtex_t *r = R_FindRawTex(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. - RawTexHash *hash = RAWTEX_HASH(lumpNum); - r->next = hash->first; - hash->first = r; - - return r; -} - -void R_InitRawTexs() -{ - std::memset(rawtexhash, 0, sizeof(rawtexhash)); -} - -void R_UpdateRawTexs() -{ - for(int i = 0; i < RAWTEX_HASH_SIZE; ++i) - for(rawtex_t *rawTex = rawtexhash[i].first; rawTex; rawTex = rawTex->next) - { - Str_Free(&rawTex->name); - } - - Z_FreeTags(PU_REFRESHRAW, PU_REFRESHRAW); - R_InitRawTexs(); -} diff --git a/doomsday/client/src/resource/resourcesystem.cpp b/doomsday/client/src/resource/resourcesystem.cpp index 8fb31751da..4b1c2c2ef3 100644 --- a/doomsday/client/src/resource/resourcesystem.cpp +++ b/doomsday/client/src/resource/resourcesystem.cpp @@ -37,6 +37,7 @@ # include "MaterialSnapshot" #endif +#include "api_filesys.h" #include "filesys/fs_main.h" #include "filesys/lumpindex.h" @@ -195,6 +196,16 @@ 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)]; + } + /// System subspace schemes containing the manifests/resources. MaterialSchemes materialSchemes; QList materialSchemeCreationOrder; @@ -327,6 +338,8 @@ 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")); @@ -2445,6 +2458,90 @@ patchid_t ResourceSystem::declarePatch(String encodedName) return 0; } +rawtex_t *ResourceSystem::rawTexture(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(); + return 0; + } + + for(rawtex_t *i = d->rawTextureHash(lumpNum).first; i; i = i->next) + { + if(i->lumpNum == lumpNum) + return i; + } + return 0; +} + +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(); + 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) + { + list[num++] = r; + } + + // Terminate. + list[num] = NULL; + + return list; +} + +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); +} + MaterialScheme &ResourceSystem::materialScheme(String name) const { LOG_AS("ResourceSystem::materialScheme"); diff --git a/doomsday/client/src/ui/finaleinterpreter.cpp b/doomsday/client/src/ui/finaleinterpreter.cpp index 00cc3a967e..b1742fa97d 100644 --- a/doomsday/client/src/ui/finaleinterpreter.cpp +++ b/doomsday/client/src/ui/finaleinterpreter.cpp @@ -1563,7 +1563,7 @@ DEFFC(Image) FIData_PicClearAnimation(obj); - rawTex = R_GetRawTex(lumpNum); + rawTex = App_ResourceSystem().declareRawTexture(lumpNum); if(NULL != rawTex) { FIData_PicAppendFrame(obj, PFT_RAW, -1, &rawTex->lumpNum, 0, false); @@ -1584,7 +1584,7 @@ DEFFC(ImageAt) AnimatorVector3_Init(obj->pos, x, y, 0); FIData_PicClearAnimation(obj); - rawTex = R_GetRawTex(lumpNum); + rawTex = App_ResourceSystem().declareRawTexture(lumpNum); if(NULL != rawTex) { FIData_PicAppendFrame(obj, PFT_RAW, -1, &rawTex->lumpNum, 0, false); @@ -1721,7 +1721,7 @@ DEFFC(AnimImage) char const *encodedName = OP_CSTRING(1); int tics = FRACSECS_TO_TICKS(OP_FLOAT(2)); lumpnum_t lumpNum = F_LumpNumForName(encodedName); - rawtex_t *rawTex = R_GetRawTex(lumpNum); + rawtex_t *rawTex = App_ResourceSystem().declareRawTexture(lumpNum); if(rawTex) { FIData_PicAppendFrame(obj, PFT_RAW, tics, &rawTex->lumpNum, 0, false); diff --git a/doomsday/client/src/ui/ui2_main.cpp b/doomsday/client/src/ui/ui2_main.cpp index 919896039e..19a4848b24 100644 --- a/doomsday/client/src/ui/ui2_main.cpp +++ b/doomsday/client/src/ui/ui2_main.cpp @@ -999,7 +999,7 @@ static void drawPicFrame(fidata_pic_t *p, uint frame, float const _origin[3], switch(f->type) { case PFT_RAW: { - rawtex_t *rawTex = R_GetRawTex(f->texRef.lumpNum); + rawtex_t *rawTex = App_ResourceSystem().declareRawTexture(f->texRef.lumpNum); if(rawTex) { DGLuint glName = GL_PrepareRawTexture(*rawTex);