2019-01-14 02:36:52 +01:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-01-14 05:58:46 +01:00
|
|
|
#include <cstring>
|
2019-01-14 03:22:15 +01:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_paths.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "common/logging/log.h"
|
2019-01-14 05:58:46 +01:00
|
|
|
#include "common/scm_rev.h"
|
2019-02-10 16:38:34 +01:00
|
|
|
#include "common/zstd_compression.h"
|
2019-01-14 03:22:15 +01:00
|
|
|
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/hle/kernel/process.h"
|
2019-01-14 06:14:27 +01:00
|
|
|
#include "core/settings.h"
|
2019-01-14 03:22:15 +01:00
|
|
|
|
|
|
|
#include "video_core/renderer_opengl/gl_shader_cache.h"
|
2019-01-14 02:36:52 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_disk_cache.h"
|
|
|
|
|
|
|
|
namespace OpenGL {
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
using ShaderCacheVersionHash = std::array<u8, 64>;
|
|
|
|
|
2019-01-15 05:07:57 +01:00
|
|
|
enum class TransferableEntryKind : u32 {
|
2019-01-14 03:29:24 +01:00
|
|
|
Raw,
|
|
|
|
Usage,
|
|
|
|
};
|
|
|
|
|
2019-01-15 05:07:57 +01:00
|
|
|
enum class PrecompiledEntryKind : u32 {
|
|
|
|
Decompiled,
|
|
|
|
Dump,
|
|
|
|
};
|
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
constexpr u32 NativeVersion = 1;
|
2019-01-14 03:33:00 +01:00
|
|
|
|
2019-01-14 02:36:52 +01:00
|
|
|
// Making sure sizes doesn't change by accident
|
|
|
|
static_assert(sizeof(BaseBindings) == 12);
|
2019-01-14 03:29:24 +01:00
|
|
|
static_assert(sizeof(ShaderDiskCacheUsage) == 24);
|
2019-01-14 02:36:52 +01:00
|
|
|
|
2019-01-14 03:22:15 +01:00
|
|
|
namespace {
|
2019-01-14 05:58:46 +01:00
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
ShaderCacheVersionHash GetShaderCacheVersionHash() {
|
|
|
|
ShaderCacheVersionHash hash{};
|
|
|
|
const std::size_t length = std::min(std::strlen(Common::g_shader_cache_version), hash.size());
|
|
|
|
std::memcpy(hash.data(), Common::g_shader_cache_version, length);
|
|
|
|
return hash;
|
2019-01-14 05:58:46 +01:00
|
|
|
}
|
2019-01-15 05:48:29 +01:00
|
|
|
|
2019-01-14 03:22:15 +01:00
|
|
|
} // namespace
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
ShaderDiskCacheRaw::ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type,
|
|
|
|
u32 program_code_size, u32 program_code_size_b,
|
|
|
|
ProgramCode program_code, ProgramCode program_code_b)
|
|
|
|
: unique_identifier{unique_identifier}, program_type{program_type},
|
|
|
|
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)} {}
|
|
|
|
|
|
|
|
ShaderDiskCacheRaw::ShaderDiskCacheRaw() = default;
|
2019-01-14 03:29:24 +01:00
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
ShaderDiskCacheRaw::~ShaderDiskCacheRaw() = default;
|
|
|
|
|
|
|
|
bool ShaderDiskCacheRaw::Load(FileUtil::IOFile& file) {
|
|
|
|
if (file.ReadBytes(&unique_identifier, sizeof(u64)) != sizeof(u64) ||
|
|
|
|
file.ReadBytes(&program_type, sizeof(u32)) != sizeof(u32)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 06:42:25 +01:00
|
|
|
u32 program_code_size{};
|
|
|
|
u32 program_code_size_b{};
|
2019-01-15 19:22:25 +01:00
|
|
|
if (file.ReadBytes(&program_code_size, sizeof(u32)) != sizeof(u32) ||
|
|
|
|
file.ReadBytes(&program_code_size_b, sizeof(u32)) != sizeof(u32)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-14 03:29:24 +01:00
|
|
|
|
|
|
|
program_code.resize(program_code_size);
|
|
|
|
program_code_b.resize(program_code_size_b);
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
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;
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
return true;
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
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;
|
|
|
|
}
|
2019-01-15 06:42:25 +01:00
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
if (file.WriteArray(program_code.data(), program_code_size) != program_code_size)
|
|
|
|
return false;
|
2019-01-14 03:29:24 +01:00
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
if (HasProgramA() &&
|
|
|
|
file.WriteArray(program_code_b.data(), program_code_size_b) != program_code_size_b) {
|
|
|
|
return false;
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
return true;
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
ShaderDiskCacheOpenGL::ShaderDiskCacheOpenGL(Core::System& system)
|
|
|
|
: system{system}, precompiled_cache_virtual_file_offset{0} {}
|
2019-01-15 20:28:42 +01:00
|
|
|
|
2019-01-15 06:17:38 +01:00
|
|
|
std::optional<std::pair<std::vector<ShaderDiskCacheRaw>, std::vector<ShaderDiskCacheUsage>>>
|
|
|
|
ShaderDiskCacheOpenGL::LoadTransferable() {
|
2019-01-15 20:28:42 +01:00
|
|
|
// Skip games without title id
|
|
|
|
const bool has_title_id = system.CurrentProcess()->GetTitleID() != 0;
|
|
|
|
if (!Settings::values.use_disk_shader_cache || !has_title_id)
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-15 19:22:25 +01:00
|
|
|
tried_to_load = true;
|
2019-01-14 06:14:27 +01:00
|
|
|
|
2019-01-14 03:30:37 +01:00
|
|
|
FileUtil::IOFile file(GetTransferablePath(), "rb");
|
|
|
|
if (!file.IsOpen()) {
|
|
|
|
LOG_INFO(Render_OpenGL, "No transferable shader cache found for game with title id={}",
|
|
|
|
GetTitleID());
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-14 03:30:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 version{};
|
2019-01-15 19:22:25 +01:00
|
|
|
if (file.ReadBytes(&version, sizeof(version)) != sizeof(version)) {
|
|
|
|
LOG_ERROR(Render_OpenGL,
|
|
|
|
"Failed to get transferable cache version for title id={} - skipping",
|
|
|
|
GetTitleID());
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-14 03:30:37 +01:00
|
|
|
|
|
|
|
if (version < NativeVersion) {
|
|
|
|
LOG_INFO(Render_OpenGL, "Transferable shader cache is old - removing");
|
|
|
|
file.Close();
|
2019-01-15 19:22:25 +01:00
|
|
|
InvalidateTransferable();
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-14 03:30:37 +01:00
|
|
|
}
|
|
|
|
if (version > NativeVersion) {
|
|
|
|
LOG_WARNING(Render_OpenGL, "Transferable shader cache was generated with a newer version "
|
|
|
|
"of the emulator - skipping");
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-14 03:30:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Version is valid, load the shaders
|
2019-01-15 06:17:38 +01:00
|
|
|
std::vector<ShaderDiskCacheRaw> raws;
|
|
|
|
std::vector<ShaderDiskCacheUsage> usages;
|
2019-01-15 19:22:25 +01:00
|
|
|
while (file.Tell() < file.GetSize()) {
|
2019-01-15 05:07:57 +01:00
|
|
|
TransferableEntryKind kind{};
|
2019-01-15 19:22:25 +01:00
|
|
|
if (file.ReadBytes(&kind, sizeof(u32)) != sizeof(u32)) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to read transferable file - skipping");
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-14 03:30:37 +01:00
|
|
|
|
|
|
|
switch (kind) {
|
2019-01-15 05:07:57 +01:00
|
|
|
case TransferableEntryKind::Raw: {
|
2019-01-15 19:22:25 +01:00
|
|
|
ShaderDiskCacheRaw entry;
|
|
|
|
if (!entry.Load(file)) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to load transferable raw entry - skipping");
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-14 03:30:37 +01:00
|
|
|
transferable.insert({entry.GetUniqueIdentifier(), {}});
|
|
|
|
raws.push_back(std::move(entry));
|
|
|
|
break;
|
|
|
|
}
|
2019-01-15 05:07:57 +01:00
|
|
|
case TransferableEntryKind::Usage: {
|
2019-01-14 03:30:37 +01:00
|
|
|
ShaderDiskCacheUsage usage{};
|
2019-01-15 19:22:25 +01:00
|
|
|
if (file.ReadBytes(&usage, sizeof(usage)) != sizeof(usage)) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to load transferable usage entry - skipping");
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-14 03:30:37 +01:00
|
|
|
usages.push_back(std::move(usage));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2019-01-15 19:22:25 +01:00
|
|
|
LOG_ERROR(Render_OpenGL, "Unknown transferable shader cache entry kind={} - skipping",
|
2019-01-14 03:30:37 +01:00
|
|
|
static_cast<u32>(kind));
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-14 03:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-25 11:47:23 +01:00
|
|
|
|
2019-01-15 06:17:38 +01:00
|
|
|
return {{raws, usages}};
|
2019-01-14 03:30:37 +01:00
|
|
|
}
|
|
|
|
|
2019-02-03 01:14:36 +01:00
|
|
|
std::pair<std::unordered_map<u64, ShaderDiskCacheDecompiled>,
|
|
|
|
std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump>>
|
2019-01-15 06:17:38 +01:00
|
|
|
ShaderDiskCacheOpenGL::LoadPrecompiled() {
|
2019-01-15 19:22:25 +01:00
|
|
|
if (!IsUsable())
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-14 06:14:27 +01:00
|
|
|
|
2019-01-14 03:33:31 +01:00
|
|
|
FileUtil::IOFile file(GetPrecompiledPath(), "rb");
|
|
|
|
if (!file.IsOpen()) {
|
|
|
|
LOG_INFO(Render_OpenGL, "No precompiled shader cache found for game with title id={}",
|
|
|
|
GetTitleID());
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-14 03:33:31 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
const auto result = LoadPrecompiledFile(file);
|
|
|
|
if (!result) {
|
|
|
|
LOG_INFO(Render_OpenGL,
|
|
|
|
"Failed to load precompiled cache for game with title id={} - removing",
|
|
|
|
GetTitleID());
|
2019-01-14 03:33:31 +01:00
|
|
|
file.Close();
|
|
|
|
InvalidatePrecompiled();
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-14 03:33:31 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
2019-02-03 01:14:36 +01:00
|
|
|
std::optional<std::pair<std::unordered_map<u64, ShaderDiskCacheDecompiled>,
|
|
|
|
std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump>>>
|
2019-01-15 19:22:25 +01:00
|
|
|
ShaderDiskCacheOpenGL::LoadPrecompiledFile(FileUtil::IOFile& file) {
|
2019-02-10 16:38:34 +01:00
|
|
|
// Read compressed file from disk and decompress to virtual precompiled cache file
|
|
|
|
std::vector<u8> compressed(file.GetSize());
|
|
|
|
file.ReadBytes(compressed.data(), compressed.size());
|
|
|
|
const std::vector<u8> decompressed = Common::Compression::DecompressDataZSTD(compressed);
|
|
|
|
SaveArrayToPrecompiled(decompressed.data(), decompressed.size());
|
2019-02-25 11:47:23 +01:00
|
|
|
precompiled_cache_virtual_file_offset = 0;
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
ShaderCacheVersionHash file_hash{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadArrayFromPrecompiled(file_hash.data(), file_hash.size())) {
|
|
|
|
precompiled_cache_virtual_file_offset = 0;
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (GetShaderCacheVersionHash() != file_hash) {
|
|
|
|
LOG_INFO(Render_OpenGL, "Precompiled cache is from another version of the emulator");
|
2019-02-25 11:47:23 +01:00
|
|
|
precompiled_cache_virtual_file_offset = 0;
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-01-14 03:33:31 +01:00
|
|
|
|
2019-02-03 01:14:36 +01:00
|
|
|
std::unordered_map<u64, ShaderDiskCacheDecompiled> decompiled;
|
|
|
|
std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump> dumps;
|
2019-02-25 11:47:23 +01:00
|
|
|
while (precompiled_cache_virtual_file_offset < precompiled_cache_virtual_file.GetSize()) {
|
2019-01-15 05:07:57 +01:00
|
|
|
PrecompiledEntryKind kind{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(kind)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-01-14 03:33:31 +01:00
|
|
|
|
2019-01-15 05:07:57 +01:00
|
|
|
switch (kind) {
|
|
|
|
case PrecompiledEntryKind::Decompiled: {
|
|
|
|
u64 unique_identifier{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(unique_identifier)) {
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2019-01-15 05:07:57 +01:00
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
const auto entry = LoadDecompiledEntry();
|
|
|
|
if (!entry) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
decompiled.insert({unique_identifier, std::move(*entry)});
|
2019-01-15 05:07:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PrecompiledEntryKind::Dump: {
|
|
|
|
ShaderDiskCacheUsage usage;
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(usage)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2019-01-15 05:07:57 +01:00
|
|
|
|
|
|
|
ShaderDiskCacheDump dump;
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(dump.binary_format)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2019-01-14 03:33:31 +01:00
|
|
|
|
2019-01-15 05:07:57 +01:00
|
|
|
u32 binary_length{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(binary_length)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-01-15 06:05:30 +01:00
|
|
|
|
2019-02-09 15:36:50 +01:00
|
|
|
dump.binary.resize(binary_length);
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadArrayFromPrecompiled(dump.binary.data(), dump.binary.size())) {
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-15 06:05:30 +01:00
|
|
|
}
|
2019-01-14 03:33:31 +01:00
|
|
|
|
2019-01-15 05:07:57 +01:00
|
|
|
dumps.insert({usage, dump});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-15 05:07:57 +01:00
|
|
|
}
|
2019-01-14 03:33:31 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
return {{decompiled, dumps}};
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
std::optional<ShaderDiskCacheDecompiled> ShaderDiskCacheOpenGL::LoadDecompiledEntry() {
|
2019-01-15 19:22:25 +01:00
|
|
|
u32 code_size{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(code_size)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-02-09 15:36:50 +01:00
|
|
|
std::vector<u8> code(code_size);
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadArrayFromPrecompiled(code.data(), code.size())) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderDiskCacheDecompiled entry;
|
|
|
|
entry.code = std::string(reinterpret_cast<const char*>(code.data()), code_size);
|
|
|
|
|
|
|
|
u32 const_buffers_count{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(const_buffers_count)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
for (u32 i = 0; i < const_buffers_count; ++i) {
|
|
|
|
u32 max_offset{};
|
|
|
|
u32 index{};
|
2019-05-19 08:27:00 +02:00
|
|
|
bool is_indirect{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(max_offset) || !LoadObjectFromPrecompiled(index) ||
|
|
|
|
!LoadObjectFromPrecompiled(is_indirect)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-05-19 08:27:00 +02:00
|
|
|
entry.entries.const_buffers.emplace_back(max_offset, is_indirect, index);
|
2019-01-15 19:22:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 samplers_count{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(samplers_count)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
for (u32 i = 0; i < samplers_count; ++i) {
|
|
|
|
u64 offset{};
|
|
|
|
u64 index{};
|
|
|
|
u32 type{};
|
2019-05-19 08:27:00 +02:00
|
|
|
bool is_array{};
|
|
|
|
bool is_shadow{};
|
|
|
|
bool is_bindless{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(offset) || !LoadObjectFromPrecompiled(index) ||
|
|
|
|
!LoadObjectFromPrecompiled(type) || !LoadObjectFromPrecompiled(is_array) ||
|
|
|
|
!LoadObjectFromPrecompiled(is_shadow) || !LoadObjectFromPrecompiled(is_bindless)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-05-19 08:27:00 +02:00
|
|
|
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, is_shadow, is_bindless);
|
2019-01-15 19:22:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 global_memory_count{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(global_memory_count)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
for (u32 i = 0; i < global_memory_count; ++i) {
|
|
|
|
u32 cbuf_index{};
|
|
|
|
u32 cbuf_offset{};
|
2019-05-19 08:27:00 +02:00
|
|
|
bool is_read{};
|
|
|
|
bool is_written{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(cbuf_index) || !LoadObjectFromPrecompiled(cbuf_offset) ||
|
|
|
|
!LoadObjectFromPrecompiled(is_read) || !LoadObjectFromPrecompiled(is_written)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-05-19 08:27:00 +02:00
|
|
|
entry.entries.global_memory_entries.emplace_back(cbuf_index, cbuf_offset, is_read,
|
|
|
|
is_written);
|
2019-01-15 19:22:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& clip_distance : entry.entries.clip_distances) {
|
2019-05-19 08:27:00 +02:00
|
|
|
if (!LoadObjectFromPrecompiled(clip_distance)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
2019-05-19 08:27:00 +02:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 shader_length{};
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!LoadObjectFromPrecompiled(shader_length)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
entry.entries.shader_length = static_cast<std::size_t>(shader_length);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
bool ShaderDiskCacheOpenGL::SaveDecompiledFile(u64 unique_identifier, const std::string& code,
|
2019-01-15 19:22:25 +01:00
|
|
|
const GLShader::ShaderEntries& entries) {
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u32>(PrecompiledEntryKind::Decompiled)) ||
|
|
|
|
!SaveObjectToPrecompiled(unique_identifier) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(code.size())) ||
|
|
|
|
!SaveArrayToPrecompiled(code.data(), code.size())) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u32>(entries.const_buffers.size()))) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
for (const auto& cbuf : entries.const_buffers) {
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u32>(cbuf.GetMaxOffset())) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(cbuf.GetIndex())) ||
|
2019-05-19 08:27:00 +02:00
|
|
|
!SaveObjectToPrecompiled(cbuf.IsIndirect())) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u32>(entries.samplers.size()))) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
for (const auto& sampler : entries.samplers) {
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u64>(sampler.GetOffset())) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u64>(sampler.GetIndex())) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(sampler.GetType())) ||
|
2019-05-19 08:27:00 +02:00
|
|
|
!SaveObjectToPrecompiled(sampler.IsArray()) ||
|
|
|
|
!SaveObjectToPrecompiled(sampler.IsShadow()) ||
|
|
|
|
!SaveObjectToPrecompiled(sampler.IsBindless())) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u32>(entries.global_memory_entries.size()))) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
for (const auto& gmem : entries.global_memory_entries) {
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u32>(gmem.GetCbufIndex())) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(gmem.GetCbufOffset())) ||
|
2019-05-19 08:27:00 +02:00
|
|
|
!SaveObjectToPrecompiled(gmem.IsRead()) || !SaveObjectToPrecompiled(gmem.IsWritten())) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const bool clip_distance : entries.clip_distances) {
|
2019-05-19 08:27:00 +02:00
|
|
|
if (!SaveObjectToPrecompiled(clip_distance)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u64>(entries.shader_length))) {
|
|
|
|
return false;
|
2019-01-15 19:22:25 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
return true;
|
2019-01-14 03:33:31 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
void ShaderDiskCacheOpenGL::InvalidateTransferable() {
|
2019-01-15 19:22:25 +01:00
|
|
|
if (!FileUtil::Delete(GetTransferablePath())) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to invalidate transferable file={}",
|
|
|
|
GetTransferablePath());
|
|
|
|
}
|
|
|
|
InvalidatePrecompiled();
|
2019-01-14 05:25:25 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
void ShaderDiskCacheOpenGL::InvalidatePrecompiled() {
|
|
|
|
// Clear virtaul precompiled cache file
|
|
|
|
precompiled_cache_virtual_file.Resize(0);
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
if (!FileUtil::Delete(GetPrecompiledPath())) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to invalidate precompiled file={}", GetPrecompiledPath());
|
|
|
|
}
|
2019-01-14 03:33:31 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
void ShaderDiskCacheOpenGL::SaveRaw(const ShaderDiskCacheRaw& entry) {
|
2019-01-15 19:22:25 +01:00
|
|
|
if (!IsUsable())
|
2019-01-14 06:14:27 +01:00
|
|
|
return;
|
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
const u64 id = entry.GetUniqueIdentifier();
|
|
|
|
if (transferable.find(id) != transferable.end()) {
|
|
|
|
// The shader already exists
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileUtil::IOFile file = AppendTransferableFile();
|
2019-01-15 19:22:25 +01:00
|
|
|
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();
|
2019-01-14 03:29:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
transferable.insert({id, {}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderDiskCacheOpenGL::SaveUsage(const ShaderDiskCacheUsage& usage) {
|
2019-01-15 19:22:25 +01:00
|
|
|
if (!IsUsable())
|
2019-01-14 06:14:27 +01:00
|
|
|
return;
|
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
const auto it = transferable.find(usage.unique_identifier);
|
2019-01-15 19:22:25 +01:00
|
|
|
ASSERT_MSG(it != transferable.end(), "Saving shader usage without storing raw previously");
|
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
auto& usages{it->second};
|
2019-05-01 05:36:11 +02:00
|
|
|
if (usages.find(usage) != usages.end()) {
|
|
|
|
// Skip this variant since the shader is already stored.
|
|
|
|
return;
|
|
|
|
}
|
2019-01-14 03:29:24 +01:00
|
|
|
usages.insert(usage);
|
|
|
|
|
|
|
|
FileUtil::IOFile file = AppendTransferableFile();
|
2019-01-15 19:22:25 +01:00
|
|
|
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();
|
2019-01-14 03:29:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 05:07:57 +01:00
|
|
|
void ShaderDiskCacheOpenGL::SaveDecompiled(u64 unique_identifier, const std::string& code,
|
|
|
|
const GLShader::ShaderEntries& entries) {
|
2019-01-15 19:22:25 +01:00
|
|
|
if (!IsUsable())
|
2019-01-15 05:07:57 +01:00
|
|
|
return;
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
if (precompiled_cache_virtual_file.GetSize() == 0) {
|
|
|
|
SavePrecompiledHeaderToVirtualPrecompiledCache();
|
|
|
|
}
|
2019-01-15 05:07:57 +01:00
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveDecompiledFile(unique_identifier, code, entries)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
LOG_ERROR(Render_OpenGL,
|
|
|
|
"Failed to save decompiled entry to the precompiled file - removing");
|
|
|
|
InvalidatePrecompiled();
|
2019-01-15 05:07:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderDiskCacheOpenGL::SaveDump(const ShaderDiskCacheUsage& usage, GLuint program) {
|
2019-01-15 19:22:25 +01:00
|
|
|
if (!IsUsable())
|
2019-01-14 06:14:27 +01:00
|
|
|
return;
|
2019-01-14 03:33:00 +01:00
|
|
|
|
|
|
|
GLint binary_length{};
|
|
|
|
glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binary_length);
|
|
|
|
|
|
|
|
GLenum binary_format{};
|
|
|
|
std::vector<u8> binary(binary_length);
|
|
|
|
glGetProgramBinary(program, binary_length, nullptr, &binary_format, binary.data());
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u32>(PrecompiledEntryKind::Dump)) ||
|
|
|
|
!SaveObjectToPrecompiled(usage) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(binary_format)) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(binary_length)) ||
|
|
|
|
!SaveArrayToPrecompiled(binary.data(), binary.size())) {
|
2019-01-15 19:22:25 +01:00
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to save binary program file in shader={:016x} - removing",
|
|
|
|
usage.unique_identifier);
|
|
|
|
InvalidatePrecompiled();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 06:05:30 +01:00
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
bool ShaderDiskCacheOpenGL::IsUsable() const {
|
|
|
|
return tried_to_load && Settings::values.use_disk_shader_cache;
|
2019-01-14 03:33:00 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
FileUtil::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const {
|
2019-01-15 19:22:25 +01:00
|
|
|
if (!EnsureDirectories())
|
2019-01-14 03:29:24 +01:00
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto transferable_path{GetTransferablePath()};
|
|
|
|
const bool existed = FileUtil::Exists(transferable_path);
|
|
|
|
|
|
|
|
FileUtil::IOFile file(transferable_path, "ab");
|
|
|
|
if (!file.IsOpen()) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to open transferable cache in path={}", transferable_path);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (!existed || file.GetSize() == 0) {
|
|
|
|
// If the file didn't exist, write its version
|
2019-01-15 19:22:25 +01:00
|
|
|
if (file.WriteObject(NativeVersion) != 1) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to write transferable cache version in path={}",
|
|
|
|
transferable_path);
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
void ShaderDiskCacheOpenGL::SavePrecompiledHeaderToVirtualPrecompiledCache() {
|
|
|
|
const auto hash{GetShaderCacheVersionHash()};
|
|
|
|
if (!SaveArrayToPrecompiled(hash.data(), hash.size())) {
|
|
|
|
LOG_ERROR(
|
|
|
|
Render_OpenGL,
|
|
|
|
"Failed to write precompiled cache version hash to virtual precompiled cache file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderDiskCacheOpenGL::SaveVirtualPrecompiledFile() {
|
|
|
|
precompiled_cache_virtual_file_offset = 0;
|
2019-02-10 16:38:34 +01:00
|
|
|
const std::vector<u8>& uncompressed = precompiled_cache_virtual_file.ReadAllBytes();
|
|
|
|
const std::vector<u8>& compressed =
|
|
|
|
Common::Compression::CompressDataZSTDDefault(uncompressed.data(), uncompressed.size());
|
2019-01-14 03:33:00 +01:00
|
|
|
|
|
|
|
const auto precompiled_path{GetPrecompiledPath()};
|
2019-02-25 11:47:23 +01:00
|
|
|
FileUtil::IOFile file(precompiled_path, "wb");
|
2019-01-14 03:33:00 +01:00
|
|
|
|
|
|
|
if (!file.IsOpen()) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to open precompiled cache in path={}", precompiled_path);
|
2019-02-25 11:47:23 +01:00
|
|
|
return;
|
2019-01-14 03:33:00 +01:00
|
|
|
}
|
2019-02-10 16:38:34 +01:00
|
|
|
if (file.WriteBytes(compressed.data(), compressed.size()) != compressed.size()) {
|
2019-02-25 11:47:23 +01:00
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to write precompiled cache version in path={}",
|
|
|
|
precompiled_path);
|
|
|
|
return;
|
2019-01-14 03:33:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 03:22:15 +01:00
|
|
|
bool ShaderDiskCacheOpenGL::EnsureDirectories() const {
|
|
|
|
const auto CreateDir = [](const std::string& dir) {
|
|
|
|
if (!FileUtil::CreateDir(dir)) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to create directory={}", dir);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
return CreateDir(FileUtil::GetUserPath(FileUtil::UserPath::ShaderDir)) &&
|
|
|
|
CreateDir(GetBaseDir()) && CreateDir(GetTransferableDir()) &&
|
|
|
|
CreateDir(GetPrecompiledDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderDiskCacheOpenGL::GetTransferablePath() const {
|
|
|
|
return FileUtil::SanitizePath(GetTransferableDir() + DIR_SEP_CHR + GetTitleID() + ".bin");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderDiskCacheOpenGL::GetPrecompiledPath() const {
|
|
|
|
return FileUtil::SanitizePath(GetPrecompiledDir() + DIR_SEP_CHR + GetTitleID() + ".bin");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderDiskCacheOpenGL::GetTransferableDir() const {
|
|
|
|
return GetBaseDir() + DIR_SEP "transferable";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderDiskCacheOpenGL::GetPrecompiledDir() const {
|
|
|
|
return GetBaseDir() + DIR_SEP "precompiled";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderDiskCacheOpenGL::GetBaseDir() const {
|
|
|
|
return FileUtil::GetUserPath(FileUtil::UserPath::ShaderDir) + DIR_SEP "opengl";
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:28:42 +01:00
|
|
|
std::string ShaderDiskCacheOpenGL::GetTitleID() const {
|
|
|
|
return fmt::format("{:016X}", system.CurrentProcess()->GetTitleID());
|
|
|
|
}
|
|
|
|
|
2019-04-23 23:19:28 +02:00
|
|
|
} // namespace OpenGL
|