Browse Source

Make shader objects load source from string rather than c-string

master
C. J. Howard 3 years ago
parent
commit
f2e7b6f0a0
5 changed files with 15 additions and 15 deletions
  1. +4
    -3
      src/gl/shader-object.cpp
  2. +2
    -3
      src/gl/shader-object.hpp
  3. +3
    -3
      src/gl/shader-template.cpp
  4. +5
    -5
      src/gl/shader-template.hpp
  5. +1
    -1
      src/resources/shader-program-loader.cpp

+ 4
- 3
src/gl/shader-object.cpp View File

@ -54,11 +54,12 @@ shader_object::~shader_object()
glDeleteShader(gl_shader_id); 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 // Replace OpenGL shader object source code
GLint gl_length = size;
glShaderSource(gl_shader_id, 1, &buffer, &gl_length);
GLint gl_length = static_cast<GLint>(source_code.length());
const GLchar* gl_string = source_code.c_str();
glShaderSource(gl_shader_id, 1, &gl_string, &gl_length);
// Handle OpenGL errors // Handle OpenGL errors
switch (glGetError()) switch (glGetError())

+ 2
- 3
src/gl/shader-object.hpp View File

@ -54,13 +54,12 @@ public:
/** /**
* Replaces the source code of the shader object. * 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 value generated by OpenGL.
* @exception std::runtime_error Shader object handle is not a shader object. * @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. * Compiles the shader object.

+ 3
- 3
src/gl/shader-template.cpp View File

@ -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); gl::shader_object* object = new gl::shader_object(stage);
// Set shader object source // Set shader object source
object->source(object_source.c_str(), object_source.length());
object->source(object_source);
// Compile shader object // Compile shader object
object->compile(); object->compile();
@ -214,9 +214,9 @@ bool shader_template::has_geometry_directive() const
return !geometry_directives.empty(); 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 } // namespace gl

+ 5
- 5
src/gl/shader-template.hpp View File

@ -37,7 +37,7 @@ namespace gl {
* * `#pragma vertex`: Replaced with `#define __VERTEX__` when generating vertex shader objects. * * `#pragma vertex`: Replaced with `#define __VERTEX__` when generating vertex shader objects.
* * `#pragma fragment`: Replaced with `#define __FRAGMENT__` when generating fragment 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 geometry`: Replaced with `#define __GEOMETRY__` when generating geometry shader objects.
* * `#pragma define <name> <value>`: Will be replaced with `#define <name> <value>` if its definition is passed to the shader template.
* * `#pragma define <key> <value>`: Will be replaced with `#define <key> <value>` if its definition is passed to the shader template.
* *
* @see gl::shader_stage * @see gl::shader_stage
* @see gl::shader_object * @see gl::shader_object
@ -66,7 +66,7 @@ public:
/** /**
* Replaces the source code of the shader template. * 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); void source(const std::string& source_code);
@ -114,11 +114,11 @@ public:
bool has_geometry_directive() const; bool has_geometry_directive() const;
/** /**
* Returns `true` if the template source contains one or more instance of `#pragma define <name>`.
* Returns `true` if the template source contains one or more instance of `#pragma define <key>`.
* *
* @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: private:
void replace_stage_directives(gl::shader_stage stage) const; void replace_stage_directives(gl::shader_stage stage) const;

+ 1
- 1
src/resources/shader-program-loader.cpp View File

@ -67,7 +67,7 @@ static void handle_includes(text_file* source, resource_manager* resource_manage
} }
else else
{ {
(*source)[i] = "#error malformed include directive";
(*source)[i] = "#error malformed include directive (" + (*source)[i] + ")";
} }
} }
} }

Loading…
Cancel
Save