Browse Source

Reduce redundant glActiveTexture calls.

--HG--
branch : minor
Alex Szpakowski 8 years ago
parent
commit
14f1afa446

+ 5 - 5
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;

+ 4 - 4
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<DrawCommand> &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);
 	}
 }

+ 2 - 2
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);

+ 7 - 7
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);

+ 2 - 2
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;

+ 6 - 9
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;
 	}
 }
 

+ 1 - 1
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);

+ 1 - 1
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;

+ 1 - 6
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()

+ 1 - 1
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;
 

+ 3 - 3
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);
 	}
 }