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

#include <mbgl/gl/types.hpp>
#include <mbgl/gl/object.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/vertex_buffer.hpp>
#include <mbgl/gl/index_buffer.hpp>
#include <mbgl/gl/attribute.hpp>
#include <mbgl/gl/uniform.hpp>

#include <string>

namespace mbgl {
namespace gl {

template <class P, class As, class Us>
class Program {
public:
    using Primitive = P;
    using Attributes = As;
    using Uniforms = Us;

    using Vertex = typename Attributes::Vertex;
    using UniformValues = typename Uniforms::Values;

    static_assert(std::is_standard_layout<Vertex>::value, "vertex type must use standard layout");

    Program(Context& context, const std::string& vertexSource, const std::string& fragmentSource)
        : vertexShader(context.createShader(ShaderType::Vertex, vertexSource)),
          fragmentShader(context.createShader(ShaderType::Fragment, fragmentSource)),
          program(context.createProgram(vertexShader, fragmentShader)),
          attributesState(Attributes::state(program)),
          uniformsState((context.linkProgram(program), Uniforms::state(program))) {}

    // Indexed drawing.
    template <class DrawMode>
    void draw(Context& context,
              DrawMode drawMode,
              DepthMode depthMode,
              StencilMode stencilMode,
              ColorMode colorMode,
              UniformValues&& uniformValues,
              const VertexBuffer<Vertex>& vertexBuffer,
              const IndexBuffer<DrawMode>& indexBuffer,
              const std::vector<Segment>& segments) {
        static_assert(std::is_same<Primitive, typename DrawMode::Primitive>::value, "incompatible draw mode");
        context.draw({
            std::move(drawMode),
            std::move(depthMode),
            std::move(stencilMode),
            std::move(colorMode),
            program,
            vertexBuffer.buffer,
            indexBuffer.buffer,
            segments,
            Uniforms::binder(uniformsState, std::move(uniformValues)),
            Attributes::binder(attributesState)
        });
    }

    // Unindexed drawing.
    template <class DrawMode>
    void draw(Context& context,
              DrawMode drawMode,
              DepthMode depthMode,
              StencilMode stencilMode,
              ColorMode colorMode,
              UniformValues&& uniformValues,
              const VertexBuffer<Vertex, DrawMode>& vertexBuffer) {
        static_assert(std::is_same<Primitive, typename DrawMode::Primitive>::value, "incompatible draw mode");
        context.draw({
            std::move(drawMode),
            std::move(depthMode),
            std::move(stencilMode),
            std::move(colorMode),
            program,
            vertexBuffer.buffer,
            0,
            {{ 0, 0, vertexBuffer.vertexCount, 0 }},
            Uniforms::binder(uniformsState, std::move(uniformValues)),
            Attributes::binder(attributesState)
        });
    }

private:
    UniqueShader vertexShader;
    UniqueShader fragmentShader;
    UniqueProgram program;

    typename Attributes::State attributesState;
    typename Uniforms::State uniformsState;
};

} // namespace gl
} // namespace mbgl