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

Author Topic: Parallel resource loading possible?  (Read 6519 times)

0 Members and 1 Guest are viewing this topic.

SajSFML

  • Newbie
  • *
  • Posts: 7
    • View Profile
Parallel resource loading possible?
« on: October 25, 2021, 12:25:09 am »
Hello you all, I am using SFML for my application and I have quite a few images to load. I was wondering if I can improve the total time it takes by loading the images in parallel? Is SFML thread-safe for me to do this?

BTW, the version I am using is 2.5.1, if you need to know.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Parallel resource loading possible?
« Reply #1 on: October 25, 2021, 11:55:09 am »
Nothing in SFML can really be assumed thread-safe.

Note that OpenGL doesn't play very well with multi-threading, as such it's generally recommended to not do any OpenGL calls (include SFML internal ones) in separate threads.
My recommendation for working with resources/images is to load the images with sf::Image in parallel into memory. This is the slow-ish part of disk reading and decompression and doesn't use any OpenGL, as such won't cause issues with OpenGL contexts. Then as a second step, you load the image data from RAM to VRAM in the main thread with the help of sf::Texture.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SajSFML

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Parallel resource loading possible?
« Reply #2 on: October 25, 2021, 04:43:21 pm »
So, this is the difference between sf::Image and sF::Texture? If I call .loadFromFile on sf::Image, then it won't do OpenGL stuff?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Parallel resource loading possible?
« Reply #3 on: October 25, 2021, 05:28:19 pm »
sf::Image just loads the image from disk and converts the format into a RGBA pixel array, while sf::Texture will take that pixel data and load it onto the GPU.

You can check out both implementations here:

Image: https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/ImageLoader.cpp#L104
Texture: https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Texture.cpp#L246
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SajSFML

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Parallel resource loading possible?
« Reply #4 on: October 25, 2021, 05:44:20 pm »
Okay I think I can understand, so I can safely load with sf::Image in parallel, but then I still have to load into sf::Texture sequentially.

 

anything