2
\$\begingroup\$

I have main render loop in which initial drawing is done via OpenGL to an SDL window, and after that the same window handle is passed to a Direct3D device, which does subsequent rendering.

Once I execute the program it will initially draw the OpenGL scene, and then do Direct3D drawing but that latter drawing overwrites the OpenGL work.

What I want to see is both drawings in parallel. How can I accomplish that?

\$\endgroup\$
  • \$\begingroup\$ by overwrites do you mean that it clears the window before drawing? Do you ever see the openGL drawing on the screen? \$\endgroup\$ – Will Jun 25 '13 at 11:32
  • \$\begingroup\$ Dismissing whether this is possible or useful, I just have one question: Why? \$\endgroup\$ – Jari Komppa Jul 1 '15 at 7:59
7
\$\begingroup\$

You cannot do this directly. Both OpenGL and Direct3D operate under the assumption that they are the only things writing to the window.

Furthermore, most of their actual rendering is done in in-memory back buffers and not directly onto the presentation window. It's only once all rendering commands are complete for a particular frame that the back buffer contents is transferred, wholesale, to the front buffer (the presentation window). This transfer is done without regard to what currently exists in that front buffer, and so D3D and GL are totally ignorant of the render results of the other.

There are two ways to achieve what you want, and you can choose between them based on whether or not you want the respective scenes to ever overlap.

In the simple case, where you want only the D3D stuff on the left half of the screen and the OpenGL stuff on the right (for example), you can simply adjust the viewport of both APIs accordingly (using glViewport in OpenGL or RSSetViewports in D3D).

However, if you want the scenes to potentially overlap (as it sort of sounds like you do), you have to instead render your scenes to textures in both APIs (potentially outdated OpenGL example, D3D example). Then you'll need to recover the bits of those textures manually and composite them into a final scene.

You can do this composition and final presentation via a third graphics API (likely GDI), if you wish, but that would probably be unpleasantly inefficient. Instead, I would suggest you do the final composition with D3D or GL itself again. You can pick either, but for the sake of demonstration I'll assume you prefer GL.

That means you will, every frame:

  • perform your OpenGL rendering into a texture
  • perform your D3D rendering into a different texture
  • copy the contents of the D3D texture into CPU memory and from there into GPU memory of a second OpenGL texture.
  • render a fullscreen quad in OpenGL using both your OpenGL textures and a shader or with appropriate blend mode settings enabled to handle composition however you'd like
\$\endgroup\$
5
\$\begingroup\$

You can also use the OpenGL Extensions WGL_NV_DX_interop and WGL_NV_DX_interop2

The first one is well supported by Nvidia and AMD and even Intel chips but works only with D3D9 objects. If you want to use DXGI (DirectX 10 and 11) you need to use the second one which only works with Nvidia and some AMD GPUs.

By using these extensions you can render to a surface or texture as Josh suggested, but you don't need to perform the expensive GPU -> CPU -> GPU copies that would slow down your program substantially.

\$\endgroup\$
-2
\$\begingroup\$

I think I will use ANGLE to convert opengl calls to DirectX. The transferring of framebuffer to the RAM and then construct a backbuffer in DirectX from it, is time-consuming involving DMA and releasing resource from Opengl to hand over the gpu to DirectX. I am trying to use ANGLE and will update my answer when I know exactly how

\$\endgroup\$
  • \$\begingroup\$ This doesn't yet appear to present a completer answer to the question. I'd recommend waiting until you've sorted out the details before posting an answer (after all, this question has lasted 4 years, it will wait for you). If you need help sorting this out, feel free to post a new question instead. \$\endgroup\$ – DMGregory Nov 20 '17 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.