From f2e7b6f0a07a01379aa669524e1ad1f09f4c5946 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Fri, 30 Apr 2021 18:35:27 +0800 Subject: [PATCH] Make shader objects load source from string rather than c-string --- src/gl/shader-object.cpp | 7 ++++--- src/gl/shader-object.hpp | 5 ++--- src/gl/shader-template.cpp | 6 +++--- src/gl/shader-template.hpp | 10 +++++----- src/resources/shader-program-loader.cpp | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/gl/shader-object.cpp b/src/gl/shader-object.cpp index 584ae09..378a3f0 100644 --- a/src/gl/shader-object.cpp +++ b/src/gl/shader-object.cpp @@ -54,11 +54,12 @@ shader_object::~shader_object() glDeleteShader(gl_shader_id); } -void shader_object::source(const char* buffer, std::size_t size) +void shader_object::source(const std::string& source_code) { // Replace OpenGL shader object source code - GLint gl_length = size; - glShaderSource(gl_shader_id, 1, &buffer, &gl_length); + GLint gl_length = static_cast(source_code.length()); + const GLchar* gl_string = source_code.c_str(); + glShaderSource(gl_shader_id, 1, &gl_string, &gl_length); // Handle OpenGL errors switch (glGetError()) diff --git a/src/gl/shader-object.hpp b/src/gl/shader-object.hpp index 7d8f70a..19c8333 100644 --- a/src/gl/shader-object.hpp +++ b/src/gl/shader-object.hpp @@ -54,13 +54,12 @@ public: /** * Replaces the source code of the shader object. * - * @param buffer Buffer containing shader object source code. - * @param size Size of the source code, in bytes. + * @param source_code String containing shader object source code. * * @exception std::runtime_error Shader object handle is not a value generated by OpenGL. * @exception std::runtime_error Shader object handle is not a shader object. */ - void source(const char* buffer, std::size_t size); + void source(const std::string& source_code); /** * Compiles the shader object. diff --git a/src/gl/shader-template.cpp b/src/gl/shader-template.cpp index b503a8a..bb0d983 100644 --- a/src/gl/shader-template.cpp +++ b/src/gl/shader-template.cpp @@ -93,7 +93,7 @@ gl::shader_object* shader_template::compile(gl::shader_stage stage, const dictio gl::shader_object* object = new gl::shader_object(stage); // Set shader object source - object->source(object_source.c_str(), object_source.length()); + object->source(object_source); // Compile shader object object->compile(); @@ -214,9 +214,9 @@ bool shader_template::has_geometry_directive() const return !geometry_directives.empty(); } -bool shader_template::has_define_directive(const std::string& name) const +bool shader_template::has_define_directive(const std::string& key) const { - return (define_directives.find(name) != define_directives.end()); + return (define_directives.find(key) != define_directives.end()); } } // namespace gl diff --git a/src/gl/shader-template.hpp b/src/gl/shader-template.hpp index 8df19ee..0529373 100644 --- a/src/gl/shader-template.hpp +++ b/src/gl/shader-template.hpp @@ -37,7 +37,7 @@ namespace gl { * * `#pragma vertex`: Replaced with `#define __VERTEX__` when generating vertex shader objects. * * `#pragma fragment`: Replaced with `#define __FRAGMENT__` when generating fragment shader objects. * * `#pragma geometry`: Replaced with `#define __GEOMETRY__` when generating geometry shader objects. - * * `#pragma define `: Will be replaced with `#define ` if its definition is passed to the shader template. + * * `#pragma define `: Will be replaced with `#define ` if its definition is passed to the shader template. * * @see gl::shader_stage * @see gl::shader_object @@ -66,7 +66,7 @@ public: /** * Replaces the source code of the shader template. * - * @param source_code String containing the shader template source code. + * @param source_code String containing shader template source code. */ void source(const std::string& source_code); @@ -114,11 +114,11 @@ public: bool has_geometry_directive() const; /** - * Returns `true` if the template source contains one or more instance of `#pragma define `. + * Returns `true` if the template source contains one or more instance of `#pragma define `. * - * @param name Definition name. + * @param key Definition key. */ - bool has_define_directive(const std::string& name) const; + bool has_define_directive(const std::string& key) const; private: void replace_stage_directives(gl::shader_stage stage) const; diff --git a/src/resources/shader-program-loader.cpp b/src/resources/shader-program-loader.cpp index 88a1f22..a5ada3a 100644 --- a/src/resources/shader-program-loader.cpp +++ b/src/resources/shader-program-loader.cpp @@ -67,7 +67,7 @@ static void handle_includes(text_file* source, resource_manager* resource_manage } else { - (*source)[i] = "#error malformed include directive"; + (*source)[i] = "#error malformed include directive (" + (*source)[i] + ")"; } } }