summaryrefslogtreecommitdiff
path: root/src/mbgl/geometry/vao.hpp
blob: dcb66dd41d16cc5a645e67dcc993374b7fdebed1 (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
#ifndef MBGL_GEOMETRY_VAO
#define MBGL_GEOMETRY_VAO

#include <mbgl/shader/shader.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/gl_object_store.hpp>
#include <mbgl/util/noncopyable.hpp>

#include <stdexcept>

namespace mbgl {

class Shader;

class VertexArrayObject : public util::noncopyable {
public:
    static void Unbind();

    VertexArrayObject();
    ~VertexArrayObject();

    template <typename Shader, typename VertexBuffer>
    inline void bind(Shader& shader, VertexBuffer &vertexBuffer, GLbyte *offset, gl::GLObjectStore& glObjectStore) {
        bindVertexArrayObject(glObjectStore);
        if (bound_shader == 0) {
            vertexBuffer.bind(glObjectStore);
            shader.bind(offset);
            if (vao) {
                storeBinding(shader, vertexBuffer.getID(), 0, offset);
            }
        } else {
            verifyBinding(shader, vertexBuffer.getID(), 0, offset);
        }
    }

    template <typename Shader, typename VertexBuffer, typename ElementsBuffer>
    inline void bind(Shader& shader, VertexBuffer &vertexBuffer, ElementsBuffer &elementsBuffer, GLbyte *offset, gl::GLObjectStore& glObjectStore) {
        bindVertexArrayObject(glObjectStore);
        if (bound_shader == 0) {
            vertexBuffer.bind(glObjectStore);
            elementsBuffer.bind(glObjectStore);
            shader.bind(offset);
            if (vao) {
                storeBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
            }
        } else {
            verifyBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
        }
    }

    GLuint getID() const {
        return vao.getID();
    }

private:
    void bindVertexArrayObject(gl::GLObjectStore&);
    void storeBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, GLbyte *offset);
    void verifyBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, GLbyte *offset);

    gl::VAOHolder vao;

    // For debug reasons, we're storing the bind information so that we can
    // detect errors and report
    GLuint bound_shader = 0;
    const char *bound_shader_name = "";
    GLuint bound_vertex_buffer = 0;
    GLuint bound_elements_buffer = 0;
    GLbyte *bound_offset = 0;
};

} // namespace mbgl

#endif