summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/gl_object_store.cpp
blob: 4948e206948cc379065578b78c8a361ada9c4adc (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <mbgl/gl/gl_object_store.hpp>

#include <cassert>

namespace mbgl {
namespace gl {

void ProgramHolder::create(GLObjectStore& objectStore_) {
    if (id) return;
    objectStore = &objectStore_;
    id = MBGL_CHECK_ERROR(glCreateProgram());
}

void ProgramHolder::reset() {
    if (!id) return;
    objectStore->abandonedPrograms.push_back(id);
    id = 0;
}

void ShaderHolder::create(GLObjectStore& objectStore_) {
    if (id) return;
    objectStore = &objectStore_;
    id = MBGL_CHECK_ERROR(glCreateShader(type));
}

void ShaderHolder::reset() {
    if (!id) return;
    objectStore->abandonedShaders.push_back(id);
    id = 0;
}

void BufferHolder::create(GLObjectStore& objectStore_) {
    if (id) return;
    objectStore = &objectStore_;
    MBGL_CHECK_ERROR(glGenBuffers(1, &id));
}

void BufferHolder::reset() {
    if (!id) return;
    objectStore->abandonedBuffers.push_back(id);
    id = 0;
}

void TextureHolder::create(GLObjectStore& objectStore_) {
    if (id) return;
    objectStore = &objectStore_;
    MBGL_CHECK_ERROR(glGenTextures(1, &id));
}

void TextureHolder::reset() {
    if (!id) return;
    objectStore->abandonedTextures.push_back(id);
    id = 0;
}

void TexturePoolHolder::create(GLObjectStore& objectStore_) {
    if (bool()) return;
    objectStore = &objectStore_;
    MBGL_CHECK_ERROR(glGenTextures(TextureMax, ids.data()));
}

void TexturePoolHolder::reset() {
    if (!bool()) return;
    for (GLuint id : ids) {
        if (id) {
            objectStore->abandonedTextures.push_back(id);
        }
    }
    ids.fill(0);
}

void VAOHolder::create(GLObjectStore& objectStore_) {
    if (id) return;
    objectStore = &objectStore_;
    MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &id));
}

void VAOHolder::reset() {
    if (!id) return;
    objectStore->abandonedVAOs.push_back(id);
    id = 0;
}

GLObjectStore::~GLObjectStore() {
    assert(abandonedPrograms.empty());
    assert(abandonedShaders.empty());
    assert(abandonedBuffers.empty());
    assert(abandonedTextures.empty());
    assert(abandonedVAOs.empty());
}

void GLObjectStore::performCleanup() {
    for (GLuint id : abandonedPrograms) {
        MBGL_CHECK_ERROR(glDeleteProgram(id));
    }
    abandonedPrograms.clear();

    for (GLuint id : abandonedShaders) {
        MBGL_CHECK_ERROR(glDeleteShader(id));
    }
    abandonedShaders.clear();

    if (!abandonedBuffers.empty()) {
        MBGL_CHECK_ERROR(glDeleteBuffers(int(abandonedBuffers.size()), abandonedBuffers.data()));
        abandonedBuffers.clear();
    }

    if (!abandonedTextures.empty()) {
        MBGL_CHECK_ERROR(glDeleteTextures(int(abandonedTextures.size()), abandonedTextures.data()));
        abandonedTextures.clear();
    }

    if (!abandonedVAOs.empty()) {
        MBGL_CHECK_ERROR(gl::DeleteVertexArrays(int(abandonedVAOs.size()), abandonedVAOs.data()));
        abandonedVAOs.clear();
    }
}

} // namespace gl
} // namespace mbgl