From 2ac4a9e6d02108a630503a2cbda5ad53d39e6fbf Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Tue, 10 Jan 2012 15:36:18 +0100 Subject: [PATCH] Manually keep track of bound textures, so we don't have to poll each frame Patch submitted by slime73. --- src/modules/graphics/opengl/Canvas.cpp | 22 ++++++++++----------- src/modules/graphics/opengl/Canvas.h | 1 + src/modules/graphics/opengl/Font.cpp | 11 +++++++---- src/modules/graphics/opengl/Font.h | 2 ++ src/modules/graphics/opengl/Graphics.cpp | 2 ++ src/modules/graphics/opengl/Graphics.h | 1 + src/modules/graphics/opengl/Image.cpp | 13 ++++-------- src/modules/graphics/opengl/Image.h | 2 ++ src/modules/graphics/opengl/PixelEffect.cpp | 4 ++-- src/modules/graphics/opengl/PixelEffect.h | 1 + 10 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/modules/graphics/opengl/Canvas.cpp b/src/modules/graphics/opengl/Canvas.cpp index d0114ca5d..a24dd969e 100644 --- a/src/modules/graphics/opengl/Canvas.cpp +++ b/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); diff --git a/src/modules/graphics/opengl/Canvas.h b/src/modules/graphics/opengl/Canvas.h index 71a4409f9..cc919cd16 100644 --- a/src/modules/graphics/opengl/Canvas.h +++ b/src/modules/graphics/opengl/Canvas.h @@ -29,6 +29,7 @@ #include #include #include +#include "OpenGL.h" #include "GLee.h" namespace love diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index ca4bcd967..9fc6186ba 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/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(gd->getBearingX()), static_cast(-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(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::iterator iter = textures.begin(); while (iter != textures.end()) { - glDeleteTextures(1, (GLuint*)&*iter); + deleteTexture(*iter); iter++; } textures.clear(); diff --git a/src/modules/graphics/opengl/Font.h b/src/modules/graphics/opengl/Font.h index b9c78fced..5a9e02c33 100644 --- a/src/modules/graphics/opengl/Font.h +++ b/src/modules/graphics/opengl/Font.h @@ -31,6 +31,7 @@ #include #include +#include "OpenGL.h" #include "GLee.h" namespace love @@ -53,6 +54,7 @@ namespace opengl struct Glyph { GLuint list; + GLuint texture; int spacing; }; diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index b59d3a0c3..1121d8372 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/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() diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index d415b685b..e0d967760 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -37,6 +37,7 @@ #include +#include "OpenGL.h" #include "Font.h" #include "Image.h" #include "Quad.h" diff --git a/src/modules/graphics/opengl/Image.cpp b/src/modules/graphics/opengl/Image.cpp index 38df49129..0de9566fd 100644 --- a/src/modules/graphics/opengl/Image.cpp +++ b/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; } } diff --git a/src/modules/graphics/opengl/Image.h b/src/modules/graphics/opengl/Image.h index 6a7a62a2a..3304c84a2 100644 --- a/src/modules/graphics/opengl/Image.h +++ b/src/modules/graphics/opengl/Image.h @@ -28,6 +28,8 @@ #include #include +#include "OpenGL.h" + // OpenGL #include "GLee.h" diff --git a/src/modules/graphics/opengl/PixelEffect.cpp b/src/modules/graphics/opengl/PixelEffect.cpp index c5963cc51..ac33c28a0 100644 --- a/src/modules/graphics/opengl/PixelEffect.cpp +++ b/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 diff --git a/src/modules/graphics/opengl/PixelEffect.h b/src/modules/graphics/opengl/PixelEffect.h index 8e911f893..552e4ad0f 100644 --- a/src/modules/graphics/opengl/PixelEffect.h +++ b/src/modules/graphics/opengl/PixelEffect.h @@ -24,6 +24,7 @@ #include #include #include +#include "OpenGL.h" #include "Image.h" #include "Canvas.h"