openframeworks / openframeworks/openFrameworks

faster fbo readback

Open
#7,111 18 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
10.4k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
9

Description

was looking at integrating ofxFastFboReader approach into ofFbo but after doing some benchmarking I found a faster approach with what we have already.

  • on desktop the current read back for ofFbo uses ofTexture::readToPixels and gives:
    approx 30fps for a 4096x4096 GL_RGB FBO with 0 samples
//----------------------------------------------------------
void ofFbo::readToPixels(ofPixels & pixels, int attachmentPoint){
	if(!bIsAllocated) return;
#ifndef TARGET_OPENGLES
	getTexture(attachmentPoint).readToPixels(pixels); <-- desktop is currently using this approach 
#else
  • with ofxFastFboReader addon which has always seemed the fastest approach:
    approx 33fps for a 4096x4096 GL_RGB FBO with 0 samples

However while trying some different approaches I found that

  • using the code that OPENGLES uses in OF
    approx 40fps for a 4096x4096 GL_RGB FBO with 0 samples <-- winner by 10fps
//----------------------------------------------------------
void ofFbo::readToPixels(ofPixels & pixels, int attachmentPoint){
	if(!bIsAllocated) return;
//#ifndef TARGET_OPENGLES
//	getTexture(attachmentPoint).readToPixels(pixels);
//#else
        //this code is faster on desktop than above, but it doesn't support multisampling 
	pixels.allocate(settings.width,settings.height,ofGetImageTypeFromGLType(settings.internalformat));
	bind();
	int format = ofGetGLFormatFromInternal(settings.internalformat);
	glReadPixels(0,0,settings.width, settings.height, format, GL_UNSIGNED_BYTE, pixels.getData());
	unbind();
//#endif
}

When we do multisampling the OPENGLES code stops working.

It can be fixed by blitting and is still faster than the current approach by 5fps.
But the added complexity may not be worth it.

Here is the multisample friendly version:

//----------------------------------------------------------
void ofFbo::readToPixels(ofPixels & pixels, int attachmentPoint){
	if(!bIsAllocated) return;
//#ifndef TARGET_OPENGLES
//	getTexture(attachmentPoint).readToPixels(pixels);
//#else
	if(settings.numSamples > 0){
	
		//we need a non multisample fbo to blit the multisamped fbo to.
		if( !nonMultiSampleFbo ){
			nonMultiSampleFbo = std::make_shared<ofFbo>();
			nonMultiSampleFbo->allocate(settings.width,settings.height,settings.internalformat,0);
		}
		
		//blit the multisample fbo to non multisample fbo
		glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fbo);
		glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, nonMultiSampleFbo->getId());

		glBlitFramebufferEXT(0, 0, settings.width,settings.height, 0, 0, settings.width,settings.height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
		
		//do the normal appproach for reading fbo to pixels with the non multisample one
		pixels.allocate(settings.width,settings.height,ofGetImageTypeFromGLType(settings.internalformat));

		nonMultiSampleFbo->bind();
		int format = ofGetGLFormatFromInternal(settings.internalformat);
		glReadPixels(0,0,settings.width, settings.height, format, GL_UNSIGNED_BYTE, pixels.getData());
		nonMultiSampleFbo->unbind();
		
	}else{
		pixels.allocate(settings.width,settings.height,ofGetImageTypeFromGLType(settings.internalformat));
		bind();
		int format = ofGetGLFormatFromInternal(settings.internalformat);
		glReadPixels(0,0,settings.width, settings.height, format, GL_UNSIGNED_BYTE, pixels.getData());
		unbind();
	}
//#endif
}

Proposal: I am thinking to use the current approach for multisample and the TARGET_OPENGLES approach when numSamples = 0. That way we use the existing code but get a 10fps boost for non multisampled FBO capture.

However if we do implement the above approach we would get multisample read back working on iOS / Android when I think it is currently broken.

Thoughts?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at ofFbo::readToPixels and compare the existing texture readback path with the TARGET_OPENGLES glReadPixels path and the multisample blit example in the issue. Check behavior for zero-sample and multisample FBOs across desktop, iOS, and Android. Done means faster non-multisample readback without regressing multisample support, validated with the stated benchmark scenarios.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, cpp, ios
Domain
computer-graphics, mobile-dev, performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.