Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading with bitmap datas converted to textures silent crashes the game #2698

Open
SomeGuyWhoLovesCoding opened this issue Mar 16, 2024 · 2 comments

Comments

@SomeGuyWhoLovesCoding
Copy link

SomeGuyWhoLovesCoding commented Mar 16, 2024

Describe the bug
So, someone helped me with how I would reduce memory usage when loading an image by converting it to a texture, simply with this function:

public static function toTexture(source:BitmapData):BitmapData
{
	if (source.readable && !GL.isContextLost())
	{
		var context:Context3D = Lib.current.stage.context3D;
		var texture:TextureBase = source.getTexture(context);
		return source = BitmapData.fromTexture(texture);
	}

	return source;
}

(All the classes were imported for this btw)
And then, when I tried it out while also threading, it silent crashes the game. I was so shocked that it happened. I did make the thread handler function safer by adding a mutex which acquires before the call, and releases the mutex after that.

OpenFL and Lime versions: Latest

To Reproduce
Steps to reproduce the behavior:

  1. Basically add this:
// Add this
import lime.graphics.Image;
import openfl.display.BitmapData;

class Main extends openfl.display.Sprite
{
    public function new():Void
    {
        super();
        new sys.thread.FixedThreadPool(1).run(function()
        {
            var img:Image = toTexture(Image.fromBitmapData(BitmapData.fromFile('image.png')));
        });
    }

    // See "Describe the bug" for the function
}
  1. Test it out

Expected behavior
I wanted the bitmap data to successfully convert to a texture (toTexture function) while also using threading (sys.thread.FixedThreadPool), and I expected the texture to not crash the game.

Screenshots
[No screenshot available, just a silent game crash]

OpenFL Targets
Windows (Tested), any other target (Not tested).

Additional context
N/A

@joshtynjala
Copy link
Member

As I understand it, all Stage 3D code is required to remain in the main thread (and that is not considered a bug). You can use threads for other things, though.

@barisyild
Copy link
Contributor

barisyild commented Apr 2, 2024

toTexture method costs almost nothing, for thread-safety you should run this method in the main thread.
OpenGL is single thread supported, so you can't make gl calls in another thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants