- streamlining fog of war unit cache

This commit is contained in:
Mark Vejvoda
2013-11-11 22:11:13 +00:00
parent 6705a346ef
commit feedde5f61
6 changed files with 22 additions and 48 deletions

View File

@@ -212,6 +212,7 @@ private:
bool quitGameCalled; bool quitGameCalled;
bool disableSpeedChange; bool disableSpeedChange;
std::map<int,FowAlphaCellsLookupItem> teamFowAlphaCellsLookupItem;
std::map<string,int64> gamePerformanceCounts; std::map<string,int64> gamePerformanceCounts;
public: public:

View File

@@ -54,10 +54,7 @@ class GameSettings;
class FowAlphaCellsLookupItem { class FowAlphaCellsLookupItem {
public: public:
std::vector<Vec2i> surfPosList; std::map<Vec2i,float> surfPosAlphaList;
std::vector<float> alphaList;
static time_t lastDebug;
}; };
// ===================================================== // =====================================================

View File

@@ -1278,18 +1278,15 @@ FowAlphaCellsLookupItem Unit::getFogOfWarRadius(bool useCache) const {
if(dist > sightRange) { if(dist > sightRange) {
alpha= clamp(1.f-(dist - sightRange) / (World::indirectSightRange), 0.f, maxAlpha); alpha= clamp(1.f-(dist - sightRange) / (World::indirectSightRange), 0.f, maxAlpha);
} }
result.surfPosList.push_back(surfPos); result.surfPosAlphaList[surfPos] = alpha;
result.alphaList.push_back(alpha);
} }
return result; return result;
} }
void Unit::calculateFogOfWarRadius() { void Unit::calculateFogOfWarRadius() {
if(game->getWorld()->getFogOfWar() == true) { if(game->getWorld()->getFogOfWar() == true) {
//if(Config::getInstance().getBool("EnableFowCache","true") == true && this->pos != this->cachedFowPos) {
if(this->pos != this->cachedFowPos) { if(this->pos != this->cachedFowPos) {
cachedFow = getFogOfWarRadius(false); cachedFow = getFogOfWarRadius(false);
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(mutexCommands,mutexOwnerId); MutexSafeWrapper safeMutex(mutexCommands,mutexOwnerId);
this->cachedFowPos = this->pos; this->cachedFowPos = this->pos;
@@ -4160,8 +4157,7 @@ Vec2i Unit::getPos() {
} }
void Unit::clearCaches() { void Unit::clearCaches() {
cachedFow.alphaList.clear(); cachedFow.surfPosAlphaList.clear();
cachedFow.surfPosList.clear();
cachedFowPos = Vec2i(0,0); cachedFowPos = Vec2i(0,0);
if(unitPath != NULL) { if(unitPath != NULL) {

View File

@@ -137,14 +137,14 @@ Minimap::~Minimap() {
void Minimap::incFowTextureAlphaSurface(const Vec2i &sPos, float alpha, void Minimap::incFowTextureAlphaSurface(const Vec2i &sPos, float alpha,
bool isIncrementalUpdate) { bool isIncrementalUpdate) {
if(fowPixmap1) { if(fowPixmap1) {
assert(sPos.x<fowPixmap1->getW() && sPos.y<fowPixmap1->getH()); assert(sPos.x < fowPixmap1->getW() && sPos.y < fowPixmap1->getH());
if(fowPixmap1->getPixelf(sPos.x, sPos.y)<alpha){ if(fowPixmap1->getPixelf(sPos.x, sPos.y) < alpha){
fowPixmap1->setPixel(sPos.x, sPos.y, alpha); fowPixmap1->setPixel(sPos.x, sPos.y, alpha);
} }
if(fowPixmap1Copy != NULL && isIncrementalUpdate == true) { if(fowPixmap1Copy != NULL && isIncrementalUpdate == true) {
if(fowPixmap1Copy->getPixelf(sPos.x, sPos.y)<alpha){ if(fowPixmap1Copy->getPixelf(sPos.x, sPos.y) < alpha){
fowPixmap1Copy->setPixel(sPos.x, sPos.y, alpha); fowPixmap1Copy->setPixel(sPos.x, sPos.y, alpha);
} }
} }

View File

@@ -54,9 +54,6 @@ World::World() {
ExploredCellsLookupItemCache.clear(); ExploredCellsLookupItemCache.clear();
ExploredCellsLookupItemCacheTimer.clear(); ExploredCellsLookupItemCacheTimer.clear();
ExploredCellsLookupItemCacheTimerCount = 0; ExploredCellsLookupItemCacheTimerCount = 0;
// Disable this cache as it takes too much RAM (not sure if its worth the performance gain)
//enableFowAlphaCellsLookupItemCache = config.getBool("EnableFowCache","true");
enableFowAlphaCellsLookupItemCache = true;
nextCommandGroupId = 0; nextCommandGroupId = 0;
techTree = NULL; techTree = NULL;
@@ -2566,23 +2563,13 @@ void World::computeFow() {
faction->getTeam() == thisTeamIndex && faction->getTeam() == thisTeamIndex &&
unit->isOperative() == true) { unit->isOperative() == true) {
if(enableFowAlphaCellsLookupItemCache == true) { const FowAlphaCellsLookupItem &cellList = unit->getCachedFow();
const FowAlphaCellsLookupItem &cellList = unit->getCachedFow(); for(std::map<Vec2i,float>::const_iterator iterMap = cellList.surfPosAlphaList.begin();
for(int cellIndex = 0; cellIndex < cellList.surfPosList.size(); ++cellIndex) { iterMap != cellList.surfPosAlphaList.end(); ++iterMap) {
const Vec2i &surfPos = cellList.surfPosList[cellIndex]; const Vec2i &surfPos = iterMap->first;
const float &alpha = cellList.alphaList[cellIndex]; const float &alpha = iterMap->second;
minimap.incFowTextureAlphaSurface(surfPos, alpha, true); minimap.incFowTextureAlphaSurface(surfPos, alpha, true);
}
}
else {
const FowAlphaCellsLookupItem cellList = unit->getFogOfWarRadius(false);
for(int cellIndex = 0; cellIndex < cellList.surfPosList.size(); ++cellIndex) {
const Vec2i &surfPos = cellList.surfPosList[cellIndex];
const float &alpha = cellList.alphaList[cellIndex];
minimap.incFowTextureAlphaSurface(surfPos, alpha, true);
}
} }
} }
} }
@@ -2687,28 +2674,26 @@ string World::getExploredCellsLookupItemCacheStats() {
string World::getFowAlphaCellsLookupItemCacheStats() { string World::getFowAlphaCellsLookupItemCacheStats() {
string result = ""; string result = "";
int surfPosCount = 0; int surfPosAlphaCount = 0;
int alphaListCount = 0;
for(int i=0; i<getFactionCount(); ++i) { for(int factionIndex = 0; factionIndex < getFactionCount(); ++factionIndex) {
Faction *faction= getFaction(i); Faction *faction= getFaction(factionIndex);
if(faction->getTeam() == thisTeamIndex) { if(faction->getTeam() == thisTeamIndex) {
for(int j=0; j<faction->getUnitCount(); ++j) { for(int unitIndex = 0; unitIndex < faction->getUnitCount(); ++unitIndex) {
const Unit *unit= faction->getUnit(j); const Unit *unit= faction->getUnit(unitIndex);
const FowAlphaCellsLookupItem &cache = unit->getCachedFow(); const FowAlphaCellsLookupItem &cache = unit->getCachedFow();
surfPosCount += (int)cache.surfPosList.size(); surfPosAlphaCount += (int)cache.surfPosAlphaList.size();
alphaListCount += (int)cache.alphaList.size();
} }
} }
} }
uint64 totalBytes = surfPosCount * sizeof(Vec2i); uint64 totalBytes = surfPosAlphaCount * sizeof(Vec2i);
totalBytes += alphaListCount * sizeof(float); totalBytes += surfPosAlphaCount * sizeof(float);
totalBytes /= 1000; totalBytes /= 1000;
char szBuf[8096]=""; char szBuf[8096]="";
snprintf(szBuf,8096,"surface count [%d] alpha count [%d] total KB: %s",surfPosCount,alphaListCount,formatNumber(totalBytes).c_str()); snprintf(szBuf,8096,"surface count [%d] total KB: %s",surfPosAlphaCount,formatNumber(totalBytes).c_str());
result = szBuf; result = szBuf;
return result; return result;
} }

View File

@@ -84,9 +84,6 @@ private:
std::map<int,ExploredCellsLookupKey> ExploredCellsLookupItemCacheTimer; std::map<int,ExploredCellsLookupKey> ExploredCellsLookupItemCacheTimer;
int ExploredCellsLookupItemCacheTimerCount; int ExploredCellsLookupItemCacheTimerCount;
bool enableFowAlphaCellsLookupItemCache;
//std::map<Vec2i, std::map<int, FowAlphaCellsLookupItem > > FowAlphaCellsLookupItemCache;
public: public:
static const int generationArea= 100; static const int generationArea= 100;
static const int indirectSightRange= 5; static const int indirectSightRange= 5;
@@ -95,7 +92,6 @@ private:
Map map; Map map;
Tileset tileset; Tileset tileset;
//TechTree techTree;
TechTree *techTree; TechTree *techTree;
TimeFlow timeFlow; TimeFlow timeFlow;
Scenario scenario; Scenario scenario;
@@ -115,7 +111,6 @@ private:
int thisFactionIndex; int thisFactionIndex;
int thisTeamIndex; int thisTeamIndex;
int frameCount; int frameCount;
//int nextUnitId;
Mutex mutexFactionNextUnitId; Mutex mutexFactionNextUnitId;
std::map<int,int> mapFactionNextUnitId; std::map<int,int> mapFactionNextUnitId;