oaheix
oaheix

Reputation: 13

Interactions between Onscreen and Offscreen rendering in Qt5 with QOpenGL\* classes

Objective:

To make some onscreen and offscreen rendering via Qt5 OpenGL framework, such that the resources can be easily shared between both rendering parts. Specifically,

Current solution:

Since I don't know how to share resources between both parts, the actual rendering work are done redundantly in both parts in my current solution:

The onscreen part: The offscreen part: The actual renderer:

Questions:

There are two QOpenGLContexts:

So my questions are:

A brief actual code example doing a simple rendering work (say, draw a triangle via shading language) will be much appreciated.

Upvotes: 1

Views: 1008

Answers (1)

Velkan
Velkan

Reputation: 7592

Assuming that QOpenGLContext *main_ctx is the context that was created by QOpenGLWidget for actual rendering, you can create another context ctx in any thread and make it share textures and buffers with the first one:

ctx = std::make_unique<QOpenGLContext>();
ctx->setFormat(main_ctx->format());
ctx->setShareContext(main_ctx);
ctx->create();

I don't think that QOffscreenSurface must be a QWindow-based.

offscreen_surface = std::make_unique<QOffscreenSurface>();
offscreen_surface->setFormat(ctx->format());
offscreen_surface->create();
ctx->makeCurrent(offscreen_surface);

Then create a QOpenGLFramebufferObject and render into it from the second context (second thread).

Then use its texture in the main context: glBindTexture(GL_TEXTURE_2D, fbo->texture());. Maybe there is a need for some synchronization when doing this.

Upvotes: 1

Related Questions