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.
30 * Functions for the GL_APPLE_vertex_array_object extension.
33 * The code in this file borrows a lot from bufferobj.c. There's a certain
34 * amount of cruft left over from that origin that may be unnecessary.
36 * \author Ian Romanick <idr@us.ibm.com>
46 #include "bufferobj.h"
51 #include "main/dispatch.h"
55 * Look up the array object for the given ID.
58 * Either a pointer to the array object with the specified ID or \c NULL for
59 * a non-existent ID. The spec defines ID 0 as being technically
63 struct gl_vertex_array_object
*
64 _mesa_lookup_arrayobj(struct gl_context
*ctx
, GLuint id
)
69 return (struct gl_vertex_array_object
*)
70 _mesa_HashLookup(ctx
->Array
.Objects
, id
);
75 * For all the vertex binding points in the array object, unbind any pointers
76 * to any buffer objects (VBOs).
77 * This is done just prior to array object destruction.
80 unbind_array_object_vbos(struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
84 for (i
= 0; i
< Elements(obj
->VertexBinding
); i
++)
85 _mesa_reference_buffer_object(ctx
, &obj
->VertexBinding
[i
].BufferObj
, NULL
);
87 for (i
= 0; i
< Elements(obj
->_VertexAttrib
); i
++)
88 _mesa_reference_buffer_object(ctx
, &obj
->_VertexAttrib
[i
].BufferObj
, NULL
);
93 * Allocate and initialize a new vertex array object.
95 * This function is intended to be called via
96 * \c dd_function_table::NewArrayObject.
98 struct gl_vertex_array_object
*
99 _mesa_new_array_object( struct gl_context
*ctx
, GLuint name
)
101 struct gl_vertex_array_object
*obj
= CALLOC_STRUCT(gl_vertex_array_object
);
103 _mesa_initialize_array_object(ctx
, obj
, name
);
109 * Delete an array object.
111 * This function is intended to be called via
112 * \c dd_function_table::DeleteArrayObject.
115 _mesa_delete_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
118 unbind_array_object_vbos(ctx
, obj
);
119 _mesa_reference_buffer_object(ctx
, &obj
->ElementArrayBufferObj
, NULL
);
120 _glthread_DESTROY_MUTEX(obj
->Mutex
);
127 * Set ptr to vao w/ reference counting.
128 * Note: this should only be called from the _mesa_reference_array_object()
132 _mesa_reference_array_object_(struct gl_context
*ctx
,
133 struct gl_vertex_array_object
**ptr
,
134 struct gl_vertex_array_object
*vao
)
139 /* Unreference the old array object */
140 GLboolean deleteFlag
= GL_FALSE
;
141 struct gl_vertex_array_object
*oldObj
= *ptr
;
143 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
144 ASSERT(oldObj
->RefCount
> 0);
147 printf("ArrayObj %p %d DECR to %d\n",
148 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
150 deleteFlag
= (oldObj
->RefCount
== 0);
151 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
154 ASSERT(ctx
->Driver
.DeleteArrayObject
);
155 ctx
->Driver
.DeleteArrayObject(ctx
, oldObj
);
163 /* reference new array object */
164 _glthread_LOCK_MUTEX(vao
->Mutex
);
165 if (vao
->RefCount
== 0) {
166 /* this array's being deleted (look just above) */
167 /* Not sure this can every really happen. Warn if it does. */
168 _mesa_problem(NULL
, "referencing deleted array object");
174 printf("ArrayObj %p %d INCR to %d\n",
175 (void *) vao
, vao
->Name
, vao
->RefCount
);
179 _glthread_UNLOCK_MUTEX(vao
->Mutex
);
186 init_array(struct gl_context
*ctx
,
187 struct gl_vertex_array_object
*obj
, GLuint index
, GLint size
, GLint type
)
189 struct gl_vertex_attrib_array
*array
= &obj
->VertexAttrib
[index
];
190 struct gl_vertex_buffer_binding
*binding
= &obj
->VertexBinding
[index
];
194 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
197 array
->RelativeOffset
= 0;
198 array
->Enabled
= GL_FALSE
;
199 array
->Normalized
= GL_FALSE
;
200 array
->Integer
= GL_FALSE
;
201 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
202 array
->VertexBinding
= index
;
205 binding
->Stride
= array
->_ElementSize
;
206 binding
->BufferObj
= NULL
;
207 binding
->_BoundArrays
= BITFIELD64_BIT(index
);
209 /* Vertex array buffers */
210 _mesa_reference_buffer_object(ctx
, &binding
->BufferObj
,
211 ctx
->Shared
->NullBufferObj
);
216 * Initialize a gl_vertex_array_object's arrays.
219 _mesa_initialize_array_object( struct gl_context
*ctx
,
220 struct gl_vertex_array_object
*obj
,
227 _glthread_INIT_MUTEX(obj
->Mutex
);
230 /* Init the individual arrays */
231 for (i
= 0; i
< Elements(obj
->_VertexAttrib
); i
++) {
233 case VERT_ATTRIB_WEIGHT
:
234 init_array(ctx
, obj
, VERT_ATTRIB_WEIGHT
, 1, GL_FLOAT
);
236 case VERT_ATTRIB_NORMAL
:
237 init_array(ctx
, obj
, VERT_ATTRIB_NORMAL
, 3, GL_FLOAT
);
239 case VERT_ATTRIB_COLOR1
:
240 init_array(ctx
, obj
, VERT_ATTRIB_COLOR1
, 3, GL_FLOAT
);
242 case VERT_ATTRIB_FOG
:
243 init_array(ctx
, obj
, VERT_ATTRIB_FOG
, 1, GL_FLOAT
);
245 case VERT_ATTRIB_COLOR_INDEX
:
246 init_array(ctx
, obj
, VERT_ATTRIB_COLOR_INDEX
, 1, GL_FLOAT
);
248 case VERT_ATTRIB_EDGEFLAG
:
249 init_array(ctx
, obj
, VERT_ATTRIB_EDGEFLAG
, 1, GL_BOOL
);
251 case VERT_ATTRIB_POINT_SIZE
:
252 init_array(ctx
, obj
, VERT_ATTRIB_POINT_SIZE
, 1, GL_FLOAT
);
255 init_array(ctx
, obj
, i
, 4, GL_FLOAT
);
260 _mesa_reference_buffer_object(ctx
, &obj
->ElementArrayBufferObj
,
261 ctx
->Shared
->NullBufferObj
);
266 * Add the given array object to the array object pool.
269 save_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
272 /* insert into hash table */
273 _mesa_HashInsert(ctx
->Array
.Objects
, obj
->Name
, obj
);
279 * Remove the given array object from the array object pool.
280 * Do not deallocate the array object though.
283 remove_array_object( struct gl_context
*ctx
, struct gl_vertex_array_object
*obj
)
286 /* remove from hash table */
287 _mesa_HashRemove(ctx
->Array
.Objects
, obj
->Name
);
294 * Helper for _mesa_update_array_object_max_element().
295 * \return min(vao->_VertexAttrib[*]._MaxElement).
298 compute_max_element(struct gl_vertex_array_object
*vao
, GLbitfield64 enabled
)
300 GLuint min
= ~((GLuint
)0);
303 struct gl_client_array
*client_array
;
304 GLint attrib
= ffsll(enabled
) - 1;
305 enabled
^= BITFIELD64_BIT(attrib
);
307 client_array
= &vao
->_VertexAttrib
[attrib
];
308 assert(client_array
->Enabled
);
309 _mesa_update_array_max_element(client_array
);
310 min
= MIN2(min
, client_array
->_MaxElement
);
318 * Examine vertex arrays to update the gl_vertex_array_object::_MaxElement field.
321 _mesa_update_array_object_max_element(struct gl_context
*ctx
,
322 struct gl_vertex_array_object
*vao
)
324 GLbitfield64 enabled
;
326 if (!ctx
->VertexProgram
._Current
||
327 ctx
->VertexProgram
._Current
== ctx
->VertexProgram
._TnlProgram
) {
328 enabled
= _mesa_array_object_get_enabled_ff(vao
);
330 enabled
= _mesa_array_object_get_enabled_arb(vao
);
333 /* _MaxElement is one past the last legal array element */
334 vao
->_MaxElement
= compute_max_element(vao
, enabled
);
339 * Updates the derived gl_client_arrays when a gl_vertex_attrib_array
340 * or a gl_vertex_buffer_binding has changed.
343 _mesa_update_array_object_client_arrays(struct gl_context
*ctx
,
344 struct gl_vertex_array_object
*vao
)
346 GLbitfield64 arrays
= vao
->NewArrays
;
349 struct gl_client_array
*client_array
;
350 struct gl_vertex_attrib_array
*attrib_array
;
351 struct gl_vertex_buffer_binding
*buffer_binding
;
353 GLint attrib
= ffsll(arrays
) - 1;
354 arrays
^= BITFIELD64_BIT(attrib
);
356 attrib_array
= &vao
->VertexAttrib
[attrib
];
357 buffer_binding
= &vao
->VertexBinding
[attrib_array
->VertexBinding
];
358 client_array
= &vao
->_VertexAttrib
[attrib
];
360 _mesa_update_client_array(ctx
, client_array
, attrib_array
,
366 /**********************************************************************/
368 /**********************************************************************/
372 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
373 * \param genRequired specifies behavour when id was not generated with
374 * glGenVertexArrays().
377 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, GLboolean genRequired
)
379 struct gl_vertex_array_object
* const oldObj
= ctx
->Array
.VAO
;
380 struct gl_vertex_array_object
*newObj
= NULL
;
382 ASSERT(oldObj
!= NULL
);
384 if ( oldObj
->Name
== id
)
385 return; /* rebinding the same array object- no change */
388 * Get pointer to new array object (newObj)
391 /* The spec says there is no array object named 0, but we use
392 * one internally because it simplifies things.
394 newObj
= ctx
->Array
.DefaultVAO
;
397 /* non-default array object */
398 newObj
= _mesa_lookup_arrayobj(ctx
, id
);
401 _mesa_error(ctx
, GL_INVALID_OPERATION
,
402 "glBindVertexArray(non-gen name)");
406 /* For APPLE version, generate a new array object now */
407 newObj
= (*ctx
->Driver
.NewArrayObject
)(ctx
, id
);
409 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
413 save_array_object(ctx
, newObj
);
416 if (!newObj
->EverBound
) {
417 /* The "Interactions with APPLE_vertex_array_object" section of the
418 * GL_ARB_vertex_array_object spec says:
420 * "The first bind call, either BindVertexArray or
421 * BindVertexArrayAPPLE, determines the semantic of the object."
423 newObj
->ARBsemantics
= genRequired
;
424 newObj
->EverBound
= GL_TRUE
;
428 ctx
->NewState
|= _NEW_ARRAY
;
429 _mesa_reference_array_object(ctx
, &ctx
->Array
.VAO
, newObj
);
431 /* Pass BindVertexArray call to device driver */
432 if (ctx
->Driver
.BindArrayObject
&& newObj
)
433 ctx
->Driver
.BindArrayObject(ctx
, newObj
);
438 * ARB version of glBindVertexArray()
439 * This function behaves differently from glBindVertexArrayAPPLE() in
440 * that this function requires all ids to have been previously generated
441 * by glGenVertexArrays[APPLE]().
444 _mesa_BindVertexArray( GLuint id
)
446 GET_CURRENT_CONTEXT(ctx
);
447 bind_vertex_array(ctx
, id
, GL_TRUE
);
455 * The binding could be done more efficiently by comparing the non-NULL
456 * pointers in the old and new objects. The only arrays that are "dirty" are
457 * the ones that are non-NULL in either object.
460 _mesa_BindVertexArrayAPPLE( GLuint id
)
462 GET_CURRENT_CONTEXT(ctx
);
463 bind_vertex_array(ctx
, id
, GL_FALSE
);
468 * Delete a set of array objects.
470 * \param n Number of array objects to delete.
471 * \param ids Array of \c n array object IDs.
474 _mesa_DeleteVertexArrays(GLsizei n
, const GLuint
*ids
)
476 GET_CURRENT_CONTEXT(ctx
);
480 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArrayAPPLE(n)");
484 for (i
= 0; i
< n
; i
++) {
485 struct gl_vertex_array_object
*obj
= _mesa_lookup_arrayobj(ctx
, ids
[i
]);
488 ASSERT( obj
->Name
== ids
[i
] );
490 /* If the array object is currently bound, the spec says "the binding
491 * for that object reverts to zero and the default vertex array
494 if ( obj
== ctx
->Array
.VAO
) {
495 _mesa_BindVertexArray(0);
498 /* The ID is immediately freed for re-use */
499 remove_array_object(ctx
, obj
);
501 /* Unreference the array object.
502 * If refcount hits zero, the object will be deleted.
504 _mesa_reference_array_object(ctx
, &obj
, NULL
);
511 * Generate a set of unique array object IDs and store them in \c arrays.
512 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
513 * \param n Number of IDs to generate.
514 * \param arrays Array of \c n locations to store the IDs.
515 * \param vboOnly Will arrays have to reside in VBOs?
518 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
)
524 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenVertexArraysAPPLE");
532 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
534 /* Allocate new, empty array objects and return identifiers */
535 for (i
= 0; i
< n
; i
++) {
536 struct gl_vertex_array_object
*obj
;
537 GLuint name
= first
+ i
;
539 obj
= (*ctx
->Driver
.NewArrayObject
)( ctx
, name
);
541 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenVertexArraysAPPLE");
544 save_array_object(ctx
, obj
);
545 arrays
[i
] = first
+ i
;
551 * ARB version of glGenVertexArrays()
552 * All arrays will be required to live in VBOs.
555 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
557 GET_CURRENT_CONTEXT(ctx
);
558 gen_vertex_arrays(ctx
, n
, arrays
);
563 * APPLE version of glGenVertexArraysAPPLE()
564 * Arrays may live in VBOs or ordinary memory.
567 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
569 GET_CURRENT_CONTEXT(ctx
);
570 gen_vertex_arrays(ctx
, n
, arrays
);
575 * Determine if ID is the name of an array object.
577 * \param id ID of the potential array object.
578 * \return \c GL_TRUE if \c id is the name of a array object,
579 * \c GL_FALSE otherwise.
582 _mesa_IsVertexArray( GLuint id
)
584 struct gl_vertex_array_object
* obj
;
585 GET_CURRENT_CONTEXT(ctx
);
586 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
591 obj
= _mesa_lookup_arrayobj(ctx
, id
);
595 return obj
->EverBound
;