mesa: Update VAO internal state when setting the _DrawVAO.
[mesa.git] / src / mesa / main / arrayobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * (C) Copyright IBM Corporation 2006
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /**
29 * \file arrayobj.c
30 *
31 * Implementation of Vertex Array Objects (VAOs), from OpenGL 3.1+ /
32 * the GL_ARB_vertex_array_object extension.
33 *
34 * \todo
35 * The code in this file borrows a lot from bufferobj.c. There's a certain
36 * amount of cruft left over from that origin that may be unnecessary.
37 *
38 * \author Ian Romanick <idr@us.ibm.com>
39 * \author Brian Paul
40 */
41
42
43 #include "glheader.h"
44 #include "hash.h"
45 #include "image.h"
46 #include "imports.h"
47 #include "context.h"
48 #include "bufferobj.h"
49 #include "arrayobj.h"
50 #include "macros.h"
51 #include "mtypes.h"
52 #include "state.h"
53 #include "varray.h"
54 #include "main/dispatch.h"
55 #include "util/bitscan.h"
56 #include "util/u_atomic.h"
57
58
59 const GLubyte
60 _mesa_vao_attribute_map[ATTRIBUTE_MAP_MODE_MAX][VERT_ATTRIB_MAX] =
61 {
62 /* ATTRIBUTE_MAP_MODE_IDENTITY
63 *
64 * Grab vertex processing attribute VERT_ATTRIB_POS from
65 * the VAO attribute VERT_ATTRIB_POS, and grab vertex processing
66 * attribute VERT_ATTRIB_GENERIC0 from the VAO attribute
67 * VERT_ATTRIB_GENERIC0.
68 */
69 {
70 VERT_ATTRIB_POS, /* VERT_ATTRIB_POS */
71 VERT_ATTRIB_NORMAL, /* VERT_ATTRIB_NORMAL */
72 VERT_ATTRIB_COLOR0, /* VERT_ATTRIB_COLOR0 */
73 VERT_ATTRIB_COLOR1, /* VERT_ATTRIB_COLOR1 */
74 VERT_ATTRIB_FOG, /* VERT_ATTRIB_FOG */
75 VERT_ATTRIB_COLOR_INDEX, /* VERT_ATTRIB_COLOR_INDEX */
76 VERT_ATTRIB_EDGEFLAG, /* VERT_ATTRIB_EDGEFLAG */
77 VERT_ATTRIB_TEX0, /* VERT_ATTRIB_TEX0 */
78 VERT_ATTRIB_TEX1, /* VERT_ATTRIB_TEX1 */
79 VERT_ATTRIB_TEX2, /* VERT_ATTRIB_TEX2 */
80 VERT_ATTRIB_TEX3, /* VERT_ATTRIB_TEX3 */
81 VERT_ATTRIB_TEX4, /* VERT_ATTRIB_TEX4 */
82 VERT_ATTRIB_TEX5, /* VERT_ATTRIB_TEX5 */
83 VERT_ATTRIB_TEX6, /* VERT_ATTRIB_TEX6 */
84 VERT_ATTRIB_TEX7, /* VERT_ATTRIB_TEX7 */
85 VERT_ATTRIB_POINT_SIZE, /* VERT_ATTRIB_POINT_SIZE */
86 VERT_ATTRIB_GENERIC0, /* VERT_ATTRIB_GENERIC0 */
87 VERT_ATTRIB_GENERIC1, /* VERT_ATTRIB_GENERIC1 */
88 VERT_ATTRIB_GENERIC2, /* VERT_ATTRIB_GENERIC2 */
89 VERT_ATTRIB_GENERIC3, /* VERT_ATTRIB_GENERIC3 */
90 VERT_ATTRIB_GENERIC4, /* VERT_ATTRIB_GENERIC4 */
91 VERT_ATTRIB_GENERIC5, /* VERT_ATTRIB_GENERIC5 */
92 VERT_ATTRIB_GENERIC6, /* VERT_ATTRIB_GENERIC6 */
93 VERT_ATTRIB_GENERIC7, /* VERT_ATTRIB_GENERIC7 */
94 VERT_ATTRIB_GENERIC8, /* VERT_ATTRIB_GENERIC8 */
95 VERT_ATTRIB_GENERIC9, /* VERT_ATTRIB_GENERIC9 */
96 VERT_ATTRIB_GENERIC10, /* VERT_ATTRIB_GENERIC10 */
97 VERT_ATTRIB_GENERIC11, /* VERT_ATTRIB_GENERIC11 */
98 VERT_ATTRIB_GENERIC12, /* VERT_ATTRIB_GENERIC12 */
99 VERT_ATTRIB_GENERIC13, /* VERT_ATTRIB_GENERIC13 */
100 VERT_ATTRIB_GENERIC14, /* VERT_ATTRIB_GENERIC14 */
101 VERT_ATTRIB_GENERIC15 /* VERT_ATTRIB_GENERIC15 */
102 },
103
104 /* ATTRIBUTE_MAP_MODE_POSITION
105 *
106 * Grab vertex processing attribute VERT_ATTRIB_POS as well as
107 * vertex processing attribute VERT_ATTRIB_GENERIC0 from the
108 * VAO attribute VERT_ATTRIB_POS.
109 */
110 {
111 VERT_ATTRIB_POS, /* VERT_ATTRIB_POS */
112 VERT_ATTRIB_NORMAL, /* VERT_ATTRIB_NORMAL */
113 VERT_ATTRIB_COLOR0, /* VERT_ATTRIB_COLOR0 */
114 VERT_ATTRIB_COLOR1, /* VERT_ATTRIB_COLOR1 */
115 VERT_ATTRIB_FOG, /* VERT_ATTRIB_FOG */
116 VERT_ATTRIB_COLOR_INDEX, /* VERT_ATTRIB_COLOR_INDEX */
117 VERT_ATTRIB_EDGEFLAG, /* VERT_ATTRIB_EDGEFLAG */
118 VERT_ATTRIB_TEX0, /* VERT_ATTRIB_TEX0 */
119 VERT_ATTRIB_TEX1, /* VERT_ATTRIB_TEX1 */
120 VERT_ATTRIB_TEX2, /* VERT_ATTRIB_TEX2 */
121 VERT_ATTRIB_TEX3, /* VERT_ATTRIB_TEX3 */
122 VERT_ATTRIB_TEX4, /* VERT_ATTRIB_TEX4 */
123 VERT_ATTRIB_TEX5, /* VERT_ATTRIB_TEX5 */
124 VERT_ATTRIB_TEX6, /* VERT_ATTRIB_TEX6 */
125 VERT_ATTRIB_TEX7, /* VERT_ATTRIB_TEX7 */
126 VERT_ATTRIB_POINT_SIZE, /* VERT_ATTRIB_POINT_SIZE */
127 VERT_ATTRIB_POS, /* VERT_ATTRIB_GENERIC0 */
128 VERT_ATTRIB_GENERIC1, /* VERT_ATTRIB_GENERIC1 */
129 VERT_ATTRIB_GENERIC2, /* VERT_ATTRIB_GENERIC2 */
130 VERT_ATTRIB_GENERIC3, /* VERT_ATTRIB_GENERIC3 */
131 VERT_ATTRIB_GENERIC4, /* VERT_ATTRIB_GENERIC4 */
132 VERT_ATTRIB_GENERIC5, /* VERT_ATTRIB_GENERIC5 */
133 VERT_ATTRIB_GENERIC6, /* VERT_ATTRIB_GENERIC6 */
134 VERT_ATTRIB_GENERIC7, /* VERT_ATTRIB_GENERIC7 */
135 VERT_ATTRIB_GENERIC8, /* VERT_ATTRIB_GENERIC8 */
136 VERT_ATTRIB_GENERIC9, /* VERT_ATTRIB_GENERIC9 */
137 VERT_ATTRIB_GENERIC10, /* VERT_ATTRIB_GENERIC10 */
138 VERT_ATTRIB_GENERIC11, /* VERT_ATTRIB_GENERIC11 */
139 VERT_ATTRIB_GENERIC12, /* VERT_ATTRIB_GENERIC12 */
140 VERT_ATTRIB_GENERIC13, /* VERT_ATTRIB_GENERIC13 */
141 VERT_ATTRIB_GENERIC14, /* VERT_ATTRIB_GENERIC14 */
142 VERT_ATTRIB_GENERIC15 /* VERT_ATTRIB_GENERIC15 */
143 },
144
145 /* ATTRIBUTE_MAP_MODE_GENERIC0
146 *
147 * Grab vertex processing attribute VERT_ATTRIB_POS as well as
148 * vertex processing attribute VERT_ATTRIB_GENERIC0 from the
149 * VAO attribute VERT_ATTRIB_GENERIC0.
150 */
151 {
152 VERT_ATTRIB_GENERIC0, /* VERT_ATTRIB_POS */
153 VERT_ATTRIB_NORMAL, /* VERT_ATTRIB_NORMAL */
154 VERT_ATTRIB_COLOR0, /* VERT_ATTRIB_COLOR0 */
155 VERT_ATTRIB_COLOR1, /* VERT_ATTRIB_COLOR1 */
156 VERT_ATTRIB_FOG, /* VERT_ATTRIB_FOG */
157 VERT_ATTRIB_COLOR_INDEX, /* VERT_ATTRIB_COLOR_INDEX */
158 VERT_ATTRIB_EDGEFLAG, /* VERT_ATTRIB_EDGEFLAG */
159 VERT_ATTRIB_TEX0, /* VERT_ATTRIB_TEX0 */
160 VERT_ATTRIB_TEX1, /* VERT_ATTRIB_TEX1 */
161 VERT_ATTRIB_TEX2, /* VERT_ATTRIB_TEX2 */
162 VERT_ATTRIB_TEX3, /* VERT_ATTRIB_TEX3 */
163 VERT_ATTRIB_TEX4, /* VERT_ATTRIB_TEX4 */
164 VERT_ATTRIB_TEX5, /* VERT_ATTRIB_TEX5 */
165 VERT_ATTRIB_TEX6, /* VERT_ATTRIB_TEX6 */
166 VERT_ATTRIB_TEX7, /* VERT_ATTRIB_TEX7 */
167 VERT_ATTRIB_POINT_SIZE, /* VERT_ATTRIB_POINT_SIZE */
168 VERT_ATTRIB_GENERIC0, /* VERT_ATTRIB_GENERIC0 */
169 VERT_ATTRIB_GENERIC1, /* VERT_ATTRIB_GENERIC1 */
170 VERT_ATTRIB_GENERIC2, /* VERT_ATTRIB_GENERIC2 */
171 VERT_ATTRIB_GENERIC3, /* VERT_ATTRIB_GENERIC3 */
172 VERT_ATTRIB_GENERIC4, /* VERT_ATTRIB_GENERIC4 */
173 VERT_ATTRIB_GENERIC5, /* VERT_ATTRIB_GENERIC5 */
174 VERT_ATTRIB_GENERIC6, /* VERT_ATTRIB_GENERIC6 */
175 VERT_ATTRIB_GENERIC7, /* VERT_ATTRIB_GENERIC7 */
176 VERT_ATTRIB_GENERIC8, /* VERT_ATTRIB_GENERIC8 */
177 VERT_ATTRIB_GENERIC9, /* VERT_ATTRIB_GENERIC9 */
178 VERT_ATTRIB_GENERIC10, /* VERT_ATTRIB_GENERIC10 */
179 VERT_ATTRIB_GENERIC11, /* VERT_ATTRIB_GENERIC11 */
180 VERT_ATTRIB_GENERIC12, /* VERT_ATTRIB_GENERIC12 */
181 VERT_ATTRIB_GENERIC13, /* VERT_ATTRIB_GENERIC13 */
182 VERT_ATTRIB_GENERIC14, /* VERT_ATTRIB_GENERIC14 */
183 VERT_ATTRIB_GENERIC15 /* VERT_ATTRIB_GENERIC15 */
184 }
185 };
186
187
188 /**
189 * Look up the array object for the given ID.
190 *
191 * \returns
192 * Either a pointer to the array object with the specified ID or \c NULL for
193 * a non-existent ID. The spec defines ID 0 as being technically
194 * non-existent.
195 */
196
197 struct gl_vertex_array_object *
198 _mesa_lookup_vao(struct gl_context *ctx, GLuint id)
199 {
200 if (id == 0) {
201 return NULL;
202 } else {
203 struct gl_vertex_array_object *vao;
204
205 if (ctx->Array.LastLookedUpVAO &&
206 ctx->Array.LastLookedUpVAO->Name == id) {
207 vao = ctx->Array.LastLookedUpVAO;
208 } else {
209 vao = (struct gl_vertex_array_object *)
210 _mesa_HashLookupLocked(ctx->Array.Objects, id);
211
212 _mesa_reference_vao(ctx, &ctx->Array.LastLookedUpVAO, vao);
213 }
214
215 return vao;
216 }
217 }
218
219
220 /**
221 * Looks up the array object for the given ID.
222 *
223 * Unlike _mesa_lookup_vao, this function generates a GL_INVALID_OPERATION
224 * error if the array object does not exist. It also returns the default
225 * array object when ctx is a compatibility profile context and id is zero.
226 */
227 struct gl_vertex_array_object *
228 _mesa_lookup_vao_err(struct gl_context *ctx, GLuint id, const char *caller)
229 {
230 /* The ARB_direct_state_access specification says:
231 *
232 * "<vaobj> is [compatibility profile:
233 * zero, indicating the default vertex array object, or]
234 * the name of the vertex array object."
235 */
236 if (id == 0) {
237 if (ctx->API == API_OPENGL_CORE) {
238 _mesa_error(ctx, GL_INVALID_OPERATION,
239 "%s(zero is not valid vaobj name in a core profile "
240 "context)", caller);
241 return NULL;
242 }
243
244 return ctx->Array.DefaultVAO;
245 } else {
246 struct gl_vertex_array_object *vao;
247
248 if (ctx->Array.LastLookedUpVAO &&
249 ctx->Array.LastLookedUpVAO->Name == id) {
250 vao = ctx->Array.LastLookedUpVAO;
251 } else {
252 vao = (struct gl_vertex_array_object *)
253 _mesa_HashLookupLocked(ctx->Array.Objects, id);
254
255 /* The ARB_direct_state_access specification says:
256 *
257 * "An INVALID_OPERATION error is generated if <vaobj> is not
258 * [compatibility profile: zero or] the name of an existing
259 * vertex array object."
260 */
261 if (!vao || !vao->EverBound) {
262 _mesa_error(ctx, GL_INVALID_OPERATION,
263 "%s(non-existent vaobj=%u)", caller, id);
264 return NULL;
265 }
266
267 _mesa_reference_vao(ctx, &ctx->Array.LastLookedUpVAO, vao);
268 }
269
270 return vao;
271 }
272 }
273
274
275 /**
276 * For all the vertex binding points in the array object, unbind any pointers
277 * to any buffer objects (VBOs).
278 * This is done just prior to array object destruction.
279 */
280 static void
281 unbind_array_object_vbos(struct gl_context *ctx, struct gl_vertex_array_object *obj)
282 {
283 GLuint i;
284
285 for (i = 0; i < ARRAY_SIZE(obj->BufferBinding); i++)
286 _mesa_reference_buffer_object(ctx, &obj->BufferBinding[i].BufferObj, NULL);
287 }
288
289
290 /**
291 * Allocate and initialize a new vertex array object.
292 */
293 struct gl_vertex_array_object *
294 _mesa_new_vao(struct gl_context *ctx, GLuint name)
295 {
296 struct gl_vertex_array_object *obj = CALLOC_STRUCT(gl_vertex_array_object);
297 if (obj)
298 _mesa_initialize_vao(ctx, obj, name);
299 return obj;
300 }
301
302
303 /**
304 * Delete an array object.
305 */
306 void
307 _mesa_delete_vao(struct gl_context *ctx, struct gl_vertex_array_object *obj)
308 {
309 unbind_array_object_vbos(ctx, obj);
310 _mesa_reference_buffer_object(ctx, &obj->IndexBufferObj, NULL);
311 free(obj->Label);
312 free(obj);
313 }
314
315
316 /**
317 * Set ptr to vao w/ reference counting.
318 * Note: this should only be called from the _mesa_reference_vao()
319 * inline function.
320 */
321 void
322 _mesa_reference_vao_(struct gl_context *ctx,
323 struct gl_vertex_array_object **ptr,
324 struct gl_vertex_array_object *vao)
325 {
326 assert(*ptr != vao);
327
328 if (*ptr) {
329 /* Unreference the old array object */
330 struct gl_vertex_array_object *oldObj = *ptr;
331
332 bool deleteFlag;
333 if (oldObj->SharedAndImmutable) {
334 deleteFlag = p_atomic_dec_zero(&oldObj->RefCount);
335 } else {
336 assert(oldObj->RefCount > 0);
337 oldObj->RefCount--;
338 deleteFlag = (oldObj->RefCount == 0);
339 }
340
341 if (deleteFlag)
342 _mesa_delete_vao(ctx, oldObj);
343
344 *ptr = NULL;
345 }
346 assert(!*ptr);
347
348 if (vao) {
349 /* reference new array object */
350 if (vao->SharedAndImmutable) {
351 p_atomic_inc(&vao->RefCount);
352 } else {
353 assert(vao->RefCount > 0);
354 vao->RefCount++;
355 }
356
357 *ptr = vao;
358 }
359 }
360
361
362 /**
363 * Initialize attributes of a vertex array within a vertex array object.
364 * \param vao the container vertex array object
365 * \param index which array in the VAO to initialize
366 * \param size number of components (1, 2, 3 or 4) per attribute
367 * \param type datatype of the attribute (GL_FLOAT, GL_INT, etc).
368 */
369 static void
370 init_array(struct gl_context *ctx,
371 struct gl_vertex_array_object *vao,
372 gl_vert_attrib index, GLint size, GLint type)
373 {
374 assert(index < ARRAY_SIZE(vao->VertexAttrib));
375 struct gl_array_attributes *array = &vao->VertexAttrib[index];
376 assert(index < ARRAY_SIZE(vao->BufferBinding));
377 struct gl_vertex_buffer_binding *binding = &vao->BufferBinding[index];
378
379 array->Size = size;
380 array->Type = type;
381 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
382 array->Stride = 0;
383 array->Ptr = NULL;
384 array->RelativeOffset = 0;
385 array->Enabled = GL_FALSE;
386 array->Normalized = GL_FALSE;
387 array->Integer = GL_FALSE;
388 array->Doubles = GL_FALSE;
389 array->_ElementSize = size * _mesa_sizeof_type(type);
390 ASSERT_BITFIELD_SIZE(struct gl_array_attributes, BufferBindingIndex,
391 VERT_ATTRIB_MAX - 1);
392 array->BufferBindingIndex = index;
393
394 binding->Offset = 0;
395 binding->Stride = array->_ElementSize;
396 binding->BufferObj = NULL;
397 binding->_BoundArrays = BITFIELD_BIT(index);
398
399 /* Vertex array buffers */
400 _mesa_reference_buffer_object(ctx, &binding->BufferObj,
401 ctx->Shared->NullBufferObj);
402 }
403
404
405 /**
406 * Initialize a gl_vertex_array_object's arrays.
407 */
408 void
409 _mesa_initialize_vao(struct gl_context *ctx,
410 struct gl_vertex_array_object *vao,
411 GLuint name)
412 {
413 GLuint i;
414
415 vao->Name = name;
416
417 vao->RefCount = 1;
418 vao->SharedAndImmutable = false;
419
420 /* Init the individual arrays */
421 for (i = 0; i < ARRAY_SIZE(vao->VertexAttrib); i++) {
422 switch (i) {
423 case VERT_ATTRIB_NORMAL:
424 init_array(ctx, vao, VERT_ATTRIB_NORMAL, 3, GL_FLOAT);
425 break;
426 case VERT_ATTRIB_COLOR1:
427 init_array(ctx, vao, VERT_ATTRIB_COLOR1, 3, GL_FLOAT);
428 break;
429 case VERT_ATTRIB_FOG:
430 init_array(ctx, vao, VERT_ATTRIB_FOG, 1, GL_FLOAT);
431 break;
432 case VERT_ATTRIB_COLOR_INDEX:
433 init_array(ctx, vao, VERT_ATTRIB_COLOR_INDEX, 1, GL_FLOAT);
434 break;
435 case VERT_ATTRIB_EDGEFLAG:
436 init_array(ctx, vao, VERT_ATTRIB_EDGEFLAG, 1, GL_BOOL);
437 break;
438 case VERT_ATTRIB_POINT_SIZE:
439 init_array(ctx, vao, VERT_ATTRIB_POINT_SIZE, 1, GL_FLOAT);
440 break;
441 default:
442 init_array(ctx, vao, i, 4, GL_FLOAT);
443 break;
444 }
445 }
446
447 vao->_AttributeMapMode = ATTRIBUTE_MAP_MODE_IDENTITY;
448
449 _mesa_reference_buffer_object(ctx, &vao->IndexBufferObj,
450 ctx->Shared->NullBufferObj);
451 }
452
453
454 /**
455 * Updates the derived gl_vertex_arrays when a gl_array_attributes
456 * or a gl_vertex_buffer_binding has changed.
457 */
458 void
459 _mesa_update_vao_derived_arrays(struct gl_context *ctx,
460 struct gl_vertex_array_object *vao)
461 {
462 /* Make sure we do not run into problems with shared objects */
463 assert(!vao->SharedAndImmutable || vao->NewArrays == 0);
464
465 /*
466 * Stay tuned, the next series scans for duplicate bindings in this
467 * function. So that drivers can easily know the minimum unique set
468 * of bindings.
469 */
470 }
471
472
473 void
474 _mesa_set_vao_immutable(struct gl_context *ctx,
475 struct gl_vertex_array_object *vao)
476 {
477 _mesa_update_vao_derived_arrays(ctx, vao);
478 vao->NewArrays = 0;
479 vao->SharedAndImmutable = true;
480 }
481
482
483 bool
484 _mesa_all_varyings_in_vbos(const struct gl_vertex_array_object *vao)
485 {
486 /* Walk those enabled arrays that have the default vbo attached */
487 GLbitfield mask = vao->_Enabled & ~vao->VertexAttribBufferMask;
488
489 while (mask) {
490 /* Do not use u_bit_scan64 as we can walk multiple
491 * attrib arrays at once
492 */
493 const int i = ffs(mask) - 1;
494 const struct gl_array_attributes *attrib_array =
495 &vao->VertexAttrib[i];
496 const struct gl_vertex_buffer_binding *buffer_binding =
497 &vao->BufferBinding[attrib_array->BufferBindingIndex];
498
499 /* Only enabled arrays shall appear in the _Enabled bitmask */
500 assert(attrib_array->Enabled);
501 /* We have already masked out vao->VertexAttribBufferMask */
502 assert(!_mesa_is_bufferobj(buffer_binding->BufferObj));
503
504 /* Bail out once we find the first non vbo with a non zero stride */
505 if (buffer_binding->Stride != 0)
506 return false;
507
508 /* Note that we cannot use the xor variant since the _BoundArray mask
509 * may contain array attributes that are bound but not enabled.
510 */
511 mask &= ~buffer_binding->_BoundArrays;
512 }
513
514 return true;
515 }
516
517 bool
518 _mesa_all_buffers_are_unmapped(const struct gl_vertex_array_object *vao)
519 {
520 /* Walk the enabled arrays that have a vbo attached */
521 GLbitfield mask = vao->_Enabled & vao->VertexAttribBufferMask;
522
523 while (mask) {
524 const int i = ffs(mask) - 1;
525 const struct gl_array_attributes *attrib_array =
526 &vao->VertexAttrib[i];
527 const struct gl_vertex_buffer_binding *buffer_binding =
528 &vao->BufferBinding[attrib_array->BufferBindingIndex];
529
530 /* Only enabled arrays shall appear in the _Enabled bitmask */
531 assert(attrib_array->Enabled);
532 /* We have already masked with vao->VertexAttribBufferMask */
533 assert(_mesa_is_bufferobj(buffer_binding->BufferObj));
534
535 /* Bail out once we find the first disallowed mapping */
536 if (_mesa_check_disallowed_mapping(buffer_binding->BufferObj))
537 return false;
538
539 /* We have handled everything that is bound to this buffer_binding. */
540 mask &= ~buffer_binding->_BoundArrays;
541 }
542
543 return true;
544 }
545
546 /**********************************************************************/
547 /* API Functions */
548 /**********************************************************************/
549
550
551 /**
552 * ARB version of glBindVertexArray()
553 */
554 static ALWAYS_INLINE void
555 bind_vertex_array(struct gl_context *ctx, GLuint id, bool no_error)
556 {
557 struct gl_vertex_array_object *const oldObj = ctx->Array.VAO;
558 struct gl_vertex_array_object *newObj = NULL;
559
560 assert(oldObj != NULL);
561
562 if (oldObj->Name == id)
563 return; /* rebinding the same array object- no change */
564
565 /*
566 * Get pointer to new array object (newObj)
567 */
568 if (id == 0) {
569 /* The spec says there is no array object named 0, but we use
570 * one internally because it simplifies things.
571 */
572 newObj = ctx->Array.DefaultVAO;
573 }
574 else {
575 /* non-default array object */
576 newObj = _mesa_lookup_vao(ctx, id);
577 if (!no_error && !newObj) {
578 _mesa_error(ctx, GL_INVALID_OPERATION,
579 "glBindVertexArray(non-gen name)");
580 return;
581 }
582
583 newObj->EverBound = GL_TRUE;
584 }
585
586 /* The _DrawArrays pointer is pointing at the VAO being unbound and
587 * that VAO may be in the process of being deleted. If it's not going
588 * to be deleted, this will have no effect, because the pointer needs
589 * to be updated by the VBO module anyway.
590 *
591 * Before the VBO module can update the pointer, we have to set it
592 * to NULL for drivers not to set up arrays which are not bound,
593 * or to prevent a crash if the VAO being unbound is going to be
594 * deleted.
595 */
596 _mesa_set_drawing_arrays(ctx, NULL);
597 _mesa_set_draw_vao(ctx, ctx->Array._EmptyVAO, 0);
598
599 ctx->NewState |= _NEW_ARRAY;
600 _mesa_reference_vao(ctx, &ctx->Array.VAO, newObj);
601 }
602
603
604 void GLAPIENTRY
605 _mesa_BindVertexArray_no_error(GLuint id)
606 {
607 GET_CURRENT_CONTEXT(ctx);
608 bind_vertex_array(ctx, id, true);
609 }
610
611
612 void GLAPIENTRY
613 _mesa_BindVertexArray(GLuint id)
614 {
615 GET_CURRENT_CONTEXT(ctx);
616 bind_vertex_array(ctx, id, false);
617 }
618
619
620 /**
621 * Delete a set of array objects.
622 *
623 * \param n Number of array objects to delete.
624 * \param ids Array of \c n array object IDs.
625 */
626 static void
627 delete_vertex_arrays(struct gl_context *ctx, GLsizei n, const GLuint *ids)
628 {
629 GLsizei i;
630
631 for (i = 0; i < n; i++) {
632 struct gl_vertex_array_object *obj = _mesa_lookup_vao(ctx, ids[i]);
633
634 if (obj) {
635 assert(obj->Name == ids[i]);
636
637 /* If the array object is currently bound, the spec says "the binding
638 * for that object reverts to zero and the default vertex array
639 * becomes current."
640 */
641 if (obj == ctx->Array.VAO)
642 _mesa_BindVertexArray_no_error(0);
643
644 /* The ID is immediately freed for re-use */
645 _mesa_HashRemoveLocked(ctx->Array.Objects, obj->Name);
646
647 if (ctx->Array.LastLookedUpVAO == obj)
648 _mesa_reference_vao(ctx, &ctx->Array.LastLookedUpVAO, NULL);
649 if (ctx->Array._DrawVAO == obj)
650 _mesa_set_draw_vao(ctx, ctx->Array._EmptyVAO, 0);
651
652 /* Unreference the array object.
653 * If refcount hits zero, the object will be deleted.
654 */
655 _mesa_reference_vao(ctx, &obj, NULL);
656 }
657 }
658 }
659
660
661 void GLAPIENTRY
662 _mesa_DeleteVertexArrays_no_error(GLsizei n, const GLuint *ids)
663 {
664 GET_CURRENT_CONTEXT(ctx);
665 delete_vertex_arrays(ctx, n, ids);
666 }
667
668
669 void GLAPIENTRY
670 _mesa_DeleteVertexArrays(GLsizei n, const GLuint *ids)
671 {
672 GET_CURRENT_CONTEXT(ctx);
673
674 if (n < 0) {
675 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArray(n)");
676 return;
677 }
678
679 delete_vertex_arrays(ctx, n, ids);
680 }
681
682
683 /**
684 * Generate a set of unique array object IDs and store them in \c arrays.
685 * Helper for _mesa_GenVertexArrays() and _mesa_CreateVertexArrays()
686 * below.
687 *
688 * \param n Number of IDs to generate.
689 * \param arrays Array of \c n locations to store the IDs.
690 * \param create Indicates that the objects should also be created.
691 * \param func The name of the GL entry point.
692 */
693 static void
694 gen_vertex_arrays(struct gl_context *ctx, GLsizei n, GLuint *arrays,
695 bool create, const char *func)
696 {
697 GLuint first;
698 GLint i;
699
700 if (!arrays)
701 return;
702
703 first = _mesa_HashFindFreeKeyBlock(ctx->Array.Objects, n);
704
705 /* For the sake of simplicity we create the array objects in both
706 * the Gen* and Create* cases. The only difference is the value of
707 * EverBound, which is set to true in the Create* case.
708 */
709 for (i = 0; i < n; i++) {
710 struct gl_vertex_array_object *obj;
711 GLuint name = first + i;
712
713 obj = _mesa_new_vao(ctx, name);
714 if (!obj) {
715 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
716 return;
717 }
718 obj->EverBound = create;
719 _mesa_HashInsertLocked(ctx->Array.Objects, obj->Name, obj);
720 arrays[i] = first + i;
721 }
722 }
723
724
725 static void
726 gen_vertex_arrays_err(struct gl_context *ctx, GLsizei n, GLuint *arrays,
727 bool create, const char *func)
728 {
729 if (n < 0) {
730 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
731 return;
732 }
733
734 gen_vertex_arrays(ctx, n, arrays, create, func);
735 }
736
737
738 /**
739 * ARB version of glGenVertexArrays()
740 * All arrays will be required to live in VBOs.
741 */
742 void GLAPIENTRY
743 _mesa_GenVertexArrays_no_error(GLsizei n, GLuint *arrays)
744 {
745 GET_CURRENT_CONTEXT(ctx);
746 gen_vertex_arrays(ctx, n, arrays, false, "glGenVertexArrays");
747 }
748
749
750 void GLAPIENTRY
751 _mesa_GenVertexArrays(GLsizei n, GLuint *arrays)
752 {
753 GET_CURRENT_CONTEXT(ctx);
754 gen_vertex_arrays_err(ctx, n, arrays, false, "glGenVertexArrays");
755 }
756
757
758 /**
759 * ARB_direct_state_access
760 * Generates ID's and creates the array objects.
761 */
762 void GLAPIENTRY
763 _mesa_CreateVertexArrays_no_error(GLsizei n, GLuint *arrays)
764 {
765 GET_CURRENT_CONTEXT(ctx);
766 gen_vertex_arrays(ctx, n, arrays, true, "glCreateVertexArrays");
767 }
768
769
770 void GLAPIENTRY
771 _mesa_CreateVertexArrays(GLsizei n, GLuint *arrays)
772 {
773 GET_CURRENT_CONTEXT(ctx);
774 gen_vertex_arrays_err(ctx, n, arrays, true, "glCreateVertexArrays");
775 }
776
777
778 /**
779 * Determine if ID is the name of an array object.
780 *
781 * \param id ID of the potential array object.
782 * \return \c GL_TRUE if \c id is the name of a array object,
783 * \c GL_FALSE otherwise.
784 */
785 GLboolean GLAPIENTRY
786 _mesa_IsVertexArray( GLuint id )
787 {
788 struct gl_vertex_array_object * obj;
789 GET_CURRENT_CONTEXT(ctx);
790 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
791
792 obj = _mesa_lookup_vao(ctx, id);
793
794 return obj != NULL && obj->EverBound;
795 }
796
797
798 /**
799 * Sets the element array buffer binding of a vertex array object.
800 *
801 * This is the ARB_direct_state_access equivalent of
802 * glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer).
803 */
804 static ALWAYS_INLINE void
805 vertex_array_element_buffer(struct gl_context *ctx, GLuint vaobj, GLuint buffer,
806 bool no_error)
807 {
808 struct gl_vertex_array_object *vao;
809 struct gl_buffer_object *bufObj;
810
811 ASSERT_OUTSIDE_BEGIN_END(ctx);
812
813 if (!no_error) {
814 /* The GL_ARB_direct_state_access specification says:
815 *
816 * "An INVALID_OPERATION error is generated by
817 * VertexArrayElementBuffer if <vaobj> is not [compatibility profile:
818 * zero or] the name of an existing vertex array object."
819 */
820 vao =_mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayElementBuffer");
821 if (!vao)
822 return;
823 } else {
824 vao = _mesa_lookup_vao(ctx, vaobj);
825 }
826
827 if (buffer != 0) {
828 if (!no_error) {
829 /* The GL_ARB_direct_state_access specification says:
830 *
831 * "An INVALID_OPERATION error is generated if <buffer> is not zero
832 * or the name of an existing buffer object."
833 */
834 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
835 "glVertexArrayElementBuffer");
836 } else {
837 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
838 }
839 } else {
840 bufObj = ctx->Shared->NullBufferObj;
841 }
842
843 if (bufObj)
844 _mesa_reference_buffer_object(ctx, &vao->IndexBufferObj, bufObj);
845 }
846
847
848 void GLAPIENTRY
849 _mesa_VertexArrayElementBuffer_no_error(GLuint vaobj, GLuint buffer)
850 {
851 GET_CURRENT_CONTEXT(ctx);
852 vertex_array_element_buffer(ctx, vaobj, buffer, true);
853 }
854
855
856 void GLAPIENTRY
857 _mesa_VertexArrayElementBuffer(GLuint vaobj, GLuint buffer)
858 {
859 GET_CURRENT_CONTEXT(ctx);
860 vertex_array_element_buffer(ctx, vaobj, buffer, false);
861 }
862
863
864 void GLAPIENTRY
865 _mesa_GetVertexArrayiv(GLuint vaobj, GLenum pname, GLint *param)
866 {
867 GET_CURRENT_CONTEXT(ctx);
868 struct gl_vertex_array_object *vao;
869
870 ASSERT_OUTSIDE_BEGIN_END(ctx);
871
872 /* The GL_ARB_direct_state_access specification says:
873 *
874 * "An INVALID_OPERATION error is generated if <vaobj> is not
875 * [compatibility profile: zero or] the name of an existing
876 * vertex array object."
877 */
878 vao =_mesa_lookup_vao_err(ctx, vaobj, "glGetVertexArrayiv");
879 if (!vao)
880 return;
881
882 /* The GL_ARB_direct_state_access specification says:
883 *
884 * "An INVALID_ENUM error is generated if <pname> is not
885 * ELEMENT_ARRAY_BUFFER_BINDING."
886 */
887 if (pname != GL_ELEMENT_ARRAY_BUFFER_BINDING) {
888 _mesa_error(ctx, GL_INVALID_ENUM,
889 "glGetVertexArrayiv(pname != "
890 "GL_ELEMENT_ARRAY_BUFFER_BINDING)");
891 return;
892 }
893
894 param[0] = vao->IndexBufferObj->Name;
895 }