summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/extension.cpp
blob: 40a2096f4f6645f63b7fd1797082dce401bb6773 (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
#include <mbgl/gl/extension.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/platform/log.hpp>

#include <mutex>
#include <string>
#include <vector>
#include <cstring>

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;

const char* getGLString(GLenum name) {
    return reinterpret_cast<const char*>(MBGL_CHECK_ERROR(glGetString(name)));
}

void InitializeExtensions(glProc (*getProcAddress)(const char*)) {
    std::call_once(initializeExtensionsOnce, [getProcAddress] {
#ifndef NDEBUG
        if (const char* vendor = getGLString(GL_VENDOR)) {
            mbgl::Log::Info(mbgl::Event::OpenGL, "Vendor: %s", vendor);
        }
        if (const char* renderer = getGLString(GL_RENDERER)) {
            mbgl::Log::Info(mbgl::Event::OpenGL, "Renderer: %s", renderer);
        }
        if (const char* version = getGLString(GL_VERSION)) {
            mbgl::Log::Info(mbgl::Event::OpenGL, "Version: %s", version);
        }
        if (const char* shaderVersion = getGLString(GL_SHADING_LANGUAGE_VERSION)) {
            mbgl::Log::Info(mbgl::Event::OpenGL, "Shading Language Version: %s", shaderVersion);
        }
#endif

        if (const char* extensions = getGLString(GL_EXTENSIONS)) {
#ifndef NDEBUG
            mbgl::Log::Info(mbgl::Event::OpenGL, "Extensions: %s", extensions);
#endif
            for (auto fn : detail::extensionFunctions()) {
                for (auto probe : fn.second) {
                    if (strstr(extensions, probe.first) != nullptr) {
                        *fn.first = getProcAddress(probe.second);
                        break;
                    }
                }
            }
        }
    });
}

} // namespace gl
} // namespace mbgl