Agents have their own thread pool, not a single thread, and we have no
easy way of registering an OpenGL context on any of them. Since we
care a lot about what thread events occur on, events.clj is how it is
for a reason. The queueing mechanism should probably just use
java.util.concurrent (I wasn't very familiar with it when I first
wrote queue.clj), but agents don't gain us much here either. Since we
can't use the agent's actual state for the reasons mentioned above,
we'd just be using agents as a thread pool, and a thread pool doesn't
give us the strict timing semantics we want.
There was originally a mechanism to do the sort of background loading
you mentioned, but since I couldn't figure out exactly what you could
and couldn't do on the background thread I took it out. However, if
you just want to create a sub-thread that shares the OpenGL context,
it's pretty easy:
(penumbra.app.core/with-app app
(penumbra.app/with-gl
*load robots and stuff*
(penumbra.app/update! *update state to have robots*)))
I can't guarantee this thread won't interfere with the main one, but
if you're creating new things and only then telling the main
application about them, I think you should be fine.
By the way, what obstacles have you encountered due to the lack of
documentation? I'd appreciate some guidance on what most needs to be
better documented.
Zach
Maybe I'm not understanding what you want, but app/update! or
app/enqueue! are kind of analogous to agents, in that they let you
trigger an update to the application state.
>
> Still, it is somehow unsatisfying that the nice concurrency model of
> Clojure is being limited defeated by OpenGL's single-threaded design.
> I will play around with using multiple threads in the same context and
> see what I learn. Maybe having a small threadpool of agents with the
> opengl context might be useful for a limited set of background opengl
> tasks?
Again, I haven't determined exactly what is and isn't allowed in these
secondary threads. A better approach, I think, is to just expose the
asynchronous loading of data, such that everything is invoked on the
main thread but doesn't block it.
>
> As for documentation, I can only speak for the difficulties I
> encountered myself -- they may or may not be typical.
>
> 1. Top priority should be to make sure all your functions have better
> docstrings...and if possible, an sample use case, and where they
> should probably be called. There are still several functions that lack
> docstrings at present, and others that need to be more verbose.
> 2. I wasn't always sure what your examples were actually doing. Just
> as an example: async.clj. Exactly what is happening asynchronously in
> this example? Even a one-line description added to the namespace
> metadata at the top of each file would help me understand if I should
> spend time reading the source of the example or not.
> 3. I ran into the issue described in [https://github.com/ztellman/
> penumbra/issues#issue/18] and had to change my xorg.conf, and was
> quite puzzled for several hours. Why didn't app/display-mode! work?
> Why does app/display-modes only return one mode?
> 4. Also on my documentation wish list for Christmas; a style guide for
> using the most important functions of penumbra for my own evil
> purposes: event/publish!, controller/wait!, app/enqueue!, error
> handling, etc.
> 5. Not documentation-related, but I also want Penumbra in a JPanel!
I'll do what I can about these. I've been extremely distracted by
work and other projects, and Penumbra's been kind of neglected. I'd
like to at least get things to a stable, well-documented state so that
I don't feel so bad about leaving it alone.
Zach
Like I said before, using other threads in OpenGL is not a
deterministic experience. I've had wild variations in behavior across
computers, which is why I took out the easy way to use other threads.
There is an asynchronous way to load threads (i.e. enqueue the
loading, and poll to see when it's complete), but it's not currently
exposed.
My suggestion is that any time you touch the textures, use
(app/enqueue! ...). If this makes the rendering unacceptably choppy
we can look for an alternative, but that's the guaranteed safe
approach.
Zach