Use OpenGL Framebuffer as SDL texture

I have an OpenGL application that is rendering a video to a HMD. I now want to display an exact copy of the displayed video in an SDL window on a monitor. What I’m currently doing is very unefficient:
[ul]
[li]Create an SDL window and SDL texture with the same dimensions as the OpenGL viewport
[/li][li]For each video frame (after rendering to HMD):
[/li][LIST]
[li]Use glReadPixels to get the current framebuffer content
[/li][li]Lock the previously created texture and copy content to it, then unlock
[/li][li]Use SDL RenderCopy and RenderPresent to render the texture to the window
[/li][/ul]
[/LIST]
So basically I’m just duplicating the framebuffer for each video frame. I think this is unnecessary since the GPU already contains what I need. How can I use the OpenGL framebuffer in the SDL window directly without copying it?

Provided that both framebuffers are implemented by the same OpenGL driver and you can rely upon support for at least OpenGL 3.0, you can use glBlitFramebuffer() to blit (part of) the contents of one framebuffer to another.

If the two framebuffers are implemented by different video hardware, there probably isn’t going to be an efficient solution, although there are some things which can be done to avoid the implicit synchronisation which is likely with a naive approach.

[QUOTE=GClements;1293150]Provided that both framebuffers are implemented by the same OpenGL driver and you can rely upon support for at least OpenGL 3.0, you can use glBlitFramebuffer() to blit (part of) the contents of one framebuffer to another.

If the two framebuffers are implemented by different video hardware, there probably isn’t going to be an efficient solution, although there are some things which can be done to avoid the implicit synchronisation which is likely with a naive approach.[/QUOTE]

Thank you for your reply. Does this also work when the SDL window and the HMD renderer are using different OpenGL contexts?

Both framebuffers must be bound within the current context; glBlitFramebuffer() blits from the framebuffer bound to GL_READ_FRAMEBUFFER to the context bound to GL_DRAW_FRAMEBUFFER.