Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Texture::checkMaximumTextureSize() creates a new gl context  (Read 1794 times)

0 Members and 1 Guest are viewing this topic.

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Hiya,

I just thought I'd share a problem I encountered incorporating a 3rd party opengl-based UI library into my game.

There were some SFML functions which, if called, would cause problems with the UI library, such as corrupt or missing graphics.  It turns out the problem was that these SFML functions create a new sf::Context.  Example:

Texture.cpp
    unsigned int checkMaximumTextureSize()
    {
        // Create a temporary context in case the user queries
        // the size before a GlResource is created, thus
        // initializing the shared context
        sf::Context context;

        GLint size;
        glCheck(glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size));

        return static_cast<unsigned int>(size);
    }
 

The code comments make it obvious why this is done.  And that makes sense.  However, in my case this led to quite a bit of debugging while incorporating the UI library.  Granted, I am quite clueless about low level OpenGL code, and I'm sure an expert may have discovered the problem more quickly.  And the solution is simple, once you understand the problem - just ensure to call setActive again on the RenderWindow (or whatever target you're rendering to).

Regardless, to save others the trouble in the future, perhaps it might be worth it to check for an existing context first, before creating a new one?  And only create a new context if none is already present.

If there's good reasons not to do this, no problem.  I'm just sharing a user experience with SFML here.  It's one of the few times it's caused me some trouble.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: sf::Texture::checkMaximumTextureSize() creates a new gl context
« Reply #1 on: June 18, 2015, 11:06:34 am »
It was hard for me to fix a bug in a way such as this, but I tried to document it the best I can as seen in the documentation here. Note that a context switch and checkMaximumTextureSize() is only ever called once. It isn't called every single time someone needs to know the maximum texture size, the value is cached until the application exits.

If you don't like surprises (I know I don't), just call sf::Texture::getMaximumSize() once at the beginning of your application (you can even call it before creating the window if you want), and no more unexpected context switches will happen, i.e. it will behave just like it did before this workaround was implemented.

Regardless, to save others the trouble in the future, perhaps it might be worth it to check for an existing context first, before creating a new one?  And only create a new context if none is already present.
This was an idea I had at the time, and something I might go ahead and add now. Adding a way to check if there currently is a context active can be useful both to SFML, external libraries (SFGUI included) and user code, it would save on all those temporary contexts that are currently created "just to be safe". The problem that they solved was a complicated one, which only arose when users would use these static functions before actually creating an SFML window. It completely messed up the internal context management, so this workaround was built in. Either way, I always try to make it less painful to work with SFML and external libraries that use OpenGL.

If there's good reasons not to do this, no problem.  I'm just sharing a user experience with SFML here.  It's one of the few times it's caused me some trouble.
It's nice of you to report such things. I know that this was an issue, but I didn't know how much impact it would have on the "daily lives" of developers. I guess now that it is obvious that it might cause a lot of time wastage a better solution has to be thought of.

I encourage all SFML users, no matter whether experienced or not, to always report such things. This is the only way we can judge whether changes we made in the past were acceptable and whether we need to change them again in the future.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: sf::Texture::checkMaximumTextureSize() creates a new gl context
« Reply #2 on: June 18, 2015, 01:16:43 pm »
Cool, appreciate the informative and thoughtful response binary.

 

anything