unless you want to get into your OS-specific windowing, use freeglut. If you want to display a 2D image, declare a pixel buffer object (PBO), register it with CUDA (to a cudaGraphicsResource), then map your cudaGraphicsResource, then get the mapped pointer (which will be a device pointer mapped to the PBO memory). Do whatever CUDA processing you want, using the mapped device pointer. Then, unmap your cudaGraphicsResource, copy your PBO to an OpenGL texture (not a CUDA texture), then draw it to a quad… Or at least, that’s how I did it.
You could also just memcpy your result from your kernel to a host-based array and use glDrawPixels on it.
By the way, have you looked at the programming guide? It has a pretty decent explanation of DirectX/OpenGL interop with CUDA (although be forewarned, the Driver API example with OpenGL is incorrect)
I actually fixed the previous problem by plugging in everything inside single .cu file instead of using separate cpp file. Thing is that
I get this messages and errors.
:\users\retinaloct2\desktop\jay\oct processor\common\gl\freeglut_std.h(78) : warning C4005: ‘GLUT_API_VERSION’ : macro redefinition
1> c:\users\retinaloct2\desktop\jay\oct processor\common\GL/glut.h(95) : see previous definition of ‘GLUT_API_VERSION’
…
1>c:\users\retinaloct2\desktop\jay\oct processor\common\gl\freeglut_std.h(354): error: redeclaration cannot add dllexport/dllimport to “glutInit”
1>c:\users\retinaloct2\desktop\jay\oct processor\common\GL/glut.h(394): here
…
I get multiples of these for every instructions. Can somebody help me with this? I see in the headerfile version for glut.h is 3 but freeglut_std.h is version 4. Does this mean anything? Thankyou.
I would advise keeping your code for OpenGL and CUDA in separate files, namely with OpenGL related items in a C++ file and your cuda code in the .cu file. Including libraries (i.e. OpenGL, OpenCV, etc) in code that nvcc compiles doesn’t always end well. Your previous include file woes stem from you not informing the compiler of the header files/where to find them.
The only includes I needed as far as OpenGL goes were
//OpenGL includes
#include <GL/glew.h>
#include <GL/freeglut.h>
The easiest way to have these in your project (assuming you’re using visual studio) is to add the path to the header files to your project, i.e.
right click on your project in the solution explorer → properties → C/C++ → General → Additional Include Directories. On my computer, these files are at
You should probably be having your cudaGraphicsGLRegisterBuffer call done in the host code, and have your CUDA<–>OpenGL include header(s), such as “cuda_gl_interop.h”. I would highly advise looking at an existing project that works, and building off of their framework. Take a look at [post=‘this article’]http://drdobbs.com/cpp/222600097[/post] for an idea about this.
As a rule of thumb, you want to do your CUDA && OpenGL setup in your C++ file, then generate your data in your .cu file. Also, you’ll want to minimize your call(s) to cudaGraphicsGLRegisterBuffer, i.e. only do this once if at all possible for your given PBO. As for the pointer mapping, your want to get a cudaGraphicsResource *, then use “cudaGraphicsMapResources”, then “cudaGraphicsResourceGetMappedPointer” to get the device pointer. This is your mapped CUDA/OpenGL pointer where you want to put your result(s) into in order to display them.
To put it succinctly, “cudaGraphicsGLRegisterBuffer” (once), then (for each frame), “cudaGraphicsMapResources”, then “cudaGraphicsResourceGetMappedPointer” in your host code, then call your kernel (in your .cu file) w/ the mapped pointer, then “cudaGraphicsUnmapResources”, then display the PBO.
Thanks for the reply. I have resolved the headerfile problems. The link helped me alot. Actually but now Im having some errors that I do not understand. It seems like a hardware language problem. The errors are as bellow.
Linking…
1>MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: ___iob_func already defined in LIBCMT.lib(_file.obj)
1>MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _fclose already defined in LIBCMT.lib(fclose.obj)
1>MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _printf already defined in LIBCMT.lib(printf.obj)
1>MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _exit already defined in LIBCMT.lib(crt0dat.obj)
1>MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _free already defined in LIBCMT.lib(free.obj)
1>MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _malloc already defined in LIBCMT.lib(malloc.obj)
1>LINK : warning LNK4098: defaultlib ‘MSVCRTD’ conflicts with use of other libs; use /NODEFAULTLIB:library
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutPostRedisplay referenced in function “void __cdecl display(void)” (?display@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutSwapBuffers referenced in function “void __cdecl display(void)” (?display@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutMainLoop referenced in function _main
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutDisplayFunc referenced in function _main
1>C:\Users\RETINALOCT2\Desktop\Jay\OCT Processor\gpu_testers\Resampling_GL\Debug\Resampling_GL.exe : fatal error LNK1120: 4 unresolved externals
1>Build log was saved at “file://c:\Users\RETINALOCT2\Desktop\Jay\OCT Processor\gpu_testers\Resampling_GL\Resampling_GL\Debug\BuildLog.htm”
1>Resampling_GL - 11 error(s), 9 warning(s)
Can somebody help me with this? My short experience make me impossible to solve the problem. Thank you
Your glut* linker errors mean you’re not linking one of the necessary glut-based .lib files, most likely freeglut.lib. Check your project’s linker settings by right clicking on your project → properties → Linker → Additional Dependencies. Make sure freeglut.lib is part of your list.
As for the MSVCRTD linker problems, you’re most likely using libraries that were compiled as release, but your project is debug. This is a pretty common issue - MSVC has 4 different runtime linking libraries, MT, MTd, MD, and MDd. check out some of the resources online, i.e. visual c++ - Runtime Library mis-matches and VC++ - Oh, the misery! - Stack Overflow for some more information on this
I fixed the problem related to libcmt.lib. However, I still don’t get with Link2017 problem. As one discribed above I have
included additional libraries for freeglut.lib but still its showing the following errors.
Resampling_GL.obj : error LNK2019: unresolved external symbol __imp__fprintf referenced in function “enum CUTBoolean __stdcall cutCheckErrorGL(char const *,int)” (?cutCheckErrorGL@@YG?AW4CUTBoolean@@PBDH@Z)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol __imp__sprintf_s referenced in function “enum CUTBoolean __stdcall cutCheckErrorGL(char const *,int)” (?cutCheckErrorGL@@YG?AW4CUTBoolean@@PBDH@Z)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol __imp__fread referenced in function “void __cdecl readData(void)” (?readData@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol __imp__rewind referenced in function “void __cdecl readData(void)” (?readData@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol __imp__ftell referenced in function “void __cdecl readData(void)” (?readData@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol __imp__fseek referenced in function “void __cdecl readData(void)” (?readData@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol __imp__perror referenced in function “void __cdecl readData(void)” (?readData@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol __imp__fopen referenced in function “void __cdecl readData(void)” (?readData@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol __imp___vsnprintf referenced in function “void __cdecl VSPrintf(struct _iobuf *,char const *,…)” (?VSPrintf@@YAXPAU_iobuf@@PBDZZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutPostRedisplay referenced in function “void __cdecl display(void)” (?display@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutSwapBuffers referenced in function “void __cdecl display(void)” (?display@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutMainLoop referenced in function _main
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutDisplayFunc referenced in function _main
One of the friend says it might be a problem related to namespace. Does this ring a bell? Can somebody help me with this? Once again thank you for
helping me out.
1>Resampling_GL.obj : warning LNK4217: locally defined symbol ___iob_func imported in function “enum CUTBoolean __stdcall cutCheckErrorGL(char const *,int)” (?cutCheckErrorGL@@YG?AW4CUTBoolean@@PBDH@Z)
1>Resampling_GL.obj : warning LNK4217: locally defined symbol _fclose imported in function “void __cdecl readData(void)” (?readData@@YAXXZ)
1>Resampling_GL.obj : warning LNK4217: locally defined symbol _printf imported in function “void __cdecl readData(void)” (?readData@@YAXXZ)
1>Resampling_GL.obj : warning LNK4217: locally defined symbol _exit imported in function “void __cdecl __cudaSafeCall(enum cudaError,char const *,int)” (?__cudaSafeCall@@YAXW4cudaError@@PBDH@Z)
1>Resampling_GL.obj : warning LNK4217: locally defined symbol _free imported in function “void __cdecl VSPrintf(struct _iobuf *,char const *,…)” (?VSPrintf@@YAXPAU_iobuf@@PBDZZ)
1>Resampling_GL.obj : warning LNK4217: locally defined symbol _malloc imported in function “void __cdecl VSPrintf(struct _iobuf *,char const *,…)” (?VSPrintf@@YAXPAU_iobuf@@PBDZZ)
Im also having such errors. It seems like ignoring msvcrtd.lib makes these problems. Can somebody help me out with the settings? Thank you.