From 60281eb67ebccd8eb26f769719f0c0ea2fe840b2 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 2 Nov 2019 23:42:21 +1000 Subject: [PATCH] Common: Use std::string_view for GL::Program --- src/common/gl_program.cpp | 8 ++++---- src/common/gl_program.h | 5 +++-- src/core/gpu_hw_opengl.cpp | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/common/gl_program.cpp b/src/common/gl_program.cpp index 1bae3c841..00e2bc1fe 100644 --- a/src/common/gl_program.cpp +++ b/src/common/gl_program.cpp @@ -17,12 +17,12 @@ Program::~Program() Destroy(); } -GLuint Program::CompileShader(GLenum type, const char* source) +GLuint Program::CompileShader(GLenum type, const std::string_view source) { GLuint id = glCreateShader(type); - std::array sources = {{source}}; - std::array source_lengths = {{static_cast(std::strlen(source))}}; + std::array sources = {{source.data()}}; + std::array source_lengths = {{static_cast(source.size())}}; glShaderSource(id, static_cast(sources.size()), sources.data(), source_lengths.data()); glCompileShader(id); @@ -69,7 +69,7 @@ void Program::ResetLastProgram() s_last_program_id = 0; } -bool Program::Compile(const char* vertex_shader, const char* fragment_shader) +bool Program::Compile(const std::string_view vertex_shader, const std::string_view fragment_shader) { GLuint vertex_shader_id = CompileShader(GL_VERTEX_SHADER, vertex_shader); if (vertex_shader_id == 0) diff --git a/src/common/gl_program.h b/src/common/gl_program.h index de2bf5362..439585f9b 100644 --- a/src/common/gl_program.h +++ b/src/common/gl_program.h @@ -1,6 +1,7 @@ #pragma once #include "glad.h" #include "types.h" +#include #include namespace GL { @@ -10,12 +11,12 @@ public: Program(); ~Program(); - static GLuint CompileShader(GLenum type, const char* source); + static GLuint CompileShader(GLenum type, const std::string_view source); static void ResetLastProgram(); bool IsVaild() const { return m_program_id != 0; } - bool Compile(const char* vertex_shader, const char* fragment_shader); + bool Compile(const std::string_view vertex_shader, const std::string_view fragment_shader); void BindAttribute(GLuint index, const char* name); void BindDefaultAttributes(); diff --git a/src/core/gpu_hw_opengl.cpp b/src/core/gpu_hw_opengl.cpp index c0d06f5b7..fac84ff4e 100644 --- a/src/core/gpu_hw_opengl.cpp +++ b/src/core/gpu_hw_opengl.cpp @@ -286,7 +286,7 @@ bool GPU_HW_OpenGL::CompilePrograms() const std::string vs = GenerateScreenQuadVertexShader(); const std::string fs = GenerateDisplayFragmentShader(ConvertToBoolUnchecked(depth_24bit), ConvertToBoolUnchecked(interlaced)); - if (!prog.Compile(vs.c_str(), fs.c_str())) + if (!prog.Compile(vs, fs)) return false; prog.BindFragData(0, "o_col0"); @@ -309,7 +309,7 @@ bool GPU_HW_OpenGL::CompileProgram(GL::Program& prog, HWBatchRenderMode render_m const bool textured = texture_mode != TextureMode::Disabled; const std::string vs = GenerateVertexShader(textured); const std::string fs = GenerateFragmentShader(render_mode, texture_mode, dithering); - if (!prog.Compile(vs.c_str(), fs.c_str())) + if (!prog.Compile(vs, fs)) return false; prog.BindAttribute(0, "a_pos");