summaryrefslogtreecommitdiff
path: root/src/mbgl/programs/program.hpp
blob: aadbceeb06843e5456c0f68454b69605f2d4678a (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
#pragma once

#include <mbgl/gl/program.hpp>
#include <mbgl/gl/features.hpp>
#include <mbgl/programs/segment.hpp>
#include <mbgl/programs/binary_program.hpp>
#include <mbgl/programs/attributes.hpp>
#include <mbgl/programs/program_parameters.hpp>
#include <mbgl/style/paint_property.hpp>
#include <mbgl/shaders/shaders.hpp>
#include <mbgl/util/io.hpp>

#include <unordered_map>

namespace mbgl {

template <class Shaders,
          class Primitive,
          class LayoutAttrs,
          class Uniforms,
          class PaintProps>
class Program {
public:
    using LayoutAttributes = LayoutAttrs;
    using LayoutVertex = typename LayoutAttributes::Vertex;

    using PaintProperties = PaintProps;
    using PaintPropertyBinders = typename PaintProperties::Binders;
    using PaintAttributes = typename PaintPropertyBinders::Attributes;
    using Attributes = gl::ConcatenateAttributes<LayoutAttributes, PaintAttributes>;

    using UniformValues = typename Uniforms::Values;
    using PaintUniforms = typename PaintPropertyBinders::Uniforms;
    using AllUniforms = gl::ConcatenateUniforms<Uniforms, PaintUniforms>;

    using ProgramType = gl::Program<Primitive, Attributes, AllUniforms>;

    ProgramType program;

    Program(gl::Context& context, const ProgramParameters& programParameters)
        : program(ProgramType::createProgram(
            context,
            programParameters,
            Shaders::name,
            Shaders::vertexSource,
            Shaders::fragmentSource)) {
    }

    static typename AllUniforms::Values computeAllUniformValues(
        const UniformValues& uniformValues,
        const PaintPropertyBinders& paintPropertyBinders,
        const typename PaintProperties::PossiblyEvaluated& currentProperties,
        float currentZoom) {
        return uniformValues
            .concat(paintPropertyBinders.uniformValues(currentZoom, currentProperties));
    }

    static typename Attributes::Bindings computeAllAttributeBindings(
        const gl::VertexBuffer<LayoutVertex>& layoutVertexBuffer,
        const PaintPropertyBinders& paintPropertyBinders,
        const typename PaintProperties::PossiblyEvaluated& currentProperties) {
        return LayoutAttributes::bindings(layoutVertexBuffer)
            .concat(paintPropertyBinders.attributeBindings(currentProperties));
    }

    static uint32_t activeBindingCount(const typename Attributes::Bindings& allAttributeBindings) {
        return Attributes::activeBindingCount(allAttributeBindings);
    }

    template <class DrawMode>
    void draw(gl::Context& context,
              DrawMode drawMode,
              gl::DepthMode depthMode,
              gl::StencilMode stencilMode,
              gl::ColorMode colorMode,
              const gl::IndexBuffer<DrawMode>& indexBuffer,
              const SegmentVector<Attributes>& segments,
              const typename AllUniforms::Values& allUniformValues,
              const typename Attributes::Bindings& allAttributeBindings,
              const std::string& layerID) {
        for (auto& segment : segments) {
            auto vertexArrayIt = segment.vertexArrays.find(layerID);

            if (vertexArrayIt == segment.vertexArrays.end()) {
                vertexArrayIt = segment.vertexArrays.emplace(layerID, context.createVertexArray()).first;
            }

            program.draw(
                context,
                std::move(drawMode),
                std::move(depthMode),
                std::move(stencilMode),
                std::move(colorMode),
                allUniformValues,
                vertexArrayIt->second,
                Attributes::offsetBindings(allAttributeBindings, segment.vertexOffset),
                indexBuffer,
                segment.indexOffset,
                segment.indexLength);
        }
    }
};

template <class Program>
class ProgramMap {
public:
    using PaintProperties = typename Program::PaintProperties;
    using PaintPropertyBinders = typename Program::PaintPropertyBinders;
    using Bitset = typename PaintPropertyBinders::Bitset;

    ProgramMap(gl::Context& context_, ProgramParameters parameters_)
        : context(context_),
          parameters(std::move(parameters_)) {
    }

    Program& get(size_t frameID, const typename PaintProperties::PossiblyEvaluated& currentProperties) {
        Bitset bits = PaintPropertyBinders::constants(currentProperties);
        auto it = programs.find(bits);
        if (it != programs.end()) {
            std::get<size_t>(it->second) = frameID;
            return std::get<Program>(it->second);
        }
        return std::get<Program>(
            programs.emplace(std::piecewise_construct,
                std::forward_as_tuple(bits),
                std::forward_as_tuple<Program, size_t>(
                    { context, parameters.withAdditionalDefines(PaintPropertyBinders::defines(currentProperties)) },
                    std::move(frameID)
                )).first->second);
    }

    // We are periodically removing old Program objects from our cache to prevent them from piling up.
    void evictNotUsedSince(const size_t frameID) {
        for (auto it = programs.begin(); it != programs.end();) {
            if (std::get<size_t>(it->second) < frameID) {
                programs.erase(it++);
            } else {
                ++it;
            }
        }
    }

private:
    gl::Context& context;
    ProgramParameters parameters;
    std::unordered_map<Bitset, std::tuple<Program, size_t>> programs;
};

} // namespace mbgl