1//
2// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// VertexBuffer.h: Defines the abstract VertexBuffer class and VertexBufferInterface
8// class with derivations, classes that perform graphics API agnostic vertex buffer operations.
9
10#ifndef LIBGLESV2_RENDERER_VERTEXBUFFER_H_
11#define LIBGLESV2_RENDERER_VERTEXBUFFER_H_
12
13#include "common/angleutils.h"
14#include "libGLESv2/Error.h"
15
16#include <GLES2/gl2.h>
17
18#include <cstddef>
19#include <vector>
20
21namespace gl
22{
23struct VertexAttribute;
24struct VertexAttribCurrentValueData;
25}
26
27namespace rx
28{
29class Renderer;
30
31class VertexBuffer
32{
33  public:
34    VertexBuffer();
35    virtual ~VertexBuffer();
36
37    virtual gl::Error initialize(unsigned int size, bool dynamicUsage) = 0;
38
39    virtual gl::Error storeVertexAttributes(const gl::VertexAttribute &attrib, const gl::VertexAttribCurrentValueData &currentValue,
40                                            GLint start, GLsizei count, GLsizei instances, unsigned int offset) = 0;
41    virtual gl::Error getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count, GLsizei instances,
42                                       unsigned int *outSpaceRequired) const = 0;
43
44    virtual unsigned int getBufferSize() const = 0;
45    virtual gl::Error setBufferSize(unsigned int size) = 0;
46    virtual gl::Error discard() = 0;
47
48    unsigned int getSerial() const;
49
50  protected:
51    void updateSerial();
52
53  private:
54    DISALLOW_COPY_AND_ASSIGN(VertexBuffer);
55
56    unsigned int mSerial;
57    static unsigned int mNextSerial;
58};
59
60class VertexBufferInterface
61{
62  public:
63    VertexBufferInterface(rx::Renderer *renderer, bool dynamic);
64    virtual ~VertexBufferInterface();
65
66    gl::Error reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances);
67
68    unsigned int getBufferSize() const;
69
70    unsigned int getSerial() const;
71
72    virtual gl::Error storeVertexAttributes(const gl::VertexAttribute &attrib, const gl::VertexAttribCurrentValueData &currentValue,
73                                            GLint start, GLsizei count, GLsizei instances, unsigned int *outStreamOffset);
74
75    bool directStoragePossible(const gl::VertexAttribute &attrib,
76                               const gl::VertexAttribCurrentValueData &currentValue) const;
77
78    VertexBuffer* getVertexBuffer() const;
79
80  protected:
81    virtual gl::Error reserveSpace(unsigned int size) = 0;
82
83    unsigned int getWritePosition() const;
84    void setWritePosition(unsigned int writePosition);
85
86    gl::Error discard();
87
88    gl::Error setBufferSize(unsigned int size);
89
90  private:
91    DISALLOW_COPY_AND_ASSIGN(VertexBufferInterface);
92
93    rx::Renderer *const mRenderer;
94
95    VertexBuffer* mVertexBuffer;
96
97    unsigned int mWritePosition;
98    unsigned int mReservedSpace;
99    bool mDynamic;
100};
101
102class StreamingVertexBufferInterface : public VertexBufferInterface
103{
104  public:
105    StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize);
106    ~StreamingVertexBufferInterface();
107
108  protected:
109    gl::Error reserveSpace(unsigned int size);
110};
111
112class StaticVertexBufferInterface : public VertexBufferInterface
113{
114  public:
115    explicit StaticVertexBufferInterface(rx::Renderer *renderer);
116    ~StaticVertexBufferInterface();
117
118    gl::Error storeVertexAttributes(const gl::VertexAttribute &attrib, const gl::VertexAttribCurrentValueData &currentValue,
119                                    GLint start, GLsizei count, GLsizei instances, unsigned int *outStreamOffset);
120
121    bool lookupAttribute(const gl::VertexAttribute &attribute, unsigned int* outStreamFffset);
122
123  protected:
124    gl::Error reserveSpace(unsigned int size);
125
126  private:
127    struct VertexElement
128    {
129        GLenum type;
130        GLuint size;
131        GLuint stride;
132        bool normalized;
133        bool pureInteger;
134        size_t attributeOffset;
135
136        unsigned int streamOffset;
137    };
138
139    std::vector<VertexElement> mCache;
140};
141
142}
143
144#endif // LIBGLESV2_RENDERER_VERTEXBUFFER_H_