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"
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_HashLookup(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_HashLookup(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
->VertexBinding
); i
++)
143 _mesa_reference_buffer_object(ctx
, &obj
->VertexBinding
[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
);
171 mtx_destroy(&obj
->Mutex
);
178 * Set ptr to vao w/ reference counting.
179 * Note: this should only be called from the _mesa_reference_vao()
183 _mesa_reference_vao_(struct gl_context
*ctx
,
184 struct gl_vertex_array_object
**ptr
,
185 struct gl_vertex_array_object
*vao
)
190 /* Unreference the old array object */
191 GLboolean deleteFlag
= GL_FALSE
;
192 struct gl_vertex_array_object
*oldObj
= *ptr
;
194 mtx_lock(&oldObj
->Mutex
);
195 assert(oldObj
->RefCount
> 0);
197 deleteFlag
= (oldObj
->RefCount
== 0);
198 mtx_unlock(&oldObj
->Mutex
);
201 _mesa_delete_vao(ctx
, oldObj
);
208 /* reference new array object */
209 mtx_lock(&vao
->Mutex
);
210 if (vao
->RefCount
== 0) {
211 /* this array's being deleted (look just above) */
212 /* Not sure this can every really happen. Warn if it does. */
213 _mesa_problem(NULL
, "referencing deleted array object");
220 mtx_unlock(&vao
->Mutex
);
227 init_array(struct gl_context
*ctx
,
228 struct gl_vertex_array_object
*obj
, GLuint index
, GLint size
, GLint type
)
230 struct gl_vertex_attrib_array
*array
= &obj
->VertexAttrib
[index
];
231 struct gl_vertex_buffer_binding
*binding
= &obj
->VertexBinding
[index
];
235 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
238 array
->RelativeOffset
= 0;
239 array
->Enabled
= GL_FALSE
;
240 array
->Normalized
= GL_FALSE
;
241 array
->Integer
= GL_FALSE
;
242 array
->Doubles
= GL_FALSE
;
243 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
244 array
->VertexBinding
= index
;
247 binding
->Stride
= array
->_ElementSize
;
248 binding
->BufferObj
= NULL
;
249 binding
->_BoundArrays
= BITFIELD64_BIT(index
);
251 /* Vertex array buffers */
252 _mesa_reference_buffer_object(ctx
, &binding
->BufferObj
,
253 ctx
->Shared
->NullBufferObj
);
258 * Initialize a gl_vertex_array_object's arrays.
261 _mesa_initialize_vao(struct gl_context
*ctx
,
262 struct gl_vertex_array_object
*obj
,
269 mtx_init(&obj
->Mutex
, mtx_plain
);
272 /* Init the individual arrays */
273 for (i
= 0; i
< ARRAY_SIZE(obj
->VertexAttrib
); i
++) {
275 case VERT_ATTRIB_WEIGHT
:
276 init_array(ctx
, obj
, VERT_ATTRIB_WEIGHT
, 1, GL_FLOAT
);
278 case VERT_ATTRIB_NORMAL
:
279 init_array(ctx
, obj
, VERT_ATTRIB_NORMAL
, 3, GL_FLOAT
);
281 case VERT_ATTRIB_COLOR1
:
282 init_array(ctx
, obj
, VERT_ATTRIB_COLOR1
, 3, GL_FLOAT
);
284 case VERT_ATTRIB_FOG
:
285 init_array(ctx
, obj
, VERT_ATTRIB_FOG
, 1, GL_FLOAT
);
287 case VERT_ATTRIB_COLOR_INDEX
:
288 init_array(ctx
, obj
, VERT_ATTRIB_COLOR_INDEX
, 1, GL_FLOAT
);
290 case VERT_ATTRIB_EDGEFLAG
:
291 init_array(ctx
, obj
, VERT_ATTRIB_EDGEFLAG
, 1, GL_BOOL
);
293 case VERT_ATTRIB_POINT_SIZE
:
294 init_array(ctx
, obj
, VERT_ATTRIB_POINT_SIZE
, 1, GL_FLOAT
);
297 init_array(ctx
, obj
, i
, 4, GL_FLOAT
);
302 _mesa_reference_buffer_object(ctx
, &obj
->IndexBufferObj
,
303 ctx
->Shared
->NullBufferObj
);
308 * Add the given array object to the array object pool.
311 save_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
314 /* insert into hash table */
315 _mesa_HashInsert(ctx
->Array
.Objects
, obj
->Name
, obj
);
321 * Remove the given array object from the array object pool.
322 * Do not deallocate the array object though.
325 remove_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
328 /* remove from hash table */
329 _mesa_HashRemove(ctx
->Array
.Objects
, obj
->Name
);
335 * Updates the derived gl_client_arrays when a gl_vertex_attrib_array
336 * or a gl_vertex_buffer_binding has changed.
339 _mesa_update_vao_client_arrays(struct gl_context
*ctx
,
340 struct gl_vertex_array_object
*vao
)
342 GLbitfield64 arrays
= vao
->NewArrays
;
345 struct gl_client_array
*client_array
;
346 struct gl_vertex_attrib_array
*attrib_array
;
347 struct gl_vertex_buffer_binding
*buffer_binding
;
349 GLint attrib
= ffsll(arrays
) - 1;
350 arrays
^= BITFIELD64_BIT(attrib
);
352 attrib_array
= &vao
->VertexAttrib
[attrib
];
353 buffer_binding
= &vao
->VertexBinding
[attrib_array
->VertexBinding
];
354 client_array
= &vao
->_VertexAttrib
[attrib
];
356 _mesa_update_client_array(ctx
, client_array
, attrib_array
,
362 /**********************************************************************/
364 /**********************************************************************/
368 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
369 * \param genRequired specifies behavour when id was not generated with
370 * glGenVertexArrays().
373 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, GLboolean genRequired
)
375 struct gl_vertex_array_object
* const oldObj
= ctx
->Array
.VAO
;
376 struct gl_vertex_array_object
*newObj
= NULL
;
378 assert(oldObj
!= NULL
);
380 if ( oldObj
->Name
== id
)
381 return; /* rebinding the same array object- no change */
384 * Get pointer to new array object (newObj)
387 /* The spec says there is no array object named 0, but we use
388 * one internally because it simplifies things.
390 newObj
= ctx
->Array
.DefaultVAO
;
393 /* non-default array object */
394 newObj
= _mesa_lookup_vao(ctx
, id
);
397 _mesa_error(ctx
, GL_INVALID_OPERATION
,
398 "glBindVertexArray(non-gen name)");
402 /* For APPLE version, generate a new array object now */
403 newObj
= _mesa_new_vao(ctx
, id
);
405 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
409 save_array_object(ctx
, newObj
);
412 if (!newObj
->EverBound
) {
413 /* The "Interactions with APPLE_vertex_array_object" section of the
414 * GL_ARB_vertex_array_object spec says:
416 * "The first bind call, either BindVertexArray or
417 * BindVertexArrayAPPLE, determines the semantic of the object."
419 newObj
->ARBsemantics
= genRequired
;
420 newObj
->EverBound
= GL_TRUE
;
424 if (ctx
->Array
.DrawMethod
== DRAW_ARRAYS
) {
425 /* The _DrawArrays pointer is pointing at the VAO being unbound and
426 * that VAO may be in the process of being deleted. If it's not going
427 * to be deleted, this will have no effect, because the pointer needs
428 * to be updated by the VBO module anyway.
430 * Before the VBO module can update the pointer, we have to set it
431 * to NULL for drivers not to set up arrays which are not bound,
432 * or to prevent a crash if the VAO being unbound is going to be
435 ctx
->Array
._DrawArrays
= NULL
;
436 ctx
->Array
.DrawMethod
= DRAW_NONE
;
439 ctx
->NewState
|= _NEW_ARRAY
;
440 _mesa_reference_vao(ctx
, &ctx
->Array
.VAO
, newObj
);
445 * ARB version of glBindVertexArray()
446 * This function behaves differently from glBindVertexArrayAPPLE() in
447 * that this function requires all ids to have been previously generated
448 * by glGenVertexArrays[APPLE]().
451 _mesa_BindVertexArray( GLuint id
)
453 GET_CURRENT_CONTEXT(ctx
);
454 bind_vertex_array(ctx
, id
, GL_TRUE
);
462 * The binding could be done more efficiently by comparing the non-NULL
463 * pointers in the old and new objects. The only arrays that are "dirty" are
464 * the ones that are non-NULL in either object.
467 _mesa_BindVertexArrayAPPLE( GLuint id
)
469 GET_CURRENT_CONTEXT(ctx
);
470 bind_vertex_array(ctx
, id
, GL_FALSE
);
475 * Delete a set of array objects.
477 * \param n Number of array objects to delete.
478 * \param ids Array of \c n array object IDs.
481 _mesa_DeleteVertexArrays(GLsizei n
, const GLuint
*ids
)
483 GET_CURRENT_CONTEXT(ctx
);
487 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArray(n)");
491 for (i
= 0; i
< n
; i
++) {
492 struct gl_vertex_array_object
*obj
= _mesa_lookup_vao(ctx
, ids
[i
]);
495 assert( obj
->Name
== ids
[i
] );
497 /* If the array object is currently bound, the spec says "the binding
498 * for that object reverts to zero and the default vertex array
501 if ( obj
== ctx
->Array
.VAO
) {
502 _mesa_BindVertexArray(0);
505 /* The ID is immediately freed for re-use */
506 remove_array_object(ctx
, obj
);
508 if (ctx
->Array
.LastLookedUpVAO
== obj
)
509 _mesa_reference_vao(ctx
, &ctx
->Array
.LastLookedUpVAO
, NULL
);
511 /* Unreference the array object.
512 * If refcount hits zero, the object will be deleted.
514 _mesa_reference_vao(ctx
, &obj
, NULL
);
521 * Generate a set of unique array object IDs and store them in \c arrays.
522 * Helper for _mesa_GenVertexArrays[APPLE]() and _mesa_CreateVertexArrays()
525 * \param n Number of IDs to generate.
526 * \param arrays Array of \c n locations to store the IDs.
527 * \param create Indicates that the objects should also be created.
528 * \param func The name of the GL entry point.
531 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
,
532 bool create
, const char *func
)
538 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
546 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
548 /* For the sake of simplicity we create the array objects in both
549 * the Gen* and Create* cases. The only difference is the value of
550 * EverBound, which is set to true in the Create* case.
552 for (i
= 0; i
< n
; i
++) {
553 struct gl_vertex_array_object
*obj
;
554 GLuint name
= first
+ i
;
556 obj
= _mesa_new_vao(ctx
, name
);
558 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
561 obj
->EverBound
= create
;
562 save_array_object(ctx
, obj
);
563 arrays
[i
] = first
+ i
;
569 * ARB version of glGenVertexArrays()
570 * All arrays will be required to live in VBOs.
573 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
575 GET_CURRENT_CONTEXT(ctx
);
576 gen_vertex_arrays(ctx
, n
, arrays
, false, "glGenVertexArrays");
581 * APPLE version of glGenVertexArraysAPPLE()
582 * Arrays may live in VBOs or ordinary memory.
585 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
587 GET_CURRENT_CONTEXT(ctx
);
588 gen_vertex_arrays(ctx
, n
, arrays
, false, "glGenVertexArraysAPPLE");
593 * ARB_direct_state_access
594 * Generates ID's and creates the array objects.
597 _mesa_CreateVertexArrays(GLsizei n
, GLuint
*arrays
)
599 GET_CURRENT_CONTEXT(ctx
);
600 gen_vertex_arrays(ctx
, n
, arrays
, true, "glCreateVertexArrays");
605 * Determine if ID is the name of an array object.
607 * \param id ID of the potential array object.
608 * \return \c GL_TRUE if \c id is the name of a array object,
609 * \c GL_FALSE otherwise.
612 _mesa_IsVertexArray( GLuint id
)
614 struct gl_vertex_array_object
* obj
;
615 GET_CURRENT_CONTEXT(ctx
);
616 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
621 obj
= _mesa_lookup_vao(ctx
, id
);
625 return obj
->EverBound
;
630 * Sets the element array buffer binding of a vertex array object.
632 * This is the ARB_direct_state_access equivalent of
633 * glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer).
636 _mesa_VertexArrayElementBuffer(GLuint vaobj
, GLuint buffer
)
638 GET_CURRENT_CONTEXT(ctx
);
639 struct gl_vertex_array_object
*vao
;
640 struct gl_buffer_object
*bufObj
;
642 ASSERT_OUTSIDE_BEGIN_END(ctx
);
644 /* The GL_ARB_direct_state_access specification says:
646 * "An INVALID_OPERATION error is generated by VertexArrayElementBuffer
647 * if <vaobj> is not [compatibility profile: zero or] the name of an
648 * existing vertex array object."
650 vao
=_mesa_lookup_vao_err(ctx
, vaobj
, "glVertexArrayElementBuffer");
654 /* The GL_ARB_direct_state_access specification says:
656 * "An INVALID_OPERATION error is generated if <buffer> is not zero or
657 * the name of an existing buffer object."
660 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
661 "glVertexArrayElementBuffer");
663 bufObj
= ctx
->Shared
->NullBufferObj
;
666 _mesa_reference_buffer_object(ctx
, &vao
->IndexBufferObj
, bufObj
);
671 _mesa_GetVertexArrayiv(GLuint vaobj
, GLenum pname
, GLint
*param
)
673 GET_CURRENT_CONTEXT(ctx
);
674 struct gl_vertex_array_object
*vao
;
676 ASSERT_OUTSIDE_BEGIN_END(ctx
);
678 /* The GL_ARB_direct_state_access specification says:
680 * "An INVALID_OPERATION error is generated if <vaobj> is not
681 * [compatibility profile: zero or] the name of an existing
682 * vertex array object."
684 vao
=_mesa_lookup_vao_err(ctx
, vaobj
, "glGetVertexArrayiv");
688 /* The GL_ARB_direct_state_access specification says:
690 * "An INVALID_ENUM error is generated if <pname> is not
691 * ELEMENT_ARRAY_BUFFER_BINDING."
693 if (pname
!= GL_ELEMENT_ARRAY_BUFFER_BINDING
) {
694 _mesa_error(ctx
, GL_INVALID_ENUM
,
695 "glGetVertexArrayiv(pname != "
696 "GL_ELEMENT_ARRAY_BUFFER_BINDING)");
700 param
[0] = vao
->IndexBufferObj
->Name
;