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

Author Topic: Multithreaded loading screen  (Read 5505 times)

0 Members and 2 Guests are viewing this topic.

ison

  • Newbie
  • *
  • Posts: 13
    • View Profile
Multithreaded loading screen
« on: September 24, 2012, 08:33:21 pm »
Hello,
is it possible to load textures in another thread without using setActive?
I want to render and load resources simultaneously but it seems that textures can't be loaded from another thread.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Multithreaded loading screen
« Reply #1 on: September 24, 2012, 08:55:20 pm »
There's nothing to do to load textures in a thread, it works out of the box.
Laurent Gomila - SFML developer

ison

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Multithreaded loading screen
« Reply #2 on: September 24, 2012, 09:06:53 pm »
Oh, it seems you are right :)
I put loading resources in another thread and textures weren't displaying so I thought the problem was with textures loading, however I just noticed that it was because of FBO creation. They can't be created in another thread. FBOs creation is no big deal for me and can be done before resources loading thread, I guess they just need to be created with setActive(true) in current thread.
sf::Texture tex;
sf::RenderTexture fbo;

void func()
{
    tex.loadFromFile("test.png");
    fbo.create(100, 100);
}

int main(int argc, char **argv)
{
    sf::RenderWindow window(sf::VideoMode(1024, 768), "test", sf::Style::Default);
    sf::Thread thread(func);
    thread.launch();
    thread.wait();
    fbo.clear(sf::Color::White);
    fbo.draw(sf::Sprite(tex));
    fbo.display();
    window.draw(sf::Sprite(fbo.getTexture()));
    window.display();
    sf::sleep(sf::milliseconds(3000));
    return 0;
}
 
« Last Edit: September 24, 2012, 10:29:54 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Multithreaded loading screen
« Reply #3 on: September 24, 2012, 10:31:17 pm »
RenderTextures are supposed to work the same. Can you tell me more about this issue, maybe with a minimal code that reproduces the problem?
Laurent Gomila - SFML developer

ison

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Multithreaded loading screen
« Reply #4 on: September 24, 2012, 11:10:23 pm »
The one given in previous post is minimal bug-reproducing code, at least for me. When I change thread.launch() to just func() everything works fine - bitmap is displayed on a screen, if I use thread.launch() nothing is shown.

However, I found more interesting bug which I was debugging for ~2 hours. I found that you can't draw text using a default font in 1 thread while in second one work on text with the same font. What I was doing was displaying loading screen in main thread while in second load resources. When I used something like getLocalBounds() on a text (not the same text variable, just the same font - default one) in a second thread then font became damaged - some characters like 'I' were missing whenever I wanted to draw text with default font.
UB bugs are really hard to debug, it seems like index-out-of-bounds write so some character sprites are damaged(?) I don't know anything else that could do such thing. Maybe later I'll try to get minimal bug-reproducing code for this bug.
« Last Edit: September 24, 2012, 11:13:07 pm by ison »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Multithreaded loading screen
« Reply #5 on: September 24, 2012, 11:40:24 pm »
However, I found more interesting bug which I was debugging for ~2 hours. I found that you can't draw text using a default font in 1 thread while in second one work on text with the same font. What I was doing was displaying loading screen in main thread while in second load resources. When I used something like getLocalBounds() on a text (not the same text variable, just the same font - default one) in a second thread then font became damaged - some characters like 'I' were missing whenever I wanted to draw text with default font.
UB bugs are really hard to debug, it seems like index-out-of-bounds write so some character sprites are damaged(?) I don't know anything else that could do such thing. Maybe later I'll try to get minimal bug-reproducing code for this bug.
Doesn't really matter anymore, since the default font is removed in the latest commit. Also there have been many similar bug reports already (search in the forum), so you can check out the known issues on github. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Multithreaded loading screen
« Reply #6 on: September 24, 2012, 11:44:35 pm »
There are some synchronization issues; glFlush() usually sloves them.
Laurent Gomila - SFML developer

 

anything