fixed alpha problems and optimized performance

pull/3471/head
kallaballa 2 years ago
parent 638b4be831
commit 5ed8beb643
  1. 12
      modules/v4d/include/opencv2/v4d/dialog.hpp
  2. 6
      modules/v4d/include/opencv2/v4d/util.hpp
  3. 19
      modules/v4d/src/detail/framebuffercontext.cpp
  4. 118
      modules/v4d/src/detail/nanoguicontext.cpp
  5. 3
      modules/v4d/src/dialog.cpp
  6. 14
      modules/v4d/src/v4d.cpp

@ -6,6 +6,7 @@
#ifndef SRC_OPENCV_V4D_DIALOG_HPP_ #ifndef SRC_OPENCV_V4D_DIALOG_HPP_
#define SRC_OPENCV_V4D_DIALOG_HPP_ #define SRC_OPENCV_V4D_DIALOG_HPP_
#include <nanogui/theme.h>
#include <nanogui/nanogui.h> #include <nanogui/nanogui.h>
#include <opencv2/core/cvdef.h> #include <opencv2/core/cvdef.h>
#include <opencv2/core/mat.hpp> #include <opencv2/core/mat.hpp>
@ -18,6 +19,16 @@ namespace v4d {
using std::string; using std::string;
class CustomTheme : public nanogui::Theme {
public:
CustomTheme(NVGcontext *ctx) : nanogui::Theme(ctx) {
m_window_drop_shadow_size = 0;
}
virtual ~CustomTheme() {
}
};
/*! /*!
* A class for light-weight dialog (a dialog renderered inside a window) derived from nanogui::Window. * A class for light-weight dialog (a dialog renderered inside a window) derived from nanogui::Window.
* It keeps track of which dialogs are presented and which are lowered and is responsible for layout of lowered dialog-bars. * It keeps track of which dialogs are presented and which are lowered and is responsible for layout of lowered dialog-bars.
@ -34,6 +45,7 @@ private:
nanogui::ref<nanogui::AdvancedGridLayout> oldLayout_; nanogui::ref<nanogui::AdvancedGridLayout> oldLayout_;
nanogui::ref<nanogui::AdvancedGridLayout> newLayout_; nanogui::ref<nanogui::AdvancedGridLayout> newLayout_;
bool minimized_ = false; bool minimized_ = false;
nanogui::ref<CustomTheme> customTheme_;
bool mouse_drag_event(const nanogui::Vector2i& p, const nanogui::Vector2i& rel, int button, bool mouse_drag_event(const nanogui::Vector2i& p, const nanogui::Vector2i& rel, int button,
int mods) override; int mods) override;
public: public:

@ -27,12 +27,9 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
const static std::thread::id MAIN_THREAD_ID = std::this_thread::get_id();
namespace cv { namespace cv {
namespace v4d { namespace v4d {
namespace detail { namespace detail {
template <const size_t _UniqueId, typename _Res, typename... _ArgTypes> template <const size_t _UniqueId, typename _Res, typename... _ArgTypes>
struct fun_ptr_helper struct fun_ptr_helper
{ {
@ -82,9 +79,6 @@ make_function(T *t)
template<std::size_t Tid> template<std::size_t Tid>
void run_sync_on_main(std::function<void()> fn) { void run_sync_on_main(std::function<void()> fn) {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
// if(MAIN_THREAD_ID == std::this_thread::get_id()) {
// throw std::runtime_error("proxying from main thread");
// }
emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, cv::v4d::detail::get_fn_ptr<Tid>(fn)); emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, cv::v4d::detail::get_fn_ptr<Tid>(fn));
#else #else
fn(); fn();

@ -107,8 +107,6 @@ void FrameBufferContext::loadBuffers() {
void FrameBufferContext::initWebGLCopy(FrameBufferContext& dst) { void FrameBufferContext::initWebGLCopy(FrameBufferContext& dst) {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
cerr << "init: " << dst.getFramebufferID() << endl;
this->makeCurrent(); this->makeCurrent();
GL_CHECK(glGenFramebuffers(1, &copyFramebuffer_)); GL_CHECK(glGenFramebuffers(1, &copyFramebuffer_));
GL_CHECK(glGenTextures(1, &copyTexture_)); GL_CHECK(glGenTextures(1, &copyTexture_));
@ -126,7 +124,6 @@ void FrameBufferContext::initWebGLCopy(FrameBufferContext& dst) {
void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) { void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
cerr << "copy: " << dst.getFramebufferID() << endl;
int width = dst.getWindowSize().width; int width = dst.getWindowSize().width;
int height = dst.getWindowSize().height; int height = dst.getWindowSize().height;
{ {
@ -170,8 +167,6 @@ void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) {
}, dst.getIndex() - 1); }, dst.getIndex() - 1);
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL_CHECK(glFlush());
GL_CHECK(glFinish());
GL_CHECK(glReadBuffer(GL_COLOR_ATTACHMENT1)); GL_CHECK(glReadBuffer(GL_COLOR_ATTACHMENT1));
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, this->getFramebufferID())); GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, this->getFramebufferID()));
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
@ -186,9 +181,7 @@ void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) {
GL_CHECK(glBindVertexArray(copyVao)); GL_CHECK(glBindVertexArray(copyVao));
GL_CHECK(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)); GL_CHECK(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0));
// dst.makeCurrent();
GL_CHECK(glFlush()); GL_CHECK(glFlush());
GL_CHECK(glFinish());
#else #else
throw std::runtime_error("WebGL not supported in none WASM builds"); throw std::runtime_error("WebGL not supported in none WASM builds");
#endif #endif
@ -464,7 +457,6 @@ cv::Size FrameBufferContext::size() {
} }
void FrameBufferContext::copyTo(cv::UMat& dst) { void FrameBufferContext::copyTo(cv::UMat& dst) {
cerr << "copyTo:" << getFramebufferID() << endl;
run_sync_on_main<7>([&,this](){ run_sync_on_main<7>([&,this](){
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
CLExecScope_t clExecScope(getCLExecContext()); CLExecScope_t clExecScope(getCLExecContext());
@ -476,8 +468,6 @@ void FrameBufferContext::copyTo(cv::UMat& dst) {
} }
void FrameBufferContext::copyFrom(const cv::UMat& src) { void FrameBufferContext::copyFrom(const cv::UMat& src) {
cerr << "copyFrom:" << getFramebufferID() << endl;
run_sync_on_main<18>([&,this](){ run_sync_on_main<18>([&,this](){
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
CLExecScope_t clExecScope(getCLExecContext()); CLExecScope_t clExecScope(getCLExecContext());
@ -496,6 +486,9 @@ void FrameBufferContext::execute(std::function<void(cv::UMat&)> fn) {
FrameBufferContext::GLScope glScope(*this, GL_FRAMEBUFFER); FrameBufferContext::GLScope glScope(*this, GL_FRAMEBUFFER);
FrameBufferContext::FrameBufferScope fbScope(*this, framebuffer_); FrameBufferContext::FrameBufferScope fbScope(*this, framebuffer_);
fn(framebuffer_); fn(framebuffer_);
#ifndef __EMSCRIPTEN__
GL_CHECK(glFinish());
#endif
}); });
} }
@ -562,6 +555,7 @@ void FrameBufferContext::blitFrameBufferToScreen(const cv::Rect& viewport,
void FrameBufferContext::begin(GLenum framebufferTarget) { void FrameBufferContext::begin(GLenum framebufferTarget) {
this->makeCurrent(); this->makeCurrent();
GL_CHECK(glFinish());
GL_CHECK(glBindFramebuffer(framebufferTarget, frameBufferID_)); GL_CHECK(glBindFramebuffer(framebufferTarget, frameBufferID_));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID_)); GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID_));
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, renderBufferID_)); GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, renderBufferID_));
@ -576,15 +570,12 @@ void FrameBufferContext::begin(GLenum framebufferTarget) {
void FrameBufferContext::end() { void FrameBufferContext::end() {
GL_CHECK(glFlush()); GL_CHECK(glFlush());
GL_CHECK(glFinish());
} }
void FrameBufferContext::download(cv::UMat& m) { void FrameBufferContext::download(cv::UMat& m) {
cv::Mat tmp = m.getMat(cv::ACCESS_WRITE); cv::Mat tmp = m.getMat(cv::ACCESS_WRITE);
assert(tmp.data != nullptr); assert(tmp.data != nullptr);
GL_CHECK(glReadPixels(0, 0, tmp.cols, tmp.rows, GL_RGBA, GL_UNSIGNED_BYTE, tmp.data)); GL_CHECK(glReadPixels(0, 0, tmp.cols, tmp.rows, GL_RGBA, GL_UNSIGNED_BYTE, tmp.data));
GL_CHECK(glFlush());
GL_CHECK(glFinish());
tmp.release(); tmp.release();
} }
@ -593,8 +584,6 @@ void FrameBufferContext::upload(const cv::UMat& m) {
assert(tmp.data != nullptr); assert(tmp.data != nullptr);
GL_CHECK( GL_CHECK(
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, tmp.cols, tmp.rows, GL_RGBA, GL_UNSIGNED_BYTE, tmp.data)); glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, tmp.cols, tmp.rows, GL_RGBA, GL_UNSIGNED_BYTE, tmp.data));
GL_CHECK(glFlush());
GL_CHECK(glFinish());
tmp.release(); tmp.release();
} }

@ -76,10 +76,11 @@ void NanoguiContext::render(bool print, bool graphical) {
GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height));
// GLfloat cColor[4]; // GLfloat cColor[4];
// glGetFloatv(GL_COLOR_CLEAR_VALUE, cColor); // glGetFloatv(GL_COLOR_CLEAR_VALUE, cColor);
glClearColor(0,0,0,0); // glClearColor(0,0,1,1);
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // glClearColor(0,0,0,0);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// glClearColor(cColor[0], cColor[1], cColor[2], cColor[3]); // glClearColor(cColor[0], cColor[1], cColor[2], cColor[3]);
// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
} }
{ {
FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
@ -112,8 +113,6 @@ void NanoguiContext::render(bool print, bool graphical) {
nvgEndFrame(context_); nvgEndFrame(context_);
nvgRestore(context_); nvgRestore(context_);
screen().draw_widgets(); screen().draw_widgets();
GL_CHECK(glFlush());
GL_CHECK(glFinish());
} }
if(!fbCtx().isShared()) { if(!fbCtx().isShared()) {
@ -131,108 +130,17 @@ void NanoguiContext::render(bool print, bool graphical) {
tick_.start(); tick_.start();
} }
//void NanoguiContext::updateFps(bool print, bool graphical) {
// if (!first_) {
// tick_.stop();
//
// if (tick_.getTimeMilli() > 50) {
// if(print) {
// cerr << "FPS : " << (fps_ = tick_.getFPS());
//#ifndef __EMSCRIPTEN__
// cerr << '\r';
//#else
// cerr << endl;
//#endif
// }
// tick_.reset();
// }
//
// if (graphical) {
// run_sync_on_main<24>([&, this](){
// string txt = "FPS: " + std::to_string(fps_);
//#ifndef __EMSCRIPTEN__
// if(!fbCtx().isShared()) {
// mainFbContext_.copyTo(copyBuffer_);
// fbCtx().copyFrom(copyBuffer_);
// }
//#endif
// {
// #ifndef __EMSCRIPTEN__
// mainFbContext_.makeCurrent();
// 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);
// #else
// FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
// GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height));
//// GLfloat cColor[4];
//// glGetFloatv(GL_COLOR_CLEAR_VALUE, cColor);
//// glClearColor(0,0,0,0);
//// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
//// glClearColor(cColor[0], cColor[1], cColor[2], cColor[3]);
// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// }
// {
// FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
// #endif
// float w = mainFbContext_.size().width;
// float h = mainFbContext_.size().height;
// float r = mainFbContext_.pixelRatioX();
//
// nvgSave(context_);
// nvgBeginFrame(context_, w, h, r);
// cv::v4d::nvg::detail::NVG::initializeContext(context_);
//
// using namespace cv::v4d::nvg;
// beginPath();
// roundedRect(5, 5, 15 * txt.size() + 5, 30, 5);
// fillColor(cv::Scalar(255, 255, 255, 180));
// fill();
// }
// {
//#ifdef __EMSCRIPTEN__
// FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
//#endif
// using namespace cv::v4d::nvg;
// fontSize(30.0f);
// fontFace("mono");
// fillColor(cv::Scalar(90, 90, 90, 255));
// textAlign(NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
// text(10, 20, txt.c_str(), nullptr);
//
// nvgEndFrame(context_);
// nvgRestore(context_);
// GL_CHECK(glFlush());
// GL_CHECK(glFinish());
// }
// if(!fbCtx().isShared()) {
//#ifdef __EMSCRIPTEN__
// mainFbContext_.doWebGLCopy(fbCtx());
//#else
// fbCtx().copyTo(copyBuffer_);
// mainFbContext_.copyFrom(copyBuffer_);
//#endif
// }
// fbCtx().makeCurrent();
// GL_CHECK(glFlush());
// GL_CHECK(glFinish());
// mainFbContext_.makeCurrent();
// GL_CHECK(glFlush());
// GL_CHECK(glFinish());
// });
//
//
// }
// }
// first_ = false;
// tick_.start();
//}
void NanoguiContext::build(std::function<void(cv::v4d::FormHelper&)> fn) { void NanoguiContext::build(std::function<void(cv::v4d::FormHelper&)> fn) {
run_sync_on_main<5>([fn,this](){ run_sync_on_main<5>([fn,this](){
FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); #ifndef __EMSCRIPTEN__
GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)); mainFbContext_.makeCurrent();
GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height));
#else
FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height));
#endif
fn(form()); fn(form());
screen().perform_layout(); screen().perform_layout();
}); });

@ -17,7 +17,8 @@ std::set<Dialog*, decltype(Dialog::v4dWin_Xcomparator)> Dialog::all_windows_xsor
v4dWin_Xcomparator); v4dWin_Xcomparator);
Dialog::Dialog(nanogui::Screen* screen, int x, int y, const string& title) : Dialog::Dialog(nanogui::Screen* screen, int x, int y, const string& title) :
Window(screen, title), screen_(screen), lastDragPos_(x, y) { Window(screen, title), screen_(screen), lastDragPos_(x, y), customTheme_(new CustomTheme(screen->nvg_context())) {
set_theme(customTheme_);
all_windows_xsorted_.insert(this); all_windows_xsorted_.insert(this);
oldLayout_ = new nanogui::AdvancedGridLayout( { 10, 0, 10, 0 }, { }); oldLayout_ = new nanogui::AdvancedGridLayout( { 10, 0, 10, 0 }, { });
oldLayout_->set_margin(10); oldLayout_->set_margin(10);

@ -675,13 +675,19 @@ bool V4D::display() {
fbCtx().makeCurrent(); fbCtx().makeCurrent();
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
glfwSwapBuffers(fbCtx().getGLFWWindow()); glfwSwapBuffers(fbCtx().getGLFWWindow());
GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
GL_CHECK(glClearColor(0, 0, 0, 1));
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
#else #else
emscripten_webgl_commit_frame(); emscripten_webgl_commit_frame();
#endif #endif
{
#ifndef __EMSCRIPTEN__
fbCtx().makeCurrent();
GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
#else
FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER);
#endif
GL_CHECK(glClearColor(0, 0, 0, 0));
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
}
glfwPollEvents(); glfwPollEvents();
result = !glfwWindowShouldClose(getGLFWWindow()); result = !glfwWindowShouldClose(getGLFWWindow());

Loading…
Cancel
Save