6
\$\begingroup\$

I have a dll that does some rendering using OpenGL. I need to render it to a texture and use that texture in direct x 11. My current attempt has been to render the OpenGL to a texture on one process, and then use Cuda to encode the texture to a h264 video stream, and then decode that stream on another process using cuda / directx 11. However, I'm wondering if there is an easier way to get a portion of a scene rendered OpenGL to DirectX11.

\$\endgroup\$
  • 4
    \$\begingroup\$ This will never be particularly elegant; does it have to be cross-process, or are you doing it all in the same process? If so you could just map the GL texture and use the mapped bits to create or update the D3D texture. I don't think you can do it keeping everything on the GPU. \$\endgroup\$ – user1430 Nov 18 '15 at 22:20
  • \$\begingroup\$ They can be on the same process. Basically, we are porting an application from an opengl based engine to a directx 11 based engine, but there are some opengl dll's that we cannot port (because they were made by a third party and we don't have the source). \$\endgroup\$ – default Nov 19 '15 at 14:27
3
+50
\$\begingroup\$

The ideal way to do this would involve OpenGL and D3D sharing the texture memory on the GPU. But there is unfortunately no practical way to do that (as far as I am aware).

Thus, you're going to have to shuffle the texture data around on the CPU. The process is fairly straightforward though. Once your render-to-texture has completed in OpenGL, map the texture to the CPU (via glGetTexImage or something fancier), which gives you access to the pixel data of the texture.

With that mapped information you can use CreateTexture2D and/or Map to create a D3D texture using the same pixel data.

If you're going to do this repeatedly you'll want to make sure you create both textures with the appropriate flags to suggest dynamic updates to the API.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Really the most elegant solution would be to replicate the functionality in Direct3d 11, but presumably this is unfeasable for you because of reasons™. You could also consider whether using the dll is more trouble than it's worth, there are no easy solutions here.

If you really, REALLY need this you could :

  • Look into memory-mapping like Josh Petrie suggested
  • Use the underlying DXGI layer which is the infrastructure Windows uses for drivers to the hardware. I don't know enough to present a complete solution but can at least point you in the right direction.

Either way will probably require some serious low-level hacking so think really long and hard if you want to take that plunge.

| improve this answer | |
\$\endgroup\$
-1
\$\begingroup\$

You might want to look at the DX_Interop OpenGL extensions. Since the extensions were contributed by NVIDIA, they might not be available on every card, but since you mentioned using CUDA, you should be covered.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Can you elaborate about the usage? Right now your answer is link-only. \$\endgroup\$ – Seth Battin Nov 23 '15 at 13:10
  • \$\begingroup\$ Thanks. I've looked into this. From what I understand, the extensions allow you to go from DirectX to OpenGL, but I need to go from OpenGL to DirectX. \$\endgroup\$ – default Nov 23 '15 at 15:27
  • 1
    \$\begingroup\$ Going from DirectX to OpenGL is equally good. Just create the texture in DirectX, use the extension to create handles in OpenGL to that texture, and then render to it in OpenGL. The accepted answer will always have the overhead of transferring the texture to host memory, and then back to device memory for rendering. One should first check if the DX_Interop extension is available and use it like this answer has proposed, and use the accepted answer only as a fallback. \$\endgroup\$ – Mattias F May 17 '18 at 8:52

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.