From 14f1afa4462837efe44d38d6bc941a78d87ddee5 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Fri, 21 Oct 2016 22:19:16 -0300 Subject: [PATCH] Reduce redundant glActiveTexture calls. --HG-- branch : minor --- src/modules/graphics/opengl/Canvas.cpp | 10 +++++----- src/modules/graphics/opengl/Font.cpp | 8 ++++---- src/modules/graphics/opengl/Graphics.cpp | 4 ++-- src/modules/graphics/opengl/Image.cpp | 14 +++++++------- src/modules/graphics/opengl/Mesh.cpp | 4 ++-- src/modules/graphics/opengl/OpenGL.cpp | 15 ++++++--------- src/modules/graphics/opengl/ParticleSystem.cpp | 2 +- src/modules/graphics/opengl/Polyline.cpp | 2 +- src/modules/graphics/opengl/Shader.cpp | 7 +------ src/modules/graphics/opengl/SpriteBatch.cpp | 2 +- src/modules/graphics/opengl/Video.cpp | 6 +++--- 11 files changed, 33 insertions(+), 41 deletions(-) diff --git a/src/modules/graphics/opengl/Canvas.cpp b/src/modules/graphics/opengl/Canvas.cpp index c7f6ad2d6..51d80bbc3 100644 --- a/src/modules/graphics/opengl/Canvas.cpp +++ b/src/modules/graphics/opengl/Canvas.cpp @@ -217,7 +217,7 @@ bool Canvas::loadVolatile() requested_samples = std::max(requested_samples, 0); glGenTextures(1, &texture); - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); if (GLAD_ANGLE_texture_usage) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_USAGE_ANGLE, GL_FRAMEBUFFER_ATTACHMENT_ANGLE); @@ -311,7 +311,7 @@ void Canvas::drawv(const Matrix4 &t, const Vertex *v) OpenGL::TempTransform transform(gl); transform.get() *= t; - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); gl.useVertexAttribArrays(ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD); @@ -339,7 +339,7 @@ void Canvas::setFilter(const Texture::Filter &f) throw love::Exception("Invalid texture filter."); filter = f; - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); gl.setTextureFilter(filter); } @@ -366,7 +366,7 @@ bool Canvas::setWrap(const Texture::Wrap &w) wrap.t = WRAP_CLAMP; } - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); gl.setTextureWrap(wrap); return success; @@ -936,7 +936,7 @@ bool Canvas::isFormatSupported(Canvas::Format format) GLuint texture = 0; glGenTextures(1, &texture); - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); Texture::Filter f; f.min = f.mag = Texture::FILTER_NEAREST; diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index 5ce09186a..aed92c740 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -169,7 +169,7 @@ void Font::createTexture() else glGenTextures(1, &t); - gl.bindTexture(t); + gl.bindTextureToUnit(t, 0, false); gl.setTextureFilter(filter); @@ -288,7 +288,7 @@ const Font::Glyph &Font::addGlyph(uint32 glyph) GLenum format = getTextureFormat(type); g.texture = textures.back(); - gl.bindTexture(g.texture); + gl.bindTextureToUnit(g.texture, 0, false); glTexSubImage2D(GL_TEXTURE_2D, 0, textureX, textureY, w, h, format, GL_UNSIGNED_BYTE, gd->getData()); @@ -674,7 +674,7 @@ void Font::drawVertices(const std::vector &drawcommands, bool buffe size_t offset = (cmd.startvertex / 4) * 6 * elemsize; // TODO: Use glDrawElementsBaseVertex when supported? - gl.bindTexture(cmd.texture); + gl.bindTextureToUnit(cmd.texture, 0, false); if (bufferedvertices) gl.drawElements(GL_TRIANGLES, count, gltype, quadIndices.getPointer(offset)); @@ -976,7 +976,7 @@ void Font::setFilter(const Texture::Filter &f) for (GLuint texture : textures) { - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); gl.setTextureFilter(filter); } } diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 3cb2db610..7635853cf 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -1287,7 +1287,7 @@ void Graphics::points(const float *coords, const uint8 *colors, size_t numpoints OpenGL::TempDebugGroup debuggroup("Graphics points draw"); gl.prepareDraw(); - gl.bindTexture(gl.getDefaultTexture()); + gl.bindTextureToUnit(gl.getDefaultTexture(), 0, false); gl.bindBuffer(BUFFER_VERTEX, 0); uint32 attribflags = ATTRIBFLAG_POS; @@ -1550,7 +1550,7 @@ void Graphics::polygon(DrawMode mode, const float *coords, size_t count) OpenGL::TempDebugGroup debuggroup("Filled polygon draw"); gl.prepareDraw(); - gl.bindTexture(gl.getDefaultTexture()); + gl.bindTextureToUnit(gl.getDefaultTexture(), 0, false); gl.bindBuffer(BUFFER_VERTEX, 0); gl.useVertexAttribArrays(ATTRIBFLAG_POS); glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, coords); diff --git a/src/modules/graphics/opengl/Image.cpp b/src/modules/graphics/opengl/Image.cpp index 5e971eb31..73b6fae81 100644 --- a/src/modules/graphics/opengl/Image.cpp +++ b/src/modules/graphics/opengl/Image.cpp @@ -218,7 +218,7 @@ void Image::loadDefaultTexture() { usingDefaultTexture = true; - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); setFilter(filter); // A nice friendly checkerboard to signify invalid textures... @@ -320,7 +320,7 @@ bool Image::loadVolatile() glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS, &maxMipmapSharpness); glGenTextures(1, &texture); - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); setFilter(filter); setWrap(wrap); @@ -406,7 +406,7 @@ bool Image::refresh(int xoffset, int yoffset, int w, int h) OpenGL::TempDebugGroup debuggroup("Image refresh"); - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); if (isCompressed()) { @@ -449,7 +449,7 @@ void Image::drawv(const Matrix4 &t, const Vertex *v) OpenGL::TempTransform transform(gl); transform.get() *= t; - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); gl.useVertexAttribArrays(ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD); @@ -513,7 +513,7 @@ void Image::setFilter(const Texture::Filter &f) filter.min = filter.mag = FILTER_NEAREST; } - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); gl.setTextureFilter(filter); } @@ -540,7 +540,7 @@ bool Image::setWrap(const Texture::Wrap &w) wrap.t = WRAP_CLAMP; } - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); gl.setTextureWrap(wrap); return success; @@ -555,7 +555,7 @@ void Image::setMipmapSharpness(float sharpness) // LOD bias has the range (-maxbias, maxbias) mipmapSharpness = std::min(std::max(sharpness, -maxMipmapSharpness + 0.01f), maxMipmapSharpness - 0.01f); - gl.bindTexture(texture); + gl.bindTextureToUnit(texture, 0, false); // negative bias is sharper glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -mipmapSharpness); diff --git a/src/modules/graphics/opengl/Mesh.cpp b/src/modules/graphics/opengl/Mesh.cpp index 16807d780..8d7e7e87b 100644 --- a/src/modules/graphics/opengl/Mesh.cpp +++ b/src/modules/graphics/opengl/Mesh.cpp @@ -582,9 +582,9 @@ void Mesh::draw(const Matrix4 &m) gl.useVertexAttribArrays(enabledattribs); if (texture.get()) - gl.bindTexture(*(GLuint *) texture->getHandle()); + gl.bindTextureToUnit(*(GLuint *) texture->getHandle(), 0, false); else - gl.bindTexture(gl.getDefaultTexture()); + gl.bindTextureToUnit(gl.getDefaultTexture(), 0, false); OpenGL::TempTransform transform(gl); transform.get() *= m; diff --git a/src/modules/graphics/opengl/OpenGL.cpp b/src/modules/graphics/opengl/OpenGL.cpp index e7e70264c..a7f293e18 100644 --- a/src/modules/graphics/opengl/OpenGL.cpp +++ b/src/modules/graphics/opengl/OpenGL.cpp @@ -560,9 +560,6 @@ GLuint OpenGL::getDefaultTexture() const void OpenGL::setTextureUnit(int textureunit) { - if (textureunit < 0 || (size_t) textureunit >= state.boundTextures.size()) - throw love::Exception("Invalid texture unit index (%d).", textureunit); - if (textureunit != state.curTextureUnit) glActiveTexture(GL_TEXTURE0 + textureunit); @@ -580,19 +577,19 @@ void OpenGL::bindTexture(GLuint texture) void OpenGL::bindTextureToUnit(GLuint texture, int textureunit, bool restoreprev) { - if (textureunit < 0 || (size_t) textureunit >= state.boundTextures.size()) - throw love::Exception("Invalid texture unit index."); - if (texture != state.boundTextures[textureunit]) { int oldtextureunit = state.curTextureUnit; - setTextureUnit(textureunit); + if (oldtextureunit != textureunit) + glActiveTexture(GL_TEXTURE0 + textureunit); state.boundTextures[textureunit] = texture; glBindTexture(GL_TEXTURE_2D, texture); - if (restoreprev) - setTextureUnit(oldtextureunit); + if (restoreprev && oldtextureunit != textureunit) + glActiveTexture(GL_TEXTURE0 + oldtextureunit); + else + state.curTextureUnit = textureunit; } } diff --git a/src/modules/graphics/opengl/ParticleSystem.cpp b/src/modules/graphics/opengl/ParticleSystem.cpp index 9f6e97c7e..43ac27dda 100644 --- a/src/modules/graphics/opengl/ParticleSystem.cpp +++ b/src/modules/graphics/opengl/ParticleSystem.cpp @@ -132,7 +132,7 @@ void ParticleSystem::draw(const Matrix4 &m) p = p->next; } - gl.bindTexture(*(GLuint *) texture->getHandle()); + gl.bindTextureToUnit(*(GLuint *) texture->getHandle(), 0, false); gl.prepareDraw(); gl.useVertexAttribArrays(ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD | ATTRIBFLAG_COLOR); diff --git a/src/modules/graphics/opengl/Polyline.cpp b/src/modules/graphics/opengl/Polyline.cpp index 0e9bb21f3..dad500b64 100644 --- a/src/modules/graphics/opengl/Polyline.cpp +++ b/src/modules/graphics/opengl/Polyline.cpp @@ -401,7 +401,7 @@ void Polyline::draw() gl.prepareDraw(); - gl.bindTexture(gl.getDefaultTexture()); + gl.bindTextureToUnit(gl.getDefaultTexture(), 0, false); gl.bindBuffer(BUFFER_VERTEX, 0); uint32 enabledattribs = ATTRIBFLAG_POS; diff --git a/src/modules/graphics/opengl/Shader.cpp b/src/modules/graphics/opengl/Shader.cpp index 4696becb8..8f5b8ddc9 100644 --- a/src/modules/graphics/opengl/Shader.cpp +++ b/src/modules/graphics/opengl/Shader.cpp @@ -425,9 +425,6 @@ void Shader::attach(bool temporary) if (activeTexUnits[i] > 0) gl.bindTextureToUnit(activeTexUnits[i], i + 1, false); } - - // We always want to use texture unit 0 for everyhing else. - gl.setTextureUnit(0); } } } @@ -556,7 +553,7 @@ void Shader::sendTexture(const UniformInfo *info, Texture *texture) int texunit = getTextureUnit(info->name); // bind texture to assigned texture unit and send uniform to shader program - gl.bindTextureToUnit(gltex, texunit, true); + gl.bindTextureToUnit(gltex, texunit, false); glUniform1i(info->location, texunit); @@ -683,8 +680,6 @@ void Shader::setVideoTextures(GLuint ytexture, GLuint cbtexture, GLuint crtextur gl.bindTextureToUnit(textures[i], videoTextureUnits[i], false); } } - - gl.setTextureUnit(0); } void Shader::checkSetScreenParams() diff --git a/src/modules/graphics/opengl/SpriteBatch.cpp b/src/modules/graphics/opengl/SpriteBatch.cpp index 426929fad..12a8aa2f3 100644 --- a/src/modules/graphics/opengl/SpriteBatch.cpp +++ b/src/modules/graphics/opengl/SpriteBatch.cpp @@ -252,7 +252,7 @@ void SpriteBatch::draw(const Matrix4 &m) OpenGL::TempTransform transform(gl); transform.get() *= m; - gl.bindTexture(*(GLuint *) texture->getHandle()); + gl.bindTextureToUnit(*(GLuint *) texture->getHandle(), 0, false); uint32 enabledattribs = ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD; diff --git a/src/modules/graphics/opengl/Video.cpp b/src/modules/graphics/opengl/Video.cpp index 8016426f1..4f452dc59 100644 --- a/src/modules/graphics/opengl/Video.cpp +++ b/src/modules/graphics/opengl/Video.cpp @@ -88,7 +88,7 @@ bool Video::loadVolatile() for (int i = 0; i < 3; i++) { - gl.bindTexture(textures[i]); + gl.bindTextureToUnit(textures[i], 0, false); gl.setTextureFilter(filter); gl.setTextureWrap(wrap); @@ -162,7 +162,7 @@ void Video::update() for (int i = 0; i < 3; i++) { - gl.bindTexture(textures[i]); + gl.bindTextureToUnit(textures[i], 0, false); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[i], heights[i], GL_LUMINANCE, GL_UNSIGNED_BYTE, data[i]); } @@ -198,7 +198,7 @@ void Video::setFilter(const Texture::Filter &f) for (int i = 0; i < 3; i++) { - gl.bindTexture(textures[i]); + gl.bindTextureToUnit(textures[i], 0, false); gl.setTextureFilter(filter); } }