Skip to content

Commit c50646a

Browse files
committed
ofFbo: report a forgotten end() when the frame ends
finishRender() cleared the framebuffer stack every frame, so a later end() logged "unbalanced fbo bind/unbind" even when the window was already bound. Warn at the end of the frame if an ofFbo is still bound, and keep a matching begin()/end() quiet. A begin() that binds an ofFbo onto itself still pushes, so its end() pops the same entry. Both GL renderers share the stack rules.
1 parent 0ed9987 commit c50646a

6 files changed

Lines changed: 231 additions & 46 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#pragma once
2+
3+
#include <deque>
4+
5+
// Tracks which framebuffer ofFbo::begin()/end() should return to.
6+
// startRender() pushes the window id and that entry stays until finishRender().
7+
// A matching begin/end inside the frame pushes and pops above it. finishRender()
8+
// warns when an ofFbo is still bound, then binds the window again so a later
9+
// end() does not report that as an unbalanced pair.
10+
11+
enum class ofFramebufferBindWarning {
12+
None,
13+
NoMatchingBegin,
14+
LeftBound,
15+
BoundOntoSelf
16+
};
17+
18+
template <class Id>
19+
struct ofFramebufferBindStep {
20+
Id framebufferId{};
21+
bool bind = false;
22+
ofFramebufferBindWarning warning = ofFramebufferBindWarning::None;
23+
};
24+
25+
template <class Id>
26+
ofFramebufferBindStep<Id> ofFramebufferStartRender(std::deque<Id> & stack, Id & current, Id defaultId) {
27+
current = defaultId;
28+
stack.push_back(defaultId);
29+
return {defaultId, true, ofFramebufferBindWarning::None};
30+
}
31+
32+
template <class Id>
33+
ofFramebufferBindStep<Id> ofFramebufferFinishRender(std::deque<Id> & stack, Id & current, Id defaultId) {
34+
const ofFramebufferBindWarning warning = (stack.size() != 1 || current != defaultId)
35+
? ofFramebufferBindWarning::LeftBound
36+
: ofFramebufferBindWarning::None;
37+
stack.clear();
38+
if (current != defaultId) {
39+
current = defaultId;
40+
return {defaultId, true, warning};
41+
}
42+
return {defaultId, false, warning};
43+
}
44+
45+
template <class Id>
46+
ofFramebufferBindStep<Id> ofFramebufferPush(std::deque<Id> & stack, Id & current, Id fboId) {
47+
const ofFramebufferBindWarning warning = (current == fboId)
48+
? ofFramebufferBindWarning::BoundOntoSelf
49+
: ofFramebufferBindWarning::None;
50+
stack.push_back(current);
51+
current = fboId;
52+
return {fboId, true, warning};
53+
}
54+
55+
template <class Id>
56+
ofFramebufferBindStep<Id> ofFramebufferPop(std::deque<Id> & stack, Id & current, Id defaultId) {
57+
if (stack.empty()) {
58+
const ofFramebufferBindWarning warning = (current != defaultId)
59+
? ofFramebufferBindWarning::NoMatchingBegin
60+
: ofFramebufferBindWarning::None;
61+
current = defaultId;
62+
return {defaultId, true, warning};
63+
}
64+
if (stack.size() == 1 && current == stack.back()) {
65+
return {current, true, ofFramebufferBindWarning::NoMatchingBegin};
66+
}
67+
current = stack.back();
68+
stack.pop_back();
69+
return {current, true, ofFramebufferBindWarning::None};
70+
}

‎libs/openFrameworks/gl/ofGLProgrammableRenderer.cpp‎

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ofCamera.h"
44
#include "ofFbo.h"
55
#include "ofGLProgrammableRenderer.h"
6+
#include "ofFramebufferBinding.h"
67
#include "ofGLUtils.h"
78
#include "ofImage.h"
89
#include "ofLight.h"
@@ -95,8 +96,8 @@ ofGLProgrammableRenderer::ofGLProgrammableRenderer(const ofAppBaseWindow * _wind
9596

9697
//----------------------------------------------------------
9798
void ofGLProgrammableRenderer::startRender() {
98-
currentFramebufferId = defaultFramebufferId;
99-
framebufferIdStack.push_back(defaultFramebufferId);
99+
ofFramebufferStartRender(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
100+
glBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
100101
matrixStack.setRenderSurface(*window);
101102
beginDefaultShader();
102103
viewport();
@@ -121,7 +122,14 @@ void ofGLProgrammableRenderer::finishRender() {
121122
if (!usingCustomShader) currentShader = nullptr;
122123
}
123124
matrixStack.clearStacks();
124-
framebufferIdStack.clear();
125+
const GLuint leftBound = currentFramebufferId;
126+
const auto framebufferStep = ofFramebufferFinishRender(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
127+
if (framebufferStep.warning == ofFramebufferBindWarning::LeftBound) {
128+
ofLogWarning("ofFbo") << "left bound at the end of the frame (id " << leftBound << ")";
129+
}
130+
if (framebufferStep.bind) {
131+
glBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
132+
}
125133
mDrawMode = GL_TRIANGLES;
126134
mUniformsTex.clear();
127135
}
@@ -1609,33 +1617,23 @@ void ofGLProgrammableRenderer::end(const ofFbo & fbo) {
16091617

16101618
//----------------------------------------------------------
16111619
void ofGLProgrammableRenderer::bind(const ofFbo & fbo) {
1612-
if (currentFramebufferId == fbo.getId()) {
1620+
const auto step = ofFramebufferPush(framebufferIdStack, currentFramebufferId, fbo.getId());
1621+
if (step.warning == ofFramebufferBindWarning::BoundOntoSelf) {
16131622
ofLogWarning() << "Framebuffer with id: " << fbo.getId() << " cannot be bound onto itself. \n"
16141623
<< "Most probably you forgot to end() the current framebuffer before calling begin() again or you forgot to allocate() before calling begin().";
1615-
return;
16161624
}
1617-
// this method could just as well have been placed in ofBaseGLRenderer
1618-
// and shared over both programmable and fixed function renderer.
1619-
// I'm keeping it here, so that if we want to do more fancyful
1620-
// named framebuffers with GL 4.5+, we can have
1621-
// different implementations.
1622-
framebufferIdStack.push_back(currentFramebufferId);
1623-
currentFramebufferId = fbo.getId();
16241625
glBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
16251626
}
16261627

16271628
#if !defined(TARGET_OPENGLES) || (defined(GL_ES_VERSION_3_0) && defined(TARGET_OPENGLES_3))
16281629
//----------------------------------------------------------
16291630
void ofGLProgrammableRenderer::bindForBlitting(const ofFbo & fboSrc, ofFbo & fboDst, int attachmentPoint) {
1630-
if (currentFramebufferId == fboSrc.getId()) {
1631+
const auto step = ofFramebufferPush(framebufferIdStack, currentFramebufferId, fboSrc.getId());
1632+
if (step.warning == ofFramebufferBindWarning::BoundOntoSelf) {
16311633
ofLogWarning() << "Framebuffer with id: " << fboSrc.getId() << " cannot be bound onto itself. \n"
16321634
<< "Most probably you forgot to end() the current framebuffer before calling getTexture().";
1633-
return;
16341635
}
16351636

1636-
framebufferIdStack.push_back(currentFramebufferId);
1637-
currentFramebufferId = fboSrc.getId();
1638-
16391637
glBindFramebuffer(GL_READ_FRAMEBUFFER, currentFramebufferId);
16401638
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboDst.getIdDrawBuffer());
16411639

@@ -1649,12 +1647,9 @@ void ofGLProgrammableRenderer::bindForBlitting(const ofFbo & fboSrc, ofFbo & fbo
16491647

16501648
//----------------------------------------------------------
16511649
void ofGLProgrammableRenderer::unbind(const ofFbo & fbo) {
1652-
if (framebufferIdStack.empty()) {
1653-
ofLogError() << "unbalanced fbo bind/unbind binding default framebuffer";
1654-
currentFramebufferId = defaultFramebufferId;
1655-
} else {
1656-
currentFramebufferId = framebufferIdStack.back();
1657-
framebufferIdStack.pop_back();
1650+
const auto step = ofFramebufferPop(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
1651+
if (step.warning == ofFramebufferBindWarning::NoMatchingBegin) {
1652+
ofLogWarning("ofFbo") << "end() with no matching begin(); binding the window";
16581653
}
16591654
glBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
16601655
fbo.flagDirty();

‎libs/openFrameworks/gl/ofGLRenderer.cpp‎

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "ofCamera.h"
55
#include "ofFbo.h"
66
#include "ofGLRenderer.h"
7+
#include "ofFramebufferBinding.h"
78
#include "ofGLUtils.h"
89
#include "ofImage.h"
910
#include "ofLight.h"
@@ -65,8 +66,8 @@ void ofGLRenderer::setup() {
6566
}
6667
}
6768
void ofGLRenderer::startRender() {
68-
currentFramebufferId = defaultFramebufferId;
69-
framebufferIdStack.push_back(defaultFramebufferId);
69+
ofFramebufferStartRender(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
70+
ofGLBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
7071
matrixStack.setRenderSurface(*window);
7172
viewport();
7273
// to do non auto clear on PC for now - we do something like "single" buffering --
@@ -85,7 +86,14 @@ void ofGLRenderer::startRender() {
8586

8687
void ofGLRenderer::finishRender() {
8788
matrixStack.clearStacks();
88-
framebufferIdStack.clear();
89+
const GLuint leftBound = currentFramebufferId;
90+
const auto framebufferStep = ofFramebufferFinishRender(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
91+
if (framebufferStep.warning == ofFramebufferBindWarning::LeftBound) {
92+
ofLogWarning("ofFbo") << "left bound at the end of the frame (id " << leftBound << ")";
93+
}
94+
if (framebufferStep.bind) {
95+
ofGLBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
96+
}
8997
}
9098

9199
//----------------------------------------------------------
@@ -550,33 +558,23 @@ void ofGLRenderer::end(const ofFbo & fbo) {
550558

551559
//----------------------------------------------------------
552560
void ofGLRenderer::bind(const ofFbo & fbo) {
553-
if (currentFramebufferId == fbo.getId()) {
561+
const auto step = ofFramebufferPush(framebufferIdStack, currentFramebufferId, fbo.getId());
562+
if (step.warning == ofFramebufferBindWarning::BoundOntoSelf) {
554563
ofLogWarning() << "Framebuffer with id: " << fbo.getId() << " cannot be bound onto itself. \n"
555564
<< "Most probably you forgot to end() the current framebuffer before calling begin() again or you forgot to allocate() before calling begin().";
556-
return;
557565
}
558-
// this method could just as well have been placed in ofBaseGLRenderer
559-
// and shared over both programmable and fixed function renderer.
560-
// I'm keeping it here, so that if we want to do more fancyful
561-
// named framebuffers with GL 4.5+, we can have
562-
// different implementations.
563-
framebufferIdStack.push_back(currentFramebufferId);
564-
currentFramebufferId = fbo.getId();
565566
ofGLBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
566567
}
567568

568569
#if !defined(TARGET_OPENGLES) || (defined(GL_ES_VERSION_3_0) && defined(TARGET_OPENGLES_3))
569570
//----------------------------------------------------------
570571
void ofGLRenderer::bindForBlitting(const ofFbo & fboSrc, ofFbo & fboDst, int attachmentPoint) {
571-
if (currentFramebufferId == fboSrc.getId()) {
572+
const auto step = ofFramebufferPush(framebufferIdStack, currentFramebufferId, fboSrc.getId());
573+
if (step.warning == ofFramebufferBindWarning::BoundOntoSelf) {
572574
ofLogWarning() << "Framebuffer with id: " << fboSrc.getId() << " cannot be bound onto itself. \n"
573575
<< "Most probably you forgot to end() the current framebuffer before calling getTexture().";
574-
return;
575576
}
576577

577-
framebufferIdStack.push_back(currentFramebufferId);
578-
currentFramebufferId = fboSrc.getId();
579-
580578
ofGLBindFramebuffer(GL_READ_FRAMEBUFFER, currentFramebufferId);
581579
ofGLBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboDst.getIdDrawBuffer());
582580

@@ -590,12 +588,9 @@ void ofGLRenderer::bindForBlitting(const ofFbo & fboSrc, ofFbo & fboDst, int att
590588

591589
//----------------------------------------------------------
592590
void ofGLRenderer::unbind(const ofFbo & fbo) {
593-
if (framebufferIdStack.empty()) {
594-
ofLogError() << "unbalanced fbo bind/unbind binding default framebuffer";
595-
currentFramebufferId = defaultFramebufferId;
596-
} else {
597-
currentFramebufferId = framebufferIdStack.back();
598-
framebufferIdStack.pop_back();
591+
const auto step = ofFramebufferPop(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
592+
if (step.warning == ofFramebufferBindWarning::NoMatchingBegin) {
593+
ofLogWarning("ofFbo") << "end() with no matching begin(); binding the window";
599594
}
600595
ofGLBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
601596
fbo.flagDirty();

‎tests/gl/fboBindStack/addons.make‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ofxUnitTests
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#pragma once
2+
3+
#include "ofFramebufferBinding.h"
4+
#include <deque>
5+
#include <string>
6+
7+
// Runs the framebuffer stack the same way the GL renderers do, with no GL context.
8+
// Returns the number of failed checks. details receives one line per failure.
9+
inline int runFboBindCases(std::string & details) {
10+
using Id = std::uint32_t;
11+
const Id window = 0;
12+
const Id fboA = 7;
13+
const Id fboB = 8;
14+
int failed = 0;
15+
16+
auto check = [&](bool ok, const char * name) {
17+
if (!ok) {
18+
details += name;
19+
details += "\n";
20+
++failed;
21+
}
22+
};
23+
24+
{
25+
std::deque<Id> stack;
26+
Id current = window;
27+
const auto start = ofFramebufferStartRender(stack, current, window);
28+
const auto began = ofFramebufferPush(stack, current, fboA);
29+
const auto ended = ofFramebufferPop(stack, current, window);
30+
check(start.warning == ofFramebufferBindWarning::None && start.bind && start.framebufferId == window, "start binds the window");
31+
check(began.warning == ofFramebufferBindWarning::None && ended.warning == ofFramebufferBindWarning::None, "balanced end is quiet");
32+
check(ended.framebufferId == window && stack.size() == 1 && current == window, "balanced end returns to the window");
33+
const auto finish = ofFramebufferFinishRender(stack, current, window);
34+
check(finish.warning == ofFramebufferBindWarning::None && !finish.bind && stack.empty() && current == window, "balanced frame does not warn");
35+
}
36+
37+
{
38+
std::deque<Id> stack;
39+
Id current = window;
40+
ofFramebufferStartRender(stack, current, window);
41+
ofFramebufferPush(stack, current, fboA);
42+
const Id left = current;
43+
const auto finish = ofFramebufferFinishRender(stack, current, window);
44+
const auto late = ofFramebufferPop(stack, current, window);
45+
check(finish.warning == ofFramebufferBindWarning::LeftBound && finish.bind && left == fboA, "missing end is reported when the frame ends");
46+
check(current == window && stack.empty(), "missing end rebinds the window");
47+
check(late.warning == ofFramebufferBindWarning::None && late.framebufferId == window, "end after the frame recovered stays quiet");
48+
}
49+
50+
{
51+
std::deque<Id> stack;
52+
Id current = window;
53+
ofFramebufferStartRender(stack, current, window);
54+
ofFramebufferPush(stack, current, fboA);
55+
ofFramebufferPop(stack, current, window);
56+
const auto extra = ofFramebufferPop(stack, current, window);
57+
check(extra.warning == ofFramebufferBindWarning::NoMatchingBegin && stack.size() == 1 && current == window, "extra end warns and keeps the window entry");
58+
const auto finish = ofFramebufferFinishRender(stack, current, window);
59+
check(finish.warning == ofFramebufferBindWarning::None, "extra end does not warn again at the end of the frame");
60+
}
61+
62+
{
63+
std::deque<Id> stack;
64+
Id current = window;
65+
ofFramebufferFinishRender(stack, current, window);
66+
const auto began = ofFramebufferPush(stack, current, fboA);
67+
const auto ended = ofFramebufferPop(stack, current, window);
68+
check(began.warning == ofFramebufferBindWarning::None && ended.warning == ofFramebufferBindWarning::None, "begin/end outside a frame is quiet");
69+
check(stack.empty() && current == window, "begin/end outside a frame restores the window");
70+
}
71+
72+
{
73+
std::deque<Id> stack;
74+
Id current = window;
75+
ofFramebufferStartRender(stack, current, window);
76+
ofFramebufferPush(stack, current, fboA);
77+
const auto nested = ofFramebufferPush(stack, current, fboA);
78+
const auto inner = ofFramebufferPop(stack, current, window);
79+
const auto outer = ofFramebufferPop(stack, current, window);
80+
const auto finish = ofFramebufferFinishRender(stack, current, window);
81+
check(nested.warning == ofFramebufferBindWarning::BoundOntoSelf && inner.framebufferId == fboA && inner.warning == ofFramebufferBindWarning::None, "binding an fbo onto itself still has a matching end");
82+
check(outer.framebufferId == window && finish.warning == ofFramebufferBindWarning::None, "self-bind end returns to the window");
83+
}
84+
85+
{
86+
std::deque<Id> stack;
87+
Id current = window;
88+
ofFramebufferStartRender(stack, current, window);
89+
ofFramebufferPush(stack, current, fboA);
90+
ofFramebufferPush(stack, current, fboB);
91+
const auto inner = ofFramebufferPop(stack, current, window);
92+
const auto outer = ofFramebufferPop(stack, current, window);
93+
check(inner.framebufferId == fboA && outer.framebufferId == window, "nested fbos pop in order");
94+
}
95+
96+
{
97+
std::deque<Id> stack;
98+
Id current = fboA;
99+
const auto late = ofFramebufferPop(stack, current, window);
100+
check(late.warning == ofFramebufferBindWarning::NoMatchingBegin && current == window, "end while another fbo is current and the stack is empty warns");
101+
}
102+
103+
return failed;
104+
}

‎tests/gl/fboBindStack/src/main.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "ofMain.h"
2+
#include "ofAppNoWindow.h"
3+
#include "ofxUnitTests.h"
4+
#include "fboBindCases.h"
5+
6+
class ofApp : public ofxUnitTestsApp {
7+
void run() {
8+
std::string details;
9+
const int failed = runFboBindCases(details);
10+
ofxTestEq(failed, 0, details.empty() ? "framebuffer bind stack" : details);
11+
}
12+
};
13+
14+
int main() {
15+
ofInit();
16+
auto window = std::make_shared<ofAppNoWindow>();
17+
auto app = std::make_shared<ofApp>();
18+
ofRunApp(window, app);
19+
return ofRunMainLoop();
20+
}

0 commit comments

Comments
 (0)