Make GL bindings conditional
[chromium-blink-merge.git] / gpu / command_buffer / service / vertex_attrib_manager.h
blob73fa4807d6e785bfa572b7c53eab385f09696db2
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 bool enabled() const {
69 return enabled_;
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;
86 private:
87 friend class VertexAttribManager;
89 void set_enabled(bool enabled) {
90 enabled_ = enabled;
93 void set_index(GLuint index) {
94 index_ = index;
97 void SetList(VertexAttribList* new_list) {
98 DCHECK(new_list);
100 if (list_) {
101 list_->erase(it_);
104 it_ = new_list->insert(new_list->end(), this);
105 list_ = new_list;
108 void SetInfo(
109 Buffer* buffer,
110 GLint size,
111 GLenum type,
112 GLboolean normalized,
113 GLsizei gl_stride,
114 GLsizei real_stride,
115 GLsizei offset);
117 void SetDivisor(GLsizei divisor) {
118 divisor_ = divisor;
121 void Unbind(Buffer* buffer);
123 // The index of this attrib.
124 GLuint index_;
126 // Whether or not this attribute is enabled.
127 bool enabled_;
129 // number of components (1, 2, 3, 4)
130 GLint size_;
132 // GL_BYTE, GL_FLOAT, etc. See glVertexAttribPointer.
133 GLenum type_;
135 // The offset into the buffer.
136 GLsizei offset_;
138 GLboolean normalized_;
140 // The stride passed to glVertexAttribPointer.
141 GLsizei gl_stride_;
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
145 // of 0.
146 GLsizei real_stride_;
148 GLsizei divisor_;
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> {
168 public:
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];
189 return NULL;
192 void SetAttribInfo(
193 GLuint index,
194 Buffer* buffer,
195 GLint size,
196 GLenum type,
197 GLboolean normalized,
198 GLsizei gl_stride,
199 GLsizei real_stride,
200 GLsizei offset) {
201 VertexAttrib* attrib = GetVertexAttrib(index);
202 if (attrib) {
203 if (attrib->type() == GL_FIXED) {
204 --num_fixed_attribs_;
206 if (type == GL_FIXED) {
207 ++num_fixed_attribs_;
209 attrib->SetInfo(
210 buffer, size, type, normalized, gl_stride, real_stride, offset);
214 void SetDivisor(GLuint index, GLuint divisor) {
215 VertexAttrib* attrib = GetVertexAttrib(index);
216 if (attrib) {
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 {
226 return service_id_;
229 void Unbind(Buffer* buffer);
231 bool IsDeleted() const {
232 return deleted_;
235 bool IsValid() const {
236 return !IsDeleted();
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,
249 bool instanced,
250 GLsizei primcount);
252 private:
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() {
264 deleted_ = true;
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_;
285 // True if deleted.
286 bool deleted_;
288 // Service side vertex array object id.
289 GLuint service_id_;
292 } // namespace gles2
293 } // namespace gpu
295 #endif // GPU_COMMAND_BUFFER_SERVICE_VERTEX_ATTRIB_MANAGER_H_