CustomTexture: prefetch all available textures

This commit is contained in:
degasus
2015-03-01 23:53:15 +01:00
parent 7e6ec5fa86
commit 242f7d964d
8 changed files with 170 additions and 14 deletions

View File

@ -5,7 +5,9 @@
#include <algorithm>
#include <cinttypes>
#include <cstring>
#include <mutex>
#include <string>
#include <thread>
#include <utility>
#include <xxhash.h>
#include <SOIL/SOIL.h>
@ -13,7 +15,10 @@
#include "Common/CommonPaths.h"
#include "Common/FileSearch.h"
#include "Common/FileUtil.h"
#include "Common/Flag.h"
#include "Common/StringUtil.h"
#include "Common/Thread.h"
#include "Common/Timer.h"
#include "Core/ConfigManager.h"
@ -22,18 +27,59 @@
#include "VideoCommon/VideoConfig.h"
static std::unordered_map<std::string, std::string> s_textureMap;
static std::unordered_map<std::string, std::shared_ptr<HiresTexture>> s_textureCache;
static std::mutex s_textureCacheMutex;
static std::mutex s_textureCacheAquireMutex; // for high priority access
static Common::Flag s_textureCacheAbortLoading;
static bool s_check_native_format;
static bool s_check_new_format;
static std::thread s_prefetcher;
static const std::string s_format_prefix = "tex1_";
void HiresTexture::Init(const std::string& gameCode)
void HiresTexture::Init()
{
s_textureMap.clear();
s_check_native_format = false;
s_check_new_format = false;
Update();
}
void HiresTexture::Shutdown()
{
if (s_prefetcher.joinable())
{
s_textureCacheAbortLoading.Set();
s_prefetcher.join();
}
s_textureMap.clear();
s_textureCache.clear();
}
void HiresTexture::Update()
{
if (s_prefetcher.joinable())
{
s_textureCacheAbortLoading.Set();
s_prefetcher.join();
}
if (!g_ActiveConfig.bHiresTextures)
{
s_textureMap.clear();
s_textureCache.clear();
return;
}
if (!g_ActiveConfig.bCacheHiresTextures)
{
s_textureCache.clear();
}
CFileSearch::XStringVector Directories;
const std::string& gameCode = SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID;
std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str());
Directories.push_back(szDir);
@ -94,6 +140,72 @@ void HiresTexture::Init(const std::string& gameCode)
s_check_new_format = true;
}
}
if (g_ActiveConfig.bCacheHiresTextures)
{
// remove cached but deleted textures
auto iter = s_textureCache.begin();
while (iter != s_textureCache.end())
{
if (s_textureMap.find(iter->first) == s_textureMap.end())
{
iter = s_textureCache.erase(iter);
}
else
{
iter++;
}
}
s_textureCacheAbortLoading.Clear();
s_prefetcher = std::thread(Prefetch);
}
}
void HiresTexture::Prefetch()
{
Common::SetCurrentThreadName("Prefetcher");
size_t size_sum = 0;
u32 starttime = Common::Timer::GetTimeMs();
for (const auto& entry : s_textureMap)
{
const std::string& base_filename = entry.first;
if (base_filename.find("_mip") == std::string::npos)
{
{
// try to get this mutex first, so the video thread is allow to get the real mutex faster
std::unique_lock<std::mutex> lk(s_textureCacheAquireMutex);
}
std::unique_lock<std::mutex> lk(s_textureCacheMutex);
auto iter = s_textureCache.find(base_filename);
if (iter == s_textureCache.end())
{
// unlock while loading a texture. This may result in a race condition where we'll load a texture twice,
// but it reduces the stuttering a lot. Notice: The loading library _must_ be thread safe now.
// But bad luck, SOIL isn't, so TODO: remove SOIL usage here and use libpng directly
// Also TODO: remove s_textureCacheAquireMutex afterwards. It won't be needed as the main mutex will be locked rarely
//lk.unlock();
std::shared_ptr<HiresTexture> ptr(Load(base_filename, 0, 0));
//lk.lock();
iter = s_textureCache.insert(iter, std::make_pair(base_filename, ptr));
}
for (const Level& l : iter->second->m_levels)
{
size_sum += l.data_size;
}
}
if (s_textureCacheAbortLoading.IsSet())
{
return;
}
}
u32 stoptime = Common::Timer::GetTimeMs();
OSD::AddMessage(StringFromFormat("Custom Textures loaded, %.1f MB in %.1f s", size_sum / (1024.0 * 1024.0), (stoptime - starttime) / 1000.0), 10000);
}
std::string HiresTexture::GenBaseName(const u8* texture, size_t texture_size, const u8* tlut, size_t tlut_size, u32 width, u32 height, int format, bool has_mipmaps, bool dump)
@ -238,10 +350,31 @@ std::string HiresTexture::GenBaseName(const u8* texture, size_t texture_size, co
return name;
}
HiresTexture* HiresTexture::Search(const u8* texture, size_t texture_size, const u8* tlut, size_t tlut_size, u32 width, u32 height, int format, bool has_mipmaps)
std::shared_ptr<HiresTexture> HiresTexture::Search(const u8* texture, size_t texture_size, const u8* tlut, size_t tlut_size, u32 width, u32 height, int format, bool has_mipmaps)
{
std::string base_filename = GenBaseName(texture, texture_size, tlut, tlut_size, width, height, format, has_mipmaps);
std::lock_guard<std::mutex> lk2(s_textureCacheAquireMutex);
std::lock_guard<std::mutex> lk(s_textureCacheMutex);
auto iter = s_textureCache.find(base_filename);
if (iter != s_textureCache.end())
{
return iter->second;
}
std::shared_ptr<HiresTexture> ptr(Load(base_filename, width, height));
if (ptr && g_ActiveConfig.bCacheHiresTextures)
{
s_textureCache[base_filename] = ptr;
}
return ptr;
}
HiresTexture* HiresTexture::Load(const std::string& base_filename, u32 width, u32 height)
{
HiresTexture* ret = nullptr;
for (int level = 0;; level++)
{
@ -275,7 +408,7 @@ HiresTexture* HiresTexture::Search(const u8* texture, size_t texture_size, const
if (l.width * height != l.height * width)
ERROR_LOG(VIDEO, "Invalid custom texture size %dx%d for texture %s. The aspect differs from the native size %dx%d.",
l.width, l.height, filename.c_str(), width, height);
if (l.width % width || l.height % height)
if (width && height && (l.width % width || l.height % height))
WARN_LOG(VIDEO, "Invalid custom texture size %dx%d for texture %s. Please use an integer upscaling factor based on the native size %dx%d.",
l.width, l.height, filename.c_str(), width, height);
width = l.width;