From 39100b613272523c2e36be213cc827857a08f824 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Fri, 4 Mar 2016 00:14:22 +0100 Subject: OPENGL: Do not hardcode any uniform/attribute handling in Shader. --- backends/graphics/opengl/opengl-func.h | 2 ++ backends/graphics/opengl/pipelines/shader.cpp | 16 +++++++++++----- backends/graphics/opengl/pipelines/shader.h | 4 ++++ backends/graphics/opengl/shader.cpp | 26 ++++++++++---------------- backends/graphics/opengl/shader.h | 14 ++++++++------ 5 files changed, 35 insertions(+), 27 deletions(-) (limited to 'backends') diff --git a/backends/graphics/opengl/opengl-func.h b/backends/graphics/opengl/opengl-func.h index ad7c572ca9..4e44c13d0f 100644 --- a/backends/graphics/opengl/opengl-func.h +++ b/backends/graphics/opengl/opengl-func.h @@ -107,6 +107,7 @@ GL_FUNC_DEF(GLenum, glGetError, ()); #if !USE_FORCED_GLES GL_FUNC_2_DEF(void, glEnableVertexAttribArray, glEnableVertexAttribArrayARB, (GLuint index)); +GL_FUNC_2_DEF(void, glDisableVertexAttribArray, glDisableVertexAttribArrayARB, (GLuint index)); GL_FUNC_2_DEF(void, glUniform1i, glUniform1iARB, (GLint location, GLint v0)); GL_FUNC_2_DEF(void, glUniform1f, glUniform1fARB, (GLint location, GLfloat v0)); GL_FUNC_2_DEF(void, glUniformMatrix4fv, glUniformMatrix4fvARB, (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)); @@ -122,6 +123,7 @@ GL_FUNC_2_DEF(void, glUseProgram, glUseProgramObjectARB, (GLprogram program)); GL_FUNC_2_DEF(void, glGetProgramiv, glGetObjectParameterivARB, (GLprogram program, GLenum pname, GLint *params)); GL_FUNC_2_DEF(void, glGetProgramInfoLog, glGetInfoLogARB, (GLprogram program, GLsizei bufSize, GLsizei *length, GLchar *infoLog)); GL_FUNC_2_DEF(void, glBindAttribLocation, glBindAttribLocationARB, (GLprogram program, GLuint index, const GLchar *name)); +GL_FUNC_2_DEF(GLint, glGetAttribLocation, glGetAttribLocationARB, (GLprogram program, const GLchar *name)); GL_FUNC_2_DEF(GLint, glGetUniformLocation, glGetUniformLocationARB, (GLprogram program, const GLchar *name)); GL_FUNC_2_DEF(GLshader, glCreateShader, glCreateShaderObjectARB, (GLenum type)); diff --git a/backends/graphics/opengl/pipelines/shader.cpp b/backends/graphics/opengl/pipelines/shader.cpp index 46e81423c5..c7befe22b8 100644 --- a/backends/graphics/opengl/pipelines/shader.cpp +++ b/backends/graphics/opengl/pipelines/shader.cpp @@ -29,11 +29,14 @@ namespace OpenGL { #if !USE_FORCED_GLES ShaderPipeline::ShaderPipeline(Shader *shader) : _activeShader(shader) { + _vertexAttribLocation = shader->getAttributeLocation("position"); + _texCoordAttribLocation = shader->getAttributeLocation("texCoordIn"); + _colorAttribLocation = shader->getAttributeLocation("blendColorIn"); } void ShaderPipeline::activateInternal() { - GL_CALL(glEnableVertexAttribArray(kPositionAttribLocation)); - GL_CALL(glEnableVertexAttribArray(kTexCoordAttribLocation)); + GL_CALL(glEnableVertexAttribArray(_vertexAttribLocation)); + GL_CALL(glEnableVertexAttribArray(_texCoordAttribLocation)); if (g_context.multitextureSupported) { GL_CALL(glActiveTexture(GL_TEXTURE0)); @@ -43,18 +46,21 @@ void ShaderPipeline::activateInternal() { } void ShaderPipeline::deactivateInternal() { + GL_CALL(glDisableVertexAttribArray(_vertexAttribLocation)); + GL_CALL(glDisableVertexAttribArray(_texCoordAttribLocation)); + _activeShader->deactivate(); } void ShaderPipeline::setColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) { - GL_CALL(glVertexAttrib4f(kColorAttribLocation, r, g, b, a)); + GL_CALL(glVertexAttrib4f(_colorAttribLocation, r, g, b, a)); } void ShaderPipeline::drawTexture(const GLTexture &texture, const GLfloat *coordinates) { texture.bind(); - GL_CALL(glVertexAttribPointer(kTexCoordAttribLocation, 2, GL_FLOAT, GL_FALSE, 0, texture.getTexCoords())); - GL_CALL(glVertexAttribPointer(kPositionAttribLocation, 2, GL_FLOAT, GL_FALSE, 0, coordinates)); + GL_CALL(glVertexAttribPointer(_texCoordAttribLocation, 2, GL_FLOAT, GL_FALSE, 0, texture.getTexCoords())); + GL_CALL(glVertexAttribPointer(_vertexAttribLocation, 2, GL_FLOAT, GL_FALSE, 0, coordinates)); GL_CALL(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); } diff --git a/backends/graphics/opengl/pipelines/shader.h b/backends/graphics/opengl/pipelines/shader.h index 8e82bd7a31..95167ee03f 100644 --- a/backends/graphics/opengl/pipelines/shader.h +++ b/backends/graphics/opengl/pipelines/shader.h @@ -44,6 +44,10 @@ protected: virtual void activateInternal(); virtual void deactivateInternal(); + GLint _vertexAttribLocation; + GLint _texCoordAttribLocation; + GLint _colorAttribLocation; + Shader *const _activeShader; }; #endif // !USE_FORCED_GLES diff --git a/backends/graphics/opengl/shader.cpp b/backends/graphics/opengl/shader.cpp index 59749ba1f0..652da57dfa 100644 --- a/backends/graphics/opengl/shader.cpp +++ b/backends/graphics/opengl/shader.cpp @@ -159,10 +159,6 @@ bool Shader::recreate() { GL_CALL(glAttachShader(_program, vertexShader)); GL_CALL(glAttachShader(_program, fragmentShader)); - GL_CALL(glBindAttribLocation(_program, kPositionAttribLocation, "position")); - GL_CALL(glBindAttribLocation(_program, kTexCoordAttribLocation, "texCoordIn")); - GL_CALL(glBindAttribLocation(_program, kColorAttribLocation, "blendColorIn")); - GL_CALL(glLinkProgram(_program)); GL_CALL(glDetachShader(_program, fragmentShader)); @@ -199,18 +195,6 @@ bool Shader::recreate() { } } - if (getUniformLocation("projection") == -1) { - warning("Shader misses \"projection\" uniform."); - destroy(); - return false; - } - - if (!setUniform1I("texture", 0)) { - warning("Shader misses \"texture\" uniform."); - destroy(); - return false; - } - return true; } @@ -230,6 +214,12 @@ void Shader::deactivate() { _isActive = false; } +GLint Shader::getAttributeLocation(const char *name) const { + GLint result = -1; + GL_ASSIGN(result, glGetAttribLocation(_program, name)); + return result; +} + GLint Shader::getUniformLocation(const char *name) const { GLint result = -1; GL_ASSIGN(result, glGetUniformLocation(_program, name)); @@ -310,6 +300,10 @@ void ShaderManager::notifyCreate() { _builtIn[kDefault] = new Shader(g_defaultVertexShader, g_defaultFragmentShader); _builtIn[kCLUT8LookUp] = new Shader(g_defaultVertexShader, g_lookUpFragmentShader); _builtIn[kCLUT8LookUp]->setUniform1I("palette", 1); + + for (uint i = 0; i < kMaxUsages; ++i) { + _builtIn[i]->setUniform1I("texture", 0); + } } else { for (int i = 0; i < ARRAYSIZE(_builtIn); ++i) { _builtIn[i]->recreate(); diff --git a/backends/graphics/opengl/shader.h b/backends/graphics/opengl/shader.h index 1aee8ed681..f219861244 100644 --- a/backends/graphics/opengl/shader.h +++ b/backends/graphics/opengl/shader.h @@ -33,12 +33,6 @@ namespace OpenGL { -enum { - kPositionAttribLocation = 0, - kTexCoordAttribLocation = 1, - kColorAttribLocation = 2 -}; - /** * A generic uniform value interface for a shader program. */ @@ -126,6 +120,14 @@ public: */ void deactivate(); + /** + * Return location for attribute with given name. + * + * @param name Name of the attribute to look up in the shader. + * @return The loctaion of -1 if attribute was not found. + */ + GLint getAttributeLocation(const char *name) const; + /** * Return location for uniform with given name. * -- cgit v1.2.3