Search code examples
openglcudatexturesinterop

How to retrieve the raw data from opengl texture mapped in CUDA?


I'm trying to render a scene in OpenGL and map the color and depth texture data to a contiguous CUDA device_ptr, similar to glReadPixels but on GPU memory. Is it possible?

The data type of color and depth textures are GL_UNSIGNED_BYTE and GL_UNSIGNED_INT, respectively.

I don't have much experience with CUDA, so I only found some examples mapping an OpenGL 2D texture to another CUDA texture, and I would like to just have the reference to the mapped raw data.

I tried to map the texture2D with cudaGraphicsGLRegisterImage and cudaGraphicsSubResourceGetMappedArray, but since it returns a cudaArray, I don't know if I can transform this pointer and use it as a device_ptr. Also, it crashes when I try to register the depth texture, which I think it might not be supported, but I would like to try some workaround if there is any.


Solution

  • So, just to give an update about what i ended up doing. I ended up just working with the color data, initially discarding depth information.

    I used the method cudaMemcpyFromArrayAsync to copy the cudaArray to a new device vector. It is a deprecated method on CUDA 11.6, but it worked and it was relatively fast since it was a device to device copy (cudaMemcpyDeviceToDevice).