ofFbo best practices...

Hello everyone,

I find myself doing a fair amount of drawing into FBOs and was wondering if anyone had a general “do this-don’t do that” set of guidelines or rules for optimization purposes. I find myself often effectively drawing to an FBO inside of an update method, for example.

Perhaps more context will help with the discussion:

I’ve been using the ofxFastFboReader addon for saving FBOs out. I’ve encountered a problem with the addon (which I added to the author’s git repository) but the source code I wrote for the example describes how I typically would draw to an FBO.

https://gist.github.com/jkosoy/4972178

I’d love to get a feel for if my approach is dangerous or slow and if anyone had any additional thoughts in general that’d just be wonderful.

Thanks!

allocating/deallocation is fairly expensive stuff so having one ofFbo declared in your class, doing the allocation once in setup() and reusing it in saveImage() will speed you up.

https://gist.github.com/jkosoy/4972178#file-testapp-cpp-L49

Awesome, thanks for the tip.

Is there anything I should be concerned about with the larger issue of calling saveImage in the update method? Just want to make sure I’m not “doing it wrong.”

Since you’re saving your image, the reading to pixels that you’re doing in saveImage() is unavoidable, but that is a very slow operation (comparatively) because you need to copy data from the graphics card to the CPU memory space, which is always expensive. Other than avoiding that unless it’s absolutely necessary (for instance in saving to the hard drive), I don’t see anything that looks too wrong in there.

You could also check out some convenience methods in OF:

  
  
void ofSaveScreen(string filename);  
void ofSaveViewport(string filename);  
void ofSaveFrame(bool bUseViewport);  
  

They probably won’t be as fast, since they are doing allocation every time they are called, as @jvcleave mentioned.

Everything here makes total sense. Appreciate the feedback guys.