Hi,
I have project where I need to work with Qt, OpenGL and CUDA.
Basically, I need to create a CUDA library that will take a pointer to an image, process the image, give back a way to get the result. The library will be used to process the image, not for loading it or displaying it.
I am a beginner in CUDA and OpenGL so I’m kind of lost.
I was using this tutorial to create my CUDA library: OpenGL Interoperability with CUDA | 3D Game Engine Programming
Here some of my code:
__global__ void sobelfilter_kernel(int iw, int ih, unsigned char *source, unsigned char *dest)
{
// Calculate our pixel's location
int x = (blockIdx.x * blockDim.x) + threadIdx.x;
int y = (blockIdx.y * blockDim.y) + threadIdx.y;
// Operate only if we are in the correct boundaries
if(x > 0 && x < iw - 1 && y > 0 && y < ih - 1)
{
int gx = -source[iw*(y-1)+(x-1)] + source[iw*(y-1)+(x+1)] +
-2*source[iw*(y)+(x-1)] + 2*source[iw*(y)+(x+1)] +
-source[iw*(y+1)+(x-1)] + source[iw*(y+1)+(x+1)];
int gy = -source[iw*(y-1)+(x-1)] - 2*source[iw*(y-1)+(x)]
-source[iw*(y-1)+(x+1)] +
source[iw*(y+1)+(x-1)] + 2*source[iw*(y+1)+(x)] +
source[iw*(y+1)+(x+1)];
dest[iw*y+x] = (int) sqrt((float)(gx)*(float)(gx) + (float)(gy)*(float)(gy));
}
}
int processingExec(int opengl_buffer, int width, int height)
{
//Register the OpenGL Vertex Buffer with CUDA
struct cudaGraphicsResource * cuda_vbo_resource = 0;
cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, opengl_buffer, cudaGraphicsRegisterFlagsNone);
//Give access authority of the vbo to cuda
//This will effectively “lock” the resource to the CUDA resource object
cudaGraphicsMapResources(1, &cuda_vbo_resource, 0);
//Get a pointer to the device memory that can be used in the CUDA kernel
size_t num_bytes = 0;
unsigned char * dev_pointer = 0;
cudaGraphicsResourceGetMappedPointer((void**)dev_pointer, &num_bytes, cuda_vbo_resource);
//Kernel
sobelfilter(width, height, dev_pointer, dev_pointer);
//Unmap the result from CUDA in order to be able to use it in opengl
cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0);
}
The opengl_buffer is the ID of my QOpenGLTexture created in Qt.
I’m using the code of a sobel filter as the kernel process.
First, I am not sure if I can use the same pointer for the source and the result for the sobelfilter.
Second, I have no idea what am I supposed to do to get back the result from the process. How am I supposed to get the result from the library to use it in OpenGL (on the Qt application, not inside the library)?
Third, the Unmap function is creating the “freeze” of the application. There is nothing displayed and the application is transparent. If I comment the Unmap function, I get the image but there is no sobel filter applied on it so basically, I think I am not getting back the result.
Thank you for your help!
Morgane