Threads in graphics module

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
grump
Party member
Posts: 577
Joined: Sat Jul 22, 2017 7:43 pm

Re: Threads in graphics module

Post by grump » Mon Mar 25, 2019 4:34 pm

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.

User avatar
slime
Solid Snayke
Posts: 2820
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Threads in graphics module

Post by slime » 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.

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).

grump
Party member
Posts: 577
Joined: Sat Jul 22, 2017 7:43 pm

Re: Threads in graphics module

Post by grump » Tue Mar 26, 2019 12:45 am

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?

User avatar
slime
Solid Snayke
Posts: 2820
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Threads in graphics module

Post by slime » Tue Mar 26, 2019 1:02 am

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).

Post Reply

Who is online

Users browsing this forum: CrimsonGuy and 8 guests