1
0
Fork 0
forked from suyu/suyu

Merge pull request #2495 from lioncash/cache

gl_shader_disk_cache: Minor cleanup
This commit is contained in:
Hexagon12 2019-05-19 15:50:23 +01:00 committed by GitHub
commit fadf66993c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 34 deletions

View file

@ -104,8 +104,9 @@ bool ShaderDiskCacheRaw::Save(FileUtil::IOFile& file) const {
return true; return true;
} }
ShaderDiskCacheOpenGL::ShaderDiskCacheOpenGL(Core::System& system) ShaderDiskCacheOpenGL::ShaderDiskCacheOpenGL(Core::System& system) : system{system} {}
: system{system}, precompiled_cache_virtual_file_offset{0} {}
ShaderDiskCacheOpenGL::~ShaderDiskCacheOpenGL() = default;
std::optional<std::pair<std::vector<ShaderDiskCacheRaw>, std::vector<ShaderDiskCacheUsage>>> std::optional<std::pair<std::vector<ShaderDiskCacheRaw>, std::vector<ShaderDiskCacheUsage>>>
ShaderDiskCacheOpenGL::LoadTransferable() { ShaderDiskCacheOpenGL::LoadTransferable() {
@ -243,7 +244,7 @@ ShaderDiskCacheOpenGL::LoadPrecompiledFile(FileUtil::IOFile& file) {
return {}; return {};
} }
const auto entry = LoadDecompiledEntry(); auto entry = LoadDecompiledEntry();
if (!entry) { if (!entry) {
return {}; return {};
} }
@ -287,13 +288,13 @@ std::optional<ShaderDiskCacheDecompiled> ShaderDiskCacheOpenGL::LoadDecompiledEn
return {}; return {};
} }
std::vector<u8> code(code_size); std::string code(code_size, '\0');
if (!LoadArrayFromPrecompiled(code.data(), code.size())) { if (!LoadArrayFromPrecompiled(code.data(), code.size())) {
return {}; return {};
} }
ShaderDiskCacheDecompiled entry; ShaderDiskCacheDecompiled entry;
entry.code = std::string(reinterpret_cast<const char*>(code.data()), code_size); entry.code = std::move(code);
u32 const_buffers_count{}; u32 const_buffers_count{};
if (!LoadObjectFromPrecompiled(const_buffers_count)) { if (!LoadObjectFromPrecompiled(const_buffers_count)) {
@ -303,12 +304,12 @@ std::optional<ShaderDiskCacheDecompiled> ShaderDiskCacheOpenGL::LoadDecompiledEn
for (u32 i = 0; i < const_buffers_count; ++i) { for (u32 i = 0; i < const_buffers_count; ++i) {
u32 max_offset{}; u32 max_offset{};
u32 index{}; u32 index{};
u8 is_indirect{}; bool is_indirect{};
if (!LoadObjectFromPrecompiled(max_offset) || !LoadObjectFromPrecompiled(index) || if (!LoadObjectFromPrecompiled(max_offset) || !LoadObjectFromPrecompiled(index) ||
!LoadObjectFromPrecompiled(is_indirect)) { !LoadObjectFromPrecompiled(is_indirect)) {
return {}; return {};
} }
entry.entries.const_buffers.emplace_back(max_offset, is_indirect != 0, index); entry.entries.const_buffers.emplace_back(max_offset, is_indirect, index);
} }
u32 samplers_count{}; u32 samplers_count{};
@ -320,18 +321,17 @@ std::optional<ShaderDiskCacheDecompiled> ShaderDiskCacheOpenGL::LoadDecompiledEn
u64 offset{}; u64 offset{};
u64 index{}; u64 index{};
u32 type{}; u32 type{};
u8 is_array{}; bool is_array{};
u8 is_shadow{}; bool is_shadow{};
u8 is_bindless{}; bool is_bindless{};
if (!LoadObjectFromPrecompiled(offset) || !LoadObjectFromPrecompiled(index) || if (!LoadObjectFromPrecompiled(offset) || !LoadObjectFromPrecompiled(index) ||
!LoadObjectFromPrecompiled(type) || !LoadObjectFromPrecompiled(is_array) || !LoadObjectFromPrecompiled(type) || !LoadObjectFromPrecompiled(is_array) ||
!LoadObjectFromPrecompiled(is_shadow) || !LoadObjectFromPrecompiled(is_bindless)) { !LoadObjectFromPrecompiled(is_shadow) || !LoadObjectFromPrecompiled(is_bindless)) {
return {}; return {};
} }
entry.entries.samplers.emplace_back(static_cast<std::size_t>(offset), entry.entries.samplers.emplace_back(
static_cast<std::size_t>(index), static_cast<std::size_t>(offset), static_cast<std::size_t>(index),
static_cast<Tegra::Shader::TextureType>(type), static_cast<Tegra::Shader::TextureType>(type), is_array, is_shadow, is_bindless);
is_array != 0, is_shadow != 0, is_bindless != 0);
} }
u32 global_memory_count{}; u32 global_memory_count{};
@ -342,21 +342,20 @@ std::optional<ShaderDiskCacheDecompiled> ShaderDiskCacheOpenGL::LoadDecompiledEn
for (u32 i = 0; i < global_memory_count; ++i) { for (u32 i = 0; i < global_memory_count; ++i) {
u32 cbuf_index{}; u32 cbuf_index{};
u32 cbuf_offset{}; u32 cbuf_offset{};
u8 is_read{}; bool is_read{};
u8 is_written{}; bool is_written{};
if (!LoadObjectFromPrecompiled(cbuf_index) || !LoadObjectFromPrecompiled(cbuf_offset) || if (!LoadObjectFromPrecompiled(cbuf_index) || !LoadObjectFromPrecompiled(cbuf_offset) ||
!LoadObjectFromPrecompiled(is_read) || !LoadObjectFromPrecompiled(is_written)) { !LoadObjectFromPrecompiled(is_read) || !LoadObjectFromPrecompiled(is_written)) {
return {}; return {};
} }
entry.entries.global_memory_entries.emplace_back(cbuf_index, cbuf_offset, is_read != 0, entry.entries.global_memory_entries.emplace_back(cbuf_index, cbuf_offset, is_read,
is_written != 0); is_written);
} }
for (auto& clip_distance : entry.entries.clip_distances) { for (auto& clip_distance : entry.entries.clip_distances) {
u8 clip_distance_raw{}; if (!LoadObjectFromPrecompiled(clip_distance)) {
if (!LoadObjectFromPrecompiled(clip_distance_raw))
return {}; return {};
clip_distance = clip_distance_raw != 0; }
} }
u64 shader_length{}; u64 shader_length{};
@ -384,7 +383,7 @@ bool ShaderDiskCacheOpenGL::SaveDecompiledFile(u64 unique_identifier, const std:
for (const auto& cbuf : entries.const_buffers) { for (const auto& cbuf : entries.const_buffers) {
if (!SaveObjectToPrecompiled(static_cast<u32>(cbuf.GetMaxOffset())) || if (!SaveObjectToPrecompiled(static_cast<u32>(cbuf.GetMaxOffset())) ||
!SaveObjectToPrecompiled(static_cast<u32>(cbuf.GetIndex())) || !SaveObjectToPrecompiled(static_cast<u32>(cbuf.GetIndex())) ||
!SaveObjectToPrecompiled(static_cast<u8>(cbuf.IsIndirect() ? 1 : 0))) { !SaveObjectToPrecompiled(cbuf.IsIndirect())) {
return false; return false;
} }
} }
@ -396,9 +395,9 @@ bool ShaderDiskCacheOpenGL::SaveDecompiledFile(u64 unique_identifier, const std:
if (!SaveObjectToPrecompiled(static_cast<u64>(sampler.GetOffset())) || if (!SaveObjectToPrecompiled(static_cast<u64>(sampler.GetOffset())) ||
!SaveObjectToPrecompiled(static_cast<u64>(sampler.GetIndex())) || !SaveObjectToPrecompiled(static_cast<u64>(sampler.GetIndex())) ||
!SaveObjectToPrecompiled(static_cast<u32>(sampler.GetType())) || !SaveObjectToPrecompiled(static_cast<u32>(sampler.GetType())) ||
!SaveObjectToPrecompiled(static_cast<u8>(sampler.IsArray() ? 1 : 0)) || !SaveObjectToPrecompiled(sampler.IsArray()) ||
!SaveObjectToPrecompiled(static_cast<u8>(sampler.IsShadow() ? 1 : 0)) || !SaveObjectToPrecompiled(sampler.IsShadow()) ||
!SaveObjectToPrecompiled(static_cast<u8>(sampler.IsBindless() ? 1 : 0))) { !SaveObjectToPrecompiled(sampler.IsBindless())) {
return false; return false;
} }
} }
@ -409,14 +408,13 @@ bool ShaderDiskCacheOpenGL::SaveDecompiledFile(u64 unique_identifier, const std:
for (const auto& gmem : entries.global_memory_entries) { for (const auto& gmem : entries.global_memory_entries) {
if (!SaveObjectToPrecompiled(static_cast<u32>(gmem.GetCbufIndex())) || if (!SaveObjectToPrecompiled(static_cast<u32>(gmem.GetCbufIndex())) ||
!SaveObjectToPrecompiled(static_cast<u32>(gmem.GetCbufOffset())) || !SaveObjectToPrecompiled(static_cast<u32>(gmem.GetCbufOffset())) ||
!SaveObjectToPrecompiled(static_cast<u8>(gmem.IsRead() ? 1 : 0)) || !SaveObjectToPrecompiled(gmem.IsRead()) || !SaveObjectToPrecompiled(gmem.IsWritten())) {
!SaveObjectToPrecompiled(static_cast<u8>(gmem.IsWritten() ? 1 : 0))) {
return false; return false;
} }
} }
for (const bool clip_distance : entries.clip_distances) { for (const bool clip_distance : entries.clip_distances) {
if (!SaveObjectToPrecompiled(static_cast<u8>(clip_distance ? 1 : 0))) { if (!SaveObjectToPrecompiled(clip_distance)) {
return false; return false;
} }
} }

View file

@ -70,14 +70,14 @@ namespace std {
template <> template <>
struct hash<OpenGL::BaseBindings> { struct hash<OpenGL::BaseBindings> {
std::size_t operator()(const OpenGL::BaseBindings& bindings) const { std::size_t operator()(const OpenGL::BaseBindings& bindings) const noexcept {
return bindings.cbuf | bindings.gmem << 8 | bindings.sampler << 16; return bindings.cbuf | bindings.gmem << 8 | bindings.sampler << 16;
} }
}; };
template <> template <>
struct hash<OpenGL::ShaderDiskCacheUsage> { struct hash<OpenGL::ShaderDiskCacheUsage> {
std::size_t operator()(const OpenGL::ShaderDiskCacheUsage& usage) const { std::size_t operator()(const OpenGL::ShaderDiskCacheUsage& usage) const noexcept {
return static_cast<std::size_t>(usage.unique_identifier) ^ return static_cast<std::size_t>(usage.unique_identifier) ^
std::hash<OpenGL::BaseBindings>()(usage.bindings) ^ usage.primitive << 16; std::hash<OpenGL::BaseBindings>()(usage.bindings) ^ usage.primitive << 16;
} }
@ -162,6 +162,7 @@ struct ShaderDiskCacheDump {
class ShaderDiskCacheOpenGL { class ShaderDiskCacheOpenGL {
public: public:
explicit ShaderDiskCacheOpenGL(Core::System& system); explicit ShaderDiskCacheOpenGL(Core::System& system);
~ShaderDiskCacheOpenGL();
/// Loads transferable cache. If file has a old version or on failure, it deletes the file. /// 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>>> std::optional<std::pair<std::vector<ShaderDiskCacheRaw>, std::vector<ShaderDiskCacheUsage>>>
@ -259,20 +260,35 @@ private:
return SaveArrayToPrecompiled(&object, 1); return SaveArrayToPrecompiled(&object, 1);
} }
bool SaveObjectToPrecompiled(bool object) {
const auto value = static_cast<u8>(object);
return SaveArrayToPrecompiled(&value, 1);
}
template <typename T> template <typename T>
bool LoadObjectFromPrecompiled(T& object) { bool LoadObjectFromPrecompiled(T& object) {
return LoadArrayFromPrecompiled(&object, 1); return LoadArrayFromPrecompiled(&object, 1);
} }
// Copre system bool LoadObjectFromPrecompiled(bool& object) {
u8 value;
const bool read_ok = LoadArrayFromPrecompiled(&value, 1);
if (!read_ok) {
return false;
}
object = value != 0;
return true;
}
// Core system
Core::System& system; Core::System& system;
// Stored transferable shaders // Stored transferable shaders
std::map<u64, std::unordered_set<ShaderDiskCacheUsage>> transferable; std::map<u64, std::unordered_set<ShaderDiskCacheUsage>> transferable;
// Stores whole precompiled cache which will be read from or saved to the precompiled chache // Stores whole precompiled cache which will be read from/saved to the precompiled cache file
// file
FileSys::VectorVfsFile precompiled_cache_virtual_file; FileSys::VectorVfsFile precompiled_cache_virtual_file;
// Stores the current offset of the precompiled cache file for IO purposes // Stores the current offset of the precompiled cache file for IO purposes
std::size_t precompiled_cache_virtual_file_offset; std::size_t precompiled_cache_virtual_file_offset = 0;
// The cache has been loaded at boot // The cache has been loaded at boot
bool tried_to_load{}; bool tried_to_load{};