OPENGL: Do not hardcode any uniform/attribute handling in Shader.

This commit is contained in:
Johannes Schickel 2016-03-04 00:14:22 +01:00
parent baca885cfc
commit 39100b6132
5 changed files with 35 additions and 27 deletions

View File

@ -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));

View File

@ -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));
}

View File

@ -44,6 +44,10 @@ protected:
virtual void activateInternal();
virtual void deactivateInternal();
GLint _vertexAttribLocation;
GLint _texCoordAttribLocation;
GLint _colorAttribLocation;
Shader *const _activeShader;
};
#endif // !USE_FORCED_GLES

View File

@ -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();

View File

@ -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.
*