From 541509d2a70675045b077c592cc02cd88531953e Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Fri, 12 Aug 2011 15:40:32 +0200 Subject: [PATCH] sf::Texture's function don't preserve the current texture binding anymore (was used to keep mixing SFML and OpenGL safe, but the potential performance loss was too high -- glGet* may trigger a glFlush) --- src/SFML/Graphics/GLCheck.hpp | 2 +- .../Graphics/RenderTextureImplDefault.cpp | 5 --- src/SFML/Graphics/Shader.cpp | 18 +++++++- src/SFML/Graphics/Texture.cpp | 41 ------------------- 4 files changed, 17 insertions(+), 49 deletions(-) diff --git a/src/SFML/Graphics/GLCheck.hpp b/src/SFML/Graphics/GLCheck.hpp index 3952b4a9..3be57e1a 100644 --- a/src/SFML/Graphics/GLCheck.hpp +++ b/src/SFML/Graphics/GLCheck.hpp @@ -44,7 +44,7 @@ namespace priv #ifdef SFML_DEBUG // In debug mode, perform a test on every OpenGL call - #define GLCheck(call) ((call), priv::GLCheckError(__FILE__, __LINE__)) +#define GLCheck(call) ((call), sf::priv::GLCheckError(__FILE__, __LINE__)) #else diff --git a/src/SFML/Graphics/RenderTextureImplDefault.cpp b/src/SFML/Graphics/RenderTextureImplDefault.cpp index 9450f6e3..7cffc63c 100644 --- a/src/SFML/Graphics/RenderTextureImplDefault.cpp +++ b/src/SFML/Graphics/RenderTextureImplDefault.cpp @@ -77,14 +77,9 @@ bool RenderTextureImplDefault::Activate(bool active) //////////////////////////////////////////////////////////// void RenderTextureImplDefault::UpdateTexture(unsigned int textureId) { - GLint previous; - GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); - // Copy the rendered pixels to the texture GLCheck(glBindTexture(GL_TEXTURE_2D, textureId)); GLCheck(glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, myWidth, myHeight)); - - GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); } } // namespace priv diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index 904da0a7..e5c62fd6 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -34,6 +34,21 @@ #include +//////////////////////////////////////////////////////////// +// Private data +//////////////////////////////////////////////////////////// +namespace +{ + // Retrieve the maximum number of texture units available + GLint GetMaxTextureUnits() + { + GLint maxUnits; + GLCheck(glGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &maxUnits)); + return maxUnits; + } +} + + namespace sf { //////////////////////////////////////////////////////////// @@ -244,8 +259,7 @@ void Shader::SetTexture(const std::string& name, const Texture& texture) if (it == myTextures.end()) { // New entry, make sure there are enough texture units - GLint maxUnits; - GLCheck(glGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &maxUnits)); + static const GLint maxUnits = GetMaxTextureUnits(); if (myTextures.size() + 1 >= static_cast(maxUnits)) { Err() << "Impossible to use texture \"" << name << "\" for shader: all available texture units are used" << std::endl; diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index fbd9d602..e623e325 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -121,10 +121,6 @@ bool Texture::Create(unsigned int width, unsigned int height) myTexture = static_cast(texture); } - // Save the current texture binding - GLint previous; - GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); - // Initialize the texture GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture)); GLCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, myTextureWidth, myTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL)); @@ -133,9 +129,6 @@ bool Texture::Create(unsigned int width, unsigned int height) GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST)); GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST)); - // Restore the previous texture - GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); - return true; } @@ -200,10 +193,6 @@ bool Texture::LoadFromImage(const Image& image, const IntRect& area) // Create the texture and upload the pixels if (Create(rectangle.Width, rectangle.Height)) { - // Save the current texture binding - GLint previous; - GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); - // Copy the pixels to the texture, row by row const Uint8* pixels = image.GetPixelsPtr() + rectangle.Left + (width * rectangle.Top); GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture)); @@ -212,10 +201,6 @@ bool Texture::LoadFromImage(const Image& image, const IntRect& area) GLCheck(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, i, myWidth, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); pixels += width; } - myPixelsFlipped = false; - - // Restore the previous texture - GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); return true; } @@ -250,10 +235,6 @@ Image Texture::CopyToImage() const EnsureGlContext(); - // Save the previous texture - GLint previous; - GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); - // Create an array of pixels std::vector pixels(myWidth * myHeight * 4); @@ -293,9 +274,6 @@ Image Texture::CopyToImage() const } } - // Restore the previous texture - GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); - // Create the image Image image; image.Create(myWidth, myHeight, &pixels[0]); @@ -322,17 +300,10 @@ void Texture::Update(const Uint8* pixels, unsigned int width, unsigned int heigh { EnsureGlContext(); - // Save the current texture binding - GLint previous; - GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); - // Copy pixels from the given array to the texture GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture)); GLCheck(glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); myPixelsFlipped = false; - - // Restore the previous texture - GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); } } @@ -367,17 +338,10 @@ void Texture::Update(const Window& window, unsigned int x, unsigned int y) if (myTexture && window.SetActive(true)) { - // Save the current texture binding - GLint previous; - GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); - // Copy pixels from the back-buffer to the texture GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture)); GLCheck(glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, y, 0, 0, window.GetWidth(), window.GetHeight())); myPixelsFlipped = true; - - // Restore the previous texture - GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); } } @@ -400,14 +364,9 @@ void Texture::SetSmooth(bool smooth) { EnsureGlContext(); - GLint previous; - GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); - GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture)); GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST)); GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST)); - - GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); } } }