2 * Mesa 3-D graphics library
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.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
31 * Implementation of Vertex Array Objects (VAOs), from OpenGL 3.1+ /
32 * the GL_ARB_vertex_array_object extension.
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.
38 * \author Ian Romanick <idr@us.ibm.com>
48 #include "bufferobj.h"
54 #include "util/bitscan.h"
55 #include "util/u_atomic.h"
59 _mesa_vao_attribute_map
[ATTRIBUTE_MAP_MODE_MAX
][VERT_ATTRIB_MAX
] =
61 /* ATTRIBUTE_MAP_MODE_IDENTITY
63 * Grab vertex processing attribute VERT_ATTRIB_POS from
64 * the VAO attribute VERT_ATTRIB_POS, and grab vertex processing
65 * attribute VERT_ATTRIB_GENERIC0 from the VAO attribute
66 * VERT_ATTRIB_GENERIC0.
69 VERT_ATTRIB_POS
, /* VERT_ATTRIB_POS */
70 VERT_ATTRIB_NORMAL
, /* VERT_ATTRIB_NORMAL */
71 VERT_ATTRIB_COLOR0
, /* VERT_ATTRIB_COLOR0 */
72 VERT_ATTRIB_COLOR1
, /* VERT_ATTRIB_COLOR1 */
73 VERT_ATTRIB_FOG
, /* VERT_ATTRIB_FOG */
74 VERT_ATTRIB_COLOR_INDEX
, /* VERT_ATTRIB_COLOR_INDEX */
75 VERT_ATTRIB_EDGEFLAG
, /* VERT_ATTRIB_EDGEFLAG */
76 VERT_ATTRIB_TEX0
, /* VERT_ATTRIB_TEX0 */
77 VERT_ATTRIB_TEX1
, /* VERT_ATTRIB_TEX1 */
78 VERT_ATTRIB_TEX2
, /* VERT_ATTRIB_TEX2 */
79 VERT_ATTRIB_TEX3
, /* VERT_ATTRIB_TEX3 */
80 VERT_ATTRIB_TEX4
, /* VERT_ATTRIB_TEX4 */
81 VERT_ATTRIB_TEX5
, /* VERT_ATTRIB_TEX5 */
82 VERT_ATTRIB_TEX6
, /* VERT_ATTRIB_TEX6 */
83 VERT_ATTRIB_TEX7
, /* VERT_ATTRIB_TEX7 */
84 VERT_ATTRIB_POINT_SIZE
, /* VERT_ATTRIB_POINT_SIZE */
85 VERT_ATTRIB_GENERIC0
, /* VERT_ATTRIB_GENERIC0 */
86 VERT_ATTRIB_GENERIC1
, /* VERT_ATTRIB_GENERIC1 */
87 VERT_ATTRIB_GENERIC2
, /* VERT_ATTRIB_GENERIC2 */
88 VERT_ATTRIB_GENERIC3
, /* VERT_ATTRIB_GENERIC3 */
89 VERT_ATTRIB_GENERIC4
, /* VERT_ATTRIB_GENERIC4 */
90 VERT_ATTRIB_GENERIC5
, /* VERT_ATTRIB_GENERIC5 */
91 VERT_ATTRIB_GENERIC6
, /* VERT_ATTRIB_GENERIC6 */
92 VERT_ATTRIB_GENERIC7
, /* VERT_ATTRIB_GENERIC7 */
93 VERT_ATTRIB_GENERIC8
, /* VERT_ATTRIB_GENERIC8 */
94 VERT_ATTRIB_GENERIC9
, /* VERT_ATTRIB_GENERIC9 */
95 VERT_ATTRIB_GENERIC10
, /* VERT_ATTRIB_GENERIC10 */
96 VERT_ATTRIB_GENERIC11
, /* VERT_ATTRIB_GENERIC11 */
97 VERT_ATTRIB_GENERIC12
, /* VERT_ATTRIB_GENERIC12 */
98 VERT_ATTRIB_GENERIC13
, /* VERT_ATTRIB_GENERIC13 */
99 VERT_ATTRIB_GENERIC14
, /* VERT_ATTRIB_GENERIC14 */
100 VERT_ATTRIB_GENERIC15
/* VERT_ATTRIB_GENERIC15 */
103 /* ATTRIBUTE_MAP_MODE_POSITION
105 * Grab vertex processing attribute VERT_ATTRIB_POS as well as
106 * vertex processing attribute VERT_ATTRIB_GENERIC0 from the
107 * VAO attribute VERT_ATTRIB_POS.
110 VERT_ATTRIB_POS
, /* VERT_ATTRIB_POS */
111 VERT_ATTRIB_NORMAL
, /* VERT_ATTRIB_NORMAL */
112 VERT_ATTRIB_COLOR0
, /* VERT_ATTRIB_COLOR0 */
113 VERT_ATTRIB_COLOR1
, /* VERT_ATTRIB_COLOR1 */
114 VERT_ATTRIB_FOG
, /* VERT_ATTRIB_FOG */
115 VERT_ATTRIB_COLOR_INDEX
, /* VERT_ATTRIB_COLOR_INDEX */
116 VERT_ATTRIB_EDGEFLAG
, /* VERT_ATTRIB_EDGEFLAG */
117 VERT_ATTRIB_TEX0
, /* VERT_ATTRIB_TEX0 */
118 VERT_ATTRIB_TEX1
, /* VERT_ATTRIB_TEX1 */
119 VERT_ATTRIB_TEX2
, /* VERT_ATTRIB_TEX2 */
120 VERT_ATTRIB_TEX3
, /* VERT_ATTRIB_TEX3 */
121 VERT_ATTRIB_TEX4
, /* VERT_ATTRIB_TEX4 */
122 VERT_ATTRIB_TEX5
, /* VERT_ATTRIB_TEX5 */
123 VERT_ATTRIB_TEX6
, /* VERT_ATTRIB_TEX6 */
124 VERT_ATTRIB_TEX7
, /* VERT_ATTRIB_TEX7 */
125 VERT_ATTRIB_POINT_SIZE
, /* VERT_ATTRIB_POINT_SIZE */
126 VERT_ATTRIB_POS
, /* VERT_ATTRIB_GENERIC0 */
127 VERT_ATTRIB_GENERIC1
, /* VERT_ATTRIB_GENERIC1 */
128 VERT_ATTRIB_GENERIC2
, /* VERT_ATTRIB_GENERIC2 */
129 VERT_ATTRIB_GENERIC3
, /* VERT_ATTRIB_GENERIC3 */
130 VERT_ATTRIB_GENERIC4
, /* VERT_ATTRIB_GENERIC4 */
131 VERT_ATTRIB_GENERIC5
, /* VERT_ATTRIB_GENERIC5 */
132 VERT_ATTRIB_GENERIC6
, /* VERT_ATTRIB_GENERIC6 */
133 VERT_ATTRIB_GENERIC7
, /* VERT_ATTRIB_GENERIC7 */
134 VERT_ATTRIB_GENERIC8
, /* VERT_ATTRIB_GENERIC8 */
135 VERT_ATTRIB_GENERIC9
, /* VERT_ATTRIB_GENERIC9 */
136 VERT_ATTRIB_GENERIC10
, /* VERT_ATTRIB_GENERIC10 */
137 VERT_ATTRIB_GENERIC11
, /* VERT_ATTRIB_GENERIC11 */
138 VERT_ATTRIB_GENERIC12
, /* VERT_ATTRIB_GENERIC12 */
139 VERT_ATTRIB_GENERIC13
, /* VERT_ATTRIB_GENERIC13 */
140 VERT_ATTRIB_GENERIC14
, /* VERT_ATTRIB_GENERIC14 */
141 VERT_ATTRIB_GENERIC15
/* VERT_ATTRIB_GENERIC15 */
144 /* ATTRIBUTE_MAP_MODE_GENERIC0
146 * Grab vertex processing attribute VERT_ATTRIB_POS as well as
147 * vertex processing attribute VERT_ATTRIB_GENERIC0 from the
148 * VAO attribute VERT_ATTRIB_GENERIC0.
151 VERT_ATTRIB_GENERIC0
, /* VERT_ATTRIB_POS */
152 VERT_ATTRIB_NORMAL
, /* VERT_ATTRIB_NORMAL */
153 VERT_ATTRIB_COLOR0
, /* VERT_ATTRIB_COLOR0 */
154 VERT_ATTRIB_COLOR1
, /* VERT_ATTRIB_COLOR1 */
155 VERT_ATTRIB_FOG
, /* VERT_ATTRIB_FOG */
156 VERT_ATTRIB_COLOR_INDEX
, /* VERT_ATTRIB_COLOR_INDEX */
157 VERT_ATTRIB_EDGEFLAG
, /* VERT_ATTRIB_EDGEFLAG */
158 VERT_ATTRIB_TEX0
, /* VERT_ATTRIB_TEX0 */
159 VERT_ATTRIB_TEX1
, /* VERT_ATTRIB_TEX1 */
160 VERT_ATTRIB_TEX2
, /* VERT_ATTRIB_TEX2 */
161 VERT_ATTRIB_TEX3
, /* VERT_ATTRIB_TEX3 */
162 VERT_ATTRIB_TEX4
, /* VERT_ATTRIB_TEX4 */
163 VERT_ATTRIB_TEX5
, /* VERT_ATTRIB_TEX5 */
164 VERT_ATTRIB_TEX6
, /* VERT_ATTRIB_TEX6 */
165 VERT_ATTRIB_TEX7
, /* VERT_ATTRIB_TEX7 */
166 VERT_ATTRIB_POINT_SIZE
, /* VERT_ATTRIB_POINT_SIZE */
167 VERT_ATTRIB_GENERIC0
, /* VERT_ATTRIB_GENERIC0 */
168 VERT_ATTRIB_GENERIC1
, /* VERT_ATTRIB_GENERIC1 */
169 VERT_ATTRIB_GENERIC2
, /* VERT_ATTRIB_GENERIC2 */
170 VERT_ATTRIB_GENERIC3
, /* VERT_ATTRIB_GENERIC3 */
171 VERT_ATTRIB_GENERIC4
, /* VERT_ATTRIB_GENERIC4 */
172 VERT_ATTRIB_GENERIC5
, /* VERT_ATTRIB_GENERIC5 */
173 VERT_ATTRIB_GENERIC6
, /* VERT_ATTRIB_GENERIC6 */
174 VERT_ATTRIB_GENERIC7
, /* VERT_ATTRIB_GENERIC7 */
175 VERT_ATTRIB_GENERIC8
, /* VERT_ATTRIB_GENERIC8 */
176 VERT_ATTRIB_GENERIC9
, /* VERT_ATTRIB_GENERIC9 */
177 VERT_ATTRIB_GENERIC10
, /* VERT_ATTRIB_GENERIC10 */
178 VERT_ATTRIB_GENERIC11
, /* VERT_ATTRIB_GENERIC11 */
179 VERT_ATTRIB_GENERIC12
, /* VERT_ATTRIB_GENERIC12 */
180 VERT_ATTRIB_GENERIC13
, /* VERT_ATTRIB_GENERIC13 */
181 VERT_ATTRIB_GENERIC14
, /* VERT_ATTRIB_GENERIC14 */
182 VERT_ATTRIB_GENERIC15
/* VERT_ATTRIB_GENERIC15 */
188 * Look up the array object for the given ID.
191 * Either a pointer to the array object with the specified ID or \c NULL for
192 * a non-existent ID. The spec defines ID 0 as being technically
196 struct gl_vertex_array_object
*
197 _mesa_lookup_vao(struct gl_context
*ctx
, GLuint id
)
202 struct gl_vertex_array_object
*vao
;
204 if (ctx
->Array
.LastLookedUpVAO
&&
205 ctx
->Array
.LastLookedUpVAO
->Name
== id
) {
206 vao
= ctx
->Array
.LastLookedUpVAO
;
208 vao
= (struct gl_vertex_array_object
*)
209 _mesa_HashLookupLocked(ctx
->Array
.Objects
, id
);
211 _mesa_reference_vao(ctx
, &ctx
->Array
.LastLookedUpVAO
, vao
);
220 * Looks up the array object for the given ID.
222 * Unlike _mesa_lookup_vao, this function generates a GL_INVALID_OPERATION
223 * error if the array object does not exist. It also returns the default
224 * array object when ctx is a compatibility profile context and id is zero.
226 struct gl_vertex_array_object
*
227 _mesa_lookup_vao_err(struct gl_context
*ctx
, GLuint id
, const char *caller
)
229 /* The ARB_direct_state_access specification says:
231 * "<vaobj> is [compatibility profile:
232 * zero, indicating the default vertex array object, or]
233 * the name of the vertex array object."
236 if (ctx
->API
== API_OPENGL_CORE
) {
237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
238 "%s(zero is not valid vaobj name in a core profile "
243 return ctx
->Array
.DefaultVAO
;
245 struct gl_vertex_array_object
*vao
;
247 if (ctx
->Array
.LastLookedUpVAO
&&
248 ctx
->Array
.LastLookedUpVAO
->Name
== id
) {
249 vao
= ctx
->Array
.LastLookedUpVAO
;
251 vao
= (struct gl_vertex_array_object
*)
252 _mesa_HashLookupLocked(ctx
->Array
.Objects
, id
);
254 /* The ARB_direct_state_access specification says:
256 * "An INVALID_OPERATION error is generated if <vaobj> is not
257 * [compatibility profile: zero or] the name of an existing
258 * vertex array object."
260 if (!vao
|| !vao
->EverBound
) {
261 _mesa_error(ctx
, GL_INVALID_OPERATION
,
262 "%s(non-existent vaobj=%u)", caller
, id
);
266 _mesa_reference_vao(ctx
, &ctx
->Array
.LastLookedUpVAO
, vao
);
275 * For all the vertex binding points in the array object, unbind any pointers
276 * to any buffer objects (VBOs).
277 * This is done just prior to array object destruction.
280 unbind_array_object_vbos(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
284 for (i
= 0; i
< ARRAY_SIZE(obj
->BufferBinding
); i
++)
285 _mesa_reference_buffer_object(ctx
, &obj
->BufferBinding
[i
].BufferObj
, NULL
);
290 * Allocate and initialize a new vertex array object.
292 struct gl_vertex_array_object
*
293 _mesa_new_vao(struct gl_context
*ctx
, GLuint name
)
295 struct gl_vertex_array_object
*obj
= CALLOC_STRUCT(gl_vertex_array_object
);
297 _mesa_initialize_vao(ctx
, obj
, name
);
303 * Delete an array object.
306 _mesa_delete_vao(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
308 unbind_array_object_vbos(ctx
, obj
);
309 _mesa_reference_buffer_object(ctx
, &obj
->IndexBufferObj
, NULL
);
316 * Set ptr to vao w/ reference counting.
317 * Note: this should only be called from the _mesa_reference_vao()
321 _mesa_reference_vao_(struct gl_context
*ctx
,
322 struct gl_vertex_array_object
**ptr
,
323 struct gl_vertex_array_object
*vao
)
328 /* Unreference the old array object */
329 struct gl_vertex_array_object
*oldObj
= *ptr
;
332 if (oldObj
->SharedAndImmutable
) {
333 deleteFlag
= p_atomic_dec_zero(&oldObj
->RefCount
);
335 assert(oldObj
->RefCount
> 0);
337 deleteFlag
= (oldObj
->RefCount
== 0);
341 _mesa_delete_vao(ctx
, oldObj
);
348 /* reference new array object */
349 if (vao
->SharedAndImmutable
) {
350 p_atomic_inc(&vao
->RefCount
);
352 assert(vao
->RefCount
> 0);
362 * Initialize attributes of a vertex array within a vertex array object.
363 * \param vao the container vertex array object
364 * \param index which array in the VAO to initialize
365 * \param size number of components (1, 2, 3 or 4) per attribute
366 * \param type datatype of the attribute (GL_FLOAT, GL_INT, etc).
369 init_array(struct gl_context
*ctx
,
370 struct gl_vertex_array_object
*vao
,
371 gl_vert_attrib index
, GLint size
, GLint type
)
373 assert(index
< ARRAY_SIZE(vao
->VertexAttrib
));
374 struct gl_array_attributes
*array
= &vao
->VertexAttrib
[index
];
375 assert(index
< ARRAY_SIZE(vao
->BufferBinding
));
376 struct gl_vertex_buffer_binding
*binding
= &vao
->BufferBinding
[index
];
380 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
383 array
->RelativeOffset
= 0;
384 array
->Enabled
= GL_FALSE
;
385 array
->Normalized
= GL_FALSE
;
386 array
->Integer
= GL_FALSE
;
387 array
->Doubles
= GL_FALSE
;
388 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
389 ASSERT_BITFIELD_SIZE(struct gl_array_attributes
, BufferBindingIndex
,
390 VERT_ATTRIB_MAX
- 1);
391 array
->BufferBindingIndex
= index
;
394 binding
->Stride
= array
->_ElementSize
;
395 binding
->BufferObj
= NULL
;
396 binding
->_BoundArrays
= BITFIELD_BIT(index
);
398 /* Vertex array buffers */
399 _mesa_reference_buffer_object(ctx
, &binding
->BufferObj
,
400 ctx
->Shared
->NullBufferObj
);
405 * Initialize a gl_vertex_array_object's arrays.
408 _mesa_initialize_vao(struct gl_context
*ctx
,
409 struct gl_vertex_array_object
*vao
,
417 vao
->SharedAndImmutable
= false;
419 /* Init the individual arrays */
420 for (i
= 0; i
< ARRAY_SIZE(vao
->VertexAttrib
); i
++) {
422 case VERT_ATTRIB_NORMAL
:
423 init_array(ctx
, vao
, VERT_ATTRIB_NORMAL
, 3, GL_FLOAT
);
425 case VERT_ATTRIB_COLOR1
:
426 init_array(ctx
, vao
, VERT_ATTRIB_COLOR1
, 3, GL_FLOAT
);
428 case VERT_ATTRIB_FOG
:
429 init_array(ctx
, vao
, VERT_ATTRIB_FOG
, 1, GL_FLOAT
);
431 case VERT_ATTRIB_COLOR_INDEX
:
432 init_array(ctx
, vao
, VERT_ATTRIB_COLOR_INDEX
, 1, GL_FLOAT
);
434 case VERT_ATTRIB_EDGEFLAG
:
435 init_array(ctx
, vao
, VERT_ATTRIB_EDGEFLAG
, 1, GL_BOOL
);
437 case VERT_ATTRIB_POINT_SIZE
:
438 init_array(ctx
, vao
, VERT_ATTRIB_POINT_SIZE
, 1, GL_FLOAT
);
441 init_array(ctx
, vao
, i
, 4, GL_FLOAT
);
446 vao
->_AttributeMapMode
= ATTRIBUTE_MAP_MODE_IDENTITY
;
448 _mesa_reference_buffer_object(ctx
, &vao
->IndexBufferObj
,
449 ctx
->Shared
->NullBufferObj
);
454 * Updates the derived gl_vertex_arrays when a gl_array_attributes
455 * or a gl_vertex_buffer_binding has changed.
458 _mesa_update_vao_derived_arrays(struct gl_context
*ctx
,
459 struct gl_vertex_array_object
*vao
)
461 /* Make sure we do not run into problems with shared objects */
462 assert(!vao
->SharedAndImmutable
|| vao
->NewArrays
== 0);
465 * Stay tuned, the next series scans for duplicate bindings in this
466 * function. So that drivers can easily know the minimum unique set
473 _mesa_set_vao_immutable(struct gl_context
*ctx
,
474 struct gl_vertex_array_object
*vao
)
476 _mesa_update_vao_derived_arrays(ctx
, vao
);
478 vao
->SharedAndImmutable
= true;
483 _mesa_all_varyings_in_vbos(const struct gl_vertex_array_object
*vao
)
485 /* Walk those enabled arrays that have the default vbo attached */
486 GLbitfield mask
= vao
->_Enabled
& ~vao
->VertexAttribBufferMask
;
489 /* Do not use u_bit_scan64 as we can walk multiple
490 * attrib arrays at once
492 const int i
= ffs(mask
) - 1;
493 const struct gl_array_attributes
*attrib_array
=
494 &vao
->VertexAttrib
[i
];
495 const struct gl_vertex_buffer_binding
*buffer_binding
=
496 &vao
->BufferBinding
[attrib_array
->BufferBindingIndex
];
498 /* Only enabled arrays shall appear in the _Enabled bitmask */
499 assert(attrib_array
->Enabled
);
500 /* We have already masked out vao->VertexAttribBufferMask */
501 assert(!_mesa_is_bufferobj(buffer_binding
->BufferObj
));
503 /* Bail out once we find the first non vbo with a non zero stride */
504 if (buffer_binding
->Stride
!= 0)
507 /* Note that we cannot use the xor variant since the _BoundArray mask
508 * may contain array attributes that are bound but not enabled.
510 mask
&= ~buffer_binding
->_BoundArrays
;
517 _mesa_all_buffers_are_unmapped(const struct gl_vertex_array_object
*vao
)
519 /* Walk the enabled arrays that have a vbo attached */
520 GLbitfield mask
= vao
->_Enabled
& vao
->VertexAttribBufferMask
;
523 const int i
= ffs(mask
) - 1;
524 const struct gl_array_attributes
*attrib_array
=
525 &vao
->VertexAttrib
[i
];
526 const struct gl_vertex_buffer_binding
*buffer_binding
=
527 &vao
->BufferBinding
[attrib_array
->BufferBindingIndex
];
529 /* Only enabled arrays shall appear in the _Enabled bitmask */
530 assert(attrib_array
->Enabled
);
531 /* We have already masked with vao->VertexAttribBufferMask */
532 assert(_mesa_is_bufferobj(buffer_binding
->BufferObj
));
534 /* Bail out once we find the first disallowed mapping */
535 if (_mesa_check_disallowed_mapping(buffer_binding
->BufferObj
))
538 /* We have handled everything that is bound to this buffer_binding. */
539 mask
&= ~buffer_binding
->_BoundArrays
;
545 /**********************************************************************/
547 /**********************************************************************/
551 * ARB version of glBindVertexArray()
553 static ALWAYS_INLINE
void
554 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, bool no_error
)
556 struct gl_vertex_array_object
*const oldObj
= ctx
->Array
.VAO
;
557 struct gl_vertex_array_object
*newObj
= NULL
;
559 assert(oldObj
!= NULL
);
561 if (oldObj
->Name
== id
)
562 return; /* rebinding the same array object- no change */
565 * Get pointer to new array object (newObj)
568 /* The spec says there is no array object named 0, but we use
569 * one internally because it simplifies things.
571 newObj
= ctx
->Array
.DefaultVAO
;
574 /* non-default array object */
575 newObj
= _mesa_lookup_vao(ctx
, id
);
576 if (!no_error
&& !newObj
) {
577 _mesa_error(ctx
, GL_INVALID_OPERATION
,
578 "glBindVertexArray(non-gen name)");
582 newObj
->EverBound
= GL_TRUE
;
585 /* The _DrawArrays pointer is pointing at the VAO being unbound and
586 * that VAO may be in the process of being deleted. If it's not going
587 * to be deleted, this will have no effect, because the pointer needs
588 * to be updated by the VBO module anyway.
590 * Before the VBO module can update the pointer, we have to set it
591 * to NULL for drivers not to set up arrays which are not bound,
592 * or to prevent a crash if the VAO being unbound is going to be
595 _mesa_set_drawing_arrays(ctx
, NULL
);
596 _mesa_set_draw_vao(ctx
, ctx
->Array
._EmptyVAO
, 0);
598 ctx
->NewState
|= _NEW_ARRAY
;
599 _mesa_reference_vao(ctx
, &ctx
->Array
.VAO
, newObj
);
604 _mesa_BindVertexArray_no_error(GLuint id
)
606 GET_CURRENT_CONTEXT(ctx
);
607 bind_vertex_array(ctx
, id
, true);
612 _mesa_BindVertexArray(GLuint id
)
614 GET_CURRENT_CONTEXT(ctx
);
615 bind_vertex_array(ctx
, id
, false);
620 * Delete a set of array objects.
622 * \param n Number of array objects to delete.
623 * \param ids Array of \c n array object IDs.
626 delete_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
630 for (i
= 0; i
< n
; i
++) {
631 struct gl_vertex_array_object
*obj
= _mesa_lookup_vao(ctx
, ids
[i
]);
634 assert(obj
->Name
== ids
[i
]);
636 /* If the array object is currently bound, the spec says "the binding
637 * for that object reverts to zero and the default vertex array
640 if (obj
== ctx
->Array
.VAO
)
641 _mesa_BindVertexArray_no_error(0);
643 /* The ID is immediately freed for re-use */
644 _mesa_HashRemoveLocked(ctx
->Array
.Objects
, obj
->Name
);
646 if (ctx
->Array
.LastLookedUpVAO
== obj
)
647 _mesa_reference_vao(ctx
, &ctx
->Array
.LastLookedUpVAO
, NULL
);
648 if (ctx
->Array
._DrawVAO
== obj
)
649 _mesa_set_draw_vao(ctx
, ctx
->Array
._EmptyVAO
, 0);
651 /* Unreference the array object.
652 * If refcount hits zero, the object will be deleted.
654 _mesa_reference_vao(ctx
, &obj
, NULL
);
661 _mesa_DeleteVertexArrays_no_error(GLsizei n
, const GLuint
*ids
)
663 GET_CURRENT_CONTEXT(ctx
);
664 delete_vertex_arrays(ctx
, n
, ids
);
669 _mesa_DeleteVertexArrays(GLsizei n
, const GLuint
*ids
)
671 GET_CURRENT_CONTEXT(ctx
);
674 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArray(n)");
678 delete_vertex_arrays(ctx
, n
, ids
);
683 * Generate a set of unique array object IDs and store them in \c arrays.
684 * Helper for _mesa_GenVertexArrays() and _mesa_CreateVertexArrays()
687 * \param n Number of IDs to generate.
688 * \param arrays Array of \c n locations to store the IDs.
689 * \param create Indicates that the objects should also be created.
690 * \param func The name of the GL entry point.
693 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
,
694 bool create
, const char *func
)
702 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
704 /* For the sake of simplicity we create the array objects in both
705 * the Gen* and Create* cases. The only difference is the value of
706 * EverBound, which is set to true in the Create* case.
708 for (i
= 0; i
< n
; i
++) {
709 struct gl_vertex_array_object
*obj
;
710 GLuint name
= first
+ i
;
712 obj
= _mesa_new_vao(ctx
, name
);
714 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
717 obj
->EverBound
= create
;
718 _mesa_HashInsertLocked(ctx
->Array
.Objects
, obj
->Name
, obj
);
719 arrays
[i
] = first
+ i
;
725 gen_vertex_arrays_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
,
726 bool create
, const char *func
)
729 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
733 gen_vertex_arrays(ctx
, n
, arrays
, create
, func
);
738 * ARB version of glGenVertexArrays()
739 * All arrays will be required to live in VBOs.
742 _mesa_GenVertexArrays_no_error(GLsizei n
, GLuint
*arrays
)
744 GET_CURRENT_CONTEXT(ctx
);
745 gen_vertex_arrays(ctx
, n
, arrays
, false, "glGenVertexArrays");
750 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
752 GET_CURRENT_CONTEXT(ctx
);
753 gen_vertex_arrays_err(ctx
, n
, arrays
, false, "glGenVertexArrays");
758 * ARB_direct_state_access
759 * Generates ID's and creates the array objects.
762 _mesa_CreateVertexArrays_no_error(GLsizei n
, GLuint
*arrays
)
764 GET_CURRENT_CONTEXT(ctx
);
765 gen_vertex_arrays(ctx
, n
, arrays
, true, "glCreateVertexArrays");
770 _mesa_CreateVertexArrays(GLsizei n
, GLuint
*arrays
)
772 GET_CURRENT_CONTEXT(ctx
);
773 gen_vertex_arrays_err(ctx
, n
, arrays
, true, "glCreateVertexArrays");
778 * Determine if ID is the name of an array object.
780 * \param id ID of the potential array object.
781 * \return \c GL_TRUE if \c id is the name of a array object,
782 * \c GL_FALSE otherwise.
785 _mesa_IsVertexArray( GLuint id
)
787 struct gl_vertex_array_object
* obj
;
788 GET_CURRENT_CONTEXT(ctx
);
789 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
791 obj
= _mesa_lookup_vao(ctx
, id
);
793 return obj
!= NULL
&& obj
->EverBound
;
798 * Sets the element array buffer binding of a vertex array object.
800 * This is the ARB_direct_state_access equivalent of
801 * glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer).
803 static ALWAYS_INLINE
void
804 vertex_array_element_buffer(struct gl_context
*ctx
, GLuint vaobj
, GLuint buffer
,
807 struct gl_vertex_array_object
*vao
;
808 struct gl_buffer_object
*bufObj
;
810 ASSERT_OUTSIDE_BEGIN_END(ctx
);
813 /* The GL_ARB_direct_state_access specification says:
815 * "An INVALID_OPERATION error is generated by
816 * VertexArrayElementBuffer if <vaobj> is not [compatibility profile:
817 * zero or] the name of an existing vertex array object."
819 vao
=_mesa_lookup_vao_err(ctx
, vaobj
, "glVertexArrayElementBuffer");
823 vao
= _mesa_lookup_vao(ctx
, vaobj
);
828 /* The GL_ARB_direct_state_access specification says:
830 * "An INVALID_OPERATION error is generated if <buffer> is not zero
831 * or the name of an existing buffer object."
833 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
834 "glVertexArrayElementBuffer");
836 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
839 bufObj
= ctx
->Shared
->NullBufferObj
;
843 _mesa_reference_buffer_object(ctx
, &vao
->IndexBufferObj
, bufObj
);
848 _mesa_VertexArrayElementBuffer_no_error(GLuint vaobj
, GLuint buffer
)
850 GET_CURRENT_CONTEXT(ctx
);
851 vertex_array_element_buffer(ctx
, vaobj
, buffer
, true);
856 _mesa_VertexArrayElementBuffer(GLuint vaobj
, GLuint buffer
)
858 GET_CURRENT_CONTEXT(ctx
);
859 vertex_array_element_buffer(ctx
, vaobj
, buffer
, false);
864 _mesa_GetVertexArrayiv(GLuint vaobj
, GLenum pname
, GLint
*param
)
866 GET_CURRENT_CONTEXT(ctx
);
867 struct gl_vertex_array_object
*vao
;
869 ASSERT_OUTSIDE_BEGIN_END(ctx
);
871 /* The GL_ARB_direct_state_access specification says:
873 * "An INVALID_OPERATION error is generated if <vaobj> is not
874 * [compatibility profile: zero or] the name of an existing
875 * vertex array object."
877 vao
=_mesa_lookup_vao_err(ctx
, vaobj
, "glGetVertexArrayiv");
881 /* The GL_ARB_direct_state_access specification says:
883 * "An INVALID_ENUM error is generated if <pname> is not
884 * ELEMENT_ARRAY_BUFFER_BINDING."
886 if (pname
!= GL_ELEMENT_ARRAY_BUFFER_BINDING
) {
887 _mesa_error(ctx
, GL_INVALID_ENUM
,
888 "glGetVertexArrayiv(pname != "
889 "GL_ELEMENT_ARRAY_BUFFER_BINDING)");
893 param
[0] = vao
->IndexBufferObj
->Name
;