Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / service / vertex_attrib_manager.h
blobf7fd4955dcaff195f7e548d38cf8b15ed4ade789
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_
8 #include <list>
9 #include <vector>
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"
17 namespace gpu {
18 namespace gles2 {
20 class FeatureInfo;
21 class GLES2Decoder;
22 class Program;
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
27 // glDrawXXX time.
28 class GPU_EXPORT VertexAttrib {
29 public:
30 typedef std::list<VertexAttrib*> VertexAttribList;
32 VertexAttrib();
33 ~VertexAttrib();
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 {
41 return offset_;
44 GLuint index() const {
45 return index_;
48 GLint size() const {
49 return size_;
52 GLenum type() const {
53 return type_;
56 GLboolean normalized() const {
57 return normalized_;
60 GLsizei gl_stride() const {
61 return gl_stride_;
64 GLuint divisor() const {
65 return divisor_;
68 GLboolean integer() const {
69 return integer_;
72 bool enabled() const {
73 return enabled_;
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;
90 private:
91 friend class VertexAttribManager;
93 void set_enabled(bool enabled) {
94 enabled_ = enabled;
97 void set_index(GLuint index) {
98 index_ = index;
101 void SetList(VertexAttribList* new_list) {
102 DCHECK(new_list);
104 if (list_) {
105 list_->erase(it_);
108 it_ = new_list->insert(new_list->end(), this);
109 list_ = new_list;
112 void SetInfo(
113 Buffer* buffer,
114 GLint size,
115 GLenum type,
116 GLboolean normalized,
117 GLsizei gl_stride,
118 GLsizei real_stride,
119 GLsizei offset,
120 GLboolean integer);
122 void SetDivisor(GLsizei divisor) {
123 divisor_ = divisor;
126 void Unbind(Buffer* buffer);
128 // The index of this attrib.
129 GLuint index_;
131 // Whether or not this attribute is enabled.
132 bool enabled_;
134 // number of components (1, 2, 3, 4)
135 GLint size_;
137 // GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer.
138 GLenum type_;
140 // The offset into the buffer.
141 GLsizei offset_;
143 GLboolean normalized_;
145 // The stride passed to glVertexAttribPointer.
146 GLsizei gl_stride_;
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_;
153 GLsizei divisor_;
155 GLboolean integer_;
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> {
175 public:
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];
196 return NULL;
199 void SetAttribInfo(
200 GLuint index,
201 Buffer* buffer,
202 GLint size,
203 GLenum type,
204 GLboolean normalized,
205 GLsizei gl_stride,
206 GLsizei real_stride,
207 GLsizei offset,
208 GLboolean integer) {
209 VertexAttrib* attrib = GetVertexAttrib(index);
210 if (attrib) {
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,
218 offset, integer);
222 void SetDivisor(GLuint index, GLuint divisor) {
223 VertexAttrib* attrib = GetVertexAttrib(index);
224 if (attrib) {
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 {
234 return service_id_;
237 void Unbind(Buffer* buffer);
239 bool IsDeleted() const {
240 return deleted_;
243 bool IsValid() const {
244 return !IsDeleted();
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,
257 bool instanced,
258 GLsizei primcount);
260 private:
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() {
272 deleted_ = true;
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_;
293 // True if deleted.
294 bool deleted_;
296 // Service side vertex array object id.
297 GLuint service_id_;
300 } // namespace gles2
301 } // namespace gpu
303 #endif // GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_