summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/uniform.cpp
blob: 2946980c19c819df89018384a95d8f0a8ac5b91a (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <mbgl/gl/uniform.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/size.hpp>
#include <mbgl/util/convert.hpp>

#include <memory>

namespace mbgl {
namespace gl {

UniformLocation uniformLocation(ProgramID id, const char* name) {
    return MBGL_CHECK_ERROR(glGetUniformLocation(id, name));
}

template <>
void bindUniform<float>(UniformLocation location, const float& t) {
    MBGL_CHECK_ERROR(glUniform1f(location, t));
}

template <>
void bindUniform<int32_t>(UniformLocation location, const int32_t& t) {
    MBGL_CHECK_ERROR(glUniform1i(location, t));
}

template <>
void bindUniform<std::array<float, 2>>(UniformLocation location, const std::array<float, 2>& t) {
    MBGL_CHECK_ERROR(glUniform2fv(location, 1, t.data()));
}

template <>
void bindUniform<std::array<float, 3>>(UniformLocation location, const std::array<float, 3>& t) {
    MBGL_CHECK_ERROR(glUniform3fv(location, 1, t.data()));
}

template <>
void bindUniform<std::array<float, 4>>(UniformLocation location, const std::array<float, 4>& t) {
    MBGL_CHECK_ERROR(glUniform4fv(location, 1, t.data()));
}

template <>
void bindUniform<std::array<double, 4>>(UniformLocation location, const std::array<double, 4>& t) {
    MBGL_CHECK_ERROR(glUniformMatrix2fv(location, 1, GL_FALSE, util::convert<float>(t).data()));
}

template <>
void bindUniform<std::array<double, 9>>(UniformLocation location, const std::array<double, 9>& t) {
    MBGL_CHECK_ERROR(glUniformMatrix3fv(location, 1, GL_FALSE, util::convert<float>(t).data()));
}

template <>
void bindUniform<std::array<double, 16>>(UniformLocation location, const std::array<double, 16>& t) {
    MBGL_CHECK_ERROR(glUniformMatrix4fv(location, 1, GL_FALSE, util::convert<float>(t).data()));
}


template <>
void bindUniform<bool>(UniformLocation location, const bool& t) {
    return bindUniform(location, int32_t(t));
}

template <>
void bindUniform<uint8_t>(UniformLocation location, const uint8_t& t) {
    bindUniform(location, int32_t(t));
}

template <>
void bindUniform<Color>(UniformLocation location, const Color& t) {
    bindUniform(location, std::array<float, 4> {{ t.r, t.g, t.b, t.a }});
}

template <>
void bindUniform<Size>(UniformLocation location, const Size& t) {
    bindUniform(location, util::convert<float>(std::array<uint32_t, 2> {{ t.width, t.height }}));
}

template <>
void bindUniform<std::array<uint16_t, 2>>(UniformLocation location, const std::array<uint16_t, 2>& t) {
    bindUniform(location, util::convert<float>(t));
}

// Add more as needed.

#ifndef NDEBUG

ActiveUniforms activeUniforms(ProgramID id) {
    ActiveUniforms active;

    GLint count;
    GLint maxLength;
    MBGL_CHECK_ERROR(glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &count));
    MBGL_CHECK_ERROR(glGetProgramiv(id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength));

    auto name = std::make_unique<GLchar[]>(maxLength);
    GLsizei length;
    GLint size;
    GLenum type;
    for (GLint index = 0; index < count; index++) {
        MBGL_CHECK_ERROR(
            glGetActiveUniform(id, index, maxLength, &length, &size, &type, name.get()));
        active.emplace(
            std::string{ name.get(), static_cast<size_t>(length) },
            ActiveUniform{ static_cast<size_t>(size), static_cast<UniformDataType>(type) });
    }

    return active;
}

template <>
bool verifyUniform<float>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 && uniform.type == UniformDataType::Float);
    return true;
}

template <>
bool verifyUniform<std::array<float, 2>>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 && uniform.type == UniformDataType::FloatVec2);
    return true;
}

template <>
bool verifyUniform<std::array<float, 3>>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 && uniform.type == UniformDataType::FloatVec3);
    return true;
}

template <>
bool verifyUniform<std::array<double, 16>>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 && uniform.type == UniformDataType::FloatMat4);
    return true;
}

template <>
bool verifyUniform<bool>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 &&
           (uniform.type == UniformDataType::Bool ||
            uniform.type == UniformDataType::Int ||
            uniform.type == UniformDataType::Float));
    return true;
}

template <>
bool verifyUniform<uint8_t>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 &&
           (uniform.type == UniformDataType::Int ||
            uniform.type == UniformDataType::Float ||
            uniform.type == UniformDataType::Sampler2D));
    return true;
}

template <>
bool verifyUniform<Color>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 && uniform.type == UniformDataType::FloatVec4);
    return true;
}

template <>
bool verifyUniform<Size>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 && uniform.type == UniformDataType::FloatVec2);
    return true;
}

template <>
bool verifyUniform<std::array<uint16_t, 2>>(const ActiveUniform& uniform) {
    assert(uniform.size == 1 &&
           (uniform.type == UniformDataType::IntVec2 ||
            uniform.type == UniformDataType::FloatVec2));
    return true;
}

// Add more as needed.

#endif

} // namespace gl
} // namespace mbgl