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 * For all the vertex binding points in the array object, unbind any pointers
79 * to any buffer objects (VBOs).
80 * This is done just prior to array object destruction.
83 unbind_array_object_vbos(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
87 for (i
= 0; i
< Elements(obj
->VertexBinding
); i
++)
88 _mesa_reference_buffer_object(ctx
, &obj
->VertexBinding
[i
].BufferObj
, NULL
);
90 for (i
= 0; i
< Elements(obj
->_VertexAttrib
); i
++)
91 _mesa_reference_buffer_object(ctx
, &obj
->_VertexAttrib
[i
].BufferObj
, NULL
);
96 * Allocate and initialize a new vertex array object.
98 * This function is intended to be called via
99 * \c dd_function_table::NewArrayObject.
101 struct gl_vertex_array_object
*
102 _mesa_new_vao(struct gl_context
*ctx
, GLuint name
)
104 struct gl_vertex_array_object
*obj
= CALLOC_STRUCT(gl_vertex_array_object
);
106 _mesa_initialize_vao(ctx
, obj
, name
);
112 * Delete an array object.
114 * This function is intended to be called via
115 * \c dd_function_table::DeleteArrayObject.
118 _mesa_delete_vao(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
121 unbind_array_object_vbos(ctx
, obj
);
122 _mesa_reference_buffer_object(ctx
, &obj
->IndexBufferObj
, NULL
);
123 _glthread_DESTROY_MUTEX(obj
->Mutex
);
130 * Set ptr to vao w/ reference counting.
131 * Note: this should only be called from the _mesa_reference_vao()
135 _mesa_reference_vao_(struct gl_context
*ctx
,
136 struct gl_vertex_array_object
**ptr
,
137 struct gl_vertex_array_object
*vao
)
142 /* Unreference the old array object */
143 GLboolean deleteFlag
= GL_FALSE
;
144 struct gl_vertex_array_object
*oldObj
= *ptr
;
146 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
147 ASSERT(oldObj
->RefCount
> 0);
150 printf("ArrayObj %p %d DECR to %d\n",
151 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
153 deleteFlag
= (oldObj
->RefCount
== 0);
154 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
157 ASSERT(ctx
->Driver
.DeleteArrayObject
);
158 ctx
->Driver
.DeleteArrayObject(ctx
, oldObj
);
166 /* reference new array object */
167 _glthread_LOCK_MUTEX(vao
->Mutex
);
168 if (vao
->RefCount
== 0) {
169 /* this array's being deleted (look just above) */
170 /* Not sure this can every really happen. Warn if it does. */
171 _mesa_problem(NULL
, "referencing deleted array object");
177 printf("ArrayObj %p %d INCR to %d\n",
178 (void *) vao
, vao
->Name
, vao
->RefCount
);
182 _glthread_UNLOCK_MUTEX(vao
->Mutex
);
189 init_array(struct gl_context
*ctx
,
190 struct gl_vertex_array_object
*obj
, GLuint index
, GLint size
, GLint type
)
192 struct gl_vertex_attrib_array
*array
= &obj
->VertexAttrib
[index
];
193 struct gl_vertex_buffer_binding
*binding
= &obj
->VertexBinding
[index
];
197 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
200 array
->RelativeOffset
= 0;
201 array
->Enabled
= GL_FALSE
;
202 array
->Normalized
= GL_FALSE
;
203 array
->Integer
= GL_FALSE
;
204 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
205 array
->VertexBinding
= index
;
208 binding
->Stride
= array
->_ElementSize
;
209 binding
->BufferObj
= NULL
;
210 binding
->_BoundArrays
= BITFIELD64_BIT(index
);
212 /* Vertex array buffers */
213 _mesa_reference_buffer_object(ctx
, &binding
->BufferObj
,
214 ctx
->Shared
->NullBufferObj
);
219 * Initialize a gl_vertex_array_object's arrays.
222 _mesa_initialize_vao(struct gl_context
*ctx
,
223 struct gl_vertex_array_object
*obj
,
230 _glthread_INIT_MUTEX(obj
->Mutex
);
233 /* Init the individual arrays */
234 for (i
= 0; i
< Elements(obj
->_VertexAttrib
); i
++) {
236 case VERT_ATTRIB_WEIGHT
:
237 init_array(ctx
, obj
, VERT_ATTRIB_WEIGHT
, 1, GL_FLOAT
);
239 case VERT_ATTRIB_NORMAL
:
240 init_array(ctx
, obj
, VERT_ATTRIB_NORMAL
, 3, GL_FLOAT
);
242 case VERT_ATTRIB_COLOR1
:
243 init_array(ctx
, obj
, VERT_ATTRIB_COLOR1
, 3, GL_FLOAT
);
245 case VERT_ATTRIB_FOG
:
246 init_array(ctx
, obj
, VERT_ATTRIB_FOG
, 1, GL_FLOAT
);
248 case VERT_ATTRIB_COLOR_INDEX
:
249 init_array(ctx
, obj
, VERT_ATTRIB_COLOR_INDEX
, 1, GL_FLOAT
);
251 case VERT_ATTRIB_EDGEFLAG
:
252 init_array(ctx
, obj
, VERT_ATTRIB_EDGEFLAG
, 1, GL_BOOL
);
254 case VERT_ATTRIB_POINT_SIZE
:
255 init_array(ctx
, obj
, VERT_ATTRIB_POINT_SIZE
, 1, GL_FLOAT
);
258 init_array(ctx
, obj
, i
, 4, GL_FLOAT
);
263 _mesa_reference_buffer_object(ctx
, &obj
->IndexBufferObj
,
264 ctx
->Shared
->NullBufferObj
);
269 * Add the given array object to the array object pool.
272 save_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
275 /* insert into hash table */
276 _mesa_HashInsert(ctx
->Array
.Objects
, obj
->Name
, obj
);
282 * Remove the given array object from the array object pool.
283 * Do not deallocate the array object though.
286 remove_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
289 /* remove from hash table */
290 _mesa_HashRemove(ctx
->Array
.Objects
, obj
->Name
);
297 * Helper for _mesa_update_vao_max_element().
298 * \return min(vao->_VertexAttrib[*]._MaxElement).
301 compute_max_element(struct gl_vertex_array_object
*vao
, GLbitfield64 enabled
)
303 GLuint min
= ~((GLuint
)0);
306 struct gl_client_array
*client_array
;
307 GLint attrib
= ffsll(enabled
) - 1;
308 enabled
^= BITFIELD64_BIT(attrib
);
310 client_array
= &vao
->_VertexAttrib
[attrib
];
311 assert(client_array
->Enabled
);
312 _mesa_update_array_max_element(client_array
);
313 min
= MIN2(min
, client_array
->_MaxElement
);
321 * Examine vertex arrays to update the gl_vertex_array_object::_MaxElement field.
324 _mesa_update_vao_max_element(struct gl_context
*ctx
,
325 struct gl_vertex_array_object
*vao
)
327 GLbitfield64 enabled
;
329 if (!ctx
->VertexProgram
._Current
||
330 ctx
->VertexProgram
._Current
== ctx
->VertexProgram
._TnlProgram
) {
331 enabled
= _mesa_array_object_get_enabled_ff(vao
);
333 enabled
= _mesa_array_object_get_enabled_arb(vao
);
336 /* _MaxElement is one past the last legal array element */
337 vao
->_MaxElement
= compute_max_element(vao
, enabled
);
342 * Updates the derived gl_client_arrays when a gl_vertex_attrib_array
343 * or a gl_vertex_buffer_binding has changed.
346 _mesa_update_vao_client_arrays(struct gl_context
*ctx
,
347 struct gl_vertex_array_object
*vao
)
349 GLbitfield64 arrays
= vao
->NewArrays
;
352 struct gl_client_array
*client_array
;
353 struct gl_vertex_attrib_array
*attrib_array
;
354 struct gl_vertex_buffer_binding
*buffer_binding
;
356 GLint attrib
= ffsll(arrays
) - 1;
357 arrays
^= BITFIELD64_BIT(attrib
);
359 attrib_array
= &vao
->VertexAttrib
[attrib
];
360 buffer_binding
= &vao
->VertexBinding
[attrib_array
->VertexBinding
];
361 client_array
= &vao
->_VertexAttrib
[attrib
];
363 _mesa_update_client_array(ctx
, client_array
, attrib_array
,
369 /**********************************************************************/
371 /**********************************************************************/
375 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
376 * \param genRequired specifies behavour when id was not generated with
377 * glGenVertexArrays().
380 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, GLboolean genRequired
)
382 struct gl_vertex_array_object
* const oldObj
= ctx
->Array
.VAO
;
383 struct gl_vertex_array_object
*newObj
= NULL
;
385 ASSERT(oldObj
!= NULL
);
387 if ( oldObj
->Name
== id
)
388 return; /* rebinding the same array object- no change */
391 * Get pointer to new array object (newObj)
394 /* The spec says there is no array object named 0, but we use
395 * one internally because it simplifies things.
397 newObj
= ctx
->Array
.DefaultVAO
;
400 /* non-default array object */
401 newObj
= _mesa_lookup_vao(ctx
, id
);
404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
405 "glBindVertexArray(non-gen name)");
409 /* For APPLE version, generate a new array object now */
410 newObj
= (*ctx
->Driver
.NewArrayObject
)(ctx
, id
);
412 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
416 save_array_object(ctx
, newObj
);
419 if (!newObj
->EverBound
) {
420 /* The "Interactions with APPLE_vertex_array_object" section of the
421 * GL_ARB_vertex_array_object spec says:
423 * "The first bind call, either BindVertexArray or
424 * BindVertexArrayAPPLE, determines the semantic of the object."
426 newObj
->ARBsemantics
= genRequired
;
427 newObj
->EverBound
= GL_TRUE
;
431 ctx
->NewState
|= _NEW_ARRAY
;
432 _mesa_reference_vao(ctx
, &ctx
->Array
.VAO
, newObj
);
434 /* Pass BindVertexArray call to device driver */
435 if (ctx
->Driver
.BindArrayObject
&& newObj
)
436 ctx
->Driver
.BindArrayObject(ctx
, newObj
);
441 * ARB version of glBindVertexArray()
442 * This function behaves differently from glBindVertexArrayAPPLE() in
443 * that this function requires all ids to have been previously generated
444 * by glGenVertexArrays[APPLE]().
447 _mesa_BindVertexArray( GLuint id
)
449 GET_CURRENT_CONTEXT(ctx
);
450 bind_vertex_array(ctx
, id
, GL_TRUE
);
458 * The binding could be done more efficiently by comparing the non-NULL
459 * pointers in the old and new objects. The only arrays that are "dirty" are
460 * the ones that are non-NULL in either object.
463 _mesa_BindVertexArrayAPPLE( GLuint id
)
465 GET_CURRENT_CONTEXT(ctx
);
466 bind_vertex_array(ctx
, id
, GL_FALSE
);
471 * Delete a set of array objects.
473 * \param n Number of array objects to delete.
474 * \param ids Array of \c n array object IDs.
477 _mesa_DeleteVertexArrays(GLsizei n
, const GLuint
*ids
)
479 GET_CURRENT_CONTEXT(ctx
);
483 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArray(n)");
487 for (i
= 0; i
< n
; i
++) {
488 struct gl_vertex_array_object
*obj
= _mesa_lookup_vao(ctx
, ids
[i
]);
491 ASSERT( obj
->Name
== ids
[i
] );
493 /* If the array object is currently bound, the spec says "the binding
494 * for that object reverts to zero and the default vertex array
497 if ( obj
== ctx
->Array
.VAO
) {
498 _mesa_BindVertexArray(0);
501 /* The ID is immediately freed for re-use */
502 remove_array_object(ctx
, obj
);
504 /* Unreference the array object.
505 * If refcount hits zero, the object will be deleted.
507 _mesa_reference_vao(ctx
, &obj
, NULL
);
514 * Generate a set of unique array object IDs and store them in \c arrays.
515 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
516 * \param n Number of IDs to generate.
517 * \param arrays Array of \c n locations to store the IDs.
518 * \param vboOnly Will arrays have to reside in VBOs?
521 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
)
527 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenVertexArrays");
535 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
537 /* Allocate new, empty array objects and return identifiers */
538 for (i
= 0; i
< n
; i
++) {
539 struct gl_vertex_array_object
*obj
;
540 GLuint name
= first
+ i
;
542 obj
= (*ctx
->Driver
.NewArrayObject
)( ctx
, name
);
544 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenVertexArrays");
547 save_array_object(ctx
, obj
);
548 arrays
[i
] = first
+ i
;
554 * ARB version of glGenVertexArrays()
555 * All arrays will be required to live in VBOs.
558 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
560 GET_CURRENT_CONTEXT(ctx
);
561 gen_vertex_arrays(ctx
, n
, arrays
);
566 * APPLE version of glGenVertexArraysAPPLE()
567 * Arrays may live in VBOs or ordinary memory.
570 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
572 GET_CURRENT_CONTEXT(ctx
);
573 gen_vertex_arrays(ctx
, n
, arrays
);
578 * Determine if ID is the name of an array object.
580 * \param id ID of the potential array object.
581 * \return \c GL_TRUE if \c id is the name of a array object,
582 * \c GL_FALSE otherwise.
585 _mesa_IsVertexArray( GLuint id
)
587 struct gl_vertex_array_object
* obj
;
588 GET_CURRENT_CONTEXT(ctx
);
589 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
594 obj
= _mesa_lookup_vao(ctx
, id
);
598 return obj
->EverBound
;