From 2d1d0239174f23551cd110e99fb57f2fe0f91481 Mon Sep 17 00:00:00 2001 From: kallaballa Date: Sun, 16 Apr 2023 12:52:18 +0200 Subject: [PATCH] restructure GLES 3.0 adaption defines --- modules/v4d/CMakeLists.txt | 32 +++++++++++-------- modules/v4d/include/opencv2/v4d/dialog.hpp | 8 +---- modules/v4d/include/opencv2/v4d/nvg.hpp | 12 ++++--- modules/v4d/include/opencv2/v4d/v4d.hpp | 10 ++---- modules/v4d/samples/shader/shader-demo.cpp | 12 +++---- modules/v4d/samples/tetra/tetra-demo.cpp | 6 ++-- modules/v4d/samples/video/video-demo.cpp | 4 +-- modules/v4d/src/detail/clvacontext.hpp | 6 ++-- modules/v4d/src/detail/framebuffercontext.cpp | 4 +-- modules/v4d/src/detail/framebuffercontext.hpp | 19 +++++++---- modules/v4d/src/detail/nanovgcontext.cpp | 4 +-- modules/v4d/src/detail/nanovgcontext.hpp | 16 ++++------ modules/v4d/src/dialog.cpp | 5 +-- modules/v4d/src/v4d.cpp | 2 +- 14 files changed, 68 insertions(+), 72 deletions(-) diff --git a/modules/v4d/CMakeLists.txt b/modules/v4d/CMakeLists.txt index d0b6362d7..73e67258f 100644 --- a/modules/v4d/CMakeLists.txt +++ b/modules/v4d/CMakeLists.txt @@ -14,6 +14,9 @@ if (${idx} LESS 0) endif() set(CMAKE_CXX_FLAGS "${CMAKE_LD_FLAGS} -L../../lib") +OCV_OPTION(OPENCV_V4D_ENABLE_ES3 "Enable OpenGL ES 3.0 backend for V4D" OFF + VERIFY HAVE_OPENGL) + find_package(Git QUIET) if(GIT_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../.git") # Update submodules as needed @@ -40,19 +43,20 @@ ocv_glob_module_sources() ocv_module_include_directories() ocv_create_module() ocv_add_samples(opencv_face opencv_tracking opencv_objdetect opencv_stitching opencv_optflow opencv_imgcodecs opencv_features2d opencv_dnn) -target_compile_features(example_v4d_display_image PRIVATE cxx_std_20) -target_compile_features(example_v4d_custom_source_and_sink PRIVATE cxx_std_20) -target_compile_features(example_v4d_display_image PRIVATE cxx_std_20) -target_compile_features(example_v4d_display_image_fb PRIVATE cxx_std_20) -target_compile_features(example_v4d_font_rendering PRIVATE cxx_std_20) -target_compile_features(example_v4d_font_with_gui PRIVATE cxx_std_20) -target_link_libraries(example_v4d_font_with_gui PRIVATE nanogui) -target_compile_features(example_v4d_render_opengl PRIVATE cxx_std_20) -target_link_libraries(example_v4d_render_opengl PRIVATE GL) - -target_compile_features(example_v4d_vector_graphics_and_fb PRIVATE cxx_std_20) -target_compile_features(example_v4d_vector_graphics PRIVATE cxx_std_20) -target_compile_features(example_v4d_video_editing PRIVATE cxx_std_20) +if(BUILD_EXAMPLES) + target_compile_features(example_v4d_display_image PRIVATE cxx_std_20) + target_compile_features(example_v4d_custom_source_and_sink PRIVATE cxx_std_20) + target_compile_features(example_v4d_display_image PRIVATE cxx_std_20) + target_compile_features(example_v4d_display_image_fb PRIVATE cxx_std_20) + target_compile_features(example_v4d_font_rendering PRIVATE cxx_std_20) + target_compile_features(example_v4d_font_with_gui PRIVATE cxx_std_20) + target_link_libraries(example_v4d_font_with_gui PRIVATE nanogui) + target_compile_features(example_v4d_render_opengl PRIVATE cxx_std_20) + target_link_libraries(example_v4d_render_opengl PRIVATE GL) + target_compile_features(example_v4d_vector_graphics_and_fb PRIVATE cxx_std_20) + target_compile_features(example_v4d_vector_graphics PRIVATE cxx_std_20) + target_compile_features(example_v4d_video_editing PRIVATE cxx_std_20) +endif() set(NANOGUI_BUILD_SHARED ON) set(NANOGUI_BUILD_PYTHON OFF) @@ -61,7 +65,7 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third/nanogui) add_library(nanovg "third/nanogui/ext/nanovg/src/nanovg.c") target_compile_features(opencv_v4d PRIVATE cxx_std_20) -ocv_warnings_disable(CMAKE_CXX_FLAGS -Wunused-parameter -Wdeprecated-enum-enum-conversion) +ocv_warnings_disable(CMAKE_CXX_FLAGS -Wunused-parameter -Wdeprecated-enum-enum-conversion -Wformat-truncation) ocv_target_link_libraries(${the_module} ${GLEW_LIBRARIES} OpenCL -lnanovg -lnanogui -lGL) diff --git a/modules/v4d/include/opencv2/v4d/dialog.hpp b/modules/v4d/include/opencv2/v4d/dialog.hpp index f6b6b6993..f2e79b258 100644 --- a/modules/v4d/include/opencv2/v4d/dialog.hpp +++ b/modules/v4d/include/opencv2/v4d/dialog.hpp @@ -6,15 +6,9 @@ #ifndef SRC_OPENCV_V4D_DIALOG_HPP_ #define SRC_OPENCV_V4D_DIALOG_HPP_ #ifdef __EMSCRIPTEN__ -#define V4D_USE_ES3 1 +#define OPENCV_V4D_USE_ES3 1 #endif -#ifndef V4D_USE_ES3 -#define NANOGUI_USE_OPENGL -#else -#define NANOGUI_USE_GLES -#define NANOGUI_GLES_VERSION 3 -#endif #include #include #include diff --git a/modules/v4d/include/opencv2/v4d/nvg.hpp b/modules/v4d/include/opencv2/v4d/nvg.hpp index 87d471cd1..045c1a220 100644 --- a/modules/v4d/include/opencv2/v4d/nvg.hpp +++ b/modules/v4d/include/opencv2/v4d/nvg.hpp @@ -7,11 +7,15 @@ #define SRC_OPENCV_V4D_NVG_HPP_ #include "v4d.hpp" -#ifndef __EMSCRIPTEN__ -#define NANOGUI_USE_OPENGL +#ifndef OPENCV_V4D_USE_ES3 +# ifndef NANOGUI_USE_OPENGL +# define NANOGUI_USE_OPENGL +# endif #else -#define NANOGUI_USE_GLES -#define NANOGUI_GLES_VERSION 3 +# ifndef NANOGUI_USE_GLES +# define NANOGUI_USE_GLES +# define NANOGUI_GLES_VERSION 3 +# endif #endif #include diff --git a/modules/v4d/include/opencv2/v4d/v4d.hpp b/modules/v4d/include/opencv2/v4d/v4d.hpp index b6c960277..446782de2 100644 --- a/modules/v4d/include/opencv2/v4d/v4d.hpp +++ b/modules/v4d/include/opencv2/v4d/v4d.hpp @@ -21,18 +21,12 @@ #include "cxxpool.hpp" #ifdef __EMSCRIPTEN__ -#define V4D_USE_ES3 1 +#define OPENCV_V4D_USE_ES3 1 #include #endif -#ifndef V4D_USE_ES3 -#define NANOGUI_USE_OPENGL -#else -#define NANOGUI_USE_GLES -#define NANOGUI_GLES_VERSION 3 -#endif #include -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 #include #else #include diff --git a/modules/v4d/samples/shader/shader-demo.cpp b/modules/v4d/samples/shader/shader-demo.cpp index 05aa8aa12..92eb99dd0 100644 --- a/modules/v4d/samples/shader/shader-demo.cpp +++ b/modules/v4d/samples/shader/shader-demo.cpp @@ -44,7 +44,7 @@ GLint zoom_hdl; /** shader and program handle **/ GLuint shader_program_hdl; -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 //vertex array GLuint VAO; #endif @@ -71,7 +71,7 @@ unsigned int indices[] = void load_buffer_data(){ -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); #endif @@ -89,7 +89,7 @@ void load_buffer_data(){ glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 glBindVertexArray(0); #endif } @@ -141,7 +141,7 @@ GLuint init_shader(const char* vShader, const char* fShader, const char* outputA glAttachShader( program, shader ); } -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 /* Link output */ glBindFragDataLocation(program, 0, outputAttributeName); #endif @@ -170,7 +170,7 @@ GLuint init_shader(const char* vShader, const char* fShader, const char* outputA //mandelbrot shader code adapted from my own project: https://github.com/kallaballa/FractalDive#after void load_shader(){ -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 const string shaderVersion = "330"; #else const string shaderVersion = "300 es"; @@ -293,7 +293,7 @@ void render_scene(const cv::Size& sz) { glUniform1f(zoom_hdl, zoom); } -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 glBindVertexArray(VAO); #endif glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/modules/v4d/samples/tetra/tetra-demo.cpp b/modules/v4d/samples/tetra/tetra-demo.cpp index 1d5d08f7b..8e6e43a3c 100644 --- a/modules/v4d/samples/tetra/tetra-demo.cpp +++ b/modules/v4d/samples/tetra/tetra-demo.cpp @@ -8,7 +8,7 @@ constexpr long unsigned int WIDTH = 1920; constexpr long unsigned int HEIGHT = 1080; constexpr double FPS = 60; -constexpr bool OFFSCREEN = false; +constexpr bool OFFSCREEN = true; constexpr const char* OUTPUT_FILENAME = "tetra-demo.mkv"; constexpr unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT)); @@ -20,7 +20,7 @@ using std::endl; static cv::Ptr v4d = cv::viz::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Tetra Demo"); void init_scene(const cv::Size& sz) { -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 //Initialize the OpenGL scene glViewport(0, 0, sz.width, sz.height); glColor3f(1.0, 1.0, 1.0); @@ -41,7 +41,7 @@ void init_scene(const cv::Size& sz) { } void render_scene(const cv::Size& sz) { -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 //Render a tetrahedron using immediate mode because the code is more concise for a demo glViewport(0, 0, sz.width, sz.height); glRotatef(1, 0, 1, 0); diff --git a/modules/v4d/samples/video/video-demo.cpp b/modules/v4d/samples/video/video-demo.cpp index a410b8eef..6da2c0d1b 100644 --- a/modules/v4d/samples/video/video-demo.cpp +++ b/modules/v4d/samples/video/video-demo.cpp @@ -21,7 +21,7 @@ using std::string; static cv::Ptr v4d = cv::viz::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Video Demo"); void init_scene(const cv::Size& sz) { -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 //Initialize the OpenGL scene glViewport(0, 0, sz.width, sz.height); glColor3f(1.0, 1.0, 1.0); @@ -42,7 +42,7 @@ void init_scene(const cv::Size& sz) { } void render_scene(const cv::Size& sz) { -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 //Render a tetrahedron using immediate mode because the code is more concise for a demo glViewport(0, 0, sz.width, sz.height); glRotatef(1, 0, 1, 0); diff --git a/modules/v4d/src/detail/clvacontext.hpp b/modules/v4d/src/detail/clvacontext.hpp index 84e373f54..8bd9c8996 100644 --- a/modules/v4d/src/detail/clvacontext.hpp +++ b/modules/v4d/src/detail/clvacontext.hpp @@ -3,8 +3,8 @@ // of this distribution and at http://opencv.org/license.html. // Copyright Amir Hassan (kallaballa) -#ifndef SRC_COMMON_CLVACONTEXT_HPP_ -#define SRC_COMMON_CLVACONTEXT_HPP_ +#ifndef SRC_OPENCV_CLVACONTEXT_HPP_ +#define SRC_OPENCV_CLVACONTEXT_HPP_ #include "framebuffercontext.hpp" @@ -59,4 +59,4 @@ public: } } -#endif /* SRC_COMMON_CLVACONTEXT_HPP_ */ +#endif /* SRC_OPENCV_CLVACONTEXT_HPP_ */ diff --git a/modules/v4d/src/detail/framebuffercontext.cpp b/modules/v4d/src/detail/framebuffercontext.cpp index 28e9d3bf7..d2c327c05 100644 --- a/modules/v4d/src/detail/framebuffercontext.cpp +++ b/modules/v4d/src/detail/framebuffercontext.cpp @@ -8,7 +8,7 @@ #include "opencv2/v4d/util.hpp" #include "opencv2/v4d/v4d.hpp" -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 #include #endif @@ -19,7 +19,7 @@ namespace detail { //FIXME use cv::ogl FrameBufferContext::FrameBufferContext(const cv::Size& frameBufferSize) : frameBufferSize_(frameBufferSize) { -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 glewExperimental = true; glewInit(); try { diff --git a/modules/v4d/src/detail/framebuffercontext.hpp b/modules/v4d/src/detail/framebuffercontext.hpp index fd967dbda..6489227d3 100644 --- a/modules/v4d/src/detail/framebuffercontext.hpp +++ b/modules/v4d/src/detail/framebuffercontext.hpp @@ -3,22 +3,27 @@ // of this distribution and at http://opencv.org/license.html. // Copyright Amir Hassan (kallaballa) -#ifndef SRC_COMMON_CLGLCONTEXT_HPP_ -#define SRC_COMMON_CLGLCONTEXT_HPP_ +#ifndef SRC_OPENCV_FRAMEBUFFERCONTEXT_HPP_ +#define SRC_OPENCV_FRAMEBUFFERCONTEXT_HPP_ #ifndef __EMSCRIPTEN__ -#include -#include +# ifndef CL_TARGET_OPENCL_VERSION +# define CL_TARGET_OPENCL_VERSION 120 +# endif +# include +# include #else -#define V4D_USE_ES3 1 +# define OPENCV_V4D_USE_ES3 1 #endif -#ifndef V4D_USE_ES3 + +#ifndef OPENCV_V4D_USE_ES3 # include # define GLFW_INCLUDE_GLCOREARB #else # define GLFW_INCLUDE_ES3 # define GLFW_INCLUDE_GLEXT #endif + #include #include #include @@ -184,4 +189,4 @@ protected: } } -#endif /* SRC_COMMON_CLGLCONTEXT_HPP_ */ +#endif /* SRC_OPENCV_FRAMEBUFFERCONTEXT_HPP_ */ diff --git a/modules/v4d/src/detail/nanovgcontext.cpp b/modules/v4d/src/detail/nanovgcontext.cpp index 68e53262a..70a9153f0 100644 --- a/modules/v4d/src/detail/nanovgcontext.cpp +++ b/modules/v4d/src/detail/nanovgcontext.cpp @@ -27,7 +27,7 @@ void NanoVGContext::render(std::function fn) { } void push() { -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 GL_CHECK(glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS)); GL_CHECK(glPushAttrib(GL_ALL_ATTRIB_BITS)); GL_CHECK(glMatrixMode(GL_MODELVIEW)); @@ -40,7 +40,7 @@ void push() { } void pop() { -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 GL_CHECK(glMatrixMode(GL_TEXTURE)); GL_CHECK(glPopMatrix()); GL_CHECK(glMatrixMode(GL_PROJECTION)); diff --git a/modules/v4d/src/detail/nanovgcontext.hpp b/modules/v4d/src/detail/nanovgcontext.hpp index 277b16e4a..7084f0011 100644 --- a/modules/v4d/src/detail/nanovgcontext.hpp +++ b/modules/v4d/src/detail/nanovgcontext.hpp @@ -3,20 +3,18 @@ // of this distribution and at http://opencv.org/license.html. // Copyright Amir Hassan (kallaballa) -#ifndef SRC_COMMON_NANOVGCONTEXT_HPP_ -#define SRC_COMMON_NANOVGCONTEXT_HPP_ -#ifdef __EMSCRIPTEN__ -#define V4D_USE_ES3 1 -#endif +#ifndef SRC_OPENCV_NANOVGCONTEXT_HPP_ +#define SRC_OPENCV_NANOVGCONTEXT_HPP_ -#ifndef V4D_USE_ES3 +#include "framebuffercontext.hpp" +#include +#ifndef OPENCV_V4D_USE_ES3 #define NANOGUI_USE_OPENGL #else #define NANOGUI_USE_GLES #define NANOGUI_GLES_VERSION 3 #endif -#include "framebuffercontext.hpp" -#include + #include #include "opencv2/v4d/util.hpp" #include "opencv2/v4d/nvg.hpp" @@ -82,4 +80,4 @@ private: } } -#endif /* SRC_COMMON_NANOVGCONTEXT_HPP_ */ +#endif /* SRC_OPENCV_NANOVGCONTEXT_HPP_ */ diff --git a/modules/v4d/src/dialog.cpp b/modules/v4d/src/dialog.cpp index f9047b993..a774c0742 100644 --- a/modules/v4d/src/dialog.cpp +++ b/modules/v4d/src/dialog.cpp @@ -5,10 +5,7 @@ #include "opencv2/v4d/dialog.hpp" #include -#ifdef __EMSCRIPTEN__ -#define V4D_USE_ES3 1 -#endif -#ifndef V4D_USE_ES3 +#ifndef OPENCV_V4D_USE_ES3 # include # define GLFW_INCLUDE_GLCOREARB #else diff --git a/modules/v4d/src/v4d.cpp b/modules/v4d/src/v4d.cpp index bf1f1548d..1481077f0 100644 --- a/modules/v4d/src/v4d.cpp +++ b/modules/v4d/src/v4d.cpp @@ -124,7 +124,7 @@ bool V4D::initializeWindowing() { glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); -#elif defined(V4D_USE_ES3) +#elif defined(OPENCV_V4D_USE_ES3) glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);