在MS Visual Studio 2010上使用带有Cuda interop的OpenGL缓冲区的异常错误

时间:2012-09-10 02:04:42

标签: c++ opengl cuda

我正在编写一个非常简单的代码,这个代码是在cuda中通过示例书给出的,它是cuda openGL interop创建图形。该程序正在成功构建,但当我运行该程序时,窗口显示: 应用程序无法正确启动。 单击“确定”关闭应用程序。 我不知道为什么会这样,因为我有几个成功运行的示例cuda程序以及opengl示例程序。我甚至从运行成功的NVidia Sample程序运行了示例cuda openGL互操作程序。我应该在这里提一下,我已将所有lib文件包含在附加库中以及附加包含目录中包含的文件中。我相信这是因为我用于互操作的像素缓冲区导致正常的openGL和cuda程序正常运行。我还应该提一下,当我试图在程序中包含那些时,visual studio intellisense正在显示缓冲区API(如glGenBuffers等),但是在程序中声明后,它显示标识符未定义,下面有一条红线。但是在NVidia示例OpenGL代码中没有发生这种情况。

我正在粘贴以下代码:

#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();
}

1 个答案:

答案 0 :(得分:0)

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