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
3 // found in the LICENSE file.
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "build/build_config.h"
13 #include "gpu/command_buffer/service/buffer_manager.h"
14 #include "gpu/command_buffer/service/gl_utils.h"
15 #include "gpu/gpu_export.h"
23 class VertexArrayManager
;
25 // Info about a Vertex Attribute. This is used to track what the user currently
26 // has bound on each Vertex Attribute so that checking can be done at
28 class GPU_EXPORT VertexAttrib
{
30 typedef std::list
<VertexAttrib
*> VertexAttribList
;
35 // Returns true if this VertexAttrib can access index.
36 bool CanAccess(GLuint index
) const;
38 Buffer
* buffer() const { return buffer_
.get(); }
40 GLsizei
offset() const {
44 GLuint
index() const {
56 GLboolean
normalized() const {
60 GLsizei
gl_stride() const {
64 GLuint
divisor() const {
68 GLboolean
integer() const {
72 bool enabled() const {
76 // Find the maximum vertex accessed, accounting for instancing.
77 GLuint
MaxVertexAccessed(GLsizei primcount
,
78 GLuint max_vertex_accessed
) const {
79 return divisor_
? ((primcount
- 1) / divisor_
) : max_vertex_accessed
;
82 bool is_client_side_array() const {
83 return is_client_side_array_
;
86 void set_is_client_side_array(bool value
) {
87 is_client_side_array_
= value
;
91 friend class VertexAttribManager
;
93 void set_enabled(bool enabled
) {
97 void set_index(GLuint index
) {
101 void SetList(VertexAttribList
* new_list
) {
108 it_
= new_list
->insert(new_list
->end(), this);
116 GLboolean normalized
,
122 void SetDivisor(GLsizei divisor
) {
126 void Unbind(Buffer
* buffer
);
128 // The index of this attrib.
131 // Whether or not this attribute is enabled.
134 // number of components (1, 2, 3, 4)
137 // GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer.
140 // The offset into the buffer.
143 GLboolean normalized_
;
145 // The stride passed to glVertexAttribPointer.
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
151 GLsizei real_stride_
;
157 // Will be true if this was assigned to a client side array.
158 bool is_client_side_array_
;
160 // The buffer bound to this attribute.
161 scoped_refptr
<Buffer
> buffer_
;
163 // List this info is on.
164 VertexAttribList
* list_
;
166 // Iterator for list this info is on. Enabled/Disabled
167 VertexAttribList::iterator it_
;
170 // Manages vertex attributes.
171 // This class also acts as the service-side representation of a
172 // vertex array object and it's contained state.
173 class GPU_EXPORT VertexAttribManager
:
174 public base::RefCounted
<VertexAttribManager
> {
176 typedef std::list
<VertexAttrib
*> VertexAttribList
;
178 VertexAttribManager();
180 void Initialize(uint32 num_vertex_attribs
, bool init_attribs
);
182 bool Enable(GLuint index
, bool enable
);
184 bool HaveFixedAttribs() const {
185 return num_fixed_attribs_
!= 0;
188 const VertexAttribList
& GetEnabledVertexAttribs() const {
189 return enabled_vertex_attribs_
;
192 VertexAttrib
* GetVertexAttrib(GLuint index
) {
193 if (index
< vertex_attribs_
.size()) {
194 return &vertex_attribs_
[index
];
204 GLboolean normalized
,
209 VertexAttrib
* attrib
= GetVertexAttrib(index
);
211 if (attrib
->type() == GL_FIXED
) {
212 --num_fixed_attribs_
;
214 if (type
== GL_FIXED
) {
215 ++num_fixed_attribs_
;
217 attrib
->SetInfo(buffer
, size
, type
, normalized
, gl_stride
, real_stride
,
222 void SetDivisor(GLuint index
, GLuint divisor
) {
223 VertexAttrib
* attrib
= GetVertexAttrib(index
);
225 attrib
->SetDivisor(divisor
);
229 void SetElementArrayBuffer(Buffer
* buffer
);
231 Buffer
* element_array_buffer() const { return element_array_buffer_
.get(); }
233 GLuint
service_id() const {
237 void Unbind(Buffer
* buffer
);
239 bool IsDeleted() const {
243 bool IsValid() const {
247 size_t num_attribs() const {
248 return vertex_attribs_
.size();
251 bool ValidateBindings(
252 const char* function_name
,
253 GLES2Decoder
* decoder
,
254 FeatureInfo
* feature_info
,
255 Program
* current_program
,
256 GLuint max_vertex_accessed
,
261 friend class VertexArrayManager
;
262 friend class VertexArrayManagerTest
;
263 friend class base::RefCounted
<VertexAttribManager
>;
265 // Used when creating from a VertexArrayManager
266 VertexAttribManager(VertexArrayManager
* manager
, GLuint service_id
,
267 uint32 num_vertex_attribs
);
269 ~VertexAttribManager();
271 void MarkAsDeleted() {
275 // number of attribs using type GL_FIXED.
276 int num_fixed_attribs_
;
278 // Info for each vertex attribute saved so we can check at glDrawXXX time
279 // if it is safe to draw.
280 std::vector
<VertexAttrib
> vertex_attribs_
;
282 // The currently bound element array buffer. If this is 0 it is illegal
283 // to call glDrawElements.
284 scoped_refptr
<Buffer
> element_array_buffer_
;
286 // Lists for which vertex attribs are enabled, disabled.
287 VertexAttribList enabled_vertex_attribs_
;
288 VertexAttribList disabled_vertex_attribs_
;
290 // The VertexArrayManager that owns this VertexAttribManager
291 VertexArrayManager
* manager_
;
296 // Service side vertex array object id.
303 #endif // GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_