Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VTK OpenGL objects (3D texture) access from CUDA‏

Tags:

cuda

opengl

vtk

Is there any proper way to access the low level OpenGL objects of VTK in order to modify them from a CUDA/OpenCL kernel using the openGL-CUDA/OpenCL interoperability feature?

Specifically, I would want to get the GLuint (or unsigned int) member from vtkOpenGLGPUVolumeRayCastMapper that points to the Opengl 3D Texture object where the dataset is stored, in order to bind it to a CUDA Surface to be able to access and modify its values from a CUDA kernel implemented by me.

For further information, the process that I need to follow is explained here: http://rauwendaal.net/2011/12/02/writing-to-3d-opengl-textures-in-cuda-4-1-with-3d-surface-writes/ where the texID object used there (in Steps 1 and 2) is the equivalent to what I want to retrieve from VTK.

At a first look at the vtkOpenGLGPUVolumeRayCastMapper functions, I don't find an easy way to do this, rather than maybe creating a vtkGPUVolumeRayCastMapper subclass, but even in that case I am not sure what should I modify exactly, since I guess that some other members depend on the 3D Texture values, and should be also updated after modifying it.

So, do you know some way to do this?

Lots of thanks.

like image 314
Alex Avatar asked Nov 29 '25 13:11

Alex


1 Answers

Subclassing might work, but you could probably avoid it if you wanted. The important thing is that you get the order of the GL/CUDA API calls in the right order.

First, you have to register the texture with CUDA. This is done using:

cudaGraphicsGLRegisterImage(&cuda_graphics_resource, texture_handle,
GL_TEXTURE_3D, cudaGraphicsRegisterFlagsSurfaceLoadStore);

with the stipulation that texture_handle is a GLuint written to by a call to glGenTextures(...)

Once you have registered the texture with CUDA, you can create the surface which can be read or written to in your kernel.

The only thing you have to worry about from here is that vtk does not use the texture in between a call to cudaGraphicsMapResources(...) and cudaGraphicsUnmapResources(...). Everything else should just be standard CUDA.

Also once you map the texture to CUDA and write to it within a kernel, there is no additional work besides unmapping the texture. GL will get the modified texture the next time it is used.

like image 100
James Norton Avatar answered Dec 01 '25 10:12

James Norton