vendredi 19 août 2016

OpenGL multiple threads rendering, compute in display interval

I need images to be displayed in a constant frame rate, so I use two threads, one for rendering with VSYNC on, one for computing using CUDA, which may takes long time. I want computing thread running in rendering thread interval(after swap buffer, before next frame start rendering).

I have two problems here:

  1. How can I know when the image are exactly drawn on screen, then I can awake rending thread. After glutSwapBuffers(), the image may not be actually displayed on screen. I have not found a API to notify display completing.
  2. How can I stop computing thread when it's time to render, I have tried this_thread::yield() but it often still runs computing thread. I am not familiar with multiple threads programming.

I use C++11 for multiple thread, CUDA for computing, OpenGL for rendering, More specific code here:

compute Thread:

void update(){
    while(1){
        lock_guard<mutex> guard(buffer_mutex);
        runSolver(d_x);// compute next several images, d_x point to the images buffer
        updateFlag = ture;
        this_thread::yield();
    }

}

rendering thread:

void render(){
    initGL();
    glutMainLoop();
}
void display(){
    if(updateFlag){
        lock_guard<mutex> guard(buffer_mutex);
        updateBuffer(d_x);
        updateFlag = false;
    }
    .../* OpenGL rendering*/
    glutSwapBuffers();
}

Aucun commentaire:

Enregistrer un commentaire