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, or the older
33 * GL_APPLE_vertex_array_object extension.
36 * The code in this file borrows a lot from bufferobj.c. There's a certain
37 * amount of cruft left over from that origin that may be unnecessary.
39 * \author Ian Romanick <idr@us.ibm.com>
49 #include "bufferobj.h"
54 #include "main/dispatch.h"
55 #include "util/bitscan.h"
59 * Look up the array object for the given ID.
62 * Either a pointer to the array object with the specified ID or \c NULL for
63 * a non-existent ID. The spec defines ID 0 as being technically
67 struct gl_vertex_array_object
*
68 _mesa_lookup_vao(struct gl_context
*ctx
, GLuint id
)
73 return (struct gl_vertex_array_object
*)
74 _mesa_HashLookup(ctx
->Array
.Objects
, id
);
79 * Looks up the array object for the given ID.
81 * Unlike _mesa_lookup_vao, this function generates a GL_INVALID_OPERATION
82 * error if the array object does not exist. It also returns the default
83 * array object when ctx is a compatibility profile context and id is zero.
85 struct gl_vertex_array_object
*
86 _mesa_lookup_vao_err(struct gl_context
*ctx
, GLuint id
, const char *caller
)
88 /* The ARB_direct_state_access specification says:
90 * "<vaobj> is [compatibility profile:
91 * zero, indicating the default vertex array object, or]
92 * the name of the vertex array object."
95 if (ctx
->API
== API_OPENGL_CORE
) {
96 _mesa_error(ctx
, GL_INVALID_OPERATION
,
97 "%s(zero is not valid vaobj name in a core profile "
102 return ctx
->Array
.DefaultVAO
;
104 struct gl_vertex_array_object
*vao
;
106 if (ctx
->Array
.LastLookedUpVAO
&&
107 ctx
->Array
.LastLookedUpVAO
->Name
== id
) {
108 vao
= ctx
->Array
.LastLookedUpVAO
;
110 vao
= (struct gl_vertex_array_object
*)
111 _mesa_HashLookup(ctx
->Array
.Objects
, id
);
113 /* The ARB_direct_state_access specification says:
115 * "An INVALID_OPERATION error is generated if <vaobj> is not
116 * [compatibility profile: zero or] the name of an existing
117 * vertex array object."
119 if (!vao
|| !vao
->EverBound
) {
120 _mesa_error(ctx
, GL_INVALID_OPERATION
,
121 "%s(non-existent vaobj=%u)", caller
, id
);
125 _mesa_reference_vao(ctx
, &ctx
->Array
.LastLookedUpVAO
, vao
);
134 * For all the vertex binding points in the array object, unbind any pointers
135 * to any buffer objects (VBOs).
136 * This is done just prior to array object destruction.
139 unbind_array_object_vbos(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
143 for (i
= 0; i
< ARRAY_SIZE(obj
->BufferBinding
); i
++)
144 _mesa_reference_buffer_object(ctx
, &obj
->BufferBinding
[i
].BufferObj
, NULL
);
146 for (i
= 0; i
< ARRAY_SIZE(obj
->_VertexAttrib
); i
++)
147 _mesa_reference_buffer_object(ctx
, &obj
->_VertexAttrib
[i
].BufferObj
, NULL
);
152 * Allocate and initialize a new vertex array object.
154 struct gl_vertex_array_object
*
155 _mesa_new_vao(struct gl_context
*ctx
, GLuint name
)
157 struct gl_vertex_array_object
*obj
= CALLOC_STRUCT(gl_vertex_array_object
);
159 _mesa_initialize_vao(ctx
, obj
, name
);
165 * Delete an array object.
168 _mesa_delete_vao(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
170 unbind_array_object_vbos(ctx
, obj
);
171 _mesa_reference_buffer_object(ctx
, &obj
->IndexBufferObj
, NULL
);
172 mtx_destroy(&obj
->Mutex
);
179 * Set ptr to vao w/ reference counting.
180 * Note: this should only be called from the _mesa_reference_vao()
184 _mesa_reference_vao_(struct gl_context
*ctx
,
185 struct gl_vertex_array_object
**ptr
,
186 struct gl_vertex_array_object
*vao
)
191 /* Unreference the old array object */
192 GLboolean deleteFlag
= GL_FALSE
;
193 struct gl_vertex_array_object
*oldObj
= *ptr
;
195 mtx_lock(&oldObj
->Mutex
);
196 assert(oldObj
->RefCount
> 0);
198 deleteFlag
= (oldObj
->RefCount
== 0);
199 mtx_unlock(&oldObj
->Mutex
);
202 _mesa_delete_vao(ctx
, oldObj
);
209 /* reference new array object */
210 mtx_lock(&vao
->Mutex
);
211 if (vao
->RefCount
== 0) {
212 /* this array's being deleted (look just above) */
213 /* Not sure this can every really happen. Warn if it does. */
214 _mesa_problem(NULL
, "referencing deleted array object");
221 mtx_unlock(&vao
->Mutex
);
227 * Initialize attribtes of a vertex array within a vertex array object.
228 * \param vao the container vertex array object
229 * \param index which array in the VAO to initialize
230 * \param size number of components (1, 2, 3 or 4) per attribute
231 * \param type datatype of the attribute (GL_FLOAT, GL_INT, etc).
234 init_array(struct gl_context
*ctx
,
235 struct gl_vertex_array_object
*vao
,
236 GLuint index
, GLint size
, GLint type
)
238 struct gl_array_attributes
*array
= &vao
->VertexAttrib
[index
];
239 struct gl_vertex_buffer_binding
*binding
= &vao
->BufferBinding
[index
];
243 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
246 array
->RelativeOffset
= 0;
247 array
->Enabled
= GL_FALSE
;
248 array
->Normalized
= GL_FALSE
;
249 array
->Integer
= GL_FALSE
;
250 array
->Doubles
= GL_FALSE
;
251 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
252 array
->BufferBindingIndex
= index
;
255 binding
->Stride
= array
->_ElementSize
;
256 binding
->BufferObj
= NULL
;
257 binding
->_BoundArrays
= BITFIELD64_BIT(index
);
259 /* Vertex array buffers */
260 _mesa_reference_buffer_object(ctx
, &binding
->BufferObj
,
261 ctx
->Shared
->NullBufferObj
);
266 * Initialize a gl_vertex_array_object's arrays.
269 _mesa_initialize_vao(struct gl_context
*ctx
,
270 struct gl_vertex_array_object
*vao
,
277 mtx_init(&vao
->Mutex
, mtx_plain
);
280 /* Init the individual arrays */
281 for (i
= 0; i
< ARRAY_SIZE(vao
->VertexAttrib
); i
++) {
283 case VERT_ATTRIB_WEIGHT
:
284 init_array(ctx
, vao
, VERT_ATTRIB_WEIGHT
, 1, GL_FLOAT
);
286 case VERT_ATTRIB_NORMAL
:
287 init_array(ctx
, vao
, VERT_ATTRIB_NORMAL
, 3, GL_FLOAT
);
289 case VERT_ATTRIB_COLOR1
:
290 init_array(ctx
, vao
, VERT_ATTRIB_COLOR1
, 3, GL_FLOAT
);
292 case VERT_ATTRIB_FOG
:
293 init_array(ctx
, vao
, VERT_ATTRIB_FOG
, 1, GL_FLOAT
);
295 case VERT_ATTRIB_COLOR_INDEX
:
296 init_array(ctx
, vao
, VERT_ATTRIB_COLOR_INDEX
, 1, GL_FLOAT
);
298 case VERT_ATTRIB_EDGEFLAG
:
299 init_array(ctx
, vao
, VERT_ATTRIB_EDGEFLAG
, 1, GL_BOOL
);
301 case VERT_ATTRIB_POINT_SIZE
:
302 init_array(ctx
, vao
, VERT_ATTRIB_POINT_SIZE
, 1, GL_FLOAT
);
305 init_array(ctx
, vao
, i
, 4, GL_FLOAT
);
310 _mesa_reference_buffer_object(ctx
, &vao
->IndexBufferObj
,
311 ctx
->Shared
->NullBufferObj
);
316 * Add the given array object to the array object pool.
319 save_array_object(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
)
322 /* insert into hash table */
323 _mesa_HashInsert(ctx
->Array
.Objects
, vao
->Name
, vao
);
329 * Remove the given array object from the array object pool.
330 * Do not deallocate the array object though.
333 remove_array_object(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
)
336 /* remove from hash table */
337 _mesa_HashRemove(ctx
->Array
.Objects
, vao
->Name
);
343 * Updates the derived gl_client_arrays when a gl_vertex_attrib_array
344 * or a gl_vertex_buffer_binding has changed.
347 _mesa_update_vao_client_arrays(struct gl_context
*ctx
,
348 struct gl_vertex_array_object
*vao
)
350 GLbitfield64 arrays
= vao
->NewArrays
;
353 const int attrib
= u_bit_scan64(&arrays
);
354 struct gl_client_array
*client_array
= &vao
->_VertexAttrib
[attrib
];
355 const struct gl_array_attributes
*attrib_array
=
356 &vao
->VertexAttrib
[attrib
];
357 const struct gl_vertex_buffer_binding
*buffer_binding
=
358 &vao
->BufferBinding
[attrib_array
->BufferBindingIndex
];
360 _mesa_update_client_array(ctx
, client_array
, attrib_array
,
367 _mesa_all_varyings_in_vbos(const struct gl_vertex_array_object
*vao
)
369 /* Walk those enabled arrays that have the default vbo attached */
370 GLbitfield64 mask
= vao
->_Enabled
& ~vao
->VertexAttribBufferMask
;
373 /* Do not use u_bit_scan64 as we can walk multiple
374 * attrib arrays at once
376 const int i
= ffsll(mask
) - 1;
377 const struct gl_array_attributes
*attrib_array
=
378 &vao
->VertexAttrib
[i
];
379 const struct gl_vertex_buffer_binding
*buffer_binding
=
380 &vao
->BufferBinding
[attrib_array
->BufferBindingIndex
];
382 /* Only enabled arrays shall appear in the _Enabled bitmask */
383 assert(attrib_array
->Enabled
);
384 /* We have already masked out vao->VertexAttribBufferMask */
385 assert(!_mesa_is_bufferobj(buffer_binding
->BufferObj
));
387 /* Bail out once we find the first non vbo with a non zero stride */
388 if (buffer_binding
->Stride
!= 0)
391 /* Note that we cannot use the xor variant since the _BoundArray mask
392 * may contain array attributes that are bound but not enabled.
394 mask
&= ~buffer_binding
->_BoundArrays
;
401 _mesa_all_buffers_are_unmapped(const struct gl_vertex_array_object
*vao
)
403 /* Walk the enabled arrays that have a vbo attached */
404 GLbitfield64 mask
= vao
->_Enabled
& vao
->VertexAttribBufferMask
;
407 const int i
= ffsll(mask
) - 1;
408 const struct gl_array_attributes
*attrib_array
=
409 &vao
->VertexAttrib
[i
];
410 const struct gl_vertex_buffer_binding
*buffer_binding
=
411 &vao
->BufferBinding
[attrib_array
->BufferBindingIndex
];
413 /* Only enabled arrays shall appear in the _Enabled bitmask */
414 assert(attrib_array
->Enabled
);
415 /* We have already masked with vao->VertexAttribBufferMask */
416 assert(_mesa_is_bufferobj(buffer_binding
->BufferObj
));
418 /* Bail out once we find the first disallowed mapping */
419 if (_mesa_check_disallowed_mapping(buffer_binding
->BufferObj
))
422 /* We have handled everything that is bound to this buffer_binding. */
423 mask
&= ~buffer_binding
->_BoundArrays
;
429 /**********************************************************************/
431 /**********************************************************************/
435 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
436 * \param genRequired specifies behavour when id was not generated with
437 * glGenVertexArrays().
440 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, GLboolean genRequired
)
442 struct gl_vertex_array_object
* const oldObj
= ctx
->Array
.VAO
;
443 struct gl_vertex_array_object
*newObj
= NULL
;
445 assert(oldObj
!= NULL
);
447 if ( oldObj
->Name
== id
)
448 return; /* rebinding the same array object- no change */
451 * Get pointer to new array object (newObj)
454 /* The spec says there is no array object named 0, but we use
455 * one internally because it simplifies things.
457 newObj
= ctx
->Array
.DefaultVAO
;
460 /* non-default array object */
461 newObj
= _mesa_lookup_vao(ctx
, id
);
464 _mesa_error(ctx
, GL_INVALID_OPERATION
,
465 "glBindVertexArray(non-gen name)");
469 /* For APPLE version, generate a new array object now */
470 newObj
= _mesa_new_vao(ctx
, id
);
472 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
476 save_array_object(ctx
, newObj
);
479 if (!newObj
->EverBound
) {
480 /* The "Interactions with APPLE_vertex_array_object" section of the
481 * GL_ARB_vertex_array_object spec says:
483 * "The first bind call, either BindVertexArray or
484 * BindVertexArrayAPPLE, determines the semantic of the object."
486 newObj
->ARBsemantics
= genRequired
;
487 newObj
->EverBound
= GL_TRUE
;
491 if (ctx
->Array
.DrawMethod
== DRAW_ARRAYS
) {
492 /* The _DrawArrays pointer is pointing at the VAO being unbound and
493 * that VAO may be in the process of being deleted. If it's not going
494 * to be deleted, this will have no effect, because the pointer needs
495 * to be updated by the VBO module anyway.
497 * Before the VBO module can update the pointer, we have to set it
498 * to NULL for drivers not to set up arrays which are not bound,
499 * or to prevent a crash if the VAO being unbound is going to be
502 ctx
->Array
._DrawArrays
= NULL
;
503 ctx
->Array
.DrawMethod
= DRAW_NONE
;
506 ctx
->NewState
|= _NEW_ARRAY
;
507 _mesa_reference_vao(ctx
, &ctx
->Array
.VAO
, newObj
);
512 * ARB version of glBindVertexArray()
513 * This function behaves differently from glBindVertexArrayAPPLE() in
514 * that this function requires all ids to have been previously generated
515 * by glGenVertexArrays[APPLE]().
518 _mesa_BindVertexArray( GLuint id
)
520 GET_CURRENT_CONTEXT(ctx
);
521 bind_vertex_array(ctx
, id
, GL_TRUE
);
529 * The binding could be done more efficiently by comparing the non-NULL
530 * pointers in the old and new objects. The only arrays that are "dirty" are
531 * the ones that are non-NULL in either object.
534 _mesa_BindVertexArrayAPPLE( GLuint id
)
536 GET_CURRENT_CONTEXT(ctx
);
537 bind_vertex_array(ctx
, id
, GL_FALSE
);
542 * Delete a set of array objects.
544 * \param n Number of array objects to delete.
545 * \param ids Array of \c n array object IDs.
548 _mesa_DeleteVertexArrays(GLsizei n
, const GLuint
*ids
)
550 GET_CURRENT_CONTEXT(ctx
);
554 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArray(n)");
558 for (i
= 0; i
< n
; i
++) {
559 struct gl_vertex_array_object
*obj
= _mesa_lookup_vao(ctx
, ids
[i
]);
562 assert( obj
->Name
== ids
[i
] );
564 /* If the array object is currently bound, the spec says "the binding
565 * for that object reverts to zero and the default vertex array
568 if ( obj
== ctx
->Array
.VAO
) {
569 _mesa_BindVertexArray(0);
572 /* The ID is immediately freed for re-use */
573 remove_array_object(ctx
, obj
);
575 if (ctx
->Array
.LastLookedUpVAO
== obj
)
576 _mesa_reference_vao(ctx
, &ctx
->Array
.LastLookedUpVAO
, NULL
);
578 /* Unreference the array object.
579 * If refcount hits zero, the object will be deleted.
581 _mesa_reference_vao(ctx
, &obj
, NULL
);
588 * Generate a set of unique array object IDs and store them in \c arrays.
589 * Helper for _mesa_GenVertexArrays[APPLE]() and _mesa_CreateVertexArrays()
592 * \param n Number of IDs to generate.
593 * \param arrays Array of \c n locations to store the IDs.
594 * \param create Indicates that the objects should also be created.
595 * \param func The name of the GL entry point.
598 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
,
599 bool create
, const char *func
)
605 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
613 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
615 /* For the sake of simplicity we create the array objects in both
616 * the Gen* and Create* cases. The only difference is the value of
617 * EverBound, which is set to true in the Create* case.
619 for (i
= 0; i
< n
; i
++) {
620 struct gl_vertex_array_object
*obj
;
621 GLuint name
= first
+ i
;
623 obj
= _mesa_new_vao(ctx
, name
);
625 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
628 obj
->EverBound
= create
;
629 save_array_object(ctx
, obj
);
630 arrays
[i
] = first
+ i
;
636 * ARB version of glGenVertexArrays()
637 * All arrays will be required to live in VBOs.
640 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
642 GET_CURRENT_CONTEXT(ctx
);
643 gen_vertex_arrays(ctx
, n
, arrays
, false, "glGenVertexArrays");
648 * APPLE version of glGenVertexArraysAPPLE()
649 * Arrays may live in VBOs or ordinary memory.
652 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
654 GET_CURRENT_CONTEXT(ctx
);
655 gen_vertex_arrays(ctx
, n
, arrays
, false, "glGenVertexArraysAPPLE");
660 * ARB_direct_state_access
661 * Generates ID's and creates the array objects.
664 _mesa_CreateVertexArrays(GLsizei n
, GLuint
*arrays
)
666 GET_CURRENT_CONTEXT(ctx
);
667 gen_vertex_arrays(ctx
, n
, arrays
, true, "glCreateVertexArrays");
672 * Determine if ID is the name of an array object.
674 * \param id ID of the potential array object.
675 * \return \c GL_TRUE if \c id is the name of a array object,
676 * \c GL_FALSE otherwise.
679 _mesa_IsVertexArray( GLuint id
)
681 struct gl_vertex_array_object
* obj
;
682 GET_CURRENT_CONTEXT(ctx
);
683 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
688 obj
= _mesa_lookup_vao(ctx
, id
);
692 return obj
->EverBound
;
697 * Sets the element array buffer binding of a vertex array object.
699 * This is the ARB_direct_state_access equivalent of
700 * glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer).
703 _mesa_VertexArrayElementBuffer(GLuint vaobj
, GLuint buffer
)
705 GET_CURRENT_CONTEXT(ctx
);
706 struct gl_vertex_array_object
*vao
;
707 struct gl_buffer_object
*bufObj
;
709 ASSERT_OUTSIDE_BEGIN_END(ctx
);
711 /* The GL_ARB_direct_state_access specification says:
713 * "An INVALID_OPERATION error is generated by VertexArrayElementBuffer
714 * if <vaobj> is not [compatibility profile: zero or] the name of an
715 * existing vertex array object."
717 vao
=_mesa_lookup_vao_err(ctx
, vaobj
, "glVertexArrayElementBuffer");
721 /* The GL_ARB_direct_state_access specification says:
723 * "An INVALID_OPERATION error is generated if <buffer> is not zero or
724 * the name of an existing buffer object."
727 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
728 "glVertexArrayElementBuffer");
730 bufObj
= ctx
->Shared
->NullBufferObj
;
733 _mesa_reference_buffer_object(ctx
, &vao
->IndexBufferObj
, bufObj
);
738 _mesa_GetVertexArrayiv(GLuint vaobj
, GLenum pname
, GLint
*param
)
740 GET_CURRENT_CONTEXT(ctx
);
741 struct gl_vertex_array_object
*vao
;
743 ASSERT_OUTSIDE_BEGIN_END(ctx
);
745 /* The GL_ARB_direct_state_access specification says:
747 * "An INVALID_OPERATION error is generated if <vaobj> is not
748 * [compatibility profile: zero or] the name of an existing
749 * vertex array object."
751 vao
=_mesa_lookup_vao_err(ctx
, vaobj
, "glGetVertexArrayiv");
755 /* The GL_ARB_direct_state_access specification says:
757 * "An INVALID_ENUM error is generated if <pname> is not
758 * ELEMENT_ARRAY_BUFFER_BINDING."
760 if (pname
!= GL_ELEMENT_ARRAY_BUFFER_BINDING
) {
761 _mesa_error(ctx
, GL_INVALID_ENUM
,
762 "glGetVertexArrayiv(pname != "
763 "GL_ELEMENT_ARRAY_BUFFER_BINDING)");
767 param
[0] = vao
->IndexBufferObj
->Name
;