2
\$\begingroup\$

I'm working on a UI system that does it's processing on a separate thread. However, the results of this processing must end up on the GPU eventually.

A simplified overview of the system boils down to this step happening in the 'rendering phase':

  1. Start at root widget (a widget is an object that controls what is rendered to the screen, and most importantly defines the hierarchical relation between controls).
  2. Render widget to screen.
  3. Iterate over all widget's children.
  4. For each child go back to step 2.

When a widget is determined to be static (render output not changing over a period of time) the system must be able to choose to cache it in a texture in GPU memory (RenderTarget2D).

There are some other things that can be cached such as VertexBuffers and IndexBuffers.

Because the UI system does all its compution on a separate thead (to avoid slowing down the other game processes) I'm concerned about what methods I can call on anything other than the game's rendering thread.

Concrete questions:

  • What communication with the graphics device can be done from a separate thread?
  • If the above depends on the card or drivers, how can I determine whether it supports those actions?
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Very little.

MonoGame doesn't provide any explicit thread-safely guarantees about GraphicsDevice that I can see, and as such you should assume it is not thread-safe in any way.

For the most part, if you access the device from a single thread at a time (using your own synchronization primitives) you'll be "safe-ish" (although you may run into problems where running a different platforms gives you wildly different results due to different graphics APIs), but there are a handful of threading-related bugs in MonoGame's code (for example, while glancing through the code for this question I saw several instances where events are raised without caching local copies, which creates a potential threading bug).

You're better off adopting a model where you can do all your work in your background threads using your own data, and then setting a flag or putting the results of that work into a queue that the render thread consumes to do the actual graphics device work necessary.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .