与CUDA / OGL互操作崩溃

时间:2013-12-24 14:52:50

标签: c++ opengl cuda interop

我正在尝试设置一个小的CUDA / GL互操作示例。我在互联网上环顾四周,所以我找到了一些有用的教程。

我想要的只是在CUDA中生成一个纹理并用OpenGL绘制它。

我现在拥有的源代码每次运行时都会崩溃我的Macbook Pro,所以我想如果有人能够关注它,那将非常有用。

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>

    #ifdef _WIN32
    #  define WINDOWS_LEAN_AND_MEAN
    #  define NOMINMAX
    #  include <windows.h>
    #endif

    // OpenGL Graphics includes
    #include <GL/glew.h>
    #if defined (__APPLE__) || defined(MACOSX)
    #include <GLUT/glut.h>
    #else
    #include <GL/freeglut.h>
    #endif

    // includes, cuda
    #include <cuda_runtime.h>
    #include <cuda_gl_interop.h>

    // Utilities and timing functions
    #include <helper_functions.h>    // includes cuda.h and cuda_runtime_api.h
    #include <timer.h>               // timing functions

    // CUDA helper functions
    #include <helper_cuda.h>         // helper functions for CUDA error check
    #include <helper_cuda_gl.h>      // helper functions for CUDA/GL interop

    #include <vector_types.h>
     const unsigned int window_width  = 512;
     const unsigned int window_height = 512;
    GLuint viewGLTexture;
    cudaGraphicsResource_t viewCudaResource;

    void initGLandCUDA() {
         int argc = 0;
         char** argv = NULL;
         glutInit(&argc, argv);
         glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
         glutInitWindowSize(window_width, window_height);
         glutCreateWindow("CUDA GL Interop");

         glewInit();

         glEnable(GL_TEXTURE_2D);
         glGenTextures(1, &viewGLTexture);
         glBindTexture(GL_TEXTURE_2D, viewGLTexture);
         {
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
         }
         glBindTexture(GL_TEXTURE_2D, 0);

         cudaGLSetGLDevice(gpuGetMaxGflopsDeviceId());
         cudaGraphicsGLRegisterImage(&viewCudaResource, viewGLTexture, GL_TEXTURE_2D,     cudaGraphicsRegisterFlagsWriteDiscard);
    }    


    __global__ void renderingKernel(cudaSurfaceObject_t image) {
         unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
         unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;

         uchar4 color = make_uchar4(0.f, 0.f, 0.f, .3f);
         //if I write in 0, 0 and not x,y, the computer is not crashing, but there is no black pixel at 0,0
         surf2Dwrite(color, image, x, y, cudaBoundaryModeClamp);
    }


    void callCUDAKernel(cudaSurfaceObject_t image) {
         dim3 block(8, 1, 1);
         dim3 grid(8, 1, 1);
         renderingKernel<<< grid, block>>>(image);
    }

     void renderFrame() {
        cudaGraphicsMapResources(1, &viewCudaResource);
        {
            cudaArray_t viewCudaArray;
            checkCudaErrors(cudaGraphicsSubResourceGetMappedArray(&viewCudaArray, viewCudaResource, 0, 0));
            cudaResourceDesc viewCudaArrayResourceDesc;
            {
                viewCudaArrayResourceDesc.resType = cudaResourceTypeArray;
                viewCudaArrayResourceDesc.res.array.array = viewCudaArray;
            }
            cudaSurfaceObject_t viewCudaSurfaceObject;
            checkCudaErrors(cudaCreateSurfaceObject(&viewCudaSurfaceObject, &viewCudaArrayResourceDesc));

            callCUDAKernel(viewCudaSurfaceObject);

            checkCudaErrors(cudaDestroySurfaceObject(viewCudaSurfaceObject));
        }
        checkCudaErrors(cudaGraphicsUnmapResources(1, &viewCudaResource));
        checkCudaErrors(cudaStreamSynchronize(0));

        glBindTexture(GL_TEXTURE_2D, viewGLTexture);
        {
            glBegin(GL_QUADS);
            {
                 glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
                 glTexCoord2f(1.0f, 0.0f); glVertex2f(+1.0f, -1.0f);
                 glTexCoord2f(1.0f, 1.0f); glVertex2f(+1.0f, +1.0f);
                 glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, +1.0f);
            }
            glEnd();
         }
         glBindTexture(GL_TEXTURE_2D, 0);
         glFinish();
    }

    int main(int argc, char **argv)
    {
         initGLandCUDA();

        glutDisplayFunc(renderFrame);
        //glutKeyboardFunc(keyboard);
        //glutMouseFunc(mouse);
        glutMainLoop();
    }

这似乎是某种超出范围的错误,但我目前没有想法(顺便说一句,这是cc 3.0,运行到nVidia 650M)。

编辑: 通过崩溃我的意思是:崩溃。电脑死机。我无法移动鼠标,我必须重新启动。

是的,我查看了所有的例子,它们并不是我想要的。改变他们想要我希望得到这个问题的结果。如果在手册或其他任何可以帮助我的地方有任何其他帮助,我发现我不会打扰寻求帮助。你需要链接cuda_runtime和glut libs

1 个答案:

答案 0 :(得分:4)

以下是您的代码的工作版本。代码中的问题是:

  • 您的内核取决于使用512x512线程启动,但您只使用64x1线程启动。

  • 您的内核正在使用surf2Dwrite()写入未对齐的地址。

  • 您在OpenGL中设置了双缓冲,但您没有交换缓冲区。 (glutSwapBuffers())。

  • 您正在使用浮点数初始化uchar4

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#ifdef _WIN32
#  define WINDOWS_LEAN_AND_MEAN
#  define NOMINMAX
#  include <windows.h>
#endif

// OpenGL Graphics includes
#include <GL/glew.h>
#if defined (__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/freeglut.h>
#endif

#include <cuda_runtime.h>
#include <cuda_gl_interop.h>

#include <vector_types.h>
const unsigned int window_width  = 512;
const unsigned int window_height = 512;

GLuint viewGLTexture;
cudaGraphicsResource_t viewCudaResource;

#define check(ans) { _check((ans), __FILE__, __LINE__); }
inline void _check(cudaError_t code, char *file, int line)
{
  if (code != cudaSuccess) {
    fprintf(stderr,"CUDA Error: %s %s %d\n", cudaGetErrorString(code), file, line);
    exit(code);
  }
}

void initGLandCUDA() {
  int argc = 0;
  char** argv = NULL;
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA);
  glutInitWindowSize(window_width, window_height);
  glutCreateWindow("CUDA GL Interop");

  glewInit();

  glEnable(GL_TEXTURE_2D);
  glGenTextures(1, &viewGLTexture);
  glBindTexture(GL_TEXTURE_2D, viewGLTexture);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, window_width, window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  glBindTexture(GL_TEXTURE_2D, 0);

  check(cudaGLSetGLDevice(0));

  check(cudaGraphicsGLRegisterImage(&viewCudaResource, viewGLTexture, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard));
}    


__global__ void renderingKernel(cudaSurfaceObject_t image) {
  unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
  unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;

  uchar4 color = make_uchar4(x / 2, y / 2, 0, 127);
  surf2Dwrite(color, image, x * sizeof(color), y, cudaBoundaryModeClamp);
}


void callCUDAKernel(cudaSurfaceObject_t image) {
  dim3 block(256, 1, 1);
  dim3 grid(2, 512, 1);
  renderingKernel<<<grid, block>>>(image);
  check(cudaPeekAtLastError());
  check(cudaDeviceSynchronize());
}

void renderFrame() {
  check(cudaGraphicsMapResources(1, &viewCudaResource));

  cudaArray_t viewCudaArray;
  check(cudaGraphicsSubResourceGetMappedArray(&viewCudaArray, viewCudaResource, 0, 0));

  cudaResourceDesc viewCudaArrayResourceDesc;
  memset(&viewCudaArrayResourceDesc, 0, sizeof(viewCudaArrayResourceDesc));
  viewCudaArrayResourceDesc.resType = cudaResourceTypeArray;
  viewCudaArrayResourceDesc.res.array.array = viewCudaArray;

  cudaSurfaceObject_t viewCudaSurfaceObject;
  check(cudaCreateSurfaceObject(&viewCudaSurfaceObject, &viewCudaArrayResourceDesc));

  callCUDAKernel(viewCudaSurfaceObject);

  check(cudaDestroySurfaceObject(viewCudaSurfaceObject));

  check(cudaGraphicsUnmapResources(1, &viewCudaResource));

  check(cudaStreamSynchronize(0));

  glBindTexture(GL_TEXTURE_2D, viewGLTexture);
  {
    glBegin(GL_QUADS);
    {
      glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
      glTexCoord2f(1.0f, 0.0f); glVertex2f(+1.0f, -1.0f);
      glTexCoord2f(1.0f, 1.0f); glVertex2f(+1.0f, +1.0f);
      glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, +1.0f);
    }
    glEnd();
  }
  glBindTexture(GL_TEXTURE_2D, 0);
  glFinish();
}

int main(int argc, char **argv)
{
  initGLandCUDA();

  glutDisplayFunc(renderFrame);
  //glutKeyboardFunc(keyboard);
  //glutMouseFunc(mouse);
  glutMainLoop();
}

输出:

enter image description here