Jeck - [Requires JGE rebuild] Extensive cache improvements.

* Numerous cache fixes, reduced filesystem access.
 * Cache fails very gracefully. 
 * Cache is now a templated class, with individual caches per data-type.
 * Much easier to extend.
 * Extensively debugged. Try compiling with -DDEBUG_CACHE.
 * Caches limits can be set on a per-item basis.
 * hgeParticleSystemInfo are now cached, mana particles now fall back to defaults.
 * Samples are not cached, but track filesystem misses using the cache backbone. 
 * Avatars are cached. Default baka avatar is now baka.jpg, to prevent collision with player.

A note on the retrieval types: 
  RETRIEVE_MANAGE puts a resource into a seperate, managed resource list.
    Managed resources are guarenteed valid for the lifetime of the program. 
    Retrieving a managed quad promotes the associated texture to managed. Don't do that by mistake.
    Calls to Resources.Refresh() will attempt to reload managed resources in place.

  RETRIVE_LOCK (and by extension, RETRIEVE_VRAM), returns a resource after locking it.
    A resource may have many locks, and remains in cache until they are all released. 
    If the resource is managed, it returns it unmodified.

A note on quads:
Unlike all other RetrieveWhatever() functions, the default behavior for RetrieveQuad is RETRIEVE_LOCK. Worse, Release(JQuad*) is slow, and will /always/ release one lock from the associated texture.   

There's a long and complicated explanation for this, involving support for live relinking of textures to existing quads, but basically what it means is that we only use RetrieveQuad for quads we intend to store and later Release(). If a temporary quad is needed, the preferred method is to use NEW JQuad* and SAFE_DELETE with RetrieveTexture(). RetrieveTempQuad is also provided, but is only guaranteed until the next call to the cache.

Note that RetrieveCard has none of these problems.
This commit is contained in:
wagic.jeck
2009-09-14 08:28:49 +00:00
parent a696b65ffb
commit d55fc91112
27 changed files with 3732 additions and 2992 deletions

View File

@@ -0,0 +1,387 @@
#include "../include/config.h"
#include "../include/utils.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <JGE.h>
#include <JFileSystem.h>
#include "../include/GameOptions.h"
#include "../include/WResourceManager.h"
//WResource
WResource::~WResource(){
return;
}
WResource::WResource(){
locks = WRES_UNLOCKED;
lastTime = resources.nowTime();
loadedMode = 0;
}
bool WResource::isLocked(){
return (locks != WRES_UNLOCKED);
}
bool WResource::isPermanent(){
return (locks == WRES_PERMANENT);
}
void WResource::deadbolt(){
if(locks <= WRES_MAX_LOCK)
locks = WRES_PERMANENT;
}
void WResource::lock(){
if(locks < WRES_MAX_LOCK)
locks++;
}
void WResource::unlock(bool force){
if(force)
locks = 0;
else if(locks > WRES_UNLOCKED && locks <= WRES_MAX_LOCK)
locks--;
}
void WResource::hit(){
lastTime = resources.nowTime();
}
//WCachedTexture
WCachedTexture::WCachedTexture(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached texture created.\n");
#endif
texture = NULL;
}
WCachedTexture::~WCachedTexture(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached texture destroyed.\n");
#endif
if(texture)
SAFE_DELETE(texture);
if(!trackedQuads.size())
return;
map<JQuad*,string>::iterator it, nit;
for(it=trackedQuads.begin();it!=trackedQuads.end();it=nit){
nit = it;
nit++;
trackedQuads.erase(it);
}
trackedQuads.clear();
}
JTexture * WCachedTexture::Actual(){
return texture;
}
bool WCachedTexture::ReleaseQuad(JQuad* quad){
#ifdef DEBUG_CACHE
OutputDebugString("Quad released.\n");
#endif
for(map<JQuad*,string>::iterator i = trackedQuads.begin();i!=trackedQuads.end();i++){
if(i->first == quad ){
unlock();
trackedQuads.erase(i);
return true;
}
}
return false;
}
JQuad * WCachedTexture::GetQuad(float offX, float offY, float width, float height,string resname){
if(texture == NULL)
return NULL;
JQuad * jq = NULL;
map<JQuad*,string>::iterator it;
std::transform(resname.begin(),resname.end(),resname.begin(),::tolower);
if(width == 0.0f || width > texture->mWidth)
width = texture->mWidth;
if(height == 0.0f || height > texture->mHeight)
height = texture->mHeight;
for(it = trackedQuads.begin();it!=trackedQuads.end();it++){
if(it->second == resname){
jq = it->first;
}
}
if(jq == NULL){
jq = NEW JQuad(texture,offX,offY,width,height);
if(!jq) {
//Probably out of memory. Try again.
resources.Cleanup();
jq = NEW JQuad(texture,offX,offY,width,height);
}
if(!jq)
return NULL; //Probably a crash.
trackedQuads[jq] = resname;
return jq;
}else{
//Update JQ's values to what we called this with.
jq->SetTextureRect(offX,offY,width,height);
return jq;
}
return NULL;
}
JQuad * WCachedTexture::GetQuad(string resname){
map<JQuad*,string>::iterator it;
std::transform(resname.begin(),resname.end(),resname.begin(),::tolower);
for(it = trackedQuads.begin();it!=trackedQuads.end();it++){
if(it->second == resname){
return it->first;
}
}
return NULL;
}
JQuad * WCachedTexture::GetCard(float offX, float offY, float width, float height, string resname){
JQuad * jq = GetQuad(offX,offY,width,height,resname);
if(jq)
jq->SetHotSpot(jq->mTex->mWidth / 2, jq->mTex->mHeight / 2);
return jq;
}
unsigned long WCachedTexture::size(){
if(!texture)
return 0;
return texture->mTexHeight*texture->mTexWidth;
}
bool WCachedTexture::isGood(){
if(!texture)
return false;
return true;
}
void WCachedTexture::Refresh(string filename){
int error = 0;
JTexture* old = texture;
texture = NULL;
if(!Attempt(filename,loadedMode, error))
SAFE_DELETE(texture);
if(!texture)
texture = old;
else
SAFE_DELETE(old);
for(map<JQuad*,string>::iterator it=trackedQuads.begin();it!=trackedQuads.end();it++){
if(it->first)
it->first->mTex = texture;
}
}
bool WCachedTexture::Attempt(string filename, int submode, int & error){
int format = TEXTURE_FORMAT;
loadedMode = submode;
string realname;
//Form correct filename.
if(submode & TEXTURE_SUB_CARD){
if(submode & TEXTURE_SUB_THUMB){
for(string::size_type i= 0;i < filename.size();i++){
if(filename[i] == '\\' || filename[i] == '/'){
filename.insert(i+1,"thumbnails/");
break;
}
}
}
realname = resources.cardFile(filename);
}
else{
if(submode & TEXTURE_SUB_THUMB)
filename.insert(0,"thumbnails/");
if(submode & TEXTURE_SUB_AVATAR)
realname = resources.avatarFile(filename);
else
realname = resources.graphicsFile(filename);
}
//Apply pixel mode
if(submode & TEXTURE_SUB_5551)
format = GU_PSM_5551;
//Use Vram?
if(submode & TEXTURE_SUB_VRAM){
texture = JRenderer::GetInstance()->LoadTexture(realname.c_str(),TEX_TYPE_USE_VRAM,format);
}
else
texture = JRenderer::GetInstance()->LoadTexture(realname.c_str(),TEX_TYPE_NORMAL,format);
//Failure.
if(!texture){
if(!fileExists(realname.c_str()))
error = CACHE_ERROR_404;
return false;
}
//Failure of a different sort.
if(texture->mTexId == -1){
SAFE_DELETE(texture);
error = CACHE_ERROR_BAD;
return false;
}
error = CACHE_ERROR_NONE;
return true;
}
void WCachedTexture::Nullify(){
if(texture)
texture = NULL;
}
//WCachedSample
void WCachedSample::Nullify(){
if(sample){
sample = NULL;
}
}
WCachedSample::WCachedSample(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached sample created.\n");
#endif
sample = NULL;
}
WCachedSample::~WCachedSample(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached sample destroyed.\n");
#endif
SAFE_DELETE(sample);
}
JSample * WCachedSample::Actual(){
return sample;
}
unsigned long WCachedSample::size(){
if(!sample || !sample->mSample)
return 0;
#if defined WIN32 || defined LINUX
return FSOUND_Sample_GetLength(sample->mSample);
#else
return sample->mSample->fileSize;
#endif
}
bool WCachedSample::isGood(){
if(!sample || !sample->mSample)
return false;
return true;
}
void WCachedSample::Refresh(string filename){
return;
}
bool WCachedSample::Attempt(string filename, int submode, int & error){
loadedMode = submode;
sample = JSoundSystem::GetInstance()->LoadSample(resources.sfxFile(filename).c_str());
if(!isGood()){
SAFE_DELETE(sample);
if(!fileExists(filename.c_str()))
error = CACHE_ERROR_404;
else
error = CACHE_ERROR_BAD;
return false;
}
return true;
}
//WCachedParticles
bool WCachedParticles::isGood(){
if(!particles)
return false;
return true;
}
unsigned long WCachedParticles::size(){
if(!particles)
return 0; //Sizeof(pointer)
return sizeof(hgeParticleSystemInfo);
}
//Only effects future particle systems, of course.
void WCachedParticles::Refresh(string filename){
hgeParticleSystemInfo * old = particles;
int error = 0;
Attempt(filename,loadedMode,error);
if(isGood())
SAFE_DELETE(old);
else{
SAFE_DELETE(particles);
particles = old;
}
return;
}
bool WCachedParticles::Attempt(string filename, int submode, int & error){
JFileSystem* fileSys = JFileSystem::GetInstance();
if(!fileSys->OpenFile(resources.graphicsFile(filename))){
error = CACHE_ERROR_404;
return false;
}
SAFE_DELETE(particles);
particles = NEW hgeParticleSystemInfo;
fileSys->ReadFile(particles, sizeof(hgeParticleSystemInfo));
fileSys->CloseFile();
particles->sprite=NULL;
error = CACHE_ERROR_NONE;
return true;
}
hgeParticleSystemInfo * WCachedParticles::Actual(){
return particles;
}
WCachedParticles::WCachedParticles(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached particles created.\n");
#endif
particles = NULL;
}
WCachedParticles::~WCachedParticles(){
#ifdef DEBUG_CACHE
OutputDebugString("Cached particles destroyed.\n");
#endif
SAFE_DELETE(particles);
}
void WCachedParticles::Nullify(){
if(particles)
particles = NULL;
}