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 (primcount
&& divisor_
) ? ((primcount
- 1) / divisor_
) :
79 bool is_client_side_array() const {
80 return is_client_side_array_
;
83 void set_is_client_side_array(bool value
) {
84 is_client_side_array_
= value
;
88 friend class VertexAttribManager
;
90 void set_enabled(bool enabled
) {
94 void set_index(GLuint index
) {
98 void SetList(VertexAttribList
* new_list
) {
105 it_
= new_list
->insert(new_list
->end(), this);
113 GLboolean normalized
,
118 void SetDivisor(GLsizei divisor
) {
122 void Unbind(Buffer
* buffer
);
124 // The index of this attrib.
127 // Whether or not this attribute is enabled.
130 // number of components (1, 2, 3, 4)
133 // GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer.
136 // The offset into the buffer.
139 GLboolean normalized_
;
141 // The stride passed to glVertexAttribPointer.
144 // The stride that will be used to access the buffer. This is the actual
145 // stide, NOT the GL bogus stride. In other words there is never a stride
147 GLsizei real_stride_
;
151 // Will be true if this was assigned to a client side array.
152 bool is_client_side_array_
;
154 // The buffer bound to this attribute.
155 scoped_refptr
<Buffer
> buffer_
;
157 // List this info is on.
158 VertexAttribList
* list_
;
160 // Iterator for list this info is on. Enabled/Disabled
161 VertexAttribList::iterator it_
;
164 // Manages vertex attributes.
165 // This class also acts as the service-side representation of a
166 // vertex array object and it's contained state.
167 class GPU_EXPORT VertexAttribManager
:
168 public base::RefCounted
<VertexAttribManager
> {
170 typedef std::list
<VertexAttrib
*> VertexAttribList
;
172 VertexAttribManager();
174 void Initialize(uint32 num_vertex_attribs
, bool init_attribs
);
176 bool Enable(GLuint index
, bool enable
);
178 bool HaveFixedAttribs() const {
179 return num_fixed_attribs_
!= 0;
182 const VertexAttribList
& GetEnabledVertexAttribs() const {
183 return enabled_vertex_attribs_
;
186 VertexAttrib
* GetVertexAttrib(GLuint index
) {
187 if (index
< vertex_attribs_
.size()) {
188 return &vertex_attribs_
[index
];
198 GLboolean normalized
,
202 VertexAttrib
* attrib
= GetVertexAttrib(index
);
204 if (attrib
->type() == GL_FIXED
) {
205 --num_fixed_attribs_
;
207 if (type
== GL_FIXED
) {
208 ++num_fixed_attribs_
;
211 buffer
, size
, type
, normalized
, gl_stride
, real_stride
, offset
);
215 void SetDivisor(GLuint index
, GLuint divisor
) {
216 VertexAttrib
* attrib
= GetVertexAttrib(index
);
218 attrib
->SetDivisor(divisor
);
222 void SetElementArrayBuffer(Buffer
* buffer
);
224 Buffer
* element_array_buffer() const { return element_array_buffer_
.get(); }
226 GLuint
service_id() const {
230 void Unbind(Buffer
* buffer
);
232 bool IsDeleted() const {
236 bool IsValid() const {
240 size_t num_attribs() const {
241 return vertex_attribs_
.size();
244 bool ValidateBindings(
245 const char* function_name
,
246 GLES2Decoder
* decoder
,
247 FeatureInfo
* feature_info
,
248 Program
* current_program
,
249 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_