1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-04 18:29:40 +00:00

Introduced an interface for specifying attribute bindings, taking the opportunity to document the interface and introduce exceptions.

This commit is contained in:
Thomas Harte 2016-04-19 07:23:15 -04:00
parent 9d39c14752
commit bcc784bda9
3 changed files with 44 additions and 9 deletions

View File

@ -246,7 +246,6 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
_input_texture_data = (uint8_t *)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, _input_texture_array_size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
// printf("%04x\n", glGetError());
_output_mutex->unlock();
}
@ -535,7 +534,7 @@ void OpenGLOutputBuilder::prepare_composite_input_shader()
char *fragment_shader = get_input_fragment_shader();
if(vertex_shader && fragment_shader)
{
composite_input_shader_program = std::unique_ptr<OpenGL::Shader>(new OpenGL::Shader(vertex_shader, fragment_shader));
composite_input_shader_program = std::unique_ptr<OpenGL::Shader>(new OpenGL::Shader(vertex_shader, fragment_shader, nullptr));
GLint texIDUniform = composite_input_shader_program->get_uniform_location("texID");
GLint phaseCyclesPerTickUniform = composite_input_shader_program->get_uniform_location("phaseCyclesPerTick");
@ -622,7 +621,7 @@ std::unique_ptr<OpenGL::Shader> OpenGLOutputBuilder::prepare_output_shader(char
if(vertex_shader && fragment_shader)
{
shader_program = std::unique_ptr<OpenGL::Shader>(new OpenGL::Shader(vertex_shader, fragment_shader));
shader_program = std::unique_ptr<OpenGL::Shader>(new OpenGL::Shader(vertex_shader, fragment_shader, nullptr));
shader_program->bind();
windowSizeUniform = shader_program->get_uniform_location("windowSize");

View File

@ -38,7 +38,7 @@ GLuint Shader::compile_shader(const char *source, GLenum type)
return shader;
}
Shader::Shader(const char *vertex_shader, const char *fragment_shader)
Shader::Shader(const char *vertex_shader, const char *fragment_shader, const AttributeBinding *attribute_bindings)
{
_shader_program = glCreateProgram();
GLuint vertex = compile_shader(vertex_shader, GL_VERTEX_SHADER);
@ -46,6 +46,16 @@ Shader::Shader(const char *vertex_shader, const char *fragment_shader)
glAttachShader(_shader_program, vertex);
glAttachShader(_shader_program, fragment);
if(attribute_bindings)
{
while(attribute_bindings->name)
{
glBindAttribLocation(_shader_program, attribute_bindings->index, attribute_bindings->name);
attribute_bindings++;
}
}
glLinkProgram(_shader_program);
#if defined(DEBUG)
@ -76,12 +86,12 @@ void Shader::bind()
glUseProgram(_shader_program);
}
GLint Shader::get_attrib_location(const char *name)
GLint Shader::get_attrib_location(const GLchar *name)
{
return glGetAttribLocation(_shader_program, name);
}
GLint Shader::get_uniform_location(const char *name)
GLint Shader::get_uniform_location(const GLchar *name)
{
return glGetUniformLocation(_shader_program, name);
}

View File

@ -14,12 +14,38 @@
namespace OpenGL {
class Shader {
public:
Shader(const char *vertex_shader, const char *fragment_shader);
struct AttributeBinding {
const GLchar *name;
GLuint index;
};
/*!
Constructs a shader, comprised of:
@param vertex_shader The vertex shader source code.
@param fragment_shader The fragment shader source code.
@param attribute_bindings Either @c nullptr or an array terminated by an entry with a @c nullptr-name of attribute bindings.
*/
Shader(const char *vertex_shader, const char *fragment_shader, const AttributeBinding *attribute_bindings);
~Shader();
/*!
Performs an @c glUseProgram to make this the active shader.
*/
void bind();
GLint get_attrib_location(const char *name);
GLint get_uniform_location(const char *name);
/*!
Performs a @c glGetAttribLocation call.
@param name The name of the attribute to locate.
@returns The location of the requested attribute.
*/
GLint get_attrib_location(const GLchar *name);
/*!
Performs a @c glGetUniformLocation call.
@param name The name of the uniform to locate.
@returns The location of the requested uniform.
*/
GLint get_uniform_location(const GLchar *name);
private:
GLuint compile_shader(const char *source, GLenum type);