summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/attribute.cpp
blob: 8c52121f6ec661a514f207fe4d63ee1781a9dc88 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <mbgl/gl/attribute.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/gl.hpp>

namespace mbgl {
namespace gl {

AttributeLocation bindAttributeLocation(ProgramID id, AttributeLocation location, const char* name) {
    MBGL_CHECK_ERROR(glBindAttribLocation(id, location, name));
    return location;
}

template <class T> DataType DataTypeOf    = static_cast<DataType>(0);
template <> DataType DataTypeOf< int8_t>  = DataType::Byte;
template <> DataType DataTypeOf<uint8_t>  = DataType::UnsignedByte;
template <> DataType DataTypeOf< int16_t> = DataType::Short;
template <> DataType DataTypeOf<uint16_t> = DataType::UnsignedShort;
template <> DataType DataTypeOf< int32_t> = DataType::Integer;
template <> DataType DataTypeOf<uint32_t> = DataType::UnsignedInteger;
template <> DataType DataTypeOf<float>    = DataType::Float;

template <class T, std::size_t N>
void VariableAttributeBinding<T, N>::bind(Context& context,
                                          AttributeLocation location,
                                          optional<VariableAttributeBinding<T, N>>& oldBinding,
                                          std::size_t vertexOffset) const {
    if (oldBinding == *this) {
        return;
    }
    context.vertexBuffer = vertexBuffer;
    MBGL_CHECK_ERROR(glEnableVertexAttribArray(location));
    oldBinding = *this;
    MBGL_CHECK_ERROR(glVertexAttribPointer(
        location,
        static_cast<GLint>(attributeSize),
        static_cast<GLenum>(DataTypeOf<T>),
        static_cast<GLboolean>(false),
        static_cast<GLsizei>(vertexSize),
        reinterpret_cast<GLvoid*>(attributeOffset + (vertexSize * vertexOffset))));
}

template class VariableAttributeBinding<uint8_t, 1>;
template class VariableAttributeBinding<uint8_t, 2>;
template class VariableAttributeBinding<uint8_t, 3>;
template class VariableAttributeBinding<uint8_t, 4>;

template class VariableAttributeBinding<uint16_t, 1>;
template class VariableAttributeBinding<uint16_t, 2>;
template class VariableAttributeBinding<uint16_t, 3>;
template class VariableAttributeBinding<uint16_t, 4>;

template class VariableAttributeBinding<int16_t, 1>;
template class VariableAttributeBinding<int16_t, 2>;
template class VariableAttributeBinding<int16_t, 3>;
template class VariableAttributeBinding<int16_t, 4>;

template class VariableAttributeBinding<float, 1>;
template class VariableAttributeBinding<float, 2>;
template class VariableAttributeBinding<float, 3>;
template class VariableAttributeBinding<float, 4>;

template <>
void ConstantAttributeBinding<uint8_t, 1>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<uint8_t, 1>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib1f(location, value[0]));
}

template <>
void ConstantAttributeBinding<uint8_t, 2>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<uint8_t, 2>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib2f(location, value[0], value[1]));
}

template <>
void ConstantAttributeBinding<uint8_t, 3>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<uint8_t, 3>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib3f(location, value[0], value[1], value[2]));
}

template <>
void ConstantAttributeBinding<uint8_t, 4>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<uint8_t, 4>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib4f(location, value[0], value[1], value[2], value[3]));
}


template <>
void ConstantAttributeBinding<uint16_t, 1>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<uint16_t, 1>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib1f(location, value[0]));
}

template <>
void ConstantAttributeBinding<uint16_t, 2>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<uint16_t, 2>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib2f(location, value[0], value[1]));
}

template <>
void ConstantAttributeBinding<uint16_t, 3>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<uint16_t, 3>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib3f(location, value[0], value[1], value[2]));
}

template <>
void ConstantAttributeBinding<uint16_t, 4>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<uint16_t, 4>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib4f(location, value[0], value[1], value[2], value[3]));
}


template <>
void ConstantAttributeBinding<int16_t, 1>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<int16_t, 1>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib1f(location, value[0]));
}

template <>
void ConstantAttributeBinding<int16_t, 2>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<int16_t, 2>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib2f(location, value[0], value[1]));
}

template <>
void ConstantAttributeBinding<int16_t, 3>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<int16_t, 3>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib3f(location, value[0], value[1], value[2]));
}

template <>
void ConstantAttributeBinding<int16_t, 4>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<int16_t, 4>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib4f(location, value[0], value[1], value[2], value[3]));
}


template <>
void ConstantAttributeBinding<float, 1>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<float, 1>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib1f(location, value[0]));
}

template <>
void ConstantAttributeBinding<float, 2>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<float, 2>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib2f(location, value[0], value[1]));
}

template <>
void ConstantAttributeBinding<float, 3>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<float, 3>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib3f(location, value[0], value[1], value[2]));
}

template <>
void ConstantAttributeBinding<float, 4>::bind(Context&, AttributeLocation location, optional<VariableAttributeBinding<float, 4>>& oldBinding, std::size_t) const {
    assert(location != 0);
    oldBinding = {};
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
    MBGL_CHECK_ERROR(glVertexAttrib4f(location, value[0], value[1], value[2], value[3]));
}

} // namespace gl
} // namespace mbgl