1
0
Fork 0
forked from suyu/suyu

gl_shader_disk_cache: Add precompiled save

This commit is contained in:
ReinUsesLisp 2019-01-13 23:33:00 -03:00
parent 3435cd8d5e
commit 57fb15d2a3
2 changed files with 57 additions and 0 deletions

View file

@ -27,6 +27,9 @@ enum class EntryKind : u32 {
constexpr u32 NativeVersion = 1; constexpr u32 NativeVersion = 1;
// TODO(Rodrigo): Hash files
constexpr u64 PrecompiledHash = 0xdeadbeefdeadbeef;
// Making sure sizes doesn't change by accident // Making sure sizes doesn't change by accident
static_assert(sizeof(BaseBindings) == 12); static_assert(sizeof(BaseBindings) == 12);
static_assert(sizeof(ShaderDiskCacheUsage) == 24); static_assert(sizeof(ShaderDiskCacheUsage) == 24);
@ -153,6 +156,26 @@ void ShaderDiskCacheOpenGL::SaveUsage(const ShaderDiskCacheUsage& usage) {
file.WriteObject(usage); file.WriteObject(usage);
} }
void ShaderDiskCacheOpenGL::SavePrecompiled(const ShaderDiskCacheUsage& usage, GLuint program) {
FileUtil::IOFile file = AppendPrecompiledFile();
if (!file.IsOpen()) {
return;
}
file.WriteObject(usage);
GLint binary_length{};
glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binary_length);
GLenum binary_format{};
std::vector<u8> binary(binary_length);
glGetProgramBinary(program, binary_length, nullptr, &binary_format, binary.data());
file.WriteObject(static_cast<u32>(binary_format));
file.WriteObject(static_cast<u32>(binary_length));
file.WriteArray(binary.data(), binary.size());
}
FileUtil::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const { FileUtil::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const {
if (!EnsureDirectories()) { if (!EnsureDirectories()) {
return {}; return {};
@ -173,6 +196,26 @@ FileUtil::IOFile ShaderDiskCacheOpenGL::AppendTransferableFile() const {
return file; return file;
} }
FileUtil::IOFile ShaderDiskCacheOpenGL::AppendPrecompiledFile() const {
if (!EnsureDirectories()) {
return {};
}
const auto precompiled_path{GetPrecompiledPath()};
const bool existed = FileUtil::Exists(precompiled_path);
FileUtil::IOFile file(precompiled_path, "ab");
if (!file.IsOpen()) {
LOG_ERROR(Render_OpenGL, "Failed to open precompiled cache in path={}", precompiled_path);
return {};
}
if (!existed || file.GetSize() == 0) {
file.WriteObject(PrecompiledHash);
}
return file;
}
bool ShaderDiskCacheOpenGL::EnsureDirectories() const { bool ShaderDiskCacheOpenGL::EnsureDirectories() const {
const auto CreateDir = [](const std::string& dir) { const auto CreateDir = [](const std::string& dir) {
if (!FileUtil::CreateDir(dir)) { if (!FileUtil::CreateDir(dir)) {

View file

@ -130,6 +130,14 @@ public:
} }
}; };
struct ShaderDiskCachePrecompiledEntry {
ShaderDiskCacheUsage usage;
GLenum binary_format;
std::vector<u8> binary;
std::string code;
GLShader::ShaderEntries entries;
};
class ShaderDiskCacheOpenGL { class ShaderDiskCacheOpenGL {
public: public:
/// Loads transferable cache. If file has a old version, it deletes it. Returns true on success. /// Loads transferable cache. If file has a old version, it deletes it. Returns true on success.
@ -142,10 +150,16 @@ public:
/// Saves shader usage to the transferable file. Does not check for collisions. /// Saves shader usage to the transferable file. Does not check for collisions.
void SaveUsage(const ShaderDiskCacheUsage& usage); void SaveUsage(const ShaderDiskCacheUsage& usage);
/// Saves a precompiled shader entry. Does not check for collisions.
void SavePrecompiled(const ShaderDiskCacheUsage& usage, GLuint program);
private: private:
/// Opens current game's transferable file and write it's header if it doesn't exist /// Opens current game's transferable file and write it's header if it doesn't exist
FileUtil::IOFile AppendTransferableFile() const; FileUtil::IOFile AppendTransferableFile() const;
/// Opens current game's precompiled file and write it's header if it doesn't exist
FileUtil::IOFile AppendPrecompiledFile() const;
/// Create shader disk cache directories. Returns true on success. /// Create shader disk cache directories. Returns true on success.
bool EnsureDirectories() const; bool EnsureDirectories() const;