fix overlay flickering on nvidia

pull/3471/head
kallaballa 2 years ago
parent eac533cc14
commit 3d68c8a0a8
  1. 1
      modules/v4d/include/opencv2/v4d/v4d.hpp
  2. 10
      modules/v4d/src/detail/framebuffercontext.cpp
  3. 39
      modules/v4d/src/detail/nanoguicontext.cpp
  4. 23
      modules/v4d/src/v4d.cpp

@ -400,6 +400,7 @@ private:
bool hasNvgCtx(); bool hasNvgCtx();
bool hasNguiCtx(); bool hasNguiCtx();
bool hasGlCtx(uint32_t idx = 0); bool hasGlCtx(uint32_t idx = 0);
size_t numGlCtx();
GLFWwindow* getGLFWWindow(); GLFWwindow* getGLFWWindow();
void swapContextBuffers(); void swapContextBuffers();

@ -33,7 +33,7 @@ static bool contains_absolute(nanogui::Widget* w, const nanogui::Vector2i& p) {
int frameBufferContextCnt = 0; int frameBufferContextCnt = 0;
FrameBufferContext::FrameBufferContext(V4D& v4d, const string& title, const FrameBufferContext& other) : FrameBufferContext(v4d, other.framebufferSize_, true, title, other.major_, other.minor_, other.compat_, other.samples_, other.debug_, other.glfwWindow_, &other) { FrameBufferContext::FrameBufferContext(V4D& v4d, const string& title, const FrameBufferContext& other) : FrameBufferContext(v4d, other.framebufferSize_, !other.debug_, title, other.major_, other.minor_, other.compat_, other.samples_, other.debug_, other.glfwWindow_, &other) {
} }
FrameBufferContext::FrameBufferContext(V4D& v4d, const cv::Size& framebufferSize, bool offscreen, FrameBufferContext::FrameBufferContext(V4D& v4d, const cv::Size& framebufferSize, bool offscreen,
@ -136,12 +136,12 @@ void FrameBufferContext::initWebGLCopy(FrameBufferContext& dst) {
void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) { void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
int width = dst.getWindowSize().width; int width = getWindowSize().width;
int height = dst.getWindowSize().height; int height = getWindowSize().height;
{ {
FrameBufferContext::GLScope glScope(dst, GL_READ_FRAMEBUFFER); FrameBufferContext::GLScope glScope(dst, GL_READ_FRAMEBUFFER);
dst.blitFrameBufferToScreen( dst.blitFrameBufferToScreen(
cv::Rect(0,0, width, height), cv::Rect(0,0, dst.size().width, dst.size().height),
dst.getWindowSize(), dst.getWindowSize(),
false); false);
emscripten_webgl_commit_frame(); emscripten_webgl_commit_frame();
@ -253,7 +253,7 @@ void FrameBufferContext::init() {
#else #else
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE); glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
#endif #endif
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_FALSE); glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindow_ = glfwCreateWindow(framebufferSize_.width, framebufferSize_.height, title_.c_str(), nullptr, glfwWindow_ = glfwCreateWindow(framebufferSize_.width, framebufferSize_.height, title_.c_str(), nullptr,
sharedWindow_); sharedWindow_);

@ -15,11 +15,11 @@ NanoguiContext::NanoguiContext(FrameBufferContext& fbContext) :
mainFbContext_(fbContext), nguiFbContext_(fbContext.getV4D(), "NanoGUI", fbContext), context_( mainFbContext_(fbContext), nguiFbContext_(fbContext.getV4D(), "NanoGUI", fbContext), context_(
nullptr), copyBuffer_(mainFbContext_.size(), CV_8UC4) { nullptr), copyBuffer_(mainFbContext_.size(), CV_8UC4) {
run_sync_on_main<25>([&, this]() { run_sync_on_main<25>([&, this]() {
{ // {
//Workaround for first frame glitch // //Workaround for first frame glitch
FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); // FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
FrameBufferContext::FrameBufferScope fbScope(fbCtx(), copyBuffer_); // FrameBufferContext::FrameBufferScope fbScope(fbCtx(), copyBuffer_);
} // }
{ {
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
mainFbContext_.makeCurrent(); mainFbContext_.makeCurrent();
@ -66,21 +66,25 @@ void NanoguiContext::render(bool printFPS, bool showFPS, bool showTracking) {
} }
#endif #endif
{ {
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
float w = mainFbContext_.getWindowSize().width;
float h = mainFbContext_.getWindowSize().height;
mainFbContext_.makeCurrent(); mainFbContext_.makeCurrent();
GL_CHECK(glFinish()); GL_CHECK(glFinish());
GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); GL_CHECK(glViewport(0, 0, w, h));
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
#else #else
float w = fbCtx().size().width;
float h = fbCtx().size().height;
FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
GL_CHECK(glViewport(0, 0, w, h));
GL_CHECK(glClearColor(0,0,0,0)); GL_CHECK(glClearColor(0,0,0,0));
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)); GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height));
#endif #endif
float w = mainFbContext_.size().width;
float h = mainFbContext_.size().height;
float r = mainFbContext_.pixelRatioX(); float r = mainFbContext_.pixelRatioX();
if(showFPS || showTracking) { if(showFPS || showTracking) {
nvgSave(context_); nvgSave(context_);
@ -103,8 +107,6 @@ void NanoguiContext::render(bool printFPS, bool showFPS, bool showTracking) {
textAlign(NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); textAlign(NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
text(7.5, 15, txt.c_str(), nullptr); text(7.5, 15, txt.c_str(), nullptr);
nvgEndFrame(context_);
nvgRestore(context_);
} }
if(showTracking) { if(showTracking) {
@ -112,7 +114,9 @@ void NanoguiContext::render(bool printFPS, bool showFPS, bool showTracking) {
std::stringstream ss; std::stringstream ss;
auto& tiMap = TimeTracker::getInstance()->getMap(); auto& tiMap = TimeTracker::getInstance()->getMap();
size_t cnt = 0; size_t cnt = 0;
beginPath(); #ifdef __EMSCRIPTEN__
fbCtx().makeCurrent();
#endif
fontSize(20.0f); fontSize(20.0f);
fontFace("mono"); fontFace("mono");
fillColor(cv::Scalar(200, 200, 200, 200)); fillColor(cv::Scalar(200, 200, 200, 200));
@ -124,14 +128,13 @@ void NanoguiContext::render(bool printFPS, bool showFPS, bool showTracking) {
text(7.5, 15 * (cnt + 3), ss.str().c_str(), nullptr); text(7.5, 15 * (cnt + 3), ss.str().c_str(), nullptr);
++cnt; ++cnt;
} }
}
if(showFPS || showTracking) {
nvgEndFrame(context_); nvgEndFrame(context_);
nvgRestore(context_); nvgRestore(context_);
} }
}
{
#ifdef __EMSCRIPTEN__
FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
#endif
screen().draw_widgets(); screen().draw_widgets();
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
GL_CHECK(glFinish()); GL_CHECK(glFinish());

@ -141,6 +141,11 @@ bool V4D::hasGlCtx(uint32_t idx) {
return glContexts_.find(idx) != glContexts_.end(); return glContexts_.find(idx) != glContexts_.end();
} }
size_t V4D::numGlCtx() {
return glContexts_.size();
}
void V4D::gl(std::function<void()> fn, uint32_t idx) { void V4D::gl(std::function<void()> fn, uint32_t idx) {
TimeTracker::getInstance()->execute("gl(" + detail::func_id(fn) + ")/" + std::to_string(idx), [&](){ TimeTracker::getInstance()->execute("gl(" + detail::func_id(fn) + ")/" + std::to_string(idx), [&](){
glCtx(idx).render([=](const cv::Size& sz) { glCtx(idx).render([=](const cv::Size& sz) {
@ -219,7 +224,10 @@ void V4D::run(std::function<bool(cv::Ptr<V4D>)> fn) {
} }
pool_.finish(); pool_.finish();
#else #else
emscripten_set_main_loop_arg(do_frame, &fn, -1, true); std::function<bool()> fnFrame([=,this](){
return fn(self());
});
emscripten_set_main_loop_arg(do_frame, &fnFrame, -1, true);
#endif #endif
} }
@ -454,13 +462,15 @@ void V4D::setDefaultKeyboardEventCallback() {
void V4D::swapContextBuffers() { void V4D::swapContextBuffers() {
run_sync_on_main<10>([this]() { run_sync_on_main<10>([this]() {
FrameBufferContext::GLScope glScope(glCtx().fbCtx(), GL_READ_FRAMEBUFFER); for(size_t i = 0; i < numGlCtx(); ++i) {
glCtx().fbCtx().blitFrameBufferToScreen(viewport(), glCtx().fbCtx().getWindowSize(), isScaling()); FrameBufferContext::GLScope glScope(glCtx(i).fbCtx(), GL_READ_FRAMEBUFFER);
glCtx(i).fbCtx().blitFrameBufferToScreen(viewport(), glCtx(i).fbCtx().getWindowSize(), isScaling());
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
glfwSwapBuffers(glCtx().fbCtx().getGLFWWindow()); glfwSwapBuffers(glCtx(i).fbCtx().getGLFWWindow());
#else #else
emscripten_webgl_commit_frame(); emscripten_webgl_commit_frame();
#endif #endif
}
}); });
run_sync_on_main<11>([this]() { run_sync_on_main<11>([this]() {
@ -492,7 +502,8 @@ bool V4D::display() {
if (true) { if (true) {
#endif #endif
// swapContextBuffers(); if(debug_)
swapContextBuffers();
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
nguiCtx().render(printFPS_, showFPS_, showTracking_); nguiCtx().render(printFPS_, showFPS_, showTracking_);

Loading…
Cancel
Save