Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions libs/openFrameworks/gl/ofFramebufferBinding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include <deque>

// Tracks which framebuffer ofFbo::begin()/end() should return to.
// startRender() pushes the window id and that entry stays until finishRender().
// A matching begin/end inside the frame pushes and pops above it. finishRender()
// warns when an ofFbo is still bound, then binds the window again so a later
// end() does not report that as an unbalanced pair.

enum class ofFramebufferBindWarning {
None,
NoMatchingBegin,
LeftBound,
BoundOntoSelf
};

template <class Id>
struct ofFramebufferBindStep {
Id framebufferId{};
bool bind = false;
ofFramebufferBindWarning warning = ofFramebufferBindWarning::None;
};

template <class Id>
ofFramebufferBindStep<Id> ofFramebufferStartRender(std::deque<Id> & stack, Id & current, Id defaultId) {
current = defaultId;
stack.push_back(defaultId);
return {defaultId, true, ofFramebufferBindWarning::None};
}

template <class Id>
ofFramebufferBindStep<Id> ofFramebufferFinishRender(std::deque<Id> & stack, Id & current, Id defaultId) {
const ofFramebufferBindWarning warning = (stack.size() != 1 || current != defaultId)
? ofFramebufferBindWarning::LeftBound
: ofFramebufferBindWarning::None;
stack.clear();
if (current != defaultId) {
current = defaultId;
return {defaultId, true, warning};
}
return {defaultId, false, warning};
}

template <class Id>
ofFramebufferBindStep<Id> ofFramebufferPush(std::deque<Id> & stack, Id & current, Id fboId) {
const ofFramebufferBindWarning warning = (current == fboId)
? ofFramebufferBindWarning::BoundOntoSelf
: ofFramebufferBindWarning::None;
stack.push_back(current);
current = fboId;
return {fboId, true, warning};
}

template <class Id>
ofFramebufferBindStep<Id> ofFramebufferPop(std::deque<Id> & stack, Id & current, Id defaultId) {
if (stack.empty()) {
const ofFramebufferBindWarning warning = (current != defaultId)
? ofFramebufferBindWarning::NoMatchingBegin
: ofFramebufferBindWarning::None;
current = defaultId;
return {defaultId, true, warning};
}
if (stack.size() == 1 && current == stack.back()) {
return {current, true, ofFramebufferBindWarning::NoMatchingBegin};
}
current = stack.back();
stack.pop_back();
return {current, true, ofFramebufferBindWarning::None};
}
41 changes: 18 additions & 23 deletions libs/openFrameworks/gl/ofGLProgrammableRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ofCamera.h"
#include "ofFbo.h"
#include "ofGLProgrammableRenderer.h"
#include "ofFramebufferBinding.h"
#include "ofGLUtils.h"
#include "ofImage.h"
#include "ofLight.h"
Expand Down Expand Up @@ -95,8 +96,8 @@ ofGLProgrammableRenderer::ofGLProgrammableRenderer(const ofAppBaseWindow * _wind

//----------------------------------------------------------
void ofGLProgrammableRenderer::startRender() {
currentFramebufferId = defaultFramebufferId;
framebufferIdStack.push_back(defaultFramebufferId);
ofFramebufferStartRender(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
glBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
matrixStack.setRenderSurface(*window);
beginDefaultShader();
viewport();
Expand All @@ -121,7 +122,14 @@ void ofGLProgrammableRenderer::finishRender() {
if (!usingCustomShader) currentShader = nullptr;
}
matrixStack.clearStacks();
framebufferIdStack.clear();
const GLuint leftBound = currentFramebufferId;
const auto framebufferStep = ofFramebufferFinishRender(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
if (framebufferStep.warning == ofFramebufferBindWarning::LeftBound) {
ofLogWarning("ofFbo") << "left bound at the end of the frame (id " << leftBound << ")";
}
if (framebufferStep.bind) {
glBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
}
mDrawMode = GL_TRIANGLES;
mUniformsTex.clear();
}
Expand Down Expand Up @@ -1609,33 +1617,23 @@ void ofGLProgrammableRenderer::end(const ofFbo & fbo) {

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

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

framebufferIdStack.push_back(currentFramebufferId);
currentFramebufferId = fboSrc.getId();

glBindFramebuffer(GL_READ_FRAMEBUFFER, currentFramebufferId);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboDst.getIdDrawBuffer());

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

//----------------------------------------------------------
void ofGLProgrammableRenderer::unbind(const ofFbo & fbo) {
if (framebufferIdStack.empty()) {
ofLogError() << "unbalanced fbo bind/unbind binding default framebuffer";
currentFramebufferId = defaultFramebufferId;
} else {
currentFramebufferId = framebufferIdStack.back();
framebufferIdStack.pop_back();
const auto step = ofFramebufferPop(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
if (step.warning == ofFramebufferBindWarning::NoMatchingBegin) {
ofLogWarning("ofFbo") << "end() with no matching begin(); binding the window";
}
glBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
fbo.flagDirty();
Expand Down
41 changes: 18 additions & 23 deletions libs/openFrameworks/gl/ofGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ofCamera.h"
#include "ofFbo.h"
#include "ofGLRenderer.h"
#include "ofFramebufferBinding.h"
#include "ofGLUtils.h"
#include "ofImage.h"
#include "ofLight.h"
Expand Down Expand Up @@ -65,8 +66,8 @@ void ofGLRenderer::setup() {
}
}
void ofGLRenderer::startRender() {
currentFramebufferId = defaultFramebufferId;
framebufferIdStack.push_back(defaultFramebufferId);
ofFramebufferStartRender(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
ofGLBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
matrixStack.setRenderSurface(*window);
viewport();
// to do non auto clear on PC for now - we do something like "single" buffering --
Expand All @@ -85,7 +86,14 @@ void ofGLRenderer::startRender() {

void ofGLRenderer::finishRender() {
matrixStack.clearStacks();
framebufferIdStack.clear();
const GLuint leftBound = currentFramebufferId;
const auto framebufferStep = ofFramebufferFinishRender(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
if (framebufferStep.warning == ofFramebufferBindWarning::LeftBound) {
ofLogWarning("ofFbo") << "left bound at the end of the frame (id " << leftBound << ")";
}
if (framebufferStep.bind) {
ofGLBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
}
}

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

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

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

framebufferIdStack.push_back(currentFramebufferId);
currentFramebufferId = fboSrc.getId();

ofGLBindFramebuffer(GL_READ_FRAMEBUFFER, currentFramebufferId);
ofGLBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboDst.getIdDrawBuffer());

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

//----------------------------------------------------------
void ofGLRenderer::unbind(const ofFbo & fbo) {
if (framebufferIdStack.empty()) {
ofLogError() << "unbalanced fbo bind/unbind binding default framebuffer";
currentFramebufferId = defaultFramebufferId;
} else {
currentFramebufferId = framebufferIdStack.back();
framebufferIdStack.pop_back();
const auto step = ofFramebufferPop(framebufferIdStack, currentFramebufferId, defaultFramebufferId);
if (step.warning == ofFramebufferBindWarning::NoMatchingBegin) {
ofLogWarning("ofFbo") << "end() with no matching begin(); binding the window";
}
ofGLBindFramebuffer(GL_FRAMEBUFFER, currentFramebufferId);
fbo.flagDirty();
Expand Down
1 change: 1 addition & 0 deletions tests/gl/fboBindStack/addons.make
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ofxUnitTests
104 changes: 104 additions & 0 deletions tests/gl/fboBindStack/src/fboBindCases.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#pragma once

#include "ofFramebufferBinding.h"
#include <deque>
#include <string>

// Runs the framebuffer stack the same way the GL renderers do, with no GL context.
// Returns the number of failed checks. details receives one line per failure.
inline int runFboBindCases(std::string & details) {
using Id = std::uint32_t;
const Id window = 0;
const Id fboA = 7;
const Id fboB = 8;
int failed = 0;

auto check = [&](bool ok, const char * name) {
if (!ok) {
details += name;
details += "\n";
++failed;
}
};

{
std::deque<Id> stack;
Id current = window;
const auto start = ofFramebufferStartRender(stack, current, window);
const auto began = ofFramebufferPush(stack, current, fboA);
const auto ended = ofFramebufferPop(stack, current, window);
check(start.warning == ofFramebufferBindWarning::None && start.bind && start.framebufferId == window, "start binds the window");
check(began.warning == ofFramebufferBindWarning::None && ended.warning == ofFramebufferBindWarning::None, "balanced end is quiet");
check(ended.framebufferId == window && stack.size() == 1 && current == window, "balanced end returns to the window");
const auto finish = ofFramebufferFinishRender(stack, current, window);
check(finish.warning == ofFramebufferBindWarning::None && !finish.bind && stack.empty() && current == window, "balanced frame does not warn");
}

{
std::deque<Id> stack;
Id current = window;
ofFramebufferStartRender(stack, current, window);
ofFramebufferPush(stack, current, fboA);
const Id left = current;
const auto finish = ofFramebufferFinishRender(stack, current, window);
const auto late = ofFramebufferPop(stack, current, window);
check(finish.warning == ofFramebufferBindWarning::LeftBound && finish.bind && left == fboA, "missing end is reported when the frame ends");
check(current == window && stack.empty(), "missing end rebinds the window");
check(late.warning == ofFramebufferBindWarning::None && late.framebufferId == window, "end after the frame recovered stays quiet");
}

{
std::deque<Id> stack;
Id current = window;
ofFramebufferStartRender(stack, current, window);
ofFramebufferPush(stack, current, fboA);
ofFramebufferPop(stack, current, window);
const auto extra = ofFramebufferPop(stack, current, window);
check(extra.warning == ofFramebufferBindWarning::NoMatchingBegin && stack.size() == 1 && current == window, "extra end warns and keeps the window entry");
const auto finish = ofFramebufferFinishRender(stack, current, window);
check(finish.warning == ofFramebufferBindWarning::None, "extra end does not warn again at the end of the frame");
}

{
std::deque<Id> stack;
Id current = window;
ofFramebufferFinishRender(stack, current, window);
const auto began = ofFramebufferPush(stack, current, fboA);
const auto ended = ofFramebufferPop(stack, current, window);
check(began.warning == ofFramebufferBindWarning::None && ended.warning == ofFramebufferBindWarning::None, "begin/end outside a frame is quiet");
check(stack.empty() && current == window, "begin/end outside a frame restores the window");
}

{
std::deque<Id> stack;
Id current = window;
ofFramebufferStartRender(stack, current, window);
ofFramebufferPush(stack, current, fboA);
const auto nested = ofFramebufferPush(stack, current, fboA);
const auto inner = ofFramebufferPop(stack, current, window);
const auto outer = ofFramebufferPop(stack, current, window);
const auto finish = ofFramebufferFinishRender(stack, current, window);
check(nested.warning == ofFramebufferBindWarning::BoundOntoSelf && inner.framebufferId == fboA && inner.warning == ofFramebufferBindWarning::None, "binding an fbo onto itself still has a matching end");
check(outer.framebufferId == window && finish.warning == ofFramebufferBindWarning::None, "self-bind end returns to the window");
}

{
std::deque<Id> stack;
Id current = window;
ofFramebufferStartRender(stack, current, window);
ofFramebufferPush(stack, current, fboA);
ofFramebufferPush(stack, current, fboB);
const auto inner = ofFramebufferPop(stack, current, window);
const auto outer = ofFramebufferPop(stack, current, window);
check(inner.framebufferId == fboA && outer.framebufferId == window, "nested fbos pop in order");
}

{
std::deque<Id> stack;
Id current = fboA;
const auto late = ofFramebufferPop(stack, current, window);
check(late.warning == ofFramebufferBindWarning::NoMatchingBegin && current == window, "end while another fbo is current and the stack is empty warns");
}

return failed;
}
20 changes: 20 additions & 0 deletions tests/gl/fboBindStack/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "ofMain.h"
#include "ofAppNoWindow.h"
#include "ofxUnitTests.h"
#include "fboBindCases.h"

class ofApp : public ofxUnitTestsApp {
void run() {
std::string details;
const int failed = runFboBindCases(details);
ofxTestEq(failed, 0, details.empty() ? "framebuffer bind stack" : details);
}
};

int main() {
ofInit();
auto window = std::make_shared<ofAppNoWindow>();
auto app = std::make_shared<ofApp>();
ofRunApp(window, app);
return ofRunMainLoop();
}
Loading