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"
53 #include "main/dispatch.h"
54 #include "util/bitscan.h"
58 * Look up the array object for the given ID.
61 * Either a pointer to the array object with the specified ID or \c NULL for
62 * a non-existent ID. The spec defines ID 0 as being technically
66 struct gl_vertex_array_object
*
67 _mesa_lookup_vao(struct gl_context
*ctx
, GLuint id
)
72 return (struct gl_vertex_array_object
*)
73 _mesa_HashLookupLocked(ctx
->Array
.Objects
, id
);
78 * Looks up the array object for the given ID.
80 * Unlike _mesa_lookup_vao, this function generates a GL_INVALID_OPERATION
81 * error if the array object does not exist. It also returns the default
82 * array object when ctx is a compatibility profile context and id is zero.
84 struct gl_vertex_array_object
*
85 _mesa_lookup_vao_err(struct gl_context
*ctx
, GLuint id
, const char *caller
)
87 /* The ARB_direct_state_access specification says:
89 * "<vaobj> is [compatibility profile:
90 * zero, indicating the default vertex array object, or]
91 * the name of the vertex array object."
94 if (ctx
->API
== API_OPENGL_CORE
) {
95 _mesa_error(ctx
, GL_INVALID_OPERATION
,
96 "%s(zero is not valid vaobj name in a core profile "
101 return ctx
->Array
.DefaultVAO
;
103 struct gl_vertex_array_object
*vao
;
105 if (ctx
->Array
.LastLookedUpVAO
&&
106 ctx
->Array
.LastLookedUpVAO
->Name
== id
) {
107 vao
= ctx
->Array
.LastLookedUpVAO
;
109 vao
= (struct gl_vertex_array_object
*)
110 _mesa_HashLookupLocked(ctx
->Array
.Objects
, id
);
112 /* The ARB_direct_state_access specification says:
114 * "An INVALID_OPERATION error is generated if <vaobj> is not
115 * [compatibility profile: zero or] the name of an existing
116 * vertex array object."
118 if (!vao
|| !vao
->EverBound
) {
119 _mesa_error(ctx
, GL_INVALID_OPERATION
,
120 "%s(non-existent vaobj=%u)", caller
, id
);
124 _mesa_reference_vao(ctx
, &ctx
->Array
.LastLookedUpVAO
, vao
);
133 * For all the vertex binding points in the array object, unbind any pointers
134 * to any buffer objects (VBOs).
135 * This is done just prior to array object destruction.
138 unbind_array_object_vbos(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
142 for (i
= 0; i
< ARRAY_SIZE(obj
->BufferBinding
); i
++)
143 _mesa_reference_buffer_object(ctx
, &obj
->BufferBinding
[i
].BufferObj
, NULL
);
145 for (i
= 0; i
< ARRAY_SIZE(obj
->_VertexAttrib
); i
++)
146 _mesa_reference_buffer_object(ctx
, &obj
->_VertexAttrib
[i
].BufferObj
, NULL
);
151 * Allocate and initialize a new vertex array object.
153 struct gl_vertex_array_object
*
154 _mesa_new_vao(struct gl_context
*ctx
, GLuint name
)
156 struct gl_vertex_array_object
*obj
= CALLOC_STRUCT(gl_vertex_array_object
);
158 _mesa_initialize_vao(ctx
, obj
, name
);
164 * Delete an array object.
167 _mesa_delete_vao(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
169 unbind_array_object_vbos(ctx
, obj
);
170 _mesa_reference_buffer_object(ctx
, &obj
->IndexBufferObj
, NULL
);
177 * Set ptr to vao w/ reference counting.
178 * Note: this should only be called from the _mesa_reference_vao()
182 _mesa_reference_vao_(struct gl_context
*ctx
,
183 struct gl_vertex_array_object
**ptr
,
184 struct gl_vertex_array_object
*vao
)
189 /* Unreference the old array object */
190 struct gl_vertex_array_object
*oldObj
= *ptr
;
192 assert(oldObj
->RefCount
> 0);
195 if (oldObj
->RefCount
== 0)
196 _mesa_delete_vao(ctx
, oldObj
);
203 /* reference new array object */
204 assert(vao
->RefCount
> 0);
213 * Initialize attribtes of a vertex array within a vertex array object.
214 * \param vao the container vertex array object
215 * \param index which array in the VAO to initialize
216 * \param size number of components (1, 2, 3 or 4) per attribute
217 * \param type datatype of the attribute (GL_FLOAT, GL_INT, etc).
220 init_array(struct gl_context
*ctx
,
221 struct gl_vertex_array_object
*vao
,
222 GLuint index
, GLint size
, GLint type
)
224 struct gl_array_attributes
*array
= &vao
->VertexAttrib
[index
];
225 struct gl_vertex_buffer_binding
*binding
= &vao
->BufferBinding
[index
];
229 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
232 array
->RelativeOffset
= 0;
233 array
->Enabled
= GL_FALSE
;
234 array
->Normalized
= GL_FALSE
;
235 array
->Integer
= GL_FALSE
;
236 array
->Doubles
= GL_FALSE
;
237 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
238 array
->BufferBindingIndex
= index
;
241 binding
->Stride
= array
->_ElementSize
;
242 binding
->BufferObj
= NULL
;
243 binding
->_BoundArrays
= BITFIELD64_BIT(index
);
245 /* Vertex array buffers */
246 _mesa_reference_buffer_object(ctx
, &binding
->BufferObj
,
247 ctx
->Shared
->NullBufferObj
);
252 * Initialize a gl_vertex_array_object's arrays.
255 _mesa_initialize_vao(struct gl_context
*ctx
,
256 struct gl_vertex_array_object
*vao
,
265 /* Init the individual arrays */
266 for (i
= 0; i
< ARRAY_SIZE(vao
->VertexAttrib
); i
++) {
268 case VERT_ATTRIB_WEIGHT
:
269 init_array(ctx
, vao
, VERT_ATTRIB_WEIGHT
, 1, GL_FLOAT
);
271 case VERT_ATTRIB_NORMAL
:
272 init_array(ctx
, vao
, VERT_ATTRIB_NORMAL
, 3, GL_FLOAT
);
274 case VERT_ATTRIB_COLOR1
:
275 init_array(ctx
, vao
, VERT_ATTRIB_COLOR1
, 3, GL_FLOAT
);
277 case VERT_ATTRIB_FOG
:
278 init_array(ctx
, vao
, VERT_ATTRIB_FOG
, 1, GL_FLOAT
);
280 case VERT_ATTRIB_COLOR_INDEX
:
281 init_array(ctx
, vao
, VERT_ATTRIB_COLOR_INDEX
, 1, GL_FLOAT
);
283 case VERT_ATTRIB_EDGEFLAG
:
284 init_array(ctx
, vao
, VERT_ATTRIB_EDGEFLAG
, 1, GL_BOOL
);
286 case VERT_ATTRIB_POINT_SIZE
:
287 init_array(ctx
, vao
, VERT_ATTRIB_POINT_SIZE
, 1, GL_FLOAT
);
290 init_array(ctx
, vao
, i
, 4, GL_FLOAT
);
295 _mesa_reference_buffer_object(ctx
, &vao
->IndexBufferObj
,
296 ctx
->Shared
->NullBufferObj
);
301 * Add the given array object to the array object pool.
304 save_array_object(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
)
307 /* insert into hash table */
308 _mesa_HashInsertLocked(ctx
->Array
.Objects
, vao
->Name
, vao
);
314 * Remove the given array object from the array object pool.
315 * Do not deallocate the array object though.
318 remove_array_object(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
)
321 /* remove from hash table */
322 _mesa_HashRemoveLocked(ctx
->Array
.Objects
, vao
->Name
);
328 * Updates the derived gl_vertex_arrays when a gl_vertex_attrib_array
329 * or a gl_vertex_buffer_binding has changed.
332 _mesa_update_vao_client_arrays(struct gl_context
*ctx
,
333 struct gl_vertex_array_object
*vao
)
335 GLbitfield64 arrays
= vao
->NewArrays
;
338 const int attrib
= u_bit_scan64(&arrays
);
339 struct gl_vertex_array
*client_array
= &vao
->_VertexAttrib
[attrib
];
340 const struct gl_array_attributes
*attrib_array
=
341 &vao
->VertexAttrib
[attrib
];
342 const struct gl_vertex_buffer_binding
*buffer_binding
=
343 &vao
->BufferBinding
[attrib_array
->BufferBindingIndex
];
345 _mesa_update_client_array(ctx
, client_array
, attrib_array
,
352 _mesa_all_varyings_in_vbos(const struct gl_vertex_array_object
*vao
)
354 /* Walk those enabled arrays that have the default vbo attached */
355 GLbitfield64 mask
= vao
->_Enabled
& ~vao
->VertexAttribBufferMask
;
358 /* Do not use u_bit_scan64 as we can walk multiple
359 * attrib arrays at once
361 const int i
= ffsll(mask
) - 1;
362 const struct gl_array_attributes
*attrib_array
=
363 &vao
->VertexAttrib
[i
];
364 const struct gl_vertex_buffer_binding
*buffer_binding
=
365 &vao
->BufferBinding
[attrib_array
->BufferBindingIndex
];
367 /* Only enabled arrays shall appear in the _Enabled bitmask */
368 assert(attrib_array
->Enabled
);
369 /* We have already masked out vao->VertexAttribBufferMask */
370 assert(!_mesa_is_bufferobj(buffer_binding
->BufferObj
));
372 /* Bail out once we find the first non vbo with a non zero stride */
373 if (buffer_binding
->Stride
!= 0)
376 /* Note that we cannot use the xor variant since the _BoundArray mask
377 * may contain array attributes that are bound but not enabled.
379 mask
&= ~buffer_binding
->_BoundArrays
;
386 _mesa_all_buffers_are_unmapped(const struct gl_vertex_array_object
*vao
)
388 /* Walk the enabled arrays that have a vbo attached */
389 GLbitfield64 mask
= vao
->_Enabled
& vao
->VertexAttribBufferMask
;
392 const int i
= ffsll(mask
) - 1;
393 const struct gl_array_attributes
*attrib_array
=
394 &vao
->VertexAttrib
[i
];
395 const struct gl_vertex_buffer_binding
*buffer_binding
=
396 &vao
->BufferBinding
[attrib_array
->BufferBindingIndex
];
398 /* Only enabled arrays shall appear in the _Enabled bitmask */
399 assert(attrib_array
->Enabled
);
400 /* We have already masked with vao->VertexAttribBufferMask */
401 assert(_mesa_is_bufferobj(buffer_binding
->BufferObj
));
403 /* Bail out once we find the first disallowed mapping */
404 if (_mesa_check_disallowed_mapping(buffer_binding
->BufferObj
))
407 /* We have handled everything that is bound to this buffer_binding. */
408 mask
&= ~buffer_binding
->_BoundArrays
;
414 /**********************************************************************/
416 /**********************************************************************/
420 * ARB version of glBindVertexArray()
423 _mesa_BindVertexArray( GLuint id
)
425 GET_CURRENT_CONTEXT(ctx
);
427 struct gl_vertex_array_object
* const oldObj
= ctx
->Array
.VAO
;
428 struct gl_vertex_array_object
*newObj
= NULL
;
430 assert(oldObj
!= NULL
);
432 if (oldObj
->Name
== id
)
433 return; /* rebinding the same array object- no change */
436 * Get pointer to new array object (newObj)
439 /* The spec says there is no array object named 0, but we use
440 * one internally because it simplifies things.
442 newObj
= ctx
->Array
.DefaultVAO
;
445 /* non-default array object */
446 newObj
= _mesa_lookup_vao(ctx
, id
);
448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
449 "glBindVertexArray(non-gen name)");
453 newObj
->EverBound
= GL_TRUE
;
456 if (ctx
->Array
.DrawMethod
== DRAW_ARRAYS
) {
457 /* The _DrawArrays pointer is pointing at the VAO being unbound and
458 * that VAO may be in the process of being deleted. If it's not going
459 * to be deleted, this will have no effect, because the pointer needs
460 * to be updated by the VBO module anyway.
462 * Before the VBO module can update the pointer, we have to set it
463 * to NULL for drivers not to set up arrays which are not bound,
464 * or to prevent a crash if the VAO being unbound is going to be
467 ctx
->Array
._DrawArrays
= NULL
;
468 ctx
->Array
.DrawMethod
= DRAW_NONE
;
471 ctx
->NewState
|= _NEW_ARRAY
;
472 _mesa_reference_vao(ctx
, &ctx
->Array
.VAO
, newObj
);
477 * Delete a set of array objects.
479 * \param n Number of array objects to delete.
480 * \param ids Array of \c n array object IDs.
483 _mesa_DeleteVertexArrays(GLsizei n
, const GLuint
*ids
)
485 GET_CURRENT_CONTEXT(ctx
);
489 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArray(n)");
493 for (i
= 0; i
< n
; i
++) {
494 struct gl_vertex_array_object
*obj
= _mesa_lookup_vao(ctx
, ids
[i
]);
497 assert( obj
->Name
== ids
[i
] );
499 /* If the array object is currently bound, the spec says "the binding
500 * for that object reverts to zero and the default vertex array
503 if ( obj
== ctx
->Array
.VAO
) {
504 _mesa_BindVertexArray(0);
507 /* The ID is immediately freed for re-use */
508 remove_array_object(ctx
, obj
);
510 if (ctx
->Array
.LastLookedUpVAO
== obj
)
511 _mesa_reference_vao(ctx
, &ctx
->Array
.LastLookedUpVAO
, NULL
);
513 /* Unreference the array object.
514 * If refcount hits zero, the object will be deleted.
516 _mesa_reference_vao(ctx
, &obj
, NULL
);
523 * Generate a set of unique array object IDs and store them in \c arrays.
524 * Helper for _mesa_GenVertexArrays() and _mesa_CreateVertexArrays()
527 * \param n Number of IDs to generate.
528 * \param arrays Array of \c n locations to store the IDs.
529 * \param create Indicates that the objects should also be created.
530 * \param func The name of the GL entry point.
533 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
,
534 bool create
, const char *func
)
540 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
548 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
550 /* For the sake of simplicity we create the array objects in both
551 * the Gen* and Create* cases. The only difference is the value of
552 * EverBound, which is set to true in the Create* case.
554 for (i
= 0; i
< n
; i
++) {
555 struct gl_vertex_array_object
*obj
;
556 GLuint name
= first
+ i
;
558 obj
= _mesa_new_vao(ctx
, name
);
560 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
563 obj
->EverBound
= create
;
564 save_array_object(ctx
, obj
);
565 arrays
[i
] = first
+ i
;
571 * ARB version of glGenVertexArrays()
572 * All arrays will be required to live in VBOs.
575 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
577 GET_CURRENT_CONTEXT(ctx
);
578 gen_vertex_arrays(ctx
, n
, arrays
, false, "glGenVertexArrays");
583 * ARB_direct_state_access
584 * Generates ID's and creates the array objects.
587 _mesa_CreateVertexArrays(GLsizei n
, GLuint
*arrays
)
589 GET_CURRENT_CONTEXT(ctx
);
590 gen_vertex_arrays(ctx
, n
, arrays
, true, "glCreateVertexArrays");
595 * Determine if ID is the name of an array object.
597 * \param id ID of the potential array object.
598 * \return \c GL_TRUE if \c id is the name of a array object,
599 * \c GL_FALSE otherwise.
602 _mesa_IsVertexArray( GLuint id
)
604 struct gl_vertex_array_object
* obj
;
605 GET_CURRENT_CONTEXT(ctx
);
606 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
611 obj
= _mesa_lookup_vao(ctx
, id
);
615 return obj
->EverBound
;
620 * Sets the element array buffer binding of a vertex array object.
622 * This is the ARB_direct_state_access equivalent of
623 * glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer).
626 _mesa_VertexArrayElementBuffer(GLuint vaobj
, GLuint buffer
)
628 GET_CURRENT_CONTEXT(ctx
);
629 struct gl_vertex_array_object
*vao
;
630 struct gl_buffer_object
*bufObj
;
632 ASSERT_OUTSIDE_BEGIN_END(ctx
);
634 /* The GL_ARB_direct_state_access specification says:
636 * "An INVALID_OPERATION error is generated by VertexArrayElementBuffer
637 * if <vaobj> is not [compatibility profile: zero or] the name of an
638 * existing vertex array object."
640 vao
=_mesa_lookup_vao_err(ctx
, vaobj
, "glVertexArrayElementBuffer");
644 /* The GL_ARB_direct_state_access specification says:
646 * "An INVALID_OPERATION error is generated if <buffer> is not zero or
647 * the name of an existing buffer object."
650 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
651 "glVertexArrayElementBuffer");
653 bufObj
= ctx
->Shared
->NullBufferObj
;
656 _mesa_reference_buffer_object(ctx
, &vao
->IndexBufferObj
, bufObj
);
661 _mesa_GetVertexArrayiv(GLuint vaobj
, GLenum pname
, GLint
*param
)
663 GET_CURRENT_CONTEXT(ctx
);
664 struct gl_vertex_array_object
*vao
;
666 ASSERT_OUTSIDE_BEGIN_END(ctx
);
668 /* The GL_ARB_direct_state_access specification says:
670 * "An INVALID_OPERATION error is generated if <vaobj> is not
671 * [compatibility profile: zero or] the name of an existing
672 * vertex array object."
674 vao
=_mesa_lookup_vao_err(ctx
, vaobj
, "glGetVertexArrayiv");
678 /* The GL_ARB_direct_state_access specification says:
680 * "An INVALID_ENUM error is generated if <pname> is not
681 * ELEMENT_ARRAY_BUFFER_BINDING."
683 if (pname
!= GL_ELEMENT_ARRAY_BUFFER_BINDING
) {
684 _mesa_error(ctx
, GL_INVALID_ENUM
,
685 "glGetVertexArrayiv(pname != "
686 "GL_ELEMENT_ARRAY_BUFFER_BINDING)");
690 param
[0] = vao
->IndexBufferObj
->Name
;