From fb384dec5501b22ac64b6770a0ae2970e24c8d27 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 26 Jul 2019 17:48:33 -0400 Subject: [PATCH 1/4] OGL/ProgramShaderCache: Use std::string_view where applicable --- .../VideoBackends/OGL/ProgramShaderCache.cpp | 55 +++++++++++-------- .../VideoBackends/OGL/ProgramShaderCache.h | 12 ++-- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp index c9bbdc2b36..8c69317531 100644 --- a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp +++ b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp @@ -4,6 +4,7 @@ #include "VideoBackends/OGL/ProgramShaderCache.h" +#include #include #include #include @@ -267,19 +268,19 @@ void ProgramShaderCache::UploadConstants(const void* data, u32 data_size) ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size); } -bool ProgramShaderCache::CompileComputeShader(SHADER& shader, const std::string& code) +bool ProgramShaderCache::CompileComputeShader(SHADER& shader, std::string_view code) { // We need to enable GL_ARB_compute_shader for drivers that support the extension, // but not GLSL 4.3. Mesa is one example. - std::string header; + std::string full_code; if (g_ActiveConfig.backend_info.bSupportsComputeShaders && g_ogl_config.eSupportedGLSLVersion < Glsl430) { - header = "#extension GL_ARB_compute_shader : enable\n"; + full_code = "#extension GL_ARB_compute_shader : enable\n"; } - std::string full_code = header + code; - GLuint shader_id = CompileSingleShader(GL_COMPUTE_SHADER, full_code); + full_code += code; + const GLuint shader_id = CompileSingleShader(GL_COMPUTE_SHADER, full_code); if (!shader_id) return false; @@ -291,7 +292,7 @@ bool ProgramShaderCache::CompileComputeShader(SHADER& shader, const std::string& // original shaders aren't needed any more glDeleteShader(shader_id); - if (!CheckProgramLinkResult(shader.glprogid, &full_code, nullptr, nullptr)) + if (!CheckProgramLinkResult(shader.glprogid, full_code, {}, {})) { shader.Destroy(); return false; @@ -301,13 +302,21 @@ bool ProgramShaderCache::CompileComputeShader(SHADER& shader, const std::string& return true; } -GLuint ProgramShaderCache::CompileSingleShader(GLenum type, const std::string& code) +GLuint ProgramShaderCache::CompileSingleShader(GLenum type, std::string_view code) { - GLuint result = glCreateShader(type); + const GLuint result = glCreateShader(type); - const char* src[] = {s_glsl_header.c_str(), code.c_str()}; + constexpr GLsizei num_strings = 2; + const std::array src{ + s_glsl_header.data(), + code.data(), + }; + const std::array src_sizes{ + static_cast(s_glsl_header.size()), + static_cast(code.size()), + }; - glShaderSource(result, 2, src, nullptr); + glShaderSource(result, num_strings, src.data(), src_sizes.data()); glCompileShader(result); if (!CheckShaderCompileResult(result, type, code)) @@ -320,7 +329,7 @@ GLuint ProgramShaderCache::CompileSingleShader(GLenum type, const std::string& c return result; } -bool ProgramShaderCache::CheckShaderCompileResult(GLuint id, GLenum type, const std::string& code) +bool ProgramShaderCache::CheckShaderCompileResult(GLuint id, GLenum type, std::string_view code) { GLint compileStatus; glGetShaderiv(id, GL_COMPILE_STATUS, &compileStatus); @@ -374,8 +383,8 @@ bool ProgramShaderCache::CheckShaderCompileResult(GLuint id, GLenum type, const return true; } -bool ProgramShaderCache::CheckProgramLinkResult(GLuint id, const std::string* vcode, - const std::string* pcode, const std::string* gcode) +bool ProgramShaderCache::CheckProgramLinkResult(GLuint id, std::string_view vcode, + std::string_view pcode, std::string_view gcode) { GLint linkStatus; glGetProgramiv(id, GL_LINK_STATUS, &linkStatus); @@ -393,12 +402,12 @@ bool ProgramShaderCache::CheckProgramLinkResult(GLuint id, const std::string* vc StringFromFormat("%sbad_p_%d.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++); std::ofstream file; File::OpenFStream(file, filename, std::ios_base::out); - if (vcode) - file << s_glsl_header << *vcode << '\n'; - if (gcode) - file << s_glsl_header << *gcode << '\n'; - if (pcode) - file << s_glsl_header << *pcode << '\n'; + if (!vcode.empty()) + file << s_glsl_header << vcode << '\n'; + if (!gcode.empty()) + file << s_glsl_header << gcode << '\n'; + if (!pcode.empty()) + file << s_glsl_header << pcode << '\n'; file << info_log; file.close(); @@ -583,10 +592,10 @@ PipelineProgram* ProgramShaderCache::GetPipelineProgram(const GLVertexFormat* ve if (!s_is_shared_context && vao != s_last_VAO) glBindVertexArray(s_last_VAO); - if (!ProgramShaderCache::CheckProgramLinkResult( - prog->shader.glprogid, vertex_shader ? &vertex_shader->GetSource() : nullptr, - geometry_shader ? &geometry_shader->GetSource() : nullptr, - pixel_shader ? &pixel_shader->GetSource() : nullptr)) + if (!CheckProgramLinkResult(prog->shader.glprogid, + vertex_shader ? vertex_shader->GetSource() : std::string_view{}, + geometry_shader ? geometry_shader->GetSource() : std::string_view{}, + pixel_shader ? pixel_shader->GetSource() : std::string_view{})) { prog->shader.Destroy(); return nullptr; diff --git a/Source/Core/VideoBackends/OGL/ProgramShaderCache.h b/Source/Core/VideoBackends/OGL/ProgramShaderCache.h index bc93445c55..72ec79b5ac 100644 --- a/Source/Core/VideoBackends/OGL/ProgramShaderCache.h +++ b/Source/Core/VideoBackends/OGL/ProgramShaderCache.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include "Common/GL/GLUtil.h" @@ -75,11 +75,11 @@ public: static void InvalidateVertexFormatIfBound(GLuint vao); static void InvalidateLastProgram(); - static bool CompileComputeShader(SHADER& shader, const std::string& code); - static GLuint CompileSingleShader(GLenum type, const std::string& code); - static bool CheckShaderCompileResult(GLuint id, GLenum type, const std::string& code); - static bool CheckProgramLinkResult(GLuint id, const std::string* vcode, const std::string* pcode, - const std::string* gcode); + static bool CompileComputeShader(SHADER& shader, std::string_view code); + static GLuint CompileSingleShader(GLenum type, std::string_view code); + static bool CheckShaderCompileResult(GLuint id, GLenum type, std::string_view code); + static bool CheckProgramLinkResult(GLuint id, std::string_view vcode, std::string_view pcode, + std::string_view gcode); static StreamBuffer* GetUniformBuffer(); static u32 GetUniformBufferAlignment(); static void UploadConstants(); From 6e69e3cf26afe3fe436fb0ae5d25b886e877aa08 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 26 Jul 2019 18:09:41 -0400 Subject: [PATCH 2/4] OGL/ProgramShaderCache: Remove unused headers Removes a few inclusions that aren't necessary, reducing the number of header dependencies. --- Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp index 8c69317531..65ee2437a4 100644 --- a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp +++ b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include @@ -18,10 +17,8 @@ #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" #include "Common/StringUtil.h" -#include "Common/Timer.h" #include "Core/ConfigManager.h" -#include "Core/Host.h" #include "VideoBackends/OGL/OGLShader.h" #include "VideoBackends/OGL/Render.h" @@ -29,14 +26,11 @@ #include "VideoBackends/OGL/VertexManager.h" #include "VideoCommon/AsyncShaderCompiler.h" -#include "VideoCommon/DriverDetails.h" #include "VideoCommon/GeometryShaderManager.h" -#include "VideoCommon/ImageWrite.h" #include "VideoCommon/PixelShaderManager.h" #include "VideoCommon/Statistics.h" #include "VideoCommon/VertexLoaderManager.h" #include "VideoCommon/VertexShaderManager.h" -#include "VideoCommon/VideoCommon.h" namespace OGL { From d6617d399f3428c43cf1094710e0a960d2784d62 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 26 Jul 2019 18:22:52 -0400 Subject: [PATCH 3/4] OGL/ProgramShaderCache: Use std::lock_guard deduction guides where applicable Same thing, less reading. --- Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp index 65ee2437a4..144d35e5a5 100644 --- a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp +++ b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp @@ -519,7 +519,7 @@ PipelineProgram* ProgramShaderCache::GetPipelineProgram(const GLVertexFormat* ve geometry_shader ? geometry_shader->GetID() : 0, pixel_shader ? pixel_shader->GetID() : 0}; { - std::lock_guard guard(s_pipeline_program_lock); + std::lock_guard guard{s_pipeline_program_lock}; auto iter = s_pipeline_programs.find(key); if (iter != s_pipeline_programs.end()) { @@ -597,7 +597,7 @@ PipelineProgram* ProgramShaderCache::GetPipelineProgram(const GLVertexFormat* ve } // Lock to insert. A duplicate program may have been created in the meantime. - std::lock_guard guard(s_pipeline_program_lock); + std::lock_guard guard{s_pipeline_program_lock}; auto iter = s_pipeline_programs.find(key); if (iter != s_pipeline_programs.end()) { @@ -627,8 +627,8 @@ void ProgramShaderCache::ReleasePipelineProgram(PipelineProgram* prog) prog->shader.Destroy(); - std::lock_guard guard(s_pipeline_program_lock); - auto iter = s_pipeline_programs.find(prog->key); + std::lock_guard guard{s_pipeline_program_lock}; + const auto iter = s_pipeline_programs.find(prog->key); ASSERT(iter != s_pipeline_programs.end() && prog == iter->second.get()); s_pipeline_programs.erase(iter); } From 67bd2de73eeef89628eb0ea26748e7a77591f83d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 26 Jul 2019 18:43:41 -0400 Subject: [PATCH 4/4] OGL/ProgramShaderCache: Convert typedef over to a using alias Same thing, but nicer to read from left to right. --- Source/Core/VideoBackends/OGL/ProgramShaderCache.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Core/VideoBackends/OGL/ProgramShaderCache.h b/Source/Core/VideoBackends/OGL/ProgramShaderCache.h index 72ec79b5ac..3b09d99e53 100644 --- a/Source/Core/VideoBackends/OGL/ProgramShaderCache.h +++ b/Source/Core/VideoBackends/OGL/ProgramShaderCache.h @@ -105,9 +105,9 @@ public: static void ReleasePipelineProgram(PipelineProgram* prog); private: - typedef std::unordered_map, - PipelineProgramKeyHash> - PipelineProgramMap; + using PipelineProgramMap = + std::unordered_map, + PipelineProgramKeyHash>; static void CreateAttributelessVAO();