Multi-threading support

Let’s say I have a function that creates Tiles from BitmapData, the gist of which is like:

var batch : h2d.SpriteBatch;
var bitmaps : Array<BitmapData>;

function make_tiles() {

    for( bdata in bitmaps ) {

        batch.add( new BatchElement( Tile.fromBitmap( bdata ) ) );
    }
}

If I call this function, for example, in App::init() or App::update() (just once) etc. it will work fine, it will create all the Tiles, the only issue is that application will freeze for a second or two while this loop is doing it’s work.
I tried to run this in a Thread with Thread.create(make_tiles) but that fails with:

SIGNAL 11
h3d.impl.MemoryManager.allocTexture(h3d/impl/MemoryManager.hx:258)
h3d.mat.Texture.alloc(h3d/mat/Texture.hx:122)
h3d.mat.$Texture.__constructor__(h3d/mat/Texture.hx:113)
h3d.mat.$Texture.fromBitmap(h3d/mat/Texture.hx:413)
h2d.$Tile.fromBitmap(h2d/Tile.hx:368)
Main.create_tiles(Main.hx:83)
sys.thread._Thread.$HaxeThread.~create.0(/usr/share/haxe/std/hl/_std/sys/thread/Thread.hx:131)
/usr/local/bin/hl: line 3: 24445 Segmentation fault      (core dumped) hl.orig "$@"

no matter where I do Thread.create(): in init(), update(), in WaitEvent::waitUntil() etc.

The question: is there a way (maybe not Thread but something else) where I can start some long processing (like in this example, creating a bunch of Tiles) in a way that does not block entire Heaps application?

While we have thread support, the graphics driver itself does not support threads. Where does these BitmapData comes from and how many do you have? It’s rare to freeze with this kind of thing unless you’re uploading very big textures from cpu to gpu

1 Like

Thanks for the response @ncannasse!

I’m loading the images from local file system where user gets to select a folder that contains them, so they are not part of resources/assets. In this particular case it was about 20 images, 4-15MB each, in approx. 2k resolution and BitmapData is created via new Image( ... ).toBitmap().

It takes about 4-5 seconds to load all of them and ~90% time it is the new Image( ... ).toBitmap() calls, so I actually put those into a separate function and ran it with Thread.cretae(). That way the app can show a loading animation while images are loading, or let user do other stuff. Once the bitmap data is loaded, I did that Tile.fromBitmap() loop which briefly freezes the app.

In the meantime, for this particular case, I solved it by lazy-creating the tiles when I actually need to use/display a specific image, and not doing it in the loop, so the freeze rarely happens and when it does it is super short and barely noticable.

Multi-threaded rendering is not as simple as it may sound, especially when one has to support different graphics drivers (opengl, directx, etc.) but I had to ask just in case there was something in Heaps I missed about async loading, that could maybe help me with that brief freeze.