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 bool enabled() const {
72 // Find the maximum vertex accessed, accounting for instancing.
73 GLuint
MaxVertexAccessed(GLsizei primcount
,
74 GLuint max_vertex_accessed
) const {
75 return divisor_
? ((primcount
- 1) / divisor_
) : max_vertex_accessed
;
78 bool is_client_side_array() const {
79 return is_client_side_array_
;
82 void set_is_client_side_array(bool value
) {
83 is_client_side_array_
= value
;
87 friend class VertexAttribManager
;
89 void set_enabled(bool enabled
) {
93 void set_index(GLuint index
) {
97 void SetList(VertexAttribList
* new_list
) {
104 it_
= new_list
->insert(new_list
->end(), this);
112 GLboolean normalized
,
117 void SetDivisor(GLsizei divisor
) {
121 void Unbind(Buffer
* buffer
);
123 // The index of this attrib.
126 // Whether or not this attribute is enabled.
129 // number of components (1, 2, 3, 4)
132 // GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer.
135 // The offset into the buffer.
138 GLboolean normalized_
;
140 // The stride passed to glVertexAttribPointer.
143 // The stride that will be used to access the buffer. This is the actual
144 // stide, NOT the GL bogus stride. In other words there is never a stride
146 GLsizei real_stride_
;
150 // Will be true if this was assigned to a client side array.
151 bool is_client_side_array_
;
153 // The buffer bound to this attribute.
154 scoped_refptr
<Buffer
> buffer_
;
156 // List this info is on.
157 VertexAttribList
* list_
;
159 // Iterator for list this info is on. Enabled/Disabled
160 VertexAttribList::iterator it_
;
163 // Manages vertex attributes.
164 // This class also acts as the service-side representation of a
165 // vertex array object and it's contained state.
166 class GPU_EXPORT VertexAttribManager
:
167 public base::RefCounted
<VertexAttribManager
> {
169 typedef std::list
<VertexAttrib
*> VertexAttribList
;
171 VertexAttribManager();
173 void Initialize(uint32 num_vertex_attribs
, bool init_attribs
);
175 bool Enable(GLuint index
, bool enable
);
177 bool HaveFixedAttribs() const {
178 return num_fixed_attribs_
!= 0;
181 const VertexAttribList
& GetEnabledVertexAttribs() const {
182 return enabled_vertex_attribs_
;
185 VertexAttrib
* GetVertexAttrib(GLuint index
) {
186 if (index
< vertex_attribs_
.size()) {
187 return &vertex_attribs_
[index
];
197 GLboolean normalized
,
201 VertexAttrib
* attrib
= GetVertexAttrib(index
);
203 if (attrib
->type() == GL_FIXED
) {
204 --num_fixed_attribs_
;
206 if (type
== GL_FIXED
) {
207 ++num_fixed_attribs_
;
210 buffer
, size
, type
, normalized
, gl_stride
, real_stride
, offset
);
214 void SetDivisor(GLuint index
, GLuint divisor
) {
215 VertexAttrib
* attrib
= GetVertexAttrib(index
);
217 attrib
->SetDivisor(divisor
);
221 void SetElementArrayBuffer(Buffer
* buffer
);
223 Buffer
* element_array_buffer() const { return element_array_buffer_
.get(); }
225 GLuint
service_id() const {
229 void Unbind(Buffer
* buffer
);
231 bool IsDeleted() const {
235 bool IsValid() const {
239 size_t num_attribs() const {
240 return vertex_attribs_
.size();
243 bool ValidateBindings(
244 const char* function_name
,
245 GLES2Decoder
* decoder
,
246 FeatureInfo
* feature_info
,
247 Program
* current_program
,
248 GLuint max_vertex_accessed
,
253 friend class VertexArrayManager
;
254 friend class VertexArrayManagerTest
;
255 friend class base::RefCounted
<VertexAttribManager
>;
257 // Used when creating from a VertexArrayManager
258 VertexAttribManager(VertexArrayManager
* manager
, GLuint service_id
,
259 uint32 num_vertex_attribs
);
261 ~VertexAttribManager();
263 void MarkAsDeleted() {
267 // number of attribs using type GL_FIXED.
268 int num_fixed_attribs_
;
270 // Info for each vertex attribute saved so we can check at glDrawXXX time
271 // if it is safe to draw.
272 std::vector
<VertexAttrib
> vertex_attribs_
;
274 // The currently bound element array buffer. If this is 0 it is illegal
275 // to call glDrawElements.
276 scoped_refptr
<Buffer
> element_array_buffer_
;
278 // Lists for which vertex attribs are enabled, disabled.
279 VertexAttribList enabled_vertex_attribs_
;
280 VertexAttribList disabled_vertex_attribs_
;
282 // The VertexArrayManager that owns this VertexAttribManager
283 VertexArrayManager
* manager_
;
288 // Service side vertex array object id.
295 #endif // GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_