|
From: Tapio V. <aa...@us...> - 2010-11-02 20:50:47
|
Module: performous
Branch: opengl2
Commit: 0d381e018623546f8a909f49bceeeb7f95642573
Author: Tapio Vierros <tap...@gm...>
Date: Tue Nov 2 22:44:28 2010 +0200
Cache shader uniform locations.
---
game/3dobject.cc | 2 --
game/3dobject.hh | 2 +-
game/glshader.cc | 6 ++++++
game/glshader.hh | 4 +++-
game/surface.hh | 26 +++++++++-----------------
themes/default/shaders/3dobject.frag | 4 ++--
themes/default/shaders/core.frag | 4 ++--
7 files changed, 23 insertions(+), 25 deletions(-)
diff --git a/game/3dobject.cc b/game/3dobject.cc
index bde08e3..7602460 100644
--- a/game/3dobject.cc
+++ b/game/3dobject.cc
@@ -95,8 +95,6 @@ void Object3d::drawVBO() {
offset += 3*sizeof(GLfloat);
if (m_vboStructure & _3DOBJECT_TEXCOORDS) {
- if (m_texture) UseTexture tex(*m_texture);
-
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, stride, (const GLvoid *)offset);
offset += 2*sizeof(GLfloat);
diff --git a/game/3dobject.hh b/game/3dobject.hh
index cef6e40..b650738 100644
--- a/game/3dobject.hh
+++ b/game/3dobject.hh
@@ -74,7 +74,7 @@ class Object3d: boost::noncopyable {
glTranslatef(x, y, z); // Move to position
if (s != 1.0) glScalef(s,s,s); // Scale if needed
if (m_texture) {
- UseTexture tex(*m_texture, shader->program);
+ UseTexture tex(*m_texture, *shader);
drawVBO();
} else {
drawVBO();
diff --git a/game/glshader.cc b/game/glshader.cc
index bb0f967..b2c6044 100644
--- a/game/glshader.cc
+++ b/game/glshader.cc
@@ -107,6 +107,12 @@ void Shader::loadFromMemory(const char* vert_source, const char* frag_source, bo
}
glutil::GLErrorChecker linkerror("Shader::loadFromMemory - glLinkProgram");
+ // Cache uniform locations
+ tex = glGetUniformLocation(program, "tex");
+ texRect = glGetUniformLocation(program, "texRect");
+ texMode = glGetUniformLocation(program, "texMode");
+ glutil::GLErrorChecker uniformerror("Shader::loadFromMemory - glGetUniformLocation");
+
if (use) bind();
}
diff --git a/game/glshader.hh b/game/glshader.hh
index ed394eb..d834936 100644
--- a/game/glshader.hh
+++ b/game/glshader.hh
@@ -4,6 +4,7 @@
#include <GL/glew.h>
#include <boost/noncopyable.hpp>
+
struct Shader: public boost::noncopyable {
Shader();
Shader(const std::string& vert_path, const std::string& frag_path, bool use = false);
@@ -17,7 +18,8 @@ struct Shader: public boost::noncopyable {
/** Binds the shader into use. */
void bind();
- GLuint program, vert_shader, frag_shader;
+ GLuint program, vert_shader, frag_shader; ///< shader object ids
+ GLint tex, texRect, texMode; ///< uniform locations
int gl_response;
};
diff --git a/game/surface.hh b/game/surface.hh
index ee008a1..059e029 100644
--- a/game/surface.hh
+++ b/game/surface.hh
@@ -120,36 +120,28 @@ template <GLenum Type> class OpenGLTexture: boost::noncopyable {
class UseTexture: boost::noncopyable {
public:
/// constructor
- template <GLenum Type> UseTexture(OpenGLTexture<Type> const& s, GLuint prog = 0):
- m_type(s.type()), m_program(prog) {
- if (m_program == 0) m_program = Window::shader->program; // Default shader
-
- GLint texmodeloc = glGetUniformLocation(m_program, "texMode");
- GLint texloc = glGetUniformLocation(m_program, "tex");
- GLint texrectloc = glGetUniformLocation(m_program, "texrect");
-
- glUniform1i(texloc, 0);
- glUniform1i(texrectloc, 1);
-
+ template <GLenum Type> UseTexture(OpenGLTexture<Type> const& s, Shader& shdr = *Window::shader):
+ m_type(s.type()), m_shader(shdr) {
glEnable(m_type);
+ glUniform1i(m_shader.tex, 0);
+ glUniform1i(m_shader.texRect, 1);
switch (Type) {
- case GL_TEXTURE_2D: glActiveTexture(GL_TEXTURE0); glUniform1i(texmodeloc, 1); break;
- case GL_TEXTURE_RECTANGLE_ARB: glActiveTexture(GL_TEXTURE0+1); glUniform1i(texmodeloc, 2); break;
- default: glUniform1i(texmodeloc, 3); break;
+ case GL_TEXTURE_2D: glActiveTexture(GL_TEXTURE0); glUniform1i(m_shader.texMode, 1); break;
+ case GL_TEXTURE_RECTANGLE_ARB: glActiveTexture(GL_TEXTURE0+1); glUniform1i(m_shader.texMode, 2); break;
+ default: glUniform1i(m_shader.texMode, 3); break;
}
glBindTexture(m_type, s.id());
}
~UseTexture() {
- GLint texmodeloc = glGetUniformLocation(m_program, "texMode");
glDisable(m_type);
- glUniform1i(texmodeloc, 0);
+ glUniform1i(m_shader.texMode, 0);
}
private:
GLenum m_type;
- GLuint m_program;
+ Shader& m_shader;
};
template <GLenum Type> void OpenGLTexture<Type>::draw(Dimensions const& dim, TexCoords const& tex) const {
diff --git a/themes/default/shaders/3dobject.frag b/themes/default/shaders/3dobject.frag
index b351e4a..3d9b59b 100644
--- a/themes/default/shaders/3dobject.frag
+++ b/themes/default/shaders/3dobject.frag
@@ -1,6 +1,6 @@
uniform int texMode;
uniform sampler2D tex;
-uniform sampler2DRect texrect;
+uniform sampler2DRect texRect;
void main()
{
@@ -9,7 +9,7 @@ void main()
if (texMode == 1) {
texel = texture2D(tex, gl_TexCoord[0].st).rgba;
} else if (texMode == 2) {
- texel = texture2DRect(texrect, gl_TexCoord[0].st).rgba;
+ texel = texture2DRect(texRect, gl_TexCoord[0].st).rgba;
} else if (texMode == 0) {
texel = gl_Color;
} else {
diff --git a/themes/default/shaders/core.frag b/themes/default/shaders/core.frag
index b351e4a..3d9b59b 100644
--- a/themes/default/shaders/core.frag
+++ b/themes/default/shaders/core.frag
@@ -1,6 +1,6 @@
uniform int texMode;
uniform sampler2D tex;
-uniform sampler2DRect texrect;
+uniform sampler2DRect texRect;
void main()
{
@@ -9,7 +9,7 @@ void main()
if (texMode == 1) {
texel = texture2D(tex, gl_TexCoord[0].st).rgba;
} else if (texMode == 2) {
- texel = texture2DRect(texrect, gl_TexCoord[0].st).rgba;
+ texel = texture2DRect(texRect, gl_TexCoord[0].st).rgba;
} else if (texMode == 0) {
texel = gl_Color;
} else {
|