Просмотр исходного кода

gl_shader_disk_cache: Add ShaderDiskCacheOpenGL class and helpers

ReinUsesLisp 7 лет назад
Родитель
Сommit
98be5a4928

+ 52 - 0
src/video_core/renderer_opengl/gl_shader_disk_cache.cpp

@@ -4,6 +4,18 @@
 
 #pragma once
 
+#include <fmt/format.h>
+
+#include "common/assert.h"
+#include "common/common_paths.h"
+#include "common/common_types.h"
+#include "common/file_util.h"
+#include "common/logging/log.h"
+
+#include "core/core.h"
+#include "core/hle/kernel/process.h"
+
+#include "video_core/renderer_opengl/gl_shader_cache.h"
 #include "video_core/renderer_opengl/gl_shader_disk_cache.h"
 
 namespace OpenGL {
@@ -11,4 +23,44 @@ namespace OpenGL {
 // Making sure sizes doesn't change by accident
 static_assert(sizeof(BaseBindings) == 12);
 
+namespace {
+std::string GetTitleID() {
+    return fmt::format("{:016X}", Core::CurrentProcess()->GetTitleID());
+}
+} // namespace
+
+bool ShaderDiskCacheOpenGL::EnsureDirectories() const {
+    const auto CreateDir = [](const std::string& dir) {
+        if (!FileUtil::CreateDir(dir)) {
+            LOG_ERROR(Render_OpenGL, "Failed to create directory={}", dir);
+            return false;
+        }
+        return true;
+    };
+
+    return CreateDir(FileUtil::GetUserPath(FileUtil::UserPath::ShaderDir)) &&
+           CreateDir(GetBaseDir()) && CreateDir(GetTransferableDir()) &&
+           CreateDir(GetPrecompiledDir());
+}
+
+std::string ShaderDiskCacheOpenGL::GetTransferablePath() const {
+    return FileUtil::SanitizePath(GetTransferableDir() + DIR_SEP_CHR + GetTitleID() + ".bin");
+}
+
+std::string ShaderDiskCacheOpenGL::GetPrecompiledPath() const {
+    return FileUtil::SanitizePath(GetPrecompiledDir() + DIR_SEP_CHR + GetTitleID() + ".bin");
+}
+
+std::string ShaderDiskCacheOpenGL::GetTransferableDir() const {
+    return GetBaseDir() + DIR_SEP "transferable";
+}
+
+std::string ShaderDiskCacheOpenGL::GetPrecompiledDir() const {
+    return GetBaseDir() + DIR_SEP "precompiled";
+}
+
+std::string ShaderDiskCacheOpenGL::GetBaseDir() const {
+    return FileUtil::GetUserPath(FileUtil::UserPath::ShaderDir) + DIR_SEP "opengl";
+}
+
 } // namespace OpenGL

+ 24 - 0
src/video_core/renderer_opengl/gl_shader_disk_cache.h

@@ -4,9 +4,11 @@
 
 #pragma once
 
+#include <string>
 #include <tuple>
 
 #include "common/common_types.h"
+#include "common/file_util.h"
 #include "video_core/engines/maxwell_3d.h"
 
 namespace OpenGL {
@@ -38,4 +40,26 @@ public:
     }
 };
 
+class ShaderDiskCacheOpenGL {
+public:
+private:
+    /// 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;
+};
+
 } // namespace OpenGL