clear all _cacheFiles on recycle

This commit is contained in:
SamGondelman 2017-12-15 17:52:48 -08:00
parent 3477aa57a8
commit 3ae52c0e0e
3 changed files with 23 additions and 4 deletions

View file

@ -772,7 +772,7 @@ void GLBackend::recycle() const {
GLVariableAllocationSupport::manageMemory();
GLVariableAllocationSupport::_frameTexturesCreated = 0;
Texture::KtxStorage::clearKtxFiles();
}
void GLBackend::setCameraCorrection(const Mat4& correction) {

View file

@ -321,10 +321,15 @@ public:
void reset() override { }
// Don't keep files open forever. We close them at the beginning of each frame (GLBackend::recycle)
static std::vector<std::pair<std::shared_ptr<storage::FileStorage>, std::shared_ptr<std::mutex>>> _cachedKtxFiles;
static std::mutex _cachedKtxFilesMutex;
static void clearKtxFiles();
protected:
std::shared_ptr<storage::FileStorage> maybeOpenFile() const;
mutable std::mutex _cacheFileMutex;
mutable std::shared_ptr<std::mutex> _cacheFileMutex { std::make_shared<std::mutex>() };
mutable std::shared_ptr<storage::FileStorage> _cacheFile;
std::string _filename;

View file

@ -23,6 +23,9 @@ using namespace gpu;
using PixelsPointer = Texture::PixelsPointer;
using KtxStorage = Texture::KtxStorage;
std::vector<std::pair<std::shared_ptr<storage::FileStorage>, std::shared_ptr<std::mutex>>> KtxStorage::_cachedKtxFiles;
std::mutex KtxStorage::_cachedKtxFilesMutex;
struct GPUKTXPayload {
using Version = uint8;
@ -190,15 +193,26 @@ KtxStorage::KtxStorage(const std::string& filename) : _filename(filename) {
std::shared_ptr<storage::FileStorage> KtxStorage::maybeOpenFile() const {
if (!_cacheFile) {
_cacheFile = std::make_shared<storage::FileStorage>(_filename.c_str());
std::lock_guard<std::mutex> lock(KtxStorage::_cachedKtxFilesMutex);
_cachedKtxFiles.emplace_back(_cacheFile, _cacheFileMutex);
}
return _cacheFile;
}
void KtxStorage::clearKtxFiles() {
std::lock_guard<std::mutex> lock(KtxStorage::_cachedKtxFilesMutex);
for (auto& cacheFileAndMutex : KtxStorage::_cachedKtxFiles) {
std::lock_guard<std::mutex> lock(*(cacheFileAndMutex.second));
cacheFileAndMutex.first.reset();
}
_cachedKtxFiles.clear();
}
PixelsPointer KtxStorage::getMipFace(uint16 level, uint8 face) const {
auto faceOffset = _ktxDescriptor->getMipFaceTexelsOffset(level, face);
auto faceSize = _ktxDescriptor->getMipFaceTexelsSize(level, face);
if (faceSize != 0 && faceOffset != 0) {
std::lock_guard<std::mutex> lock(_cacheFileMutex);
std::lock_guard<std::mutex> lock(*_cacheFileMutex);
auto file = maybeOpenFile();
if (file) {
auto storageView = file->createView(faceSize, faceOffset);
@ -244,7 +258,7 @@ void KtxStorage::assignMipData(uint16 level, const storage::StoragePointer& stor
return;
}
std::lock_guard<std::mutex> lock(_cacheFileMutex);
std::lock_guard<std::mutex> lock(*_cacheFileMutex);
auto file = maybeOpenFile();
if (!file) {
qWarning() << "Failed to open file to assign mip data " << QString::fromStdString(_filename);