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

Author Topic: RenderTexture creation on a thread issue  (Read 2349 times)

0 Members and 1 Guest are viewing this topic.

sorum40

  • Newbie
  • *
  • Posts: 26
    • View Profile
RenderTexture creation on a thread issue
« on: August 19, 2017, 10:39:49 pm »
Hi;

I use a thread for background loading which does load textures, and creates renderTextures.

The thread do a glFlush and go to idle when the load is finished.

If I don't add "sf::Context cx" to the function that load textures, there is no display.
If I add it, it displays fine.

void PD_Texture::LoadFromFile(const string& xFileName)
{
        sf::Context cx; // if commented, no display

        ....
   m_pxTexture = new sf::Texture();
   m_pxTexture->loadFromMemory(pxBuffer, static_cast<size_t>(llSize));
}

So I use the sf::Context cx.

It works fine like this. But the problem happens when a second load happens.
In this second load, the textures are not loaded again (as they are still in memory), but the renderTextures are deleted, and recreated.

There is no more display, and I get a lot of OpenGL errors on the console, like:

"failed to activate OpenGL context"

Thanks for helping.
« Last Edit: August 19, 2017, 10:59:31 pm by sorum40 »

sorum40

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: RenderTexture creation on a thread issue
« Reply #1 on: August 23, 2017, 02:59:14 pm »
I would have hoped for an answer...

In the meantime, I made the RenderTexture creations to be done on the main thread.
This solves the issue.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture creation on a thread issue
« Reply #2 on: August 23, 2017, 03:56:00 pm »
In general, you should avoid dispatching OpenGL calls to multiple threads. Here, this means:
- load sf::Image from background thread and upload it to a sf::Texture from the OpenGL thread
- don't create sf::RenderTexture instances all the time, reuse them as much as possible
Laurent Gomila - SFML developer

sorum40

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: RenderTexture creation on a thread issue
« Reply #3 on: August 23, 2017, 04:44:10 pm »
In general, you should avoid dispatching OpenGL calls to multiple threads. Here, this means:
- load sf::Image from background thread and upload it to a sf::Texture from the OpenGL thread
- don't create sf::RenderTexture instances all the time, reuse them as much as possible
I still do the loadFromMemory for the sf::Texture on the loading thread.
It works fine as far as I can tell.

But is it ok to leave it like this? or should I move the sf::Texture loadFromMemory to be called from the main thread?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture creation on a thread issue
« Reply #4 on: August 23, 2017, 05:08:13 pm »
SFML is made so that it works, yes, you can keep it like this.
Laurent Gomila - SFML developer

sorum40

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: RenderTexture creation on a thread issue
« Reply #5 on: August 23, 2017, 06:00:27 pm »
SFML is made so that it works, yes, you can keep it like this.
Great thank you!