简体   繁体   中英

Unusual Error Using OpenGL Buffers with Cuda interop on MS Visual Studio 2010

I was writing a very simple piece of code given in the cuda by example book which is the cuda openGL interop creating graphics. The program is getting build successfully but when I am running the program the windows shows : The application was unable to start correctly. Click OK to close the application. I don't know why it happening cause I have few sample cuda programs which are running successfully as well as the opengl sample programs. I even run the sample cuda openGL interop program from the NVidia Sample program which is running successfully. I should mention here that I have included all the lib files in the additional libraries as well as the included files in the additional include directories. I believe that this is happening because of the pixel buffers that I am using for the interop cause normal openGL and cuda program are running fine. I should also mention that visual studio intellisense is showing the buffers API (like glGenBuffers,etc.) when I am trying to include those in the program but after declaring in the program it is showing that the identifier is undefined with a red line underneath it. But this thing is not happening in the NVidia sample OpenGL code.

I am pasting the code below :

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cutil.h>
#include <cuda_gl_interop.h>

#define     DIM    512

GLuint  bufferObj;
cudaGraphicsResource *resource;

// based on ripple code, but uses uchar4 which is the type of data
// graphic inter op uses. see screenshot - basic2.png
__global__ void kernel( uchar4 *ptr ) {
int x = threadIdx.x + blockIdx.x * blockDim.x;
    int y = threadIdx.y + blockIdx.y * blockDim.y;
    int offset = x + y * blockDim.x * gridDim.x;

    // now calculate the value at that position
    float fx = x/(float)DIM - 0.5f;
    float fy = y/(float)DIM - 0.5f;
    unsigned char   green = 128 + 127 *
        sin( abs(fx*100) - abs(fy*100) );

    // accessing uchar4 vs unsigned char*
    ptr[offset].x = 0;
    ptr[offset].y = green;
    ptr[offset].z = 0;
    ptr[offset].w = 255;
}

static void key_func( unsigned char key, int x, int y ) {
    switch (key) {
    case 27:
        // clean up OpenGL and CUDA
         cudaGraphicsUnregisterResource( resource );
        glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
        glDeleteBuffers( 1, &bufferObj );
        exit(0);
    }
}

static void draw_func( void ) {

    glDrawPixels( DIM, DIM, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
    glutSwapBuffers();
}


int main( int argc, char **argv ) {
    cudaDeviceProp  prop;
    int dev;

    memset( &prop, 0, sizeof( cudaDeviceProp ) );
    prop.major = 1;
    prop.minor = 0;
     cudaChooseDevice( &dev, &prop );
    cudaGLSetGLDevice( dev ) ;
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowSize( DIM, DIM );
    glutCreateWindow( "bitmap" );

    glGenBuffers( 1, &bufferObj );
    glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, bufferObj );
    glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, DIM * DIM * 4,NULL, GL_DYNAMIC_DRAW_ARB);
cudaGraphicsGLRegisterBuffer( &resource, 
        bufferObj, 
        cudaGraphicsMapFlagsNone ) ;

    // do work with the memory dst being on the GPU, gotten via mapping
     cudaGraphicsMapResources( 1, &resource, NULL ) ;
    uchar4* devPtr;
    size_t  size;
    cudaGraphicsResourceGetMappedPointer( (void**)&devPtr, 
        &size, 
        resource) ;

    dim3    grids(DIM/16,DIM/16);
    dim3    threads(16,16);
    kernel<<<grids,threads>>>( devPtr );
    cudaGraphicsUnmapResources( 1, &resource, NULL ) ;

    glutKeyboardFunc( key_func );
    glutDisplayFunc( draw_func );
    glutMainLoop();
}

不要忘记在glewInit()之前glGenBuffers()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM