Nominate our 2022 Qt Champions!

Problems with Qt/OpenGL linked with CUDA library

  • Hi,

    I have a project where I have to create a library in CUDA and link it into my Qt/OpenGL application.
    Basically, I'm

    • loading in Qt/OpenGL an image
    • give it to the library (in a way, right now I'm giving the textureId() of my QOpenGLWidget)
    • apply a sobel filter in my CUDA code
    • return the result (in a way, I do not know what and how to link it in OpenGL)
    • display it in Qt/OpenGL

    A lot of my Qt/OpenGL code is based on the cube example:
    I am getting a lot of problems when I'm trying to link it.

    How am I supposed to link the result in Qt/OpenGL ?

    I'm getting a freezed windows or the original image (when I remove the "Unmap function from my CUDA code") but I never got my image with the sobel filter on it.

    Thank you for your help,


    Note: I'm not familiar at all with CUDA and OpenGL so sorry if it is silly questions....

  • Lifetime Qt Champion


    Can you share the code you are using ? Also which version of Qt/CUDA/OpenGL are you using ?

  • Hi,

    I'm using Qt 5.4.0, OpenGL ES 2.0 and CUDA 6.5.
    Which part do you want exactly ? I have a lot of different version as I am having some problems with it but all my Qt/OpenGL code is the same than the Cube example (just using a square instead of a cube, it's the only modification I have done).

    At the moment, I'm trying to get a pointer of the CUDA result but I have no idea how to use it in my Qt/OpenGL code and if it is the good way of doing it.

    Here the part where I am calling the CUDA library:
    @ unsigned char * resultPointer = CUDA_library_processing(m_texture->textureId(), width, height);
    where m_texture is my QOpenGLTexture.

    Here the CUDA part:

    unsigned char* CUDA_library_processing(int opengl_buffer, int width, int height)
    struct cudaGraphicsResource * cuda_vbo_resource = 0;
    cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, opengl_buffer, cudaGraphicsRegisterFlagsNone);

    cudaGraphicsMapResources(1, &cuda_vbo_resource, 0);

    size_t num_bytes = 0;
    unsigned char * dev_pointer = 0;
    cudaGraphicsResourceGetMappedPointer((void**)dev_pointer, &num_bytes, cuda_vbo_resource);

    unsigned char * result_pointer = 0;
    cudaMalloc((void **) &result_pointer, (num_bytes));

    unsigned char * respointer = (unsigned char *) malloc(num_bytes);

    sobelfilter(width, height, dev_pointer, result_pointer);


    cudaMemcpy((void**)respointer, (void**)result_pointer, num_bytes, cudaMemcpyDeviceToHost);

    cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0));


    return respointer;

  • Lifetime Qt Champion

    Is it a typo or are you missing a & before dev_point in cudaGraphicsResourceGetMappedPointer ?

    You seem to be also mis-typecasting respointer and result_pointer in cudaMemcpy

  • The CUDA section in the article may give an idea how this could be achieved.

Log in to reply