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>;
|
|
|
|
|
2019-09-26 02:46:34 +02:00
|
|
|
struct ConstBufferKey {
|
2020-02-26 20:13:47 +01:00
|
|
|
u32 cbuf = 0;
|
|
|
|
u32 offset = 0;
|
|
|
|
u32 value = 0;
|
2019-09-26 02:46:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BoundSamplerKey {
|
2020-02-26 20:13:47 +01:00
|
|
|
u32 offset = 0;
|
|
|
|
Tegra::Engines::SamplerDescriptor sampler;
|
2019-09-26 02:46:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BindlessSamplerKey {
|
2020-02-26 20:13:47 +01:00
|
|
|
u32 cbuf = 0;
|
|
|
|
u32 offset = 0;
|
|
|
|
Tegra::Engines::SamplerDescriptor sampler;
|
2019-01-14 03:29:24 +01:00
|
|
|
};
|
|
|
|
|
2020-03-02 05:54:00 +01:00
|
|
|
constexpr u32 NativeVersion = 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
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
ShaderDiskCacheEntry::ShaderDiskCacheEntry() = default;
|
2019-01-15 19:22:25 +01:00
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
ShaderDiskCacheEntry::~ShaderDiskCacheEntry() = default;
|
2019-01-14 03:29:24 +01:00
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
bool ShaderDiskCacheEntry::Load(FileUtil::IOFile& file) {
|
|
|
|
if (file.ReadBytes(&type, sizeof(u32)) != sizeof(u32)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-26 20:13:47 +01:00
|
|
|
u32 code_size;
|
|
|
|
u32 code_size_b;
|
2019-11-18 22:35:21 +01:00
|
|
|
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-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
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
if (file.ReadArray(code.data(), code_size) != code_size) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
2020-02-26 20:13:47 +01:00
|
|
|
}
|
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
|
|
|
}
|
2020-02-26 20:13:47 +01:00
|
|
|
|
2020-02-29 07:49:51 +01:00
|
|
|
u8 is_texture_handler_size_known;
|
2020-02-26 20:13:47 +01:00
|
|
|
u32 texture_handler_size_value;
|
|
|
|
u32 num_keys;
|
|
|
|
u32 num_bound_samplers;
|
|
|
|
u32 num_bindless_samplers;
|
|
|
|
if (file.ReadArray(&unique_identifier, 1) != 1 || file.ReadArray(&bound_buffer, 1) != 1 ||
|
|
|
|
file.ReadArray(&is_texture_handler_size_known, 1) != 1 ||
|
2020-02-29 07:49:51 +01:00
|
|
|
file.ReadArray(&texture_handler_size_value, 1) != 1 ||
|
|
|
|
file.ReadArray(&graphics_info, 1) != 1 || file.ReadArray(&compute_info, 1) != 1 ||
|
|
|
|
file.ReadArray(&num_keys, 1) != 1 || file.ReadArray(&num_bound_samplers, 1) != 1 ||
|
2020-02-26 20:13:47 +01:00
|
|
|
file.ReadArray(&num_bindless_samplers, 1) != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (is_texture_handler_size_known) {
|
|
|
|
texture_handler_size = texture_handler_size_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ConstBufferKey> flat_keys(num_keys);
|
|
|
|
std::vector<BoundSamplerKey> flat_bound_samplers(num_bound_samplers);
|
|
|
|
std::vector<BindlessSamplerKey> flat_bindless_samplers(num_bindless_samplers);
|
|
|
|
if (file.ReadArray(flat_keys.data(), flat_keys.size()) != flat_keys.size() ||
|
|
|
|
file.ReadArray(flat_bound_samplers.data(), flat_bound_samplers.size()) !=
|
|
|
|
flat_bound_samplers.size() ||
|
|
|
|
file.ReadArray(flat_bindless_samplers.data(), flat_bindless_samplers.size()) !=
|
|
|
|
flat_bindless_samplers.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const auto& key : flat_keys) {
|
|
|
|
keys.insert({{key.cbuf, key.offset}, key.value});
|
|
|
|
}
|
|
|
|
for (const auto& key : flat_bound_samplers) {
|
|
|
|
bound_samplers.emplace(key.offset, key.sampler);
|
|
|
|
}
|
|
|
|
for (const auto& key : flat_bindless_samplers) {
|
|
|
|
bindless_samplers.insert({{key.cbuf, key.offset}, key.sampler});
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
return true;
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
bool ShaderDiskCacheEntry::Save(FileUtil::IOFile& file) const {
|
|
|
|
if (file.WriteObject(static_cast<u32>(type)) != 1 ||
|
2019-11-18 22:35:21 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-02-26 20:13:47 +01:00
|
|
|
if (file.WriteArray(code.data(), code.size()) != code.size()) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return false;
|
2020-02-26 20:13:47 +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
|
|
|
}
|
2020-02-26 20:13:47 +01:00
|
|
|
|
|
|
|
if (file.WriteObject(unique_identifier) != 1 || file.WriteObject(bound_buffer) != 1 ||
|
2020-02-29 07:49:51 +01:00
|
|
|
file.WriteObject(static_cast<u8>(texture_handler_size.has_value())) != 1 ||
|
2020-02-26 20:13:47 +01:00
|
|
|
file.WriteObject(texture_handler_size.value_or(0)) != 1 ||
|
2020-02-29 07:49:51 +01:00
|
|
|
file.WriteObject(graphics_info) != 1 || file.WriteObject(compute_info) != 1 ||
|
2020-02-26 20:13:47 +01:00
|
|
|
file.WriteObject(static_cast<u32>(keys.size())) != 1 ||
|
|
|
|
file.WriteObject(static_cast<u32>(bound_samplers.size())) != 1 ||
|
|
|
|
file.WriteObject(static_cast<u32>(bindless_samplers.size())) != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ConstBufferKey> flat_keys;
|
|
|
|
flat_keys.reserve(keys.size());
|
|
|
|
for (const auto& [address, value] : keys) {
|
|
|
|
flat_keys.push_back(ConstBufferKey{address.first, address.second, value});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BoundSamplerKey> flat_bound_samplers;
|
|
|
|
flat_bound_samplers.reserve(bound_samplers.size());
|
|
|
|
for (const auto& [address, sampler] : bound_samplers) {
|
|
|
|
flat_bound_samplers.push_back(BoundSamplerKey{address, sampler});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BindlessSamplerKey> flat_bindless_samplers;
|
|
|
|
flat_bindless_samplers.reserve(bindless_samplers.size());
|
|
|
|
for (const auto& [address, sampler] : bindless_samplers) {
|
|
|
|
flat_bindless_samplers.push_back(
|
|
|
|
BindlessSamplerKey{address.first, address.second, sampler});
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.WriteArray(flat_keys.data(), flat_keys.size()) == flat_keys.size() &&
|
|
|
|
file.WriteArray(flat_bound_samplers.data(), flat_bound_samplers.size()) ==
|
|
|
|
flat_bound_samplers.size() &&
|
|
|
|
file.WriteArray(flat_bindless_samplers.data(), flat_bindless_samplers.size()) ==
|
|
|
|
flat_bindless_samplers.size();
|
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;
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
std::optional<std::vector<ShaderDiskCacheEntry>> 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()) {
|
2020-02-26 20:13:47 +01:00
|
|
|
LOG_INFO(Render_OpenGL, "No transferable shader cache found");
|
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)) {
|
2020-02-26 20:13:47 +01:00
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to get transferable cache version, skipping it");
|
2019-01-15 19:22:25 +01:00
|
|
|
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
|
2020-02-26 20:13:47 +01:00
|
|
|
std::vector<ShaderDiskCacheEntry> entries;
|
2019-01-15 19:22:25 +01:00
|
|
|
while (file.Tell() < file.GetSize()) {
|
2020-02-26 20:13:47 +01:00
|
|
|
ShaderDiskCacheEntry& entry = entries.emplace_back();
|
|
|
|
if (!entry.Load(file)) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to load transferable raw entry, skipping");
|
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;
|
2020-02-26 20:13:47 +01:00
|
|
|
return {std::move(entries)};
|
2019-01-14 03:30:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
std::vector<ShaderDiskCachePrecompiled> 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
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
FileUtil::IOFile file(GetPrecompiledPath(), "rb");
|
2019-01-14 03:33:31 +01:00
|
|
|
if (!file.IsOpen()) {
|
2020-02-26 20:13:47 +01:00
|
|
|
LOG_INFO(Render_OpenGL, "No precompiled shader cache found");
|
2019-01-15 06:17:38 +01:00
|
|
|
return {};
|
2019-01-14 03:33:31 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
if (const auto result = LoadPrecompiledFile(file)) {
|
|
|
|
return *result;
|
2019-01-14 03:33:31 +01:00
|
|
|
}
|
2020-02-26 20:13:47 +01:00
|
|
|
|
|
|
|
LOG_INFO(Render_OpenGL, "Failed to load precompiled cache");
|
|
|
|
file.Close();
|
|
|
|
InvalidatePrecompiled();
|
|
|
|
return {};
|
2019-01-15 19:22:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
std::optional<std::vector<ShaderDiskCachePrecompiled>> 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
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
std::vector<ShaderDiskCachePrecompiled> entries;
|
2019-02-25 11:47:23 +01:00
|
|
|
while (precompiled_cache_virtual_file_offset < precompiled_cache_virtual_file.GetSize()) {
|
2020-02-26 20:13:47 +01:00
|
|
|
u32 binary_size;
|
|
|
|
auto& entry = entries.emplace_back();
|
|
|
|
if (!LoadObjectFromPrecompiled(entry.unique_identifier) ||
|
|
|
|
!LoadObjectFromPrecompiled(entry.binary_format) ||
|
|
|
|
!LoadObjectFromPrecompiled(binary_size)) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
entry.binary.resize(binary_size);
|
|
|
|
if (!LoadArrayFromPrecompiled(entry.binary.data(), entry.binary.size())) {
|
2019-01-15 19:22:25 +01:00
|
|
|
return {};
|
|
|
|
}
|
2019-02-25 11:47:23 +01:00
|
|
|
}
|
2020-02-26 20:13:47 +01:00
|
|
|
return entries;
|
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
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
void ShaderDiskCacheOpenGL::SaveEntry(const ShaderDiskCacheEntry& 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
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
const u64 id = entry.unique_identifier;
|
|
|
|
if (stored_transferable.find(id) != stored_transferable.end()) {
|
2019-01-14 03:29:24 +01:00
|
|
|
// 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
|
|
|
}
|
2020-02-26 20:13:47 +01:00
|
|
|
if (!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;
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
stored_transferable.insert(id);
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
void ShaderDiskCacheOpenGL::SavePrecompiled(u64 unique_identifier, 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
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
GLint binary_length;
|
2019-01-14 03:33:00 +01:00
|
|
|
glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binary_length);
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
GLenum binary_format;
|
2019-01-14 03:33:00 +01:00
|
|
|
std::vector<u8> binary(binary_length);
|
|
|
|
glGetProgramBinary(program, binary_length, nullptr, &binary_format, binary.data());
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
if (!SaveObjectToPrecompiled(unique_identifier) || !SaveObjectToPrecompiled(binary_format) ||
|
|
|
|
!SaveObjectToPrecompiled(static_cast<u32>(binary.size())) ||
|
|
|
|
!SaveArrayToPrecompiled(binary.data(), binary.size())) {
|
2019-09-26 02:46:34 +02:00
|
|
|
LOG_ERROR(Render_OpenGL, "Failed to save binary program file in shader={:016X}, removing",
|
2020-02-26 20:13:47 +01:00
|
|
|
unique_identifier);
|
2019-01-15 19:22:25 +01:00
|
|
|
InvalidatePrecompiled();
|
2019-09-26 02:46:34 +02:00
|
|
|
}
|
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);
|
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
|