summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/uniform.hpp
blob: 3827015bb3855cb3a105ebbdf5d7361a75a190f3 (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
#pragma once

#include <mbgl/gl/types.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/ignore.hpp>
#include <mbgl/util/indexed_tuple.hpp>

#include <array>
#include <vector>
#include <map>
#include <functional>

namespace mbgl {
namespace gl {

template <class T>
void bindUniform(UniformLocation, const T&);

class ActiveUniform {
public:
    std::size_t size;
    UniformDataType type;
};

#ifndef NDEBUG

template <class T>
bool verifyUniform(const ActiveUniform&);

using ActiveUniforms = std::map<std::string, ActiveUniform>;
ActiveUniforms activeUniforms(ProgramID);

#endif

template <class Value>
class UniformState {
public:
    UniformState(UniformLocation location_ = -1) : location(std::move(location_)) {
    }

    void operator=(const Value& value) {
        if (location >= 0 && (!current || *current != value)) {
            current = value;
            bindUniform(location, value);
        }
    }

    UniformLocation location;
    optional<Value> current = {};
};

UniformLocation uniformLocation(ProgramID, const char * name);

template <class>
class Uniforms;

template <class... Us>
class Uniforms<TypeList<Us...>> final {
public:
    using Types = TypeList<Us...>;
    using State = IndexedTuple<TypeList<Us...>, TypeList<UniformState<typename Us::Value>...>>;
    using Values = IndexedTuple<TypeList<Us...>, TypeList<typename Us::Value...>>;
    using NamedLocations = std::vector<std::pair<const std::string, UniformLocation>>;

    static State bindLocations(const ProgramID& id) {
#ifndef NDEBUG
        // Verify active uniform types match the enum
        const auto active = activeUniforms(id);

        util::ignore(
            { // Some shader programs have uniforms declared, but not used, so they're not active.
              // Therefore, we'll only verify them when they are indeed active.
              (active.find(Us::name()) != active.end()
                   ? verifyUniform<typename Us::Value>(active.at(Us::name()))
                   : false)... });
#endif

        return State(uniformLocation(id, Us::name())...);
    }

    template <class Program>
    static State loadNamedLocations(const Program& program) {
        return State(UniformState<typename Us::Value>(program.uniformLocation(Us::name()))...);
    }

    static NamedLocations getNamedLocations(const State& state) {
        return NamedLocations{ { Us::name(), state.template get<Us>().location }... };
    }

    static void bind(State& state, const Values& values) {
        util::ignore({ (state.template get<Us>() = values.template get<Us>(), 0)... });
    }
};

} // namespace gl
} // namespace mbgl