Skip to content

Instantly share code, notes, and snippets.

@ofTheo
Created September 17, 2014 17:07
Show Gist options
  • Save ofTheo/44e306f88463a3395170 to your computer and use it in GitHub Desktop.
Save ofTheo/44e306f88463a3395170 to your computer and use it in GitHub Desktop.
WIP threaded FBO read back - requires GLFW
//
// ThreadedFBOReadback.cpp
//
// Created by Theodore Watson on 9/16/14.
//
//
#include "ThreadedFBOReadback.h"
#include "ofAppGLFWWindow.h"
GLFWwindow * gpuWindow = NULL;
GLFWwindow * mainWindow = NULL;
ThreadedFBOReadback::ThreadedFBOReadback(){
bNeedsToRun = false;
width = height = 0;
}
void ThreadedFBOReadback::setup( int _width, int _height, int glType, int numSamples){
width = _width;
height = _height;
mainWindow = glfwGetCurrentContext();
renderFbo.allocate(width, height, glType, numSamples);
}
void ThreadedFBOReadback::begin(){
if( bNeedsToRun == false ){
renderFbo.begin();
}
}
void ThreadedFBOReadback::end(){
if( bNeedsToRun == false ){
renderFbo.end();
startRender();
}
}
float ThreadedFBOReadback::getWidth(){
return width;
}
float ThreadedFBOReadback::getHeight(){
return height;
}
void ThreadedFBOReadback::startRender(){
bNeedsToRun = true;
if( isThreadRunning() == false ){
startThread();
}
}
void ThreadedFBOReadback::stopRender(){
bNeedsToRun = false;
}
void ThreadedFBOReadback::threadedFunction(){
while( ofThread::isThreadRunning() ){
if(bNeedsToRun && renderFbo.isAllocated() ){
if( gpuWindow == NULL ){
glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
gpuWindow = glfwCreateWindow(width, height, "HiddenWindowForSecondContext", 0, mainWindow);
}
//this tells this thread to use this context
//shouldn't have to do this for the main thread as that doesn't get unset by this call.
glfwMakeContextCurrent(gpuWindow);
renderFbo.readToPixels(outPixels);
stopRender();
}
yield();
}
}
ofPixels ThreadedFBOReadback::getPixels(){
return outPixels;
}
//
// ThreadedFBOReadback.h
//
// Created by Theodore Watson on 9/16/14.
//
//
#pragma once
#include "ofMain.h"
class ThreadedFBOReadback : public ofThread{
public:
ThreadedFBOReadback();
//setup your FBO
//using an internal FBO for now as I thought some crashes were due to simultanous use of same fbo.
//probably cleaner to allow passing in fbo.
void setup( int width, int height, int glType, int numSamples );
//use to capture contents into fbo
//on end() the pixels are downloaded into a second thread.
//NOTE: right now you can't call begin until threaded readback is finished.
void begin();
void end();
float getWidth();
float getHeight();
//this is dumb for now - needs a isFrameNew type flag
//not doing it for now as trying to avoid needing to lock
ofPixels getPixels();
protected:
void threadedFunction();
void startRender();
void stopRender();
int width, height;
bool bNeedsToRun;
ofPixels outPixels;
ofFbo renderFbo;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment