仅显示 glClear(..) 颜色,不显示任何其他内容(CUDA/OpenGL 互操作)

Only glClear(..) color is displayed, nothing else rendered (CUDA/OpenGL interop)

我有一个带有面板(500x500 像素)的 WinForms 应用程序,我想在其中渲染一些东西。此时我只是想用特定的颜色填充它。我想使用 OpenGL/CUDA 互操作来执行此操作。

我将面板配置为渲染内容的区域,但是当我 运行 我的代码时,面板只填充了 glClear(..) 颜色,内核没有分配任何内容被展示。今天早上它有点工作(不一致),在我试图解决 SwapBuffers() 混乱的过程中,我想我搞砸了。

这里是 OpenGL 的像素格式初始化。它似乎工作正常,我有我预期的两个缓冲区,上下文正确:

static  PIXELFORMATDESCRIPTOR pfd=              
{
    sizeof(PIXELFORMATDESCRIPTOR),              // Size Of This Pixel Format Descriptor
    1,                                          // Version Number
    PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
    PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
    PFD_DOUBLEBUFFER,                           // Must Support Double Buffering
    PFD_TYPE_RGBA,                              // Request An RGBA Format
    16,                                         // Select Our Color Depth
    0, 0, 0, 0, 0, 0,                           // Color Bits Ignored
    0,                                          // No Alpha Buffer
    0,                                          // Shift Bit Ignored
    0,                                          // No Accumulation Buffer
    0, 0, 0, 0,                                 // Accumulation Bits Ignored
    16,                                         // 16Bit Z-Buffer (Depth Buffer) 
    0,                                          // No Stencil Buffer
    0,                                          // No Auxiliary Buffer
    PFD_MAIN_PLANE,                             // Main Drawing Layer
    0,                                          // Reserved
    0, 0, 0                                     // Layer Masks Ignored
};

GLint  iPixelFormat; 

// get the device context's best, available pixel format match 
if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
{
    MessageBox::Show("ChoosePixelFormat Failed");
    return 0;
}

// make that match the device context's current pixel format 
if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
{
    MessageBox::Show("SetPixelFormat Failed");
    return 0;
}

if((m_hglrc = wglCreateContext(m_hDC)) == NULL)
{
    MessageBox::Show("wglCreateContext Failed");
    return 0;
}

if((wglMakeCurrent(m_hDC, m_hglrc)) == NULL)
{
    MessageBox::Show("wglMakeCurrent Failed");
    return 0;
}   

完成后,我将 ViewPort 设置为:

glViewport(0,0,iWidth,iHeight);                     // Reset The Current Viewport
glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
glLoadIdentity();                                   // Reset The Modelview Matrix
glEnable(GL_DEPTH_TEST);

然后我设置了clear color,做了一个clear:

glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);  

现在我设置 CUDA/OpenGL 互操作:

cudaDeviceProp prop; int dev;
memset(&prop, 0, sizeof(cudaDeviceProp));
prop.major = 1; prop.minor = 0;

checkCudaErrors(cudaChooseDevice(&dev, &prop));
checkCudaErrors(cudaGLSetGLDevice(dev));

glBindBuffer    = (PFNGLBINDBUFFERARBPROC)GET_PROC_ADDRESS("glBindBuffer");
glDeleteBuffers = (PFNGLDELETEBUFFERSARBPROC)GET_PROC_ADDRESS("glDeleteBuffers");
glGenBuffers    = (PFNGLGENBUFFERSARBPROC)GET_PROC_ADDRESS("glGenBuffers");
glBufferData    = (PFNGLBUFFERDATAARBPROC)GET_PROC_ADDRESS("glBufferData");

GLuint bufferID;
cudaGraphicsResource * resourceID;

glGenBuffers(1, &bufferID);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, bufferID);
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, fWidth*fHeight*4, NULL, GL_DYNAMIC_DRAW_ARB);

checkCudaErrors(cudaGraphicsGLRegisterBuffer( &resourceID, bufferID, cudaGraphicsMapFlagsNone ));

现在我尝试调用我的内核(它只为每个像素绘制特定颜色)并显示它。

uchar4* devPtr;
size_t size;

// First clear the back buffer:
glClearColor(1.0f, 0.5f, 0.0f, 0.0f); // orange
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

checkCudaErrors(cudaGraphicsMapResources(1, &resourceID, NULL));
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void**)&devPtr, &size, resourceID));

animate(devPtr); // This will call the kernel and do a sync (see later)

checkCudaErrors(cudaGraphicsUnmapResources(1, &resourceID, NULL));

// Swap buffers to bring back buffer forward:
SwapBuffers(m_hDC);

此时我希望在屏幕上看到内核颜色,但是没有!我看到橙色,这是我刚刚设置的清晰颜色。

这里是对内核的调用:

void animate(uchar4* dispPtr)
{
    checkCudaErrors(cudaDeviceSynchronize());
    animKernel<<<blocks, threads>>>(dispPtr, envdim);;
    checkCudaErrors(cudaDeviceSynchronize());
}

这里 envdim 只是尺寸(所以是 500x500)。内核本身:

__global__ void animKernel(uchar4 *optr, dim3 matdim)
{
    int x = threadIdx.x + blockIdx.x * blockDim.x;
    int y = threadIdx.y + blockIdx.y * blockDim.y;
    int offset = x + y * matdim.x;

    if (x < matdim.x && y < matdim.y)
    {
        // BLACK:
        optr[offset].x = 0; optr[offset].y = 0; optr[offset].z = 0;
    }
}

我做过的事情:

所以我想我遗漏了一些东西,但我正在疯狂地寻找它。有什么建议吗?帮助?

这是我自己回答的另一个问题!哼,正如我所料,这是一个单行问题。问题在于渲染调用本身。

配置没问题,我在上面的代码中遇到的一个问题是: 我从未调用过 glDrawPixels(),这是 OpenGL 驱动程序将共享缓冲区 (GL_PiXEL_UNPACK_BUFFER_ARB) 源复制到显示缓冲区所必需的。那么正确的渲染顺序是:

uchar4* devPtr;
size_t size;

// First clear the back buffer:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

checkCudaErrors(cudaGraphicsMapResources(1, &resourceID, NULL));
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void**)&devPtr, &size, resourceID));

animate(devPtr); // This will call the kernel and do a sync (see later)

checkCudaErrors(cudaGraphicsUnmapResources(1, &resourceID, NULL));

// This is necessary to copy the shared buffer to display
glDrawPixels(fWidth, fHeight, GL_RGBA, GL_UNSIGNED_BYTE, 0);

// Swap buffers to bring back buffer forward:
SwapBuffers(m_hDC);

我要感谢 Acade-- 呃,CUDA By Example,再次帮助我。尽管书中的示例代码使用了 GLUT(对此完全没用......),但书中引用了普通的 gl 函数。