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

View File

@ -19,6 +19,7 @@
#include "common/assert.h"
#include "common/common_types.h"
#include "common/file_util.h"
#include "video_core/regs.h"
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_opengl/gl_shader_gen.h"
@ -141,9 +142,12 @@ private:
/// Returns if the cache can be used
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();
/// 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
void SavePrecompiledHeaderToVirtualPrecompiledCache();
@ -223,7 +227,8 @@ private:
u64 program_id{};
std::string title_id;
FileUtil::IOFile AppendPrecompiledFile();
FileUtil::IOFile transferable_file;
FileUtil::IOFile precompiled_file;
};
} // namespace OpenGL