【问题标题】:How to use Cuda NPP(NVIDIA Performance Primitives) on openGL textures如何在 openGL 纹理上使用 Cuda NPP(NVIDIA 性能基元)
【发布时间】:2012-09-10 22:36:35
【问题描述】:

我目前正在开发一个需要进行大量视频处理的项目。该项目使用 C++、openGL 和 glsl。最近我向它添加了一些 CUDA 例程,以进行一些更高级的图像处理工作。我设法在 cuda 中使用 openGL 纹理,但是现在我正在尝试使用带有 openGL 纹理作为输入的一些 cuda 的 npp 函数(没有成功)。

例如,我想计算均值和标准差。预处理输入openGL纹理的偏差:

//I first connect the cudaGraphicsResource to a cudaArray 
//cudaGraphicsResource_t inputImage is input passed from openGL framework
cudaGraphicsMapResources(1,&inputImage);
cudaArray* inputImgArray = NULL;
cudaGraphicsSubResourceGetMappedArray(&inputArray, inputImage,0,0);

//Next I do some preparationwork and cast the cudaArray to a Npp8u* and give
//it as an input source imagebuffer to the npp routine.
NppiSize roi={imgwidth, imgheight};
int bufferSize = 0;
nppiMeanStdDev8uC1RGetBufferHostSize(roi,bufferSize);
double mean = 0;
double stdDev = 0;
Npp8u* scratch =  nppsMalloc_8f(bufferSize);
int stepSize = imgWidth * sizeof(unsigned char);
NppStatus status = nppiMean_stdDev_8u_C1R((Npp8u*)inputImgArray, stepSize, roi, scratch,&mean,&stdDev);

//cleanup
nppsFree(bufferSize);
cudaGraphicsUnmapresources(1,&inputImage);

nppiMean_stdDev_8u_C1R 函数总是返回错误代码:NPP_MEMCPY_ERROR。 我到处查看手册,但找不到这是否是使用 NPP 和 opengl 纹理的正确方法。我在互联网上也找不到任何东西。我不敢相信我是第一个试图用 NPP 做这些事情的人。也许我只是错过了一份重要文件中的一个重要章节:-)。

【问题讨论】:

    标签: opengl cuda textures


    【解决方案1】:

    您需要将设备指针传递给 NPP,而不是 CUDA 数组。所以在你的情况下,我认为你想使用cudaGraphicsResourceGetMappedPointer 而不是cudaGraphicsSubResourceGetMappedArray

    例如:(免责声明:此代码是在浏览器中编写的,未经测试,未经验证。)

    //I first connect the cudaGraphicsResource to a cudaArray 
    //cudaGraphicsResource_t inputImage is input passed from openGL framework
    cudaGraphicsMapResources(1,&inputImage);
    unsigned char* inputPtr = NULL; // to hold device pointer to input image
    size_t inputSize;
    cudaGraphicsResourceGetMappedPointer((void**)&inputPtr, &inputSize, inputImage);
    
    //Next I do some preparation work and cast the device pointer to a Npp8u* and give
    //it as an input source imagebuffer to the npp routine.
    NppiSize roi={imgwidth, imgheight};
    int bufferSize = 0;
    nppiMeanStdDev8uC1RGetBufferHostSize(roi, &bufferSize); // note & missing from original!
    double mean = 0;
    double stdDev = 0;
    Npp8u* scratch =  nppsMalloc_8f(bufferSize);
    int stepSize = imgWidth * sizeof(unsigned char);
    NppStatus status = nppiMean_stdDev_8u_C1R((Npp8u*)inputPtr, stepSize, roi, scratch, &mean, &stdDev);
    
    //cleanup
    nppsFree(bufferSize);
    cudaGraphicsUnmapresources(1,&inputImage);
    

    【讨论】:

    • 感谢您的快速回复。但是,我尝试了它并得到了一个 cudaUnknownError。来自 cudaGraphicsResourceGetMappedPointer() 的 CUDA 参考指南条目:如果资源不是缓冲区,则无法通过指针访问它并返回 cudaErrorUnknown。来自 cudaGraphicsSubResourceGetMappedArray() 的 CUDA 参考指南条目:如果资源不是纹理,则无法通过数组访问它并返回 cudaErrorUnknown。我必须对纹理对象使用 GetMappedArray。
    • 很抱歉,我没有意识到您正在使用纹理对象。您需要首先将纹理数据放入 GL 端的像素缓冲区对象中。如果要渲染此数据,可以将其渲染为 PBO,然后将其映射到 CUDA 以使用 NPP。
    猜你喜欢
    • 2012-06-02
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多