cuGLMapBufferObject Explanation of its usage

Would it be possible to see an example of using cuGLMapBufferObject, which

uses the driver API? In the code below, I have allocated 200 float4s.

when calling, cuGLMapBufferObject, I’d expect ssize to equal 3200,

and ssize to be identical in magnitude to size.

However, it does not. Why not? Thanks!

Gordon

   //Initialize the host data

    float4* ptr = new float4(200);

    unsigned int size = 200*sizeof(float4);

    printf("size= %d\n", size);

   glGenBuffers(1, &vbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    glBufferData(GL_ARRAY_BUFFER,  size, (void *) ptr, GL_DYNAMIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

   CU_SAFE_CALL(cuGLRegisterBufferObject(vbo));

   CUdeviceptr devPtr;

    unsigned int ssize;

    CUDA_SAFE_CALL(cuGLMapBufferObject(&devPtr, &ssize, vbo));

    printf("devPtr= %d, size= %d\n", devPtr, ssize);

Can anyone please help me (Nvidia experts?) or at least tell me that you cannot help me? I am stuck until this is resolved. Thanks.

Gordon

What size is being reported back? If it’s somewhat larger than expected, the buffer probably got bumped up to some alignment constraint when it was allocated. This is not concerning, the interop should still work.

I am attaching a test program to try out.

CODE OUTPUT

  • size= 80000, ssize= 5892533, devPtr= 6496

  • size= 80000, ssize= 5892533, devPtr= 6496

ANALYSIS

I called cuGLMapBufferObject with the first two arguments in different

orders, yet the ssize (which is incorrect) prints out the same. The only

that could happen is if this function were overloaded. Is this the case.

Seems rather strange.

Note that ssize should be equal to size, or slightly larger if there is an

alignment issue.

Thanks for any help.

Gordon

#include <GL/glew.h>

#include <GL/glut.h>

#include <cuda.h>

#include <cutil.h>

#include <cudaGL.h>

#include <stdio.h>

#include <stdlib.h>

void display()

{

    glClearColor( 0.0, 0.0, 0.0, 1.0);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSwapBuffers();

}

//----------------------------------------------------------------------

void idle()

{

    glutPostRedisplay();

}

//----------------------------------------------------------------------

void keyboard(unsigned char key, int x, int y)

{

    switch( key) {

    case(27) :

    case('q') :

        exit(0);

        break;

    }

}

//----------------------------------------------------------------------

int main(int argc, char** argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize(512, 512);

    glutCreateWindow("BasicGL");

   glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutIdleFunc(idle);

   glewInit();

   CU_SAFE_CALL(cuGLInit());

    CUT_CHECK_ERROR_GL();

   //glutPostRedisplay();

   int sz = 20000;

    float* ptr = new float[sz];

    unsigned int size = sz*sizeof(float);

   unsigned int vbo;

    glGenBuffers(1, &vbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    glBufferData(GL_ARRAY_BUFFER,  size, (const void*) ptr, GL_DYNAMIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

   CUdeviceptr devPtr;

    unsigned int ssize;

   CU_SAFE_CALL(cuGLRegisterBufferObject(vbo));

    //CU_SAFE_CALL(cuGLMapBufferObject(&devPtr, &ssize, vbo));

   // <<<<   PAY ATTENTION ....

    CU_SAFE_CALL(cuGLMapBufferObject(&devPtr, &ssize, vbo));

    printf("- size= %d, ssize= %d, devPtr= %d\n", size, ssize, devPtr);

   CU_SAFE_CALL(cuGLMapBufferObject(&ssize, &devPtr, vbo));

    printf("- size= %d, ssize= %d, devPtr= %d\n", size, ssize, devPtr);

   glutMainLoop();

}#if 0

CODE OUTPUT

- size= 80000, ssize= 5892533, devPtr= 6496

- size= 80000, ssize= 5892533, devPtr= 6496

ANALYSIS

I called cuGLMapBufferObject with the first two arguments in different 

orders, yet the ssize (which is incorrect) prints out the same. The only 

that could happen is if this function were overloaded. Is this the case. 

Seems rather strange. 

Note that ssize should be equal to size, or slightly larger if there is an 

alignment issue. 

Thanks for any help. 

#endif

Hi,

Could somebody please take a look at the code I provided in the previous message and
try it out? I am really stuck until this issue is resolved. Thanks.

Gordon

For anyone interested, here is the solution to the bug in cuGLMapBufferObject() that I reported several days ago. The fact that I got no responses indicates that very few people use the driver API, and even fewer interact such codes with GL. In any case, I simply wrote a mapping routine with the standard API (cudaxxx):

template <class T>

T* mapBufferObject(T* ptr, unsigned int vbo)

{

   T *dptr;

    CUDA_SAFE_CALL(cudaGLMapBufferObject((void**) &dptr, vbo));

   return dptr;

}

To use from the driver API, simply execute:

 CUdeviceptr dev = (CUdeviceptr) mapBufferObject(float*4 ptr, vbo)

This works because CUdeviceptr is actually typedef’ed to an unsigned int, which clearly suggests that it is actually the pointer to the data we are interested in. In any case, this approach allowed me to solve my problem.

It would however be nice if the people at NVidia could respond as to whether the Driver API routine is functioning as expected by providing some example code. There is none in the various projects.

Thanks,

Gordon