From 4fbc8c0016c11d7419d950550038bb6354dcb0b6 Mon Sep 17 00:00:00 2001 From: kallaballa Date: Sat, 17 Dec 2022 10:14:41 +0100 Subject: [PATCH] fixed OpenGL isolation issue if nvg --- src/common/detail/clglcontext.cpp | 4 +++- src/common/detail/clglcontext.hpp | 1 + src/common/detail/nanovgcontext.cpp | 24 ++++++++++++++++++++++++ src/common/util.cpp | 24 ------------------------ src/common/util.hpp | 10 ---------- src/common/viz2d.cpp | 25 +++++++++++++++++++++++++ src/common/viz2d.hpp | 11 +++++++++++ src/font/font-demo.cpp | 6 +++--- src/tetra/tetra-demo.cpp | 2 +- src/video/video-demo.cpp | 3 +-- 10 files changed, 69 insertions(+), 41 deletions(-) diff --git a/src/common/detail/clglcontext.cpp b/src/common/detail/clglcontext.cpp index 8f5044c7a..4ced693de 100644 --- a/src/common/detail/clglcontext.cpp +++ b/src/common/detail/clglcontext.cpp @@ -74,11 +74,13 @@ void CLGLContext::begin() { GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, renderBufferID)); GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBufferID)); frameBufferTex_->bind(); + GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, frameBufferTex_->texId(), 0)); + assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); + } void CLGLContext::end() { GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0)); -// GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0)); GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, 0)); GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0)); //glFlush seems enough but i wanna make sure that there won't be race conditions. diff --git a/src/common/detail/clglcontext.hpp b/src/common/detail/clglcontext.hpp index 48a6d6d8e..14b858db1 100644 --- a/src/common/detail/clglcontext.hpp +++ b/src/common/detail/clglcontext.hpp @@ -11,6 +11,7 @@ #include #define GLFW_INCLUDE_GLCOREARB #include +#include #include "../util.hpp" diff --git a/src/common/detail/nanovgcontext.cpp b/src/common/detail/nanovgcontext.cpp index 552fbe7a9..53f82efce 100644 --- a/src/common/detail/nanovgcontext.cpp +++ b/src/common/detail/nanovgcontext.cpp @@ -22,7 +22,30 @@ void NanoVGContext::render(std::function fn) { fn(clglContext_.getSize()); } +void push() { + GL_CHECK(glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS)); + GL_CHECK(glPushAttrib(GL_ALL_ATTRIB_BITS)); + GL_CHECK(glMatrixMode(GL_MODELVIEW)); + GL_CHECK(glPushMatrix()); + GL_CHECK(glMatrixMode(GL_PROJECTION)); + GL_CHECK(glPushMatrix()); + GL_CHECK(glMatrixMode(GL_TEXTURE)); + GL_CHECK(glPushMatrix()); +} + +void pop() { + GL_CHECK(glMatrixMode(GL_TEXTURE)); + GL_CHECK(glPopMatrix()); + GL_CHECK(glMatrixMode(GL_PROJECTION)); + GL_CHECK(glPopMatrix()); + GL_CHECK(glMatrixMode(GL_MODELVIEW)); + GL_CHECK(glPopMatrix()); + GL_CHECK(glPopClientAttrib()); + GL_CHECK(glPopAttrib()); +} + void NanoVGContext::begin() { + push(); float w = v2d_.getVideoFrameSize().width; float h = v2d_.getVideoFrameSize().height; float r = v2d_.getXPixelRatio(); @@ -36,6 +59,7 @@ void NanoVGContext::end() { //FIXME make nvgCancelFrame possible nvgEndFrame(context_); nvgRestore(context_); + pop(); } } } diff --git a/src/common/util.cpp b/src/common/util.cpp index 80b79754b..76699c34e 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -6,19 +6,6 @@ namespace kb { namespace viz2d { -void gl_check_error(const std::filesystem::path &file, unsigned int line, const char *expression) { - int errorCode = glGetError(); - - if (errorCode != 0) { - std::cerr << "GL failed in " << file.filename() << " (" << line << ") : " << "\nExpression:\n " << expression << "\nError code:\n " << errorCode << "\n " << std::endl; - assert(false); - } -} - -void error_callback(int error, const char *description) { - fprintf(stderr, "GLFW Error: %s\n", description); -} - std::string get_gl_info() { return reinterpret_cast(glGetString(GL_VERSION)); } @@ -89,16 +76,5 @@ void update_fps(cv::Ptr window, bool graphically) { ++cnt; } -cv::Scalar convert(const cv::Scalar& src, cv::ColorConversionCodes code) { - cv::Mat tmpIn(1,1,CV_8UC3); - cv::Mat tmpOut(1,1,CV_8UC3); - - tmpIn.at(0,0) = cv::Vec3b(src[0], src[1], src[2]); - cvtColor(tmpIn, tmpOut, code); - const cv::Vec3b& vdst = tmpOut.at(0,0); - cv::Scalar dst(vdst[0],vdst[1],vdst[2], src[3]); - return dst; -} - } } diff --git a/src/common/util.hpp b/src/common/util.hpp index ca3d358a1..859c9ad63 100644 --- a/src/common/util.hpp +++ b/src/common/util.hpp @@ -2,7 +2,6 @@ #define SRC_COMMON_UTIL_HPP_ #include -#include #include #include #include @@ -10,19 +9,10 @@ namespace kb { namespace viz2d { class Viz2D; - -void gl_check_error(const std::filesystem::path &file, unsigned int line, const char *expression); - -#define GL_CHECK(expr) \ - expr; \ - kb::viz2d::gl_check_error(__FILE__, __LINE__, #expr); - -void error_callback(int error, const char *description); std::string get_gl_info(); std::string get_cl_info(); void print_system_info(); void update_fps(cv::Ptr viz2d, bool graphical); -cv::Scalar convert(const cv::Scalar& src, cv::ColorConversionCodes code); } } diff --git a/src/common/viz2d.cpp b/src/common/viz2d.cpp index aaab997e1..85ce8f2c9 100644 --- a/src/common/viz2d.cpp +++ b/src/common/viz2d.cpp @@ -5,6 +5,31 @@ namespace kb { namespace viz2d { +namespace detail { +void gl_check_error(const std::filesystem::path &file, unsigned int line, const char *expression) { + int errorCode = glGetError(); + + if (errorCode != 0) { + std::cerr << "GL failed in " << file.filename() << " (" << line << ") : " << "\nExpression:\n " << expression << "\nError code:\n " << errorCode << "\n " << std::endl; + assert(false); + } +} + +void error_callback(int error, const char *description) { + fprintf(stderr, "GLFW Error: %s\n", description); +} +} + +cv::Scalar convert(const cv::Scalar& src, cv::ColorConversionCodes code) { + cv::Mat tmpIn(1,1,CV_8UC3); + cv::Mat tmpOut(1,1,CV_8UC3); + + tmpIn.at(0,0) = cv::Vec3b(src[0], src[1], src[2]); + cvtColor(tmpIn, tmpOut, code); + const cv::Vec3b& vdst = tmpOut.at(0,0); + cv::Scalar dst(vdst[0],vdst[1],vdst[2], src[3]); + return dst; +} Viz2D::Viz2D(const cv::Size &size, const cv::Size& frameBufferSize, bool offscreen, const string &title, int major, int minor, int samples, bool debug) : size_(size), frameBufferSize_(frameBufferSize), offscreen_(offscreen), title_(title), major_(major), minor_(minor), samples_(samples), debug_(debug) { diff --git a/src/common/viz2d.hpp b/src/common/viz2d.hpp index ce6b4a279..ce9b3ccc3 100644 --- a/src/common/viz2d.hpp +++ b/src/common/viz2d.hpp @@ -1,6 +1,7 @@ #ifndef SRC_COMMON_VIZ2D_HPP_ #define SRC_COMMON_VIZ2D_HPP_ +#include #include #include #include @@ -18,7 +19,17 @@ namespace detail { class CLGLContext; class CLVAContext; class NanoVGContext; +void gl_check_error(const std::filesystem::path &file, unsigned int line, const char *expression); + +#define GL_CHECK(expr) \ + expr; \ + kb::viz2d::gl_check_error(__FILE__, __LINE__, #expr); + +void error_callback(int error, const char *description); } + +cv::Scalar convert(const cv::Scalar& src, cv::ColorConversionCodes code); + using namespace kb::viz2d::detail; class NVG; diff --git a/src/font/font-demo.cpp b/src/font/font-demo.cpp index 89c8fb303..f5d6a3c45 100644 --- a/src/font/font-demo.cpp +++ b/src/font/font-demo.cpp @@ -28,8 +28,8 @@ constexpr int MIN_STAR_COUNT = 1000; constexpr int MAX_STAR_COUNT = 3000; constexpr float MIN_STAR_LIGHTNESS = 1.0f; constexpr int MIN_STAR_ALPHA = 5; -// Intensity of glow defined by kernel size. The default scales with the image diagonal. -constexpr int glow_kernel_size = std::max(int(DIAG / 138 % 2 == 0 ? DIAG / 138 + 1 : DIAG / 138), 1); +// Intensity of bloom effect defined by kernel size. The default scales with the image diagonal. +constexpr int bloom_kernel_size = std::max(int(DIAG / 200 % 2 == 0 ? DIAG / 200 + 1 : DIAG / 200), 1); using std::cerr; using std::endl; @@ -97,7 +97,7 @@ int main(int argc, char **argv) { nvg::beginPath(); nvg::fontSize(FONT_SIZE); nvg::fontFace("libertine"); - nvg::fillColor(convert(cv::Scalar(0.15 * 180.0, 128, 128, 255), cv::COLOR_HLS2BGR)); + nvg::fillColor(convert(cv::Scalar(0.15 * 180.0, 128, 255, 255), cv::COLOR_HLS2BGR)); nvg::textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP); /** only draw lines that are visible **/ diff --git a/src/tetra/tetra-demo.cpp b/src/tetra/tetra-demo.cpp index 5a5044919..7d93f0b38 100644 --- a/src/tetra/tetra-demo.cpp +++ b/src/tetra/tetra-demo.cpp @@ -6,7 +6,7 @@ constexpr long unsigned int WIDTH = 1920; constexpr long unsigned int HEIGHT = 1080; constexpr double FPS = 60; -constexpr bool OFFSCREEN = true; +constexpr bool OFFSCREEN = false; constexpr const char* OUTPUT_FILENAME = "tetra-demo.mkv"; constexpr const int VA_HW_DEVICE_INDEX = 0; constexpr unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT)); diff --git a/src/video/video-demo.cpp b/src/video/video-demo.cpp index 68b73c7cd..bb0697e82 100644 --- a/src/video/video-demo.cpp +++ b/src/video/video-demo.cpp @@ -12,8 +12,7 @@ constexpr bool OFFSCREEN = false; constexpr const char* OUTPUT_FILENAME = "video-demo.mkv"; constexpr unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT)); -constexpr int glow_kernel_size = std::max(int(DIAG / 138 % 2 == 0 ? DIAG / 138 + 1 : DIAG / 138), 1); - +constexpr int glow_kernel_size = std::max(int(DIAG / 500 % 2 == 0 ? DIAG / 500 + 1 : DIAG / 500), 1); using std::cerr; using std::endl; using std::string;