Memory junk piling up in FBO, what is really happening?

Hi everyone,

I’m currently learning about FBO / how to use it and decided to do a couple tests. In one test, I am drawing to FBO with the mouse and displaying a circle trail. Like in the photo :

So pretty straightforward stuff. Now, to make that work I had to wrap fbo.begin() and fbo.end() around ofClear() in the setup. Like this :

void ofApp::setup(){   
fbo.allocate(ofGetWidth(), ofGetHeight(), GL_RGB);
fbo.begin();
ofClear(255, 0, 0);
fbo.end();
}

Otherwise… I got this crazy things :

So clearly the functions in the setup() clear the buffer. But what are those graphics in the window ? Are they bits of memory stored in the video card ? I’m looking for a simple explanation of what it is I’m seeing or perhaps an insight on how memory and FBO work together.

My code (with ofFbo fbo in the header) :

void ofApp::setup(){
fbo.allocate(ofGetWidth(), ofGetHeight(), GL_RGB);  
fbo.begin();
ofClear(255, 0, 0);
fbo.end();
}

void ofApp::draw(){

int x = ofGetMouseX();
int y = ofGetMouseY();

fbo.begin();
ofSetColor(255);
ofNoFill();
ofDrawCircle(x, y, 100);
fbo.end();
fbo.draw(0, 0, ofGetWidth(), ofGetHeight());
}

Many thanks for your help ! :wink:

Regarding the documentation, it is a standard behavior :

Often the FBO will contain artefacts from the memory that the graphics card has just allocated for it, so it’s good to clear it before starting to draw it:

fbo.begin();
ofClear(255,255,255, 0);
fbo.end();

When any application requests GPU memory space to the GPU this block gets assigned to this app and no other can touch it or access it; this app becomes the owner of this memory space. Once this memory gets released it is simply left as ownerless but it is not cleared, so whatever it was holding will remain in there.
So when you allocate a new memory section (like when you use fbo.allocate(…) ) your app gets assigned the needed memory block out of the ownerless memory, so whatever the previous owner had here will become “available” to your app. Here the ofClear function comes in to clean all that junk.
Seeing this from a different perspective, you can take advantage of this behavior to make some nice glitchy graphics :slight_smile:
Open a lot of windows in your internet browser with different pages, then close all and run your ofApp without clearing the fbo. See what happens.

best

4 Likes

@Poulp The documentation described the behavior a bit too vaguely for me. Or at least I didn’t fully understand what it meant by ‘artifacts’. But it’s all clear now :slight_smile: thanks for the input !

@roymacdonald Thank you for the great explanation ! It’s crystal clear now :wink: