2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "VideoCommon/HiresTextures.h"
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
#include <SOIL/SOIL.h>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <algorithm>
|
2015-01-21 15:06:18 -07:00
|
|
|
#include <cinttypes>
|
2009-07-05 20:10:26 -06:00
|
|
|
#include <cstring>
|
2016-01-17 14:54:31 -07:00
|
|
|
#include <memory>
|
2015-03-01 15:53:15 -07:00
|
|
|
#include <mutex>
|
2014-03-12 13:33:41 -06:00
|
|
|
#include <string>
|
2015-03-01 15:53:15 -07:00
|
|
|
#include <thread>
|
2015-12-29 05:17:59 -07:00
|
|
|
#include <unordered_map>
|
2009-07-05 20:10:26 -06:00
|
|
|
#include <utility>
|
2016-01-17 14:54:31 -07:00
|
|
|
#include <vector>
|
2015-01-21 13:45:14 -07:00
|
|
|
#include <xxhash.h>
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2017-01-15 13:46:32 -07:00
|
|
|
#include "Common/File.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2015-03-01 15:53:15 -07:00
|
|
|
#include "Common/Flag.h"
|
2015-12-29 05:17:59 -07:00
|
|
|
#include "Common/Hash.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-05-16 07:26:11 -06:00
|
|
|
#include "Common/MemoryUtil.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.h"
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "Common/Swap.h"
|
2015-03-01 15:53:15 -07:00
|
|
|
#include "Common/Thread.h"
|
|
|
|
#include "Common/Timer.h"
|
2017-05-18 06:59:38 -06:00
|
|
|
#include "Core/Config/GraphicsSettings.h"
|
2014-12-22 04:53:03 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
2015-01-14 15:19:10 -07:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2014-12-22 04:53:03 -07:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2015-01-14 13:53:05 -07:00
|
|
|
static std::unordered_map<std::string, std::string> s_textureMap;
|
2015-03-01 15:53:15 -07:00
|
|
|
static std::unordered_map<std::string, std::shared_ptr<HiresTexture>> s_textureCache;
|
|
|
|
static std::mutex s_textureCacheMutex;
|
2016-06-24 02:43:46 -06:00
|
|
|
static std::mutex s_textureCacheAquireMutex; // for high priority access
|
2015-03-01 15:53:15 -07:00
|
|
|
static Common::Flag s_textureCacheAbortLoading;
|
2015-01-14 13:53:05 -07:00
|
|
|
static bool s_check_native_format;
|
|
|
|
static bool s_check_new_format;
|
|
|
|
|
2015-03-01 15:53:15 -07:00
|
|
|
static std::thread s_prefetcher;
|
|
|
|
|
2015-01-14 13:53:05 -07:00
|
|
|
static const std::string s_format_prefix = "tex1_";
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
HiresTexture::Level::Level() : data(nullptr, SOIL_free_image_data)
|
2015-12-29 05:32:39 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-01 15:53:15 -07:00
|
|
|
void HiresTexture::Init()
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
s_check_native_format = false;
|
|
|
|
s_check_new_format = false;
|
2011-02-05 03:08:06 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
Update();
|
2015-03-01 15:53:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void HiresTexture::Shutdown()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
if (s_prefetcher.joinable())
|
|
|
|
{
|
|
|
|
s_textureCacheAbortLoading.Set();
|
|
|
|
s_prefetcher.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
s_textureMap.clear();
|
|
|
|
s_textureCache.clear();
|
2015-03-01 15:53:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void HiresTexture::Update()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-03-09 01:47:43 -07:00
|
|
|
const std::string& game_id = SConfig::GetInstance().GetGameID();
|
2016-06-24 02:43:46 -06:00
|
|
|
const std::string texture_directory = GetTextureDirectory(game_id);
|
|
|
|
std::vector<std::string> extensions{
|
|
|
|
".png", ".bmp", ".tga", ".dds",
|
|
|
|
".jpg" // Why not? Could be useful for large photo-like textures
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> filenames =
|
2017-06-20 22:24:26 -06:00
|
|
|
Common::DoFileSearch({texture_directory}, extensions, /*recursive*/ true);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
const std::string code = game_id + "_";
|
|
|
|
|
|
|
|
for (auto& rFilename : filenames)
|
|
|
|
{
|
|
|
|
std::string FileName;
|
|
|
|
SplitPath(rFilename, nullptr, &FileName, nullptr);
|
|
|
|
|
|
|
|
if (FileName.substr(0, code.length()) == code)
|
|
|
|
{
|
|
|
|
s_textureMap[FileName] = rFilename;
|
|
|
|
s_check_native_format = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileName.substr(0, s_format_prefix.length()) == s_format_prefix)
|
|
|
|
{
|
|
|
|
s_textureMap[FileName] = rFilename;
|
|
|
|
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);
|
|
|
|
}
|
2015-03-01 15:53:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void HiresTexture::Prefetch()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
Common::SetCurrentThreadName("Prefetcher");
|
|
|
|
|
|
|
|
size_t size_sum = 0;
|
2016-08-07 11:03:07 -06:00
|
|
|
size_t sys_mem = Common::MemPhysical();
|
2016-06-24 02:43:46 -06:00
|
|
|
size_t recommended_min_mem = 2 * size_t(1024 * 1024 * 1024);
|
|
|
|
// keep 2GB memory for system stability if system RAM is 4GB+ - use half of memory in other cases
|
|
|
|
size_t max_mem =
|
|
|
|
(sys_mem / 2 < recommended_min_mem) ? (sys_mem / 2) : (sys_mem - recommended_min_mem);
|
|
|
|
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::unique_ptr<HiresTexture> texture = Load(base_filename, 0, 0);
|
|
|
|
// lk.lock();
|
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
std::shared_ptr<HiresTexture> ptr(std::move(texture));
|
|
|
|
iter = s_textureCache.insert(iter, std::make_pair(base_filename, ptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (iter != s_textureCache.end())
|
|
|
|
{
|
|
|
|
for (const Level& l : iter->second->m_levels)
|
|
|
|
{
|
|
|
|
size_sum += l.data_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_textureCacheAbortLoading.IsSet())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size_sum > max_mem)
|
|
|
|
{
|
2017-05-18 06:59:38 -06:00
|
|
|
Config::SetCurrent(Config::GFX_HIRES_TEXTURES, false);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
OSD::AddMessage(
|
|
|
|
StringFromFormat(
|
|
|
|
"Custom Textures prefetching after %.1f MB aborted, not enough RAM available",
|
|
|
|
size_sum / (1024.0 * 1024.0)),
|
|
|
|
10000);
|
|
|
|
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);
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
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)
|
2012-05-12 06:31:38 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
std::string name = "";
|
|
|
|
bool convert = false;
|
|
|
|
if (!dump && s_check_native_format)
|
|
|
|
{
|
|
|
|
// try to load the old format first
|
|
|
|
u64 tex_hash = GetHashHiresTexture(texture, (int)texture_size,
|
|
|
|
g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
|
|
|
u64 tlut_hash = tlut_size ? GetHashHiresTexture(tlut, (int)tlut_size,
|
|
|
|
g_ActiveConfig.iSafeTextureCache_ColorSamples) :
|
|
|
|
0;
|
2017-03-09 01:47:43 -07:00
|
|
|
name = StringFromFormat("%s_%08x_%i", SConfig::GetInstance().GetGameID().c_str(),
|
2016-06-24 02:43:46 -06:00
|
|
|
(u32)(tex_hash ^ tlut_hash), (u16)format);
|
|
|
|
if (s_textureMap.find(name) != s_textureMap.end())
|
|
|
|
{
|
|
|
|
if (g_ActiveConfig.bConvertHiresTextures)
|
|
|
|
convert = true;
|
|
|
|
else
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dump || s_check_new_format || convert)
|
|
|
|
{
|
|
|
|
// checking for min/max on paletted textures
|
|
|
|
u32 min = 0xffff;
|
|
|
|
u32 max = 0;
|
|
|
|
switch (tlut_size)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 16 * 2:
|
|
|
|
for (size_t i = 0; i < texture_size; i++)
|
|
|
|
{
|
|
|
|
min = std::min<u32>(min, texture[i] & 0xf);
|
|
|
|
min = std::min<u32>(min, texture[i] >> 4);
|
|
|
|
max = std::max<u32>(max, texture[i] & 0xf);
|
|
|
|
max = std::max<u32>(max, texture[i] >> 4);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 256 * 2:
|
|
|
|
for (size_t i = 0; i < texture_size; i++)
|
|
|
|
{
|
|
|
|
min = std::min<u32>(min, texture[i]);
|
|
|
|
max = std::max<u32>(max, texture[i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16384 * 2:
|
|
|
|
for (size_t i = 0; i < texture_size / 2; i++)
|
|
|
|
{
|
|
|
|
min = std::min<u32>(min, Common::swap16(((u16*)texture)[i]) & 0x3fff);
|
|
|
|
max = std::max<u32>(max, Common::swap16(((u16*)texture)[i]) & 0x3fff);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tlut_size > 0)
|
|
|
|
{
|
|
|
|
tlut_size = 2 * (max + 1 - min);
|
|
|
|
tlut += 2 * min;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 tex_hash = XXH64(texture, texture_size, 0);
|
|
|
|
u64 tlut_hash = tlut_size ? XXH64(tlut, tlut_size, 0) : 0;
|
|
|
|
|
|
|
|
std::string basename = s_format_prefix + StringFromFormat("%dx%d%s_%016" PRIx64, width, height,
|
|
|
|
has_mipmaps ? "_m" : "", tex_hash);
|
|
|
|
std::string tlutname = tlut_size ? StringFromFormat("_%016" PRIx64, tlut_hash) : "";
|
|
|
|
std::string formatname = StringFromFormat("_%d", format);
|
|
|
|
std::string fullname = basename + tlutname + formatname;
|
|
|
|
|
|
|
|
for (int level = 0; level < 10 && convert; level++)
|
|
|
|
{
|
|
|
|
std::string oldname = name;
|
|
|
|
if (level)
|
|
|
|
oldname += StringFromFormat("_mip%d", level);
|
|
|
|
|
|
|
|
// skip not existing levels
|
|
|
|
if (s_textureMap.find(oldname) == s_textureMap.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int i = 0;; i++)
|
|
|
|
{
|
|
|
|
// for hash collisions, padd with an integer
|
|
|
|
std::string newname = fullname;
|
|
|
|
if (level)
|
|
|
|
newname += StringFromFormat("_mip%d", level);
|
|
|
|
if (i)
|
|
|
|
newname += StringFromFormat(".%d", i);
|
|
|
|
|
|
|
|
// new texture
|
|
|
|
if (s_textureMap.find(newname) == s_textureMap.end())
|
|
|
|
{
|
|
|
|
std::string src = s_textureMap[oldname];
|
|
|
|
size_t postfix = src.find_last_of('.');
|
|
|
|
std::string dst = src.substr(0, postfix - oldname.length()) + newname +
|
|
|
|
src.substr(postfix, src.length() - postfix);
|
|
|
|
if (File::Rename(src, dst))
|
|
|
|
{
|
|
|
|
s_textureMap.erase(oldname);
|
|
|
|
s_textureMap[newname] = dst;
|
|
|
|
s_check_new_format = true;
|
|
|
|
OSD::AddMessage(StringFromFormat("Rename custom texture %s to %s", oldname.c_str(),
|
|
|
|
newname.c_str()),
|
|
|
|
5000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "rename failed");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// dst fail already exist, compare content
|
|
|
|
std::string a, b;
|
|
|
|
File::ReadFileToString(s_textureMap[oldname], a);
|
|
|
|
File::ReadFileToString(s_textureMap[newname], b);
|
|
|
|
|
|
|
|
if (a == b && a != "")
|
|
|
|
{
|
|
|
|
// equal, so remove
|
|
|
|
if (File::Delete(s_textureMap[oldname]))
|
|
|
|
{
|
|
|
|
s_textureMap.erase(oldname);
|
|
|
|
OSD::AddMessage(
|
|
|
|
StringFromFormat("Delete double old custom texture %s", oldname.c_str()), 5000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "delete failed");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else continue in this loop with the next higher padding variable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to match a wildcard template
|
|
|
|
if (!dump && s_textureMap.find(basename + "_*" + formatname) != s_textureMap.end())
|
|
|
|
return basename + "_*" + formatname;
|
|
|
|
|
|
|
|
// else generate the complete texture
|
|
|
|
if (dump || s_textureMap.find(fullname) != s_textureMap.end())
|
|
|
|
return fullname;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
2014-12-22 14:33:38 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 03:53:03 -06:00
|
|
|
u32 HiresTexture::CalculateMipCount(u32 width, u32 height)
|
|
|
|
{
|
|
|
|
u32 mip_width = width;
|
|
|
|
u32 mip_height = height;
|
|
|
|
u32 mip_count = 1;
|
|
|
|
while (mip_width > 1 || mip_height > 1)
|
|
|
|
{
|
|
|
|
mip_width = std::max(mip_width / 2, 1u);
|
|
|
|
mip_height = std::max(mip_height / 2, 1u);
|
|
|
|
mip_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mip_count;
|
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
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)
|
2014-12-22 14:33:38 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
std::string base_filename =
|
|
|
|
GenBaseName(texture, texture_size, tlut, tlut_size, width, height, format, has_mipmaps);
|
2012-05-12 06:31:38 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
std::lock_guard<std::mutex> lk2(s_textureCacheAquireMutex);
|
|
|
|
std::lock_guard<std::mutex> lk(s_textureCacheMutex);
|
2015-03-01 15:53:15 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
auto iter = s_textureCache.find(base_filename);
|
|
|
|
if (iter != s_textureCache.end())
|
|
|
|
{
|
|
|
|
return iter->second;
|
|
|
|
}
|
2015-03-01 15:53:15 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
std::shared_ptr<HiresTexture> ptr(Load(base_filename, width, height));
|
2015-03-01 15:53:15 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
if (ptr && g_ActiveConfig.bCacheHiresTextures)
|
|
|
|
{
|
|
|
|
s_textureCache[base_filename] = ptr;
|
|
|
|
}
|
2015-03-01 15:53:15 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return ptr;
|
2015-03-01 15:53:15 -07:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
std::unique_ptr<HiresTexture> HiresTexture::Load(const std::string& base_filename, u32 width,
|
|
|
|
u32 height)
|
2015-03-01 15:53:15 -07:00
|
|
|
{
|
2017-04-16 08:24:16 -06:00
|
|
|
// We need to have a level 0 custom texture to even consider loading.
|
|
|
|
auto filename_iter = s_textureMap.find(base_filename);
|
|
|
|
if (filename_iter == s_textureMap.end())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Try to load level 0 (and any mipmaps) from a DDS file.
|
|
|
|
// If this fails, it's fine, we'll just load level0 again using SOIL.
|
|
|
|
// Can't use make_unique due to private constructor.
|
|
|
|
std::unique_ptr<HiresTexture> ret = std::unique_ptr<HiresTexture>(new HiresTexture());
|
|
|
|
const std::string& first_mip_filename = filename_iter->second;
|
|
|
|
LoadDDSTexture(ret.get(), first_mip_filename);
|
|
|
|
|
|
|
|
// Load remaining mip levels, or from the start if it's not a DDS texture.
|
|
|
|
for (u32 mip_level = static_cast<u32>(ret->m_levels.size());; mip_level++)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
std::string filename = base_filename;
|
2017-04-16 08:24:16 -06:00
|
|
|
if (mip_level != 0)
|
|
|
|
filename += StringFromFormat("_mip%u", mip_level);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-04-16 08:24:16 -06:00
|
|
|
filename_iter = s_textureMap.find(filename);
|
|
|
|
if (filename_iter == s_textureMap.end())
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-04-16 08:24:16 -06:00
|
|
|
// Try loading DDS textures first, that way we maintain compression of DXT formats.
|
|
|
|
// TODO: Reduce the number of open() calls here. We could use one fd.
|
|
|
|
Level level;
|
|
|
|
if (!LoadDDSTexture(level, filename_iter->second))
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
File::IOFile file;
|
2017-04-16 03:30:11 -06:00
|
|
|
file.Open(filename_iter->second, "rb");
|
2016-06-24 02:43:46 -06:00
|
|
|
std::vector<u8> buffer(file.GetSize());
|
|
|
|
file.ReadBytes(buffer.data(), file.GetSize());
|
2017-04-16 08:24:16 -06:00
|
|
|
if (!LoadTexture(level, buffer))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Custom texture %s failed to load", filename.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2017-04-16 08:24:16 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-04-16 08:24:16 -06:00
|
|
|
ret->m_levels.push_back(std::move(level));
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-04-16 08:24:16 -06:00
|
|
|
// If we failed to load any mip levels, we can't use this texture at all.
|
|
|
|
if (ret->m_levels.empty())
|
|
|
|
return nullptr;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-04-16 08:24:16 -06:00
|
|
|
// Verify that the aspect ratio of the texture hasn't changed, as this could have side-effects.
|
|
|
|
const Level& first_mip = ret->m_levels[0];
|
|
|
|
if (first_mip.width * height != first_mip.height * width)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Invalid custom texture size %ux%u for texture %s. The aspect differs "
|
|
|
|
"from the native size %ux%u.",
|
|
|
|
first_mip.width, first_mip.height, first_mip_filename.c_str(), width, height);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-04-16 08:24:16 -06:00
|
|
|
// Same deal if the custom texture isn't a multiple of the native size.
|
|
|
|
if (width != 0 && height != 0 && (first_mip.width % width || first_mip.height % height))
|
|
|
|
{
|
2017-04-21 03:53:03 -06:00
|
|
|
ERROR_LOG(VIDEO, "Invalid custom texture size %ux%u for texture %s. Please use an integer "
|
|
|
|
"upscaling factor based on the native size %ux%u.",
|
|
|
|
first_mip.width, first_mip.height, first_mip_filename.c_str(), width, height);
|
2017-04-16 08:24:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that each mip level is the correct size (divide by 2 each time).
|
2017-04-21 03:53:03 -06:00
|
|
|
u32 current_mip_width = first_mip.width;
|
|
|
|
u32 current_mip_height = first_mip.height;
|
2017-04-16 08:24:16 -06:00
|
|
|
for (u32 mip_level = 1; mip_level < static_cast<u32>(ret->m_levels.size()); mip_level++)
|
|
|
|
{
|
2017-04-21 03:53:03 -06:00
|
|
|
if (current_mip_width != 1 || current_mip_height != 1)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-04-16 08:24:16 -06:00
|
|
|
current_mip_width = std::max(current_mip_width / 2, 1u);
|
|
|
|
current_mip_height = std::max(current_mip_height / 2, 1u);
|
|
|
|
|
2017-04-21 03:53:03 -06:00
|
|
|
const Level& level = ret->m_levels[mip_level];
|
|
|
|
if (current_mip_width == level.width && current_mip_height == level.height)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ERROR_LOG(VIDEO,
|
|
|
|
"Invalid custom texture size %dx%d for texture %s. Mipmap level %u must be %dx%d.",
|
|
|
|
level.width, level.height, first_mip_filename.c_str(), mip_level, current_mip_width,
|
|
|
|
current_mip_height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It is invalid to have more than a single 1x1 mipmap.
|
|
|
|
ERROR_LOG(VIDEO, "Custom texture %s has too many 1x1 mipmaps. Skipping extra levels.",
|
|
|
|
first_mip_filename.c_str());
|
|
|
|
}
|
2017-04-16 08:24:16 -06:00
|
|
|
|
|
|
|
// Drop this mip level and any others after it.
|
|
|
|
while (ret->m_levels.size() > mip_level)
|
|
|
|
ret->m_levels.pop_back();
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2017-04-16 03:30:11 -06:00
|
|
|
// All levels have to have the same format.
|
2017-04-21 03:53:03 -06:00
|
|
|
if (std::any_of(ret->m_levels.begin(), ret->m_levels.end(),
|
|
|
|
[&ret](const Level& l) { return l.format != ret->m_levels[0].format; }))
|
2017-04-16 03:30:11 -06:00
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Custom texture %s has inconsistent formats across mip levels.",
|
2017-04-16 08:24:16 -06:00
|
|
|
first_mip_filename.c_str());
|
|
|
|
|
|
|
|
return nullptr;
|
2017-04-16 03:30:11 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return ret;
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
|
2017-04-16 03:30:11 -06:00
|
|
|
bool HiresTexture::LoadTexture(Level& level, const std::vector<u8>& buffer)
|
|
|
|
{
|
|
|
|
int channels;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
|
|
|
|
u8* data = SOIL_load_image_from_memory(buffer.data(), static_cast<int>(buffer.size()), &width,
|
|
|
|
&height, &channels, SOIL_LOAD_RGBA);
|
|
|
|
if (!data)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Images loaded by SOIL are converted to RGBA.
|
|
|
|
level.width = static_cast<u32>(width);
|
|
|
|
level.height = static_cast<u32>(height);
|
2017-06-12 11:37:28 -06:00
|
|
|
level.format = AbstractTextureFormat::RGBA8;
|
2017-04-16 03:30:11 -06:00
|
|
|
level.data = ImageDataPointer(data, SOIL_free_image_data);
|
|
|
|
level.row_length = level.width;
|
|
|
|
level.data_size = static_cast<size_t>(level.row_length) * 4 * level.height;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-06 11:35:16 -07:00
|
|
|
std::string HiresTexture::GetTextureDirectory(const std::string& game_id)
|
2016-01-06 06:33:36 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
const std::string texture_directory = File::GetUserPath(D_HIRESTEXTURES_IDX) + game_id;
|
2016-01-06 11:35:16 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// If there's no directory with the region-specific ID, look for a 3-character region-free one
|
|
|
|
if (!File::Exists(texture_directory))
|
|
|
|
return File::GetUserPath(D_HIRESTEXTURES_IDX) + game_id.substr(0, 3);
|
2016-01-06 11:35:16 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return texture_directory;
|
2016-01-06 06:33:36 -07:00
|
|
|
}
|
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
HiresTexture::~HiresTexture()
|
|
|
|
{
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
2017-04-16 03:30:11 -06:00
|
|
|
|
2017-06-12 11:37:28 -06:00
|
|
|
AbstractTextureFormat HiresTexture::GetFormat() const
|
2017-04-16 03:30:11 -06:00
|
|
|
{
|
|
|
|
return m_levels.at(0).format;
|
|
|
|
}
|