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-11-18 22:35:21 +01:00
|
|
|
|
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-11-18 22:35:21 +01:00
|
|
|
#include "video_core/engines/shader_type.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-11-18 22:35:21 +01:00
|
|
|
using Tegra::Engines::ShaderType;
|
2019-09-26 02:46:34 +02:00
|
|
|
using VideoCommon::Shader::BindlessSamplerMap;
|
|
|
|
using VideoCommon::Shader::BoundSamplerMap;
|
|
|
|
using VideoCommon::Shader::KeyMap;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-11-06 08:32:43 +01:00
|
|
|
using ShaderCacheVersionHash = std::array<u8, 64>;
|
|
|
|
|
|
|
|
enum class TransferableEntryKind : u32 {
|
|
|
|
Raw,
|
|
|
|
Usage,
|
|
|
|
};
|
|
|
|
|
2019-09-26 02:46:34 +02:00
|
|
|
struct ConstBufferKey {
|
2019-11-06 08:32:43 +01:00
|
|
|
u32 cbuf{};
|
|
|
|
u32 offset{};
|
|
|
|
u32 value{};
|
2019-09-26 02:46:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BoundSamplerKey {
|
2019-11-06 08:32:43 +01:00
|
|
|
u32 offset{};
|
|
|
|
Tegra::Engines::SamplerDescriptor sampler{};
|
2019-09-26 02:46:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BindlessSamplerKey {
|
2019-11-06 08:32:43 +01:00
|
|
|
u32 cbuf{};
|
|
|
|
u32 offset{};
|
|
|
|
Tegra::Engines::SamplerDescriptor sampler{};
|
2019-01-14 03:29:24 +01:00
|
|
|
};
|
|
|
|
|
2020-01-03 23:15:24 +01:00
|
|
|
constexpr u32 NativeVersion = 12;
|
2019-01-14 03:33:00 +01:00
|
|
|
|
2019-01-14 02:36:52 +01:00
|
|
|
// Making sure sizes doesn't change by accident
|
2019-11-19 01:38:15 +01:00
|
|
|
static_assert(sizeof(ProgramVariant) == 20);
|
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-09-25 04:34:18 +02:00
|
|
|
} // Anonymous namespace
|
2019-01-14 03:22:15 +01:00
|
|
|
|
2019-11-18 22:35:21 +01:00
|
|
|
ShaderDiskCacheRaw::ShaderDiskCacheRaw(u64 unique_identifier, ShaderType type, ProgramCode code,
|
|
|
|
ProgramCode code_b)
|
|
|
|
: unique_identifier{unique_identifier}, type{type}, code{std::move(code)}, code_b{std::move(
|
|
|
|
code_b)} {}
|
2019-01-15 19:22:25 +01:00
|
|
|
|
|
|
|
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) ||
|
2019-11-18 22:35:21 +01:00
|
|
|
file.ReadBytes(&type, sizeof(u32)) != sizeof(u32)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-18 22:35:21 +01:00
|
|
|
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)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-14 03:29:24 +01:00
|
|
|
|
2019-11-18 22:35:21 +01:00
|
|
|
code.resize(code_size);
|
|
|
|
code_b.resize(code_size_b);
|
2019-01-14 03:29:24 +01:00
|
|
|
|
2019-11-18 22:35:21 +01:00
|
|
|
if (file.ReadArray(code.data(), code_size) != code_size)
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
|
2019-11-18 22:35:21 +01:00
|
|
|
if (HasProgramA() && file.ReadArray(code_b.data(), code_size_b) != code_size_b) {
|
2019-01-15 19:22:25 +01:00
|
|
|
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 {
|
2019-11-18 22:35:21 +01:00
|
|
|
if (file.WriteObject(unique_identifier) != 1 || file.WriteObject(static_cast<u32>(type)) != 1 ||
|
|
|
|
file.WriteObject(static_cast<u32>(code.size())) != 1 ||
|
|
|
|
file.WriteObject(static_cast<u32>(code_b.size())) != 1) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 06:42:25 +01:00
|
|
|
|
2019-11-18 22:35:21 +01:00
|
|
|
if (file.WriteArray(code.data(), code.size()) != code.size())
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
2019-01-14 03:29:24 +01:00
|
|
|
|
2019-11-18 22:35:21 +01:00
|
|
|
if (HasProgramA() && file.WriteArray(code_b.data(), code_b.size()) != code_b.size()) {
|
2019-01-15 19:22:25 +01:00
|
|
|
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-05-19 08:53:38 +02:00
|
|
|
ShaderDiskCacheOpenGL::ShaderDiskCacheOpenGL(Core::System& system) : system{system} {}
|
2019-01-15 20:28:42 +01:00
|
|
|
|
2019-05-19 08:50:47 +02:00
|
|
|
ShaderDiskCacheOpenGL::~ShaderDiskCacheOpenGL() = default;
|
|
|
|
|
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;
|
2019-10-06 22:48:48 +02:00
|
|
|
if (!Settings::values.use_disk_shader_cache || !has_title_id) {
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-10-06 22:48:48 +02:00
|
|
|
}
|
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-10-06 22:48:48 +02:00
|
|
|
is_usable = true;
|
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,
|
2019-09-26 02:46:34 +02:00
|
|
|
"Failed to get transferable cache version for title id={}, skipping",
|
2019-01-15 19:22:25 +01:00
|
|
|
GetTitleID());
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-14 03:30:37 +01:00
|
|
|
|
|
|
|
if (version < NativeVersion) {
|
2019-09-26 02:46:34 +02:00
|
|
|
LOG_INFO(Render_OpenGL, "Transferable shader cache is old, removing");
|
2019-01-14 03:30:37 +01:00
|
|
|
file.Close();
|
2019-01-15 19:22:25 +01:00
|
|
|
InvalidateTransferable();
|
2019-10-06 22:48:48 +02:00
|
|
|
is_usable = true;
|
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 "
|
2019-09-26 02:46:34 +02:00
|
|
|
"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-09-26 02:46:34 +02:00
|
|
|
constexpr const char error_loading[] = "Failed to load transferable raw entry, skipping";
|
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)) {
|
2019-09-26 02:46:34 +02:00
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to read transferable file, skipping");
|
2019-01-15 19:22:25 +01:00
|
|
|
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)) {
|
2019-09-26 02:46:34 +02:00
|
|
|
LOG_ERROR(Render_OpenGL, error_loading);
|
2019-01-15 19:22:25 +01:00
|
|
|
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-09-26 02:46:34 +02:00
|
|
|
ShaderDiskCacheUsage usage;
|
|
|
|
|
|
|
|
u32 num_keys{};
|
|
|
|
u32 num_bound_samplers{};
|
|
|
|
u32 num_bindless_samplers{};
|
|
|
|
if (file.ReadArray(&usage.unique_identifier, 1) != 1 ||
|
2020-01-03 23:15:24 +01:00
|
|
|
file.ReadArray(&usage.variant, 1) != 1 ||
|
|
|
|
file.ReadArray(&usage.bound_buffer, 1) != 1 || file.ReadArray(&num_keys, 1) != 1 ||
|
2019-09-26 02:46:34 +02:00
|
|
|
file.ReadArray(&num_bound_samplers, 1) != 1 ||
|
|
|
|
file.ReadArray(&num_bindless_samplers, 1) != 1) {
|
|
|
|
LOG_ERROR(Render_OpenGL, error_loading);
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-09-26 02:46:34 +02:00
|
|
|
|
|
|
|
std::vector<ConstBufferKey> keys(num_keys);
|
|
|
|
std::vector<BoundSamplerKey> bound_samplers(num_bound_samplers);
|
|
|
|
std::vector<BindlessSamplerKey> bindless_samplers(num_bindless_samplers);
|
|
|
|
if (file.ReadArray(keys.data(), keys.size()) != keys.size() ||
|
|
|
|
file.ReadArray(bound_samplers.data(), bound_samplers.size()) !=
|
|
|
|
bound_samplers.size() ||
|
|
|
|
file.ReadArray(bindless_samplers.data(), bindless_samplers.size()) !=
|
|
|
|
bindless_samplers.size()) {
|
|
|
|
LOG_ERROR(Render_OpenGL, error_loading);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
for (const auto& key : keys) {
|
|
|
|
usage.keys.insert({{key.cbuf, key.offset}, key.value});
|
|
|
|
}
|
|
|
|
for (const auto& key : bound_samplers) {
|
|
|
|
usage.bound_samplers.emplace(key.offset, key.sampler);
|
|
|
|
}
|
|
|
|
for (const auto& key : bindless_samplers) {
|
|
|
|
usage.bindless_samplers.insert({{key.cbuf, key.offset}, key.sampler});
|
|
|
|
}
|
|
|
|
|
2019-01-14 03:30:37 +01:00
|
|
|
usages.push_back(std::move(usage));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2019-09-26 02:46:34 +02: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-10-06 22:48:48 +02:00
|
|
|
is_usable = true;
|
|
|
|
return {{std::move(raws), std::move(usages)}};
|
2019-01-14 03:30:37 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 04:34:18 +02:00
|
|
|
std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump>
|
2019-01-15 06:17:38 +01:00
|
|
|
ShaderDiskCacheOpenGL::LoadPrecompiled() {
|
2019-10-06 22:48:48 +02:00
|
|
|
if (!is_usable) {
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-10-06 22:48:48 +02:00
|
|
|
}
|
2019-01-14 06:14:27 +01:00
|
|
|
|
2019-09-25 04:34:18 +02:00
|
|
|
std::string path = GetPrecompiledPath();
|
|
|
|
FileUtil::IOFile file(path, "rb");
|
2019-01-14 03:33:31 +01:00
|
|
|
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,
|
2019-09-26 02:46:34 +02:00
|
|
|
"Failed to load precompiled cache for game with title id={}, removing",
|
2019-01-15 19:22:25 +01:00
|
|
|
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-09-25 04:34:18 +02:00
|
|
|
std::optional<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-04-26 01:10:20 +02:00
|
|
|
ShaderDumpsMap dumps;
|
2019-02-25 11:47:23 +01:00
|
|
|
while (precompiled_cache_virtual_file_offset < precompiled_cache_virtual_file.GetSize()) {
|
2019-09-26 02:46:34 +02:00
|
|
|
u32 num_keys{};
|
|
|
|
u32 num_bound_samplers{};
|
|
|
|
u32 num_bindless_samplers{};
|
2019-09-25 04:34:18 +02:00
|
|
|
ShaderDiskCacheUsage usage;
|
2019-09-26 02:46:34 +02:00
|
|
|
if (!LoadObjectFromPrecompiled(usage.unique_identifier) ||
|
2020-01-03 23:15:24 +01:00
|
|
|
!LoadObjectFromPrecompiled(usage.variant) ||
|
|
|
|
!LoadObjectFromPrecompiled(usage.bound_buffer) ||
|
|
|
|
!LoadObjectFromPrecompiled(num_keys) ||
|
2019-09-26 02:46:34 +02:00
|
|
|
!LoadObjectFromPrecompiled(num_bound_samplers) ||
|
|
|
|
!LoadObjectFromPrecompiled(num_bindless_samplers)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::vector<ConstBufferKey> keys(num_keys);
|
|
|
|
std::vector<BoundSamplerKey> bound_samplers(num_bound_samplers);
|
|
|
|
std::vector<BindlessSamplerKey> bindless_samplers(num_bindless_samplers);
|
|
|
|
if (!LoadArrayFromPrecompiled(keys.data(), keys.size()) ||
|
|
|
|
!LoadArrayFromPrecompiled(bound_samplers.data(), bound_samplers.size()) !=
|
|
|
|
bound_samplers.size() ||
|
|
|
|
!LoadArrayFromPrecompiled(bindless_samplers.data(), bindless_samplers.size()) !=
|
|
|
|
bindless_samplers.size()) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-09-26 02:46:34 +02:00
|
|
|
for (const auto& key : keys) {
|
|
|
|
usage.keys.insert({{key.cbuf, key.offset}, key.value});
|
|
|
|
}
|
|
|
|
for (const auto& key : bound_samplers) {
|
|
|
|
usage.bound_samplers.emplace(key.offset, key.sampler);
|
|
|
|
}
|
|
|
|
for (const auto& key : bindless_samplers) {
|
|
|
|
usage.bindless_samplers.insert({{key.cbuf, key.offset}, key.sampler});
|
|
|
|
}
|
2019-01-14 03:33:31 +01:00
|
|
|
|
2019-09-25 04:34:18 +02:00
|
|
|
ShaderDiskCacheDump dump;
|
|
|
|
if (!LoadObjectFromPrecompiled(dump.binary_format)) {
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-15 05:07:57 +01:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
|
2019-09-25 04:34:18 +02:00
|
|
|
u32 binary_length{};
|
|
|
|
if (!LoadObjectFromPrecompiled(binary_length)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-09-25 04:34:18 +02:00
|
|
|
dump.binary.resize(binary_length);
|
|
|
|
if (!LoadArrayFromPrecompiled(dump.binary.data(), dump.binary.size())) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-09-26 02:46:34 +02:00
|
|
|
dumps.emplace(std::move(usage), dump);
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2019-09-25 04:34:18 +02:00
|
|
|
return dumps;
|
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-10-06 22:48:48 +02:00
|
|
|
if (!is_usable) {
|
2019-01-14 06:14:27 +01:00
|
|
|
return;
|
2019-10-06 22:48:48 +02:00
|
|
|
}
|
2019-01-14 06:14:27 +01:00
|
|
|
|
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-09-26 02:46:34 +02:00
|
|
|
if (!file.IsOpen()) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return;
|
2019-09-26 02:46:34 +02:00
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
if (file.WriteObject(TransferableEntryKind::Raw) != 1 || !entry.Save(file)) {
|
2019-09-26 02:46:34 +02:00
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to save raw transferable cache entry, removing");
|
2019-01-15 19:22:25 +01:00
|
|
|
file.Close();
|
|
|
|
InvalidateTransferable();
|
2019-01-14 03:29:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
transferable.insert({id, {}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderDiskCacheOpenGL::SaveUsage(const ShaderDiskCacheUsage& usage) {
|
2019-10-06 22:48:48 +02:00
|
|
|
if (!is_usable) {
|
2019-01-14 06:14:27 +01:00
|
|
|
return;
|
2019-10-06 22:48:48 +02:00
|
|
|
}
|
2019-01-14 06:14:27 +01:00
|
|
|
|
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;
|
2019-09-26 02:46:34 +02:00
|
|
|
const auto Close = [&] {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to save usage transferable cache entry, removing");
|
2019-01-15 19:22:25 +01:00
|
|
|
file.Close();
|
|
|
|
InvalidateTransferable();
|
2019-09-26 02:46:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (file.WriteObject(TransferableEntryKind::Usage) != 1 ||
|
|
|
|
file.WriteObject(usage.unique_identifier) != 1 || file.WriteObject(usage.variant) != 1 ||
|
2020-01-03 23:15:24 +01:00
|
|
|
file.WriteObject(usage.bound_buffer) != 1 ||
|
2019-09-26 02:46:34 +02:00
|
|
|
file.WriteObject(static_cast<u32>(usage.keys.size())) != 1 ||
|
|
|
|
file.WriteObject(static_cast<u32>(usage.bound_samplers.size())) != 1 ||
|
|
|
|
file.WriteObject(static_cast<u32>(usage.bindless_samplers.size())) != 1) {
|
|
|
|
Close();
|
2019-01-14 03:29:24 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-09-26 02:46:34 +02:00
|
|
|
for (const auto& [pair, value] : usage.keys) {
|
|
|
|
const auto [cbuf, offset] = pair;
|
|
|
|
if (file.WriteObject(ConstBufferKey{cbuf, offset, value}) != 1) {
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto& [offset, sampler] : usage.bound_samplers) {
|
|
|
|
if (file.WriteObject(BoundSamplerKey{offset, sampler}) != 1) {
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto& [pair, sampler] : usage.bindless_samplers) {
|
|
|
|
const auto [cbuf, offset] = pair;
|
|
|
|
if (file.WriteObject(BindlessSamplerKey{cbuf, offset, sampler}) != 1) {
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 04:34:18 +02:00
|
|
|
void ShaderDiskCacheOpenGL::SaveDump(const ShaderDiskCacheUsage& usage, GLuint program) {
|
2019-10-06 22:48:48 +02:00
|
|
|
if (!is_usable) {
|
2019-01-15 05:07:57 +01:00
|
|
|
return;
|
2019-10-06 22:48:48 +02:00
|
|
|
}
|
2019-01-15 05:07:57 +01:00
|
|
|
|
2019-09-25 04:34:18 +02:00
|
|
|
// TODO(Rodrigo): This is a design smell. I shouldn't be having to manually write the header
|
|
|
|
// when writing the dump. This should be done the moment I get access to write to the virtual
|
|
|
|
// file.
|
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-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-09-26 02:46:34 +02:00
|
|
|
const auto Close = [&] {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to save binary program file in shader={:016X}, removing",
|
2019-01-15 19:22:25 +01:00
|
|
|
usage.unique_identifier);
|
|
|
|
InvalidatePrecompiled();
|
2019-09-26 02:46:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!SaveObjectToPrecompiled(usage.unique_identifier) ||
|
2020-01-03 23:15:24 +01:00
|
|
|
!SaveObjectToPrecompiled(usage.variant) || !SaveObjectToPrecompiled(usage.bound_buffer) ||
|
2019-09-26 02:46:34 +02:00
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(usage.keys.size())) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(usage.bound_samplers.size())) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(usage.bindless_samplers.size()))) {
|
|
|
|
Close();
|
2019-01-15 19:22:25 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-09-26 02:46:34 +02:00
|
|
|
for (const auto& [pair, value] : usage.keys) {
|
|
|
|
const auto [cbuf, offset] = pair;
|
|
|
|
if (SaveObjectToPrecompiled(ConstBufferKey{cbuf, offset, value}) != 1) {
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto& [offset, sampler] : usage.bound_samplers) {
|
|
|
|
if (SaveObjectToPrecompiled(BoundSamplerKey{offset, sampler}) != 1) {
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto& [pair, sampler] : usage.bindless_samplers) {
|
|
|
|
const auto [cbuf, offset] = pair;
|
|
|
|
if (SaveObjectToPrecompiled(BindlessSamplerKey{cbuf, offset, sampler}) != 1) {
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!SaveObjectToPrecompiled(static_cast<u32>(binary_format)) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(binary_length)) ||
|
|
|
|
!SaveArrayToPrecompiled(binary.data(), binary.size())) {
|
|
|
|
Close();
|
|
|
|
}
|
2019-01-15 19:22:25 +01:00
|
|
|
}
|
2019-01-15 06:05:30 +01:00
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
FileUtil::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const {
|
2019-09-25 04:34:18 +02:00
|
|
|
if (!EnsureDirectories()) {
|
2019-01-14 03:29:24 +01:00
|
|
|
return {};
|
2019-09-25 04:34:18 +02:00
|
|
|
}
|
2019-01-14 03:29:24 +01:00
|
|
|
|
|
|
|
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-09-25 04:34:18 +02:00
|
|
|
const std::vector<u8> uncompressed = precompiled_cache_virtual_file.ReadAllBytes();
|
|
|
|
const std::vector<u8> compressed =
|
2019-02-10 16:38:34 +01:00
|
|
|
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
|