2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
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"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.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"
|
2023-03-20 06:23:51 +00:00
|
|
|
#include "Core/System.h"
|
|
|
|
#include "VideoCommon/Assets/CustomAsset.h"
|
|
|
|
#include "VideoCommon/Assets/CustomAssetLoader.h"
|
|
|
|
#include "VideoCommon/Assets/DirectFilesystemAssetLibrary.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
|
|
|
|
2023-03-20 06:23:51 +00: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 13:56:18 +00:00
|
|
|
{
|
2023-03-20 06:23:51 +00:00
|
|
|
std::pair<std::string, bool> GetNameArbPair(const TextureInfo& texture_info)
|
|
|
|
{
|
|
|
|
if (s_hires_texture_id_to_arbmipmap.empty())
|
|
|
|
return {"", false};
|
2018-01-10 13:56:18 +00:00
|
|
|
|
2023-03-20 06:23:51 +00: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 22:10:41 +00:00
|
|
|
|
2023-03-20 06:23:51 +00: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 20:53:05 +00:00
|
|
|
|
2023-03-20 06:23:51 +00: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 22:53:15 +00:00
|
|
|
|
|
|
|
void HiresTexture::Init()
|
2009-05-07 07:43:56 +00:00
|
|
|
{
|
2023-03-20 06:23:51 +00:00
|
|
|
Update();
|
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 (!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
|
|
|
|
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
|
|
|
|
2023-03-20 06:23:51 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
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] =
|
2023-03-20 06:23:51 +00:00
|
|
|
s_hires_texture_id_to_arbmipmap.try_emplace(filename, has_arbitrary_mipmaps);
|
2020-02-13 03:10:44 +00:00
|
|
|
if (!inserted)
|
|
|
|
{
|
|
|
|
failed_insert = true;
|
|
|
|
}
|
2023-08-11 05:36:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Since this is just a texture (single file) the mapper doesn't really matter
|
|
|
|
// just provide a string
|
|
|
|
s_file_library->SetAssetIDMapData(
|
2023-08-16 07:54:24 +00:00
|
|
|
filename, std::map<std::string, std::filesystem::path>{{"", StringToPath(path)}});
|
2023-08-11 05:36:41 +00: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-13 03:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2023-07-22 01:13:27 +00: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 22:53:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 19:40:58 +00:00
|
|
|
void HiresTexture::Clear()
|
|
|
|
{
|
2023-03-20 06:23:51 +00:00
|
|
|
s_hires_texture_cache.clear();
|
|
|
|
s_hires_texture_id_to_arbmipmap.clear();
|
|
|
|
s_file_library = std::make_shared<VideoCommon::DirectFilesystemAssetLibrary>();
|
2014-12-22 21:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 20:52:43 +00:00
|
|
|
std::shared_ptr<HiresTexture> HiresTexture::Search(const TextureInfo& texture_info)
|
2014-12-22 21:33:38 +00:00
|
|
|
{
|
2023-03-20 06:23:51 +00:00
|
|
|
const auto [base_filename, has_arb_mipmaps] = GetNameArbPair(texture_info);
|
|
|
|
if (base_filename == "")
|
2017-04-16 14:24:16 +00:00
|
|
|
return nullptr;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-03-20 06:23:51 +00:00
|
|
|
if (auto iter = s_hires_texture_cache.find(base_filename); iter != s_hires_texture_cache.end())
|
2017-04-16 14:24:16 +00:00
|
|
|
{
|
2023-03-20 06:23:51 +00:00
|
|
|
return iter->second;
|
2017-04-16 14:24:16 +00:00
|
|
|
}
|
2023-03-20 06:23:51 +00:00
|
|
|
else
|
2017-04-16 14:24:16 +00:00
|
|
|
{
|
2023-03-20 06:23:51 +00: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 09:53:03 +00:00
|
|
|
{
|
2023-03-20 06:23:51 +00:00
|
|
|
s_hires_texture_cache.try_emplace(base_filename, hires_texture);
|
2017-04-21 09:53:03 +00:00
|
|
|
}
|
2023-03-20 06:23:51 +00:00
|
|
|
return hires_texture;
|
2017-04-16 09:30:11 +00:00
|
|
|
}
|
2023-03-20 06:23:51 +00:00
|
|
|
}
|
2017-04-16 09:30:11 +00:00
|
|
|
|
2023-03-20 06:23:51 +00: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 07:43:56 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 04:43:36 +00:00
|
|
|
const auto match_gameid_or_all = [game_id](const std::string& filename) {
|
2020-02-13 03:10:44 +00:00
|
|
|
std::string basename;
|
|
|
|
SplitPath(filename, nullptr, &basename, nullptr);
|
2022-03-16 04:43:36 +00:00
|
|
|
return basename == game_id || basename == game_id.substr(0, 3) || basename == "all";
|
2020-02-13 03:10:44 +00: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-16 04:43:36 +00:00
|
|
|
if (match_gameid_or_all(file))
|
2020-02-13 03:10:44 +00:00
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|