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

Introduced bind-if-necessary/unbind semantics for shaders.

This commit is contained in:
Thomas Harte 2016-05-01 11:07:49 -04:00
parent fe2abbd4ed
commit 2616d748fe
2 changed files with 25 additions and 2 deletions

View File

@ -13,6 +13,10 @@
using namespace OpenGL;
namespace {
Shader *bound_shader = nullptr;
}
GLuint Shader::compile_shader(const char *source, GLenum type)
{
GLuint shader = glCreateShader(type);
@ -80,12 +84,24 @@ Shader::Shader(const char *vertex_shader, const char *fragment_shader, const Att
Shader::~Shader()
{
if(bound_shader == this) Shader::unbind();
glDeleteProgram(_shader_program);
}
void Shader::bind()
{
glUseProgram(_shader_program);
if(bound_shader != this)
{
glUseProgram(_shader_program);
bound_shader = this;
}
else printf("-");
}
void Shader::unbind()
{
bound_shader = nullptr;
glUseProgram(0);
}
GLint Shader::get_attrib_location(const GLchar *name)

View File

@ -41,10 +41,17 @@ public:
~Shader();
/*!
Performs an @c glUseProgram to make this the active shader.
Performs an @c glUseProgram to make this the active shader unless:
(i) it was the previous shader bound; and
(ii) no calls have been received to unbind in the interim.
*/
void bind();
/*!
Unbinds the current instance of Shader, if one is bound.
*/
static void unbind();
/*!
Performs a @c glGetAttribLocation call.
@param name The name of the attribute to locate.