Merge pull request #8265 from lioncash/view

OGL/ProgramShaderCache: Use std::string_view where applicable
This commit is contained in:
Connor McLaughlin 2019-07-28 14:21:59 +10:00 committed by GitHub
commit 5bad233b1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 42 deletions

View File

@ -4,8 +4,8 @@
#include "VideoBackends/OGL/ProgramShaderCache.h" #include "VideoBackends/OGL/ProgramShaderCache.h"
#include <array>
#include <atomic> #include <atomic>
#include <limits>
#include <memory> #include <memory>
#include <string> #include <string>
@ -17,10 +17,8 @@
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "Common/Timer.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
#include "Core/Host.h"
#include "VideoBackends/OGL/OGLShader.h" #include "VideoBackends/OGL/OGLShader.h"
#include "VideoBackends/OGL/Render.h" #include "VideoBackends/OGL/Render.h"
@ -28,14 +26,11 @@
#include "VideoBackends/OGL/VertexManager.h" #include "VideoBackends/OGL/VertexManager.h"
#include "VideoCommon/AsyncShaderCompiler.h" #include "VideoCommon/AsyncShaderCompiler.h"
#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/GeometryShaderManager.h" #include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/ImageWrite.h"
#include "VideoCommon/PixelShaderManager.h" #include "VideoCommon/PixelShaderManager.h"
#include "VideoCommon/Statistics.h" #include "VideoCommon/Statistics.h"
#include "VideoCommon/VertexLoaderManager.h" #include "VideoCommon/VertexLoaderManager.h"
#include "VideoCommon/VertexShaderManager.h" #include "VideoCommon/VertexShaderManager.h"
#include "VideoCommon/VideoCommon.h"
namespace OGL namespace OGL
{ {
@ -267,19 +262,19 @@ void ProgramShaderCache::UploadConstants(const void* data, u32 data_size)
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, 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, // We need to enable GL_ARB_compute_shader for drivers that support the extension,
// but not GLSL 4.3. Mesa is one example. // but not GLSL 4.3. Mesa is one example.
std::string header; std::string full_code;
if (g_ActiveConfig.backend_info.bSupportsComputeShaders && if (g_ActiveConfig.backend_info.bSupportsComputeShaders &&
g_ogl_config.eSupportedGLSLVersion < Glsl430) 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; full_code += code;
GLuint shader_id = CompileSingleShader(GL_COMPUTE_SHADER, full_code); const GLuint shader_id = CompileSingleShader(GL_COMPUTE_SHADER, full_code);
if (!shader_id) if (!shader_id)
return false; return false;
@ -291,7 +286,7 @@ bool ProgramShaderCache::CompileComputeShader(SHADER& shader, const std::string&
// original shaders aren't needed any more // original shaders aren't needed any more
glDeleteShader(shader_id); glDeleteShader(shader_id);
if (!CheckProgramLinkResult(shader.glprogid, &full_code, nullptr, nullptr)) if (!CheckProgramLinkResult(shader.glprogid, full_code, {}, {}))
{ {
shader.Destroy(); shader.Destroy();
return false; return false;
@ -301,13 +296,21 @@ bool ProgramShaderCache::CompileComputeShader(SHADER& shader, const std::string&
return true; 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<const char*, num_strings> src{
s_glsl_header.data(),
code.data(),
};
const std::array<GLint, num_strings> src_sizes{
static_cast<GLint>(s_glsl_header.size()),
static_cast<GLint>(code.size()),
};
glShaderSource(result, 2, src, nullptr); glShaderSource(result, num_strings, src.data(), src_sizes.data());
glCompileShader(result); glCompileShader(result);
if (!CheckShaderCompileResult(result, type, code)) if (!CheckShaderCompileResult(result, type, code))
@ -320,7 +323,7 @@ GLuint ProgramShaderCache::CompileSingleShader(GLenum type, const std::string& c
return result; 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; GLint compileStatus;
glGetShaderiv(id, GL_COMPILE_STATUS, &compileStatus); glGetShaderiv(id, GL_COMPILE_STATUS, &compileStatus);
@ -374,8 +377,8 @@ bool ProgramShaderCache::CheckShaderCompileResult(GLuint id, GLenum type, const
return true; return true;
} }
bool ProgramShaderCache::CheckProgramLinkResult(GLuint id, const std::string* vcode, bool ProgramShaderCache::CheckProgramLinkResult(GLuint id, std::string_view vcode,
const std::string* pcode, const std::string* gcode) std::string_view pcode, std::string_view gcode)
{ {
GLint linkStatus; GLint linkStatus;
glGetProgramiv(id, GL_LINK_STATUS, &linkStatus); glGetProgramiv(id, GL_LINK_STATUS, &linkStatus);
@ -393,12 +396,12 @@ bool ProgramShaderCache::CheckProgramLinkResult(GLuint id, const std::string* vc
StringFromFormat("%sbad_p_%d.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++); StringFromFormat("%sbad_p_%d.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
std::ofstream file; std::ofstream file;
File::OpenFStream(file, filename, std::ios_base::out); File::OpenFStream(file, filename, std::ios_base::out);
if (vcode) if (!vcode.empty())
file << s_glsl_header << *vcode << '\n'; file << s_glsl_header << vcode << '\n';
if (gcode) if (!gcode.empty())
file << s_glsl_header << *gcode << '\n'; file << s_glsl_header << gcode << '\n';
if (pcode) if (!pcode.empty())
file << s_glsl_header << *pcode << '\n'; file << s_glsl_header << pcode << '\n';
file << info_log; file << info_log;
file.close(); file.close();
@ -516,7 +519,7 @@ PipelineProgram* ProgramShaderCache::GetPipelineProgram(const GLVertexFormat* ve
geometry_shader ? geometry_shader->GetID() : 0, geometry_shader ? geometry_shader->GetID() : 0,
pixel_shader ? pixel_shader->GetID() : 0}; pixel_shader ? pixel_shader->GetID() : 0};
{ {
std::lock_guard<std::mutex> guard(s_pipeline_program_lock); std::lock_guard guard{s_pipeline_program_lock};
auto iter = s_pipeline_programs.find(key); auto iter = s_pipeline_programs.find(key);
if (iter != s_pipeline_programs.end()) if (iter != s_pipeline_programs.end())
{ {
@ -583,10 +586,10 @@ PipelineProgram* ProgramShaderCache::GetPipelineProgram(const GLVertexFormat* ve
if (!s_is_shared_context && vao != s_last_VAO) if (!s_is_shared_context && vao != s_last_VAO)
glBindVertexArray(s_last_VAO); glBindVertexArray(s_last_VAO);
if (!ProgramShaderCache::CheckProgramLinkResult( if (!CheckProgramLinkResult(prog->shader.glprogid,
prog->shader.glprogid, vertex_shader ? &vertex_shader->GetSource() : nullptr, vertex_shader ? vertex_shader->GetSource() : std::string_view{},
geometry_shader ? &geometry_shader->GetSource() : nullptr, geometry_shader ? geometry_shader->GetSource() : std::string_view{},
pixel_shader ? &pixel_shader->GetSource() : nullptr)) pixel_shader ? pixel_shader->GetSource() : std::string_view{}))
{ {
prog->shader.Destroy(); prog->shader.Destroy();
return nullptr; return nullptr;
@ -594,7 +597,7 @@ PipelineProgram* ProgramShaderCache::GetPipelineProgram(const GLVertexFormat* ve
} }
// Lock to insert. A duplicate program may have been created in the meantime. // Lock to insert. A duplicate program may have been created in the meantime.
std::lock_guard<std::mutex> guard(s_pipeline_program_lock); std::lock_guard guard{s_pipeline_program_lock};
auto iter = s_pipeline_programs.find(key); auto iter = s_pipeline_programs.find(key);
if (iter != s_pipeline_programs.end()) if (iter != s_pipeline_programs.end())
{ {
@ -624,8 +627,8 @@ void ProgramShaderCache::ReleasePipelineProgram(PipelineProgram* prog)
prog->shader.Destroy(); prog->shader.Destroy();
std::lock_guard<std::mutex> guard(s_pipeline_program_lock); std::lock_guard guard{s_pipeline_program_lock};
auto iter = s_pipeline_programs.find(prog->key); const auto iter = s_pipeline_programs.find(prog->key);
ASSERT(iter != s_pipeline_programs.end() && prog == iter->second.get()); ASSERT(iter != s_pipeline_programs.end() && prog == iter->second.get());
s_pipeline_programs.erase(iter); s_pipeline_programs.erase(iter);
} }

View File

@ -7,7 +7,7 @@
#include <atomic> #include <atomic>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <tuple> #include <string_view>
#include <unordered_map> #include <unordered_map>
#include "Common/GL/GLUtil.h" #include "Common/GL/GLUtil.h"
@ -75,11 +75,11 @@ public:
static void InvalidateVertexFormatIfBound(GLuint vao); static void InvalidateVertexFormatIfBound(GLuint vao);
static void InvalidateLastProgram(); static void InvalidateLastProgram();
static bool CompileComputeShader(SHADER& shader, const std::string& code); static bool CompileComputeShader(SHADER& shader, std::string_view code);
static GLuint CompileSingleShader(GLenum type, const std::string& code); static GLuint CompileSingleShader(GLenum type, std::string_view code);
static bool CheckShaderCompileResult(GLuint id, GLenum type, const std::string& code); static bool CheckShaderCompileResult(GLuint id, GLenum type, std::string_view code);
static bool CheckProgramLinkResult(GLuint id, const std::string* vcode, const std::string* pcode, static bool CheckProgramLinkResult(GLuint id, std::string_view vcode, std::string_view pcode,
const std::string* gcode); std::string_view gcode);
static StreamBuffer* GetUniformBuffer(); static StreamBuffer* GetUniformBuffer();
static u32 GetUniformBufferAlignment(); static u32 GetUniformBufferAlignment();
static void UploadConstants(); static void UploadConstants();
@ -105,9 +105,9 @@ public:
static void ReleasePipelineProgram(PipelineProgram* prog); static void ReleasePipelineProgram(PipelineProgram* prog);
private: private:
typedef std::unordered_map<PipelineProgramKey, std::unique_ptr<PipelineProgram>, using PipelineProgramMap =
PipelineProgramKeyHash> std::unordered_map<PipelineProgramKey, std::unique_ptr<PipelineProgram>,
PipelineProgramMap; PipelineProgramKeyHash>;
static void CreateAttributelessVAO(); static void CreateAttributelessVAO();