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 LIBANGLE_RENDERER_D3D_VERTEXBUFFER_H_
11 #define LIBANGLE_RENDERER_D3D_VERTEXBUFFER_H_
12 
13 #include "common/angleutils.h"
14 #include "libANGLE/Error.h"
15 
16 #include <GLES2/gl2.h>
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <vector>
21 
22 namespace gl
23 {
24 struct VertexAttribute;
25 class VertexBinding;
26 struct VertexAttribCurrentValueData;
27 }
28 
29 namespace rx
30 {
31 class BufferFactoryD3D;
32 
33 // Use a ref-counting scheme with self-deletion on release. We do this so that we can more
34 // easily manage the static buffer cache, without deleting currently bound buffers.
35 class VertexBuffer : angle::NonCopyable
36 {
37   public:
38     VertexBuffer();
39 
40     virtual gl::Error initialize(unsigned int size, bool dynamicUsage) = 0;
41 
42     // Warning: you should ensure binding really matches attrib.bindingIndex before using this
43     // function.
44     virtual gl::Error storeVertexAttributes(const gl::VertexAttribute &attrib,
45                                             const gl::VertexBinding &binding,
46                                             GLenum currentValueType,
47                                             GLint start,
48                                             GLsizei count,
49                                             GLsizei instances,
50                                             unsigned int offset,
51                                             const uint8_t *sourceData) = 0;
52 
53     virtual unsigned int getBufferSize() const = 0;
54     virtual gl::Error setBufferSize(unsigned int size) = 0;
55     virtual gl::Error discard() = 0;
56 
57     unsigned int getSerial() const;
58 
59     // This may be overridden (e.g. by VertexBuffer11) if necessary.
hintUnmapResource()60     virtual void hintUnmapResource() { };
61 
62     // Reference counting.
63     void addRef();
64     void release();
65 
66   protected:
67     void updateSerial();
68     virtual ~VertexBuffer();
69 
70   private:
71     unsigned int mSerial;
72     static unsigned int mNextSerial;
73     unsigned int mRefCount;
74 };
75 
76 class VertexBufferInterface : angle::NonCopyable
77 {
78   public:
79     VertexBufferInterface(BufferFactoryD3D *factory, bool dynamic);
80     virtual ~VertexBufferInterface();
81 
82     unsigned int getBufferSize() const;
empty()83     bool empty() const { return getBufferSize() == 0; }
84 
85     unsigned int getSerial() const;
86 
87     VertexBuffer *getVertexBuffer() const;
88 
89   protected:
90     gl::Error discard();
91 
92     gl::Error setBufferSize(unsigned int size);
93 
94     gl::ErrorOrResult<unsigned int> getSpaceRequired(const gl::VertexAttribute &attrib,
95                                                      const gl::VertexBinding &binding,
96                                                      GLsizei count,
97                                                      GLsizei instances) const;
98     BufferFactoryD3D *const mFactory;
99     VertexBuffer *mVertexBuffer;
100     bool mDynamic;
101 };
102 
103 class StreamingVertexBufferInterface : public VertexBufferInterface
104 {
105   public:
106     StreamingVertexBufferInterface(BufferFactoryD3D *factory, std::size_t initialSize);
107     ~StreamingVertexBufferInterface() override;
108 
109     gl::Error storeDynamicAttribute(const gl::VertexAttribute &attrib,
110                                     const gl::VertexBinding &binding,
111                                     GLenum currentValueType,
112                                     GLint start,
113                                     GLsizei count,
114                                     GLsizei instances,
115                                     unsigned int *outStreamOffset,
116                                     const uint8_t *sourceData);
117 
118     gl::Error reserveVertexSpace(const gl::VertexAttribute &attribute,
119                                  const gl::VertexBinding &binding,
120                                  GLsizei count,
121                                  GLsizei instances);
122 
123   private:
124     gl::Error reserveSpace(unsigned int size);
125 
126     unsigned int mWritePosition;
127     unsigned int mReservedSpace;
128 };
129 
130 class StaticVertexBufferInterface : public VertexBufferInterface
131 {
132   public:
133     explicit StaticVertexBufferInterface(BufferFactoryD3D *factory);
134     ~StaticVertexBufferInterface() override;
135 
136     // Warning: you should ensure binding really matches attrib.bindingIndex before using these
137     // functions.
138     gl::Error storeStaticAttribute(const gl::VertexAttribute &attrib,
139                                    const gl::VertexBinding &binding,
140                                    GLint start,
141                                    GLsizei count,
142                                    GLsizei instances,
143                                    const uint8_t *sourceData);
144 
145     bool matchesAttribute(const gl::VertexAttribute &attribute,
146                           const gl::VertexBinding &binding) const;
147 
148     void setAttribute(const gl::VertexAttribute &attribute, const gl::VertexBinding &binding);
149 
150   private:
151     class AttributeSignature final : angle::NonCopyable
152     {
153       public:
154         AttributeSignature();
155 
156         bool matchesAttribute(const gl::VertexAttribute &attrib,
157                               const gl::VertexBinding &binding) const;
158 
159         void set(const gl::VertexAttribute &attrib, const gl::VertexBinding &binding);
160 
161       private:
162         GLenum type;
163         GLuint size;
164         GLuint stride;
165         bool normalized;
166         bool pureInteger;
167         size_t offset;
168     };
169 
170     AttributeSignature mSignature;
171 };
172 
173 }  // namespace rx
174 
175 #endif // LIBANGLE_RENDERER_D3D_VERTEXBUFFER_H_
176