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
< ARRAY_SIZE(obj
->VertexBinding
); i
++)
88 _mesa_reference_buffer_object(ctx
, &obj
->VertexBinding
[i
].BufferObj
, NULL
);
90 for (i
= 0; i
< ARRAY_SIZE(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
)
120 unbind_array_object_vbos(ctx
, obj
);
121 _mesa_reference_buffer_object(ctx
, &obj
->IndexBufferObj
, NULL
);
122 mtx_destroy(&obj
->Mutex
);
129 * Set ptr to vao w/ reference counting.
130 * Note: this should only be called from the _mesa_reference_vao()
134 _mesa_reference_vao_(struct gl_context
*ctx
,
135 struct gl_vertex_array_object
**ptr
,
136 struct gl_vertex_array_object
*vao
)
141 /* Unreference the old array object */
142 GLboolean deleteFlag
= GL_FALSE
;
143 struct gl_vertex_array_object
*oldObj
= *ptr
;
145 mtx_lock(&oldObj
->Mutex
);
146 assert(oldObj
->RefCount
> 0);
149 printf("ArrayObj %p %d DECR to %d\n",
150 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
152 deleteFlag
= (oldObj
->RefCount
== 0);
153 mtx_unlock(&oldObj
->Mutex
);
156 assert(ctx
->Driver
.DeleteArrayObject
);
157 ctx
->Driver
.DeleteArrayObject(ctx
, oldObj
);
165 /* reference new array object */
166 mtx_lock(&vao
->Mutex
);
167 if (vao
->RefCount
== 0) {
168 /* this array's being deleted (look just above) */
169 /* Not sure this can every really happen. Warn if it does. */
170 _mesa_problem(NULL
, "referencing deleted array object");
176 printf("ArrayObj %p %d INCR to %d\n",
177 (void *) vao
, vao
->Name
, vao
->RefCount
);
181 mtx_unlock(&vao
->Mutex
);
188 init_array(struct gl_context
*ctx
,
189 struct gl_vertex_array_object
*obj
, GLuint index
, GLint size
, GLint type
)
191 struct gl_vertex_attrib_array
*array
= &obj
->VertexAttrib
[index
];
192 struct gl_vertex_buffer_binding
*binding
= &obj
->VertexBinding
[index
];
196 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
199 array
->RelativeOffset
= 0;
200 array
->Enabled
= GL_FALSE
;
201 array
->Normalized
= GL_FALSE
;
202 array
->Integer
= GL_FALSE
;
203 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
204 array
->VertexBinding
= index
;
207 binding
->Stride
= array
->_ElementSize
;
208 binding
->BufferObj
= NULL
;
209 binding
->_BoundArrays
= BITFIELD64_BIT(index
);
211 /* Vertex array buffers */
212 _mesa_reference_buffer_object(ctx
, &binding
->BufferObj
,
213 ctx
->Shared
->NullBufferObj
);
218 * Initialize a gl_vertex_array_object's arrays.
221 _mesa_initialize_vao(struct gl_context
*ctx
,
222 struct gl_vertex_array_object
*obj
,
229 mtx_init(&obj
->Mutex
, mtx_plain
);
232 /* Init the individual arrays */
233 for (i
= 0; i
< ARRAY_SIZE(obj
->VertexAttrib
); i
++) {
235 case VERT_ATTRIB_WEIGHT
:
236 init_array(ctx
, obj
, VERT_ATTRIB_WEIGHT
, 1, GL_FLOAT
);
238 case VERT_ATTRIB_NORMAL
:
239 init_array(ctx
, obj
, VERT_ATTRIB_NORMAL
, 3, GL_FLOAT
);
241 case VERT_ATTRIB_COLOR1
:
242 init_array(ctx
, obj
, VERT_ATTRIB_COLOR1
, 3, GL_FLOAT
);
244 case VERT_ATTRIB_FOG
:
245 init_array(ctx
, obj
, VERT_ATTRIB_FOG
, 1, GL_FLOAT
);
247 case VERT_ATTRIB_COLOR_INDEX
:
248 init_array(ctx
, obj
, VERT_ATTRIB_COLOR_INDEX
, 1, GL_FLOAT
);
250 case VERT_ATTRIB_EDGEFLAG
:
251 init_array(ctx
, obj
, VERT_ATTRIB_EDGEFLAG
, 1, GL_BOOL
);
253 case VERT_ATTRIB_POINT_SIZE
:
254 init_array(ctx
, obj
, VERT_ATTRIB_POINT_SIZE
, 1, GL_FLOAT
);
257 init_array(ctx
, obj
, i
, 4, GL_FLOAT
);
262 _mesa_reference_buffer_object(ctx
, &obj
->IndexBufferObj
,
263 ctx
->Shared
->NullBufferObj
);
268 * Add the given array object to the array object pool.
271 save_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
274 /* insert into hash table */
275 _mesa_HashInsert(ctx
->Array
.Objects
, obj
->Name
, obj
);
281 * Remove the given array object from the array object pool.
282 * Do not deallocate the array object though.
285 remove_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
288 /* remove from hash table */
289 _mesa_HashRemove(ctx
->Array
.Objects
, obj
->Name
);
295 * Updates the derived gl_client_arrays when a gl_vertex_attrib_array
296 * or a gl_vertex_buffer_binding has changed.
299 _mesa_update_vao_client_arrays(struct gl_context
*ctx
,
300 struct gl_vertex_array_object
*vao
)
302 GLbitfield64 arrays
= vao
->NewArrays
;
305 struct gl_client_array
*client_array
;
306 struct gl_vertex_attrib_array
*attrib_array
;
307 struct gl_vertex_buffer_binding
*buffer_binding
;
309 GLint attrib
= ffsll(arrays
) - 1;
310 arrays
^= BITFIELD64_BIT(attrib
);
312 attrib_array
= &vao
->VertexAttrib
[attrib
];
313 buffer_binding
= &vao
->VertexBinding
[attrib_array
->VertexBinding
];
314 client_array
= &vao
->_VertexAttrib
[attrib
];
316 _mesa_update_client_array(ctx
, client_array
, attrib_array
,
322 /**********************************************************************/
324 /**********************************************************************/
328 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
329 * \param genRequired specifies behavour when id was not generated with
330 * glGenVertexArrays().
333 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, GLboolean genRequired
)
335 struct gl_vertex_array_object
* const oldObj
= ctx
->Array
.VAO
;
336 struct gl_vertex_array_object
*newObj
= NULL
;
338 assert(oldObj
!= NULL
);
340 if ( oldObj
->Name
== id
)
341 return; /* rebinding the same array object- no change */
344 * Get pointer to new array object (newObj)
347 /* The spec says there is no array object named 0, but we use
348 * one internally because it simplifies things.
350 newObj
= ctx
->Array
.DefaultVAO
;
353 /* non-default array object */
354 newObj
= _mesa_lookup_vao(ctx
, id
);
357 _mesa_error(ctx
, GL_INVALID_OPERATION
,
358 "glBindVertexArray(non-gen name)");
362 /* For APPLE version, generate a new array object now */
363 newObj
= (*ctx
->Driver
.NewArrayObject
)(ctx
, id
);
365 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
369 save_array_object(ctx
, newObj
);
372 if (!newObj
->EverBound
) {
373 /* The "Interactions with APPLE_vertex_array_object" section of the
374 * GL_ARB_vertex_array_object spec says:
376 * "The first bind call, either BindVertexArray or
377 * BindVertexArrayAPPLE, determines the semantic of the object."
379 newObj
->ARBsemantics
= genRequired
;
380 newObj
->EverBound
= GL_TRUE
;
384 if (ctx
->Array
.DrawMethod
== DRAW_ARRAYS
) {
385 /* The _DrawArrays pointer is pointing at the VAO being unbound and
386 * that VAO may be in the process of being deleted. If it's not going
387 * to be deleted, this will have no effect, because the pointer needs
388 * to be updated by the VBO module anyway.
390 * Before the VBO module can update the pointer, we have to set it
391 * to NULL for drivers not to set up arrays which are not bound,
392 * or to prevent a crash if the VAO being unbound is going to be
395 ctx
->Array
._DrawArrays
= NULL
;
396 ctx
->Array
.DrawMethod
= DRAW_NONE
;
399 ctx
->NewState
|= _NEW_ARRAY
;
400 _mesa_reference_vao(ctx
, &ctx
->Array
.VAO
, newObj
);
402 /* Pass BindVertexArray call to device driver */
403 if (ctx
->Driver
.BindArrayObject
&& newObj
)
404 ctx
->Driver
.BindArrayObject(ctx
, newObj
);
409 * ARB version of glBindVertexArray()
410 * This function behaves differently from glBindVertexArrayAPPLE() in
411 * that this function requires all ids to have been previously generated
412 * by glGenVertexArrays[APPLE]().
415 _mesa_BindVertexArray( GLuint id
)
417 GET_CURRENT_CONTEXT(ctx
);
418 bind_vertex_array(ctx
, id
, GL_TRUE
);
426 * The binding could be done more efficiently by comparing the non-NULL
427 * pointers in the old and new objects. The only arrays that are "dirty" are
428 * the ones that are non-NULL in either object.
431 _mesa_BindVertexArrayAPPLE( GLuint id
)
433 GET_CURRENT_CONTEXT(ctx
);
434 bind_vertex_array(ctx
, id
, GL_FALSE
);
439 * Delete a set of array objects.
441 * \param n Number of array objects to delete.
442 * \param ids Array of \c n array object IDs.
445 _mesa_DeleteVertexArrays(GLsizei n
, const GLuint
*ids
)
447 GET_CURRENT_CONTEXT(ctx
);
451 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArray(n)");
455 for (i
= 0; i
< n
; i
++) {
456 struct gl_vertex_array_object
*obj
= _mesa_lookup_vao(ctx
, ids
[i
]);
459 assert( obj
->Name
== ids
[i
] );
461 /* If the array object is currently bound, the spec says "the binding
462 * for that object reverts to zero and the default vertex array
465 if ( obj
== ctx
->Array
.VAO
) {
466 _mesa_BindVertexArray(0);
469 /* The ID is immediately freed for re-use */
470 remove_array_object(ctx
, obj
);
472 /* Unreference the array object.
473 * If refcount hits zero, the object will be deleted.
475 _mesa_reference_vao(ctx
, &obj
, NULL
);
482 * Generate a set of unique array object IDs and store them in \c arrays.
483 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
484 * \param n Number of IDs to generate.
485 * \param arrays Array of \c n locations to store the IDs.
486 * \param vboOnly Will arrays have to reside in VBOs?
489 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
)
495 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenVertexArrays");
503 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
505 /* Allocate new, empty array objects and return identifiers */
506 for (i
= 0; i
< n
; i
++) {
507 struct gl_vertex_array_object
*obj
;
508 GLuint name
= first
+ i
;
510 obj
= (*ctx
->Driver
.NewArrayObject
)( ctx
, name
);
512 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenVertexArrays");
515 save_array_object(ctx
, obj
);
516 arrays
[i
] = first
+ i
;
522 * ARB version of glGenVertexArrays()
523 * All arrays will be required to live in VBOs.
526 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
528 GET_CURRENT_CONTEXT(ctx
);
529 gen_vertex_arrays(ctx
, n
, arrays
);
534 * APPLE version of glGenVertexArraysAPPLE()
535 * Arrays may live in VBOs or ordinary memory.
538 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
540 GET_CURRENT_CONTEXT(ctx
);
541 gen_vertex_arrays(ctx
, n
, arrays
);
546 * Determine if ID is the name of an array object.
548 * \param id ID of the potential array object.
549 * \return \c GL_TRUE if \c id is the name of a array object,
550 * \c GL_FALSE otherwise.
553 _mesa_IsVertexArray( GLuint id
)
555 struct gl_vertex_array_object
* obj
;
556 GET_CURRENT_CONTEXT(ctx
);
557 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
562 obj
= _mesa_lookup_vao(ctx
, id
);
566 return obj
->EverBound
;