I am sharing data between OpenGL and CUDA as follows:
GLuint buffer;
glGenBuffers(1, &buffer);
// Some image is bound to this texture buffer at some point.
...
cudaGraphicsResource_t cgr;
checkCudaErrors(cudaGraphicsGLRegisterBuffer(&cgr,
buffer, cudaGraphicsRegisterFlagsNone));
checkCudaErrors(cudaGraphicsMapResources(1, &cgr, 0));
uchar4 * device_ptr = 0;
size_t num_bytes;
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
(void **)&device_ptr, &num_bytes, cgr));
This works fine and device_ptr is not a pointer to my CUDA memory. Now, at some point I want to resample this image using bilinear interpolation. It seems that the preferred way to do this in CUDA is map the device data to CUDA texture memory and then perform the interpolation using the tex2D calls.
Now, my question is that the imaging data already exists in OpenGL texture memory and I wonder if there is a way to avoid calling cudaBindTexture2D again and somehow use the OpenGL texture from within CUDA for this interpolation?
cudaGraphicsResourceGetMappedPointer, you should be able to treat it like a pointer to an array on device memory and launch a kernel to perform the interpolation. Once the interpolation is done, all you have to do is to callcudaGraphicsUnmapResources(1, &cgr, 0)for it to be displayed by OpenGL. - mty