Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using Glad instead of GLEW #642

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
36f7fd1
cmake: Make templates match again
rpavlik Apr 14, 2020
9f25c70
Fix missing include guard.
rpavlik Apr 14, 2020
60de26a
Add a copy of GLAD as alternative to GLEW
rpavlik Mar 5, 2020
fba7faf
Initial port to GLAD - not completely tested
rpavlik Apr 14, 2020
b202d40
Add extensions to GLAD
rpavlik Apr 14, 2020
5f749bb
Fix extension checking.
rpavlik Apr 14, 2020
70fc1bd
Fix missing include
rpavlik Apr 14, 2020
d857468
More glClearColor disambiguation.
rpavlik Apr 14, 2020
8b38fcd
Add more extensions
rpavlik Apr 14, 2020
ecf8837
Work around GLEW-specific code in vcglib
rpavlik Apr 14, 2020
30f0bda
Include adjustment
rpavlik Apr 14, 2020
c11a434
More extensions
rpavlik Apr 14, 2020
8a7afbd
More clear color qualification
rpavlik Apr 14, 2020
b8d39d5
Fix missing include
rpavlik Apr 14, 2020
79abaf6
Qualify all remaining glClearColor calls
rpavlik Apr 14, 2020
9be9fbf
Fix missing include
rpavlik Apr 14, 2020
3c1c30d
One more glClearColor
rpavlik Apr 14, 2020
356cbaa
If you include gl_label, you also need GL/glu.h
rpavlik Apr 14, 2020
45f9b40
More includes
rpavlik Apr 14, 2020
f69272f
Make sure all files using GLU have a GLU include.
rpavlik Apr 15, 2020
774b626
Separately compile trackball vcglib code
rpavlik Apr 15, 2020
ce67626
Fix compilation issue - GLAD/Qt interference
rpavlik Apr 15, 2020
a469a75
Fix include.
rpavlik Apr 15, 2020
5c236aa
Split the GL includes and the internal includes in gl_defs
rpavlik Apr 15, 2020
1c7b750
Adjust includes in common to fix build
rpavlik Apr 15, 2020
caf2cbd
Make a header wrapping glu for Mac.
rpavlik Apr 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(BUILD_ADDITIONAL_PLUGINS

option(ALLOW_BUNDLED_EIGEN "Allow use of bundled Eigen source" ON)
option(ALLOW_BUNDLED_GLEW "Allow use of bundled GLEW source" ON)
option(ALLOW_BUNDLED_GLAD "Allow use of bundled glad source" ON)
option(ALLOW_BUNDLED_NEWUOA "Allow use of bundled newuoa source" ON)
option(ALLOW_BUNDLED_LEVMAR "Allow use of bundled levmar source" ON)
option(ALLOW_BUNDLED_LIB3DS "Allow use of bundled lib3ds source" ON)
Expand All @@ -57,6 +58,8 @@ option(ALLOW_SYSTEM_MUPARSER "Allow use of system-provided muparser" ON)
option(ALLOW_SYSTEM_OPENCTM "Allow use of system-provided OpenCTM" ON)
option(ALLOW_SYSTEM_QHULL "Allow use of system-provided QHull" ON)

option(BUILD_PREFER_GLAD "If both glew and glad are available, prefer to use glad" OFF)

### Dependencies
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
message(STATUS "Searching for required components")
Expand Down Expand Up @@ -169,11 +172,24 @@ add_subdirectory(common)
set(CMAKE_INSTALL_RPATH $ORIGIN/../${MESHLAB_LIB_INSTALL_DIR};$ORIGIN/../${CMAKE_INSTALL_LIBDIR})
add_subdirectory(meshlab)
add_subdirectory(meshlabserver)
if (WIN32)
if(WIN32)
add_subdirectory(use_cpu_opengl)
endif()
set(CMAKE_INSTALL_RPATH)

### Bits of vcglib directly included
add_library(vcglib_trackball STATIC ${VCGDIR}/wrap/gui/trackball.cpp ${VCGDIR}/wrap/gui/trackmode.cpp
${VCGDIR}/wrap/gui/trackball.h ${VCGDIR}/wrap/gui/trackmode.h)
target_link_libraries(vcglib_trackball PUBLIC external-opengl PRIVATE Qt5::Core Qt5::OpenGL)
if(MESHLAB_USE_GLAD)
# Force include to use GLAD instead of GLEW without modifying vcglib.
if(MSVC)
target_compile_options(vcglib_trackball PRIVATE /FI"glad/glad.h")
else()
target_compile_options(vcglib_trackball PRIVATE -include "glad/glad.h")
endif()
endif()

### Plugin subdirectories
if(BUILD_MINI)
# mini
Expand Down
6 changes: 4 additions & 2 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

### Generated file! Edit the templates in src/templates then re-run ./make-cmake.py

if (EXISTS ../../ML_VERSION)
if(EXISTS ../../ML_VERSION)
file(READ ../../ML_VERSION MESHLAB_VERSION)
add_definitions(-DMESHLAB_VERSION=${MESHLAB_VERSION})
endif()
Expand All @@ -30,6 +30,8 @@ set(HEADERS
GLLogStream.h
filterparameter.h
filterscript.h
gl_defs.h
glu_defs.h
interfaces.h
meshlabdocumentbundler.h
meshlabdocumentxml.h
Expand Down Expand Up @@ -63,7 +65,7 @@ target_link_libraries(
Qt5::Widgets
Qt5::Xml
Qt5::XmlPatterns
external-glew
external-opengl
PRIVATE external-jhead)

set_property(TARGET common PROPERTY FOLDER Core)
Expand Down
72 changes: 69 additions & 3 deletions src/common/gl_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,56 @@
* *
****************************************************************************/

#include "mlexception.h"
#ifndef GL_DEFS_H
#define GL_DEFS_H

#ifdef MESHLAB_USE_GLAD
#include <glad/glad.h>

// Block loading of GLEW
// #define __glew_h__

#else
#include <GL/glew.h>
#endif

#include "mlexception.h"

// This string is searched for by an error handler, so it must be included
// verbatim in all graphics-related errors.
static const char MESHLAB_GL_ERROR_MSG[] =
"OpenGL extension initialization failed";

#ifdef MESHLAB_USE_GLAD
static inline bool initializeGLextensions_notThrowing() {
if (!gladLoadGL()) {
qWarning(MESHLAB_GL_ERROR_MSG);
return false;
}
return true;
}

static inline void initializeGLextensions() {
if (!gladLoadGL()) {
throw MLException(QString("%1\n").arg(MESHLAB_GL_ERROR_MSG));
}
}

#define MESHLAB_MAKE_EXTENSION_CHECKER(EXT) \
static inline bool glExtensionsHas##EXT() { return GLAD_GL_##EXT; }

// Work around GLEW-specific code in vcglib

#define GLEW_ARB_uniform_buffer_object GLAD_GL_ARB_uniform_buffer_object
#define GLEW_EXT_transform_feedback GLAD_GL_EXT_transform_feedback

#else

static inline bool initializeGLextensions_notThrowing() {
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) {
qWarning("GLEW initialization failed: %s",
qWarning("%s: %s", MESHLAB_GL_ERROR_MSG,
(const char *)glewGetErrorString(err));
}
return err == GLEW_OK;
Expand All @@ -39,7 +80,32 @@ static inline void initializeGLextensions() {
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) {
throw MLException(QString("GLEW initialization failed: %1\n")
throw MLException(QString("%1: %2\n")
.arg(MESHLAB_GL_ERROR_MSG)
.arg((const char *)glewGetErrorString(err)));
}
}

#define MESHLAB_MAKE_EXTENSION_CHECKER(EXT) \
static inline bool glExtensionsHas##EXT() { return GLEW_##EXT; }

#endif

MESHLAB_MAKE_EXTENSION_CHECKER(ARB_draw_buffers)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_fragment_program)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_fragment_shader)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_shader_objects)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_shading_language_100)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_texture_cube_map)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_texture_float)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_texture_non_power_of_two)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_vertex_buffer_object)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_vertex_program)
MESHLAB_MAKE_EXTENSION_CHECKER(ARB_vertex_shader)
MESHLAB_MAKE_EXTENSION_CHECKER(EXT_framebuffer_object)
MESHLAB_MAKE_EXTENSION_CHECKER(EXT_gpu_shader4)
MESHLAB_MAKE_EXTENSION_CHECKER(EXT_vertex_shader)

#undef MESHLAB_MAKE_EXTENSION_CHECKER

#endif // !GL_DEFS_H
35 changes: 35 additions & 0 deletions src/common/glu_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/****************************************************************************
* MeshLab o o *
* An extendible mesh processor o o *
* _ O _ *
* Copyright(C) 2005, 2006 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/

#ifndef GLU_DEFS_H
#define GLU_DEFS_H

#include "gl_defs.h"

#ifdef __APPLE__
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#endif

#endif // !GLU_DEFS_H
3 changes: 2 additions & 1 deletion src/common/meshmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

#ifndef MESHMODEL_H
#define MESHMODEL_H
#include <GL/glew.h>

#include "gl_defs.h"

#include <stdio.h>
#include <time.h>
Expand Down
2 changes: 1 addition & 1 deletion src/common/ml_shared_data_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef __ML_SHARED_DATA_CONTEXT_H
#define __ML_SHARED_DATA_CONTEXT_H

#include <GL/glew.h>
#include "gl_defs.h"
#include <QTimer>

#include "ml_mesh_type.h"
Expand Down
50 changes: 48 additions & 2 deletions src/external.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ target_compile_definitions(external-jhead PRIVATE main=jhead_main)
target_include_directories(external-jhead PUBLIC ${JHEAD_DIR})
set_property(TARGET external-jhead PROPERTY FOLDER External)

# GLEW - required
# GLEW - either this or GLAD is required
set(GLEW_DIR ${EXTERNAL_DIR}/glew-2.1.0)
if(ALLOW_SYSTEM_GLEW AND TARGET GLEW::GLEW)
message(STATUS "- glew - using system-provided library")
Expand Down Expand Up @@ -50,10 +50,56 @@ elseif(ALLOW_BUNDLED_GLEW AND EXISTS "${GLEW_DIR}/src/glew.c")
target_link_libraries(external-glew PUBLIC OpenGL::GLX)
endif()
set_property(TARGET external-glew PROPERTY FOLDER External)
endif()

# glad - either this or GLEW is required
set(GLAD_DIR ${EXTERNAL_DIR}/glad)
if(ALLOW_BUNDLED_GLAD AND EXISTS "${GLAD_DIR}/src/glad.c")
message(STATUS "- glad - using bundled source")
add_library(external-glad STATIC "${GLAD_DIR}/src/glad.c")
target_compile_definitions(external-glad PUBLIC MESHLAB_USE_GLAD)
target_include_directories(external-glad SYSTEM PUBLIC ${GLAD_DIR}/include)
if(TARGET OpenGL::OpenGL)
target_link_libraries(external-glad PUBLIC OpenGL::OpenGL)
elseif(TARGET OpenGL::GL)
target_link_libraries(external-glad PUBLIC OpenGL::GL)
else()
message(FATAL_ERROR "OpenGL not found or your CMake version is too old!")
endif()
if(TARGET OpenGL::GLX)
target_link_libraries(external-glad PUBLIC OpenGL::GLX)
endif()
target_link_libraries(external-glad PRIVATE ${CMAKE_DL_LIBS})
set_property(TARGET external-glad PROPERTY FOLDER External)
endif()

# Determine which OpenGL wrapper to use
unset(USE_GLAD)
unset(USE_GLEW)
if(TARGET external-glad AND TARGET external-glew)
if(BUILD_PREFER_GLAD)
set(USE_GLAD ON)
else()
set(USE_GLEW ON)
endif()
elseif(TARGET external-glad)
set(USE_GLAD ON)
elseif(TARGET external-glew)
set(USE_GLEW ON)
else()
message(
FATAL_ERROR
"GLEW is required - at least one of ALLOW_SYSTEM_GLEW or ALLOW_BUNDLED_GLEW must be enabled and found.")
"glew or glad is required - at least one of ALLOW_SYSTEM_GLEW, ALLOW_BUNDLED_GLEW, or ALLOW_BUNDLED_GLAD must be enabled and found.")
endif()
set(MESHLAB_USE_GLAD "${USE_GLAD}" CACHE INTERNAL "" FORCE)
set(MESHLAB_USE_GLEW "${USE_GLEW}" CACHE INTERNAL "" FORCE)

if(USE_GLAD)
message(STATUS " - Using glad as OpenGL loader and extension handler")
add_library(external-opengl ALIAS external-glad)
elseif(USE_GLEW)
message(STATUS " - Using glew as OpenGL loader and extension handler")
add_library(external-opengl ALIAS external-glew)
endif()

# Eigen3 - required
Expand Down
Loading