使用CUDA、SFML和OpenGL:纹理不显示在四边形上

3
使用各种在线教程/示例/文档/论坛,我已经输入了代码,以使CUDA能够操作OpenGL纹理,从而可以输出到屏幕上。我的显示方法是使用PBO和一个分配的uchar4数组的纹理图像。尽管我试图修复问题,该纹理仍无法显示在2D表面上。我似乎无法确定问题所在。
到目前为止,这些是我检查/执行的所有内容:我已经创建了一个PBO并在CUDA中注册它,调用了cudaGraphicsResourceGetMappedPointer和GPU函数调用之前和之后的取消映射等效函数,确保启用了2D_TEXTURE,对于不需要的任何值都会调用glDisable,并在不需要时释放了纹理/缓冲区。我还重置了SFML OpenGL状态,以防SFML是原因。还采用了方形纹理。我的OpenGL版本和CUDA版本适用于我使用的所有函数调用。
当我检查cudaErrors和OpenGL错误时,程序内部似乎没有任何错误。
我不确定这是否与调用有关:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

我的四元组似乎没有显示出来。

我主要从这个网站中获得灵感。 非常感谢!

这是我的代码:

Main.cpp

#include <GL/glew.h>
#include <windows.h>
#include <GL/GL.h>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include "GeneralTypedef.h"
#include "OpenGLTest.cuh"


int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(1024, 1024), "OpenGL");
    //window.setVerticalSyncEnabled(true);
    sf::Vector2u windowSize;

    windowSize = sf::Vector2u(window.getSize());

    bool running = true;
    glewInit();
    window.resetGLStates();
    std::printf("OpenGL: %s:", glGetString(GL_VERSION));
    // We will not be using SFML's gl states.

    OpenGLTest* test = new OpenGLTest(window.getSize());

    sf::Time time;

    while (running)
    {
        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
                windowSize = window.getSize();
            }

        }

        // clear the buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        test->createFrame(time.asMicroseconds());
        test->drawFrame();
        window.display();
    }

    // release resources...
    delete test;

    return 0;
}

OpenGLTest.cuh

#ifndef OPENGLTEST_CUH
#define OPENGLTEST_CUH

#include <GL/glew.h>
#include <windows.h>
#include <GL/GL.h>

#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

#include "GeneralTypedef.h"

class OpenGLTest
{
    public:
        uchar4* image;
        GLuint gltexture;
        GLuint pbo;
        cudaGraphicsResource_t cudaPBO;
        uchar4* d_textureBufferData;

        sf::Vector2u windowSize;

        OpenGLTest(sf::Vector2u windowSize)
        {
            this->windowSize = sf::Vector2u(windowSize);
            this->setupOpenGL();
        };

        ~OpenGLTest()
        {
            delete image;
            image == nullptr;
            cudaFree(d_textureBufferData);
            d_textureBufferData == nullptr;
            glDeleteTextures(1, &gltexture);
        }

        void drawFrame();
        void createFrame(float time);
    private:
        void setupOpenGL();
};
#endif //OPENGLTEST_CUH

OpenGLTest.cu

#include "OpenGLTest.cuh"

__global__ void createGPUTexture(uchar4* d_texture)
{
    uint pixelID = blockIdx.x*blockDim.x + threadIdx.x;
    d_texture[pixelID].x = 0;
    d_texture[pixelID].y = 1;
    d_texture[pixelID].z =  1;
    d_texture[pixelID].w = 0;
}
__global__ void wow(uchar4* pos, unsigned int width, unsigned int height,
    float time)
{
    int index = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int x = index%width;
    unsigned int y = index / width;

    if (index < width*height) {
        unsigned char r = (x + (int)time) & 0xff;
        unsigned char g = (y + (int)time) & 0xff;
        unsigned char b = ((x + y) + (int)time) & 0xff;

        // Each thread writes one pixel location in the texture (textel)
        pos[index].w = 0;
        pos[index].x = r;
        pos[index].y = g;
        pos[index].z = b;
    }
}
void OpenGLTest::drawFrame()
{
    glColor3f(1.0f,1.0f,1.0f);

    glBindTexture(GL_TEXTURE_2D, gltexture);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, windowSize.x, windowSize.y, GL_RGBA, GL_UNSIGNED_BYTE, 0);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex2f(0.0f, float(windowSize.y));
    glTexCoord2f(1.0f, 0.0f);
    glVertex2f(float(windowSize.x), float(windowSize.y));
    glTexCoord2f(1.0f, 1.0f);
    glVertex2f(float(windowSize.x), 0.0f);
    glTexCoord2f(0.0f,1.0f);
    glVertex2f(0.0f, 0.0f);
    glEnd();

    glFlush();

    // Release
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
    glBindTexture(GL_TEXTURE_2D, 0);

    // Test Triangle
    /*
    glBegin(GL_TRIANGLES);
    glColor3f(0.1, 0.2, 0.3);
    glVertex2f(0, 0);
    glVertex2f(10, 0);
    glVertex2f(0, 100);
    glEnd();
    */
}

void OpenGLTest::createFrame(float time)
{
    cudaGraphicsMapResources(1, &cudaPBO, 0);
    size_t numBytes;
    cudaGraphicsResourceGetMappedPointer((void**)&d_textureBufferData, &numBytes, cudaPBO);

    int totalThreads = windowSize.x * windowSize.y;
    int nBlocks = totalThreads/ 256;

    // Run code here.
    createGPUTexture << <nBlocks,  256>> >(d_textureBufferData);
    //wow << <nBlocks, 256 >> >(d_textureBufferData, windowSize.x, windowSize.y, time);
    // Unmap mapping to PBO so that OpenGL can access.
    cudaGraphicsUnmapResources(1, &cudaPBO, 0);
}

void OpenGLTest::setupOpenGL()
{
    image  = new uchar4[1024*1024];

    glViewport(0, 0, windowSize.x, windowSize.y);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, windowSize.x, windowSize.y, 0.0, -1.0, 1.0);

    glEnable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);
    glDisable(GL_DEPTH_TEST);

    // Unbind any textures from previous.
    glBindTexture(GL_TEXTURE_2D, 0);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);

    // Create new textures.
    glGenTextures(1, &gltexture);
    glBindTexture(GL_TEXTURE_2D, gltexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    // Create image with same resolution as window.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, windowSize.x , windowSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);


    // Create pixel buffer boject.
    glGenBuffers(1, &pbo);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
    glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, windowSize.x * windowSize.y * sizeof(uchar4), image, GL_STREAM_COPY);

    cudaGraphicsGLRegisterBuffer(&cudaPBO, pbo, cudaGraphicsMapFlagsNone);

    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

通用类型

#ifndef GENERALTYPEDEF_CUH
#define GENERALTYPEDEF_CUH
typedef unsigned int uint;

#endif // GENERALTYPEDEF_CUH
1个回答

4

在重写整个代码并更好地理解它之后,我已经找到了原因。内核函数中uchar4的颜色分量映射为0-255。w分量是透明度。因此,应将其映射为255以显示图像。希望这对那些可能遇到相同问题的人有所帮助。一些网站也将此值设置得非常低。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接