not-fl3 / macroquad

Cross-platform game engine in Rust.
Apache License 2.0
3.36k stars 324 forks source link

Using image loaded from another thread #604

Open baehyunsol opened 1 year ago

baehyunsol commented 1 year ago

Is it okay to use an image (Texture2D) loaded from another thread? I spawned 2 processes, A and B. I called Texture2D::from_file_with_format in process A, and sent the Texture2D from the function to process B using mpsc::Sender.

My code is working but it's showing an empty image. I suspect something's wrong with the mpsc stuff. Is it safe to send Texture2D across processes?

not-fl3 commented 1 year ago

No, macroquad silently assumes that all its API calls are made from one single thread (like we have on wasm).

Unfortunately macroquad is not yet panicing on incorrent usage :(

baehyunsol commented 1 year ago

In that case, what would be the best way to use a Texture2D created from another thread? Is there any workaround?

I want them to run on different processes because I don't want the resource loading to block the main game process.

not-fl3 commented 1 year ago

what would be the best way to use a Texture2D created from another thread

Again, macroquad does not allow this :(

But, OpenGl is basically single-threaded, so even if macroquad would allow it, you can't really gain anything from doing texture2d::from_whatever on another thread.

You can parse PNG data on another thread, or do any CPU-heavy computation on another threads.

baehyunsol commented 1 year ago

Okay, I should figure out another way to load resources background

Thanks!