gl_shader_disk_cache: Address miscellaneous feedback

This commit is contained in:
ReinUsesLisp 2019-01-15 02:42:25 -03:00
parent 8ee3666a3c
commit 750abcc23d
5 changed files with 57 additions and 43 deletions

View File

@ -181,7 +181,8 @@ CachedProgram SpecializeShader(const std::string& code, const GLShader::ShaderEn
} }
if (program_type == Maxwell::ShaderProgram::Geometry) { if (program_type == Maxwell::ShaderProgram::Geometry) {
const auto [glsl_topology, _, max_vertices] = GetPrimitiveDescription(primitive_mode); const auto [glsl_topology, debug_name, max_vertices] =
GetPrimitiveDescription(primitive_mode);
source += "layout (" + std::string(glsl_topology) + ") in;\n"; source += "layout (" + std::string(glsl_topology) + ") in;\n";
source += "#define MAX_VERTEX_INPUT " + std::to_string(max_vertices) + '\n'; source += "#define MAX_VERTEX_INPUT " + std::to_string(max_vertices) + '\n';
@ -314,7 +315,7 @@ GLuint CachedShader::LazyGeometryProgram(CachedProgram& target_program, BaseBind
if (target_program) { if (target_program) {
return target_program->handle; return target_program->handle;
} }
const auto [_, debug_name, __] = GetPrimitiveDescription(primitive_mode); const auto [glsl_name, debug_name, vertices] = GetPrimitiveDescription(primitive_mode);
target_program = TryLoadProgram(primitive_mode, base_bindings); target_program = TryLoadProgram(primitive_mode, base_bindings);
if (!target_program) { if (!target_program) {
target_program = target_program =
@ -419,7 +420,6 @@ CachedProgram ShaderCacheOpenGL::GeneratePrecompiledProgram(
std::map<u64, UnspecializedShader> ShaderCacheOpenGL::GenerateUnspecializedShaders( std::map<u64, UnspecializedShader> ShaderCacheOpenGL::GenerateUnspecializedShaders(
const std::vector<ShaderDiskCacheRaw>& raws, const std::vector<ShaderDiskCacheRaw>& raws,
const std::map<u64, ShaderDiskCacheDecompiled>& decompiled) { const std::map<u64, ShaderDiskCacheDecompiled>& decompiled) {
std::map<u64, UnspecializedShader> unspecialized; std::map<u64, UnspecializedShader> unspecialized;
for (const auto& raw : raws) { for (const auto& raw : raws) {

View File

@ -86,9 +86,9 @@ private:
ShaderDiskCacheUsage GetUsage(GLenum primitive_mode, BaseBindings base_bindings) const; ShaderDiskCacheUsage GetUsage(GLenum primitive_mode, BaseBindings base_bindings) const;
const VAddr addr; VAddr addr{};
const u64 unique_identifier; u64 unique_identifier{};
const Maxwell::ShaderProgram program_type; Maxwell::ShaderProgram program_type{};
ShaderDiskCacheOpenGL& disk_cache; ShaderDiskCacheOpenGL& disk_cache;
const PrecompiledPrograms& precompiled_programs; const PrecompiledPrograms& precompiled_programs;

View File

@ -19,7 +19,11 @@ class ShaderIR;
namespace OpenGL::GLShader { namespace OpenGL::GLShader {
struct ShaderEntries;
using Maxwell = Tegra::Engines::Maxwell3D::Regs; using Maxwell = Tegra::Engines::Maxwell3D::Regs;
using ProgramResult = std::pair<std::string, ShaderEntries>;
using SamplerEntry = VideoCommon::Shader::Sampler;
class ConstBufferEntry : public VideoCommon::Shader::ConstBuffer { class ConstBufferEntry : public VideoCommon::Shader::ConstBuffer {
public: public:
@ -34,8 +38,6 @@ private:
u32 index{}; u32 index{};
}; };
using SamplerEntry = VideoCommon::Shader::Sampler;
class GlobalMemoryEntry { class GlobalMemoryEntry {
public: public:
explicit GlobalMemoryEntry(u32 cbuf_index, u32 cbuf_offset) explicit GlobalMemoryEntry(u32 cbuf_index, u32 cbuf_offset)
@ -62,8 +64,6 @@ struct ShaderEntries {
std::size_t shader_length{}; std::size_t shader_length{};
}; };
using ProgramResult = std::pair<std::string, ShaderEntries>;
std::string GetCommonDeclarations(); std::string GetCommonDeclarations();
ProgramResult Decompile(const VideoCommon::Shader::ShaderIR& ir, Maxwell::ShaderStage stage, ProgramResult Decompile(const VideoCommon::Shader::ShaderIR& ir, Maxwell::ShaderStage stage,

View File

@ -49,7 +49,7 @@ std::string GetTitleID() {
std::string GetShaderHash() { std::string GetShaderHash() {
std::array<char, ShaderHashSize> hash{}; std::array<char, ShaderHashSize> hash{};
std::strncpy(hash.data(), Common::g_shader_cache_version, ShaderHashSize); std::strncpy(hash.data(), Common::g_shader_cache_version, ShaderHashSize);
return std::string(hash.data()); return std::string(hash.data(), hash.size());
} }
template <typename T> template <typename T>
@ -86,7 +86,8 @@ ShaderDiskCacheRaw::ShaderDiskCacheRaw(FileUtil::IOFile& file) {
file.ReadBytes(&unique_identifier, sizeof(u64)); file.ReadBytes(&unique_identifier, sizeof(u64));
file.ReadBytes(&program_type, sizeof(u32)); file.ReadBytes(&program_type, sizeof(u32));
u32 program_code_size{}, program_code_size_b{}; u32 program_code_size{};
u32 program_code_size_b{};
file.ReadBytes(&program_code_size, sizeof(u32)); file.ReadBytes(&program_code_size, sizeof(u32));
file.ReadBytes(&program_code_size_b, sizeof(u32)); file.ReadBytes(&program_code_size_b, sizeof(u32));
@ -99,6 +100,15 @@ ShaderDiskCacheRaw::ShaderDiskCacheRaw(FileUtil::IOFile& file) {
} }
} }
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;
void ShaderDiskCacheRaw::Save(FileUtil::IOFile& file) const { void ShaderDiskCacheRaw::Save(FileUtil::IOFile& file) const {
file.WriteObject(unique_identifier); file.WriteObject(unique_identifier);
file.WriteObject(static_cast<u32>(program_type)); file.WriteObject(static_cast<u32>(program_type));
@ -186,7 +196,7 @@ ShaderDiskCacheOpenGL::LoadPrecompiled() {
char precompiled_hash[ShaderHashSize]; char precompiled_hash[ShaderHashSize];
file.ReadBytes(&precompiled_hash, ShaderHashSize); file.ReadBytes(&precompiled_hash, ShaderHashSize);
if (std::string(precompiled_hash) != GetShaderHash()) { if (precompiled_hash != GetShaderHash()) {
LOG_INFO(Render_OpenGL, "Precompiled cache is from another version of yuzu - removing"); LOG_INFO(Render_OpenGL, "Precompiled cache is from another version of yuzu - removing");
file.Close(); file.Close();
InvalidatePrecompiled(); InvalidatePrecompiled();
@ -311,13 +321,13 @@ ShaderDiskCacheOpenGL::LoadPrecompiled() {
return {decompiled, dumps}; return {decompiled, dumps};
} }
void ShaderDiskCacheOpenGL::InvalidateTransferable() const { bool ShaderDiskCacheOpenGL::InvalidateTransferable() const {
FileUtil::Delete(GetTransferablePath()); const bool success = FileUtil::Delete(GetTransferablePath());
InvalidatePrecompiled(); return InvalidatePrecompiled() && success;
} }
void ShaderDiskCacheOpenGL::InvalidatePrecompiled() const { bool ShaderDiskCacheOpenGL::InvalidatePrecompiled() const {
FileUtil::Delete(GetPrecompiledPath()); return FileUtil::Delete(GetPrecompiledPath());
} }
void ShaderDiskCacheOpenGL::SaveRaw(const ShaderDiskCacheRaw& entry) { void ShaderDiskCacheOpenGL::SaveRaw(const ShaderDiskCacheRaw& entry) {

View File

@ -15,22 +15,20 @@
#include "common/assert.h" #include "common/assert.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/file_util.h"
#include "video_core/engines/maxwell_3d.h" #include "video_core/engines/maxwell_3d.h"
#include "video_core/renderer_opengl/gl_shader_gen.h" #include "video_core/renderer_opengl/gl_shader_gen.h"
namespace FileUtil {
class IOFile;
} // namespace FileUtil
namespace OpenGL { namespace OpenGL {
using ProgramCode = std::vector<u64>; using ProgramCode = std::vector<u64>;
using Maxwell = Tegra::Engines::Maxwell3D::Regs; using Maxwell = Tegra::Engines::Maxwell3D::Regs;
/// Allocated bindings used by an OpenGL shader program
struct BaseBindings { struct BaseBindings {
private:
auto Tie() const {
return std::tie(cbuf, gmem, sampler);
}
public:
u32 cbuf{}; u32 cbuf{};
u32 gmem{}; u32 gmem{};
u32 sampler{}; u32 sampler{};
@ -44,20 +42,24 @@ public:
} }
bool operator!=(const BaseBindings& rhs) const { bool operator!=(const BaseBindings& rhs) const {
return !this->operator==(rhs); return !operator==(rhs);
}
std::tuple<u32, u32, u32> Tie() const {
return std::tie(cbuf, gmem, sampler);
} }
}; };
/// Describes a shader how it's used by the guest GPU
class ShaderDiskCacheRaw { class ShaderDiskCacheRaw {
public: public:
explicit ShaderDiskCacheRaw(FileUtil::IOFile& file); explicit ShaderDiskCacheRaw(FileUtil::IOFile& file);
explicit ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type, explicit ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type,
u32 program_code_size, u32 program_code_size_b, u32 program_code_size, u32 program_code_size_b,
ProgramCode program_code, ProgramCode program_code_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}, ~ShaderDiskCacheRaw();
program_code{std::move(program_code)}, program_code_b{std::move(program_code_b)} {}
void Save(FileUtil::IOFile& file) const; void Save(FileUtil::IOFile& file) const;
@ -108,17 +110,8 @@ private:
ProgramCode program_code_b; ProgramCode program_code_b;
}; };
/// Describes how a shader is used
struct ShaderDiskCacheUsage { struct ShaderDiskCacheUsage {
private:
auto Tie() const {
return std::tie(unique_identifier, bindings, primitive);
}
public:
u64 unique_identifier{};
BaseBindings bindings;
GLenum primitive{};
bool operator<(const ShaderDiskCacheUsage& rhs) const { bool operator<(const ShaderDiskCacheUsage& rhs) const {
return Tie() < rhs.Tie(); return Tie() < rhs.Tie();
} }
@ -128,15 +121,26 @@ public:
} }
bool operator!=(const ShaderDiskCacheUsage& rhs) const { bool operator!=(const ShaderDiskCacheUsage& rhs) const {
return !this->operator==(rhs); return !operator==(rhs);
}
u64 unique_identifier{};
BaseBindings bindings;
GLenum primitive{};
private:
std::tuple<u64, BaseBindings, GLenum> Tie() const {
return std::tie(unique_identifier, bindings, primitive);
} }
}; };
/// Contains decompiled data from a shader
struct ShaderDiskCacheDecompiled { struct ShaderDiskCacheDecompiled {
std::string code; std::string code;
GLShader::ShaderEntries entries; GLShader::ShaderEntries entries;
}; };
/// Contains an OpenGL dumped binary program
struct ShaderDiskCacheDump { struct ShaderDiskCacheDump {
GLenum binary_format; GLenum binary_format;
std::vector<u8> binary; std::vector<u8> binary;
@ -154,10 +158,10 @@ public:
LoadPrecompiled(); LoadPrecompiled();
/// Removes the transferable (and precompiled) cache file. /// Removes the transferable (and precompiled) cache file.
void InvalidateTransferable() const; bool InvalidateTransferable() const;
/// Removes the precompiled cache file. /// Removes the precompiled cache file.
void InvalidatePrecompiled() const; bool InvalidatePrecompiled() const;
/// Saves a raw dump to the transferable file. Checks for collisions. /// Saves a raw dump to the transferable file. Checks for collisions.
void SaveRaw(const ShaderDiskCacheRaw& entry); void SaveRaw(const ShaderDiskCacheRaw& entry);