gl_engine: revise the gl portability

- guarantee minimum gl version requirement
- removed glesv2 dependency
- corrected gl ver dependency

issue: https://github.com/thorvg/thorvg/issues/2282
This commit is contained in:
Hermet Park 2024-06-23 19:54:35 +09:00 committed by Hermet Park
parent 9fb8a49d52
commit af1f3cb59d
8 changed files with 63 additions and 48 deletions

View file

@ -494,7 +494,7 @@ meson setup builddir -Dbindings="capi"
## Dependencies ## Dependencies
ThorVG offers versatile support for image loading, accommodating both static and external loaders. This flexibility ensures that, even in environments without external libraries, users can still leverage static loaders as a reliable alternative. At its foundation, the ThorVG core library is engineered to function autonomously, free from external dependencies. However, it is important to note that ThorVG also encompasses a range of optional feature extensions, each with its specific set of dependencies. The dependencies associated with these selective features are outlined as follows: ThorVG offers versatile support for image loading, accommodating both static and external loaders. This flexibility ensures that, even in environments without external libraries, users can still leverage static loaders as a reliable alternative. At its foundation, the ThorVG core library is engineered to function autonomously, free from external dependencies. However, it is important to note that ThorVG also encompasses a range of optional feature extensions, each with its specific set of dependencies. The dependencies associated with these selective features are outlined as follows:
* GL engine: [GLESv2/GLESv3](https://www.khronos.org/opengles/) * GL engine: [OpenGL v3.3](https://www.khronos.org/opengl/) or [GLES v3.0](https://www.khronos.org/opengles/)
* WG engine: [webgpu-native](https://github.com/gfx-rs/wgpu-native) * WG engine: [webgpu-native](https://github.com/gfx-rs/wgpu-native)
* External PNG support: [libpng](https://github.com/glennrp/libpng) * External PNG support: [libpng](https://github.com/glennrp/libpng)
* External JPG support: [turbojpeg](https://github.com/libjpeg-turbo/libjpeg-turbo) * External JPG support: [turbojpeg](https://github.com/libjpeg-turbo/libjpeg-turbo)

View file

@ -317,19 +317,16 @@ struct GlWindow : Window
GlWindow(Example* example, uint32_t width, uint32_t height) : Window(tvg::CanvasEngine::Gl, example, width, height) GlWindow(Example* example, uint32_t width, uint32_t height) : Window(tvg::CanvasEngine::Gl, example, width, height)
{ {
#ifdef THORVG_GL_TARGET_GLES
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); #else
window = SDL_CreateWindow("ThorVG Example (OpenGLES)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
if (window == nullptr) {
// try create OpenGLES context failed, create OpenGL context instead
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); #endif
window = SDL_CreateWindow("ThorVG Example (OpenGL)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE); window = SDL_CreateWindow("ThorVG Example (OpenGL/ES)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
}
context = SDL_GL_CreateContext(window); context = SDL_GL_CreateContext(window);
} }

View file

@ -2,6 +2,10 @@ if (lib_type == 'static')
compiler_flags += ['-DTVG_STATIC'] compiler_flags += ['-DTVG_STATIC']
endif endif
if target_opengles
compiler_flags += '-DTHORVG_GL_TARGET_GLES=1'
endif
examples_dep = [dependency('sdl2')] examples_dep = [dependency('sdl2')]
if (get_option('engines').contains('wg_beta')) if (get_option('engines').contains('wg_beta'))

View file

@ -144,6 +144,9 @@ configure_file(
headers = [include_directories('inc'), include_directories('.')] headers = [include_directories('inc'), include_directories('.')]
#OpenGL profile: OpenGLES(true) or OpenGL(false), confirmed by gl_engine
target_opengles = false
subdir('inc') subdir('inc')
subdir('src') subdir('src')
subdir('tools') subdir('tools')

View file

@ -21,42 +21,36 @@ source_file = [
'tvgGlTessellator.h', 'tvgGlTessellator.h',
] ]
#find a gl library with fallbacks if host_machine.system() == 'darwin'
gles_dep = meson.get_compiler('cpp').find_library('GLESv3', required: false) gl_dep = declare_dependency(link_args: ['-framework', 'OpenGL'])
if not gles_dep.found()
gles_dep = dependency('GLESv3', required: false)
endif
if not gles_dep.found()
gles_dep = meson.get_compiler('cpp').find_library('GLESv2', required: false)
endif
if not gles_dep.found()
gles_dep = dependency('GLESv2', required: false)
endif
link_opengl = false
if not gles_dep.found()
gles_dep = dependency('GL')
link_opengl = true
endif
if not gles_dep.found()
gles_dep = meson.get_compiler('cpp').find_library('GL')
link_opengl = true
endif
if link_opengl
gl_target_define = '-DTHORVG_GL_TARGET_GL=1'
else else
gl_target_define = '-DTHORVG_GL_TARGET_GLES=1' #find a opengl library with fallbacks
gl_dep = dependency('GL', required: false)
if not gl_dep.found()
gl_dep = meson.get_compiler('cpp').find_library('GL', required: false)
endif
if not gl_dep.found()
gl_dep = dependency('GLESv3', required: false)
target_opengles = true
endif
if not gl_dep.found()
gl_dep = meson.get_compiler('cpp').find_library('GLESv3')
target_opengles = true
endif
endif
if target_opengles
gl_target_profile = '-DTHORVG_GL_TARGET_GLES=1'
else
gl_target_profile = '-DTHORVG_GL_TARGET_GL=1'
endif endif
engine_dep += [declare_dependency( engine_dep += [declare_dependency(
compile_args : gl_target_define, compile_args : gl_target_profile,
dependencies : gles_dep, dependencies : gl_dep,
include_directories : include_directories('.'), include_directories : include_directories('.'),
sources : source_file, sources : source_file,
)] )]

View file

@ -26,12 +26,17 @@
#include <assert.h> #include <assert.h>
#if defined (THORVG_GL_TARGET_GLES) #if defined (THORVG_GL_TARGET_GLES)
#include <GLES3/gl3.h> #include <GLES3/gl3.h>
#elif defined (THORVG_GL_TARGET_GL) #define TVG_REQUIRE_GL_MAJOR_VER 3
#define TVG_REQUIRE_GL_MINOR_VER 0
#else
#if defined(__APPLE__) || defined(__MACH__) #if defined(__APPLE__) || defined(__MACH__)
#include <OpenGL/gl3.h> #include <OpenGL/gl3.h>
#else #else
#include <GL/gl3.h> #define GL_GLEXT_PROTOTYPES 1
#include <GL/gl.h>
#endif #endif
#define TVG_REQUIRE_GL_MAJOR_VER 3
#define TVG_REQUIRE_GL_MINOR_VER 3
#endif #endif
#include "tvgCommon.h" #include "tvgCommon.h"
#include "tvgRender.h" #include "tvgRender.h"

View file

@ -485,7 +485,7 @@ int GlRenderer::init(uint32_t threads)
{ {
if ((initEngineCnt++) > 0) return true; if ((initEngineCnt++) > 0) return true;
//TODO: //TODO: runtime linking?
return true; return true;
} }
@ -511,13 +511,25 @@ int GlRenderer::term()
GlRenderer* GlRenderer::gen() GlRenderer* GlRenderer::gen()
{ {
//TODO: GL minimum version check, should be replaced with the runtime linking in GlRenderer::init()
GLint vMajor, vMinor;
glGetIntegerv(GL_MAJOR_VERSION, &vMajor);
glGetIntegerv(GL_MINOR_VERSION, &vMinor);
if (vMajor < TVG_REQUIRE_GL_MAJOR_VER || (vMajor == TVG_REQUIRE_GL_MAJOR_VER && vMinor < TVG_REQUIRE_GL_MINOR_VER)) {
TVGERR("GL_ENGINE", "OpenGL/ES version is not statisfied. Current: v%d.%d, Required: v%d.%d", vMajor, vMinor, TVG_REQUIRE_GL_MAJOR_VER, TVG_REQUIRE_GL_MINOR_VER);
return nullptr;
}
TVGLOG("GL_ENGINE", "OpenGL/ES version = v%d.%d", vMajor, vMinor);
return new GlRenderer(); return new GlRenderer();
} }
GlRenderer::GlRenderer() :mGpuBuffer(new GlStageBuffer), mPrograms(), mComposePool() GlRenderer::GlRenderer() :mGpuBuffer(new GlStageBuffer), mPrograms(), mComposePool()
{ {
} }
GlRenderer::~GlRenderer() GlRenderer::~GlRenderer()
{ {
for (uint32_t i = 0; i < mComposePool.count; i++) { for (uint32_t i = 0; i < mComposePool.count; i++) {

View file

@ -76,7 +76,7 @@ uint32_t GlShader::complileShader(uint32_t type, char* shaderSrc)
// but in general All Desktop GPU should use OpenGL version ( #version 330 core ) // but in general All Desktop GPU should use OpenGL version ( #version 330 core )
#if defined (THORVG_GL_TARGET_GLES) #if defined (THORVG_GL_TARGET_GLES)
shaderPack[0] ="#version 300 es\n"; shaderPack[0] ="#version 300 es\n";
#elif defined (THORVG_GL_TARGET_GL) #else
shaderPack[0] ="#version 330 core\n"; shaderPack[0] ="#version 330 core\n";
#endif #endif
shaderPack[1] = "precision highp float;\n precision mediump int;\n"; shaderPack[1] = "precision highp float;\n precision mediump int;\n";