2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** texpool.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp 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.
|
|
|
|
**
|
|
|
|
** mkxp 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 mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "texpool.h"
|
2013-09-03 13:22:00 +00:00
|
|
|
#include "exception.h"
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-03 13:22:00 +00:00
|
|
|
#include "glstate.h"
|
2013-12-11 19:46:54 +00:00
|
|
|
#include "boost-hash.h"
|
|
|
|
#include "debugwriter.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <list>
|
|
|
|
#include <utility>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
typedef std::pair<uint16_t, uint16_t> Size;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-04 11:30:14 +00:00
|
|
|
static uint32_t byteCount(Size &s)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
return s.first * s.second * 4;
|
|
|
|
}
|
|
|
|
|
2013-11-23 11:27:10 +00:00
|
|
|
struct CacheNode
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-06 10:26:41 +00:00
|
|
|
TEXFBO obj;
|
2013-12-11 19:46:54 +00:00
|
|
|
std::list<TEXFBO>::iterator prioIter;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
bool operator==(const CacheNode &o) const
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
return obj == o.obj;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
typedef std::list<CacheNode> CNodeList;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
struct TexPoolPrivate
|
|
|
|
{
|
|
|
|
/* Contains all cached TexFBOs, grouped by size */
|
2013-12-11 19:46:54 +00:00
|
|
|
BoostHash<Size, CNodeList> poolHash;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Contains all cached TexFBOs, sorted by release time */
|
2013-12-11 19:46:54 +00:00
|
|
|
std::list<TEXFBO> priorityQueue;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Maximal allowed cache memory */
|
2013-09-04 11:30:14 +00:00
|
|
|
const uint32_t maxMemSize;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Current amound of memory consumed by the cache */
|
2013-09-04 11:30:14 +00:00
|
|
|
uint32_t memSize;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Current amount of TexFBOs cached */
|
2013-09-04 11:30:14 +00:00
|
|
|
uint16_t objCount;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Has this pool been disabled? */
|
|
|
|
bool disabled;
|
|
|
|
|
2013-09-04 11:30:14 +00:00
|
|
|
TexPoolPrivate(uint32_t maxMemSize)
|
2013-09-01 14:27:21 +00:00
|
|
|
: maxMemSize(maxMemSize),
|
|
|
|
memSize(0),
|
|
|
|
objCount(0),
|
|
|
|
disabled(false)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2013-09-04 11:30:14 +00:00
|
|
|
TexPool::TexPool(uint32_t maxMemSize)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
p = new TexPoolPrivate(maxMemSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
TexPool::~TexPool()
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
std::list<TEXFBO>::iterator iter;
|
|
|
|
|
|
|
|
for (iter = p->priorityQueue.begin();
|
|
|
|
iter != p->priorityQueue.end();
|
|
|
|
++iter)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
TEXFBO obj = *iter;
|
2013-09-06 10:26:41 +00:00
|
|
|
TEXFBO::fini(obj);
|
2013-09-01 14:27:21 +00:00
|
|
|
--p->objCount;
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
assert(p->objCount == 0);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
2013-09-06 10:26:41 +00:00
|
|
|
TEXFBO TexPool::request(int width, int height)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-11-23 11:27:10 +00:00
|
|
|
CacheNode cnode;
|
2013-09-01 14:27:21 +00:00
|
|
|
Size size(width, height);
|
|
|
|
|
|
|
|
/* See if we can statisfy request from cache */
|
2013-11-23 11:27:10 +00:00
|
|
|
CNodeList &bucket = p->poolHash[size];
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
if (!bucket.empty())
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
/* Found one! */
|
2013-12-11 19:46:54 +00:00
|
|
|
cnode = bucket.back();
|
|
|
|
bucket.pop_back();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-23 11:27:10 +00:00
|
|
|
p->priorityQueue.erase(cnode.prioIter);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->memSize -= byteCount(size);
|
|
|
|
--p->objCount;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
// Debug() << "TexPool: <?+> (" << width << height << ")";
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-23 11:27:10 +00:00
|
|
|
return cnode.obj;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:22:00 +00:00
|
|
|
int maxSize = glState.caps.maxTexSize;
|
|
|
|
if (width > maxSize || height > maxSize)
|
2013-12-29 17:05:11 +00:00
|
|
|
throw Exception(Exception::MKXPError,
|
|
|
|
"Texture dimensions [%d, %d] exceed hardware capabilities",
|
|
|
|
width, height);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Nope, create it instead */
|
2013-11-23 11:27:10 +00:00
|
|
|
TEXFBO::init(cnode.obj);
|
|
|
|
TEXFBO::allocEmpty(cnode.obj, width, height);
|
|
|
|
TEXFBO::linkFBO(cnode.obj);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
// Debug() << "TexPool: <?-> (" << width << height << ")";
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-11-23 11:27:10 +00:00
|
|
|
return cnode.obj;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-06 10:26:41 +00:00
|
|
|
void TexPool::release(TEXFBO &obj)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-06 10:26:41 +00:00
|
|
|
if (obj.tex == TEX::ID(0) || obj.fbo == FBO::ID(0))
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-06 10:26:41 +00:00
|
|
|
TEXFBO::fini(obj);
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->disabled)
|
|
|
|
{
|
|
|
|
/* If we're disabled, delete without caching */
|
2013-12-11 19:46:54 +00:00
|
|
|
// Debug() << "TexPool: <!#> (" << obj.width << obj.height << ")";
|
2013-09-06 10:26:41 +00:00
|
|
|
TEXFBO::fini(obj);
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size size(obj.width, obj.height);
|
|
|
|
|
2013-09-04 11:30:14 +00:00
|
|
|
uint32_t newMemSize = p->memSize + byteCount(size);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* If caching this object would spill over the allowed memory budget,
|
|
|
|
* delete least used objects until we're good again */
|
|
|
|
while (newMemSize > p->maxMemSize)
|
|
|
|
{
|
|
|
|
if (p->objCount == 0)
|
|
|
|
break;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
// Debug() << "TexPool: <!~> Size:" << p->memSize;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Retrieve object with lowest priority for deletion */
|
2013-11-23 11:27:10 +00:00
|
|
|
CacheNode last;
|
2013-12-11 19:46:54 +00:00
|
|
|
last.obj = p->priorityQueue.back();
|
2013-09-01 14:27:21 +00:00
|
|
|
Size removedSize(last.obj.width, last.obj.height);
|
|
|
|
|
2013-11-23 11:27:10 +00:00
|
|
|
CNodeList &bucket = p->poolHash[removedSize];
|
2013-12-11 19:46:54 +00:00
|
|
|
|
|
|
|
std::list<CacheNode>::iterator toRemove =
|
|
|
|
std::find(bucket.begin(), bucket.end(), last);
|
|
|
|
assert(toRemove != bucket.end());
|
|
|
|
bucket.erase(toRemove);
|
|
|
|
|
|
|
|
p->priorityQueue.pop_back();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-06 10:26:41 +00:00
|
|
|
TEXFBO::fini(last.obj);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
newMemSize -= byteCount(removedSize);
|
2013-09-01 14:27:21 +00:00
|
|
|
--p->objCount;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
// Debug() << "TexPool: <!-> (" << last.obj.width << last.obj.height << ")";
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 11:40:23 +00:00
|
|
|
p->memSize = newMemSize;
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
/* Retain object */
|
2013-12-11 19:46:54 +00:00
|
|
|
p->priorityQueue.push_front(obj);
|
2013-11-23 11:27:10 +00:00
|
|
|
CacheNode cnode;
|
|
|
|
cnode.obj = obj;
|
|
|
|
cnode.prioIter = p->priorityQueue.begin();
|
|
|
|
CNodeList &bucket = p->poolHash[size];
|
2013-12-11 19:46:54 +00:00
|
|
|
bucket.push_back(cnode);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
++p->objCount;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
// Debug() << "TexPool: <!+> (" << obj.width << obj.height << ") Current size:" << p->memSize;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TexPool::disable()
|
|
|
|
{
|
|
|
|
p->disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|