From 55695bb5bc91d7cbda6f3f45f609c9152d408933 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 20 Oct 2021 17:41:45 +1000 Subject: [PATCH] GS/OpenGL: Use ShaderCache --- pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.cpp | 16 +++++++++------- pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.h | 5 +++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.cpp b/pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.cpp index 50cc68cc39..01936b42de 100644 --- a/pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.cpp +++ b/pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.cpp @@ -14,6 +14,7 @@ */ #include "PrecompiledHeader.h" +#include "common/StringUtil.h" #include "GS/GSState.h" #include "GSDeviceOGL.h" #include "GLState.h" @@ -262,6 +263,9 @@ bool GSDeviceOGL::Create(const WindowInfo& wi) return false; } + if (!m_shader_cache.Open(false, StringUtil::wxStringToUTF8String(EmuFolders::Cache.ToString()), SHADER_VERSION)) + Console.Warning("Shader cache failed to open."); + { auto shader = Host::ReadResourceFileToString("shaders/opengl/common_header.glsl"); if (!shader.has_value()) @@ -387,7 +391,7 @@ bool GSDeviceOGL::Create(const WindowInfo& wi) format("#define PS_SCALE_FACTOR %d\n", m_upscale_multiplier) : std::string(); const std::string ps(GetShaderSource(name, GL_FRAGMENT_SHADER, m_shader_common_header, *shader, macro_sel)); - if (!m_convert.ps[i].Compile(m_convert.vs, {}, ps) || !m_convert.ps[i].Link()) + if (!m_shader_cache.GetProgram(&m_convert.ps[i], m_convert.vs, {}, ps)) return false; m_convert.ps[i].SetFormattedName("Convert pipe %s", name); } @@ -422,7 +426,7 @@ bool GSDeviceOGL::Create(const WindowInfo& wi) for (size_t i = 0; i < std::size(m_merge_obj.ps); i++) { const std::string ps(GetShaderSource(format("ps_main%d", i), GL_FRAGMENT_SHADER, m_shader_common_header, *shader, {})); - if (!m_merge_obj.ps[i].Compile(m_convert.vs, {}, ps) || !m_merge_obj.ps[i].Link()) + if (!m_shader_cache.GetProgram(&m_merge_obj.ps[i], m_convert.vs, {}, ps)) return false; m_merge_obj.ps[i].SetFormattedName("Merge pipe %zu", i); } @@ -445,7 +449,7 @@ bool GSDeviceOGL::Create(const WindowInfo& wi) for (size_t i = 0; i < std::size(m_interlace.ps); i++) { const std::string ps(GetShaderSource(format("ps_main%d", i), GL_FRAGMENT_SHADER, m_shader_common_header, *shader, {})); - if (!m_interlace.ps[i].Compile(m_convert.vs, {}, ps) || !m_interlace.ps[i].Link()) + if (!m_shader_cache.GetProgram(&m_interlace.ps[i], m_convert.vs, {}, ps)) return false; m_interlace.ps[i].SetFormattedName("Merge pipe %zu", i); } @@ -469,7 +473,7 @@ bool GSDeviceOGL::Create(const WindowInfo& wi) return false; const std::string ps(GetShaderSource("ps_main", GL_FRAGMENT_SHADER, m_shader_common_header, *shader, shade_macro)); - if (!m_shadeboost.ps.Compile(m_convert.vs, {}, ps) || !m_shadeboost.ps.Link()) + if (!m_shader_cache.GetProgram(&m_shadeboost.ps, m_convert.vs, {}, ps)) return false; m_shadeboost.ps.SetName("Shadeboost pipe"); } @@ -1769,9 +1773,7 @@ void GSDeviceOGL::SetupPipeline(const ProgramSelector& psel) const std::string gs((psel.gs.key != 0) ? GetGSSource(psel.gs) : std::string()); GL::Program prog; - if (prog.Compile(vs, gs, ps)) - prog.Link(); - + m_shader_cache.GetProgram(&prog, vs, gs, ps); it = m_programs.emplace(psel, std::move(prog)).first; it->second.Bind(); } diff --git a/pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.h b/pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.h index 1f0ed042bc..edbe6465df 100644 --- a/pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.h +++ b/pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.h @@ -18,6 +18,7 @@ #include "common/GL/Context.h" #include "common/GL/StreamBuffer.h" #include "common/GL/Program.h" +#include "common/GL/ShaderCache.h" #include "common/HashCombine.h" #include "GS/Renderers/Common/GSDevice.h" #include "GSTextureOGL.h" @@ -213,6 +214,9 @@ public: static int m_shader_reg; private: + // Increment this constant whenever shaders change, to invalidate user's program binary cache. + static constexpr u32 SHADER_VERSION = 1; + std::unique_ptr m_gl_context; int m_mipmap; int m_upscale_multiplier; @@ -299,6 +303,7 @@ private: GLuint m_ps_ss[1 << 7]; GSDepthStencilOGL* m_om_dss[1 << 5]; std::unordered_map m_programs; + GL::ShaderCache m_shader_cache; GLuint m_palette_ss;