From: <re...@us...> - 2007-10-30 22:49:29
|
Revision: 28048 http://crystal.svn.sourceforge.net/crystal/?rev=28048&view=rev Author: res2002 Date: 2007-10-30 15:49:27 -0700 (Tue, 30 Oct 2007) Log Message: ----------- res changed CS::RenderManager::TextureCache to utilize CS::Utility::GenericResourceCache internally. Modified Paths: -------------- CS/branches/feature/rendermanager/docs/history.txt CS/branches/feature/rendermanager/include/csplugincommon/rendermanager/texturecache.h Modified: CS/branches/feature/rendermanager/docs/history.txt =================================================================== --- CS/branches/feature/rendermanager/docs/history.txt 2007-10-30 22:43:19 UTC (rev 28047) +++ CS/branches/feature/rendermanager/docs/history.txt 2007-10-30 22:49:27 UTC (rev 28048) @@ -31,6 +31,8 @@ resource cache deals with that while allowing for necessary behaviour customization (e.g. only return a resource that satisfies some constraint). + - res changed CS::RenderManager::TextureCache to utilize + CS::Utility::GenericResourceCache internally. 28-Oct-2007 - res changed the Jam LinkWith rules to add a dependency of targets on the "naked" target names (e.g. 'crystalspace') instead of the Modified: CS/branches/feature/rendermanager/include/csplugincommon/rendermanager/texturecache.h =================================================================== --- CS/branches/feature/rendermanager/include/csplugincommon/rendermanager/texturecache.h 2007-10-30 22:43:19 UTC (rev 28047) +++ CS/branches/feature/rendermanager/include/csplugincommon/rendermanager/texturecache.h 2007-10-30 22:49:27 UTC (rev 28048) @@ -19,44 +19,79 @@ #ifndef __CS_CSPLUGINCOMMON_RENDERMANAGER_TEXTURECACHE_H__ #define __CS_CSPLUGINCOMMON_RENDERMANAGER_TEXTURECACHE_H__ +#include "csutil/genericresourcecache.h" + struct iTextureHandle; namespace CS { namespace RenderManager { + namespace TextureCacheImpl + { + struct TextureSizeConstraint + { + struct KeyType + { + int w, h; + }; + + static bool IsLargerEqual (const csRef<iTextureHandle>& t1, + const csRef<iTextureHandle>& t2) + { + int tw1, th1, tw2, th2; + t1->GetRendererDimensions (tw1, th1); + t2->GetRendererDimensions (tw2, th2); + + if ((tw1 > tw2) && (th1 > th2)) return true; + return false; + } + + static bool IsEqual (const csRef<iTextureHandle>& t1, + const csRef<iTextureHandle>& t2) + { + int tw1, th1, tw2, th2; + t1->GetRendererDimensions (tw1, th1); + t2->GetRendererDimensions (tw2, th2); + + if ((tw1 == tw2) && (th1 == th2)) return true; + return false; + } + + static bool IsLargerEqual (const csRef<iTextureHandle>& t1, + const KeyType& t2) + { + int tw1, th1; + t1->GetRendererDimensions (tw1, th1); + + if ((tw1 > t2.w) && (th1 > t2.h)) return true; + return false; + } + + static bool IsEqual (const csRef<iTextureHandle>& t1, + const KeyType& t2) + { + int tw1, th1; + t1->GetRendererDimensions (tw1, th1); + + if ((tw1 == t2.w) && (th1 == t2.h)) return true; + return false; + } + + }; + } // namespace TextureCacheImpl + class TextureCache { csRef<iGraphics3D> g3d; + + CS::Utility::GenericResourceCache<csRef<iTextureHandle>, + csTicks, TextureCacheImpl::TextureSizeConstraint> backend; - struct HeldTexture - { - csRef<iTextureHandle> texture; - csTicks deathTime; - }; csImageType imgtype; const char* format; int textureFlags; const char* texClass; - - uint currentTime; - - csArray<HeldTexture> texInUse; - csArray<HeldTexture> texAvailable; - - static int SortHeldTexture (const HeldTexture& t1, const HeldTexture& t2) - { - int tw1, th1, tw2, th2; - t1.texture->GetRendererDimensions (tw1, th1); - t2.texture->GetRendererDimensions (tw2, th2); - - if (tw1 < tw2) return -1; - else if (tw1 > tw2) return 1; - if (th1 < th2) return -1; - else if (th1 > th2) return 1; - return 0; - } - uint options; public: enum @@ -68,54 +103,35 @@ TextureCache (csImageType imgtype, const char* format, int textureFlags, const char* texClass, uint options) : g3d (0), imgtype (imgtype), format (format), textureFlags (textureFlags), texClass (texClass), - currentTime (0), options (options) {} + options (options) + { + backend.agedPurgeInterval = 5000; + backend.purgeAge = 10000; + } void SetG3D (iGraphics3D* g3d) { this->g3d = g3d; } void Clear () { - texInUse.DeleteAll (); - texAvailable.DeleteAll (); + backend.Clear (); } void AdvanceFrame (csTicks currentTime) { - this->currentTime = currentTime; - size_t i = 0; - while (i < texInUse.GetSize()) - { - if (currentTime > texInUse[i].deathTime) - { - texAvailable.InsertSorted (texInUse[i], SortHeldTexture); - texInUse.DeleteIndexFast (i); - } - else - i++; - } + backend.AdvanceTime (currentTime); } iTextureHandle* QueryUnusedTexture (int width, int height, csTicks lifetime, int& real_w, int& real_h) { - HeldTexture tex; - size_t i = 0; - while (i < texAvailable.GetSize()) + TextureCacheImpl::TextureSizeConstraint::KeyType queryKey; + queryKey.w = width; queryKey.h = height; + csRef<iTextureHandle>* tex = backend.Query (queryKey, + (options & tcacheExactSizeMatch)); + if (tex != 0) { - const HeldTexture& oldTex = texAvailable[i]; - iTextureHandle* texh = oldTex.texture; - int tw, th; - texh->GetRendererDimensions (tw, th); - if (((options & tcacheExactSizeMatch) && (tw == width) && (th == height)) - || ((tw >= width) && (th >= height))) - { - tex = oldTex; - texAvailable.DeleteIndex (i); - tex.deathTime = currentTime + lifetime; - texInUse.Push (tex); - real_w = tw; - real_h = th; - return tex.texture; - } - i++; + (*tex)->GetRendererDimensions (real_w, real_h); + return *tex; } + if (options & tcachePowerOfTwo) { width = csFindNearestPowerOf2 (width); @@ -123,12 +139,15 @@ } real_w = width; real_h = height; - tex.texture = g3d->GetTextureManager()->CreateTexture ( - width, height, imgtype, format, textureFlags); - tex.texture->SetTextureClass (texClass); - tex.deathTime = currentTime + lifetime; - texInUse.Push (tex); - return tex.texture; + + csRef<iTextureHandle> newTex ( + g3d->GetTextureManager()->CreateTexture ( + width, height, imgtype, format, textureFlags)); + newTex->SetTextureClass (texClass); + + backend.AddActive (newTex); + + return newTex; } iTextureHandle* QueryUnusedTexture (int width, int height, csTicks lifetime) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |