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-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"
|
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2019-01-14 03:29:24 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_gen.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 {
|
|
|
|
|
|
|
|
using ProgramCode = std::vector<u64>;
|
|
|
|
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
|
|
|
|
|
2019-01-15 06:42:25 +01: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{};
|
|
|
|
|
|
|
|
bool operator==(const BaseBindings& rhs) const {
|
2019-02-03 01:14:36 +01:00
|
|
|
return std::tie(cbuf, gmem, sampler) == std::tie(rhs.cbuf, rhs.gmem, rhs.sampler);
|
2019-01-14 02:36:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const BaseBindings& rhs) const {
|
2019-01-15 06:42:25 +01:00
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
2019-02-03 01:14:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Describes how a shader is used
|
|
|
|
struct ShaderDiskCacheUsage {
|
|
|
|
u64 unique_identifier{};
|
|
|
|
BaseBindings bindings;
|
|
|
|
GLenum primitive{};
|
|
|
|
|
|
|
|
bool operator==(const ShaderDiskCacheUsage& rhs) const {
|
|
|
|
return std::tie(unique_identifier, bindings, primitive) ==
|
|
|
|
std::tie(rhs.unique_identifier, rhs.bindings, rhs.primitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const ShaderDiskCacheUsage& rhs) const {
|
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OpenGL
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct hash<OpenGL::BaseBindings> {
|
|
|
|
std::size_t operator()(const OpenGL::BaseBindings& bindings) const {
|
|
|
|
return bindings.cbuf | bindings.gmem << 8 | bindings.sampler << 16;
|
|
|
|
}
|
|
|
|
};
|
2019-01-15 06:42:25 +01:00
|
|
|
|
2019-02-03 01:14:36 +01:00
|
|
|
template <>
|
|
|
|
struct hash<OpenGL::ShaderDiskCacheUsage> {
|
|
|
|
std::size_t operator()(const OpenGL::ShaderDiskCacheUsage& usage) const {
|
|
|
|
return static_cast<std::size_t>(usage.unique_identifier) ^
|
|
|
|
std::hash<OpenGL::BaseBindings>()(usage.bindings) ^ usage.primitive << 16;
|
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:
|
|
|
|
explicit ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type,
|
|
|
|
u32 program_code_size, u32 program_code_size_b,
|
2019-01-15 06:42:25 +01: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 {
|
|
|
|
return program_type == Maxwell::ShaderProgram::VertexA;
|
|
|
|
}
|
|
|
|
|
|
|
|
Maxwell::ShaderProgram GetProgramType() const {
|
|
|
|
return program_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
Maxwell::ShaderStage GetProgramStage() const {
|
|
|
|
switch (program_type) {
|
|
|
|
case Maxwell::ShaderProgram::VertexA:
|
|
|
|
case Maxwell::ShaderProgram::VertexB:
|
|
|
|
return Maxwell::ShaderStage::Vertex;
|
|
|
|
case Maxwell::ShaderProgram::TesselationControl:
|
|
|
|
return Maxwell::ShaderStage::TesselationControl;
|
|
|
|
case Maxwell::ShaderProgram::TesselationEval:
|
|
|
|
return Maxwell::ShaderStage::TesselationEval;
|
|
|
|
case Maxwell::ShaderProgram::Geometry:
|
|
|
|
return Maxwell::ShaderStage::Geometry;
|
|
|
|
case Maxwell::ShaderProgram::Fragment:
|
|
|
|
return Maxwell::ShaderStage::Fragment;
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProgramCode& GetProgramCode() const {
|
|
|
|
return program_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProgramCode& GetProgramCodeB() const {
|
|
|
|
return program_code_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
u64 unique_identifier{};
|
|
|
|
Maxwell::ShaderProgram program_type{};
|
|
|
|
u32 program_code_size{};
|
|
|
|
u32 program_code_size_b{};
|
|
|
|
|
|
|
|
ProgramCode program_code;
|
|
|
|
ProgramCode program_code_b;
|
|
|
|
};
|
|
|
|
|
2019-01-15 06:42:25 +01:00
|
|
|
/// Contains decompiled data from a shader
|
2019-01-15 05:07:57 +01:00
|
|
|
struct ShaderDiskCacheDecompiled {
|
2019-01-14 03:33:00 +01:00
|
|
|
std::string code;
|
|
|
|
GLShader::ShaderEntries entries;
|
|
|
|
};
|
|
|
|
|
2019-01-15 06:42:25 +01:00
|
|
|
/// Contains an OpenGL dumped binary program
|
2019-01-15 05:07:57 +01:00
|
|
|
struct ShaderDiskCacheDump {
|
|
|
|
GLenum binary_format;
|
|
|
|
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-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-02-03 01:14:36 +01:00
|
|
|
std::pair<std::unordered_map<u64, ShaderDiskCacheDecompiled>,
|
|
|
|
std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump>>
|
2019-01-15 06:17:38 +01:00
|
|
|
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-01-15 19:22:25 +01:00
|
|
|
void InvalidateTransferable() const;
|
2019-01-14 05:25:25 +01:00
|
|
|
|
2019-01-14 03:33:31 +01:00
|
|
|
/// Removes the precompiled cache file.
|
2019-01-15 19:22:25 +01:00
|
|
|
void InvalidatePrecompiled() const;
|
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 decompiled entry to the precompiled file. Does not check for collisions.
|
|
|
|
void SaveDecompiled(u64 unique_identifier, const std::string& code,
|
|
|
|
const GLShader::ShaderEntries& entries);
|
|
|
|
|
|
|
|
/// 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-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-02-03 01:14:36 +01:00
|
|
|
std::optional<std::pair<std::unordered_map<u64, ShaderDiskCacheDecompiled>,
|
|
|
|
std::unordered_map<ShaderDiskCacheUsage, ShaderDiskCacheDump>>>
|
2019-01-15 19:22:25 +01:00
|
|
|
LoadPrecompiledFile(FileUtil::IOFile& file);
|
|
|
|
|
|
|
|
/// Loads a decompiled cache entry from the passed file. Returns empty on failure.
|
|
|
|
std::optional<ShaderDiskCacheDecompiled> LoadDecompiledEntry(FileUtil::IOFile& file);
|
|
|
|
|
|
|
|
/// Saves a decompiled entry to the passed file. Returns true on success.
|
|
|
|
bool SaveDecompiledFile(FileUtil::IOFile& file, u64 unique_identifier, const std::string& code,
|
|
|
|
const GLShader::ShaderEntries& entries);
|
|
|
|
|
|
|
|
/// Returns if the cache can be used
|
|
|
|
bool IsUsable() const;
|
|
|
|
|
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-01-14 03:33:00 +01:00
|
|
|
/// Opens current game's precompiled file and write it's header if it doesn't exist
|
|
|
|
FileUtil::IOFile AppendPrecompiledFile() const;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Copre system
|
|
|
|
Core::System& system;
|
2019-01-14 03:29:24 +01:00
|
|
|
// Stored transferable shaders
|
2019-02-03 01:14:36 +01:00
|
|
|
std::map<u64, std::unordered_set<ShaderDiskCacheUsage>> transferable;
|
2019-01-15 19:22:25 +01:00
|
|
|
// The cache has been loaded at boot
|
|
|
|
bool tried_to_load{};
|
2019-01-14 03:22:15 +01:00
|
|
|
};
|
|
|
|
|
2019-01-14 02:36:52 +01:00
|
|
|
} // namespace OpenGL
|