3

I’m working on some code that has a grid view (~20 child views on screen at once). Each child view draws its content in GL, and has its own drawing thread and EAGLContext.

The advantage of this is that each view is relatively insulated from other GL usage in the app, though with 20 such views on screen, we have to glFlush+setCurrentContext: 20 times per frame. My gut tells me this is not the most efficient use of GL.

My questions:

  • What's the cost of switching contexts?
  • Does having to glFlush for each context actually slow it down, or does glFlush only stall the current context?
genpfault
  • 51,148
  • 11
  • 85
  • 139
David Cairns
  • 603
  • 5
  • 18
  • 2
    If each of those views has its own thread and context, then why do you have to set the current context at all? It's stored per-thread. – Andon M. Coleman Apr 26 '14 at 01:21
  • 1
    He doesn't have to make calls for switching contexts. But the GPU will have to switch contexts anyway when rendering from different contexts is submitted. – Reto Koradi Apr 26 '14 at 02:37

2 Answers2

0

• Does having to glFlush for each context actually slow it down, or does glFlush only stall the current context?

Contexts have their own individual command streams.

All of this stuff eventually has to be serialized for drawing on a single GPU, so flushing the command stream for 20 concurrent contexts is going to put some pressure on whatever part of the driver does that.

Luckily, GL does not guarantee any sort of synchronization between different contexts so GL itself is not going to spend a whole lot of effort making sure commands from different contexts are executed in a particular order relative to one another. However, if you were waiting on a fence sync. object associated with another context in one of the command streams then it would introduce some interesting GL-related overhead.

• What's the cost of switching contexts?

Why are you switching contexts?

You said that each view has its own thread and context, so I am having trouble understanding why you would ever change the context current to a thread.

Community
  • 1
  • 1
Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
0

The cost of switching contexts is very hardware dependent. Newer hardware generations tend to have more efficient context switching support. But it's generally a pretty heavy weight operation in any case.

The cost of a glFlush is neither very small nor very large. Not something you want to do more often than needed, but not very harmful when used in moderation. I would be much more worried about the context switch than the flush. As Andon mentioned in his response, a glFlush will not be enough if you need synchronization between the contexts/threads. That will require either a glFinish, or some kind of fence.

With your setup, you'll also have the price for thread switches on the CPU.

I think your gut feeling is absolutely right. The way I understand your use case, it would probably be much more efficient to render your sub-views in sequence, with a single rendering thread and context. It might make the state management a little more cumbersome, but you should be able to handle that fairly cleanly.

To make this answer more self contained, with credit to Andon: You don't have to make calls to set the current context, since the current context is maintained per thread. But on the GPU level, there will still be context switches as soon as work from multiple contexts is submitted.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • 1
    I only see now that the question has an "ios" tag. Since all IOS devices use TBDR (Tile Based Deferred Rendering) GPU architectures, the characteristics might be different, since the GPU does not really start rendering until all the triangles for a frame are submitted. I have only worked on drivers for non-TBDR architectures, so I don't know how that changes things. – Reto Koradi Apr 26 '14 at 03:03