From 6fbbc2683eeedf3767ea3d741c62555a30d3ff45 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 22 Nov 2019 17:10:41 -0500 Subject: VideoCommon: Make use of fmt outside of shader generators Migrates most of VideoCommon over to using fmt, with the exception being the shader generator code. The shader generators are quite large and have more corner cases to deal with in terms of conversion (shaders have braces in them, so we need to make sure to escape them). Because of the large amount of code that would need to be converted, the conversion of VideoCommon will be in two parts: - This change (which converts over the general case string formatting), - A follow up change that will specifically deal with converting over the shader generators. --- Source/Core/VideoCommon/HiresTextures.cpp | 58 +++++++++++++++++-------------- 1 file changed, 32 insertions(+), 26 deletions(-) (limited to 'Source/Core/VideoCommon/HiresTextures.cpp') diff --git a/Source/Core/VideoCommon/HiresTextures.cpp b/Source/Core/VideoCommon/HiresTextures.cpp index 70b9df00be..d70a6ab44e 100644 --- a/Source/Core/VideoCommon/HiresTextures.cpp +++ b/Source/Core/VideoCommon/HiresTextures.cpp @@ -5,22 +5,22 @@ #include "VideoCommon/HiresTextures.h" #include -#include -#include #include #include #include +#include #include #include #include #include #include +#include + #include "Common/File.h" #include "Common/FileSearch.h" #include "Common/FileUtil.h" #include "Common/Flag.h" -#include "Common/Hash.h" #include "Common/Image.h" #include "Common/Logging/Log.h" #include "Common/MemoryUtil.h" @@ -39,6 +39,8 @@ struct DiskTexture bool has_arbitrary_mipmaps; }; +constexpr std::string_view s_format_prefix{"tex1_"}; + static std::unordered_map s_textureMap; static std::unordered_map> s_textureCache; static std::mutex s_textureCacheMutex; @@ -46,8 +48,6 @@ static Common::Flag s_textureCacheAbortLoading; static std::thread s_prefetcher; -static const std::string s_format_prefix = "tex1_"; - void HiresTexture::Init() { Update(); @@ -135,12 +135,13 @@ void HiresTexture::Prefetch() Common::SetCurrentThreadName("Prefetcher"); size_t size_sum = 0; - size_t sys_mem = Common::MemPhysical(); - size_t recommended_min_mem = 2 * size_t(1024 * 1024 * 1024); + const size_t sys_mem = Common::MemPhysical(); + const 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 = + const size_t max_mem = (sys_mem / 2 < recommended_min_mem) ? (sys_mem / 2) : (sys_mem - recommended_min_mem); - u32 starttime = Common::Timer::GetTimeMs(); + + const u32 start_time = Common::Timer::GetTimeMs(); for (const auto& entry : s_textureMap) { const std::string& base_filename = entry.first; @@ -180,16 +181,17 @@ void HiresTexture::Prefetch() Config::SetCurrent(Config::GFX_HIRES_TEXTURES, false); OSD::AddMessage( - StringFromFormat( - "Custom Textures prefetching after %.1f MB aborted, not enough RAM available", + fmt::format( + "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), + + 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), 10000); } @@ -244,22 +246,26 @@ std::string HiresTexture::GenBaseName(const u8* texture, size_t texture_size, co tlut += 2 * min; } - u64 tex_hash = XXH64(texture, texture_size, 0); - u64 tlut_hash = tlut_size ? XXH64(tlut, tlut_size, 0) : 0; + const u64 tex_hash = XXH64(texture, texture_size, 0); + const 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", static_cast(format)); - std::string fullname = basename + tlutname + formatname; + const std::string base_name = fmt::format("{}{}x{}{}_{:016x}", s_format_prefix, width, height, + has_mipmaps ? "_m" : "", tex_hash); + const std::string tlut_name = tlut_size ? fmt::format("_{:016x}", tlut_hash) : ""; + const std::string format_name = fmt::format("_{}", static_cast(format)); + const std::string full_name = base_name + tlut_name + format_name; // try to match a wildcard template - if (!dump && s_textureMap.find(basename + "_$" + formatname) != s_textureMap.end()) - return basename + "_$" + formatname; + if (!dump) + { + const std::string texture_name = fmt::format("{}_${}", base_name, format_name); + if (s_textureMap.find(texture_name) != s_textureMap.end()) + return texture_name; + } // else generate the complete texture - if (dump || s_textureMap.find(fullname) != s_textureMap.end()) - return fullname; + if (dump || s_textureMap.find(full_name) != s_textureMap.end()) + return full_name; return ""; } @@ -326,7 +332,7 @@ std::unique_ptr HiresTexture::Load(const std::string& base_filenam { std::string filename = base_filename; if (mip_level != 0) - filename += StringFromFormat("_mip%u", mip_level); + filename += fmt::format("_mip{}", mip_level); filename_iter = s_textureMap.find(filename); if (filename_iter == s_textureMap.end()) -- cgit v1.2.3