Browse Source

Manually keep track of bound textures, so we don't have to poll each frame

Patch submitted by slime73.
Bart van Strien 13 years ago
parent
commit
2ac4a9e6d0

+ 11 - 11
src/modules/graphics/opengl/Canvas.cpp

@@ -77,12 +77,12 @@ namespace opengl
 
 			// generate texture save target
 			glGenTextures(1, &img);
-			glBindTexture(GL_TEXTURE_2D, img);
+			bindTexture(img);
 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height,
 					0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-			glBindTexture(GL_TEXTURE_2D, 0);
+			bindTexture(0);
 			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
 					GL_TEXTURE_2D, img, 0);
 
@@ -96,7 +96,7 @@ namespace opengl
 		}
 		virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil,  GLuint img)
 		{
-			glDeleteTextures(1, &img);
+			deleteTexture(img);
 			glDeleteRenderbuffers(1, &depth_stencil);
 			glDeleteFramebuffers(1, &framebuffer);
 		}
@@ -129,12 +129,12 @@ namespace opengl
 
 			// generate texture save target
 			glGenTextures(1, &img);
-			glBindTexture(GL_TEXTURE_2D, img);
+			bindTexture(img);
 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height,
 					0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-			glBindTexture(GL_TEXTURE_2D, 0);
+			bindTexture(0);
 			glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
 					GL_TEXTURE_2D, img, 0);
 
@@ -149,7 +149,7 @@ namespace opengl
 
 		virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img)
 		{
-			glDeleteTextures(1, &img);
+			deleteTexture(img);
 			glDeleteRenderbuffersEXT(1, &depth_stencil);
 			glDeleteFramebuffersEXT(1, &framebuffer);
 		}
@@ -339,7 +339,7 @@ namespace opengl
 		GLint gmin = (f.min == Image::FILTER_NEAREST) ? GL_NEAREST : GL_LINEAR;
 		GLint gmag = (f.mag == Image::FILTER_NEAREST) ? GL_NEAREST : GL_LINEAR;
 
-		glBindTexture(GL_TEXTURE_2D, img);
+		bindTexture(img);
 
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gmin);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag);
@@ -349,7 +349,7 @@ namespace opengl
 	{
 		GLint gmin, gmag;
 
-		glBindTexture(GL_TEXTURE_2D, img);
+		bindTexture(img);
 		glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, &gmin);
 		glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, &gmag);
 
@@ -364,7 +364,7 @@ namespace opengl
 		GLint wrap_s = (w.s == Image::WRAP_CLAMP) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
 		GLint wrap_t = (w.t == Image::WRAP_CLAMP) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
 
-		glBindTexture(GL_TEXTURE_2D, img);
+		bindTexture(img);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
 	}
@@ -372,7 +372,7 @@ namespace opengl
 	Image::Wrap Canvas::getWrap() const
 	{
 		GLint wrap_s, wrap_t;
-		glBindTexture(GL_TEXTURE_2D, img);
+		bindTexture(img);
 		glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, &wrap_s);
 		glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, &wrap_t);
 
@@ -420,7 +420,7 @@ namespace opengl
 
 		glMultMatrixf((const GLfloat*)t.getElements());
 
-		glBindTexture(GL_TEXTURE_2D, img);
+		bindTexture(img);
 
 		glEnableClientState(GL_VERTEX_ARRAY);
 		glEnableClientState(GL_TEXTURE_COORD_ARRAY);

+ 1 - 0
src/modules/graphics/opengl/Canvas.h

@@ -29,6 +29,7 @@
 #include <image/ImageData.h>
 #include <common/math.h>
 #include <common/Matrix.h>
+#include "OpenGL.h"
 #include "GLee.h"
 
 namespace love

+ 7 - 4
src/modules/graphics/opengl/Font.cpp

@@ -61,7 +61,7 @@ namespace opengl
 		GLuint t;
 		glGenTextures(1, &t);
 		textures.push_back(t);
-		glBindTexture(GL_TEXTURE_2D, t);
+		bindTexture(t);
 
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
 						(filter.mag == Image::FILTER_LINEAR) ? GL_LINEAR : GL_NEAREST);
@@ -117,9 +117,11 @@ namespace opengl
 			createTexture();
 		}
 		GLuint t = textures.back();
-		glBindTexture(GL_TEXTURE_2D, t);
+		bindTexture(t);
 		glTexSubImage2D(GL_TEXTURE_2D, 0, texture_x, texture_y, w, h, (type == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA), GL_UNSIGNED_BYTE, gd->getData());
 
+		g->texture = t;
+
 		Quad::Viewport v;
 		v.x = (float) texture_x;
 		v.y = (float) texture_y;
@@ -134,7 +136,6 @@ namespace opengl
 		glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&verts[0].s);
 
 		glNewList(g->list, GL_COMPILE);
-		glBindTexture(GL_TEXTURE_2D, t);
 		glPushMatrix();
 		glTranslatef(static_cast<float>(gd->getBearingX()), static_cast<float>(-gd->getBearingY()), 0.0f);
 		glDrawArrays(GL_QUADS, 0, 4);
@@ -185,6 +186,7 @@ namespace opengl
 				glPushMatrix();
 				// 1.25 is magic line height for true type fonts
 				if (type == FONT_TRUETYPE) glTranslatef(0, floor(getHeight() / 1.25f + 0.5f), 0);
+				bindTexture(glyph->texture);
 				glCallList(glyph->list);
 				glPopMatrix();
 				glTranslatef(static_cast<GLfloat>(glyph->spacing), 0, 0);
@@ -205,6 +207,7 @@ namespace opengl
 		if (!glyph) glyph = addGlyph(character);
 		glPushMatrix();
 		glTranslatef(x, floor(y+getHeight() + 0.5f), 0.0f);
+		bindTexture(glyph->texture);
 		glCallList(glyph->list);
 		glPopMatrix();
 	}
@@ -344,7 +347,7 @@ namespace opengl
 		std::vector<GLuint>::iterator iter = textures.begin();
 		while (iter != textures.end())
 		{
-			glDeleteTextures(1, (GLuint*)&*iter);
+			deleteTexture(*iter);
 			iter++;
 		}
 		textures.clear();

+ 2 - 0
src/modules/graphics/opengl/Font.h

@@ -31,6 +31,7 @@
 #include <font/Rasterizer.h>
 #include <graphics/Image.h>
 
+#include "OpenGL.h"
 #include "GLee.h"
 
 namespace love
@@ -53,6 +54,7 @@ namespace opengl
 		struct Glyph
 		{
 			GLuint list;
+			GLuint texture;
 			int spacing;
 		};
 

+ 2 - 0
src/modules/graphics/opengl/Graphics.cpp

@@ -41,6 +41,8 @@ namespace opengl
 		: currentFont(0), currentImageFilter(), lineStyle(LINE_SMOOTH), lineWidth(1), matrixLimit(0), userMatrices(0)
 	{
 		currentWindow = love::window::sdl::Window::getSingleton();
+
+		resetBoundTexture();
 	}
 
 	Graphics::~Graphics()

+ 1 - 0
src/modules/graphics/opengl/Graphics.h

@@ -37,6 +37,7 @@
 
 #include <window/Window.h>
 
+#include "OpenGL.h"
 #include "Font.h"
 #include "Image.h"
 #include "Quad.h"

+ 4 - 9
src/modules/graphics/opengl/Image.cpp

@@ -265,12 +265,7 @@ namespace opengl
 		if (texture == 0)
 			return;
 
-		GLuint boundTex;
-		glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *)&boundTex);
-
-		// only bind if this texture is not already bound
-		if (boundTex != texture)
-			glBindTexture(GL_TEXTURE_2D, texture);
+		bindTexture(texture);
 	}
 
 	bool Image::load()
@@ -294,7 +289,7 @@ namespace opengl
 	bool Image::loadVolatilePOT()
 	{
 		glGenTextures(1,(GLuint*)&texture);
-		glBindTexture(GL_TEXTURE_2D, texture);
+		bindTexture(texture);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
@@ -340,7 +335,7 @@ namespace opengl
 	bool Image::loadVolatileNPOT()
 	{
 		glGenTextures(1,(GLuint*)&texture);
-		glBindTexture(GL_TEXTURE_2D, texture);
+		bindTexture(texture);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
@@ -370,7 +365,7 @@ namespace opengl
 		// Delete the hardware texture.
 		if (texture != 0)
 		{
-			glDeleteTextures(1, (GLuint*)&texture);
+			deleteTexture(texture);
 			texture = 0;
 		}
 	}

+ 2 - 0
src/modules/graphics/opengl/Image.h

@@ -28,6 +28,8 @@
 #include <image/ImageData.h>
 #include <graphics/Image.h>
 
+#include "OpenGL.h"
+
 // OpenGL
 #include "GLee.h"
 

+ 2 - 2
src/modules/graphics/opengl/PixelEffect.cpp

@@ -253,7 +253,7 @@ namespace opengl
 		GLint location = getUniformLocation(name);
 
 		glActiveTexture(GL_TEXTURE0 + texture_unit);
-		glBindTexture(GL_TEXTURE_2D, image.getTextureName());
+		bindTexture(image.getTextureName(), true); // guarantee it gets bound
 		glUniform1i(location, texture_unit);
 
 		// reset texture unit
@@ -271,7 +271,7 @@ namespace opengl
 		GLint location = getUniformLocation(name);
 
 		glActiveTexture(GL_TEXTURE0 + texture_unit);
-		glBindTexture(GL_TEXTURE_2D, canvas.getTextureName());
+		bindTexture(canvas.getTextureName(), true); // guarantee it gets bound
 		glUniform1i(location, texture_unit);
 
 		// reset texture unit

+ 1 - 0
src/modules/graphics/opengl/PixelEffect.h

@@ -24,6 +24,7 @@
 #include <common/Object.h>
 #include <string>
 #include <map>
+#include "OpenGL.h"
 #include "Image.h"
 #include "Canvas.h"