0

我正在编写 cuda by example book 中给出的一段非常简单的代码,它是 cuda openGL interop 创建图形。该程序正在成功构建,但是当我运行该程序时,窗口显示:应用程序无法正确启动。单击确定关闭应用程序。我不知道为什么会这样,因为我很少有成功运行的示例 cuda 程序以及 opengl 示例程序。我什至从运行成功的 NVidia 示例程序中运行示例 cuda openGL 互操作程序。我应该在这里提一下,我已将所有 lib 文件包含在附加库中,以及包含在附加包含目录中的文件。我相信这是因为我用于互操作的像素缓冲区导致正常的 openGL 和 cuda 程序运行良好。我还应该提到,当我尝试将缓冲区 API 包含在程序中时,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();
}
4

1 回答 1

0

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

于 2012-09-10T17:31:36.643 回答