Skip to main content
GameDev.net gamedev.net
🔒 Locked

Is using OpenCL with DirectX possible?

Started by 67rtyus Jun 18, 2011 at 6:52 PM 2 replies 5.2k views
Original Post
67rtyus
67rtyus
Hi;

I am working on a project with DirectX 9. I am rendering a huge terrain and implemented a special shadowing algorithm for this structure. The algorithm is similar to a classical ray-tracing application, so it can benefit highly from parallelization, since each ray can be processed independently. I already wrote the single threaded version of the algorithm, which runs in 500-600 ms. My goal is to optimize the algorithm such that it can be executed in real time; meaning shadows can be recalculated if the sun's direction changes constantly in every frame, with an acceptable speed loss. Parallelization of the code was the first way which came to my mind. I especially want to use GPU for this purpose.

I don't want to use CUDA since I cannot limit my project to NVidia cards. I am using DirectX 9 and therefore DirectCompute is out of question. So the most reasonable method seems to be using OpenCL. I did not use OpenCL before and I am going to learn it from scratch for this project. But I am not sure whether OpenCL and DirectX 9 can be used in the same time: This is an actual 3D graphics project and it constantly uses Graphics hardware via the Direct3D 9 runtime. OpenCL will also make use of the graphics hardware. In every frame, before rendering the scene with Direct3D, I want to run my shadow algorithm using OpenCL. My question is; are they compatible with each other? For example, will running OpenCL in a Direct3D project mess up any data structure associated with Direct3D, like vertex buffers, textures, etc.? Are these two able to communicate with each other or do they have the means to safely run together? It seems that there aren't any meaningful documents in the internet on this specific issue, so I am asking for help here.

Thanks in advance.

MJP
MJP
It seems a little weird to me that you want to use DirectX9 to do something that will only work on DX10+ GPU's.

Anyway, I believe Nvidia and AMD have OpenCL interop examples in their respective SDK's. I don't know very much about OpenCL so I couldn't tell you how easy it is, or how much performance impact it has.
SimonForsman
SimonForsman

It seems a little weird to me that you want to use DirectX9 to do something that will only work on DX10+ GPU's.

Anyway, I believe Nvidia and AMD have OpenCL interop examples in their respective SDK's. I don't know very much about OpenCL so I couldn't tell you how easy it is, or how much performance impact it has.


I think the main advantage would be that by using DX9 he can:

1) Still run the software on XP (Some XP machines have DX10+ GPUs allthough it is fairly rare)
2) Don't have to bother updating his renderer.

Anyway, you can definitly use OpenCL at the same time as DIrectX9 without any problems (Just as you can run two games at the same time), the problems only occur if you want to integrate the APIs. (For example have DirectX access a texture you've generated with OpenCL, if you're using OpenGL this isn't a problem as the integration between OpenCL and OpenGL is very smooth), For D3D9 you got cl_nv_d3d9_sharing which is a nvidia extension that allows OpenCL to access shared D3D9 surfaces, i don't know how well it works or what cards support it though.

If you can't integrate the two APIs directly you can always read the data back to the software and re-upload it to the GPU using the other API allthough this obviously lowers performance quite a bit.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
67rtyus
67rtyus
Actually, the project already started with using DirectX 9 and I can't update it to a new renderer now.

In my case, the APIs don't need to directly communicate with each other, with a single exception. In the current version of my algorithm, I am locking a Direct3D floating point texture before the algorithm starts and the algorithm writes the results into the locked texture as it proceeds. After the algorithm finishes I unlock the texture and it will be used by Direct3D in the following frames. Now, it will be OpenCL's job to write into this texture. But I don't think that this will be a problem, since Direct3D does nothing as the algorithm is running and writing its results into the texture. It must wait for the algorithm to finish its job and unlock the surface. (I won't call Render method before the algorithm finishes.) In OpenCL, I will block the main program and make it wait for the OpenCL to complete its job. In this case, I don't think it would make a difference who is writing into the locked texture, the main program or OpenCL from Direct3D's point of view. I will just pass the pointer to the locked surface to OpenCL somehow and then wait for it to complete.

I am still curious about how do DirectX and OpenCL manage to work together without problems; for example when OpenCL allocates a surface on the video ram, it must be a memory area which is not occupied by DirectX already. Since both APIs communicates with the graphics hardware via the driver, I think the driver manages this resource management but I am not sure.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.