summaryrefslogtreecommitdiff
path: root/src/mbgl/programs/binary_program.cpp
blob: 09b9acc514814084991bab162ee2578f8de291f4 (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
#include <mbgl/programs/binary_program.hpp>

#include <protozero/pbf_reader.hpp>
#include <protozero/pbf_writer.hpp>
#include <utility>

template <class Binding>
static std::pair<const std::string, Binding> parseBinding(protozero::pbf_reader&& pbf) {
    bool hasName = false, hasValue = false;
    std::pair<std::string, Binding> binding;
    while (pbf.next()) {
        switch (pbf.tag()) {
        case 1: // name
            binding.first = pbf.get_string();
            hasName = true;
            break;
        case 2: // value
            binding.second = pbf.get_uint32();
            hasValue = true;
            break;
        default:
            pbf.skip();
            break;
        }
    }
    if (!hasName || !hasValue) {
        throw std::runtime_error("BinaryProgram binding is missing required fields");
    }
    return binding;
}

namespace mbgl {

BinaryProgram::BinaryProgram(std::string&& data) {
    bool hasFormat = false, hasCode = false;
    protozero::pbf_reader pbf(data);
    while (pbf.next()) {
        switch (pbf.tag()) {
        case 1: // format
            binaryFormat = pbf.get_uint32();
            hasFormat = true;
            break;
        case 2: // code
            binaryCode = pbf.get_bytes();
            hasCode = true;
            break;
        case 3: // variable
            attributes.emplace_back(parseBinding<gl::AttributeLocation>(pbf.get_message()));
            break;
        case 4: // uniform
            uniforms.emplace_back(parseBinding<gl::UniformLocation>(pbf.get_message()));
            break;
        case 5: // identifier
        default:
            binaryIdentifier = pbf.get_string();
            break;
        }
    }

    if (!hasFormat || !hasCode) {
        throw std::runtime_error("BinaryProgram is missing required fields");
    }
}

BinaryProgram::BinaryProgram(
    gl::BinaryProgramFormat binaryFormat_,
    std::string&& binaryCode_,
    std::string binaryIdentifier_,
    std::vector<std::pair<const std::string, gl::AttributeLocation>>&& attributes_,
    std::vector<std::pair<const std::string, gl::UniformLocation>>&& uniforms_)
    : binaryFormat(binaryFormat_),
      binaryCode(std::move(binaryCode_)),
      binaryIdentifier(std::move(binaryIdentifier_)),
      attributes(std::move(attributes_)),
      uniforms(std::move(uniforms_)) {
}

std::string BinaryProgram::serialize() const {
    std::string data;
    data.reserve(32 + binaryCode.size() + uniforms.size() * 32 + attributes.size() * 32);
    protozero::pbf_writer pbf(data);
    pbf.add_uint32(1 /* format */, binaryFormat);
    pbf.add_bytes(2 /* code */, binaryCode.data(), binaryCode.size());
    for (const auto& binding : attributes) {
        protozero::pbf_writer pbf_binding(pbf, 3 /* attribute */);
        pbf_binding.add_string(1 /* name */, binding.first);
        pbf_binding.add_uint32(2 /* value */, binding.second);
    }
    for (const auto& binding : uniforms) {
        protozero::pbf_writer pbf_binding(pbf, 4 /* uniform */);
        pbf_binding.add_string(1 /* name */, binding.first);
        pbf_binding.add_uint32(2 /* value */, binding.second);
    }
    if (!binaryIdentifier.empty()) {
        pbf.add_string(5 /* identifier */, binaryIdentifier);
    }
    return data;
}

gl::AttributeLocation BinaryProgram::attributeLocation(const std::string& name) const {
    for (const auto& pair : attributes) {
        if (pair.first == name) {
            return pair.second;
        }
    }
    return {};
}

gl::UniformLocation BinaryProgram::uniformLocation(const std::string& name) const {
    for (const auto& pair : uniforms) {
        if (pair.first == name) {
            return pair.second;
        }
    }
    return {};
}

} // namespace mbgl