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

Patch submitted by slime73.
This commit is contained in:
Bart van Strien
2012-01-10 15:36:18 +01:00
parent 51d4553b7d
commit 2ac4a9e6d0
10 changed files with 33 additions and 26 deletions
+11 -11
View File
@@ -77,12 +77,12 @@ namespace opengl
// generate texture save target // generate texture save target
glGenTextures(1, &img); 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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0); bindTexture(0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, img, 0); GL_TEXTURE_2D, img, 0);
@@ -96,7 +96,7 @@ namespace opengl
} }
virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img) virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img)
{ {
glDeleteTextures(1, &img); deleteTexture(img);
glDeleteRenderbuffers(1, &depth_stencil); glDeleteRenderbuffers(1, &depth_stencil);
glDeleteFramebuffers(1, &framebuffer); glDeleteFramebuffers(1, &framebuffer);
} }
@@ -129,12 +129,12 @@ namespace opengl
// generate texture save target // generate texture save target
glGenTextures(1, &img); 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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0); bindTexture(0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, img, 0); GL_TEXTURE_2D, img, 0);
@@ -149,7 +149,7 @@ namespace opengl
virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img) virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img)
{ {
glDeleteTextures(1, &img); deleteTexture(img);
glDeleteRenderbuffersEXT(1, &depth_stencil); glDeleteRenderbuffersEXT(1, &depth_stencil);
glDeleteFramebuffersEXT(1, &framebuffer); glDeleteFramebuffersEXT(1, &framebuffer);
} }
@@ -339,7 +339,7 @@ namespace opengl
GLint gmin = (f.min == Image::FILTER_NEAREST) ? GL_NEAREST : GL_LINEAR; GLint gmin = (f.min == Image::FILTER_NEAREST) ? GL_NEAREST : GL_LINEAR;
GLint gmag = (f.mag == 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_MIN_FILTER, gmin);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag);
@@ -349,7 +349,7 @@ namespace opengl
{ {
GLint gmin, gmag; 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_MIN_FILTER, &gmin);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, &gmag); 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_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; 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_S, wrap_s);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
} }
@@ -372,7 +372,7 @@ namespace opengl
Image::Wrap Canvas::getWrap() const Image::Wrap Canvas::getWrap() const
{ {
GLint wrap_s, wrap_t; 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_S, &wrap_s);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, &wrap_t); glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, &wrap_t);
@@ -420,7 +420,7 @@ namespace opengl
glMultMatrixf((const GLfloat*)t.getElements()); glMultMatrixf((const GLfloat*)t.getElements());
glBindTexture(GL_TEXTURE_2D, img); bindTexture(img);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+1
View File
@@ -29,6 +29,7 @@
#include <image/ImageData.h> #include <image/ImageData.h>
#include <common/math.h> #include <common/math.h>
#include <common/Matrix.h> #include <common/Matrix.h>
#include "OpenGL.h"
#include "GLee.h" #include "GLee.h"
namespace love namespace love
+7 -4
View File
@@ -61,7 +61,7 @@ namespace opengl
GLuint t; GLuint t;
glGenTextures(1, &t); glGenTextures(1, &t);
textures.push_back(t); textures.push_back(t);
glBindTexture(GL_TEXTURE_2D, t); bindTexture(t);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
(filter.mag == Image::FILTER_LINEAR) ? GL_LINEAR : GL_NEAREST); (filter.mag == Image::FILTER_LINEAR) ? GL_LINEAR : GL_NEAREST);
@@ -117,9 +117,11 @@ namespace opengl
createTexture(); createTexture();
} }
GLuint t = textures.back(); 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()); 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; Quad::Viewport v;
v.x = (float) texture_x; v.x = (float) texture_x;
v.y = (float) texture_y; v.y = (float) texture_y;
@@ -134,7 +136,6 @@ namespace opengl
glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&verts[0].s); glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&verts[0].s);
glNewList(g->list, GL_COMPILE); glNewList(g->list, GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, t);
glPushMatrix(); glPushMatrix();
glTranslatef(static_cast<float>(gd->getBearingX()), static_cast<float>(-gd->getBearingY()), 0.0f); glTranslatef(static_cast<float>(gd->getBearingX()), static_cast<float>(-gd->getBearingY()), 0.0f);
glDrawArrays(GL_QUADS, 0, 4); glDrawArrays(GL_QUADS, 0, 4);
@@ -185,6 +186,7 @@ namespace opengl
glPushMatrix(); glPushMatrix();
// 1.25 is magic line height for true type fonts // 1.25 is magic line height for true type fonts
if (type == FONT_TRUETYPE) glTranslatef(0, floor(getHeight() / 1.25f + 0.5f), 0); if (type == FONT_TRUETYPE) glTranslatef(0, floor(getHeight() / 1.25f + 0.5f), 0);
bindTexture(glyph->texture);
glCallList(glyph->list); glCallList(glyph->list);
glPopMatrix(); glPopMatrix();
glTranslatef(static_cast<GLfloat>(glyph->spacing), 0, 0); glTranslatef(static_cast<GLfloat>(glyph->spacing), 0, 0);
@@ -205,6 +207,7 @@ namespace opengl
if (!glyph) glyph = addGlyph(character); if (!glyph) glyph = addGlyph(character);
glPushMatrix(); glPushMatrix();
glTranslatef(x, floor(y+getHeight() + 0.5f), 0.0f); glTranslatef(x, floor(y+getHeight() + 0.5f), 0.0f);
bindTexture(glyph->texture);
glCallList(glyph->list); glCallList(glyph->list);
glPopMatrix(); glPopMatrix();
} }
@@ -344,7 +347,7 @@ namespace opengl
std::vector<GLuint>::iterator iter = textures.begin(); std::vector<GLuint>::iterator iter = textures.begin();
while (iter != textures.end()) while (iter != textures.end())
{ {
glDeleteTextures(1, (GLuint*)&*iter); deleteTexture(*iter);
iter++; iter++;
} }
textures.clear(); textures.clear();
+2
View File
@@ -31,6 +31,7 @@
#include <font/Rasterizer.h> #include <font/Rasterizer.h>
#include <graphics/Image.h> #include <graphics/Image.h>
#include "OpenGL.h"
#include "GLee.h" #include "GLee.h"
namespace love namespace love
@@ -53,6 +54,7 @@ namespace opengl
struct Glyph struct Glyph
{ {
GLuint list; GLuint list;
GLuint texture;
int spacing; int spacing;
}; };
+2
View File
@@ -41,6 +41,8 @@ namespace opengl
: currentFont(0), currentImageFilter(), lineStyle(LINE_SMOOTH), lineWidth(1), matrixLimit(0), userMatrices(0) : currentFont(0), currentImageFilter(), lineStyle(LINE_SMOOTH), lineWidth(1), matrixLimit(0), userMatrices(0)
{ {
currentWindow = love::window::sdl::Window::getSingleton(); currentWindow = love::window::sdl::Window::getSingleton();
resetBoundTexture();
} }
Graphics::~Graphics() Graphics::~Graphics()
+1
View File
@@ -37,6 +37,7 @@
#include <window/Window.h> #include <window/Window.h>
#include "OpenGL.h"
#include "Font.h" #include "Font.h"
#include "Image.h" #include "Image.h"
#include "Quad.h" #include "Quad.h"
+4 -9
View File
@@ -265,12 +265,7 @@ namespace opengl
if (texture == 0) if (texture == 0)
return; return;
GLuint boundTex; bindTexture(texture);
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *)&boundTex);
// only bind if this texture is not already bound
if (boundTex != texture)
glBindTexture(GL_TEXTURE_2D, texture);
} }
bool Image::load() bool Image::load()
@@ -294,7 +289,7 @@ namespace opengl
bool Image::loadVolatilePOT() bool Image::loadVolatilePOT()
{ {
glGenTextures(1,(GLuint*)&texture); 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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -340,7 +335,7 @@ namespace opengl
bool Image::loadVolatileNPOT() bool Image::loadVolatileNPOT()
{ {
glGenTextures(1,(GLuint*)&texture); 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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -370,7 +365,7 @@ namespace opengl
// Delete the hardware texture. // Delete the hardware texture.
if (texture != 0) if (texture != 0)
{ {
glDeleteTextures(1, (GLuint*)&texture); deleteTexture(texture);
texture = 0; texture = 0;
} }
} }
+2
View File
@@ -28,6 +28,8 @@
#include <image/ImageData.h> #include <image/ImageData.h>
#include <graphics/Image.h> #include <graphics/Image.h>
#include "OpenGL.h"
// OpenGL // OpenGL
#include "GLee.h" #include "GLee.h"
+2 -2
View File
@@ -253,7 +253,7 @@ namespace opengl
GLint location = getUniformLocation(name); GLint location = getUniformLocation(name);
glActiveTexture(GL_TEXTURE0 + texture_unit); glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_2D, image.getTextureName()); bindTexture(image.getTextureName(), true); // guarantee it gets bound
glUniform1i(location, texture_unit); glUniform1i(location, texture_unit);
// reset texture unit // reset texture unit
@@ -271,7 +271,7 @@ namespace opengl
GLint location = getUniformLocation(name); GLint location = getUniformLocation(name);
glActiveTexture(GL_TEXTURE0 + texture_unit); glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_2D, canvas.getTextureName()); bindTexture(canvas.getTextureName(), true); // guarantee it gets bound
glUniform1i(location, texture_unit); glUniform1i(location, texture_unit);
// reset texture unit // reset texture unit
@@ -24,6 +24,7 @@
#include <common/Object.h> #include <common/Object.h>
#include <string> #include <string>
#include <map> #include <map>
#include "OpenGL.h"
#include "Image.h" #include "Image.h"
#include "Canvas.h" #include "Canvas.h"