Raylib: [textures] LoadTextureFromImage() Texture could not be created in Multithread

Created on 29 Jan 2018  路  8Comments  路  Source: raysan5/raylib

Hi nice people,
I do not understand why it does not create the texture:
look at the code, where am I wrong?

void GetImageFromCache(char * path, std::function<void(Image)>CallBack) {
CallBack(LoadImage(path));
}
std::thread t(GetImageFromCache,PATH,[this](Image img) {
TEXTURE_NORMAL = LoadTextureFromImage(img);
});

All 8 comments

To the best of my knowledge that's because you need an opengl context to load textures. The new thread doesn't have one.

Hi @TheLumaio! Thanks for the answer! You're right on the assumption!

Hello,
Thanks for your answer.
I need to load not to block the main process in dynamic loading of the asset.
@TheLumaio @raysan5

This is a pretty common problem, a simple google search will lead to a lot of different ways to solve it. You might need to change raylib youself to implement those solutions though unless @raysan5 wants to add it to the framework but I think it's probably a little out of scope of what raylib was meant to do.

@TheLumaio

You are probably right with "out of scope of what raylib was meant to be".
raylib.com: ".. raylib is a simple and easy-to-use library to learn videogames programming".
In that sense, supporting the development of commercial grade apps is out of scope.

But I think that facilitating the use of OpenGL in c programs should include loading game assets asynchronously.
Raylib should at least include example code that works on all supported platforms.

Personally, I favor examples and templates over bloated API's, that's why I am trying out raylib.

That being said, I believe raylib is fully capable of being used for commercial products. Even if raylib doesn't directly contain abstraction layers for certain things like async texture loading you still have all the tools necessary to facilitate that yourself, which is why I linked the khronos wiki page on your issue.

Personally, I favor examples and templates over bloated API's, that's why I am trying out raylib.

Agree.

About textures loading in a secondary thread, what do you mean exactly? Two options:

  • Load image in a secondary thread and then upload data to VRAM (texture) in main thread. That's easy.
  • Load image and upload data to VRAM (texture) in secondary thread. Not that easy, check this info.

Try to prepare a sample about first method this week (or maybe in the following hours). Leave this issue open.

loading_multithread.zip

Here it is an example of multithreading data loading from disk (it actually writes to disk to simulate the loading wait time because reading from disk is quite fast).

If compiling with MinGW, it requires compilation parameters -static to link statically with default pthreads library.

I'm closing this issue now, please, feel free to reopen it if needed.

Was this page helpful?
0 / 5 - 0 ratings