gl_shader_disk_cache: Avoid reopening files every time a shader need to be written. (#6344)

Hopefully reduces stutters with Android SAF
This commit is contained in:
SachinVin 2023-03-26 03:05:17 +05:30 committed by GitHub
parent fdb7ab47ff
commit 44097c2a8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 39 deletions

View File

@ -104,7 +104,10 @@ bool ShaderDiskCacheRaw::Save(FileUtil::IOFile& file) const {
return true; return true;
} }
ShaderDiskCache::ShaderDiskCache(bool separable) : separable{separable} {} ShaderDiskCache::ShaderDiskCache(bool separable)
: separable{separable}, transferable_file(AppendTransferableFile()),
// seperable shaders use the virtual precompile file, that already has a header.
precompiled_file(AppendPrecompiledFile(!separable)) {}
std::optional<std::vector<ShaderDiskCacheRaw>> ShaderDiskCache::LoadTransferable() { std::optional<std::vector<ShaderDiskCacheRaw>> ShaderDiskCache::LoadTransferable() {
const bool has_title_id = GetProgramID() != 0; const bool has_title_id = GetProgramID() != 0;
@ -114,15 +117,14 @@ std::optional<std::vector<ShaderDiskCacheRaw>> ShaderDiskCache::LoadTransferable
} }
tried_to_load = true; tried_to_load = true;
FileUtil::IOFile file(GetTransferablePath(), "rb"); if (transferable_file.GetSize() == 0) {
if (!file.IsOpen()) {
LOG_INFO(Render_OpenGL, "No transferable shader cache found for game with title id={}", LOG_INFO(Render_OpenGL, "No transferable shader cache found for game with title id={}",
GetTitleID()); GetTitleID());
return std::nullopt; return std::nullopt;
} }
u32 version{}; u32 version{};
if (file.ReadBytes(&version, sizeof(version)) != sizeof(version)) { if (transferable_file.ReadBytes(&version, sizeof(version)) != sizeof(version)) {
LOG_ERROR(Render_OpenGL, LOG_ERROR(Render_OpenGL,
"Failed to get transferable cache version for title id={} - skipping", "Failed to get transferable cache version for title id={} - skipping",
GetTitleID()); GetTitleID());
@ -131,7 +133,6 @@ std::optional<std::vector<ShaderDiskCacheRaw>> ShaderDiskCache::LoadTransferable
if (version < NativeVersion) { if (version < NativeVersion) {
LOG_INFO(Render_OpenGL, "Transferable shader cache is old - removing"); LOG_INFO(Render_OpenGL, "Transferable shader cache is old - removing");
file.Close();
InvalidateAll(); InvalidateAll();
return std::nullopt; return std::nullopt;
} }
@ -143,9 +144,9 @@ std::optional<std::vector<ShaderDiskCacheRaw>> ShaderDiskCache::LoadTransferable
// Version is valid, load the shaders // Version is valid, load the shaders
std::vector<ShaderDiskCacheRaw> raws; std::vector<ShaderDiskCacheRaw> raws;
while (file.Tell() < file.GetSize()) { while (transferable_file.Tell() < transferable_file.GetSize()) {
TransferableEntryKind kind{}; TransferableEntryKind kind{};
if (file.ReadBytes(&kind, sizeof(u32)) != sizeof(u32)) { if (transferable_file.ReadBytes(&kind, sizeof(u32)) != sizeof(u32)) {
LOG_ERROR(Render_OpenGL, "Failed to read transferable file - skipping"); LOG_ERROR(Render_OpenGL, "Failed to read transferable file - skipping");
return std::nullopt; return std::nullopt;
} }
@ -153,7 +154,7 @@ std::optional<std::vector<ShaderDiskCacheRaw>> ShaderDiskCache::LoadTransferable
switch (kind) { switch (kind) {
case TransferableEntryKind::Raw: { case TransferableEntryKind::Raw: {
ShaderDiskCacheRaw entry; ShaderDiskCacheRaw entry;
if (!entry.Load(file)) { if (!entry.Load(transferable_file)) {
LOG_ERROR(Render_OpenGL, "Failed to load transferable raw entry - skipping"); LOG_ERROR(Render_OpenGL, "Failed to load transferable raw entry - skipping");
return std::nullopt; return std::nullopt;
} }
@ -177,19 +178,17 @@ ShaderDiskCache::LoadPrecompiled(bool compressed) {
if (!IsUsable()) if (!IsUsable())
return {}; return {};
FileUtil::IOFile file(GetPrecompiledPath(), "rb"); if (precompiled_file.GetSize() == 0) {
if (!file.IsOpen()) {
LOG_INFO(Render_OpenGL, "No precompiled shader cache found for game with title id={}", LOG_INFO(Render_OpenGL, "No precompiled shader cache found for game with title id={}",
GetTitleID()); GetTitleID());
return {}; return {};
} }
const auto result = LoadPrecompiledFile(file, compressed); const auto result = LoadPrecompiledFile(precompiled_file, compressed);
if (!result) { if (!result) {
LOG_INFO(Render_OpenGL, LOG_INFO(Render_OpenGL,
"Failed to load precompiled cache for game with title id={} - removing", "Failed to load precompiled cache for game with title id={} - removing",
GetTitleID()); GetTitleID());
file.Close();
InvalidatePrecompiled(); InvalidatePrecompiled();
return {}; return {};
} }
@ -331,10 +330,13 @@ bool ShaderDiskCache::SaveDecompiledToCache(u64 unique_identifier,
} }
void ShaderDiskCache::InvalidateAll() { void ShaderDiskCache::InvalidateAll() {
transferable_file.Close();
if (!FileUtil::Delete(GetTransferablePath())) { if (!FileUtil::Delete(GetTransferablePath())) {
LOG_ERROR(Render_OpenGL, "Failed to invalidate transferable file={}", LOG_ERROR(Render_OpenGL, "Failed to invalidate transferable file={}",
GetTransferablePath()); GetTransferablePath());
} }
transferable_file = AppendTransferableFile();
InvalidatePrecompiled(); InvalidatePrecompiled();
} }
@ -342,9 +344,11 @@ void ShaderDiskCache::InvalidatePrecompiled() {
// Clear virtual precompiled cache file // Clear virtual precompiled cache file
decompressed_precompiled_cache.resize(0); decompressed_precompiled_cache.resize(0);
precompiled_file.Close();
if (!FileUtil::Delete(GetPrecompiledPath())) { if (!FileUtil::Delete(GetPrecompiledPath())) {
LOG_ERROR(Render_OpenGL, "Failed to invalidate precompiled file={}", GetPrecompiledPath()); LOG_ERROR(Render_OpenGL, "Failed to invalidate precompiled file={}", GetPrecompiledPath());
} }
precompiled_file = AppendPrecompiledFile(!separable);
} }
void ShaderDiskCache::SaveRaw(const ShaderDiskCacheRaw& entry) { void ShaderDiskCache::SaveRaw(const ShaderDiskCacheRaw& entry) {
@ -357,12 +361,9 @@ void ShaderDiskCache::SaveRaw(const ShaderDiskCacheRaw& entry) {
return; return;
} }
FileUtil::IOFile file = AppendTransferableFile(); if (transferable_file.WriteObject(TransferableEntryKind::Raw) != 1 ||
if (!file.IsOpen()) !entry.Save(transferable_file)) {
return;
if (file.WriteObject(TransferableEntryKind::Raw) != 1 || !entry.Save(file)) {
LOG_ERROR(Render_OpenGL, "Failed to save raw transferable cache entry - removing"); LOG_ERROR(Render_OpenGL, "Failed to save raw transferable cache entry - removing");
file.Close();
InvalidateAll(); InvalidateAll();
return; return;
} }
@ -413,10 +414,6 @@ void ShaderDiskCache::SaveDumpToFile(u64 unique_identifier, GLuint program, bool
if (!IsUsable()) if (!IsUsable())
return; return;
FileUtil::IOFile file = AppendPrecompiledFile();
if (!file.IsOpen())
return;
GLint binary_length{}; GLint binary_length{};
glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binary_length); glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binary_length);
@ -424,11 +421,11 @@ void ShaderDiskCache::SaveDumpToFile(u64 unique_identifier, GLuint program, bool
std::vector<u8> binary(binary_length); std::vector<u8> binary(binary_length);
glGetProgramBinary(program, binary_length, nullptr, &binary_format, binary.data()); glGetProgramBinary(program, binary_length, nullptr, &binary_format, binary.data());
if (file.WriteObject(static_cast<u32>(PrecompiledEntryKind::Dump)) != 1 || if (precompiled_file.WriteObject(static_cast<u32>(PrecompiledEntryKind::Dump)) != 1 ||
file.WriteObject(unique_identifier) != 1 || precompiled_file.WriteObject(unique_identifier) != 1 ||
file.WriteObject(static_cast<u32>(binary_format)) != 1 || precompiled_file.WriteObject(static_cast<u32>(binary_format)) != 1 ||
file.WriteObject(static_cast<u32>(binary_length)) != 1 || precompiled_file.WriteObject(static_cast<u32>(binary_length)) != 1 ||
file.WriteArray(binary.data(), binary.size()) != binary.size()) { precompiled_file.WriteArray(binary.data(), binary.size()) != binary.size()) {
LOG_ERROR(Render_OpenGL, "Failed to save binary program file in shader={:016x} - removing", LOG_ERROR(Render_OpenGL, "Failed to save binary program file in shader={:016x} - removing",
unique_identifier); unique_identifier);
InvalidatePrecompiled(); InvalidatePrecompiled();
@ -437,7 +434,7 @@ void ShaderDiskCache::SaveDumpToFile(u64 unique_identifier, GLuint program, bool
// SaveDecompiled is used only to store the accurate multiplication setting, a better way is to // SaveDecompiled is used only to store the accurate multiplication setting, a better way is to
// probably change the header in SaveDump // probably change the header in SaveDump
SaveDecompiledToFile(file, unique_identifier, {}, sanitize_mul); SaveDecompiledToFile(precompiled_file, unique_identifier, {}, sanitize_mul);
} }
bool ShaderDiskCache::IsUsable() const { bool ShaderDiskCache::IsUsable() const {
@ -451,7 +448,7 @@ FileUtil::IOFile ShaderDiskCache::AppendTransferableFile() {
const auto transferable_path{GetTransferablePath()}; const auto transferable_path{GetTransferablePath()};
const bool existed = FileUtil::Exists(transferable_path); const bool existed = FileUtil::Exists(transferable_path);
FileUtil::IOFile file(transferable_path, "ab"); FileUtil::IOFile file(transferable_path, "ab+");
if (!file.IsOpen()) { 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={}", transferable_path);
return {}; return {};
@ -467,20 +464,21 @@ FileUtil::IOFile ShaderDiskCache::AppendTransferableFile() {
return file; return file;
} }
FileUtil::IOFile ShaderDiskCache::AppendPrecompiledFile() { FileUtil::IOFile ShaderDiskCache::AppendPrecompiledFile(bool write_header) {
if (!EnsureDirectories()) if (!EnsureDirectories())
return {}; return {};
const auto precompiled_path{GetPrecompiledPath()}; const auto precompiled_path{GetPrecompiledPath()};
const bool existed = FileUtil::Exists(precompiled_path); const bool existed = FileUtil::Exists(precompiled_path);
FileUtil::IOFile file(precompiled_path, "ab"); FileUtil::IOFile file(precompiled_path, "ab+");
if (!file.IsOpen()) { 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={}", precompiled_path);
return {}; return {};
} }
if (!existed || file.GetSize() == 0) {
// If the file didn't exist, write its version // If the file didn't exist, write its version
if (write_header && (!existed || file.GetSize() == 0)) {
const auto hash{GetShaderCacheVersionHash()}; const auto hash{GetShaderCacheVersionHash()};
if (file.WriteArray(hash.data(), hash.size()) != hash.size()) { if (file.WriteArray(hash.data(), hash.size()) != hash.size()) {
LOG_ERROR(Render_OpenGL, "Failed to write precompiled cache version in path={}", LOG_ERROR(Render_OpenGL, "Failed to write precompiled cache version in path={}",
@ -506,13 +504,14 @@ void ShaderDiskCache::SaveVirtualPrecompiledFile() {
decompressed_precompiled_cache.data(), decompressed_precompiled_cache.size()); decompressed_precompiled_cache.data(), decompressed_precompiled_cache.size());
const auto precompiled_path{GetPrecompiledPath()}; const auto precompiled_path{GetPrecompiledPath()};
FileUtil::IOFile file(precompiled_path, "wb");
if (!file.IsOpen()) { precompiled_file.Close();
LOG_ERROR(Render_OpenGL, "Failed to open precompiled cache in path={}", precompiled_path); if (!FileUtil::Delete(GetPrecompiledPath())) {
return; LOG_ERROR(Render_OpenGL, "Failed to invalidate precompiled file={}", GetPrecompiledPath());
} }
if (file.WriteBytes(compressed.data(), compressed.size()) != compressed.size()) { precompiled_file = AppendPrecompiledFile(!separable);
if (precompiled_file.WriteBytes(compressed.data(), compressed.size()) != compressed.size()) {
LOG_ERROR(Render_OpenGL, "Failed to write precompiled cache version in path={}", LOG_ERROR(Render_OpenGL, "Failed to write precompiled cache version in path={}",
precompiled_path); precompiled_path);
return; return;

View File

@ -19,6 +19,7 @@
#include "common/assert.h" #include "common/assert.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/file_util.h"
#include "video_core/regs.h" #include "video_core/regs.h"
#include "video_core/renderer_opengl/gl_shader_decompiler.h" #include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_opengl/gl_shader_gen.h" #include "video_core/renderer_opengl/gl_shader_gen.h"
@ -141,9 +142,12 @@ private:
/// Returns if the cache can be used /// Returns if the cache can be used
bool IsUsable() const; bool IsUsable() const;
/// Opens current game's transferable file and write it's header if it doesn't exist /// Opens current game's transferable file and write it's header if it doesn't exist.
FileUtil::IOFile AppendTransferableFile(); FileUtil::IOFile AppendTransferableFile();
/// Opens current game's precompiled file and write it's header if it doesn't exist
FileUtil::IOFile AppendPrecompiledFile(bool write_header);
/// Save precompiled header to precompiled_cache_in_memory /// Save precompiled header to precompiled_cache_in_memory
void SavePrecompiledHeaderToVirtualPrecompiledCache(); void SavePrecompiledHeaderToVirtualPrecompiledCache();
@ -223,7 +227,8 @@ private:
u64 program_id{}; u64 program_id{};
std::string title_id; std::string title_id;
FileUtil::IOFile AppendPrecompiledFile(); FileUtil::IOFile transferable_file;
FileUtil::IOFile precompiled_file;
}; };
} // namespace OpenGL } // namespace OpenGL