diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index f9081e69c..2fc17af76 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -81,6 +81,8 @@ set(MBGL_CORE_FILES src/mbgl/gl/context.hpp src/mbgl/gl/debugging.cpp src/mbgl/gl/debugging.hpp + src/mbgl/gl/extension.cpp + src/mbgl/gl/extension.hpp src/mbgl/gl/gl.cpp src/mbgl/gl/object.cpp src/mbgl/gl/object.hpp @@ -88,6 +90,8 @@ set(MBGL_CORE_FILES src/mbgl/gl/types.hpp src/mbgl/gl/value.cpp src/mbgl/gl/value.hpp + src/mbgl/gl/vertex_array.cpp + src/mbgl/gl/vertex_array.hpp # layout src/mbgl/layout/clip_lines.cpp diff --git a/include/mbgl/gl/gl.hpp b/include/mbgl/gl/gl.hpp index c849a935c..a499d731d 100644 --- a/include/mbgl/gl/gl.hpp +++ b/include/mbgl/gl/gl.hpp @@ -1,8 +1,6 @@ #pragma once -#include #include -#include #if __APPLE__ #include "TargetConditionals.h" @@ -38,51 +36,12 @@ struct Error : std::runtime_error { void checkError(const char *cmd, const char *file, int line); +} // namespace gl +} // namespace mbgl + #ifndef NDEBUG #define MBGL_CHECK_ERROR(cmd) ([&]() { struct __MBGL_C_E { ~__MBGL_C_E() { ::mbgl::gl::checkError(#cmd, __FILE__, __LINE__); } } __MBGL_C_E; return cmd; }()) #else #define MBGL_CHECK_ERROR(cmd) (cmd) #endif -class ExtensionFunctionBase { -public: - static std::vector& functions(); - typedef std::pair Probe; - std::vector probes; - void (*ptr)(); -}; - -template -class ExtensionFunction; - -template -class ExtensionFunction : protected ExtensionFunctionBase { -public: - ExtensionFunction(std::initializer_list probes_) { - probes = probes_; - ExtensionFunctionBase::functions().push_back(this); - } - - explicit operator bool() const { - return ptr; - } - - R operator()(Args... args) const { - return (*reinterpret_cast(ptr))(std::forward(args)...); - } -}; - -using glProc = void (*)(); -void InitializeExtensions(glProc (*getProcAddress)(const char *)); - -extern ExtensionFunction - BindVertexArray; - -extern ExtensionFunction - DeleteVertexArrays; - -extern ExtensionFunction - GenVertexArrays; - -} // namespace gl -} // namespace mbgl diff --git a/include/mbgl/platform/default/headless_view.hpp b/include/mbgl/platform/default/headless_view.hpp index baa6a0382..23f1e8251 100644 --- a/include/mbgl/platform/default/headless_view.hpp +++ b/include/mbgl/platform/default/headless_view.hpp @@ -23,6 +23,7 @@ typedef XID GLXPbuffer; #include #include #include +#include #include #include diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 33ba115b9..2714b832e 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 17b233a41..fa5190c80 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index f2a365f45..4d660f8f3 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -1,7 +1,7 @@ #import "MGLMapView_Private.h" #include -#include +#include #import #import diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index 13ec4508d..b40f6ebc6 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -22,6 +22,7 @@ #import #import #import +#import #import #import #import diff --git a/platform/qt/src/qmapbox.cpp b/platform/qt/src/qmapbox.cpp index 7e7a3c5f8..118b1355e 100644 --- a/platform/qt/src/qmapbox.cpp +++ b/platform/qt/src/qmapbox.cpp @@ -1,6 +1,6 @@ #include "qmapbox.hpp" -#include +#include #include #include #include diff --git a/src/mbgl/geometry/vao.cpp b/src/mbgl/geometry/vao.cpp index 214ed7a88..2c5e1677f 100644 --- a/src/mbgl/geometry/vao.cpp +++ b/src/mbgl/geometry/vao.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index 3c1b5e45a..ae50e64cb 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -1,5 +1,6 @@ #include #include +#include #include namespace mbgl { diff --git a/src/mbgl/gl/debugging.cpp b/src/mbgl/gl/debugging.cpp index e61de42bc..943710ab7 100644 --- a/src/mbgl/gl/debugging.cpp +++ b/src/mbgl/gl/debugging.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include diff --git a/src/mbgl/gl/extension.cpp b/src/mbgl/gl/extension.cpp new file mode 100644 index 000000000..ee94e8ecf --- /dev/null +++ b/src/mbgl/gl/extension.cpp @@ -0,0 +1,48 @@ +#include +#include + +#include +#include +#include + +namespace mbgl { +namespace gl { +namespace detail { + +using Probes = std::vector; +using ExtensionFunctions = std::vector>; +ExtensionFunctions& extensionFunctions() { + static ExtensionFunctions functions; + return functions; +} + +ExtensionFunctionBase::ExtensionFunctionBase(std::initializer_list probes) { + extensionFunctions().emplace_back(&ptr, probes); +} + +} // namespace detail + +static std::once_flag initializeExtensionsOnce; + +void InitializeExtensions(glProc (*getProcAddress)(const char*)) { + std::call_once(initializeExtensionsOnce, [getProcAddress] { + const char* extensionsPtr = + reinterpret_cast(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS))); + + if (!extensionsPtr) + return; + + const std::string extensions = extensionsPtr; + for (auto fn : detail::extensionFunctions()) { + for (auto probe : fn.second) { + if (extensions.find(probe.first) != std::string::npos) { + *fn.first = getProcAddress(probe.second); + break; + } + } + } + }); +} + +} // namespace gl +} // namespace mbgl diff --git a/src/mbgl/gl/extension.hpp b/src/mbgl/gl/extension.hpp new file mode 100644 index 000000000..ea5a1ae5f --- /dev/null +++ b/src/mbgl/gl/extension.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include +#include + +namespace mbgl { +namespace gl { + +using glProc = void (*)(); +void InitializeExtensions(glProc (*getProcAddress)(const char*)); + +namespace detail { + +class ExtensionFunctionBase { +public: + using Probe = std::pair; + ExtensionFunctionBase(std::initializer_list); + glProc ptr; +}; + +} // namespace detail + +template +class ExtensionFunction; + +template +class ExtensionFunction : protected detail::ExtensionFunctionBase { +public: + using detail::ExtensionFunctionBase::ExtensionFunctionBase; + + explicit operator bool() const { + return ptr; + } + + R operator()(Args... args) const { + return (*reinterpret_cast(ptr))(std::forward(args)...); + } +}; + +} // namespace gl +} // namespace mbgl diff --git a/src/mbgl/gl/gl.cpp b/src/mbgl/gl/gl.cpp index 2040ff057..5cef25416 100644 --- a/src/mbgl/gl/gl.cpp +++ b/src/mbgl/gl/gl.cpp @@ -1,63 +1,9 @@ #include #include -#include - -#include -#include -#include -#include namespace mbgl { namespace gl { -ExtensionFunction - BindVertexArray({ - {"GL_ARB_vertex_array_object", "glBindVertexArray"}, - {"GL_OES_vertex_array_object", "glBindVertexArrayOES"}, - {"GL_APPLE_vertex_array_object", "glBindVertexArrayAPPLE"} - }); - -ExtensionFunction - DeleteVertexArrays({ - {"GL_ARB_vertex_array_object", "glDeleteVertexArrays"}, - {"GL_OES_vertex_array_object", "glDeleteVertexArraysOES"}, - {"GL_APPLE_vertex_array_object", "glDeleteVertexArraysAPPLE"} - }); - -ExtensionFunction - GenVertexArrays({ - {"GL_ARB_vertex_array_object", "glGenVertexArrays"}, - {"GL_OES_vertex_array_object", "glGenVertexArraysOES"}, - {"GL_APPLE_vertex_array_object", "glGenVertexArraysAPPLE"} - }); - -std::vector& ExtensionFunctionBase::functions() { - static std::vector functions; - return functions; -} - -static std::once_flag initializeExtensionsOnce; - -void InitializeExtensions(glProc (*getProcAddress)(const char *)) { - std::call_once(initializeExtensionsOnce, [getProcAddress] { - const char * extensionsPtr = reinterpret_cast( - MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS))); - - if (!extensionsPtr) - return; - - const std::string extensions = extensionsPtr; - for (auto fn : ExtensionFunctionBase::functions()) { - for (auto probe : fn->probes) { - if (extensions.find(probe.first) != std::string::npos) { - fn->ptr = getProcAddress(probe.second); - break; - } - } - } - }); -} - namespace { constexpr const char* stringFromError(GLenum err) { diff --git a/src/mbgl/gl/value.cpp b/src/mbgl/gl/value.cpp index ae7d648a4..e78b97ef2 100644 --- a/src/mbgl/gl/value.cpp +++ b/src/mbgl/gl/value.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace mbgl { namespace gl { diff --git a/src/mbgl/gl/vertex_array.cpp b/src/mbgl/gl/vertex_array.cpp new file mode 100644 index 000000000..df63bbc4b --- /dev/null +++ b/src/mbgl/gl/vertex_array.cpp @@ -0,0 +1,22 @@ +#include + +namespace mbgl { +namespace gl { + +ExtensionFunction + BindVertexArray({ { "GL_ARB_vertex_array_object", "glBindVertexArray" }, + { "GL_OES_vertex_array_object", "glBindVertexArrayOES" }, + { "GL_APPLE_vertex_array_object", "glBindVertexArrayAPPLE" } }); + +ExtensionFunction + DeleteVertexArrays({ { "GL_ARB_vertex_array_object", "glDeleteVertexArrays" }, + { "GL_OES_vertex_array_object", "glDeleteVertexArraysOES" }, + { "GL_APPLE_vertex_array_object", "glDeleteVertexArraysAPPLE" } }); + +ExtensionFunction + GenVertexArrays({ { "GL_ARB_vertex_array_object", "glGenVertexArrays" }, + { "GL_OES_vertex_array_object", "glGenVertexArraysOES" }, + { "GL_APPLE_vertex_array_object", "glGenVertexArraysAPPLE" } }); + +} // namespace gl +} // namespace mbgl diff --git a/src/mbgl/gl/vertex_array.hpp b/src/mbgl/gl/vertex_array.hpp new file mode 100644 index 000000000..6215e56f2 --- /dev/null +++ b/src/mbgl/gl/vertex_array.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +namespace mbgl { +namespace gl { + +extern ExtensionFunction BindVertexArray; +extern ExtensionFunction DeleteVertexArrays; +extern ExtensionFunction GenVertexArrays; + +} // namespace gl +} // namespace mbgl \ No newline at end of file