'cuGraphicsGLRegisterImage' problem No 'cuGraphicsGLRegisterImage' definition can be

Hi,
I use CUDA Toolkit 4.0, my card is GeForce 9600 GT, with newest (280.26 WHQL) Windows 7 x64 driver installed. When I compile my code in VS-2010-PRO I encounter the problem stated in topic description. I include in the code ‘cuda.h’ and link cuda.lib (all in 32 bit configuration, for 64 bit the situation is alike). Simmilar problem is when I try to call ‘cuGLCtxCreate’. Any idea why? Do I shold include some more headers? Tried to find OpenGL CUDA driver API samples but failed in finding any.

Regards,
MK

P.S.
Should such a topic already exists, please redirect me to it, for I couldn’t find it myself.
Thanks.

I think I found the solution - include cudaGL.h after You include glew.h.

But the other think is that when I call ‘cuGLCtxCreate’ I get CUDA_ERROR_UNKNOWN returned. Any idea why?

Regards,
MK

I just ran into this today; I believe it’s because I had not created an OpenGL context prior to doing the cuGLCtxCreate call. It’s not documented very well, but (it seems) you need an OpenGL context prior to using any of the cuGL* calls. You can make an OpenGL context in a few different ways, i.e. glutInit(&argc, argv);

Don’t know if you’re still interested in this, but it might help someone in the future

I changed my code and added ‘glutInit’ before calling ‘cuGLCtxCreate’ but the situation stays the same, whereas I call ‘cuCtxCreate’ instead, the mentioned error occures back while calling ‘cuGraphicsGLRegisterImage’. Would You be so kind and provide Your code You mentioned for me to check whether I do things in right order?

Regards,

MK

P.S.

Could it be that I am linking OpenGL in a wrong way?

Hey, here’s the skeleton of the glut/OpenGL setup code I’m using (sans the error checking or non-relevant portions). If you use the regular ‘cuCtxCreate’ function instead of ‘cuGLCtxCreate’ to create your cuda context, it’ll fail on the first attempt at using a cuGL* call.

CUdevice dev;

CUcontext GLcontext;

...

//call this before any driver api calls

cuInit(0);

//set the device to use

cuDeviceGet(&dev, 0);

//call this for the OpenGL context. This assumes you don't pass any command line-args for glutInit

int numArgs = 0;

glutInit(&numArgs, NULL);

...

//create glut window

...

glewInit();

//creates the cuda context w/ opengl interoperability

cuGLCtxCreate(&GLcontext, CU_CTX_SCHED_BLOCKING_SYNC, dev);

cuGLInit (); //not sure if this is needed?

...

//init driver api parameters, i.e. cuModuleLoad, etc.

//register glut callbacks, create buffers, etc.

...

//enter the endless glut loop

glutMainLoop();

Just as an aside, the

int numArgs = 0;

glutInit(&numArgs, NULL);

call just eliminates the need for having command line arguments to pass to the glutInit function, so you can have it somewhere other than main and not have to pass the arguments around. I just found it to be convenient, and I haven’t noticed any problems from using it

Hope this solves your problem(s), let me know how it goes

Thanks, it helped! :)

But…

Now I get the same error (meaning CUDA_ERROR_UNKNOWN) returned when I call ‘cuGraphicsResourceGetMappedPointer’ :(

I have code something like the one below:

glGenTextures(1, &s_glColor);

glBindTexture(GL_TEXTURE_2D, s_glColor);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_FLOAT, 0);

glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST);

cuGraphicsGLRegisterImage(&s_cuColorBuffer, s_glColor, GL_TEXTURE_2D, CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD);

glBindTexture(GL_TEXTURE_2D, 0);

...

cuGraphicsMapResources(1, &s_cuColorBuffer, 0);

...

cuGraphicsResourceGetMappedPointer(&s_ColorBuffer, &bytes, s_cuColorBuffer);// here...

...

Could the wrong texture format/type/or-something-else can cause such an error? Is the texture ‘s_glColor’ need to be bound while the ‘cuGraphicsResourceGetMappedPointer’ is called?

Regards,

MK

Ok, I reviewed my code and did things diffrently. I don’t try to use GL texture directly in CUDA, but map PBO instead as a buffer resource. It does the work!

...

glGenTextures(1, &s_glColor);

glBindTexture(GL_TEXTURE_2D, s_glColor);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, s_glW, s_glH, 0, GL_RGBA, GL_FLOAT, 0);

glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST);

glBindTexture(GL_TEXTURE_2D, 0);

glGenBuffers(1, &s_glColorBuffer);

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, s_glColorBuffer);

glBufferData(GL_PIXEL_UNPACK_BUFFER, s_glW * s_glH * 4 * size, 0, GL_DYNAMIC_DRAW);

cuGraphicsGLRegisterBuffer(&s_cuColorBuffer, s_glColorBuffer, CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD);

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

...

Then after doing what CUDA has to do I unpack the PBO to a bound texture and render it to screen.

...

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, s_glColorBuffer);

glBindTexture(GL_TEXTURE_2D, s_glColor);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, s_glW, s_glH, GL_RGBA, GL_FLOAT, 0);

renderScreenAlignedQuad(s_glColor);

...

Thanks again for help.

Regards,

MK