Advertisement
Guest User

Show Image CUDA

a guest
Mar 27th, 2013
1,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <gl/freeglut.h>
  3. #include <cuda_gl_interop.h>
  4. #include <cuda_runtime.h>
  5. #include <rendercheckGL.h>
  6.  
  7. GLuint pointer_to_buffer = NULL;
  8. GLuint texture_ID = NULL;
  9.  
  10. void* gimg;
  11. int full = 0;
  12.  
  13. int W = 0, H = 0, CH = 0, PICH = 0;
  14.  
  15. int current_width = 0, current_height = 0;
  16.  
  17. char* winTitle = "Image";
  18.  
  19. int waitKeyCalled   = 0;
  20. int timeDelay = 0;
  21. int pressed_key = -1;
  22.  
  23. unsigned short channel_type = 0;
  24. unsigned short channel_sequence = 0;
  25. unsigned short image_depth = 0;
  26.  
  27. void CreateTexture(GLuint* textureId, int width, int height, int channels)
  28. {
  29.     glEnable(GL_TEXTURE_2D);
  30.     glGenTextures(1,textureId);
  31.     glBindTexture(GL_TEXTURE_2D,*textureId);
  32.  
  33.     glTexImage2D(GL_TEXTURE_2D,0,channel_type,width,height,0,channel_sequence,image_depth,NULL);
  34.  
  35.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  36.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  37.  
  38. }
  39.  
  40. void CreatePBO(GLuint* pbo)
  41. {
  42.     if(pbo)
  43.     {
  44.         int data_size = W * H * CH * sizeof(GLubyte);
  45.  
  46.         glGenBuffers(1,pbo);
  47.         glBindBuffer(GL_PIXEL_UNPACK_BUFFER,*pbo);
  48.         glBufferData(GL_PIXEL_UNPACK_BUFFER,data_size,NULL,GL_DYNAMIC_COPY);
  49.         cudaGLRegisterBufferObject(*pbo);
  50.     }
  51. }
  52.  
  53. void RunCUDA()
  54. {
  55.     void* dptr = NULL;
  56.     size_t size = W * H * CH;
  57.     size_t pitch = W * CH;
  58.  
  59.     CreatePBO(&pointer_to_buffer);
  60.     cudaGLMapBufferObject((void**)&dptr,pointer_to_buffer);
  61.     cudaMemcpy2D(dptr,pitch,gimg,PICH,pitch,H,cudaMemcpyDeviceToDevice);
  62.     cudaGLUnmapBufferObject(pointer_to_buffer);
  63. }
  64.  
  65. void TimerFunction(int value)
  66. {
  67.     if(value == 1)
  68.         glutLeaveMainLoop();
  69.     glutPostRedisplay();
  70. }
  71.  
  72. void DisplayFunction()
  73. {
  74.     glutTimerFunc(timeDelay,TimerFunction,timeDelay!=0);
  75.     glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  76.     glBindBuffer(GL_PIXEL_UNPACK_BUFFER,pointer_to_buffer);
  77.     glBindTexture(GL_TEXTURE_2D,texture_ID);
  78.     glTexSubImage2D(GL_TEXTURE_2D,0,0,0,W,H,channel_sequence,image_depth,NULL);
  79.     glBegin(GL_QUADS);
  80.     glTexCoord2f(0.0f,1.0f);    glVertex2f(0.0f,0.0f);
  81.     glTexCoord2f(0.0f,0.0f);    glVertex2f(0.0f,1.0f);
  82.     glTexCoord2f(1.0f,0.0f);    glVertex2f(1.0f,1.0f);
  83.     glTexCoord2f(1.0f,1.0f);    glVertex2f(1.0f,0.0f);
  84.     glEnd();
  85.     glutSwapBuffers();
  86. }
  87.  
  88. void MouseWheelFunc(int wheel, int direction, int x, int y)
  89. {
  90.     if(!full)
  91.     {
  92.         if(direction > 0)
  93.         {
  94.             current_width /= 0.97f;
  95.             current_height /= 0.97f;
  96.         }
  97.         else
  98.         {
  99.             current_width *= 0.97f;
  100.             current_height *= 0.97f;
  101.         }
  102.         glutReshapeWindow(current_width,current_height);
  103.  
  104.         glutPostRedisplay();
  105.     }
  106. }
  107.  
  108. void KeyBoardFunction(unsigned char key, int x, int y)
  109. {
  110.     pressed_key = key;
  111.     int mod = glutGetModifiers();
  112.  
  113.     switch(key)
  114.     {
  115.     case 102:
  116.         glutFullScreenToggle();
  117.         full = 1 - full;
  118.         break;
  119.  
  120.     default:
  121.         glutLeaveMainLoop();
  122.         break;
  123.     }
  124.     glutPostRedisplay();
  125. }
  126.  
  127. void ClosingFuntion()
  128. {
  129.     cudaGLUnregisterBufferObject(pointer_to_buffer);
  130.     glDeleteTextures(1,&texture_ID);
  131.     glDeleteBuffers(1,&pointer_to_buffer);
  132.     glDisable(GL_TEXTURE_2D);
  133. }
  134.  
  135. void InitCUDA()
  136. {
  137.     cudaGLSetGLDevice(0);
  138.     CreateTexture(&texture_ID,W,H,CH);
  139.     RunCUDA();
  140. }
  141.  
  142.  
  143. void InitGL()
  144. {
  145.     glutInit(&__argc,__argv);
  146.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  147.     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
  148.     unsigned int desk_w = 0, desk_h = 0;
  149.  
  150.     desk_w = glutGet(GLUT_SCREEN_WIDTH);
  151.     desk_h = glutGet(GLUT_SCREEN_HEIGHT);
  152.  
  153.     if(W > desk_w || H > desk_h)
  154.         glutInitWindowPosition(0,0);
  155.     else
  156.         glutInitWindowPosition((desk_w - W)/2,(desk_h-H)/2);
  157.     glutInitWindowSize(W,H);
  158.     glutCreateWindow(winTitle);
  159.     glutDisplayFunc(DisplayFunction);
  160.     glutKeyboardFunc(KeyBoardFunction);
  161.     glutMouseWheelFunc(MouseWheelFunc);
  162.     glutCloseFunc(ClosingFuntion);
  163.  
  164.     glewInit();
  165.  
  166.     glViewport(0,0,W,H);
  167.  
  168.     glDisable(GL_DEPTH_TEST);
  169.  
  170.     glMatrixMode(GL_MODELVIEW);
  171.     glLoadIdentity();
  172.  
  173.     glMatrixMode(GL_PROJECTION);
  174.     glLoadIdentity();
  175.  
  176.     glOrtho(0.0,1.0,0.0,1.0,0.0,1.0);
  177. }
  178.  
  179. void _showImage()
  180. {
  181.     InitGL();
  182.     InitCUDA();
  183.     glutDisplayFunc(DisplayFunction);
  184.     glutMainLoop();
  185. }
  186.  
  187. //data = image data in GPU Global memory
  188. int ShowRawImage(void* data, int width, int height, size_t pitch, int channels)
  189. {
  190.     winTitle = "Image";
  191.  
  192.     W = width;
  193.     H = height;
  194.     current_width =  W;
  195.     current_height = H;
  196.     CH = channels;
  197.     PICH = pitch;
  198.     gimg = data;
  199.  
  200.     switch(channels)
  201.     {
  202.     case 1:
  203.         channel_type = GL_LUMINANCE;
  204.         channel_sequence = GL_LUMINANCE;
  205.         image_depth = GL_UNSIGNED_BYTE;
  206.  
  207.     case 3:
  208.         channel_type = GL_RGB;
  209.         channel_sequence = GL_RGB;
  210.         image_depth = GL_UNSIGNED_BYTE;
  211.         break;
  212.  
  213.     case 4:
  214.         channel_type = GL_RGBA;
  215.         channel_sequence = GL_RGBA;
  216.         image_depth = GL_UNSIGNED_BYTE;
  217.         break;
  218.     default:
  219.         return -1;
  220.     }
  221.     _showImage();
  222.  
  223.     return 0;
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement