c ++ cuda opengl无法渲染vbo

时间:2019-05-11 22:34:31

标签: c++ opengl cuda vbo opengl-compat

我正在尝试在屏幕上绘制一堆点。我正在使用CUDA生成数据(位置和颜色),并使用OpenGL绘制数据。我试图让CUDA更新VBO,然后用OpenGL绘制它,但是出现黑屏。我不确定CUDA是否无法更新缓冲区,或者缓冲区绘制不正确。我的GPU是GTX 1080,我正在尝试使用OpenGL 4.0。颜色也由CUDA指定。如果我的问题是我需要一个着色器,我该如何添加它,同时还要通过CUDA指定颜色?

更新:问题似乎是openGL。更新了代码以使用三角形,因此添加了新问题。为什么我的VBO无法渲染?

代码如下:

GPUmain.cuh:

#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/remove.h>
#include <curand.h>
#include <GL/glew.h>
#include <SDL_opengl.h>
#include <cuda_gl_interop.h>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

//ver: x, y, z, r, g, b, a
struct ver {

    // x, y, z pos
    GLuint x, y, z;

    // r, g, b, a color
    GLubyte r, g, b, a;

};

class GPU {

public:



    static int nParticles;

    static GLuint vboid;

    static cudaGraphicsResource *CGR;

    //collection of vertices to be simulated and rendered
    static thrust::device_vector<ver> rverts;

    static void init(int w, int h);

    static void compute();

    static void render();

    static void GPUmain();

    static void free();

};

GPUmain.cu:

#include "GPUmain.cuh"

__global__ void uploadVerts(ver *vv, ver *vb) {
    int id = threadIdx.x + (blockDim.x * blockIdx.x);
    vb[id] = vv[id];
    vb[id].x = vv[id].x;
    vb[id].y = vv[id].y;
    vb[id].z = vv[id].z;
    vb[id].r = vv[id].r;
    vb[id].g = vv[id].g;
    vb[id].b = vv[id].b;
    vb[id].a = vv[id].a;
}

__global__ void genGrid(ver *v) {
    int i = threadIdx.x + (blockDim.x * blockIdx.x);
    float x = (float)(i % ((int)1080));
    float y = (float)(i / ((int)1920));

    v[i].x = x;
    v[i].y = y;
    v[i].z = 1;

    v[i].r = 255;
    v[i].g = 0;
    v[i].b = 0;
    v[i].a = 0;
}

int GPU::nParticles;

GLuint GPU::vboid;

cudaGraphicsResource *GPU::CGR;

//collection of vertices to be simulated and rendered
thrust::device_vector<ver> GPU::rverts;

void GPU::init(int w, int h)
{   
    nParticles = w * h;
    /*rverts.resize(nParticles, ver{0,0,0,0,0,0,0});
    genGrid<<<nParticles/1024,1024>>>(thrust::raw_pointer_cast(&rverts[0]));*/

    ver e[3] = { 
        ver{1024,200,2,255,0,0,255},
        ver{499,288,173,0,255,0,255},
        ver{462,1674,8,0,0,255,255} 
    };

    glGenBuffers(1,&vboid);
    glBindBuffer(GL_ARRAY_BUFFER,vboid);
    glBufferData(GL_ARRAY_BUFFER,3*sizeof(ver),e,GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    /*cudaGraphicsGLRegisterBuffer(&CGR,vboid,cudaGraphicsMapFlagsWriteDiscard);*/


}

void GPU::compute()
{

}

void GPU::render()
{

/*ver *verts;
size_t size;
cudaGraphicsMapResources(1, &CGR, 0);
cudaGraphicsResourceGetMappedPointer((void**)&verts, &size, CGR);
uploadVerts<<<nParticles/1024, 1024>>>(thrust::raw_pointer_cast(&rverts[0]), verts);
cudaGraphicsUnmapResources(1, &CGR, 0);
cudaDeviceSynchronize();*/

glClearColor(0, 0, 0, 0); // we clear the screen with black (else, frames would overlay...)
glClear(GL_COLOR_BUFFER_BIT); // clear the buffer

glBindBuffer(GL_ARRAY_BUFFER, vboid);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3, GL_INT, 4 * sizeof(GLubyte), 0);
glColorPointer(4, GL_BYTE, 3 * sizeof(GLuint), BUFFER_OFFSET(3 * sizeof(GLuint)));

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void GPU::GPUmain()
{

    compute();

    render();

}

void GPU::free()
{
    cudaGraphicsUnregisterResource(CGR);
    glBindBuffer(GL_ARRAY_BUFFER, vboid);
    glDeleteBuffers(1, &vboid);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    rverts.clear();
    thrust::device_vector<ver>().swap(rverts);
}

window.cpp的相关部分(包含OpenGL代码):

bool Window::init()
{
    //initialize SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {

        log << "Failed to initialize SDL!\n";
        return false;

    }

    //set window atributes
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);


    //creat window
    window = SDL_CreateWindow(
        name.c_str(),
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        width,
        height,
        SDL_WINDOW_OPENGL

    );

    //create opengl context in the window
    glcontext = SDL_GL_CreateContext(window);

    SDL_GL_SetSwapInterval(1);

    //check if the window was created
    if (window == nullptr) {

        log << "Failed to create window!\n";
        return false;

    }

    //turn on experimental features
    glewExperimental = GL_TRUE;

    //initiallize glew
    if (glewInit() != GLEW_OK) {

        log << "Failed to Init GLEW";

        return false;

    }



    //set drawing parameters
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, 0, height, 0, 255);
    glPointSize(1);
    glEnable(GL_BLEND);                                // Allow Transparency
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  // how transparency acts

    std::cout << sizeof(ver);

    GPU::init(width, height);

    return true;
}

void Window::renderFrame()
{

    GPU::render();

    SDL_GL_SwapWindow(window); //swap buffers
}

1 个答案:

答案 0 :(得分:1)

如果您使用固定功能属性和客户端功能,则必须使用兼容性配置文件上下文。
请参见Fixed Function PipelineLegacy OpenGL。 如果要使用核心配置文件,则必须使用Vertex Array ObjectShader

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);

以下几何形状

ver e[3] = { 
     //     x    y    z      r    g    b    a       
     ver{1024, 200,   2,   255,   0,   0, 255},
     ver{ 499, 288, 173,     0, 255,   0, 255},
     ver{462,  1674,  8,     0,   0, 255, 255} 
};

被正交投影的近平面裁剪。请注意,在视图空间中,z轴指向视口之外。
更改正交投影(或反转几何的z坐标):

glOrtho(0, width, 0, height, 0, 255);

glOrtho(0, width, 0, height, -255, 0);

glVertexPointerglColorPointer的步幅参数是连续属性之间的偏移量。因此必须为sizeof(ver)
颜色属性的类型为GL_UNSIGNED_BYTE,而不是GL_BYTE

glVertexPointer(3, GL_INT, 4 * sizeof(GLubyte), 0);
glColorPointer(4, GL_BYTE, 3 * sizeof(GLuint), BUFFER_OFFSET(3 * sizeof(GLuint)));

glVertexPointer(3, GL_INT, sizeof(ver), 0);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ver), BUFFER_OFFSET(3 * sizeof(GLuint)));