2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "VideoCommon/HiresTextures.h"
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <memory>
|
2015-03-01 22:53:15 +00:00
|
|
|
#include <mutex>
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
2019-11-22 22:10:41 +00:00
|
|
|
#include <string_view>
|
2015-03-01 22:53:15 +00:00
|
|
|
#include <thread>
|
2015-12-29 12:17:59 +00:00
|
|
|
#include <unordered_map>
|
2009-05-07 07:43:56 +00:00
|
|
|
#include <utility>
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <vector>
|
2015-01-21 20:45:14 +00:00
|
|
|
#include <xxhash.h>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2019-11-22 22:10:41 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-02-13 03:10:44 +00:00
|
|
|
#include "Common/CommonPaths.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2015-03-01 22:53:15 +00:00
|
|
|
#include "Common/Flag.h"
|
2020-09-15 10:29:41 +00:00
|
|
|
#include "Common/IOFile.h"
|
2016-12-27 15:32:32 +00:00
|
|
|
#include "Common/Image.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-05-16 13:26:11 +00:00
|
|
|
#include "Common/MemoryUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2015-03-01 22:53:15 +00:00
|
|
|
#include "Common/Thread.h"
|
|
|
|
#include "Common/Timer.h"
|
2017-05-18 12:59:38 +00:00
|
|
|
#include "Core/Config/GraphicsSettings.h"
|
2014-12-22 11:53:03 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2015-01-14 22:19:10 +00:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2014-12-22 11:53:03 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2018-01-10 13:56:18 +00:00
|
|
|
struct DiskTexture
|
|
|
|
{
|
|
|
|
std::string path;
|
|
|
|
bool has_arbitrary_mipmaps;
|
|
|
|
};
|
|
|
|
|
2019-11-22 22:10:41 +00:00
|
|
|
constexpr std::string_view s_format_prefix{"tex1_"};
|
|
|
|
|
2018-01-10 13:56:18 +00:00
|
|
|
static std::unordered_map<std::string, DiskTexture> s_textureMap;
|
2015-03-01 22:53:15 +00:00
|
|
|
static std::unordered_map<std::string, std::shared_ptr<HiresTexture>> s_textureCache;
|
|
|
|
static std::mutex s_textureCacheMutex;
|
|
|
|
static Common::Flag s_textureCacheAbortLoading;
|
2015-01-14 20:53:05 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
static std::thread s_prefetcher;
|
|
|
|
|
|
|
|
void HiresTexture::Init()
|
2009-05-07 07:43:56 +00:00
|
|
|
{
|
2019-11-01 06:30:24 +00:00
|
|
|
// Note: Update is not called here so that we handle dynamic textures on startup more gracefully
|
2015-03-01 22:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HiresTexture::Shutdown()
|
|
|
|
{
|
2021-02-03 05:02:41 +00:00
|
|
|
Clear();
|
2015-03-01 22:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HiresTexture::Update()
|
|
|
|
{
|
|
|
|
if (s_prefetcher.joinable())
|
|
|
|
{
|
|
|
|
s_textureCacheAbortLoading.Set();
|
|
|
|
s_prefetcher.join();
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
if (!g_ActiveConfig.bHiresTextures)
|
|
|
|
{
|
2019-08-17 19:40:58 +00:00
|
|
|
Clear();
|
2015-03-01 22:53:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
if (!g_ActiveConfig.bCacheHiresTextures)
|
|
|
|
{
|
|
|
|
s_textureCache.clear();
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-03-09 08:47:43 +00:00
|
|
|
const std::string& game_id = SConfig::GetInstance().GetGameID();
|
2020-05-03 17:49:46 +00:00
|
|
|
const std::set<std::string> texture_directories =
|
|
|
|
GetTextureDirectoriesWithGameId(File::GetUserPath(D_HIRESTEXTURES_IDX), game_id);
|
2016-12-27 15:32:32 +00:00
|
|
|
const std::vector<std::string> extensions{".png", ".dds"};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-02-13 03:10:44 +00:00
|
|
|
for (const auto& texture_directory : texture_directories)
|
|
|
|
{
|
|
|
|
const auto texture_paths =
|
|
|
|
Common::DoFileSearch({texture_directory}, extensions, /*recursive*/ true);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-02-13 03:10:44 +00:00
|
|
|
bool failed_insert = false;
|
|
|
|
for (auto& path : texture_paths)
|
|
|
|
{
|
|
|
|
std::string filename;
|
|
|
|
SplitPath(path, nullptr, &filename, nullptr);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-02-13 03:10:44 +00:00
|
|
|
if (filename.substr(0, s_format_prefix.length()) == s_format_prefix)
|
|
|
|
{
|
|
|
|
const size_t arb_index = filename.rfind("_arb");
|
|
|
|
const bool has_arbitrary_mipmaps = arb_index != std::string::npos;
|
|
|
|
if (has_arbitrary_mipmaps)
|
|
|
|
filename.erase(arb_index, 4);
|
|
|
|
|
|
|
|
const auto [it, inserted] =
|
|
|
|
s_textureMap.try_emplace(filename, DiskTexture{path, has_arbitrary_mipmaps});
|
|
|
|
if (!inserted)
|
|
|
|
{
|
|
|
|
failed_insert = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-02-13 03:10:44 +00:00
|
|
|
if (failed_insert)
|
2015-01-14 20:53:05 +00:00
|
|
|
{
|
2020-11-14 03:33:26 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "One or more textures at path '{}' were already inserted",
|
|
|
|
texture_directory);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2009-05-07 07:43:56 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
s_textureCacheAbortLoading.Clear();
|
|
|
|
s_prefetcher = std::thread(Prefetch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 19:40:58 +00:00
|
|
|
void HiresTexture::Clear()
|
|
|
|
{
|
2021-02-03 05:02:41 +00:00
|
|
|
if (s_prefetcher.joinable())
|
|
|
|
{
|
|
|
|
s_textureCacheAbortLoading.Set();
|
|
|
|
s_prefetcher.join();
|
|
|
|
}
|
2019-08-17 19:40:58 +00:00
|
|
|
s_textureMap.clear();
|
|
|
|
s_textureCache.clear();
|
|
|
|
}
|
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
void HiresTexture::Prefetch()
|
|
|
|
{
|
|
|
|
Common::SetCurrentThreadName("Prefetcher");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
size_t size_sum = 0;
|
2019-11-22 22:10:41 +00:00
|
|
|
const size_t sys_mem = Common::MemPhysical();
|
|
|
|
const size_t recommended_min_mem = 2 * size_t(1024 * 1024 * 1024);
|
2015-08-18 19:38:14 +00:00
|
|
|
// keep 2GB memory for system stability if system RAM is 4GB+ - use half of memory in other cases
|
2019-11-22 22:10:41 +00:00
|
|
|
const size_t max_mem =
|
2015-08-18 19:38:14 +00:00
|
|
|
(sys_mem / 2 < recommended_min_mem) ? (sys_mem / 2) : (sys_mem - recommended_min_mem);
|
2019-11-22 22:10:41 +00:00
|
|
|
|
|
|
|
const u32 start_time = Common::Timer::GetTimeMs();
|
2015-03-01 22:53:15 +00:00
|
|
|
for (const auto& entry : s_textureMap)
|
|
|
|
{
|
|
|
|
const std::string& base_filename = entry.first;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
if (base_filename.find("_mip") == std::string::npos)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2015-03-01 22:53:15 +00:00
|
|
|
std::unique_lock<std::mutex> lk(s_textureCacheMutex);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
auto iter = s_textureCache.find(base_filename);
|
|
|
|
if (iter == s_textureCache.end())
|
|
|
|
{
|
2016-12-27 17:15:33 +00:00
|
|
|
// 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.
|
|
|
|
lk.unlock();
|
2015-12-29 12:21:51 +00:00
|
|
|
std::unique_ptr<HiresTexture> texture = Load(base_filename, 0, 0);
|
2016-12-27 17:15:33 +00:00
|
|
|
lk.lock();
|
2015-12-29 12:21:51 +00:00
|
|
|
if (texture)
|
2015-05-29 05:31:11 +00:00
|
|
|
{
|
2015-12-29 12:21:51 +00:00
|
|
|
std::shared_ptr<HiresTexture> ptr(std::move(texture));
|
2015-05-29 05:31:11 +00:00
|
|
|
iter = s_textureCache.insert(iter, std::make_pair(base_filename, ptr));
|
|
|
|
}
|
2015-03-01 22:53:15 +00:00
|
|
|
}
|
2015-05-29 05:31:11 +00:00
|
|
|
if (iter != s_textureCache.end())
|
2015-03-01 22:53:15 +00:00
|
|
|
{
|
2015-05-29 05:31:11 +00:00
|
|
|
for (const Level& l : iter->second->m_levels)
|
2016-12-27 15:32:32 +00:00
|
|
|
size_sum += l.data.size();
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2015-05-29 05:31:11 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
if (s_textureCacheAbortLoading.IsSet())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-16 13:26:11 +00:00
|
|
|
if (size_sum > max_mem)
|
|
|
|
{
|
2017-05-18 12:59:38 +00:00
|
|
|
Config::SetCurrent(Config::GFX_HIRES_TEXTURES, false);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-16 13:26:11 +00:00
|
|
|
OSD::AddMessage(
|
2019-11-22 22:10:41 +00:00
|
|
|
fmt::format(
|
|
|
|
"Custom Textures prefetching after {:.1f} MB aborted, not enough RAM available",
|
2015-05-16 13:26:11 +00:00
|
|
|
size_sum / (1024.0 * 1024.0)),
|
|
|
|
10000);
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2015-05-16 13:26:11 +00:00
|
|
|
}
|
2019-11-22 22:10:41 +00:00
|
|
|
|
|
|
|
const u32 stop_time = Common::Timer::GetTimeMs();
|
|
|
|
OSD::AddMessage(fmt::format("Custom Textures loaded, {:.1f} MB in {:.1f}s",
|
|
|
|
size_sum / (1024.0 * 1024.0), (stop_time - start_time) / 1000.0),
|
2015-03-01 22:53:15 +00:00
|
|
|
10000);
|
2009-05-07 07:43:56 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2021-05-01 05:59:24 +00:00
|
|
|
std::string HiresTexture::GenBaseName(TextureInfo& texture_info, bool dump)
|
2012-05-12 12:31:38 +00:00
|
|
|
{
|
2016-11-15 21:25:35 +00:00
|
|
|
if (!dump && s_textureMap.empty())
|
|
|
|
return "";
|
|
|
|
|
2021-05-01 05:59:24 +00:00
|
|
|
const auto texture_name_details = texture_info.CalculateTextureName();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-05-14 23:18:15 +00:00
|
|
|
// look for an exact match first
|
2021-05-01 05:59:24 +00:00
|
|
|
const std::string full_name = texture_name_details.GetFullName();
|
2019-11-22 22:10:41 +00:00
|
|
|
if (dump || s_textureMap.find(full_name) != s_textureMap.end())
|
|
|
|
return full_name;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-05-14 23:18:15 +00:00
|
|
|
// else try and find a wildcard
|
|
|
|
if (!dump)
|
|
|
|
{
|
|
|
|
// Single wildcard ignoring the tlut hash
|
|
|
|
const std::string texture_name_single_wildcard_tlut =
|
|
|
|
fmt::format("{}_{}_$_{}", texture_name_details.base_name, texture_name_details.texture_name,
|
|
|
|
texture_name_details.format_name);
|
|
|
|
if (s_textureMap.find(texture_name_single_wildcard_tlut) != s_textureMap.end())
|
|
|
|
return texture_name_single_wildcard_tlut;
|
|
|
|
|
|
|
|
// Single wildcard ignoring the texture hash
|
|
|
|
const std::string texture_name_single_wildcard_tex =
|
|
|
|
fmt::format("{}_${}_{}", texture_name_details.base_name, texture_name_details.tlut_name,
|
|
|
|
texture_name_details.format_name);
|
|
|
|
if (s_textureMap.find(texture_name_single_wildcard_tex) != s_textureMap.end())
|
|
|
|
return texture_name_single_wildcard_tex;
|
|
|
|
}
|
|
|
|
|
2016-11-15 21:25:35 +00:00
|
|
|
return "";
|
2014-12-22 21:33:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 09:53:03 +00: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;
|
|
|
|
}
|
|
|
|
|
2021-05-01 05:59:24 +00:00
|
|
|
std::shared_ptr<HiresTexture> HiresTexture::Search(TextureInfo& texture_info)
|
2014-12-22 21:33:38 +00:00
|
|
|
{
|
2021-05-01 05:59:24 +00:00
|
|
|
const std::string base_filename = GenBaseName(texture_info);
|
2012-05-12 12:31:38 +00:00
|
|
|
|
2015-03-01 22:53:15 +00:00
|
|
|
std::lock_guard<std::mutex> lk(s_textureCacheMutex);
|
|
|
|
|
|
|
|
auto iter = s_textureCache.find(base_filename);
|
|
|
|
if (iter != s_textureCache.end())
|
|
|
|
{
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
2021-05-01 05:59:24 +00:00
|
|
|
std::shared_ptr<HiresTexture> ptr(
|
|
|
|
Load(base_filename, texture_info.GetRawWidth(), texture_info.GetRawHeight()));
|
2015-03-01 22:53:15 +00:00
|
|
|
|
|
|
|
if (ptr && g_ActiveConfig.bCacheHiresTextures)
|
|
|
|
{
|
|
|
|
s_textureCache[base_filename] = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-12-29 12:21:51 +00:00
|
|
|
std::unique_ptr<HiresTexture> HiresTexture::Load(const std::string& base_filename, u32 width,
|
|
|
|
u32 height)
|
2015-03-01 22:53:15 +00:00
|
|
|
{
|
2017-04-16 14:24:16 +00: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());
|
2018-01-10 13:56:18 +00:00
|
|
|
const DiskTexture& first_mip_file = filename_iter->second;
|
|
|
|
ret->m_has_arbitrary_mipmaps = first_mip_file.has_arbitrary_mipmaps;
|
|
|
|
LoadDDSTexture(ret.get(), first_mip_file.path);
|
2017-04-16 14:24:16 +00:00
|
|
|
|
|
|
|
// 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++)
|
2014-12-22 11:53:03 +00:00
|
|
|
{
|
|
|
|
std::string filename = base_filename;
|
2017-04-16 14:24:16 +00:00
|
|
|
if (mip_level != 0)
|
2019-11-22 22:10:41 +00:00
|
|
|
filename += fmt::format("_mip{}", mip_level);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-16 14:24:16 +00:00
|
|
|
filename_iter = s_textureMap.find(filename);
|
|
|
|
if (filename_iter == s_textureMap.end())
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-16 14:24:16 +00: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;
|
2018-05-22 06:13:54 +00:00
|
|
|
if (!LoadDDSTexture(level, filename_iter->second.path, mip_level))
|
2017-04-16 14:24:16 +00:00
|
|
|
{
|
2014-12-22 11:53:03 +00:00
|
|
|
File::IOFile file;
|
2018-01-10 13:56:18 +00:00
|
|
|
file.Open(filename_iter->second.path, "rb");
|
2014-12-22 11:53:03 +00:00
|
|
|
std::vector<u8> buffer(file.GetSize());
|
|
|
|
file.ReadBytes(buffer.data(), file.GetSize());
|
2016-12-27 15:32:32 +00:00
|
|
|
|
2017-04-16 14:24:16 +00:00
|
|
|
if (!LoadTexture(level, buffer))
|
2014-12-22 11:53:03 +00:00
|
|
|
{
|
2020-11-14 03:33:26 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Custom texture {} failed to load", filename);
|
2014-12-22 11:53:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-04-16 14:24:16 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-16 14:24:16 +00:00
|
|
|
ret->m_levels.push_back(std::move(level));
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-16 14:24:16 +00: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 08:43:46 +00:00
|
|
|
|
2017-04-16 14:24:16 +00: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)
|
|
|
|
{
|
2020-11-14 03:33:26 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO,
|
|
|
|
"Invalid custom texture size {}x{} for texture {}. The aspect differs "
|
|
|
|
"from the native size {}x{}.",
|
|
|
|
first_mip.width, first_mip.height, first_mip_file.path, width, height);
|
2017-04-16 14:24:16 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-16 14:24:16 +00: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))
|
|
|
|
{
|
2020-11-14 03:33:26 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO,
|
|
|
|
"Invalid custom texture size {}x{} for texture {}. Please use an integer "
|
|
|
|
"upscaling factor based on the native size {}x{}.",
|
|
|
|
first_mip.width, first_mip.height, first_mip_file.path, width, height);
|
2017-04-16 14:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that each mip level is the correct size (divide by 2 each time).
|
2017-04-21 09:53:03 +00:00
|
|
|
u32 current_mip_width = first_mip.width;
|
|
|
|
u32 current_mip_height = first_mip.height;
|
2017-04-16 14:24:16 +00:00
|
|
|
for (u32 mip_level = 1; mip_level < static_cast<u32>(ret->m_levels.size()); mip_level++)
|
|
|
|
{
|
2017-04-21 09:53:03 +00:00
|
|
|
if (current_mip_width != 1 || current_mip_height != 1)
|
2009-05-15 07:07:24 +00:00
|
|
|
{
|
2017-04-16 14:24:16 +00:00
|
|
|
current_mip_width = std::max(current_mip_width / 2, 1u);
|
|
|
|
current_mip_height = std::max(current_mip_height / 2, 1u);
|
|
|
|
|
2017-04-21 09:53:03 +00:00
|
|
|
const Level& level = ret->m_levels[mip_level];
|
|
|
|
if (current_mip_width == level.width && current_mip_height == level.height)
|
|
|
|
continue;
|
|
|
|
|
2020-11-14 03:33:26 +00:00
|
|
|
ERROR_LOG_FMT(
|
|
|
|
VIDEO, "Invalid custom texture size {}x{} for texture {}. Mipmap level {} must be {}x{}.",
|
|
|
|
level.width, level.height, first_mip_file.path, mip_level, current_mip_width,
|
|
|
|
current_mip_height);
|
2017-04-21 09:53:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It is invalid to have more than a single 1x1 mipmap.
|
2020-11-14 03:33:26 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Custom texture {} has too many 1x1 mipmaps. Skipping extra levels.",
|
|
|
|
first_mip_file.path);
|
2017-04-21 09:53:03 +00:00
|
|
|
}
|
2017-04-16 14:24:16 +00:00
|
|
|
|
|
|
|
// Drop this mip level and any others after it.
|
|
|
|
while (ret->m_levels.size() > mip_level)
|
|
|
|
ret->m_levels.pop_back();
|
2009-05-15 07:07:24 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-16 09:30:11 +00:00
|
|
|
// All levels have to have the same format.
|
2017-04-21 09:53:03 +00: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 09:30:11 +00:00
|
|
|
{
|
2020-11-14 03:33:26 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Custom texture {} has inconsistent formats across mip levels.",
|
|
|
|
first_mip_file.path);
|
2017-04-16 14:24:16 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2017-04-16 09:30:11 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
return ret;
|
2009-05-07 07:43:56 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2017-04-16 09:30:11 +00:00
|
|
|
bool HiresTexture::LoadTexture(Level& level, const std::vector<u8>& buffer)
|
|
|
|
{
|
2016-12-27 15:32:32 +00:00
|
|
|
if (!Common::LoadPNG(buffer, &level.data, &level.width, &level.height))
|
|
|
|
return false;
|
2017-04-16 09:30:11 +00:00
|
|
|
|
2016-12-27 15:32:32 +00:00
|
|
|
if (level.data.empty())
|
2017-04-16 09:30:11 +00:00
|
|
|
return false;
|
|
|
|
|
2016-12-27 15:32:32 +00:00
|
|
|
// Loaded PNG images are converted to RGBA.
|
2017-06-12 17:37:28 +00:00
|
|
|
level.format = AbstractTextureFormat::RGBA8;
|
2017-04-16 09:30:11 +00:00
|
|
|
level.row_length = level.width;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:49:46 +00:00
|
|
|
std::set<std::string> GetTextureDirectoriesWithGameId(const std::string& root_directory,
|
|
|
|
const std::string& game_id)
|
2016-01-06 13:33:36 +00:00
|
|
|
{
|
2020-02-13 03:10:44 +00:00
|
|
|
std::set<std::string> result;
|
2020-05-03 17:49:46 +00:00
|
|
|
const std::string texture_directory = root_directory + game_id;
|
2016-01-06 18:35:16 +00:00
|
|
|
|
2020-02-13 03:10:44 +00:00
|
|
|
if (File::Exists(texture_directory))
|
|
|
|
{
|
|
|
|
result.insert(texture_directory);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If there's no directory with the region-specific ID, look for a 3-character region-free one
|
2020-05-03 17:49:46 +00:00
|
|
|
const std::string region_free_directory = root_directory + game_id.substr(0, 3);
|
2020-02-13 03:10:44 +00:00
|
|
|
|
|
|
|
if (File::Exists(region_free_directory))
|
|
|
|
{
|
|
|
|
result.insert(region_free_directory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto match_gameid = [game_id](const std::string& filename) {
|
|
|
|
std::string basename;
|
|
|
|
SplitPath(filename, nullptr, &basename, nullptr);
|
|
|
|
return basename == game_id || basename == game_id.substr(0, 3);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Look for any other directories that might be specific to the given gameid
|
|
|
|
const auto files = Common::DoFileSearch({root_directory}, {".txt"}, true);
|
|
|
|
for (const auto& file : files)
|
|
|
|
{
|
|
|
|
if (match_gameid(file))
|
|
|
|
{
|
|
|
|
// The following code is used to calculate the top directory
|
|
|
|
// of a found gameid.txt file
|
2020-05-03 17:49:46 +00:00
|
|
|
// ex: <root directory>/My folder/gameids/<gameid>.txt
|
|
|
|
// would insert "<root directory>/My folder"
|
2020-02-13 03:10:44 +00:00
|
|
|
const auto directory_path = file.substr(root_directory.size());
|
|
|
|
const std::size_t first_path_separator_position = directory_path.find_first_of(DIR_SEP_CHR);
|
|
|
|
result.insert(root_directory + directory_path.substr(0, first_path_separator_position));
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 18:35:16 +00:00
|
|
|
|
2020-02-13 03:10:44 +00:00
|
|
|
return result;
|
2016-01-06 13:33:36 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
HiresTexture::~HiresTexture()
|
|
|
|
{
|
2009-05-07 07:43:56 +00:00
|
|
|
}
|
2017-04-16 09:30:11 +00:00
|
|
|
|
2017-06-12 17:37:28 +00:00
|
|
|
AbstractTextureFormat HiresTexture::GetFormat() const
|
2017-04-16 09:30:11 +00:00
|
|
|
{
|
|
|
|
return m_levels.at(0).format;
|
|
|
|
}
|
2018-01-10 13:56:18 +00:00
|
|
|
|
|
|
|
bool HiresTexture::HasArbitraryMipmaps() const
|
|
|
|
{
|
|
|
|
return m_has_arbitrary_mipmaps;
|
|
|
|
}
|