Page 1 of 1

Re: Threads in graphics module

Posted: Mon Mar 25, 2019 4:34 pm
by grump
Kidbooo wrote: Mon Mar 25, 2019 4:08 pm Can I assume it's safe to use as long as I don't do anything that might cause concurrency issues
The documentation says "can only be used in the main thread", so the only thing you can assume here is that it doesn't work reliably if you still do it.

You can always run into trouble when accessing a rendering context from multiple threads, it's an OpenGL limitation AFAIK. There's also no thread synchronization primitives in the LÖVE API, meaning you can't synchronize calls to love.graphics between multiple threads. Maybe with some Channel trickery, but that would probably be very slow and inefficient.

Re: Threads in graphics module

Posted: Mon Mar 25, 2019 11:53 pm
by slime
If you use love.graphics functions, methods, and objects in other threads it will cause crashes in the best case, or a slow and hard-to-track eventual corruption of your game's memory in the worst case.

On top of the typical synchronization problems, OpenGL is both completely full of state (so calling a function will modify global state that every other OpenGL function uses) and is inherently only bound to a single thread at any time (via a "current context" mechanism).

Re: Threads in graphics module

Posted: Tue Mar 26, 2019 12:45 am
by grump
slime wrote: Mon Mar 25, 2019 11:53 pm If you use love.graphics functions, methods, and objects in other threads it will cause crashes in the best case, or a slow and hard-to-track eventual corruption of your game's memory in the worst case.
If I disabled the window module in love.conf and replaced love.run, and never accessed any graphics related function etc. from the main thread, could I safely do all graphics stuff in another thread?

Re: Threads in graphics module

Posted: Tue Mar 26, 2019 1:02 am
by slime
Nope. love.graphics only works with an active window (the window owns the graphics context, effectively) and it calls OpenGL functions on the main thread and there has to be an explicit function call to switch the context to a different thread (which would break the main thread if you tried it).