Shared OpenGL context with the OpenGL context of the application UI
Is there a way to create a shared OpenGL context with the OpenGL context of the application UI?
Let's consider this situation:
- We have an application with QGraphicsView as the main widget.
- We run this application with the argument "-graphicssystem opengl".
- Viewport of the QGraphicsView instance is QWidget.
Inside application code I would like to:
- Detect whether application runs with OpenGL graphicssystem.
- If it does:
** Retrieve OpenGL context of the application.
** Create QGLWidget with shared OpenGL context with OpenGL context of the application.
First of all I have not tested this. I do not know if this is even a sensible thing to do. Having said that...
You should be able to get this by getting a pointer to the paint engine and trying to cast it to a QGL2PaintEngineEx pointer. If that works you are running with the opengl graphicssystem.
If the above is true, then you may be able to get the GL context by getting a pointer to the top-level widget's window surface (use QWidget::windowSurface()), and trying to cast that to a QGLWindowSurface pointer. If that works you can then call the QGLWindowSurface::context() function to get at the context.
This uses some non-public API but it does exist. So be warned that it may change in the future. Once you have the context you can then pass it into the constructor of QGlWidget.
Good luck with whatever you are doing and I hope it doesn't blow up ;-)
This is exactly what I was a afraid of :) There should be some way to do this with public APIs because it would help to combine Qt UI and OpenGL with minimal performance hit in a clean way. This would be useful for games and other more graphics intensive applications especially when working with mobile and embedded devices.
I suggest you make a feature request for that on "Jira":bugreports.qt.nokia.com (check if it isn't there yet first, please). Better yet, make a patch and a merge request :-)
Have you tried it? Did it work? If so the MR should be fairly simple as the API looks to be there. Could probably just do with some syntactic sugar on it though. Please let us know how you get on.
[quote author="ZapB" date="1300281117"]Have you tried it? Did it work? If so the MR should be fairly simple as the API looks to be there. Could probably just do with some syntactic sugar on it though. Please let us know how you get on.[/quote]
I think I'll make a "proof-of-concept" of this. I'll let you know the results.
I'm working on QtWebKit WebGL support. There I have to use glReadPixels() instead of drawTexture() if context for WebGL is not shared with the GL context of the viewport. It works only when QGraphicsView has QGLWidget viewport.
This solution would require to use private APIs of the master Qt tree and it is not acceptable for QtWebKit. As far as I understand it there should not be source level dependencies between Qt tree and WebKit tree.
[quote author="Andre" date="1300280904"]I suggest you make a feature request for that on "Jira":bugreports.qt.nokia.com (check if it isn't there yet first, please). Better yet, make a patch and a merge request :-)[/quote]
I'll probably make that proof-of-concept patch for QtWebKit that uses the "private API" approach and put it part of the bug report. For internal change, I would probably create a patch but this needs more consideration since it needs additions to the API. Therefore patch that demonstrates the use case is better.
Sounds like an excellent plan. Let us know how it works out please!