cudaGraphicsGLRegisterImage parameters

May 10, 2013 at 9:35 PM
Edited May 10, 2013 at 9:36 PM
I'm working on getting OpenGL (OpenTK)/Cudafy interop working. I've hit a stumbling block, in:
        public extern static cudaError cudaGraphicsGLRegisterImage(cudaGraphicsResource[] resource, uint image, uint target, uint Flags);
This method is (I think) supposed to populate the resource parameter with handle(s) to the mapped objects. However, I'm unclear how this extern declaration would allow that. Arrays are immutable! I'd expect a ref or an IntPtr argument here, not an array.

Any suggestions? Is this one of those things that was magically generated from the CUDA headers, or has anyone actually made this method work? I don't receive an error when calling this, but I also don't get anything back that's useful (since I need the resource handles later on).

I'm aware of the array/pointer quasi-equivalence in C, but I'm not sure how to take advantage of this in C#. The only thing I've thought of is sending a pinned array, and hoping that I'll get something valuable back, but I'm not entirely sure how to do that.

(I'd LOVE an example of using this method in any functional app. Bonus points for anything that writes to a GL texture)
May 10, 2013 at 9:42 PM
Update: I added my own extern declaration and changed the signature to:

    public extern static cudaError cudaGraphicsGLRegisterImage(ref cudaGraphicsResource resource, uint image, uint target, uint Flags);

And I get what looks like a valid handle back. I can submit a patch for this, but I'm unaware of the procedure involved, or what method is in place to generate the C# CUDA wrapper. Link/advice?
May 11, 2013 at 9:39 AM
Hi David,
You can email patches and usage instructions directly to me either via codeplex or If you can share what you are doing with OpenGL that would be brilliant since we do not have experience of graphics and this has been a long time request.
May 11, 2013 at 5:40 PM
Will do. I'm still working through the process, but at this point it's just a matter of time. I'll post an OpenTK + Cudafy example when it is ready.

I'm surprised more people aren't writing shaders in CUDA or OpenCL. The amount of control you get is awesome. Also, being able to write shaders in C#, and them on the CPU or GPU from a single code base is impressive.

I've only been using Cudafy for a few days, but so far it has been great.

Sent from my iPhone

May 15, 2013 at 2:57 PM
I now have Cudafy and OpenTK playing nicely together, with CUDA generating and continuously updating a height map texture (single-channel floating point) that is consumed by a GLSL shader and used to vertically offset vertices.

I'll have to rip out my game engine before it will make a good tutorial/example.

In the mean time, anyone that is facing similar issues can post here or email me at david Lively (no spaces) at gmail.

May 15, 2013 at 6:48 PM
What you did is pretty cool. Congratulations!
May 16, 2013 at 11:09 AM
This is indeed really neat and could meet the requirements of a number of CUDAfy users. If you want to then you can also email sample project.
May 16, 2013 at 10:25 PM
I managed to work around the float4 and var name issues, and have a sample project working with the 1.21 beta and latest OpenTK. Who wants it?

Sent from my iPhone

May 17, 2013 at 5:30 AM
Can you email me this please? Thanks.
May 17, 2013 at 12:51 PM

Sent from my iPhone

May 17, 2013 at 1:09 PM
Edited May 17, 2013 at 1:09 PM
Email address can be found here:
May 17, 2013 at 6:15 PM

Sent from my iPhone

Jun 19, 2013 at 10:15 AM
Very exciting!

I have a bit of background doing image processing using hlsl shaders in xna. Works okay, but the level of control you get with cudafy is just mindblowing!

Im surprised more people arent using cudafy for image processing, it's quite awesome what you can do. I just hope future developments will include Texture2D functionality :-)

If you could please send me your sample project as well, I would be eternally grateful!

Best regards, Ulf
Jun 19, 2013 at 10:17 AM
Oh, and my email adress is

Jun 20, 2013 at 1:32 AM

Sent from my iPhone

Jun 20, 2013 at 4:56 AM
Ulf and David, if you have ideas of how textures could be incorporated in cudafy then please share them. It has been requested on and off over the last two years so likely to be of interest to many more people. We're also looking to add the vectors like float4, etc - only hitch being that the preferably are applicable to both CUDA and OpenCL.
Jun 20, 2013 at 2:17 PM
Hi Nick,

Sure. Do you mean generating textures in cuda, modifying an existing GL texture in a kernel, or both? I should be able to modify the existing GL interop code.

Ive has some success getting the vector types to work. It is a bit of a hack, but workable. Someone more familiar with the cudafy source may be able to integrate it, or we could consider adding a new class library, like "CUDAfy.GL," to keep the graphics stuff isolated, and make it easier to provide CUDA and OpenCL versions with a common interface. The big stumbling block is the Cuda runtime dependencies.

I've been trying to finish the write up on the first interop example, but work has been crazy. It's typically frowned upon to work on side projects while one's employees are watching. (I have a small team in Cambridge UK that are visiting my office in Dallas, TX this week.)

Sent from my iPhone

Jun 20, 2013 at 2:45 PM
I fear I'm not at an expertise level in interop stuff to be of much assistance to add functionality to cudafy.

I would however be happy to help with code samples or tutorials that show image processing techniques like convolution filtering, color space transforms/adjustments or such if texture functionality is added to cudafy.

At this point I have been succesful with creating cuda functionality using 2d arrays of rgba pixel structs and creating helper methods for bilinear sampling to mimic the way tex2d works. I figure this introduces a performance degrade though, compared to the "silicon based" texture sampling the built in tex2d class would bring.

Also, creating my own structs with attached helper functions only works in cuda, not opencl

Keep up the good work!
Jun 21, 2013 at 8:02 AM
Many of the vectors can be used in OpenCL and CUDA, I think only ones like float3 were an issue in that OpenCL does the padding. Another user looked into this and I still need to review and add.
I can add both of you to our internal project website and SVN so we can share material for tutorials and graphics support in general. Even rough drafts are not a problem since we can review. Best is usually just to get started and tweak later.
I've got your email addresses so if you give go ahead I will add you.
Jun 21, 2013 at 3:07 PM
Works for me!

Jan 9, 2015 at 11:32 PM
Since I've received a few requests for this one:

Codeplex is running pretty slow for me at the moment, so GitHub it is.