From 065867e2c24e9856c360fc2d6b9a86c92aedc43e Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Tue, 25 May 2021 19:32:56 -0400 Subject: common: fs: Rework the Common Filesystem interface to make use of std::filesystem (#6270) * common: fs: fs_types: Create filesystem types Contains various filesystem types used by the Common::FS library * common: fs: fs_util: Add std::string to std::u8string conversion utility * common: fs: path_util: Add utlity functions for paths Contains various utility functions for getting or manipulating filesystem paths used by the Common::FS library * common: fs: file: Rewrite the IOFile implementation * common: fs: Reimplement Common::FS library using std::filesystem * common: fs: fs_paths: Add fs_paths to replace common_paths * common: fs: path_util: Add the rest of the path functions * common: Remove the previous Common::FS implementation * general: Remove unused fs includes * string_util: Remove unused function and include * nvidia_flags: Migrate to the new Common::FS library * settings: Migrate to the new Common::FS library * logging: backend: Migrate to the new Common::FS library * core: Migrate to the new Common::FS library * perf_stats: Migrate to the new Common::FS library * reporter: Migrate to the new Common::FS library * telemetry_session: Migrate to the new Common::FS library * key_manager: Migrate to the new Common::FS library * bis_factory: Migrate to the new Common::FS library * registered_cache: Migrate to the new Common::FS library * xts_archive: Migrate to the new Common::FS library * service: acc: Migrate to the new Common::FS library * applets/profile: Migrate to the new Common::FS library * applets/web: Migrate to the new Common::FS library * service: filesystem: Migrate to the new Common::FS library * loader: Migrate to the new Common::FS library * gl_shader_disk_cache: Migrate to the new Common::FS library * nsight_aftermath_tracker: Migrate to the new Common::FS library * vulkan_library: Migrate to the new Common::FS library * configure_debug: Migrate to the new Common::FS library * game_list_worker: Migrate to the new Common::FS library * config: Migrate to the new Common::FS library * configure_filesystem: Migrate to the new Common::FS library * configure_per_game_addons: Migrate to the new Common::FS library * configure_profile_manager: Migrate to the new Common::FS library * configure_ui: Migrate to the new Common::FS library * input_profiles: Migrate to the new Common::FS library * yuzu_cmd: config: Migrate to the new Common::FS library * yuzu_cmd: Migrate to the new Common::FS library * vfs_real: Migrate to the new Common::FS library * vfs: Migrate to the new Common::FS library * vfs_libzip: Migrate to the new Common::FS library * service: bcat: Migrate to the new Common::FS library * yuzu: main: Migrate to the new Common::FS library * vfs_real: Delete the contents of an existing file in CreateFile Current usages of CreateFile expect to delete the contents of an existing file, retain this behavior for now. * input_profiles: Don't iterate the input profile dir if it does not exist Silences an error produced in the log if the directory does not exist. * game_list_worker: Skip parsing file if the returned VfsFile is nullptr Prevents crashes in GetLoader when the virtual file is nullptr * common: fs: Validate paths for path length * service: filesystem: Open the mod load directory as read only --- .../renderer_opengl/gl_shader_disk_cache.cpp | 153 ++++++++++----------- 1 file changed, 76 insertions(+), 77 deletions(-) (limited to 'src/video_core/renderer_opengl/gl_shader_disk_cache.cpp') diff --git a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp index dbcb751cb..0deb86517 100644 --- a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp +++ b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp @@ -7,9 +7,10 @@ #include #include "common/assert.h" -#include "common/common_paths.h" #include "common/common_types.h" -#include "common/file_util.h" +#include "common/fs/file.h" +#include "common/fs/fs.h" +#include "common/fs/path_util.h" #include "common/logging/log.h" #include "common/scm_rev.h" #include "common/settings.h" @@ -26,11 +27,7 @@ using Tegra::Engines::ShaderType; using VideoCommon::Shader::BindlessSamplerMap; using VideoCommon::Shader::BoundSamplerMap; using VideoCommon::Shader::KeyMap; - -namespace { - using VideoCommon::Shader::SeparateSamplerKey; - using ShaderCacheVersionHash = std::array; struct ConstBufferKey { @@ -58,6 +55,8 @@ struct BindlessSamplerEntry { Tegra::Engines::SamplerDescriptor sampler; }; +namespace { + constexpr u32 NativeVersion = 21; ShaderCacheVersionHash GetShaderCacheVersionHash() { @@ -74,22 +73,20 @@ ShaderDiskCacheEntry::ShaderDiskCacheEntry() = default; ShaderDiskCacheEntry::~ShaderDiskCacheEntry() = default; bool ShaderDiskCacheEntry::Load(Common::FS::IOFile& file) { - if (file.ReadBytes(&type, sizeof(u32)) != sizeof(u32)) { + if (!file.ReadObject(type)) { return false; } u32 code_size; u32 code_size_b; - if (file.ReadBytes(&code_size, sizeof(u32)) != sizeof(u32) || - file.ReadBytes(&code_size_b, sizeof(u32)) != sizeof(u32)) { + if (!file.ReadObject(code_size) || !file.ReadObject(code_size_b)) { return false; } code.resize(code_size); code_b.resize(code_size_b); - - if (file.ReadArray(code.data(), code_size) != code_size) { + if (file.Read(code) != code_size) { return false; } - if (HasProgramA() && file.ReadArray(code_b.data(), code_size_b) != code_size_b) { + if (HasProgramA() && file.Read(code_b) != code_size_b) { return false; } @@ -99,13 +96,12 @@ bool ShaderDiskCacheEntry::Load(Common::FS::IOFile& file) { u32 num_bound_samplers; u32 num_separate_samplers; u32 num_bindless_samplers; - if (file.ReadArray(&unique_identifier, 1) != 1 || file.ReadArray(&bound_buffer, 1) != 1 || - file.ReadArray(&is_texture_handler_size_known, 1) != 1 || - file.ReadArray(&texture_handler_size_value, 1) != 1 || - file.ReadArray(&graphics_info, 1) != 1 || file.ReadArray(&compute_info, 1) != 1 || - file.ReadArray(&num_keys, 1) != 1 || file.ReadArray(&num_bound_samplers, 1) != 1 || - file.ReadArray(&num_separate_samplers, 1) != 1 || - file.ReadArray(&num_bindless_samplers, 1) != 1) { + if (!file.ReadObject(unique_identifier) || !file.ReadObject(bound_buffer) || + !file.ReadObject(is_texture_handler_size_known) || + !file.ReadObject(texture_handler_size_value) || !file.ReadObject(graphics_info) || + !file.ReadObject(compute_info) || !file.ReadObject(num_keys) || + !file.ReadObject(num_bound_samplers) || !file.ReadObject(num_separate_samplers) || + !file.ReadObject(num_bindless_samplers)) { return false; } if (is_texture_handler_size_known) { @@ -116,13 +112,10 @@ bool ShaderDiskCacheEntry::Load(Common::FS::IOFile& file) { std::vector flat_bound_samplers(num_bound_samplers); std::vector flat_separate_samplers(num_separate_samplers); std::vector flat_bindless_samplers(num_bindless_samplers); - if (file.ReadArray(flat_keys.data(), flat_keys.size()) != flat_keys.size() || - file.ReadArray(flat_bound_samplers.data(), flat_bound_samplers.size()) != - flat_bound_samplers.size() || - file.ReadArray(flat_separate_samplers.data(), flat_separate_samplers.size()) != - flat_separate_samplers.size() || - file.ReadArray(flat_bindless_samplers.data(), flat_bindless_samplers.size()) != - flat_bindless_samplers.size()) { + if (file.Read(flat_keys) != flat_keys.size() || + file.Read(flat_bound_samplers) != flat_bound_samplers.size() || + file.Read(flat_separate_samplers) != flat_separate_samplers.size() || + file.Read(flat_bindless_samplers) != flat_bindless_samplers.size()) { return false; } for (const auto& entry : flat_keys) { @@ -145,26 +138,25 @@ bool ShaderDiskCacheEntry::Load(Common::FS::IOFile& file) { } bool ShaderDiskCacheEntry::Save(Common::FS::IOFile& file) const { - if (file.WriteObject(static_cast(type)) != 1 || - file.WriteObject(static_cast(code.size())) != 1 || - file.WriteObject(static_cast(code_b.size())) != 1) { + if (!file.WriteObject(static_cast(type)) || + !file.WriteObject(static_cast(code.size())) || + !file.WriteObject(static_cast(code_b.size()))) { return false; } - if (file.WriteArray(code.data(), code.size()) != code.size()) { + if (file.Write(code) != code.size()) { return false; } - if (HasProgramA() && file.WriteArray(code_b.data(), code_b.size()) != code_b.size()) { + if (HasProgramA() && file.Write(code_b) != code_b.size()) { return false; } - if (file.WriteObject(unique_identifier) != 1 || file.WriteObject(bound_buffer) != 1 || - file.WriteObject(static_cast(texture_handler_size.has_value())) != 1 || - file.WriteObject(texture_handler_size.value_or(0)) != 1 || - file.WriteObject(graphics_info) != 1 || file.WriteObject(compute_info) != 1 || - file.WriteObject(static_cast(keys.size())) != 1 || - file.WriteObject(static_cast(bound_samplers.size())) != 1 || - file.WriteObject(static_cast(separate_samplers.size())) != 1 || - file.WriteObject(static_cast(bindless_samplers.size())) != 1) { + if (!file.WriteObject(unique_identifier) || !file.WriteObject(bound_buffer) || + !file.WriteObject(static_cast(texture_handler_size.has_value())) || + !file.WriteObject(texture_handler_size.value_or(0)) || !file.WriteObject(graphics_info) || + !file.WriteObject(compute_info) || !file.WriteObject(static_cast(keys.size())) || + !file.WriteObject(static_cast(bound_samplers.size())) || + !file.WriteObject(static_cast(separate_samplers.size())) || + !file.WriteObject(static_cast(bindless_samplers.size()))) { return false; } @@ -197,13 +189,10 @@ bool ShaderDiskCacheEntry::Save(Common::FS::IOFile& file) const { BindlessSamplerEntry{address.first, address.second, sampler}); } - return file.WriteArray(flat_keys.data(), flat_keys.size()) == flat_keys.size() && - file.WriteArray(flat_bound_samplers.data(), flat_bound_samplers.size()) == - flat_bound_samplers.size() && - file.WriteArray(flat_separate_samplers.data(), flat_separate_samplers.size()) == - flat_separate_samplers.size() && - file.WriteArray(flat_bindless_samplers.data(), flat_bindless_samplers.size()) == - flat_bindless_samplers.size(); + return file.Write(flat_keys) == flat_keys.size() && + file.Write(flat_bound_samplers) == flat_bound_samplers.size() && + file.Write(flat_separate_samplers) == flat_separate_samplers.size() && + file.Write(flat_bindless_samplers) == flat_bindless_samplers.size(); } ShaderDiskCacheOpenGL::ShaderDiskCacheOpenGL() = default; @@ -221,7 +210,8 @@ std::optional> ShaderDiskCacheOpenGL::LoadTran return std::nullopt; } - Common::FS::IOFile file(GetTransferablePath(), "rb"); + Common::FS::IOFile file{GetTransferablePath(), Common::FS::FileAccessMode::Read, + Common::FS::FileType::BinaryFile}; if (!file.IsOpen()) { LOG_INFO(Render_OpenGL, "No transferable shader cache found"); is_usable = true; @@ -229,7 +219,7 @@ std::optional> ShaderDiskCacheOpenGL::LoadTran } u32 version{}; - if (file.ReadBytes(&version, sizeof(version)) != sizeof(version)) { + if (!file.ReadObject(version)) { LOG_ERROR(Render_OpenGL, "Failed to get transferable cache version, skipping it"); return std::nullopt; } @@ -249,7 +239,7 @@ std::optional> ShaderDiskCacheOpenGL::LoadTran // Version is valid, load the shaders std::vector entries; - while (file.Tell() < file.GetSize()) { + while (static_cast(file.Tell()) < file.GetSize()) { ShaderDiskCacheEntry& entry = entries.emplace_back(); if (!entry.Load(file)) { LOG_ERROR(Render_OpenGL, "Failed to load transferable raw entry, skipping"); @@ -266,7 +256,8 @@ std::vector ShaderDiskCacheOpenGL::LoadPrecompiled() return {}; } - Common::FS::IOFile file(GetPrecompiledPath(), "rb"); + Common::FS::IOFile file{GetPrecompiledPath(), Common::FS::FileAccessMode::Read, + Common::FS::FileType::BinaryFile}; if (!file.IsOpen()) { LOG_INFO(Render_OpenGL, "No precompiled shader cache found"); return {}; @@ -286,7 +277,9 @@ std::optional> ShaderDiskCacheOpenGL::Lo Common::FS::IOFile& file) { // Read compressed file from disk and decompress to virtual precompiled cache file std::vector compressed(file.GetSize()); - file.ReadBytes(compressed.data(), compressed.size()); + if (file.Read(compressed) != file.GetSize()) { + return std::nullopt; + } const std::vector decompressed = Common::Compression::DecompressDataZSTD(compressed); SaveArrayToPrecompiled(decompressed.data(), decompressed.size()); precompiled_cache_virtual_file_offset = 0; @@ -321,9 +314,9 @@ std::optional> ShaderDiskCacheOpenGL::Lo } void ShaderDiskCacheOpenGL::InvalidateTransferable() { - if (!Common::FS::Delete(GetTransferablePath())) { + if (!Common::FS::RemoveFile(GetTransferablePath())) { LOG_ERROR(Render_OpenGL, "Failed to invalidate transferable file={}", - GetTransferablePath()); + Common::FS::PathToUTF8String(GetTransferablePath())); } InvalidatePrecompiled(); } @@ -332,8 +325,9 @@ void ShaderDiskCacheOpenGL::InvalidatePrecompiled() { // Clear virtaul precompiled cache file precompiled_cache_virtual_file.Resize(0); - if (!Common::FS::Delete(GetPrecompiledPath())) { - LOG_ERROR(Render_OpenGL, "Failed to invalidate precompiled file={}", GetPrecompiledPath()); + if (!Common::FS::RemoveFile(GetPrecompiledPath())) { + LOG_ERROR(Render_OpenGL, "Failed to invalidate precompiled file={}", + Common::FS::PathToUTF8String(GetPrecompiledPath())); } } @@ -398,16 +392,18 @@ Common::FS::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const { const auto transferable_path{GetTransferablePath()}; const bool existed = Common::FS::Exists(transferable_path); - Common::FS::IOFile file(transferable_path, "ab"); + Common::FS::IOFile file{transferable_path, Common::FS::FileAccessMode::Append, + Common::FS::FileType::BinaryFile}; if (!file.IsOpen()) { - LOG_ERROR(Render_OpenGL, "Failed to open transferable cache in path={}", transferable_path); + LOG_ERROR(Render_OpenGL, "Failed to open transferable cache in path={}", + Common::FS::PathToUTF8String(transferable_path)); return {}; } if (!existed || file.GetSize() == 0) { // If the file didn't exist, write its version - if (file.WriteObject(NativeVersion) != 1) { + if (!file.WriteObject(NativeVersion)) { LOG_ERROR(Render_OpenGL, "Failed to write transferable cache version in path={}", - transferable_path); + Common::FS::PathToUTF8String(transferable_path)); return {}; } } @@ -429,51 +425,54 @@ void ShaderDiskCacheOpenGL::SaveVirtualPrecompiledFile() { const std::vector compressed = Common::Compression::CompressDataZSTDDefault(uncompressed.data(), uncompressed.size()); - const auto precompiled_path{GetPrecompiledPath()}; - Common::FS::IOFile file(precompiled_path, "wb"); + const auto precompiled_path = GetPrecompiledPath(); + Common::FS::IOFile file{precompiled_path, Common::FS::FileAccessMode::Write, + Common::FS::FileType::BinaryFile}; if (!file.IsOpen()) { - LOG_ERROR(Render_OpenGL, "Failed to open precompiled cache in path={}", precompiled_path); + LOG_ERROR(Render_OpenGL, "Failed to open precompiled cache in path={}", + Common::FS::PathToUTF8String(precompiled_path)); return; } - if (file.WriteBytes(compressed.data(), compressed.size()) != compressed.size()) { + if (file.Write(compressed) != compressed.size()) { LOG_ERROR(Render_OpenGL, "Failed to write precompiled cache version in path={}", - precompiled_path); + Common::FS::PathToUTF8String(precompiled_path)); } } bool ShaderDiskCacheOpenGL::EnsureDirectories() const { - const auto CreateDir = [](const std::string& dir) { + const auto CreateDir = [](const std::filesystem::path& dir) { if (!Common::FS::CreateDir(dir)) { - LOG_ERROR(Render_OpenGL, "Failed to create directory={}", dir); + LOG_ERROR(Render_OpenGL, "Failed to create directory={}", + Common::FS::PathToUTF8String(dir)); return false; } return true; }; - return CreateDir(Common::FS::GetUserPath(Common::FS::UserPath::ShaderDir)) && + return CreateDir(Common::FS::GetYuzuPath(Common::FS::YuzuPath::ShaderDir)) && CreateDir(GetBaseDir()) && CreateDir(GetTransferableDir()) && CreateDir(GetPrecompiledDir()); } -std::string ShaderDiskCacheOpenGL::GetTransferablePath() const { - return Common::FS::SanitizePath(GetTransferableDir() + DIR_SEP_CHR + GetTitleID() + ".bin"); +std::filesystem::path ShaderDiskCacheOpenGL::GetTransferablePath() const { + return GetTransferableDir() / fmt::format("{}.bin", GetTitleID()); } -std::string ShaderDiskCacheOpenGL::GetPrecompiledPath() const { - return Common::FS::SanitizePath(GetPrecompiledDir() + DIR_SEP_CHR + GetTitleID() + ".bin"); +std::filesystem::path ShaderDiskCacheOpenGL::GetPrecompiledPath() const { + return GetPrecompiledDir() / fmt::format("{}.bin", GetTitleID()); } -std::string ShaderDiskCacheOpenGL::GetTransferableDir() const { - return GetBaseDir() + DIR_SEP "transferable"; +std::filesystem::path ShaderDiskCacheOpenGL::GetTransferableDir() const { + return GetBaseDir() / "transferable"; } -std::string ShaderDiskCacheOpenGL::GetPrecompiledDir() const { - return GetBaseDir() + DIR_SEP "precompiled"; +std::filesystem::path ShaderDiskCacheOpenGL::GetPrecompiledDir() const { + return GetBaseDir() / "precompiled"; } -std::string ShaderDiskCacheOpenGL::GetBaseDir() const { - return Common::FS::GetUserPath(Common::FS::UserPath::ShaderDir) + DIR_SEP "opengl"; +std::filesystem::path ShaderDiskCacheOpenGL::GetBaseDir() const { + return Common::FS::GetYuzuPath(Common::FS::YuzuPath::ShaderDir) / "opengl"; } std::string ShaderDiskCacheOpenGL::GetTitleID() const { -- cgit v1.2.3