Search code examples
c++openglcudavboopengl-compat

c++ cuda opengl not rendering vbo


I am trying to draw a bunch of points on the screen. I'm using CUDA to generate the data (position and color), and OpenGL to draw it. I am trying to get CUDA to update a VBO and then OpenGL to draw it, but I get a blank screen. I am not sure if CUDA is not able to update the buffer, or that the buffer is not drawing properly. My GPU is a GTX 1080, and I'm trying to use OpenGL 4.0. Colors are specified by CUDA as well. If my problem is that I need a shader, how do I add that, but also still specify the color through CUDA?

UPDATE: problem seems to be openGL. Updated code to use triangle So new question to add. Why is my VBO not being rendered?

Here is the code:

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);
}

The relevant (that contain OpenGL code) parts of window.cpp:

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
}

Solution

  • If you use the fixed-function attributes and client side capabilities, then you've to use a compatibility profile context.
    See Fixed Function Pipeline and Legacy OpenGL. If you want to use a core profile, then you've to use Vertex Array Object and Shader:

    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);
    

    The following geometry

    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} 
    };
    

    is clipped by the near plane of the orthographic projection. Note, in view space the z-axis points out of the viewport.
    Change the orthographic projection (or invert the z coordinates of the geometry):

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

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

    The stride parameter of glVertexPointer respectively glColorPointer is the offset between consecutive attributes. So it has to be sizeof(ver).
    The type of the color attributes is GL_UNSIGNED_BYTE rather than 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)));