2

Normally, I would just use NSOpenGLContext, but in this case I have to have a CGContextRef as the OpenGL context in order to support some other frameworks I am using. My question is: can this be done?

I cannot afford to use glReadPixels() to fill the context because it is way to slow for this. I have to render on the entire screen (1440 x 900) at least 32 times per second, so the performance is a very big concern for me. Is there a way to make OpenGL draw into the CGContextRef efficiently, or do I need some kind of workaround?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Justin
  • 2,122
  • 3
  • 27
  • 47
  • "I have to have a CGContextRef" - why? –  Oct 10 '12 at 15:15
  • For a number of reasons. The biggest reason is compatibility with another framework I am using. – Justin Oct 10 '12 at 15:16
  • "*I now it can be done.*" Assuming you meant "I *know*", what makes you think that? It seems rather unlikely that Apple would allow you to subvert Quartz2D with random OpenGL calls. – Nicol Bolas Oct 19 '12 at 22:13
  • Yes, I did mean _know_. I don't know if it was a typo or just me having another brain fart. I _know_ it is possible because I have seen other questions, many of which are on this site, that mention using a `CGContextRef` as an OpenGL context. Besides, that is what `CGContextRef` is for: Apple's drawing commands, which boil down to OpenGL drawing commands. – Justin Oct 19 '12 at 22:27
  • @Justin: Could you give an example of such a question? I did a search on it, and I couldn't find one; all of the ones I looked at used a bitmap CGContextRef. Also, even if Apple's drawing commands use OpenGL directly (instead of more likely using what Apple's OpenGL commands use), Apple probably isn't going to let you mess with their OpenGL context directly. If they did, you could change GL state such that Quartz2D doesn't work anymore for that window. At least, not without them verifying that the state they expect to be set is set. Also, you could get their objects and play with them. – Nicol Bolas Oct 21 '12 at 07:17

1 Answers1

0

Since it seems 1 functionality will cause issues with using the GLContext and using glReadPixels() instead of a CGContext. Why not make multiple contexts?

Read here about the shared context, dedicate a Context to a background thread or operation queue and read your heavy operation into that and access it via the shared context to render it on the main thread context.

If you still must use a CGContextRef, can you tell us the exact frameworks that you are using that require it? People on here may be able to offer another solution

CStreel
  • 2,642
  • 2
  • 19
  • 37
  • Could you please just tell me how to use a CGContextRef? I use QuartzCore, CoreGraphics, Cocoa, and a framework I made myself for some special effects. They operate faster than OpenGL in some cases because the fragment shaders that do the same things slow down when I zoom in on the targeted objects or scenes. If you *still* think I shouldn't use CGContextRef, maybe I will just have to settle for something else and use some more complex to keep the performance nice. – Justin Oct 21 '12 at 22:20
  • Can you tell us what framework that you said is required to be used that needs a CGContextRef? – CStreel Oct 22 '12 at 00:02