2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * (C) Copyright IBM Corporation 2006
7 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL OR IBM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * Functions for the GL_APPLE_vertex_array_object extension.
34 * The code in this file borrows a lot from bufferobj.c. There's a certain
35 * amount of cruft left over from that origin that may be unnecessary.
37 * \author Ian Romanick <idr@us.ibm.com>
46 #include "mfeatures.h"
47 #if FEATURE_ARB_vertex_buffer_object
48 #include "bufferobj.h"
53 #include "main/dispatch.h"
57 * Look up the array object for the given ID.
60 * Either a pointer to the array object with the specified ID or \c NULL for
61 * a non-existent ID. The spec defines ID 0 as being technically
65 static INLINE
struct gl_array_object
*
66 lookup_arrayobj(struct gl_context
*ctx
, GLuint id
)
71 return (struct gl_array_object
*)
72 _mesa_HashLookup(ctx
->Array
.Objects
, id
);
77 * For all the vertex arrays in the array object, unbind any pointers
78 * to any buffer objects (VBOs).
79 * This is done just prior to array object destruction.
82 unbind_array_object_vbos(struct gl_context
*ctx
, struct gl_array_object
*obj
)
86 _mesa_reference_buffer_object(ctx
, &obj
->Vertex
.BufferObj
, NULL
);
87 _mesa_reference_buffer_object(ctx
, &obj
->Weight
.BufferObj
, NULL
);
88 _mesa_reference_buffer_object(ctx
, &obj
->Normal
.BufferObj
, NULL
);
89 _mesa_reference_buffer_object(ctx
, &obj
->Color
.BufferObj
, NULL
);
90 _mesa_reference_buffer_object(ctx
, &obj
->SecondaryColor
.BufferObj
, NULL
);
91 _mesa_reference_buffer_object(ctx
, &obj
->FogCoord
.BufferObj
, NULL
);
92 _mesa_reference_buffer_object(ctx
, &obj
->Index
.BufferObj
, NULL
);
93 _mesa_reference_buffer_object(ctx
, &obj
->EdgeFlag
.BufferObj
, NULL
);
95 for (i
= 0; i
< Elements(obj
->TexCoord
); i
++)
96 _mesa_reference_buffer_object(ctx
, &obj
->TexCoord
[i
].BufferObj
, NULL
);
98 for (i
= 0; i
< Elements(obj
->VertexAttrib
); i
++)
99 _mesa_reference_buffer_object(ctx
, &obj
->VertexAttrib
[i
].BufferObj
,NULL
);
101 #if FEATURE_point_size_array
102 _mesa_reference_buffer_object(ctx
, &obj
->PointSize
.BufferObj
, NULL
);
108 * Allocate and initialize a new vertex array object.
110 * This function is intended to be called via
111 * \c dd_function_table::NewArrayObject.
113 struct gl_array_object
*
114 _mesa_new_array_object( struct gl_context
*ctx
, GLuint name
)
116 struct gl_array_object
*obj
= CALLOC_STRUCT(gl_array_object
);
118 _mesa_initialize_array_object(ctx
, obj
, name
);
124 * Delete an array object.
126 * This function is intended to be called via
127 * \c dd_function_table::DeleteArrayObject.
130 _mesa_delete_array_object( struct gl_context
*ctx
, struct gl_array_object
*obj
)
133 unbind_array_object_vbos(ctx
, obj
);
134 _glthread_DESTROY_MUTEX(obj
->Mutex
);
140 * Set ptr to arrayObj w/ reference counting.
143 _mesa_reference_array_object(struct gl_context
*ctx
,
144 struct gl_array_object
**ptr
,
145 struct gl_array_object
*arrayObj
)
147 if (*ptr
== arrayObj
)
151 /* Unreference the old array object */
152 GLboolean deleteFlag
= GL_FALSE
;
153 struct gl_array_object
*oldObj
= *ptr
;
155 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
156 ASSERT(oldObj
->RefCount
> 0);
159 printf("ArrayObj %p %d DECR to %d\n",
160 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
162 deleteFlag
= (oldObj
->RefCount
== 0);
163 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
166 ASSERT(ctx
->Driver
.DeleteArrayObject
);
167 ctx
->Driver
.DeleteArrayObject(ctx
, oldObj
);
175 /* reference new array object */
176 _glthread_LOCK_MUTEX(arrayObj
->Mutex
);
177 if (arrayObj
->RefCount
== 0) {
178 /* this array's being deleted (look just above) */
179 /* Not sure this can every really happen. Warn if it does. */
180 _mesa_problem(NULL
, "referencing deleted array object");
184 arrayObj
->RefCount
++;
186 printf("ArrayObj %p %d INCR to %d\n",
187 (void *) arrayObj
, arrayObj
->Name
, arrayObj
->RefCount
);
191 _glthread_UNLOCK_MUTEX(arrayObj
->Mutex
);
198 init_array(struct gl_context
*ctx
,
199 struct gl_client_array
*array
, GLint size
, GLint type
)
203 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
207 array
->Enabled
= GL_FALSE
;
208 array
->Normalized
= GL_FALSE
;
209 #if FEATURE_ARB_vertex_buffer_object
210 /* Vertex array buffers */
211 _mesa_reference_buffer_object(ctx
, &array
->BufferObj
,
212 ctx
->Shared
->NullBufferObj
);
218 * Initialize a gl_array_object's arrays.
221 _mesa_initialize_array_object( struct gl_context
*ctx
,
222 struct gl_array_object
*obj
,
229 _glthread_INIT_MUTEX(obj
->Mutex
);
232 /* Init the individual arrays */
233 init_array(ctx
, &obj
->Vertex
, 4, GL_FLOAT
);
234 init_array(ctx
, &obj
->Weight
, 1, GL_FLOAT
);
235 init_array(ctx
, &obj
->Normal
, 3, GL_FLOAT
);
236 init_array(ctx
, &obj
->Color
, 4, GL_FLOAT
);
237 init_array(ctx
, &obj
->SecondaryColor
, 3, GL_FLOAT
);
238 init_array(ctx
, &obj
->FogCoord
, 1, GL_FLOAT
);
239 init_array(ctx
, &obj
->Index
, 1, GL_FLOAT
);
240 for (i
= 0; i
< Elements(obj
->TexCoord
); i
++) {
241 init_array(ctx
, &obj
->TexCoord
[i
], 4, GL_FLOAT
);
243 init_array(ctx
, &obj
->EdgeFlag
, 1, GL_BOOL
);
244 for (i
= 0; i
< Elements(obj
->VertexAttrib
); i
++) {
245 init_array(ctx
, &obj
->VertexAttrib
[i
], 4, GL_FLOAT
);
248 #if FEATURE_point_size_array
249 init_array(ctx
, &obj
->PointSize
, 1, GL_FLOAT
);
255 * Add the given array object to the array object pool.
258 save_array_object( struct gl_context
*ctx
, struct gl_array_object
*obj
)
261 /* insert into hash table */
262 _mesa_HashInsert(ctx
->Array
.Objects
, obj
->Name
, obj
);
268 * Remove the given array object from the array object pool.
269 * Do not deallocate the array object though.
272 remove_array_object( struct gl_context
*ctx
, struct gl_array_object
*obj
)
275 /* remove from hash table */
276 _mesa_HashRemove(ctx
->Array
.Objects
, obj
->Name
);
283 * Compute the index of the last array element that can be safely accessed
284 * in a vertex array. We can really only do this when the array lives in
286 * The array->_MaxElement field will be updated.
287 * Later in glDrawArrays/Elements/etc we can do some bounds checking.
290 compute_max_element(struct gl_client_array
*array
)
292 if (array
->BufferObj
->Name
) {
293 /* Compute the max element we can access in the VBO without going
296 array
->_MaxElement
= ((GLsizeiptrARB
) array
->BufferObj
->Size
297 - (GLsizeiptrARB
) array
->Ptr
+ array
->StrideB
298 - array
->_ElementSize
) / array
->StrideB
;
300 printf("%s Object %u Size %u MaxElement %u\n",
302 array
->BufferObj
->Name
,
303 (GLuint
) array
->BufferObj
->Size
,
307 /* user-space array, no idea how big it is */
308 array
->_MaxElement
= 2 * 1000 * 1000 * 1000; /* just a big number */
314 * Helper for update_arrays().
315 * \return min(current min, array->_MaxElement).
318 update_min(GLuint min
, struct gl_client_array
*array
)
320 compute_max_element(array
);
322 return MIN2(min
, array
->_MaxElement
);
329 * Examine vertex arrays to update the gl_array_object::_MaxElement field.
332 _mesa_update_array_object_max_element(struct gl_context
*ctx
,
333 struct gl_array_object
*arrayObj
)
337 min
= update_min(min
, &arrayObj
->Vertex
);
338 min
= update_min(min
, &arrayObj
->Weight
);
339 min
= update_min(min
, &arrayObj
->Normal
);
340 min
= update_min(min
, &arrayObj
->Color
);
341 min
= update_min(min
, &arrayObj
->SecondaryColor
);
342 min
= update_min(min
, &arrayObj
->FogCoord
);
343 min
= update_min(min
, &arrayObj
->Index
);
344 min
= update_min(min
, &arrayObj
->EdgeFlag
);
345 #if FEATURE_point_size_array
346 min
= update_min(min
, &arrayObj
->PointSize
);
348 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
349 min
= update_min(min
, &arrayObj
->TexCoord
[i
]);
350 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++)
351 min
= update_min(min
, &arrayObj
->VertexAttrib
[i
]);
353 /* _MaxElement is one past the last legal array element */
354 arrayObj
->_MaxElement
= min
;
358 /**********************************************************************/
360 /**********************************************************************/
364 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
365 * \param genRequired specifies behavour when id was not generated with
366 * glGenVertexArrays().
369 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, GLboolean genRequired
)
371 struct gl_array_object
* const oldObj
= ctx
->Array
.ArrayObj
;
372 struct gl_array_object
*newObj
= NULL
;
373 ASSERT_OUTSIDE_BEGIN_END(ctx
);
375 ASSERT(oldObj
!= NULL
);
377 if ( oldObj
->Name
== id
)
378 return; /* rebinding the same array object- no change */
381 * Get pointer to new array object (newObj)
384 /* The spec says there is no array object named 0, but we use
385 * one internally because it simplifies things.
387 newObj
= ctx
->Array
.DefaultArrayObj
;
390 /* non-default array object */
391 newObj
= lookup_arrayobj(ctx
, id
);
394 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindVertexArray(id)");
398 /* For APPLE version, generate a new array object now */
399 newObj
= (*ctx
->Driver
.NewArrayObject
)(ctx
, id
);
401 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
404 save_array_object(ctx
, newObj
);
408 ctx
->NewState
|= _NEW_ARRAY
;
409 ctx
->Array
.NewState
|= _NEW_ARRAY_ALL
;
410 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, newObj
);
412 /* Pass BindVertexArray call to device driver */
413 if (ctx
->Driver
.BindArrayObject
&& newObj
)
414 ctx
->Driver
.BindArrayObject(ctx
, newObj
);
419 * ARB version of glBindVertexArray()
420 * This function behaves differently from glBindVertexArrayAPPLE() in
421 * that this function requires all ids to have been previously generated
422 * by glGenVertexArrays[APPLE]().
425 _mesa_BindVertexArray( GLuint id
)
427 GET_CURRENT_CONTEXT(ctx
);
428 bind_vertex_array(ctx
, id
, GL_TRUE
);
436 * The binding could be done more efficiently by comparing the non-NULL
437 * pointers in the old and new objects. The only arrays that are "dirty" are
438 * the ones that are non-NULL in either object.
441 _mesa_BindVertexArrayAPPLE( GLuint id
)
443 GET_CURRENT_CONTEXT(ctx
);
444 bind_vertex_array(ctx
, id
, GL_FALSE
);
449 * Delete a set of array objects.
451 * \param n Number of array objects to delete.
452 * \param ids Array of \c n array object IDs.
455 _mesa_DeleteVertexArraysAPPLE(GLsizei n
, const GLuint
*ids
)
457 GET_CURRENT_CONTEXT(ctx
);
459 ASSERT_OUTSIDE_BEGIN_END(ctx
);
462 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArrayAPPLE(n)");
466 for (i
= 0; i
< n
; i
++) {
467 struct gl_array_object
*obj
= lookup_arrayobj(ctx
, ids
[i
]);
470 ASSERT( obj
->Name
== ids
[i
] );
472 /* If the array object is currently bound, the spec says "the binding
473 * for that object reverts to zero and the default vertex array
476 if ( obj
== ctx
->Array
.ArrayObj
) {
477 CALL_BindVertexArrayAPPLE( ctx
->Exec
, (0) );
480 /* The ID is immediately freed for re-use */
481 remove_array_object(ctx
, obj
);
483 /* Unreference the array object.
484 * If refcount hits zero, the object will be deleted.
486 _mesa_reference_array_object(ctx
, &obj
, NULL
);
493 * Generate a set of unique array object IDs and store them in \c arrays.
494 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
495 * \param n Number of IDs to generate.
496 * \param arrays Array of \c n locations to store the IDs.
497 * \param vboOnly Will arrays have to reside in VBOs?
500 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
,
505 ASSERT_OUTSIDE_BEGIN_END(ctx
);
508 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenVertexArraysAPPLE");
516 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
518 /* Allocate new, empty array objects and return identifiers */
519 for (i
= 0; i
< n
; i
++) {
520 struct gl_array_object
*obj
;
521 GLuint name
= first
+ i
;
523 obj
= (*ctx
->Driver
.NewArrayObject
)( ctx
, name
);
525 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenVertexArraysAPPLE");
528 obj
->VBOonly
= vboOnly
;
529 save_array_object(ctx
, obj
);
530 arrays
[i
] = first
+ i
;
536 * ARB version of glGenVertexArrays()
537 * All arrays will be required to live in VBOs.
540 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
542 GET_CURRENT_CONTEXT(ctx
);
543 gen_vertex_arrays(ctx
, n
, arrays
, GL_TRUE
);
548 * APPLE version of glGenVertexArraysAPPLE()
549 * Arrays may live in VBOs or ordinary memory.
552 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
554 GET_CURRENT_CONTEXT(ctx
);
555 gen_vertex_arrays(ctx
, n
, arrays
, GL_FALSE
);
560 * Determine if ID is the name of an array object.
562 * \param id ID of the potential array object.
563 * \return \c GL_TRUE if \c id is the name of a array object,
564 * \c GL_FALSE otherwise.
567 _mesa_IsVertexArrayAPPLE( GLuint id
)
569 struct gl_array_object
* obj
;
570 GET_CURRENT_CONTEXT(ctx
);
571 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
576 obj
= lookup_arrayobj(ctx
, id
);
578 return (obj
!= NULL
) ? GL_TRUE
: GL_FALSE
;