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?
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
Related
I'm trying to port some GDI/GDI+ code to Direct2D, but I'm still a little confused about which type of target is better to use (DC or Hwnd), because I found different performance depending on whether or not I used the Gpu. In particular, I found the following problems:
If I use the DCRenderTarget I can not use hardware acceleration (or Default), because I have continued violations of protected areas of memory. This does not happen if I use HwndRenderTarget.
If I use HwndRenderTarget, in general everything is fine, but if I have many window (like buttons), I lose the focus on the main window, which does not recognize the KeyPressed message, and, if I use the Gpu, performance fall a lot and strongly depend on the number of active targets (which does not happen if I use the software acceleration).
Has anyone encountered the same problems? Can you recommend something about it?
Thanks a lot!
In general, if you want Direct2D to interoperate with GDI, you should use ID2D1DCRenderTarget, otherwise, use ID2D1HwndRenderTarget.
I am not quite understood what you said about the performance, do you mean the performance was bad when your main window lost focus? If that's the case, you can handle the windows status to let it stop rendering when the window was lost focus, for example inactive.
I understand that Cocoa requires that windows be created/managed on the main thread. So, I'd like to have two or three windows with unique contexts, but I'd really prefer to draw to each of them from separate threads. Plus, a little bit of Google searching seems to indicate that rapidly context-switching on one thread is pretty expensive/slow.
You might want to look at the CGL interface for fast context switching, specifically: CGLSetCurrentContext. However, it may be more consistent to use the makeCurrentContext method for NSOpenGLContext in a Cocoa application.
I am reading Nehe OpenGL tutorial and there is a tutorial about Display Lists. Are there any reasons to use it instead of "classes and objects" way? For example creating class of cube and then just simply write cube.render(x, y, z); ?
The idea was to take advantage of hardware with on-board display-list processing. Basically, you'd build the display list in the memory of the graphics processor, then you could display the whole list by sending it a single command instead of re-sending all the coordinates and such every time you want to display the object. This can reduce the bandwidth requirement between the CPU and GPU substantially.
In reality, it depends heavily on the hardware and OpenGL implementation you're using though. With some implementations, a display list gets you major performance improvement -- but in others, it does essentially no good at all.
I am reading Nehe OpenGL tutorial and there is a tutorial about Display Lists.
I'd advise to stop reading Nehe. I've never seen anything decent related to Nehe to be mentioned on stackoverflow, and tutorials I saw use too much of platform-specific API.
Instead of NEHE, go to OpenGL.org and check "books" section. Alternatively, first version of "OpenGL red book" is available at glprogramming.com. While it doesn't cover most recent API present in OpenGL 4, all available methods are still available even in the most recent version of OpenGL via "compatibility profile".
Are there any reasons to use it instead of "classes and objects" way?
You're confusing two different concepts. Display list is OpenGL way to save sequence of OpenGL calls so you can "recall" them later quickly. Depending on OpenGL implementation, it might improve application performance. Usage of display lists has nothing to do with internal organization with your program (OOP). You can use OOP and still use display lists within cube.render(). Or you could use vertex buffer objects or whatever you want. Or you could work in "non-OOP" style and still use display lists.
Display lists are pre-compiled at GPU level.
Your own rendering functions are compiled at CPU level, whose individual commands still need to go through the GPU at run time.
This is like comparing "stored procedures" to custom functions calling inline SQL.
Stored procedures are compiled and execution plans are generated on the server side, while custom functions are only compiled in client side assemblies.
I've been trying to create a Cocoa project that uses OpenGL. NSOpenGLView is too restrictive for my needs, so I've created my own custom NSView subclass. I need to have multiple copies of this class on screen at once. And I need to use shaders.
What's the best way to organize this sort of project? I've tried a ton of different setups, but I always seem to wind up having invalid drawables, GL errors, trouble managing contexts, etc. Is there a simple way that I'm overlooking? Maybe setting up a single context at app launch and having all views share it?
Stay away from messing with OpenGL yourself...it sucks. Just say no.
I HIGHLY recommend using Cocos2D. Its a FANTASTIC graphics library that is highly supported and documented and handles all the OpenGL nonsense and makes the pain go away. Don't touch OpenGL unless you want to spend more time dinking with technical details than actually making your project happen.
The reference says this:
"You should call this function from the main thread of your application only."
But I would like to get the current graphics context from separate threads. What do you think? What can I do to reach this?
Edit:
Thanks guys for the answers. I don't know which is good for me at the moment, but thank you very much.
Never draw to the screen from anything other than the main thread!!! The graphics chip is single threaded, so you could cause all kinds of race conditions if you don't follow that rule.
You can draw to a background NSImage and then notify the main thread when you're finished and to update the screen.
If you want to draw from other threads, either create your own CGBitmapContext or use a CATiledLayer.
I would say restructure your logic to conform to the reference's advice.
Generally it is accurate about things you should and shouldn't do.
Why do you want to access it from different threads?