Results 1 to 8 of 8

Thread: QGLWidget + OpenGL + CUDA

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QGLWidget + OpenGL + CUDA

    Well... after nearly three weeks of unsuccessful work on this I decided to create a topic here...
    Now, one day later I found my "mistake"... you have to use glTexSubImage2D() instead of glTexImage2D() wit same parameters... even if you want to read the whole image.

    I don't get it... but here's some working code, for anybody, that comes across this thread (this time in CODE-tags instead of QUOTE-tags ):

    Qt Code:
    1. void GLWidget::initializeGL()
    2. {
    3. makeCurrent();
    4.  
    5. cudaGLSetGLDevice(cutGetMaxGflopsDeviceId() );
    6.  
    7. int c=1;
    8. char* dummy = "";
    9. glutInit( &c, &dummy );
    10.  
    11. glewInit();
    12.  
    13. unsigned int w, h;
    14. char data[1024];
    15. strncpy(data, getImagePath().toLatin1(), sizeof(data) - 1);
    16. const char* image_filename = data;
    17.  
    18. if (cutLoadPPM4ub(image_filename, &pixels, &w, &h) != CUTTrue) {
    19. printf("Failed to load image file: %s\n", image_filename);
    20. exit(-1);
    21. }
    22.  
    23.  
    24. GLint bsize;
    25. setupTexture(m_pQImage.width(), m_pQImage.height(), pixels, 4);
    26.  
    27. memset(pixels, 0x0, 4 * sizeof(Pixel) * m_pQImage.width() * m_pQImage.height());
    28.  
    29. // use OpenGL Path
    30. glGenBuffers(1, &pbo_buffer);
    31. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer);
    32. glBufferData(GL_PIXEL_UNPACK_BUFFER,
    33. 4 * sizeof(Pixel) * m_pQImage.width() * m_pQImage.height(),
    34. pixels, GL_STREAM_DRAW);
    35.  
    36. glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER, GL_BUFFER_SIZE, &bsize);
    37. if ((GLuint)bsize != (4 * sizeof(Pixel) * m_pQImage.width() * m_pQImage.height()))
    38. {
    39. printf("Buffer object (%d) has incorrect size (%d).\n", (unsigned)pbo_buffer, (unsigned)bsize);
    40. cudaThreadExit();
    41. exit(-1);
    42. }
    43.  
    44. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
    45.  
    46. // register this buffer object with CUDA
    47. cutilSafeCall(cudaGraphicsGLRegisterBuffer(&cuda_pbo_resource, pbo_buffer, cudaGraphicsMapFlagsWriteDiscard));
    48.  
    49.  
    50. glGenTextures (1, &m_pTexture);
    51. glBindTexture (GL_TEXTURE_2D, m_pTexture);
    52. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, m_pQImage.width(), m_pQImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    53. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    54. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    55. glBindTexture (GL_TEXTURE_2D, 0);
    56.  
    57. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    58. glPixelStorei (GL_PACK_ALIGNMENT, 1);
    59. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void GLWidget::paintGL()
    2. {
    3. makeCurrent();
    4.  
    5. glMatrixMode(GL_PROJECTION);
    6. glLoadIdentity();
    7. glMatrixMode(GL_MODELVIEW);
    8.  
    9. // Sobel operation
    10. Pixel *data = NULL;
    11.  
    12. // map PBO to get CUDA device pointer
    13. cutilSafeCall(cudaGraphicsMapResources(1, &cuda_pbo_resource, 0));
    14. size_t num_bytes;
    15. cutilSafeCall(cudaGraphicsResourceGetMappedPointer((void **)&data, &num_bytes,
    16. cuda_pbo_resource));
    17. qDebug() << QString("CUDA mapped PBO: May access %1 bytes").arg(num_bytes);
    18.  
    19. sobelFilter(data, m_pQImage.width(), m_pQImage.height(), SOBELDISPLAY_IMAGE, 1.0f);
    20. cutilSafeCall(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0));
    21.  
    22. glClear(GL_COLOR_BUFFER_BIT);
    23.  
    24. glBindTexture(GL_TEXTURE_2D, m_pTexture);
    25. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer);
    26. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_pQImage.width(), m_pQImage.height(),
    27. GL_LUMINANCE, GL_UNSIGNED_BYTE, (char *)NULL);
    28. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
    29.  
    30. glDisable(GL_DEPTH_TEST);
    31. glEnable(GL_TEXTURE_2D);
    32. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    33. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    34. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    35. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    36.  
    37. glBegin(GL_QUADS);
    38. glVertex2f(-1, -1); glTexCoord2f(0, 0);
    39. glVertex2f(-1, 1); glTexCoord2f(1, 0);
    40. glVertex2f( 1, 1); glTexCoord2f(1, 1);
    41. glVertex2f( 1, -1); glTexCoord2f(0, 1);
    42. glEnd();
    43.  
    44. //glDisable(GL_TEXTURE_2D);
    45. glBindTexture(GL_TEXTURE_2D, 0);
    46.  
    47. swapBuffers();
    48. }
    To copy to clipboard, switch view to plain text mode 


    Cheers!

  2. #2

    Default Re: QGLWidget + OpenGL + CUDA

    Quick question for you about compiling this; where exactly do you use the nvcc compiler in all this?
    Do you need to compile the Sobel filter separately using nvcc and then link to the library?

    Thanks in advance!

  3. #3
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QGLWidget + OpenGL + CUDA

    Quote Originally Posted by jpapon View Post
    Quick question for you about compiling this; where exactly do you use the nvcc compiler in all this?
    Do you need to compile the Sobel filter separately using nvcc and then link to the library?

    Thanks in advance!
    Yes, normally (!) all cu files have to be compiled with nvcc.

  4. #4

    Default Re: QGLWidget + OpenGL + CUDA

    Yeah, I just didn't see any reference to a pre compiled library in the .pro file.
    I'm just adding a subdirectory, placing the kernel in it, compiling to a static lib.
    Then I'm just linking to the library in the .pro file. Does that sound like the correct procedure?

    Then I need to figure out how to put a command in a .pro file so the qmake generated makefile also runs the make in the subdirectory.

  5. #5
    Join Date
    Jan 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget + OpenGL + CUDA

    Hi,

    I am trying to do something similar, but I'm finding that I cannot register the cuda resource from outside of the GLWidget... if I call cudaGraphicsGLRegisterBuffer from my QGLWidget everything is fine, but I wanted to seperate the logic into a different class, say 'Worker'.

    So I tried sending a signal to the main window when the QGLWidget has initialized OpenGL/VBO's and then spawn my Worker class (which is a thread and in which I wish to run my CUDA kernels).

    I just get a segfault. Does anyone see why I shouldn't be able to do this?

    Thanks

Similar Threads

  1. NVIDIA CUDA + Qt
    By redscorp in forum Qt Programming
    Replies: 3
    Last Post: 24th October 2010, 17:35
  2. QGLWidget renderText() problem in openGL.
    By pastispast in forum Qt Programming
    Replies: 7
    Last Post: 11th October 2010, 18:21
  3. OpenGL ES2.0 QGLWidget at GraphicsScene
    By chithize in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 19th August 2010, 05:11
  4. Replies: 1
    Last Post: 7th May 2010, 17:20
  5. cuda gdb
    By Wu in forum Newbie
    Replies: 2
    Last Post: 8th January 2010, 11:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.