summaryrefslogtreecommitdiff
path: root/src/mbgl/style/paint_property_binder.hpp
blob: 964f33d2ec9c2776514cf45782d8c2706876d474 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#pragma once

#include <mbgl/programs/attributes.hpp>
#include <mbgl/gl/attribute.hpp>
#include <mbgl/gl/uniform.hpp>
#include <mbgl/util/type_list.hpp>

namespace mbgl {
namespace style {

template <class T, class A>
class ConstantPaintPropertyBinder {
public:
    using Attribute = A;
    using AttributeValue = typename Attribute::Value;
    using AttributeBinding = typename Attribute::Binding;

    ConstantPaintPropertyBinder(T constant_)
        : constant(std::move(constant_)) {
    }

    void populateVertexVector(const GeometryTileFeature&, std::size_t) {}
    void upload(gl::Context&) {}

    AttributeBinding minAttributeBinding(const PossiblyEvaluatedPropertyValue<T>& currentValue) const {
        return typename Attribute::ConstantBinding {
            Attribute::value(currentValue.constantOr(constant))
        };
    }

    AttributeBinding maxAttributeBinding(const PossiblyEvaluatedPropertyValue<T>&) const {
        return AttributeBinding();
    }

    float interpolationFactor(float) const {
        return 0.0f;
    }

private:
    T constant;
};

template <class T, class A>
class SourceFunctionPaintPropertyBinder {
public:
    using Attribute = A;
    using AttributeValue = typename Attribute::Value;
    using AttributeBinding = typename Attribute::Binding;

    using Attributes = gl::Attributes<Attribute>;
    using Vertex = typename Attributes::Vertex;

    SourceFunctionPaintPropertyBinder(SourceFunction<T> function_)
        : function(std::move(function_)) {
    }

    void populateVertexVector(const GeometryTileFeature& feature, std::size_t length) {
        AttributeValue value = Attribute::value(function.evaluate(feature));
        for (std::size_t i = vertexVector.vertexSize(); i < length; ++i) {
            vertexVector.emplace_back(Vertex { value });
        }
    }

    void upload(gl::Context& context) {
        vertexBuffer = context.createVertexBuffer(std::move(vertexVector));
    }

    AttributeBinding minAttributeBinding(const PossiblyEvaluatedPropertyValue<T>& currentValue) const {
        if (currentValue.isConstant()) {
            return typename Attribute::ConstantBinding {
                Attribute::value(*currentValue.constant())
            };
        } else {
            return Attributes::allVariableBindings(*vertexBuffer)
                .template get<Attribute>();
        }
    }

    AttributeBinding maxAttributeBinding(const PossiblyEvaluatedPropertyValue<T>&) const {
        return AttributeBinding();
    }

    float interpolationFactor(float) const {
        return 0.0f;
    }

private:
    SourceFunction<T> function;
    gl::VertexVector<Vertex> vertexVector;
    optional<gl::VertexBuffer<Vertex>> vertexBuffer;
};

template <class T, class A>
class CompositeFunctionPaintPropertyBinder {
public:
    using Attribute = A;
    using AttributeValue = typename Attribute::Value;
    using AttributeBinding = typename Attribute::Binding;

    using MinAttribute = attributes::Min<Attribute>;
    using MaxAttribute = attributes::Max<Attribute>;

    using Attributes = gl::Attributes<MinAttribute, MaxAttribute>;
    using Vertex = typename Attributes::Vertex;

    CompositeFunctionPaintPropertyBinder(CompositeFunction<T> function_, float zoom)
        : function(std::move(function_)),
          coveringRanges(function.coveringRanges(zoom)) {
    }

    void populateVertexVector(const GeometryTileFeature& feature, std::size_t length) {
        Range<T> range = function.evaluate(std::get<1>(coveringRanges), feature);
        AttributeValue min = Attribute::value(range.min);
        AttributeValue max = Attribute::value(range.max);
        for (std::size_t i = vertexVector.vertexSize(); i < length; ++i) {
            vertexVector.emplace_back(Vertex { min, max });
        }
    }

    void upload(gl::Context& context) {
        vertexBuffer = context.createVertexBuffer(std::move(vertexVector));
    }

    AttributeBinding minAttributeBinding(const PossiblyEvaluatedPropertyValue<T>& currentValue) const {
        if (currentValue.isConstant()) {
            return typename Attribute::ConstantBinding {
                Attribute::value(*currentValue.constant())
            };
        } else {
            return Attributes::allVariableBindings(*vertexBuffer)
                .template get<MinAttribute>();
        }
    }

    AttributeBinding maxAttributeBinding(const PossiblyEvaluatedPropertyValue<T>& currentValue) const {
        if (currentValue.isConstant()) {
            return AttributeBinding();
        } else {
            return Attributes::allVariableBindings(*vertexBuffer)
                .template get<MaxAttribute>();
        }
    }

    float interpolationFactor(float currentZoom) const {
        return util::interpolationFactor(1.0f, std::get<0>(coveringRanges), currentZoom);
    }

private:
    using InnerStops = typename CompositeFunction<T>::InnerStops;
    CompositeFunction<T> function;
    std::tuple<Range<float>, Range<InnerStops>> coveringRanges;
    gl::VertexVector<Vertex> vertexVector;
    optional<gl::VertexBuffer<Vertex>> vertexBuffer;
};

template <class PaintProperty>
class PaintPropertyBinder {
public:
    using Type = typename PaintProperty::Type;
    using Attribute = typename PaintProperty::Attribute;
    using PropertyValue = typename PaintProperty::EvaluatedType;

    using Binder = variant<
        ConstantPaintPropertyBinder<Type, Attribute>,
        SourceFunctionPaintPropertyBinder<Type, Attribute>,
        CompositeFunctionPaintPropertyBinder<Type, Attribute>>;

    PaintPropertyBinder(const PropertyValue& value, float zoom)
        : binder(value.match(
            [&] (const Type& constant) -> Binder {
                return ConstantPaintPropertyBinder<Type, Attribute>(constant);
            },
            [&] (const SourceFunction<Type>& function) {
                return SourceFunctionPaintPropertyBinder<Type, Attribute>(function);
            },
            [&] (const CompositeFunction<Type>& function) {
                return CompositeFunctionPaintPropertyBinder<Type, Attribute>(function, zoom);
            }
        )) {
    }

    void populateVertexVector(const GeometryTileFeature& feature, std::size_t length) {
        binder.match([&] (auto& b) {
            b.populateVertexVector(feature, length);
        });
    }

    void upload(gl::Context& context) {
        binder.match([&] (auto& b) {
            b.upload(context);
        });
    }

    using MinAttribute = attributes::Min<Attribute>;
    using MaxAttribute = attributes::Max<Attribute>;
    using AttributeBinding = typename Attribute::Binding;

    AttributeBinding minAttributeBinding(const PropertyValue& currentValue) const {
        return binder.match([&] (const auto& b) {
            return b.minAttributeBinding(currentValue);
        });
    }

    AttributeBinding maxAttributeBinding(const PropertyValue& currentValue) const {
        return binder.match([&] (const auto& b) {
            return b.maxAttributeBinding(currentValue);
        });
    }

    using InterpolationUniform = attributes::InterpolationUniform<Attribute>;
    using InterpolationUniformValue = typename InterpolationUniform::Value;

    InterpolationUniformValue interpolationUniformValue(float currentZoom) const {
        return InterpolationUniformValue {
            binder.match([&] (const auto& b) {
                return b.interpolationFactor(currentZoom);
            })
        };
    }

private:
    Binder binder;
};

template <class Ps>
class PaintPropertyBinders;

template <class... Ps>
class PaintPropertyBinders<TypeList<Ps...>> {
public:
    using Binders = IndexedTuple<TypeList<Ps...>, TypeList<PaintPropertyBinder<Ps>...>>;

    template <class EvaluatedProperties>
    PaintPropertyBinders(const EvaluatedProperties& properties, float z)
        : binders(PaintPropertyBinder<Ps>(properties.template get<Ps>(), z)...) {
        (void)z; // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56958
    }

    void populateVertexVectors(const GeometryTileFeature& feature, std::size_t length) {
        util::ignore({
            (binders.template get<Ps>().populateVertexVector(feature, length), 0)...
        });
    }

    void upload(gl::Context& context) {
        util::ignore({
            (binders.template get<Ps>().upload(context), 0)...
        });
    }

    using MinAttributes = gl::Attributes<typename PaintPropertyBinder<Ps>::MinAttribute...>;
    using MaxAttributes = gl::Attributes<typename PaintPropertyBinder<Ps>::MaxAttribute...>;

    using Attributes = gl::ConcatenateAttributes<MinAttributes, MaxAttributes>;
    using AttributeBindings = typename Attributes::Bindings;

    template <class EvaluatedProperties>
    AttributeBindings attributeBindings(const EvaluatedProperties& currentProperties) const {
        const typename MinAttributes::Bindings min {
            binders.template get<Ps>().minAttributeBinding(currentProperties.template get<Ps>())...
        };
        const typename MaxAttributes::Bindings max {
            binders.template get<Ps>().maxAttributeBinding(currentProperties.template get<Ps>())...
        };
        return min.concat(max);
    }

    using Uniforms = gl::Uniforms<typename PaintPropertyBinder<Ps>::InterpolationUniform...>;
    using UniformValues = typename Uniforms::Values;

    UniformValues uniformValues(float currentZoom) const {
        (void)currentZoom; // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56958
        return UniformValues {
            binders.template get<Ps>().interpolationUniformValue(currentZoom)...
        };
    }

private:
    Binders binders;
};

} // namespace style
} // namespace mbgl