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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-01-15 06:17:38 +01:00
|
|
|
#include <optional>
|
2019-01-14 03:22:15 +01:00
|
|
|
#include <string>
|
2019-01-14 02:36:52 +01:00
|
|
|
#include <tuple>
|
2019-09-26 02:46:34 +02:00
|
|
|
#include <type_traits>
|
2019-02-03 01:14:36 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2019-01-15 06:17:38 +01:00
|
|
|
#include <utility>
|
2019-01-14 03:29:24 +01:00
|
|
|
#include <vector>
|
2019-01-14 02:36:52 +01:00
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
#include <glad/glad.h>
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
2019-01-14 02:36:52 +01:00
|
|
|
#include "common/common_types.h"
|
2019-02-25 11:47:23 +01:00
|
|
|
#include "core/file_sys/vfs_vector.h"
|
2019-01-14 03:29:24 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_gen.h"
|
2019-09-26 02:46:34 +02:00
|
|
|
#include "video_core/shader/const_buffer_locker.h"
|
2019-01-14 02:36:52 +01:00
|
|
|
|
2019-01-15 20:28:42 +01:00
|
|
|
namespace Core {
|
|
|
|
class System;
|
|
|
|
}
|
|
|
|
|
2019-01-15 06:42:25 +01:00
|
|
|
namespace FileUtil {
|
|
|
|
class IOFile;
|
2019-01-15 20:28:42 +01:00
|
|
|
}
|
2019-01-15 06:42:25 +01:00
|
|
|
|
2019-01-14 02:36:52 +01:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2019-04-26 01:10:20 +02:00
|
|
|
struct ShaderDiskCacheUsage;
|
|
|
|
struct ShaderDiskCacheDump;
|
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
using ProgramCode = std::vector<u64>;
|
2019-07-15 03:25:13 +02:00
|
|
|
using ShaderDumpsMap = std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump>;
|
2019-04-29 02:08:31 +02:00
|
|
|
|
2019-07-15 03:25:13 +02:00
|
|
|
/// Allocated bindings used by an OpenGL shader program
|
2019-01-14 02:36:52 +01:00
|
|
|
struct BaseBindings {
|
|
|
|
u32 cbuf{};
|
|
|
|
u32 gmem{};
|
|
|
|
u32 sampler{};
|
2019-04-27 07:37:15 +02:00
|
|
|
u32 image{};
|
2019-01-14 02:36:52 +01:00
|
|
|
|
2019-11-13 03:26:56 +01:00
|
|
|
bool operator==(const BaseBindings& rhs) const noexcept {
|
2019-04-27 07:37:15 +02:00
|
|
|
return std::tie(cbuf, gmem, sampler, image) ==
|
|
|
|
std::tie(rhs.cbuf, rhs.gmem, rhs.sampler, rhs.image);
|
2019-01-14 02:36:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 03:26:56 +01:00
|
|
|
bool operator!=(const BaseBindings& rhs) const noexcept {
|
2019-01-15 06:42:25 +01:00
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
2019-02-03 01:14:36 +01:00
|
|
|
};
|
2019-09-26 02:46:34 +02:00
|
|
|
static_assert(std::is_trivially_copyable_v<BaseBindings>);
|
2019-02-03 01:14:36 +01:00
|
|
|
|
2019-11-13 03:26:56 +01:00
|
|
|
/// Describes the different variants a program can be compiled with.
|
|
|
|
struct ProgramVariant final {
|
|
|
|
ProgramVariant() = default;
|
|
|
|
|
|
|
|
/// Graphics constructor.
|
|
|
|
explicit constexpr ProgramVariant(BaseBindings base_bindings, GLenum primitive_mode) noexcept
|
|
|
|
: base_bindings{base_bindings}, primitive_mode{primitive_mode} {}
|
|
|
|
|
|
|
|
/// Compute constructor.
|
2019-11-13 04:25:52 +01:00
|
|
|
explicit constexpr ProgramVariant(u32 block_x, u32 block_y, u32 block_z, u32 shared_memory_size,
|
|
|
|
u32 local_memory_size) noexcept
|
2019-11-13 03:39:45 +01:00
|
|
|
: block_x{block_x}, block_y{static_cast<u16>(block_y)}, block_z{static_cast<u16>(block_z)},
|
2019-11-13 04:25:52 +01:00
|
|
|
shared_memory_size{shared_memory_size}, local_memory_size{local_memory_size} {}
|
2019-11-13 03:26:56 +01:00
|
|
|
|
|
|
|
// Graphics specific parameters.
|
|
|
|
BaseBindings base_bindings{};
|
2019-04-29 02:08:31 +02:00
|
|
|
GLenum primitive_mode{};
|
|
|
|
|
2019-11-13 03:26:56 +01:00
|
|
|
// Compute specific parameters.
|
|
|
|
u32 block_x{};
|
|
|
|
u16 block_y{};
|
|
|
|
u16 block_z{};
|
2019-11-13 03:39:45 +01:00
|
|
|
u32 shared_memory_size{};
|
2019-11-13 04:25:52 +01:00
|
|
|
u32 local_memory_size{};
|
2019-11-13 03:26:56 +01:00
|
|
|
|
|
|
|
bool operator==(const ProgramVariant& rhs) const noexcept {
|
2019-11-13 03:39:45 +01:00
|
|
|
return std::tie(base_bindings, primitive_mode, block_x, block_y, block_z,
|
2019-11-13 04:25:52 +01:00
|
|
|
shared_memory_size, local_memory_size) ==
|
|
|
|
std::tie(rhs.base_bindings, rhs.primitive_mode, rhs.block_x, rhs.block_y,
|
|
|
|
rhs.block_z, rhs.shared_memory_size, rhs.local_memory_size);
|
2019-04-29 02:08:31 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 03:26:56 +01:00
|
|
|
bool operator!=(const ProgramVariant& rhs) const noexcept {
|
2019-04-29 02:08:31 +02:00
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
|
|
|
};
|
2019-09-26 02:46:34 +02:00
|
|
|
static_assert(std::is_trivially_copyable_v<ProgramVariant>);
|
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
/// Describes how a shader is used.
|
2019-02-03 01:14:36 +01:00
|
|
|
struct ShaderDiskCacheUsage {
|
|
|
|
u64 unique_identifier{};
|
2019-04-29 02:08:31 +02:00
|
|
|
ProgramVariant variant;
|
2019-09-26 02:46:34 +02:00
|
|
|
VideoCommon::Shader::KeyMap keys;
|
|
|
|
VideoCommon::Shader::BoundSamplerMap bound_samplers;
|
|
|
|
VideoCommon::Shader::BindlessSamplerMap bindless_samplers;
|
2019-02-03 01:14:36 +01:00
|
|
|
|
|
|
|
bool operator==(const ShaderDiskCacheUsage& rhs) const {
|
2019-09-26 02:46:34 +02:00
|
|
|
return std::tie(unique_identifier, variant, keys, bound_samplers, bindless_samplers) ==
|
|
|
|
std::tie(rhs.unique_identifier, rhs.variant, rhs.keys, rhs.bound_samplers,
|
|
|
|
rhs.bindless_samplers);
|
2019-02-03 01:14:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const ShaderDiskCacheUsage& rhs) const {
|
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OpenGL
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct hash<OpenGL::BaseBindings> {
|
2019-06-24 06:58:44 +02:00
|
|
|
std::size_t operator()(const OpenGL::BaseBindings& bindings) const noexcept {
|
2019-04-27 07:37:15 +02:00
|
|
|
return static_cast<std::size_t>(bindings.cbuf) ^
|
|
|
|
(static_cast<std::size_t>(bindings.gmem) << 8) ^
|
|
|
|
(static_cast<std::size_t>(bindings.sampler) << 16) ^
|
|
|
|
(static_cast<std::size_t>(bindings.image) << 24);
|
2019-02-03 01:14:36 +01:00
|
|
|
}
|
|
|
|
};
|
2019-01-15 06:42:25 +01:00
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
template <>
|
|
|
|
struct hash<OpenGL::ProgramVariant> {
|
2019-06-24 06:58:44 +02:00
|
|
|
std::size_t operator()(const OpenGL::ProgramVariant& variant) const noexcept {
|
2019-11-13 03:26:56 +01:00
|
|
|
return std::hash<OpenGL::BaseBindings>{}(variant.base_bindings) ^
|
|
|
|
(static_cast<std::size_t>(variant.primitive_mode) << 6) ^
|
|
|
|
static_cast<std::size_t>(variant.block_x) ^
|
|
|
|
(static_cast<std::size_t>(variant.block_y) << 32) ^
|
2019-11-13 03:39:45 +01:00
|
|
|
(static_cast<std::size_t>(variant.block_z) << 48) ^
|
2019-11-13 04:25:52 +01:00
|
|
|
(static_cast<std::size_t>(variant.shared_memory_size) << 16) ^
|
|
|
|
(static_cast<std::size_t>(variant.local_memory_size) << 36);
|
2019-04-29 02:08:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-03 01:14:36 +01:00
|
|
|
template <>
|
|
|
|
struct hash<OpenGL::ShaderDiskCacheUsage> {
|
2019-05-19 08:42:57 +02:00
|
|
|
std::size_t operator()(const OpenGL::ShaderDiskCacheUsage& usage) const noexcept {
|
2019-02-03 01:14:36 +01:00
|
|
|
return static_cast<std::size_t>(usage.unique_identifier) ^
|
2019-11-13 03:26:56 +01:00
|
|
|
std::hash<OpenGL::ProgramVariant>{}(usage.variant);
|
2019-01-14 02:36:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-03 01:14:36 +01:00
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
namespace OpenGL {
|
|
|
|
|
2019-01-15 06:42:25 +01:00
|
|
|
/// Describes a shader how it's used by the guest GPU
|
2019-01-14 03:29:24 +01:00
|
|
|
class ShaderDiskCacheRaw {
|
|
|
|
public:
|
2019-07-15 03:25:13 +02:00
|
|
|
explicit ShaderDiskCacheRaw(u64 unique_identifier, ProgramType program_type,
|
2019-09-25 04:34:18 +02:00
|
|
|
ProgramCode program_code, ProgramCode program_code_b = {});
|
2019-01-15 19:22:25 +01:00
|
|
|
ShaderDiskCacheRaw();
|
2019-01-15 06:42:25 +01:00
|
|
|
~ShaderDiskCacheRaw();
|
2019-01-14 03:29:24 +01:00
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
bool Load(FileUtil::IOFile& file);
|
|
|
|
|
|
|
|
bool Save(FileUtil::IOFile& file) const;
|
2019-01-14 03:29:24 +01:00
|
|
|
|
|
|
|
u64 GetUniqueIdentifier() const {
|
|
|
|
return unique_identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasProgramA() const {
|
2019-07-15 03:25:13 +02:00
|
|
|
return program_type == ProgramType::VertexA;
|
2019-01-14 03:29:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-15 03:25:13 +02:00
|
|
|
ProgramType GetProgramType() const {
|
2019-01-14 03:29:24 +01:00
|
|
|
return program_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProgramCode& GetProgramCode() const {
|
|
|
|
return program_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProgramCode& GetProgramCodeB() const {
|
|
|
|
return program_code_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
u64 unique_identifier{};
|
2019-07-15 03:25:13 +02:00
|
|
|
ProgramType program_type{};
|
2019-01-14 03:29:24 +01:00
|
|
|
|
|
|
|
ProgramCode program_code;
|
|
|
|
ProgramCode program_code_b;
|
|
|
|
};
|
|
|
|
|
2019-01-15 06:42:25 +01:00
|
|
|
/// Contains an OpenGL dumped binary program
|
2019-01-15 05:07:57 +01:00
|
|
|
struct ShaderDiskCacheDump {
|
2019-09-25 04:34:18 +02:00
|
|
|
GLenum binary_format{};
|
2019-01-15 05:07:57 +01:00
|
|
|
std::vector<u8> binary;
|
|
|
|
};
|
|
|
|
|
2019-01-14 03:22:15 +01:00
|
|
|
class ShaderDiskCacheOpenGL {
|
|
|
|
public:
|
2019-01-15 20:28:42 +01:00
|
|
|
explicit ShaderDiskCacheOpenGL(Core::System& system);
|
2019-05-19 08:50:47 +02:00
|
|
|
~ShaderDiskCacheOpenGL();
|
2019-01-15 20:28:42 +01:00
|
|
|
|
2019-01-15 06:17:38 +01:00
|
|
|
/// Loads transferable cache. If file has a old version or on failure, it deletes the file.
|
|
|
|
std::optional<std::pair<std::vector<ShaderDiskCacheRaw>, std::vector<ShaderDiskCacheUsage>>>
|
|
|
|
LoadTransferable();
|
|
|
|
|
|
|
|
/// Loads current game's precompiled cache. Invalidates on failure.
|
2019-09-25 04:34:18 +02:00
|
|
|
std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump> LoadPrecompiled();
|
2019-01-14 03:33:31 +01:00
|
|
|
|
2019-01-14 05:25:25 +01:00
|
|
|
/// Removes the transferable (and precompiled) cache file.
|
2019-02-25 11:47:23 +01:00
|
|
|
void InvalidateTransferable();
|
2019-01-14 05:25:25 +01:00
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
/// Removes the precompiled cache file and clears virtual precompiled cache file.
|
|
|
|
void InvalidatePrecompiled();
|
2019-01-14 03:33:31 +01:00
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
/// Saves a raw dump to the transferable file. Checks for collisions.
|
|
|
|
void SaveRaw(const ShaderDiskCacheRaw& entry);
|
|
|
|
|
|
|
|
/// Saves shader usage to the transferable file. Does not check for collisions.
|
|
|
|
void SaveUsage(const ShaderDiskCacheUsage& usage);
|
|
|
|
|
2019-01-15 05:07:57 +01:00
|
|
|
/// Saves a dump entry to the precompiled file. Does not check for collisions.
|
|
|
|
void SaveDump(const ShaderDiskCacheUsage& usage, GLuint program);
|
2019-01-14 03:33:00 +01:00
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
/// Serializes virtual precompiled shader cache file to real file
|
|
|
|
void SaveVirtualPrecompiledFile();
|
|
|
|
|
2019-01-14 03:22:15 +01:00
|
|
|
private:
|
2019-01-15 19:22:25 +01:00
|
|
|
/// Loads the transferable cache. Returns empty on failure.
|
2019-09-25 04:34:18 +02:00
|
|
|
std::optional<std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump>>
|
2019-01-15 19:22:25 +01:00
|
|
|
LoadPrecompiledFile(FileUtil::IOFile& file);
|
|
|
|
|
2019-01-14 03:29:24 +01:00
|
|
|
/// Opens current game's transferable file and write it's header if it doesn't exist
|
|
|
|
FileUtil::IOFile AppendTransferableFile() const;
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
/// Save precompiled header to precompiled_cache_in_memory
|
|
|
|
void SavePrecompiledHeaderToVirtualPrecompiledCache();
|
2019-01-14 03:33:00 +01:00
|
|
|
|
2019-01-14 03:22:15 +01:00
|
|
|
/// Create shader disk cache directories. Returns true on success.
|
|
|
|
bool EnsureDirectories() const;
|
|
|
|
|
|
|
|
/// Gets current game's transferable file path
|
|
|
|
std::string GetTransferablePath() const;
|
|
|
|
|
|
|
|
/// Gets current game's precompiled file path
|
|
|
|
std::string GetPrecompiledPath() const;
|
|
|
|
|
|
|
|
/// Get user's transferable directory path
|
|
|
|
std::string GetTransferableDir() const;
|
|
|
|
|
|
|
|
/// Get user's precompiled directory path
|
|
|
|
std::string GetPrecompiledDir() const;
|
|
|
|
|
|
|
|
/// Get user's shader directory path
|
|
|
|
std::string GetBaseDir() const;
|
2019-01-14 03:29:24 +01:00
|
|
|
|
2019-01-15 20:28:42 +01:00
|
|
|
/// Get current game's title id
|
|
|
|
std::string GetTitleID() const;
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
template <typename T>
|
|
|
|
bool SaveArrayToPrecompiled(const T* data, std::size_t length) {
|
|
|
|
const std::size_t write_length = precompiled_cache_virtual_file.WriteArray(
|
|
|
|
data, length, precompiled_cache_virtual_file_offset);
|
|
|
|
precompiled_cache_virtual_file_offset += write_length;
|
|
|
|
return write_length == sizeof(T) * length;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool LoadArrayFromPrecompiled(T* data, std::size_t length) {
|
|
|
|
const std::size_t read_length = precompiled_cache_virtual_file.ReadArray(
|
|
|
|
data, length, precompiled_cache_virtual_file_offset);
|
|
|
|
precompiled_cache_virtual_file_offset += read_length;
|
|
|
|
return read_length == sizeof(T) * length;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool SaveObjectToPrecompiled(const T& object) {
|
|
|
|
return SaveArrayToPrecompiled(&object, 1);
|
|
|
|
}
|
|
|
|
|
2019-05-19 08:27:00 +02:00
|
|
|
bool SaveObjectToPrecompiled(bool object) {
|
|
|
|
const auto value = static_cast<u8>(object);
|
|
|
|
return SaveArrayToPrecompiled(&value, 1);
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:47:23 +01:00
|
|
|
template <typename T>
|
|
|
|
bool LoadObjectFromPrecompiled(T& object) {
|
|
|
|
return LoadArrayFromPrecompiled(&object, 1);
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:28:42 +01:00
|
|
|
Core::System& system;
|
2019-04-27 07:37:15 +02:00
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
// Stores whole precompiled cache which will be read from or saved to the precompiled chache
|
|
|
|
// file
|
2019-02-25 11:47:23 +01:00
|
|
|
FileSys::VectorVfsFile precompiled_cache_virtual_file;
|
|
|
|
// Stores the current offset of the precompiled cache file for IO purposes
|
2019-05-19 08:53:38 +02:00
|
|
|
std::size_t precompiled_cache_virtual_file_offset = 0;
|
2019-02-25 11:47:23 +01:00
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
// Stored transferable shaders
|
|
|
|
std::unordered_map<u64, std::unordered_set<ShaderDiskCacheUsage>> transferable;
|
|
|
|
|
2019-01-15 19:22:25 +01:00
|
|
|
// The cache has been loaded at boot
|
2019-10-06 22:48:48 +02:00
|
|
|
bool is_usable{};
|
2019-01-14 03:22:15 +01:00
|
|
|
};
|
|
|
|
|
2019-04-26 01:10:20 +02:00
|
|
|
} // namespace OpenGL
|