diff --git a/libs/openFrameworks/gl/ofFramebufferBinding.h b/libs/openFrameworks/gl/ofFramebufferBinding.h new file mode 100644 index 00000000000..d71fa111659 --- /dev/null +++ b/libs/openFrameworks/gl/ofFramebufferBinding.h @@ -0,0 +1,70 @@ +#pragma once + +#include + +// 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 +struct ofFramebufferBindStep { + Id framebufferId{}; + bool bind = false; + ofFramebufferBindWarning warning = ofFramebufferBindWarning::None; +}; + +template +ofFramebufferBindStep ofFramebufferStartRender(std::deque & stack, Id & current, Id defaultId) { + current = defaultId; + stack.push_back(defaultId); + return {defaultId, true, ofFramebufferBindWarning::None}; +} + +template +ofFramebufferBindStep ofFramebufferFinishRender(std::deque & 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 +ofFramebufferBindStep ofFramebufferPush(std::deque & 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 +ofFramebufferBindStep ofFramebufferPop(std::deque & 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}; +} diff --git a/libs/openFrameworks/gl/ofGLProgrammableRenderer.cpp b/libs/openFrameworks/gl/ofGLProgrammableRenderer.cpp index f4c4f54f682..06693d47b8c 100644 --- a/libs/openFrameworks/gl/ofGLProgrammableRenderer.cpp +++ b/libs/openFrameworks/gl/ofGLProgrammableRenderer.cpp @@ -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" @@ -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(); @@ -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(); } @@ -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()); @@ -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(); diff --git a/libs/openFrameworks/gl/ofGLRenderer.cpp b/libs/openFrameworks/gl/ofGLRenderer.cpp index 2091231d47b..a310338b3ab 100644 --- a/libs/openFrameworks/gl/ofGLRenderer.cpp +++ b/libs/openFrameworks/gl/ofGLRenderer.cpp @@ -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" @@ -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 -- @@ -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); + } } //---------------------------------------------------------- @@ -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()); @@ -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(); diff --git a/tests/gl/fboBindStack/addons.make b/tests/gl/fboBindStack/addons.make new file mode 100644 index 00000000000..0e0303d2e09 --- /dev/null +++ b/tests/gl/fboBindStack/addons.make @@ -0,0 +1 @@ +ofxUnitTests diff --git a/tests/gl/fboBindStack/src/fboBindCases.h b/tests/gl/fboBindStack/src/fboBindCases.h new file mode 100644 index 00000000000..7f14f89cc69 --- /dev/null +++ b/tests/gl/fboBindStack/src/fboBindCases.h @@ -0,0 +1,104 @@ +#pragma once + +#include "ofFramebufferBinding.h" +#include +#include + +// 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 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 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 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 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 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 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 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; +} diff --git a/tests/gl/fboBindStack/src/main.cpp b/tests/gl/fboBindStack/src/main.cpp new file mode 100644 index 00000000000..4219a7f3c2b --- /dev/null +++ b/tests/gl/fboBindStack/src/main.cpp @@ -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(); + auto app = std::make_shared(); + ofRunApp(window, app); + return ofRunMainLoop(); +}