PDA

View Full Version : Loading assets in a background thread?



Denis Hilliard
11-13-2013, 09:33 AM
Just wondering how I would go about achieving this. I was thinking of doing this either;

through a shared context and a runnable object
mapping opengl buffers into memory, loading in the background thread (then unmapped in the main thread)
loaded/decompressed in the background thread (from files) then uploaded piecemeal each frame in the main thread.

rombust
11-14-2013, 08:14 AM
I have not done this. Another developer has, but I'm not sure how he achieved it, but this is my guess (if he reads this I'm sure that he'll correct me :) )

He loads the assets in a separate thread:
1) Main thread, create new window that is hidden (for use by the worker thread)
2) Main thread contains graphics using the clan::Resource<> template. For example "clan::Resource<clan::Image>" (this can be initialised with a stock object)
3) Main thread requests the worker thread to load an image. (To load into "clan::Resource<clan::Image>")
4) Worker thread uses this hidden window's graphic context (and canvas) to load the Texture's, Images and Sprites
5) When worker thread has completed, "set" is called on "clan::Resource<clan::Image>" that now contains the new image
6) Main thread will automatically use this loaded object. Or you can use "updated()" to determine if a loading stage is complete

Note, only create worker windows in the main thread. Else you'll run into problems (I.E. Only works on some gpu's)

Again, I have personally not done this. So I could be wrong!

An additional note. The clan:: DisplayCache ( http://clanlib.org/documentation/3.0/classclan_1_1DisplayCache.html ) uses this clan::Resource<> template.
If you create your own DisplayCache to use worker threads, you can load fonts, sprites, images and textures in your own loader, and call (for example)

static Resource<Image> Image::resource(Canvas &canvas, const std::string &id, const ResourceManager &resources);
This is useful if you want clanGUI to load graphics in an external thread (why? !!! )

Denis Hilliard
11-14-2013, 10:00 AM
As a particular project grows bigger, I want to implement a loading screen so I don't block the main thread whilst loading all assets needed by a scene file.

So I will want to load the entire world in the background thread, then signal when we're done.