[core] move OpenGL extension loading to their own headers

This commit is contained in:
Konstantin Käfer
2016-09-28 17:21:20 +02:00
committed by John Firebaugh
parent cc78b74098
commit 2ec2860ce5
17 changed files with 142 additions and 100 deletions

View File

@@ -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

View File

@@ -1,8 +1,6 @@
#pragma once
#include <string>
#include <stdexcept>
#include <vector>
#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<ExtensionFunctionBase*>& functions();
typedef std::pair<const char *, const char *> Probe;
std::vector<Probe> probes;
void (*ptr)();
};
template <class>
class ExtensionFunction;
template <class R, class... Args>
class ExtensionFunction<R (Args...)> : protected ExtensionFunctionBase {
public:
ExtensionFunction(std::initializer_list<Probe> probes_) {
probes = probes_;
ExtensionFunctionBase::functions().push_back(this);
}
explicit operator bool() const {
return ptr;
}
R operator()(Args... args) const {
return (*reinterpret_cast<R (*)(Args...)>(ptr))(std::forward<Args>(args)...);
}
};
using glProc = void (*)();
void InitializeExtensions(glProc (*getProcAddress)(const char *));
extern ExtensionFunction<void (GLuint array)>
BindVertexArray;
extern ExtensionFunction<void (GLsizei n, const GLuint* arrays)>
DeleteVertexArrays;
extern ExtensionFunction<void (GLsizei n, GLuint* arrays)>
GenVertexArrays;
} // namespace gl
} // namespace mbgl

View File

@@ -23,6 +23,7 @@ typedef XID GLXPbuffer;
#include <mbgl/mbgl.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/types.hpp>
#include <mbgl/gl/extension.hpp>
#include <memory>
#include <thread>

View File

@@ -15,6 +15,7 @@
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/event.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/gl/extension.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/util/constants.hpp>

View File

@@ -3,6 +3,7 @@
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/extension.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/util/string.hpp>

View File

@@ -1,7 +1,7 @@
#import "MGLMapView_Private.h"
#include <mbgl/platform/log.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/extension.hpp>
#import <GLKit/GLKit.h>
#import <OpenGLES/EAGL.h>

View File

@@ -22,6 +22,7 @@
#import <mbgl/annotation/annotation.hpp>
#import <mbgl/map/camera.hpp>
#import <mbgl/platform/darwin/reachability.h>
#import <mbgl/gl/extension.hpp>
#import <mbgl/gl/gl.hpp>
#import <mbgl/sprite/sprite_image.hpp>
#import <mbgl/storage/default_file_source.hpp>

View File

@@ -1,6 +1,6 @@
#include "qmapbox.hpp"
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/extension.hpp>
#include <mbgl/map/change.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/default_styles.hpp>

View File

@@ -1,4 +1,5 @@
#include <mbgl/geometry/vao.hpp>
#include <mbgl/gl/vertex_array.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/gl/gl.hpp>

View File

@@ -1,5 +1,6 @@
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/vertex_array.hpp>
#include <mbgl/util/traits.hpp>
namespace mbgl {

View File

@@ -1,5 +1,6 @@
#include <mbgl/gl/debugging.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/extension.hpp>
#include <mbgl/platform/event.hpp>
#include <mbgl/platform/log.hpp>

48
src/mbgl/gl/extension.cpp Normal file
View File

@@ -0,0 +1,48 @@
#include <mbgl/gl/extension.hpp>
#include <mbgl/gl/gl.hpp>
#include <mutex>
#include <string>
#include <vector>
namespace mbgl {
namespace gl {
namespace detail {
using Probes = std::vector<ExtensionFunctionBase::Probe>;
using ExtensionFunctions = std::vector<std::pair<glProc*, Probes>>;
ExtensionFunctions& extensionFunctions() {
static ExtensionFunctions functions;
return functions;
}
ExtensionFunctionBase::ExtensionFunctionBase(std::initializer_list<Probe> 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<const char*>(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

41
src/mbgl/gl/extension.hpp Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#include <initializer_list>
#include <utility>
namespace mbgl {
namespace gl {
using glProc = void (*)();
void InitializeExtensions(glProc (*getProcAddress)(const char*));
namespace detail {
class ExtensionFunctionBase {
public:
using Probe = std::pair<const char*, const char*>;
ExtensionFunctionBase(std::initializer_list<Probe>);
glProc ptr;
};
} // namespace detail
template <class>
class ExtensionFunction;
template <class R, class... Args>
class ExtensionFunction<R(Args...)> : protected detail::ExtensionFunctionBase {
public:
using detail::ExtensionFunctionBase::ExtensionFunctionBase;
explicit operator bool() const {
return ptr;
}
R operator()(Args... args) const {
return (*reinterpret_cast<R (*)(Args...)>(ptr))(std::forward<Args>(args)...);
}
};
} // namespace gl
} // namespace mbgl

View File

@@ -1,63 +1,9 @@
#include <mbgl/gl/gl.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/platform/log.hpp>
#include <cassert>
#include <iostream>
#include <unordered_map>
#include <mutex>
namespace mbgl {
namespace gl {
ExtensionFunction<void (GLuint array)>
BindVertexArray({
{"GL_ARB_vertex_array_object", "glBindVertexArray"},
{"GL_OES_vertex_array_object", "glBindVertexArrayOES"},
{"GL_APPLE_vertex_array_object", "glBindVertexArrayAPPLE"}
});
ExtensionFunction<void (GLsizei n, const GLuint* arrays)>
DeleteVertexArrays({
{"GL_ARB_vertex_array_object", "glDeleteVertexArrays"},
{"GL_OES_vertex_array_object", "glDeleteVertexArraysOES"},
{"GL_APPLE_vertex_array_object", "glDeleteVertexArraysAPPLE"}
});
ExtensionFunction<void (GLsizei n, GLuint* arrays)>
GenVertexArrays({
{"GL_ARB_vertex_array_object", "glGenVertexArrays"},
{"GL_OES_vertex_array_object", "glGenVertexArraysOES"},
{"GL_APPLE_vertex_array_object", "glGenVertexArraysAPPLE"}
});
std::vector<ExtensionFunctionBase*>& ExtensionFunctionBase::functions() {
static std::vector<ExtensionFunctionBase*> functions;
return functions;
}
static std::once_flag initializeExtensionsOnce;
void InitializeExtensions(glProc (*getProcAddress)(const char *)) {
std::call_once(initializeExtensionsOnce, [getProcAddress] {
const char * extensionsPtr = reinterpret_cast<const char *>(
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) {

View File

@@ -1,5 +1,6 @@
#include <mbgl/gl/value.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/vertex_array.hpp>
namespace mbgl {
namespace gl {

View File

@@ -0,0 +1,22 @@
#include <mbgl/gl/vertex_array.hpp>
namespace mbgl {
namespace gl {
ExtensionFunction<void(GLuint array)>
BindVertexArray({ { "GL_ARB_vertex_array_object", "glBindVertexArray" },
{ "GL_OES_vertex_array_object", "glBindVertexArrayOES" },
{ "GL_APPLE_vertex_array_object", "glBindVertexArrayAPPLE" } });
ExtensionFunction<void(GLsizei n, const GLuint* arrays)>
DeleteVertexArrays({ { "GL_ARB_vertex_array_object", "glDeleteVertexArrays" },
{ "GL_OES_vertex_array_object", "glDeleteVertexArraysOES" },
{ "GL_APPLE_vertex_array_object", "glDeleteVertexArraysAPPLE" } });
ExtensionFunction<void(GLsizei n, GLuint* arrays)>
GenVertexArrays({ { "GL_ARB_vertex_array_object", "glGenVertexArrays" },
{ "GL_OES_vertex_array_object", "glGenVertexArraysOES" },
{ "GL_APPLE_vertex_array_object", "glGenVertexArraysAPPLE" } });
} // namespace gl
} // namespace mbgl

View File

@@ -0,0 +1,14 @@
#pragma once
#include <mbgl/gl/extension.hpp>
#include <mbgl/gl/gl.hpp>
namespace mbgl {
namespace gl {
extern ExtensionFunction<void(GLuint array)> BindVertexArray;
extern ExtensionFunction<void(GLsizei n, const GLuint* arrays)> DeleteVertexArrays;
extern ExtensionFunction<void(GLsizei n, GLuint* arrays)> GenVertexArrays;
} // namespace gl
} // namespace mbgl