gl_shader_disk_cache: Guard reads and writes against failure

This commit is contained in:
ReinUsesLisp 2019-01-15 15:22:25 -03:00
parent 750abcc23d
commit 2bc6a699dc
2 changed files with 342 additions and 219 deletions

View File

@ -24,6 +24,8 @@
namespace OpenGL { namespace OpenGL {
using ShaderCacheVersionHash = std::array<u8, 64>;
enum class TransferableEntryKind : u32 { enum class TransferableEntryKind : u32 {
Raw, Raw,
Usage, Usage,
@ -35,7 +37,6 @@ enum class PrecompiledEntryKind : u32 {
}; };
constexpr u32 NativeVersion = 1; constexpr u32 NativeVersion = 1;
constexpr u32 ShaderHashSize = 64;
// Making sure sizes doesn't change by accident // Making sure sizes doesn't change by accident
static_assert(sizeof(BaseBindings) == 12); static_assert(sizeof(BaseBindings) == 12);
@ -46,10 +47,11 @@ std::string GetTitleID() {
return fmt::format("{:016X}", Core::CurrentProcess()->GetTitleID()); return fmt::format("{:016X}", Core::CurrentProcess()->GetTitleID());
} }
std::string GetShaderHash() { ShaderCacheVersionHash GetShaderCacheVersionHash() {
std::array<char, ShaderHashSize> hash{}; ShaderCacheVersionHash hash{};
std::strncpy(hash.data(), Common::g_shader_cache_version, ShaderHashSize); const std::size_t length = std::min(std::strlen(Common::g_shader_cache_version), hash.size());
return std::string(hash.data(), hash.size()); std::memcpy(hash.data(), Common::g_shader_cache_version, length);
return hash;
} }
template <typename T> template <typename T>
@ -82,24 +84,6 @@ std::vector<u8> DecompressData(const std::vector<u8>& compressed, std::size_t un
} }
} // namespace } // namespace
ShaderDiskCacheRaw::ShaderDiskCacheRaw(FileUtil::IOFile& file) {
file.ReadBytes(&unique_identifier, sizeof(u64));
file.ReadBytes(&program_type, sizeof(u32));
u32 program_code_size{};
u32 program_code_size_b{};
file.ReadBytes(&program_code_size, sizeof(u32));
file.ReadBytes(&program_code_size_b, sizeof(u32));
program_code.resize(program_code_size);
program_code_b.resize(program_code_size_b);
file.ReadArray(program_code.data(), program_code_size);
if (HasProgramA()) {
file.ReadArray(program_code_b.data(), program_code_size_b);
}
}
ShaderDiskCacheRaw::ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type, ShaderDiskCacheRaw::ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type,
u32 program_code_size, u32 program_code_size_b, u32 program_code_size, u32 program_code_size_b,
ProgramCode program_code, ProgramCode program_code_b) ProgramCode program_code, ProgramCode program_code_b)
@ -107,25 +91,57 @@ ShaderDiskCacheRaw::ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderPro
program_code_size{program_code_size}, program_code_size_b{program_code_size_b}, program_code_size{program_code_size}, program_code_size_b{program_code_size_b},
program_code{std::move(program_code)}, program_code_b{std::move(program_code_b)} {} program_code{std::move(program_code)}, program_code_b{std::move(program_code_b)} {}
ShaderDiskCacheRaw::ShaderDiskCacheRaw() = default;
ShaderDiskCacheRaw::~ShaderDiskCacheRaw() = default; ShaderDiskCacheRaw::~ShaderDiskCacheRaw() = default;
void ShaderDiskCacheRaw::Save(FileUtil::IOFile& file) const { bool ShaderDiskCacheRaw::Load(FileUtil::IOFile& file) {
file.WriteObject(unique_identifier); if (file.ReadBytes(&unique_identifier, sizeof(u64)) != sizeof(u64) ||
file.WriteObject(static_cast<u32>(program_type)); file.ReadBytes(&program_type, sizeof(u32)) != sizeof(u32)) {
file.WriteObject(program_code_size); return false;
file.WriteObject(program_code_size_b);
file.WriteArray(program_code.data(), program_code_size);
if (HasProgramA()) {
file.WriteArray(program_code_b.data(), program_code_size_b);
} }
u32 program_code_size{};
u32 program_code_size_b{};
if (file.ReadBytes(&program_code_size, sizeof(u32)) != sizeof(u32) ||
file.ReadBytes(&program_code_size_b, sizeof(u32)) != sizeof(u32)) {
return false;
}
program_code.resize(program_code_size);
program_code_b.resize(program_code_size_b);
if (file.ReadArray(program_code.data(), program_code_size) != program_code_size)
return false;
if (HasProgramA() &&
file.ReadArray(program_code_b.data(), program_code_size_b) != program_code_size_b) {
return false;
}
return true;
}
bool ShaderDiskCacheRaw::Save(FileUtil::IOFile& file) const {
if (file.WriteObject(unique_identifier) != 1 ||
file.WriteObject(static_cast<u32>(program_type)) != 1 ||
file.WriteObject(program_code_size) != 1 || file.WriteObject(program_code_size_b) != 1) {
return false;
}
if (file.WriteArray(program_code.data(), program_code_size) != program_code_size)
return false;
if (HasProgramA() &&
file.WriteArray(program_code_b.data(), program_code_size_b) != program_code_size_b) {
return false;
}
return true;
} }
std::optional<std::pair<std::vector<ShaderDiskCacheRaw>, std::vector<ShaderDiskCacheUsage>>> std::optional<std::pair<std::vector<ShaderDiskCacheRaw>, std::vector<ShaderDiskCacheUsage>>>
ShaderDiskCacheOpenGL::LoadTransferable() { ShaderDiskCacheOpenGL::LoadTransferable() {
if (!Settings::values.use_disk_shader_cache) { if (!Settings::values.use_disk_shader_cache)
return {}; return {};
} tried_to_load = true;
FileUtil::IOFile file(GetTransferablePath(), "rb"); FileUtil::IOFile file(GetTransferablePath(), "rb");
if (!file.IsOpen()) { if (!file.IsOpen()) {
@ -133,15 +149,19 @@ ShaderDiskCacheOpenGL::LoadTransferable() {
GetTitleID()); GetTitleID());
return {}; return {};
} }
const u64 file_size = file.GetSize();
u32 version{}; u32 version{};
file.ReadBytes(&version, sizeof(version)); if (file.ReadBytes(&version, sizeof(version)) != sizeof(version)) {
LOG_ERROR(Render_OpenGL,
"Failed to get transferable cache version for title id={} - skipping",
GetTitleID());
return {};
}
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(); file.Close();
FileUtil::Delete(GetTransferablePath()); InvalidateTransferable();
return {}; return {};
} }
if (version > NativeVersion) { if (version > NativeVersion) {
@ -153,25 +173,35 @@ ShaderDiskCacheOpenGL::LoadTransferable() {
// Version is valid, load the shaders // Version is valid, load the shaders
std::vector<ShaderDiskCacheRaw> raws; std::vector<ShaderDiskCacheRaw> raws;
std::vector<ShaderDiskCacheUsage> usages; std::vector<ShaderDiskCacheUsage> usages;
while (file.Tell() < file_size) { while (file.Tell() < file.GetSize()) {
TransferableEntryKind kind{}; TransferableEntryKind kind{};
file.ReadBytes(&kind, sizeof(u32)); if (file.ReadBytes(&kind, sizeof(u32)) != sizeof(u32)) {
LOG_ERROR(Render_OpenGL, "Failed to read transferable file - skipping");
return {};
}
switch (kind) { switch (kind) {
case TransferableEntryKind::Raw: { case TransferableEntryKind::Raw: {
ShaderDiskCacheRaw entry{file}; ShaderDiskCacheRaw entry;
if (!entry.Load(file)) {
LOG_ERROR(Render_OpenGL, "Failed to load transferable raw entry - skipping");
return {};
}
transferable.insert({entry.GetUniqueIdentifier(), {}}); transferable.insert({entry.GetUniqueIdentifier(), {}});
raws.push_back(std::move(entry)); raws.push_back(std::move(entry));
break; break;
} }
case TransferableEntryKind::Usage: { case TransferableEntryKind::Usage: {
ShaderDiskCacheUsage usage{}; ShaderDiskCacheUsage usage{};
file.ReadBytes(&usage, sizeof(usage)); if (file.ReadBytes(&usage, sizeof(usage)) != sizeof(usage)) {
LOG_ERROR(Render_OpenGL, "Failed to load transferable usage entry - skipping");
return {};
}
usages.push_back(std::move(usage)); usages.push_back(std::move(usage));
break; break;
} }
default: default:
LOG_ERROR(Render_OpenGL, "Unknown transferable shader cache entry kind={} - aborting", LOG_ERROR(Render_OpenGL, "Unknown transferable shader cache entry kind={} - skipping",
static_cast<u32>(kind)); static_cast<u32>(kind));
return {}; return {};
} }
@ -182,9 +212,8 @@ ShaderDiskCacheOpenGL::LoadTransferable() {
std::pair<std::map<u64, ShaderDiskCacheDecompiled>, std::pair<std::map<u64, ShaderDiskCacheDecompiled>,
std::map<ShaderDiskCacheUsage, ShaderDiskCacheDump>> std::map<ShaderDiskCacheUsage, ShaderDiskCacheDump>>
ShaderDiskCacheOpenGL::LoadPrecompiled() { ShaderDiskCacheOpenGL::LoadPrecompiled() {
if (!Settings::values.use_disk_shader_cache) { if (!IsUsable())
return {}; return {};
}
FileUtil::IOFile file(GetPrecompiledPath(), "rb"); FileUtil::IOFile file(GetPrecompiledPath(), "rb");
if (!file.IsOpen()) { if (!file.IsOpen()) {
@ -192,119 +221,75 @@ ShaderDiskCacheOpenGL::LoadPrecompiled() {
GetTitleID()); GetTitleID());
return {}; return {};
} }
const u64 file_size = file.GetSize();
char precompiled_hash[ShaderHashSize]; const auto result = LoadPrecompiledFile(file);
file.ReadBytes(&precompiled_hash, ShaderHashSize); if (!result) {
if (precompiled_hash != GetShaderHash()) { LOG_INFO(Render_OpenGL,
LOG_INFO(Render_OpenGL, "Precompiled cache is from another version of yuzu - removing"); "Failed to load precompiled cache for game with title id={} - removing",
GetTitleID());
file.Close(); file.Close();
InvalidatePrecompiled(); InvalidatePrecompiled();
return {}; return {};
} }
return *result;
}
std::optional<std::pair<std::map<u64, ShaderDiskCacheDecompiled>,
std::map<ShaderDiskCacheUsage, ShaderDiskCacheDump>>>
ShaderDiskCacheOpenGL::LoadPrecompiledFile(FileUtil::IOFile& file) {
ShaderCacheVersionHash file_hash{};
if (file.ReadArray(file_hash.data(), file_hash.size()) != file_hash.size()) {
return {};
}
if (GetShaderCacheVersionHash() != file_hash) {
LOG_INFO(Render_OpenGL, "Precompiled cache is from another version of the emulator");
return {};
}
std::map<u64, ShaderDiskCacheDecompiled> decompiled; std::map<u64, ShaderDiskCacheDecompiled> decompiled;
std::map<ShaderDiskCacheUsage, ShaderDiskCacheDump> dumps; std::map<ShaderDiskCacheUsage, ShaderDiskCacheDump> dumps;
while (file.Tell() < file_size) { while (file.Tell() < file.GetSize()) {
PrecompiledEntryKind kind{}; PrecompiledEntryKind kind{};
file.ReadBytes(&kind, sizeof(u32)); if (file.ReadBytes(&kind, sizeof(u32)) != sizeof(u32)) {
return {};
}
switch (kind) { switch (kind) {
case PrecompiledEntryKind::Decompiled: { case PrecompiledEntryKind::Decompiled: {
ShaderDiskCacheDecompiled entry;
u64 unique_identifier{}; u64 unique_identifier{};
file.ReadBytes(&unique_identifier, sizeof(u64)); if (file.ReadBytes(&unique_identifier, sizeof(u64)) != sizeof(u64))
u32 code_size{};
u32 compressed_code_size{};
file.ReadBytes(&code_size, sizeof(u32));
file.ReadBytes(&compressed_code_size, sizeof(u32));
std::vector<u8> compressed_code(compressed_code_size);
file.ReadArray(compressed_code.data(), compressed_code.size());
const std::vector<u8> code = DecompressData(compressed_code, code_size);
if (code.empty()) {
LOG_ERROR(Render_OpenGL,
"Failed to decompress GLSL code in precompiled shader={:016x} - removing",
unique_identifier);
InvalidatePrecompiled();
return {}; return {};
}
entry.code = std::string(reinterpret_cast<const char*>(code.data()), code_size);
u32 const_buffers_count{}; const auto entry = LoadDecompiledEntry(file);
file.ReadBytes(&const_buffers_count, sizeof(u32)); if (!entry)
for (u32 i = 0; i < const_buffers_count; ++i) { return {};
u32 max_offset{}, index{}; decompiled.insert({unique_identifier, std::move(*entry)});
u8 is_indirect{};
file.ReadBytes(&max_offset, sizeof(u32));
file.ReadBytes(&index, sizeof(u32));
file.ReadBytes(&is_indirect, sizeof(u8));
entry.entries.const_buffers.emplace_back(max_offset, is_indirect != 0, index);
}
u32 samplers_count{};
file.ReadBytes(&samplers_count, sizeof(u32));
for (u32 i = 0; i < samplers_count; ++i) {
u64 offset{}, index{};
u32 type{};
u8 is_array{}, is_shadow{};
file.ReadBytes(&offset, sizeof(u64));
file.ReadBytes(&index, sizeof(u64));
file.ReadBytes(&type, sizeof(u32));
file.ReadBytes(&is_array, sizeof(u8));
file.ReadBytes(&is_shadow, sizeof(u8));
entry.entries.samplers.emplace_back(
static_cast<std::size_t>(offset), static_cast<std::size_t>(index),
static_cast<Tegra::Shader::TextureType>(type), is_array != 0, is_shadow != 0);
}
u32 global_memory_count{};
file.ReadBytes(&global_memory_count, sizeof(u32));
for (u32 i = 0; i < global_memory_count; ++i) {
u32 cbuf_index{}, cbuf_offset{};
file.ReadBytes(&cbuf_index, sizeof(u32));
file.ReadBytes(&cbuf_offset, sizeof(u32));
entry.entries.global_memory_entries.emplace_back(cbuf_index, cbuf_offset);
}
for (auto& clip_distance : entry.entries.clip_distances) {
u8 clip_distance_raw{};
file.ReadBytes(&clip_distance_raw, sizeof(u8));
clip_distance = clip_distance_raw != 0;
}
u64 shader_length{};
file.ReadBytes(&shader_length, sizeof(u64));
entry.entries.shader_length = static_cast<std::size_t>(shader_length);
decompiled.insert({unique_identifier, std::move(entry)});
break; break;
} }
case PrecompiledEntryKind::Dump: { case PrecompiledEntryKind::Dump: {
ShaderDiskCacheUsage usage; ShaderDiskCacheUsage usage;
file.ReadBytes(&usage, sizeof(usage)); if (file.ReadBytes(&usage, sizeof(usage)) != sizeof(usage))
return {};
ShaderDiskCacheDump dump; ShaderDiskCacheDump dump;
file.ReadBytes(&dump.binary_format, sizeof(u32)); if (file.ReadBytes(&dump.binary_format, sizeof(u32)) != sizeof(u32))
return {};
u32 binary_length{}; u32 binary_length{};
u32 compressed_size{}; u32 compressed_size{};
file.ReadBytes(&binary_length, sizeof(u32)); if (file.ReadBytes(&binary_length, sizeof(u32)) != sizeof(u32) ||
file.ReadBytes(&compressed_size, sizeof(u32)); file.ReadBytes(&compressed_size, sizeof(u32)) != sizeof(u32)) {
return {};
}
std::vector<u8> compressed_binary(compressed_size); std::vector<u8> compressed_binary(compressed_size);
file.ReadArray(compressed_binary.data(), compressed_binary.size()); if (file.ReadArray(compressed_binary.data(), compressed_binary.size()) !=
compressed_binary.size()) {
return {};
}
dump.binary = DecompressData(compressed_binary, binary_length); dump.binary = DecompressData(compressed_binary, binary_length);
if (dump.binary.empty()) { if (dump.binary.empty()) {
LOG_ERROR(Render_OpenGL,
"Failed to decompress precompiled binary program - removing");
InvalidatePrecompiled();
return {}; return {};
} }
@ -312,28 +297,165 @@ ShaderDiskCacheOpenGL::LoadPrecompiled() {
break; break;
} }
default: default:
LOG_ERROR(Render_OpenGL, "Unknown precompiled shader cache entry kind={} - removing",
static_cast<u32>(kind));
InvalidatePrecompiled();
return {}; return {};
} }
} }
return {decompiled, dumps}; return {{decompiled, dumps}};
} }
bool ShaderDiskCacheOpenGL::InvalidateTransferable() const { std::optional<ShaderDiskCacheDecompiled> ShaderDiskCacheOpenGL::LoadDecompiledEntry(
const bool success = FileUtil::Delete(GetTransferablePath()); FileUtil::IOFile& file) {
return InvalidatePrecompiled() && success; u32 code_size{};
u32 compressed_code_size{};
if (file.ReadBytes(&code_size, sizeof(u32)) != sizeof(u32) ||
file.ReadBytes(&compressed_code_size, sizeof(u32)) != sizeof(u32)) {
return {};
} }
bool ShaderDiskCacheOpenGL::InvalidatePrecompiled() const { std::vector<u8> compressed_code(compressed_code_size);
return FileUtil::Delete(GetPrecompiledPath()); if (file.ReadArray(compressed_code.data(), compressed_code.size()) != compressed_code.size()) {
return {};
}
const std::vector<u8> code = DecompressData(compressed_code, code_size);
if (code.empty()) {
return {};
}
ShaderDiskCacheDecompiled entry;
entry.code = std::string(reinterpret_cast<const char*>(code.data()), code_size);
u32 const_buffers_count{};
if (file.ReadBytes(&const_buffers_count, sizeof(u32)) != sizeof(u32))
return {};
for (u32 i = 0; i < const_buffers_count; ++i) {
u32 max_offset{};
u32 index{};
u8 is_indirect{};
if (file.ReadBytes(&max_offset, sizeof(u32)) != sizeof(u32) ||
file.ReadBytes(&index, sizeof(u32)) != sizeof(u32) ||
file.ReadBytes(&is_indirect, sizeof(u8)) != sizeof(u8)) {
return {};
}
entry.entries.const_buffers.emplace_back(max_offset, is_indirect != 0, index);
}
u32 samplers_count{};
if (file.ReadBytes(&samplers_count, sizeof(u32)) != sizeof(u32))
return {};
for (u32 i = 0; i < samplers_count; ++i) {
u64 offset{};
u64 index{};
u32 type{};
u8 is_array{};
u8 is_shadow{};
if (file.ReadBytes(&offset, sizeof(u64)) != sizeof(u64) ||
file.ReadBytes(&index, sizeof(u64)) != sizeof(u64) ||
file.ReadBytes(&type, sizeof(u32)) != sizeof(u32) ||
file.ReadBytes(&is_array, sizeof(u8)) != sizeof(u8) ||
file.ReadBytes(&is_shadow, sizeof(u8)) != sizeof(u8)) {
return {};
}
entry.entries.samplers.emplace_back(
static_cast<std::size_t>(offset), static_cast<std::size_t>(index),
static_cast<Tegra::Shader::TextureType>(type), is_array != 0, is_shadow != 0);
}
u32 global_memory_count{};
if (file.ReadBytes(&global_memory_count, sizeof(u32)) != sizeof(u32))
return {};
for (u32 i = 0; i < global_memory_count; ++i) {
u32 cbuf_index{};
u32 cbuf_offset{};
if (file.ReadBytes(&cbuf_index, sizeof(u32)) != sizeof(u32) ||
file.ReadBytes(&cbuf_offset, sizeof(u32)) != sizeof(u32)) {
return {};
}
entry.entries.global_memory_entries.emplace_back(cbuf_index, cbuf_offset);
}
for (auto& clip_distance : entry.entries.clip_distances) {
u8 clip_distance_raw{};
if (file.ReadBytes(&clip_distance_raw, sizeof(u8)) != sizeof(u8))
return {};
clip_distance = clip_distance_raw != 0;
}
u64 shader_length{};
if (file.ReadBytes(&shader_length, sizeof(u64)) != sizeof(u64))
return {};
entry.entries.shader_length = static_cast<std::size_t>(shader_length);
return entry;
}
bool ShaderDiskCacheOpenGL::SaveDecompiledFile(FileUtil::IOFile& file, u64 unique_identifier,
const std::string& code,
const std::vector<u8>& compressed_code,
const GLShader::ShaderEntries& entries) {
if (file.WriteObject(static_cast<u32>(PrecompiledEntryKind::Decompiled)) != 1 ||
file.WriteObject(unique_identifier) != 1 ||
file.WriteObject(static_cast<u32>(code.size())) != 1 ||
file.WriteObject(static_cast<u32>(compressed_code.size())) != 1 ||
file.WriteArray(compressed_code.data(), compressed_code.size()) != compressed_code.size()) {
return false;
}
if (file.WriteObject(static_cast<u32>(entries.const_buffers.size())) != 1)
return false;
for (const auto& cbuf : entries.const_buffers) {
if (file.WriteObject(static_cast<u32>(cbuf.GetMaxOffset())) != 1 ||
file.WriteObject(static_cast<u32>(cbuf.GetIndex())) != 1 ||
file.WriteObject(static_cast<u8>(cbuf.IsIndirect() ? 1 : 0)) != 1) {
return false;
}
}
if (file.WriteObject(static_cast<u32>(entries.samplers.size())) != 1)
return false;
for (const auto& sampler : entries.samplers) {
if (file.WriteObject(static_cast<u64>(sampler.GetOffset())) != 1 ||
file.WriteObject(static_cast<u64>(sampler.GetIndex())) != 1 ||
file.WriteObject(static_cast<u32>(sampler.GetType())) != 1 ||
file.WriteObject(static_cast<u8>(sampler.IsArray() ? 1 : 0)) != 1 ||
file.WriteObject(static_cast<u8>(sampler.IsShadow() ? 1 : 0)) != 1) {
return false;
}
}
if (file.WriteObject(static_cast<u32>(entries.global_memory_entries.size())) != 1)
return false;
for (const auto& gmem : entries.global_memory_entries) {
if (file.WriteObject(static_cast<u32>(gmem.GetCbufIndex())) != 1 ||
file.WriteObject(static_cast<u32>(gmem.GetCbufOffset())) != 1) {
return false;
}
}
for (const bool clip_distance : entries.clip_distances) {
if (file.WriteObject(static_cast<u8>(clip_distance ? 1 : 0)) != 1)
return false;
}
return file.WriteObject(static_cast<u64>(entries.shader_length)) == 1;
}
void ShaderDiskCacheOpenGL::InvalidateTransferable() const {
if (!FileUtil::Delete(GetTransferablePath())) {
LOG_ERROR(Render_OpenGL, "Failed to invalidate transferable file={}",
GetTransferablePath());
}
InvalidatePrecompiled();
}
void ShaderDiskCacheOpenGL::InvalidatePrecompiled() const {
if (!FileUtil::Delete(GetPrecompiledPath())) {
LOG_ERROR(Render_OpenGL, "Failed to invalidate precompiled file={}", GetPrecompiledPath());
}
} }
void ShaderDiskCacheOpenGL::SaveRaw(const ShaderDiskCacheRaw& entry) { void ShaderDiskCacheOpenGL::SaveRaw(const ShaderDiskCacheRaw& entry) {
if (!Settings::values.use_disk_shader_cache) { if (!IsUsable())
return; return;
}
const u64 id = entry.GetUniqueIdentifier(); const u64 id = entry.GetUniqueIdentifier();
if (transferable.find(id) != transferable.end()) { if (transferable.find(id) != transferable.end()) {
@ -342,47 +464,44 @@ void ShaderDiskCacheOpenGL::SaveRaw(const ShaderDiskCacheRaw& entry) {
} }
FileUtil::IOFile file = AppendTransferableFile(); FileUtil::IOFile file = AppendTransferableFile();
if (!file.IsOpen()) { if (!file.IsOpen())
return;
if (file.WriteObject(TransferableEntryKind::Raw) != 1 || !entry.Save(file)) {
LOG_ERROR(Render_OpenGL, "Failed to save raw transferable cache entry - removing");
file.Close();
InvalidateTransferable();
return; return;
} }
file.WriteObject(TransferableEntryKind::Raw);
entry.Save(file);
transferable.insert({id, {}}); transferable.insert({id, {}});
} }
void ShaderDiskCacheOpenGL::SaveUsage(const ShaderDiskCacheUsage& usage) { void ShaderDiskCacheOpenGL::SaveUsage(const ShaderDiskCacheUsage& usage) {
if (!Settings::values.use_disk_shader_cache) { if (!IsUsable())
return; return;
}
const auto it = transferable.find(usage.unique_identifier); const auto it = transferable.find(usage.unique_identifier);
if (it == transferable.end()) { ASSERT_MSG(it != transferable.end(), "Saving shader usage without storing raw previously");
LOG_CRITICAL(Render_OpenGL, "Saving shader usage without storing raw previously");
UNREACHABLE();
}
auto& usages{it->second}; auto& usages{it->second};
ASSERT(usages.find(usage) == usages.end()); ASSERT(usages.find(usage) == usages.end());
usages.insert(usage); usages.insert(usage);
FileUtil::IOFile file = AppendTransferableFile(); FileUtil::IOFile file = AppendTransferableFile();
if (!file.IsOpen()) { if (!file.IsOpen())
return;
if (file.WriteObject(TransferableEntryKind::Usage) != 1 || file.WriteObject(usage) != 1) {
LOG_ERROR(Render_OpenGL, "Failed to save usage transferable cache entry - removing");
file.Close();
InvalidateTransferable();
return; return;
} }
file.WriteObject(TransferableEntryKind::Usage);
file.WriteObject(usage);
} }
void ShaderDiskCacheOpenGL::SaveDecompiled(u64 unique_identifier, const std::string& code, void ShaderDiskCacheOpenGL::SaveDecompiled(u64 unique_identifier, const std::string& code,
const GLShader::ShaderEntries& entries) { const GLShader::ShaderEntries& entries) {
if (!Settings::values.use_disk_shader_cache) { if (!IsUsable())
return; return;
}
FileUtil::IOFile file = AppendPrecompiledFile();
if (!file.IsOpen()) {
return;
}
const std::vector<u8> compressed_code{CompressData(code.data(), code.size())}; const std::vector<u8> compressed_code{CompressData(code.data(), code.size())};
if (compressed_code.empty()) { if (compressed_code.empty()) {
@ -391,52 +510,21 @@ void ShaderDiskCacheOpenGL::SaveDecompiled(u64 unique_identifier, const std::str
return; return;
} }
file.WriteObject(static_cast<u32>(PrecompiledEntryKind::Decompiled)); FileUtil::IOFile file = AppendPrecompiledFile();
if (!file.IsOpen())
return;
file.WriteObject(unique_identifier); if (!SaveDecompiledFile(file, unique_identifier, code, compressed_code, entries)) {
LOG_ERROR(Render_OpenGL,
file.WriteObject(static_cast<u32>(code.size())); "Failed to save decompiled entry to the precompiled file - removing");
file.WriteObject(static_cast<u32>(compressed_code.size())); file.Close();
file.WriteArray(compressed_code.data(), compressed_code.size()); InvalidatePrecompiled();
file.WriteObject(static_cast<u32>(entries.const_buffers.size()));
for (const auto& cbuf : entries.const_buffers) {
file.WriteObject(static_cast<u32>(cbuf.GetMaxOffset()));
file.WriteObject(static_cast<u32>(cbuf.GetIndex()));
file.WriteObject(static_cast<u8>(cbuf.IsIndirect() ? 1 : 0));
} }
file.WriteObject(static_cast<u32>(entries.samplers.size()));
for (const auto& sampler : entries.samplers) {
file.WriteObject(static_cast<u64>(sampler.GetOffset()));
file.WriteObject(static_cast<u64>(sampler.GetIndex()));
file.WriteObject(static_cast<u32>(sampler.GetType()));
file.WriteObject(static_cast<u8>(sampler.IsArray() ? 1 : 0));
file.WriteObject(static_cast<u8>(sampler.IsShadow() ? 1 : 0));
}
file.WriteObject(static_cast<u32>(entries.global_memory_entries.size()));
for (const auto& gmem : entries.global_memory_entries) {
file.WriteObject(static_cast<u32>(gmem.GetCbufIndex()));
file.WriteObject(static_cast<u32>(gmem.GetCbufOffset()));
}
for (const bool clip_distance : entries.clip_distances) {
file.WriteObject(static_cast<u8>(clip_distance ? 1 : 0));
}
file.WriteObject(static_cast<u64>(entries.shader_length));
} }
void ShaderDiskCacheOpenGL::SaveDump(const ShaderDiskCacheUsage& usage, GLuint program) { void ShaderDiskCacheOpenGL::SaveDump(const ShaderDiskCacheUsage& usage, GLuint program) {
if (!Settings::values.use_disk_shader_cache) { 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);
@ -452,20 +540,31 @@ void ShaderDiskCacheOpenGL::SaveDump(const ShaderDiskCacheUsage& usage, GLuint p
return; return;
} }
file.WriteObject(static_cast<u32>(PrecompiledEntryKind::Dump)); FileUtil::IOFile file = AppendPrecompiledFile();
if (!file.IsOpen())
return;
file.WriteObject(usage); if (file.WriteObject(static_cast<u32>(PrecompiledEntryKind::Dump)) != 1 ||
file.WriteObject(usage) != 1 || file.WriteObject(static_cast<u32>(binary_format)) != 1 ||
file.WriteObject(static_cast<u32>(binary_length)) != 1 ||
file.WriteObject(static_cast<u32>(compressed_binary.size())) != 1 ||
file.WriteArray(compressed_binary.data(), compressed_binary.size()) !=
compressed_binary.size()) {
LOG_ERROR(Render_OpenGL, "Failed to save binary program file in shader={:016x} - removing",
usage.unique_identifier);
file.Close();
InvalidatePrecompiled();
return;
}
}
file.WriteObject(static_cast<u32>(binary_format)); bool ShaderDiskCacheOpenGL::IsUsable() const {
file.WriteObject(static_cast<u32>(binary_length)); return tried_to_load && Settings::values.use_disk_shader_cache;
file.WriteObject(static_cast<u32>(compressed_binary.size()));
file.WriteArray(compressed_binary.data(), compressed_binary.size());
} }
FileUtil::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const { FileUtil::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const {
if (!EnsureDirectories()) { if (!EnsureDirectories())
return {}; return {};
}
const auto transferable_path{GetTransferablePath()}; const auto transferable_path{GetTransferablePath()};
const bool existed = FileUtil::Exists(transferable_path); const bool existed = FileUtil::Exists(transferable_path);
@ -477,15 +576,18 @@ FileUtil::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const {
} }
if (!existed || file.GetSize() == 0) { if (!existed || file.GetSize() == 0) {
// If the file didn't exist, write its version // If the file didn't exist, write its version
file.WriteObject(NativeVersion); if (file.WriteObject(NativeVersion) != 1) {
LOG_ERROR(Render_OpenGL, "Failed to write transferable cache version in path={}",
transferable_path);
return {};
}
} }
return file; return file;
} }
FileUtil::IOFile ShaderDiskCacheOpenGL::AppendPrecompiledFile() const { FileUtil::IOFile ShaderDiskCacheOpenGL::AppendPrecompiledFile() const {
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);
@ -497,9 +599,12 @@ FileUtil::IOFile ShaderDiskCacheOpenGL::AppendPrecompiledFile() const {
} }
if (!existed || file.GetSize() == 0) { if (!existed || file.GetSize() == 0) {
std::array<char, ShaderHashSize> hash{}; const auto hash{GetShaderCacheVersionHash()};
std::strcpy(hash.data(), GetShaderHash().c_str()); if (file.WriteArray(hash.data(), hash.size()) != hash.size()) {
file.WriteArray(hash.data(), hash.size()); LOG_ERROR(Render_OpenGL, "Failed to write precompiled cache version hash in path={}",
precompiled_path);
return {};
}
} }
return file; return file;
} }

View File

@ -53,15 +53,15 @@ struct BaseBindings {
/// Describes a shader how it's used by the guest GPU /// Describes a shader how it's used by the guest GPU
class ShaderDiskCacheRaw { class ShaderDiskCacheRaw {
public: public:
explicit ShaderDiskCacheRaw(FileUtil::IOFile& file);
explicit ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type, explicit ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type,
u32 program_code_size, u32 program_code_size_b, u32 program_code_size, u32 program_code_size_b,
ProgramCode program_code, ProgramCode program_code_b); ProgramCode program_code, ProgramCode program_code_b);
ShaderDiskCacheRaw();
~ShaderDiskCacheRaw(); ~ShaderDiskCacheRaw();
void Save(FileUtil::IOFile& file) const; bool Load(FileUtil::IOFile& file);
bool Save(FileUtil::IOFile& file) const;
u64 GetUniqueIdentifier() const { u64 GetUniqueIdentifier() const {
return unique_identifier; return unique_identifier;
@ -158,10 +158,10 @@ public:
LoadPrecompiled(); LoadPrecompiled();
/// Removes the transferable (and precompiled) cache file. /// Removes the transferable (and precompiled) cache file.
bool InvalidateTransferable() const; void InvalidateTransferable() const;
/// Removes the precompiled cache file. /// Removes the precompiled cache file.
bool InvalidatePrecompiled() const; void InvalidatePrecompiled() const;
/// Saves a raw dump to the transferable file. Checks for collisions. /// Saves a raw dump to the transferable file. Checks for collisions.
void SaveRaw(const ShaderDiskCacheRaw& entry); void SaveRaw(const ShaderDiskCacheRaw& entry);
@ -177,6 +177,22 @@ public:
void SaveDump(const ShaderDiskCacheUsage& usage, GLuint program); void SaveDump(const ShaderDiskCacheUsage& usage, GLuint program);
private: private:
/// Loads the transferable cache. Returns empty on failure.
std::optional<std::pair<std::map<u64, ShaderDiskCacheDecompiled>,
std::map<ShaderDiskCacheUsage, ShaderDiskCacheDump>>>
LoadPrecompiledFile(FileUtil::IOFile& file);
/// Loads a decompiled cache entry from the passed file. Returns empty on failure.
std::optional<ShaderDiskCacheDecompiled> LoadDecompiledEntry(FileUtil::IOFile& file);
/// Saves a decompiled entry to the passed file. Returns true on success.
bool SaveDecompiledFile(FileUtil::IOFile& file, u64 unique_identifier, const std::string& code,
const std::vector<u8>& compressed_code,
const GLShader::ShaderEntries& entries);
/// 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() const; FileUtil::IOFile AppendTransferableFile() const;
@ -203,6 +219,8 @@ private:
// Stored transferable shaders // Stored transferable shaders
std::map<u64, std::set<ShaderDiskCacheUsage>> transferable; std::map<u64, std::set<ShaderDiskCacheUsage>> transferable;
// The cache has been loaded at boot
bool tried_to_load{};
}; };
} // namespace OpenGL } // namespace OpenGL