OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_ | 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_ |
6 #define GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_ | 6 #define GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <vector> | 9 #include <vector> |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "gpu/command_buffer/service/buffer_manager.h" | 13 #include "gpu/command_buffer/service/buffer_manager.h" |
14 #include "gpu/command_buffer/service/gl_utils.h" | 14 #include "gpu/command_buffer/service/gl_utils.h" |
15 #include "gpu/gpu_export.h" | 15 #include "gpu/gpu_export.h" |
16 | 16 |
17 namespace gpu { | 17 namespace gpu { |
18 namespace gles2 { | 18 namespace gles2 { |
19 | 19 |
20 class VertexArrayManager; | 20 class VertexArrayManager; |
21 | 21 |
| 22 // Info about a Vertex Attribute. This is used to track what the user currently |
| 23 // has bound on each Vertex Attribute so that checking can be done at |
| 24 // glDrawXXX time. |
| 25 class GPU_EXPORT VertexAttrib { |
| 26 public: |
| 27 typedef std::list<VertexAttrib*> VertexAttribInfoList; |
| 28 |
| 29 VertexAttrib(); |
| 30 ~VertexAttrib(); |
| 31 |
| 32 // Returns true if this VertexAttrib can access index. |
| 33 bool CanAccess(GLuint index) const; |
| 34 |
| 35 BufferManager::Buffer* buffer() const { |
| 36 return buffer_; |
| 37 } |
| 38 |
| 39 GLsizei offset() const { |
| 40 return offset_; |
| 41 } |
| 42 |
| 43 GLuint index() const { |
| 44 return index_; |
| 45 } |
| 46 |
| 47 GLint size() const { |
| 48 return size_; |
| 49 } |
| 50 |
| 51 GLenum type() const { |
| 52 return type_; |
| 53 } |
| 54 |
| 55 GLboolean normalized() const { |
| 56 return normalized_; |
| 57 } |
| 58 |
| 59 GLsizei gl_stride() const { |
| 60 return gl_stride_; |
| 61 } |
| 62 |
| 63 GLuint divisor() const { |
| 64 return divisor_; |
| 65 } |
| 66 |
| 67 bool enabled() const { |
| 68 return enabled_; |
| 69 } |
| 70 |
| 71 // Find the maximum vertex accessed, accounting for instancing. |
| 72 GLuint MaxVertexAccessed(GLsizei primcount, |
| 73 GLuint max_vertex_accessed) const { |
| 74 return (primcount && divisor_) ? ((primcount - 1) / divisor_) : |
| 75 max_vertex_accessed; |
| 76 } |
| 77 |
| 78 private: |
| 79 friend class VertexAttribManager; |
| 80 |
| 81 void set_enabled(bool enabled) { |
| 82 enabled_ = enabled; |
| 83 } |
| 84 |
| 85 void set_index(GLuint index) { |
| 86 index_ = index; |
| 87 } |
| 88 |
| 89 void SetList(VertexAttribInfoList* new_list) { |
| 90 DCHECK(new_list); |
| 91 |
| 92 if (list_) { |
| 93 list_->erase(it_); |
| 94 } |
| 95 |
| 96 it_ = new_list->insert(new_list->end(), this); |
| 97 list_ = new_list; |
| 98 } |
| 99 |
| 100 void SetInfo( |
| 101 BufferManager::Buffer* buffer, |
| 102 GLint size, |
| 103 GLenum type, |
| 104 GLboolean normalized, |
| 105 GLsizei gl_stride, |
| 106 GLsizei real_stride, |
| 107 GLsizei offset) { |
| 108 DCHECK_GT(real_stride, 0); |
| 109 buffer_ = buffer; |
| 110 size_ = size; |
| 111 type_ = type; |
| 112 normalized_ = normalized; |
| 113 gl_stride_ = gl_stride; |
| 114 real_stride_ = real_stride; |
| 115 offset_ = offset; |
| 116 } |
| 117 |
| 118 void SetDivisor(GLsizei divisor) { |
| 119 divisor_ = divisor; |
| 120 } |
| 121 |
| 122 void Unbind(BufferManager::Buffer* buffer) { |
| 123 if (buffer_ == buffer) { |
| 124 buffer_ = NULL; |
| 125 } |
| 126 } |
| 127 |
| 128 // The index of this attrib. |
| 129 GLuint index_; |
| 130 |
| 131 // Whether or not this attribute is enabled. |
| 132 bool enabled_; |
| 133 |
| 134 // number of components (1, 2, 3, 4) |
| 135 GLint size_; |
| 136 |
| 137 // GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer. |
| 138 GLenum type_; |
| 139 |
| 140 // The offset into the buffer. |
| 141 GLsizei offset_; |
| 142 |
| 143 GLboolean normalized_; |
| 144 |
| 145 // The stride passed to glVertexAttribPointer. |
| 146 GLsizei gl_stride_; |
| 147 |
| 148 // The stride that will be used to access the buffer. This is the actual |
| 149 // stide, NOT the GL bogus stride. In other words there is never a stride |
| 150 // of 0. |
| 151 GLsizei real_stride_; |
| 152 |
| 153 GLsizei divisor_; |
| 154 |
| 155 // The buffer bound to this attribute. |
| 156 scoped_refptr<BufferManager::Buffer> buffer_; |
| 157 |
| 158 // List this info is on. |
| 159 VertexAttribInfoList* list_; |
| 160 |
| 161 // Iterator for list this info is on. Enabled/Disabled |
| 162 VertexAttribInfoList::iterator it_; |
| 163 }; |
| 164 |
22 // Manages vertex attributes. | 165 // Manages vertex attributes. |
23 // This class also acts as the service-side representation of a | 166 // This class also acts as the service-side representation of a |
24 // vertex array object and it's contained state. | 167 // vertex array object and it's contained state. |
25 class GPU_EXPORT VertexAttribManager : | 168 class GPU_EXPORT VertexAttribManager : |
26 public base::RefCounted<VertexAttribManager> { | 169 public base::RefCounted<VertexAttribManager> { |
27 public: | 170 public: |
28 typedef scoped_refptr<VertexAttribManager> Ref; | 171 typedef std::list<VertexAttrib*> VertexAttribInfoList; |
29 | |
30 // Info about Vertex Attributes. This is used to track what the user currently | |
31 // has bound on each Vertex Attribute so that checking can be done at | |
32 // glDrawXXX time. | |
33 class GPU_EXPORT VertexAttribInfo { | |
34 public: | |
35 typedef std::list<VertexAttribInfo*> VertexAttribInfoList; | |
36 | |
37 VertexAttribInfo(); | |
38 ~VertexAttribInfo(); | |
39 | |
40 // Returns true if this VertexAttrib can access index. | |
41 bool CanAccess(GLuint index) const; | |
42 | |
43 BufferManager::BufferInfo* buffer() const { | |
44 return buffer_; | |
45 } | |
46 | |
47 GLsizei offset() const { | |
48 return offset_; | |
49 } | |
50 | |
51 GLuint index() const { | |
52 return index_; | |
53 } | |
54 | |
55 GLint size() const { | |
56 return size_; | |
57 } | |
58 | |
59 GLenum type() const { | |
60 return type_; | |
61 } | |
62 | |
63 GLboolean normalized() const { | |
64 return normalized_; | |
65 } | |
66 | |
67 GLsizei gl_stride() const { | |
68 return gl_stride_; | |
69 } | |
70 | |
71 GLuint divisor() const { | |
72 return divisor_; | |
73 } | |
74 | |
75 bool enabled() const { | |
76 return enabled_; | |
77 } | |
78 | |
79 // Find the maximum vertex accessed, accounting for instancing. | |
80 GLuint MaxVertexAccessed(GLsizei primcount, | |
81 GLuint max_vertex_accessed) const { | |
82 return (primcount && divisor_) ? ((primcount - 1) / divisor_) : | |
83 max_vertex_accessed; | |
84 } | |
85 | |
86 private: | |
87 friend class VertexAttribManager; | |
88 | |
89 void set_enabled(bool enabled) { | |
90 enabled_ = enabled; | |
91 } | |
92 | |
93 void set_index(GLuint index) { | |
94 index_ = index; | |
95 } | |
96 | |
97 void SetList(VertexAttribInfoList* new_list) { | |
98 DCHECK(new_list); | |
99 | |
100 if (list_) { | |
101 list_->erase(it_); | |
102 } | |
103 | |
104 it_ = new_list->insert(new_list->end(), this); | |
105 list_ = new_list; | |
106 } | |
107 | |
108 void SetInfo( | |
109 BufferManager::BufferInfo* buffer, | |
110 GLint size, | |
111 GLenum type, | |
112 GLboolean normalized, | |
113 GLsizei gl_stride, | |
114 GLsizei real_stride, | |
115 GLsizei offset) { | |
116 DCHECK_GT(real_stride, 0); | |
117 buffer_ = buffer; | |
118 size_ = size; | |
119 type_ = type; | |
120 normalized_ = normalized; | |
121 gl_stride_ = gl_stride; | |
122 real_stride_ = real_stride; | |
123 offset_ = offset; | |
124 } | |
125 | |
126 void SetDivisor(GLsizei divisor) { | |
127 divisor_ = divisor; | |
128 } | |
129 | |
130 void Unbind(BufferManager::BufferInfo* buffer) { | |
131 if (buffer_ == buffer) { | |
132 buffer_ = NULL; | |
133 } | |
134 } | |
135 | |
136 // The index of this attrib. | |
137 GLuint index_; | |
138 | |
139 // Whether or not this attribute is enabled. | |
140 bool enabled_; | |
141 | |
142 // number of components (1, 2, 3, 4) | |
143 GLint size_; | |
144 | |
145 // GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer. | |
146 GLenum type_; | |
147 | |
148 // The offset into the buffer. | |
149 GLsizei offset_; | |
150 | |
151 GLboolean normalized_; | |
152 | |
153 // The stride passed to glVertexAttribPointer. | |
154 GLsizei gl_stride_; | |
155 | |
156 // The stride that will be used to access the buffer. This is the actual | |
157 // stide, NOT the GL bogus stride. In other words there is never a stride | |
158 // of 0. | |
159 GLsizei real_stride_; | |
160 | |
161 GLsizei divisor_; | |
162 | |
163 // The buffer bound to this attribute. | |
164 BufferManager::BufferInfo::Ref buffer_; | |
165 | |
166 // List this info is on. | |
167 VertexAttribInfoList* list_; | |
168 | |
169 // Iterator for list this info is on. Enabled/Disabled | |
170 VertexAttribInfoList::iterator it_; | |
171 }; | |
172 | |
173 typedef std::list<VertexAttribInfo*> VertexAttribInfoList; | |
174 | 172 |
175 VertexAttribManager(); | 173 VertexAttribManager(); |
176 | 174 |
177 void Initialize(uint32 num_vertex_attribs, bool init_attribs = true); | 175 void Initialize(uint32 num_vertex_attribs, bool init_attribs = true); |
178 | 176 |
179 bool Enable(GLuint index, bool enable); | 177 bool Enable(GLuint index, bool enable); |
180 | 178 |
181 bool HaveFixedAttribs() const { | 179 bool HaveFixedAttribs() const { |
182 return num_fixed_attribs_ != 0; | 180 return num_fixed_attribs_ != 0; |
183 } | 181 } |
184 | 182 |
185 const VertexAttribInfoList& GetEnabledVertexAttribInfos() const { | 183 const VertexAttribInfoList& GetEnabledVertexAttribInfos() const { |
186 return enabled_vertex_attribs_; | 184 return enabled_vertex_attribs_; |
187 } | 185 } |
188 | 186 |
189 VertexAttribInfo* GetVertexAttribInfo(GLuint index) { | 187 VertexAttrib* GetVertexAttrib(GLuint index) { |
190 if (index < vertex_attrib_infos_.size()) { | 188 if (index < vertex_attrib_infos_.size()) { |
191 return &vertex_attrib_infos_[index]; | 189 return &vertex_attrib_infos_[index]; |
192 } | 190 } |
193 return NULL; | 191 return NULL; |
194 } | 192 } |
195 | 193 |
196 void SetAttribInfo( | 194 void SetAttribInfo( |
197 GLuint index, | 195 GLuint index, |
198 BufferManager::BufferInfo* buffer, | 196 BufferManager::Buffer* buffer, |
199 GLint size, | 197 GLint size, |
200 GLenum type, | 198 GLenum type, |
201 GLboolean normalized, | 199 GLboolean normalized, |
202 GLsizei gl_stride, | 200 GLsizei gl_stride, |
203 GLsizei real_stride, | 201 GLsizei real_stride, |
204 GLsizei offset) { | 202 GLsizei offset) { |
205 VertexAttribInfo* info = GetVertexAttribInfo(index); | 203 VertexAttrib* info = GetVertexAttrib(index); |
206 if (info) { | 204 if (info) { |
207 if (info->type() == GL_FIXED) { | 205 if (info->type() == GL_FIXED) { |
208 --num_fixed_attribs_; | 206 --num_fixed_attribs_; |
209 } | 207 } |
210 if (type == GL_FIXED) { | 208 if (type == GL_FIXED) { |
211 ++num_fixed_attribs_; | 209 ++num_fixed_attribs_; |
212 } | 210 } |
213 info->SetInfo( | 211 info->SetInfo( |
214 buffer, size, type, normalized, gl_stride, real_stride, offset); | 212 buffer, size, type, normalized, gl_stride, real_stride, offset); |
215 } | 213 } |
216 } | 214 } |
217 | 215 |
218 void SetDivisor(GLuint index, GLuint divisor) { | 216 void SetDivisor(GLuint index, GLuint divisor) { |
219 VertexAttribInfo* info = GetVertexAttribInfo(index); | 217 VertexAttrib* info = GetVertexAttrib(index); |
220 if (info) { | 218 if (info) { |
221 info->SetDivisor(divisor); | 219 info->SetDivisor(divisor); |
222 } | 220 } |
223 } | 221 } |
224 | 222 |
225 void SetElementArrayBuffer(BufferManager::BufferInfo* buffer) { | 223 void SetElementArrayBuffer(BufferManager::Buffer* buffer) { |
226 element_array_buffer_ = buffer; | 224 element_array_buffer_ = buffer; |
227 } | 225 } |
228 | 226 |
229 BufferManager::BufferInfo* element_array_buffer() const { | 227 BufferManager::Buffer* element_array_buffer() const { |
230 return element_array_buffer_; | 228 return element_array_buffer_; |
231 } | 229 } |
232 | 230 |
233 GLuint service_id() const { | 231 GLuint service_id() const { |
234 return service_id_; | 232 return service_id_; |
235 } | 233 } |
236 | 234 |
237 void Unbind(BufferManager::BufferInfo* buffer); | 235 void Unbind(BufferManager::Buffer* buffer); |
238 | 236 |
239 bool IsDeleted() const { | 237 bool IsDeleted() const { |
240 return deleted_; | 238 return deleted_; |
241 } | 239 } |
242 | 240 |
243 bool IsValid() const { | 241 bool IsValid() const { |
244 return !IsDeleted(); | 242 return !IsDeleted(); |
245 } | 243 } |
246 | 244 |
247 size_t num_attribs() const { | 245 size_t num_attribs() const { |
(...skipping 13 matching lines...) Expand all Loading... |
261 | 259 |
262 void MarkAsDeleted() { | 260 void MarkAsDeleted() { |
263 deleted_ = true; | 261 deleted_ = true; |
264 } | 262 } |
265 | 263 |
266 // number of attribs using type GL_FIXED. | 264 // number of attribs using type GL_FIXED. |
267 int num_fixed_attribs_; | 265 int num_fixed_attribs_; |
268 | 266 |
269 // Info for each vertex attribute saved so we can check at glDrawXXX time | 267 // Info for each vertex attribute saved so we can check at glDrawXXX time |
270 // if it is safe to draw. | 268 // if it is safe to draw. |
271 std::vector<VertexAttribInfo> vertex_attrib_infos_; | 269 std::vector<VertexAttrib> vertex_attrib_infos_; |
272 | 270 |
273 // The currently bound element array buffer. If this is 0 it is illegal | 271 // The currently bound element array buffer. If this is 0 it is illegal |
274 // to call glDrawElements. | 272 // to call glDrawElements. |
275 BufferManager::BufferInfo::Ref element_array_buffer_; | 273 scoped_refptr<BufferManager::Buffer> element_array_buffer_; |
276 | 274 |
277 // Lists for which vertex attribs are enabled, disabled. | 275 // Lists for which vertex attribs are enabled, disabled. |
278 VertexAttribInfoList enabled_vertex_attribs_; | 276 VertexAttribInfoList enabled_vertex_attribs_; |
279 VertexAttribInfoList disabled_vertex_attribs_; | 277 VertexAttribInfoList disabled_vertex_attribs_; |
280 | 278 |
281 // The VertexArrayManager that owns this VertexAttribManager | 279 // The VertexArrayManager that owns this VertexAttribManager |
282 VertexArrayManager* manager_; | 280 VertexArrayManager* manager_; |
283 | 281 |
284 // True if deleted. | 282 // True if deleted. |
285 bool deleted_; | 283 bool deleted_; |
286 | 284 |
287 // Service side vertex array object id. | 285 // Service side vertex array object id. |
288 GLuint service_id_; | 286 GLuint service_id_; |
289 }; | 287 }; |
290 | 288 |
291 } // namespace gles2 | 289 } // namespace gles2 |
292 } // namespace gpu | 290 } // namespace gpu |
293 | 291 |
294 #endif // GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_ | 292 #endif // GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_ |
295 | 293 |
OLD | NEW |