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 hasNguiCtx();
bool hasGlCtx(uint32_t idx = 0);
size_t numGlCtx();
GLFWwindow* getGLFWWindow();
void swapContextBuffers();

@ -33,7 +33,7 @@ static bool contains_absolute(nanogui::Widget* w, const nanogui::Vector2i& p) {
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,
@ -136,12 +136,12 @@ void FrameBufferContext::initWebGLCopy(FrameBufferContext& dst) {
void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) {
#ifdef __EMSCRIPTEN__
int width = dst.getWindowSize().width;
int height = dst.getWindowSize().height;
int width = getWindowSize().width;
int height = getWindowSize().height;
{
FrameBufferContext::GLScope glScope(dst, GL_READ_FRAMEBUFFER);
dst.blitFrameBufferToScreen(
cv::Rect(0,0, width, height),
cv::Rect(0,0, dst.size().width, dst.size().height),
dst.getWindowSize(),
false);
emscripten_webgl_commit_frame();
@ -253,7 +253,7 @@ void FrameBufferContext::init() {
#else
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
#endif
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_FALSE);
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindow_ = glfwCreateWindow(framebufferSize_.width, framebufferSize_.height, title_.c_str(), nullptr,
sharedWindow_);

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

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

Loading…
Cancel
Save