I have a really weird issue happening with my little OpenGL project. There's a
certain form that if recompiled should do an update procedure (it recompiles gpu
shaders on the fly among other things). If I recompile it from a Lisp file via
Slime's C-c C-c, I get a bizarre error:
Unhandled memory fault at #x13B0.
Yet, if I take the exact same form, as the program is still running, and enter it at the REPL, it works perfectly fine as it should. I have ZERO clue why this is happening.
With someone's help it seems to be because I can't call gl code across different
threads (and share the gl context). And C-c C-c is executing code in a new
thread every time.
To be a bit more specific, in the case of OpenGL:
An OpenGL context represents many things. A context stores all of the state associated with this instance of OpenGL. It represents the (potentially visible) default framebuffer that rendering commands will draw to when not drawing to a framebuffer object. Think of a context as an object that holds all of OpenGL; when a context is destroyed, OpenGL is destroyed.
[…]
In order for any OpenGL commands to work, a context must be current; all OpenGL commands affect the state of whichever context is current. The current context is a thread-local variable, so a single process can have several threads, each of which has its own current context. However, a single context cannot be current in multiple threads at the same time.
In my case, as I understand it, my OpenGL context is the thread that starts from
the REPL (the main thread). Previously I was trying to execute gl commands from
another thread (Because every time you C-c C-c, SLIME starts another thread
for that compilation) that did not have any associated context. The last part
seems to be what was biting me: you cannot share a context across threads.
Hopefully my mental model of OpenGL is somewhat accurate.