| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/HiresTextures.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <string> |
| 8 | #include <string_view> |
| 9 | #include <unordered_map> |
| 10 | #include <utility> |
| 11 | #include <xxhash.h> |
| 12 | |
| 13 | #include <fmt/format.h> |
| 14 | |
| 15 | #include "Common/CommonPaths.h" |
| 16 | #include "Common/FileSearch.h" |
| 17 | #include "Common/FileUtil.h" |
| 18 | #include "Common/Logging/Log.h" |
| 19 | #include "Common/StringUtil.h" |
| 20 | #include "Core/ConfigManager.h" |
| 21 | #include "Core/System.h" |
| 22 | #include "VideoCommon/Assets/DirectFilesystemAssetLibrary.h" |
| 23 | #include "VideoCommon/OnScreenDisplay.h" |
| 24 | #include "VideoCommon/Resources/CustomResourceManager.h" |
| 25 | #include "VideoCommon/VideoConfig.h" |
| 26 | |
| 27 | constexpr std::string_view s_format_prefix{"tex1_"}; |
| 28 | |
| 29 | static std::unordered_map<std::string, std::shared_ptr<HiresTexture>> s_hires_texture_cache; |
| 30 | static std::unordered_map<std::string, bool> s_hires_texture_id_to_arbmipmap; |
| 31 | |
| 32 | static auto s_file_library = std::make_shared<VideoCommon::DirectFilesystemAssetLibrary>(); |
| 33 | |
| 34 | namespace |
| 35 | { |
| 36 | std::pair<std::string, bool> GetNameArbPair(const TextureInfo& texture_info) |
| 37 | { |
| 38 | if (s_hires_texture_id_to_arbmipmap.empty()) |
| 39 | return {"", false}; |
| 40 | |
| 41 | const auto texture_name_details = texture_info.CalculateTextureName(); |
| 42 | // look for an exact match first |
| 43 | const std::string full_name = texture_name_details.GetFullName(); |
| 44 | if (auto iter = s_hires_texture_id_to_arbmipmap.find(full_name); |
| 45 | iter != s_hires_texture_id_to_arbmipmap.end()) |
| 46 | { |
| 47 | return {full_name, iter->second}; |
| 48 | } |
| 49 | |
| 50 | // Single wildcard ignoring the tlut hash |
| 51 | const std::string texture_name_single_wildcard_tlut = |
| 52 | fmt::format("{}_{}_$_{}", texture_name_details.base_name, texture_name_details.texture_name, |
| 53 | texture_name_details.format_name); |
| 54 | if (auto iter = s_hires_texture_id_to_arbmipmap.find(texture_name_single_wildcard_tlut); |
| 55 | iter != s_hires_texture_id_to_arbmipmap.end()) |
| 56 | { |
| 57 | return {texture_name_single_wildcard_tlut, iter->second}; |
| 58 | } |
| 59 | |
| 60 | // Single wildcard ignoring the texture hash |
| 61 | const std::string texture_name_single_wildcard_tex = |
| 62 | fmt::format("{}_${}_{}", texture_name_details.base_name, texture_name_details.tlut_name, |
| 63 | texture_name_details.format_name); |
| 64 | if (auto iter = s_hires_texture_id_to_arbmipmap.find(texture_name_single_wildcard_tex); |
| 65 | iter != s_hires_texture_id_to_arbmipmap.end()) |
| 66 | { |
| 67 | return {texture_name_single_wildcard_tex, iter->second}; |
| 68 | } |
| 69 | |
| 70 | return {"", false}; |
| 71 | } |
| 72 | } // namespace |
| 73 | |
| 74 | void HiresTexture::Shutdown() |
| 75 | { |
| 76 | Clear(); |
| 77 | } |
| 78 | |
| 79 | void HiresTexture::Update() |
| 80 | { |
| 81 | if (!g_ActiveConfig.bHiresTextures) |
| 82 | { |
| 83 | Clear(); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | const std::string& game_id = SConfig::GetInstance().GetGameID(); |
| 88 | const std::set<std::string> texture_directories = |
| 89 | GetTextureDirectoriesWithGameId(File::GetUserPath(D_HIRESTEXTURES_IDX), game_id); |
| 90 | constexpr auto extensions = std::to_array<std::string_view>({".png", ".dds"}); |
| 91 | |
| 92 | for (const auto& texture_directory : texture_directories) |
| 93 | { |
| 94 | // Watch this directory for any texture reloads |
| 95 | s_file_library->Watch(texture_directory); |
| 96 | |
| 97 | const auto texture_paths = |
| 98 | Common::DoFileSearch(texture_directory, extensions, /*recursive*/ true); |
| 99 | |
| 100 | bool failed_insert = false; |
| 101 | for (auto& path : texture_paths) |
| 102 | { |
| 103 | std::string filename; |
| 104 | SplitPath(path, nullptr, &filename, nullptr); |
| 105 | |
| 106 | if (filename.substr(0, s_format_prefix.length()) == s_format_prefix) |
| 107 | { |
| 108 | const size_t arb_index = filename.rfind("_arb"); |
| 109 | const bool has_arbitrary_mipmaps = arb_index != std::string::npos; |
| 110 | if (has_arbitrary_mipmaps) |
| 111 | filename.erase(arb_index, 4); |
| 112 | |
| 113 | const auto [it, inserted] = |
| 114 | s_hires_texture_id_to_arbmipmap.try_emplace(filename, has_arbitrary_mipmaps); |
| 115 | if (!inserted) |
| 116 | { |
| 117 | failed_insert = true; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | // Since this is just a texture (single file) the mapper doesn't really matter |
| 122 | // just provide a string |
| 123 | s_file_library->SetAssetIDMapData(filename, std::map<std::string, std::filesystem::path>{ |
| 124 | {"texture", StringToPath(path)}}); |
| 125 | |
| 126 | if (g_ActiveConfig.bCacheHiresTextures) |
| 127 | { |
| 128 | auto hires_texture = |
| 129 | std::make_shared<HiresTexture>(has_arbitrary_mipmaps, std::move(filename)); |
| 130 | static_cast<void>(hires_texture->LoadTexture()); |
| 131 | s_hires_texture_cache.try_emplace(hires_texture->GetId(), hires_texture); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (failed_insert) |
| 138 | { |
| 139 | ERROR_LOG_FMT(VIDEO, "One or more textures at path '{}' were already inserted", |
| 140 | texture_directory); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (g_ActiveConfig.bCacheHiresTextures) |
| 145 | { |
| 146 | OSD::AddMessage(fmt::format("Loading '{}' custom textures", s_hires_texture_cache.size()), |
| 147 | 10000); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | OSD::AddMessage( |
| 152 | fmt::format("Found '{}' custom textures", s_hires_texture_id_to_arbmipmap.size()), 10000); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void HiresTexture::Clear() |
| 157 | { |
| 158 | s_hires_texture_cache.clear(); |
| 159 | s_hires_texture_id_to_arbmipmap.clear(); |
| 160 | s_file_library = std::make_shared<VideoCommon::DirectFilesystemAssetLibrary>(); |
| 161 | } |
| 162 | |
| 163 | std::shared_ptr<HiresTexture> HiresTexture::Search(const TextureInfo& texture_info) |
| 164 | { |
| 165 | auto [base_filename, has_arb_mipmaps] = GetNameArbPair(texture_info); |
| 166 | if (base_filename == "") |
| 167 | return nullptr; |
| 168 | |
| 169 | if (auto iter = s_hires_texture_cache.find(base_filename); iter != s_hires_texture_cache.end()) |
| 170 | { |
| 171 | return iter->second; |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | auto hires_texture = std::make_shared<HiresTexture>(has_arb_mipmaps, std::move(base_filename)); |
| 176 | if (g_ActiveConfig.bCacheHiresTextures) |
| 177 | { |
| 178 | s_hires_texture_cache.try_emplace(hires_texture->GetId(), hires_texture); |
| 179 | } |
| 180 | return hires_texture; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | HiresTexture::HiresTexture(bool has_arbitrary_mipmaps, std::string id) |
| 185 | : m_has_arbitrary_mipmaps(has_arbitrary_mipmaps), m_id(std::move(id)) |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | VideoCommon::TextureDataResource* HiresTexture::LoadTexture() const |
| 190 | { |
| 191 | auto& system = Core::System::GetInstance(); |
| 192 | auto& custom_resource_manager = system.GetCustomResourceManager(); |
| 193 | return custom_resource_manager.GetTextureDataFromAsset(m_id, s_file_library); |
| 194 | } |
| 195 | |
| 196 | std::set<std::string> GetTextureDirectoriesWithGameId(const std::string& root_directory, |
| 197 | const std::string& game_id) |
| 198 | { |
| 199 | std::set<std::string> result; |
| 200 | const std::string texture_directory = root_directory + game_id; |
| 201 | |
| 202 | if (File::Exists(texture_directory)) |
| 203 | { |
| 204 | result.insert(texture_directory); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | // If there's no directory with the region-specific ID, look for a 3-character region-free one |
| 209 | const std::string region_free_directory = root_directory + game_id.substr(0, 3); |
| 210 | |
| 211 | if (File::Exists(region_free_directory)) |
| 212 | { |
| 213 | result.insert(region_free_directory); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | const auto match_gameid_or_all = [game_id](const std::string& filename) { |
| 218 | std::string basename; |
| 219 | SplitPath(filename, nullptr, &basename, nullptr); |
| 220 | return basename == game_id || basename == game_id.substr(0, 3) || basename == "all"; |
| 221 | }; |
| 222 | |
| 223 | // Look for any other directories that might be specific to the given gameid |
| 224 | const auto files = Common::DoFileSearch(root_directory, ".txt", true); |
| 225 | for (const auto& file : files) |
| 226 | { |
| 227 | if (match_gameid_or_all(file)) |
| 228 | { |
| 229 | // The following code is used to calculate the top directory |
| 230 | // of a found gameid.txt file |
| 231 | // ex: <root directory>/My folder/gameids/<gameid>.txt |
| 232 | // would insert "<root directory>/My folder" |
| 233 | const auto directory_path = file.substr(root_directory.size()); |
| 234 | const std::size_t first_path_separator_position = directory_path.find_first_of(DIR_SEP_CHR); |
| 235 | result.insert(root_directory + directory_path.substr(0, first_path_separator_position)); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return result; |
| 240 | } |