summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/attribute.cpp
blob: 4569e3cb32a6296640095a731ce951c6b62d25d4 (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
#include <mbgl/gl/attribute.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/gl.hpp>

#include <cstring>

namespace mbgl {
namespace gl {

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

int32_t getActiveAttributeCount(ProgramID id) {
    GLint numAttributes;
    MBGL_CHECK_ERROR(glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, &numAttributes));
    return numAttributes;
}

int32_t getMaxAttributeNameLength(ProgramID id) {
    GLint nameLength;
    MBGL_CHECK_ERROR(glGetProgramiv(id, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &nameLength));
    return nameLength;
}

std::string getAttributeName(ProgramID id, int32_t maxLength, AttributeLocation location) {
    std::string attributeName;
    attributeName.resize(maxLength);
    GLsizei actualLength;
    GLint size;
    GLenum type;
    MBGL_CHECK_ERROR(glGetActiveAttrib(id, static_cast<GLuint>(location),
                                       static_cast<GLsizei>(maxLength), &actualLength, &size, &type,
                                       const_cast<char*>(attributeName.data())));
    attributeName.resize(actualLength);
    return attributeName;
}

std::set<std::string> getActiveAttributes(ProgramID id) {
    std::set<std::string> activeAttributes;

    GLint attributeCount = getActiveAttributeCount(id);
    GLint maxAttributeLength = getMaxAttributeNameLength(id);

    for (int32_t i = 0; i < attributeCount; i++) {
        activeAttributes.emplace(getAttributeName(id, maxAttributeLength, i));
    }

    return activeAttributes;
}

void DisabledAttribute::bind(Context&, AttributeLocation location, std::size_t) const {
    MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
}

template <class T>
constexpr DataType DataTypeOf() {
    return std::is_same<T,   int8_t>::value ? DataType::Byte :
           std::is_same<T,  uint8_t>::value ? DataType::UnsignedByte :
           std::is_same<T,  int16_t>::value ? DataType::Short :
           std::is_same<T, uint16_t>::value ? DataType::UnsignedShort :
           std::is_same<T,  int32_t>::value ? DataType::Integer :
           std::is_same<T, uint32_t>::value ? DataType::UnsignedInteger :
           std::is_same<T,    float>::value ? DataType::Float : static_cast<DataType>(0);
}

template <class T, std::size_t N>
void AttributeBinding<T, N>::bind(Context& context, AttributeLocation location, std::size_t vertexOffset) const {
    // FillProgram will attempt to bind at location -1 because it includes
    // a fill-outline-color paint property but does not use it or have an
    // a_outline_color shader attribute - in this case, we have nothing to bind.
    if (location == -1) return;

    context.vertexBuffer = vertexBuffer;
    MBGL_CHECK_ERROR(glEnableVertexAttribArray(location));
    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 AttributeBinding<uint8_t, 1>;
template class AttributeBinding<uint8_t, 2>;
template class AttributeBinding<uint8_t, 3>;
template class AttributeBinding<uint8_t, 4>;

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

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

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

} // namespace gl
} // namespace mbgl