LWJGL, Clojure, Single Thread for OpenGL Commands

Short question:

Given the Clojure concurrency model, how can I guarantee that all LWJGL OpenGL Update functions are called from the same thread?

Long question

After a long and glorious battle Using lwjgl in Leiningen / Clojure , I now have LWJGL + Leiningen + Clojure, working together.

However, according to the LWJGL documentation, it seems that OpenGL client commands have a local thread state - and if I call OpenGL commands from different threads, Bad Things (TM) can happen.

So my question is: what is the correct way, inside Clojure, to set up something like Swing Threads (but I can't use Swing Threads / should set up my own) so that:

(1) there is one main thread responsible for all OpenGL calls

(2) other threads do some communication with this main thread to coordinate OpenGL calls

My background

I am familiar with threads in C / C ++. I am familiar with the Clojure agent / atom / ref model. However, I am not familiar with the "manual" concurrency / threading in Clojure.

Thanks!

+6
source share
1 answer

The usual way I would design this would be to have a single thread responsible for the entire rendering. By doing this, you avoid the need for other threads to "make OpenGL calls."

Of course, you still need a way to get information into the rendering stream, but you can use this using the standard Clojure concurrency methods. For example, if your world state is unchanged, you can simply use the atom to update the state and have a rendering stream only to render using the last world state stored in the atom:

(def world-state (atom (initial-world-state))) ;; in rendering thread ..... (loop [] (render-world @world-state) ; all OpenGL calls happen in this function!! (other-stuff) (if (still-running) (recur))) ;; in other thread responsible for updating world (loop [] (swap! world-state update-world-function) (other-stuff) (sleep-if-needed) (if (still-running) (recur))) 
+4
source

Source: https://habr.com/ru/post/915547/


All Articles