• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2002 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 // VertexDataManager.h: Defines the VertexDataManager, a class that
8 // runs the Buffer translation process.
9 
10 #ifndef LIBANGLE_RENDERER_D3D_VERTEXDATAMANAGER_H_
11 #define LIBANGLE_RENDERER_D3D_VERTEXDATAMANAGER_H_
12 
13 #include "common/angleutils.h"
14 #include "libANGLE/Constants.h"
15 #include "libANGLE/VertexAttribute.h"
16 #include "libANGLE/angletypes.h"
17 #include "libANGLE/renderer/d3d/VertexBuffer.h"
18 
19 namespace gl
20 {
21 class State;
22 struct VertexAttribute;
23 class VertexBinding;
24 struct VertexAttribCurrentValueData;
25 }  // namespace gl
26 
27 namespace rx
28 {
29 class BufferD3D;
30 class BufferFactoryD3D;
31 class StreamingVertexBufferInterface;
32 class VertexBuffer;
33 
34 class VertexBufferBinding final
35 {
36   public:
37     VertexBufferBinding();
38     VertexBufferBinding(const VertexBufferBinding &other);
39     ~VertexBufferBinding();
40 
41     void set(VertexBuffer *vertexBuffer);
42     VertexBuffer *get() const;
43     VertexBufferBinding &operator=(const VertexBufferBinding &other);
44 
45   private:
46     VertexBuffer *mBoundVertexBuffer;
47 };
48 
49 struct TranslatedAttribute
50 {
51     TranslatedAttribute();
52     TranslatedAttribute(const TranslatedAttribute &other);
53 
54     // Computes the correct offset from baseOffset, usesFirstVertexOffset, stride and startVertex.
55     // Can throw an error on integer overflow.
56     angle::Result computeOffset(const gl::Context *context,
57                                 GLint startVertex,
58                                 unsigned int *offsetOut) const;
59 
60     bool active;
61 
62     const gl::VertexAttribute *attribute;
63     const gl::VertexBinding *binding;
64     gl::VertexAttribType currentValueType;
65     unsigned int baseOffset;
66     bool usesFirstVertexOffset;
67     unsigned int stride;  // 0 means not to advance the read pointer at all
68 
69     VertexBufferBinding vertexBuffer;
70     BufferD3D *storage;
71     unsigned int serial;
72     unsigned int divisor;
73 };
74 
75 enum class VertexStorageType
76 {
77     UNKNOWN,
78     STATIC,         // Translate the vertex data once and re-use it.
79     DYNAMIC,        // Translate the data every frame into a ring buffer.
80     DIRECT,         // Bind a D3D buffer directly without any translation.
81     CURRENT_VALUE,  // Use a single value for the attribute.
82 };
83 
84 // Given a vertex attribute, return the type of storage it will use.
85 VertexStorageType ClassifyAttributeStorage(const gl::Context *context,
86                                            const gl::VertexAttribute &attrib,
87                                            const gl::VertexBinding &binding);
88 
89 class VertexDataManager : angle::NonCopyable
90 {
91   public:
92     VertexDataManager(BufferFactoryD3D *factory);
93     virtual ~VertexDataManager();
94 
95     angle::Result initialize(const gl::Context *context);
96     void deinitialize();
97 
98     angle::Result prepareVertexData(const gl::Context *context,
99                                     GLint start,
100                                     GLsizei count,
101                                     std::vector<TranslatedAttribute> *translatedAttribs,
102                                     GLsizei instances);
103 
104     static void StoreDirectAttrib(const gl::Context *context, TranslatedAttribute *directAttrib);
105 
106     static angle::Result StoreStaticAttrib(const gl::Context *context,
107                                            TranslatedAttribute *translated);
108 
109     angle::Result storeDynamicAttribs(const gl::Context *context,
110                                       std::vector<TranslatedAttribute> *translatedAttribs,
111                                       const gl::AttributesMask &dynamicAttribsMask,
112                                       GLint start,
113                                       size_t count,
114                                       GLsizei instances,
115                                       GLuint baseInstance);
116 
117     // Promote static usage of dynamic buffers.
118     static void PromoteDynamicAttribs(const gl::Context *context,
119                                       const std::vector<TranslatedAttribute> &translatedAttribs,
120                                       const gl::AttributesMask &dynamicAttribsMask,
121                                       size_t count);
122 
123     angle::Result storeCurrentValue(const gl::Context *context,
124                                     const gl::VertexAttribCurrentValueData &currentValue,
125                                     TranslatedAttribute *translated,
126                                     size_t attribIndex);
127 
128   private:
129     struct CurrentValueState final : angle::NonCopyable
130     {
131         CurrentValueState(BufferFactoryD3D *factory);
132         CurrentValueState(CurrentValueState &&other);
133         ~CurrentValueState();
134 
135         std::unique_ptr<StreamingVertexBufferInterface> buffer;
136         gl::VertexAttribCurrentValueData data;
137         size_t offset;
138     };
139 
140     angle::Result reserveSpaceForAttrib(const gl::Context *context,
141                                         const TranslatedAttribute &translatedAttrib,
142                                         GLint start,
143                                         size_t count,
144                                         GLsizei instances,
145                                         GLuint baseInstance);
146 
147     angle::Result storeDynamicAttrib(const gl::Context *context,
148                                      TranslatedAttribute *translated,
149                                      GLint start,
150                                      size_t count,
151                                      GLsizei instances,
152                                      GLuint baseInstance);
153 
154     BufferFactoryD3D *const mFactory;
155 
156     StreamingVertexBufferInterface mStreamingBuffer;
157     std::vector<CurrentValueState> mCurrentValueCache;
158     gl::AttributesMask mDynamicAttribsMaskCache;
159 };
160 
161 }  // namespace rx
162 
163 #endif  // LIBANGLE_RENDERER_D3D_VERTEXDATAMANAGER_H_
164