2013-04-20 80 views
3

我最近開始編碼CUDA OpenGL互操作性。大部分時間我都在Linux環境下開發。想我的代碼轉移到我碰到下面的問題就來了我的MacBook Pro運行OS X:CUDA OpenGL互操作,資源映射混淆緩衝區

每當我從映射OpenGL的頂點緩衝區對象作爲一種資源來CUDA它攪亂了存儲在相應的緩衝區中的數據。

這是我的代碼:

// OpenGL and window handler includes 
#include <GL/glew.h> 
#include <SDL/SDL.h> 
#include <OpenGL/GL.h> 

// OpenGL/CUDA interop 
#include <cuda_runtime.h> 
#include <cuda_gl_interop.h> 

// C libraries 
#include <iostream> 

int main(int argc, char* argv[]) 
{ 
    GLuint vbo; 
    struct cudaGraphicsResource *cuda_vbo_resource; 

    // create OpenGL context with SDL 
    SDL_Init(SDL_INIT_VIDEO); 
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); 
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); 
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); 
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); 
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 
    SDL_SetVideoMode(400, 200, 16, SDL_OPENGL); 

    // initialize extension wrangler 
    glewInit(); 

    // set cuda/GL device 
    cudaGLSetGLDevice(0); 

    // generate buffer 
    glGenBuffers(1, &vbo); 

    // fill buffer with data (1, 2, 3) 
    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    int inData[] = {1,2,3}; 
    glBufferData(GL_ARRAY_BUFFER, 3*sizeof(int), inData, GL_DYNAMIC_DRAW); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 

    // Register buffer with cuda 
    cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, vbo,    
             cudaGraphicsMapFlagsWriteDiscard); 

    // let's have a look at the buffer 
    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    int* mappedBuffer = (int *) glMapBuffer(GL_ARRAY_BUFFER,GL_READ_ONLY); 
    printf("\tbefore mapping: %d, %d, %d\n",mappedBuffer[0], mappedBuffer[1], 
      mappedBuffer[2]); 
    glUnmapBuffer(GL_ARRAY_BUFFER); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 

    // map and unmap the cuda resource 
    if (cudaGraphicsMapResources(1, &cuda_vbo_resource, 0) != cudaSuccess) 
     printf("Resource mapping failed...\n"); 
    if (cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0) != cudaSuccess) 
     printf("Resource unmapping failed...\n"); 

    // let's have a look at the buffer again 
    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    mappedBuffer = (int *) glMapBuffer(GL_ARRAY_BUFFER,GL_READ_ONLY); 
    printf("\tafter mapping: %d, %d, %d\n",mappedBuffer[0], mappedBuffer[1], 
      mappedBuffer[2]); 
    glUnmapBuffer(GL_ARRAY_BUFFER); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 

    // unregister the cuda resource 
    cudaGraphicsUnregisterResource(cuda_vbo_resource); 

    // delete the buffer 
    glBindBuffer(1, vbo); 
    glDeleteBuffers(1, &vbo); 

    return 0; 
} 

與編譯後:

g++ -o resourceMapping resourceMapping.cpp `sdl-config --cflags --libs` -lglew 
     -framework OpenGL -I /usr/local/cuda/include -L /usr/local/cuda/lib -lcudart 

我得到

before mapping: 1, 2, 3 
after mapping: 0, 33554432, 32 

有誰知道爲什麼映射改變緩衝區中的數據?這對我來說尤其令人困惑,因爲這在Linux機器上不會發生。

感謝您的任何意見或提示!

回答

3

我注意到你用cudaGraphicsMapFlagsWriteDiscard註冊了vbo。

here「指定CUDA不會從該資源讀取數據,並將寫入資源的全部內容,因此以前存儲在資源中的數據都不會保留。」

也許嘗試cudaGraphicsMapFlagsNone或cudaGraphicsMapFlagsReadOnly?

+0

非常感謝!這解決了問題。 – nils 2013-04-21 05:23:56