简体   繁体   English

在MS Visual Studio 2010上将OpenGL缓冲区与Cuda互操作一起使用时出现异常错误

[英]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. 我正在编写示例书中的cuda中给出的一段非常简单的代码,即cuda openGL interop创建图形。 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. 我不知道为什么会这样,原因是我没有几个成功运行的示例cuda程序以及opengl示例程序。 I even run the sample cuda openGL interop program from the NVidia Sample program which is running successfully. 我什至从成功运行的NVidia示例程序中运行示例cuda openGL interop程序。 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. 我应该在这里提到,我已经将所有库文件都包含在附加库中,并将包含文件包含在附加include目录中。 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. 我相信发生这种情况是因为我为互操作使用的像素缓冲区导致正常的openGL和cuda程序运行良好。 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. 我还应该提到,当我尝试在程序中包含缓冲区API时,visual studio intellisense会显示缓冲区API(例如glGenBuffers等),但是在程序中声明后,它显示的标识符未定义,并在其下方显示了红线。 。 But this thing is not happening in the NVidia sample OpenGL code. 但是NVidia示例OpenGL代码中没有发生这种情况。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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