2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "VideoCommon/HiresTextures.h"
|
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <algorithm>
|
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>
|
2019-11-22 15:10:41 -07:00
|
|
|
#include <string_view>
|
2015-03-01 15:53:15 -07:00
|
|
|
#include <thread>
|
2015-12-29 05:17:59 -07:00
|
|
|
#include <unordered_map>
|
2009-05-07 01:43:56 -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
|
|
|
|
2019-11-22 15:10:41 -07:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-02-12 20:10:44 -07:00
|
|
|
#include "Common/CommonPaths.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.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"
|
2023-03-20 00:23:51 -06:00
|
|
|
#include "Core/System.h"
|
|
|
|
#include "VideoCommon/Assets/CustomAsset.h"
|
|
|
|
#include "VideoCommon/Assets/CustomAssetLoader.h"
|
|
|
|
#include "VideoCommon/Assets/DirectFilesystemAssetLibrary.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
|
|
|
|
2023-03-20 00:23:51 -06:00
|
|
|
constexpr std::string_view s_format_prefix{"tex1_"};
|
|
|
|
|
|
|
|
static std::unordered_map<std::string, std::shared_ptr<HiresTexture>> s_hires_texture_cache;
|
|
|
|
static std::unordered_map<std::string, bool> s_hires_texture_id_to_arbmipmap;
|
|
|
|
|
|
|
|
static auto s_file_library = std::make_shared<VideoCommon::DirectFilesystemAssetLibrary>();
|
|
|
|
|
|
|
|
namespace
|
2018-01-10 06:56:18 -07:00
|
|
|
{
|
2023-03-20 00:23:51 -06:00
|
|
|
std::pair<std::string, bool> GetNameArbPair(const TextureInfo& texture_info)
|
|
|
|
{
|
|
|
|
if (s_hires_texture_id_to_arbmipmap.empty())
|
|
|
|
return {"", false};
|
2018-01-10 06:56:18 -07:00
|
|
|
|
2023-03-20 00:23:51 -06:00
|
|
|
const auto texture_name_details = texture_info.CalculateTextureName();
|
|
|
|
// look for an exact match first
|
|
|
|
const std::string full_name = texture_name_details.GetFullName();
|
|
|
|
if (auto iter = s_hires_texture_id_to_arbmipmap.find(full_name);
|
|
|
|
iter != s_hires_texture_id_to_arbmipmap.end())
|
|
|
|
{
|
|
|
|
return {full_name, iter->second};
|
|
|
|
}
|
2019-11-22 15:10:41 -07:00
|
|
|
|
2023-03-20 00:23:51 -06:00
|
|
|
// 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 (auto iter = s_hires_texture_id_to_arbmipmap.find(texture_name_single_wildcard_tlut);
|
|
|
|
iter != s_hires_texture_id_to_arbmipmap.end())
|
|
|
|
{
|
|
|
|
return {texture_name_single_wildcard_tlut, iter->second};
|
|
|
|
}
|
2015-01-14 13:53:05 -07:00
|
|
|
|
2023-03-20 00:23:51 -06:00
|
|
|
// 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 (auto iter = s_hires_texture_id_to_arbmipmap.find(texture_name_single_wildcard_tex);
|
|
|
|
iter != s_hires_texture_id_to_arbmipmap.end())
|
|
|
|
{
|
|
|
|
return {texture_name_single_wildcard_tex, iter->second};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {"", false};
|
|
|
|
}
|
|
|
|
} // namespace
|
2015-03-01 15:53:15 -07:00
|
|
|
|
|
|
|
void HiresTexture::Init()
|
2009-05-07 01:43:56 -06:00
|
|
|
{
|
2023-03-20 00:23:51 -06:00
|
|
|
Update();
|
2015-03-01 15:53:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void HiresTexture::Shutdown()
|
|
|
|
{
|
2021-02-02 22:02:41 -07:00
|
|
|
Clear();
|
2015-03-01 15:53:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void HiresTexture::Update()
|
|
|
|
{
|
|
|
|
if (!g_ActiveConfig.bHiresTextures)
|
|
|
|
{
|
2019-08-17 13:40:58 -06:00
|
|
|
Clear();
|
2015-03-01 15:53:15 -07:00
|
|
|
return;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-09 01:47:43 -07:00
|
|
|
const std::string& game_id = SConfig::GetInstance().GetGameID();
|
2020-05-03 11:49:46 -06:00
|
|
|
const std::set<std::string> texture_directories =
|
|
|
|
GetTextureDirectoriesWithGameId(File::GetUserPath(D_HIRESTEXTURES_IDX), game_id);
|
2016-12-27 08:32:32 -07:00
|
|
|
const std::vector<std::string> extensions{".png", ".dds"};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2023-03-20 00:23:51 -06:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
2020-02-12 20:10:44 -07:00
|
|
|
for (const auto& texture_directory : texture_directories)
|
|
|
|
{
|
|
|
|
const auto texture_paths =
|
|
|
|
Common::DoFileSearch({texture_directory}, extensions, /*recursive*/ true);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-02-12 20:10:44 -07:00
|
|
|
bool failed_insert = false;
|
|
|
|
for (auto& path : texture_paths)
|
|
|
|
{
|
|
|
|
std::string filename;
|
|
|
|
SplitPath(path, nullptr, &filename, nullptr);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-02-12 20:10:44 -07: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] =
|
2023-03-20 00:23:51 -06:00
|
|
|
s_hires_texture_id_to_arbmipmap.try_emplace(filename, has_arbitrary_mipmaps);
|
2020-02-12 20:10:44 -07:00
|
|
|
if (!inserted)
|
|
|
|
{
|
|
|
|
failed_insert = true;
|
|
|
|
}
|
2023-08-10 23:36:41 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Since this is just a texture (single file) the mapper doesn't really matter
|
|
|
|
// just provide a string
|
2023-09-05 23:16:26 -06:00
|
|
|
s_file_library->SetAssetIDMapData(filename, std::map<std::string, std::filesystem::path>{
|
|
|
|
{"texture", StringToPath(path)}});
|
2023-08-10 23:36:41 -06:00
|
|
|
|
|
|
|
if (g_ActiveConfig.bCacheHiresTextures)
|
|
|
|
{
|
|
|
|
auto hires_texture = std::make_shared<HiresTexture>(
|
|
|
|
has_arbitrary_mipmaps,
|
|
|
|
system.GetCustomAssetLoader().LoadGameTexture(filename, s_file_library));
|
|
|
|
s_hires_texture_cache.try_emplace(filename, std::move(hires_texture));
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 20:10:44 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-02-12 20:10:44 -07:00
|
|
|
if (failed_insert)
|
2015-01-14 13:53:05 -07:00
|
|
|
{
|
2020-11-13 20:33:26 -07:00
|
|
|
ERROR_LOG_FMT(VIDEO, "One or more textures at path '{}' were already inserted",
|
|
|
|
texture_directory);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2009-05-07 01:43:56 -06:00
|
|
|
}
|
2023-07-21 19:13:27 -06:00
|
|
|
|
|
|
|
if (g_ActiveConfig.bCacheHiresTextures)
|
|
|
|
{
|
|
|
|
OSD::AddMessage(fmt::format("Loading '{}' custom textures", s_hires_texture_cache.size()),
|
|
|
|
10000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OSD::AddMessage(
|
|
|
|
fmt::format("Found '{}' custom textures", s_hires_texture_id_to_arbmipmap.size()), 10000);
|
|
|
|
}
|
2015-03-01 15:53:15 -07:00
|
|
|
}
|
|
|
|
|
2019-08-17 13:40:58 -06:00
|
|
|
void HiresTexture::Clear()
|
|
|
|
{
|
2023-03-20 00:23:51 -06:00
|
|
|
s_hires_texture_cache.clear();
|
|
|
|
s_hires_texture_id_to_arbmipmap.clear();
|
|
|
|
s_file_library = std::make_shared<VideoCommon::DirectFilesystemAssetLibrary>();
|
2014-12-22 14:33:38 -07:00
|
|
|
}
|
|
|
|
|
2022-03-05 13:52:43 -07:00
|
|
|
std::shared_ptr<HiresTexture> HiresTexture::Search(const TextureInfo& texture_info)
|
2014-12-22 14:33:38 -07:00
|
|
|
{
|
2023-03-20 00:23:51 -06:00
|
|
|
const auto [base_filename, has_arb_mipmaps] = GetNameArbPair(texture_info);
|
|
|
|
if (base_filename == "")
|
2017-04-16 08:24:16 -06:00
|
|
|
return nullptr;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2023-03-20 00:23:51 -06:00
|
|
|
if (auto iter = s_hires_texture_cache.find(base_filename); iter != s_hires_texture_cache.end())
|
2017-04-16 08:24:16 -06:00
|
|
|
{
|
2023-03-20 00:23:51 -06:00
|
|
|
return iter->second;
|
2017-04-16 08:24:16 -06:00
|
|
|
}
|
2023-03-20 00:23:51 -06:00
|
|
|
else
|
2017-04-16 08:24:16 -06:00
|
|
|
{
|
2023-03-20 00:23:51 -06:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
auto hires_texture = std::make_shared<HiresTexture>(
|
|
|
|
has_arb_mipmaps,
|
|
|
|
system.GetCustomAssetLoader().LoadGameTexture(base_filename, s_file_library));
|
|
|
|
if (g_ActiveConfig.bCacheHiresTextures)
|
2017-04-21 03:53:03 -06:00
|
|
|
{
|
2023-03-20 00:23:51 -06:00
|
|
|
s_hires_texture_cache.try_emplace(base_filename, hires_texture);
|
2017-04-21 03:53:03 -06:00
|
|
|
}
|
2023-03-20 00:23:51 -06:00
|
|
|
return hires_texture;
|
2017-04-16 03:30:11 -06:00
|
|
|
}
|
2023-03-20 00:23:51 -06:00
|
|
|
}
|
2017-04-16 03:30:11 -06:00
|
|
|
|
2023-03-20 00:23:51 -06:00
|
|
|
HiresTexture::HiresTexture(bool has_arbitrary_mipmaps,
|
|
|
|
std::shared_ptr<VideoCommon::GameTextureAsset> asset)
|
|
|
|
: m_has_arbitrary_mipmaps(has_arbitrary_mipmaps), m_game_texture(std::move(asset))
|
|
|
|
{
|
2009-05-07 01:43:56 -06:00
|
|
|
}
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2020-05-03 11:49:46 -06:00
|
|
|
std::set<std::string> GetTextureDirectoriesWithGameId(const std::string& root_directory,
|
|
|
|
const std::string& game_id)
|
2016-01-06 06:33:36 -07:00
|
|
|
{
|
2020-02-12 20:10:44 -07:00
|
|
|
std::set<std::string> result;
|
2020-05-03 11:49:46 -06:00
|
|
|
const std::string texture_directory = root_directory + game_id;
|
2016-01-06 11:35:16 -07:00
|
|
|
|
2020-02-12 20:10:44 -07: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 11:49:46 -06:00
|
|
|
const std::string region_free_directory = root_directory + game_id.substr(0, 3);
|
2020-02-12 20:10:44 -07:00
|
|
|
|
|
|
|
if (File::Exists(region_free_directory))
|
|
|
|
{
|
|
|
|
result.insert(region_free_directory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:43:36 -06:00
|
|
|
const auto match_gameid_or_all = [game_id](const std::string& filename) {
|
2020-02-12 20:10:44 -07:00
|
|
|
std::string basename;
|
|
|
|
SplitPath(filename, nullptr, &basename, nullptr);
|
2022-03-15 22:43:36 -06:00
|
|
|
return basename == game_id || basename == game_id.substr(0, 3) || basename == "all";
|
2020-02-12 20:10:44 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2022-03-15 22:43:36 -06:00
|
|
|
if (match_gameid_or_all(file))
|
2020-02-12 20:10:44 -07:00
|
|
|
{
|
|
|
|
// The following code is used to calculate the top directory
|
|
|
|
// of a found gameid.txt file
|
2020-05-03 11:49:46 -06:00
|
|
|
// ex: <root directory>/My folder/gameids/<gameid>.txt
|
|
|
|
// would insert "<root directory>/My folder"
|
2020-02-12 20:10:44 -07: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 11:35:16 -07:00
|
|
|
|
2020-02-12 20:10:44 -07:00
|
|
|
return result;
|
2016-01-06 06:33:36 -07:00
|
|
|
}
|