summaryrefslogtreecommitdiff
path: root/include/mbgl/gl/gl.hpp
blob: c849a935c20c8e469a41ce73e9937e8cf96d888d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once

#include <string>
#include <stdexcept>
#include <vector>

#if __APPLE__
    #include "TargetConditionals.h"
    #if TARGET_OS_IPHONE
        #include <OpenGLES/ES2/gl.h>
        #include <OpenGLES/ES2/glext.h>
    #elif TARGET_IPHONE_SIMULATOR
        #include <OpenGLES/ES2/gl.h>
        #include <OpenGLES/ES2/glext.h>
    #elif TARGET_OS_MAC
        #include <OpenGL/OpenGL.h>
        #include <OpenGL/gl.h>
        #include <OpenGL/glext.h>
    #else
        #error Unsupported Apple platform
    #endif
#elif __ANDROID__ || MBGL_USE_GLES2
    #define GL_GLEXT_PROTOTYPES
    #include <GLES2/gl2.h>
    #include <GLES2/gl2ext.h>
#else
    #define GL_GLEXT_PROTOTYPES
    #include <GL/gl.h>
    #include <GL/glext.h>
#endif

namespace mbgl {
namespace gl {

struct Error : std::runtime_error {
    using std::runtime_error::runtime_error;
};

void checkError(const char *cmd, const char *file, int line);

#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