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>
47 #include "mfeatures.h"
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 for (i
= 0; i
< Elements(obj
->VertexAttrib
); i
++)
87 _mesa_reference_buffer_object(ctx
, &obj
->VertexAttrib
[i
].BufferObj
, NULL
);
92 * Allocate and initialize a new vertex array object.
94 * This function is intended to be called via
95 * \c dd_function_table::NewArrayObject.
97 struct gl_array_object
*
98 _mesa_new_array_object( struct gl_context
*ctx
, GLuint name
)
100 struct gl_array_object
*obj
= CALLOC_STRUCT(gl_array_object
);
102 _mesa_initialize_array_object(ctx
, obj
, name
);
108 * Delete an array object.
110 * This function is intended to be called via
111 * \c dd_function_table::DeleteArrayObject.
114 _mesa_delete_array_object( struct gl_context
*ctx
, struct gl_array_object
*obj
)
117 unbind_array_object_vbos(ctx
, obj
);
118 _mesa_reference_buffer_object(ctx
, &obj
->ElementArrayBufferObj
, NULL
);
119 _glthread_DESTROY_MUTEX(obj
->Mutex
);
125 * Set ptr to arrayObj w/ reference counting.
128 _mesa_reference_array_object(struct gl_context
*ctx
,
129 struct gl_array_object
**ptr
,
130 struct gl_array_object
*arrayObj
)
132 if (*ptr
== arrayObj
)
136 /* Unreference the old array object */
137 GLboolean deleteFlag
= GL_FALSE
;
138 struct gl_array_object
*oldObj
= *ptr
;
140 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
141 ASSERT(oldObj
->RefCount
> 0);
144 printf("ArrayObj %p %d DECR to %d\n",
145 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
147 deleteFlag
= (oldObj
->RefCount
== 0);
148 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
151 ASSERT(ctx
->Driver
.DeleteArrayObject
);
152 ctx
->Driver
.DeleteArrayObject(ctx
, oldObj
);
160 /* reference new array object */
161 _glthread_LOCK_MUTEX(arrayObj
->Mutex
);
162 if (arrayObj
->RefCount
== 0) {
163 /* this array's being deleted (look just above) */
164 /* Not sure this can every really happen. Warn if it does. */
165 _mesa_problem(NULL
, "referencing deleted array object");
169 arrayObj
->RefCount
++;
171 printf("ArrayObj %p %d INCR to %d\n",
172 (void *) arrayObj
, arrayObj
->Name
, arrayObj
->RefCount
);
176 _glthread_UNLOCK_MUTEX(arrayObj
->Mutex
);
183 init_array(struct gl_context
*ctx
,
184 struct gl_client_array
*array
, GLint size
, GLint type
)
188 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
192 array
->Enabled
= GL_FALSE
;
193 array
->Normalized
= GL_FALSE
;
194 array
->Integer
= GL_FALSE
;
195 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
196 /* Vertex array buffers */
197 _mesa_reference_buffer_object(ctx
, &array
->BufferObj
,
198 ctx
->Shared
->NullBufferObj
);
203 * Initialize a gl_array_object's arrays.
206 _mesa_initialize_array_object( struct gl_context
*ctx
,
207 struct gl_array_object
*obj
,
214 _glthread_INIT_MUTEX(obj
->Mutex
);
217 /* Init the individual arrays */
218 for (i
= 0; i
< Elements(obj
->VertexAttrib
); i
++) {
220 case VERT_ATTRIB_WEIGHT
:
221 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_WEIGHT
], 1, GL_FLOAT
);
223 case VERT_ATTRIB_NORMAL
:
224 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_NORMAL
], 3, GL_FLOAT
);
226 case VERT_ATTRIB_COLOR1
:
227 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_COLOR1
], 3, GL_FLOAT
);
229 case VERT_ATTRIB_FOG
:
230 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_FOG
], 1, GL_FLOAT
);
232 case VERT_ATTRIB_COLOR_INDEX
:
233 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
], 1, GL_FLOAT
);
235 case VERT_ATTRIB_EDGEFLAG
:
236 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
], 1, GL_BOOL
);
238 #if FEATURE_point_size_array
239 case VERT_ATTRIB_POINT_SIZE
:
240 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_POINT_SIZE
], 1, GL_FLOAT
);
244 init_array(ctx
, &obj
->VertexAttrib
[i
], 4, GL_FLOAT
);
249 _mesa_reference_buffer_object(ctx
, &obj
->ElementArrayBufferObj
,
250 ctx
->Shared
->NullBufferObj
);
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 * Helper for update_arrays().
284 * \return min(current min, array->_MaxElement).
287 update_min(GLuint min
, struct gl_client_array
*array
)
289 assert(array
->Enabled
);
290 _mesa_update_array_max_element(array
);
291 return MIN2(min
, array
->_MaxElement
);
296 * Examine vertex arrays to update the gl_array_object::_MaxElement field.
299 _mesa_update_array_object_max_element(struct gl_context
*ctx
,
300 struct gl_array_object
*arrayObj
)
302 GLbitfield64 enabled
= arrayObj
->_Enabled
;
306 GLint attrib
= _mesa_ffsll(enabled
) - 1;
307 enabled
&= ~BITFIELD64_BIT(attrib
);
308 min
= update_min(min
, &arrayObj
->VertexAttrib
[attrib
]);
311 /* _MaxElement is one past the last legal array element */
312 arrayObj
->_MaxElement
= min
;
316 /**********************************************************************/
318 /**********************************************************************/
322 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
323 * \param genRequired specifies behavour when id was not generated with
324 * glGenVertexArrays().
327 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, GLboolean genRequired
)
329 struct gl_array_object
* const oldObj
= ctx
->Array
.ArrayObj
;
330 struct gl_array_object
*newObj
= NULL
;
331 ASSERT_OUTSIDE_BEGIN_END(ctx
);
333 ASSERT(oldObj
!= NULL
);
335 if ( oldObj
->Name
== id
)
336 return; /* rebinding the same array object- no change */
339 * Get pointer to new array object (newObj)
342 /* The spec says there is no array object named 0, but we use
343 * one internally because it simplifies things.
345 newObj
= ctx
->Array
.DefaultArrayObj
;
348 /* non-default array object */
349 newObj
= lookup_arrayobj(ctx
, id
);
352 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindVertexArray(id)");
356 /* For APPLE version, generate a new array object now */
357 newObj
= (*ctx
->Driver
.NewArrayObject
)(ctx
, id
);
359 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
362 save_array_object(ctx
, newObj
);
366 ctx
->NewState
|= _NEW_ARRAY
;
367 ctx
->Array
.NewState
|= VERT_BIT_ALL
;
368 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, newObj
);
370 /* Pass BindVertexArray call to device driver */
371 if (ctx
->Driver
.BindArrayObject
&& newObj
)
372 ctx
->Driver
.BindArrayObject(ctx
, newObj
);
377 * ARB version of glBindVertexArray()
378 * This function behaves differently from glBindVertexArrayAPPLE() in
379 * that this function requires all ids to have been previously generated
380 * by glGenVertexArrays[APPLE]().
383 _mesa_BindVertexArray( GLuint id
)
385 GET_CURRENT_CONTEXT(ctx
);
386 bind_vertex_array(ctx
, id
, GL_TRUE
);
394 * The binding could be done more efficiently by comparing the non-NULL
395 * pointers in the old and new objects. The only arrays that are "dirty" are
396 * the ones that are non-NULL in either object.
399 _mesa_BindVertexArrayAPPLE( GLuint id
)
401 GET_CURRENT_CONTEXT(ctx
);
402 bind_vertex_array(ctx
, id
, GL_FALSE
);
407 * Delete a set of array objects.
409 * \param n Number of array objects to delete.
410 * \param ids Array of \c n array object IDs.
413 _mesa_DeleteVertexArraysAPPLE(GLsizei n
, const GLuint
*ids
)
415 GET_CURRENT_CONTEXT(ctx
);
417 ASSERT_OUTSIDE_BEGIN_END(ctx
);
420 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArrayAPPLE(n)");
424 for (i
= 0; i
< n
; i
++) {
425 struct gl_array_object
*obj
= lookup_arrayobj(ctx
, ids
[i
]);
428 ASSERT( obj
->Name
== ids
[i
] );
430 /* If the array object is currently bound, the spec says "the binding
431 * for that object reverts to zero and the default vertex array
434 if ( obj
== ctx
->Array
.ArrayObj
) {
435 CALL_BindVertexArrayAPPLE( ctx
->Exec
, (0) );
438 /* The ID is immediately freed for re-use */
439 remove_array_object(ctx
, obj
);
441 /* Unreference the array object.
442 * If refcount hits zero, the object will be deleted.
444 _mesa_reference_array_object(ctx
, &obj
, NULL
);
451 * Generate a set of unique array object IDs and store them in \c arrays.
452 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
453 * \param n Number of IDs to generate.
454 * \param arrays Array of \c n locations to store the IDs.
455 * \param vboOnly Will arrays have to reside in VBOs?
458 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
,
463 ASSERT_OUTSIDE_BEGIN_END(ctx
);
466 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenVertexArraysAPPLE");
474 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
476 /* Allocate new, empty array objects and return identifiers */
477 for (i
= 0; i
< n
; i
++) {
478 struct gl_array_object
*obj
;
479 GLuint name
= first
+ i
;
481 obj
= (*ctx
->Driver
.NewArrayObject
)( ctx
, name
);
483 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenVertexArraysAPPLE");
486 obj
->VBOonly
= vboOnly
;
487 save_array_object(ctx
, obj
);
488 arrays
[i
] = first
+ i
;
494 * ARB version of glGenVertexArrays()
495 * All arrays will be required to live in VBOs.
498 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
500 GET_CURRENT_CONTEXT(ctx
);
501 gen_vertex_arrays(ctx
, n
, arrays
, GL_TRUE
);
506 * APPLE version of glGenVertexArraysAPPLE()
507 * Arrays may live in VBOs or ordinary memory.
510 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
512 GET_CURRENT_CONTEXT(ctx
);
513 gen_vertex_arrays(ctx
, n
, arrays
, GL_FALSE
);
518 * Determine if ID is the name of an array object.
520 * \param id ID of the potential array object.
521 * \return \c GL_TRUE if \c id is the name of a array object,
522 * \c GL_FALSE otherwise.
525 _mesa_IsVertexArrayAPPLE( GLuint id
)
527 struct gl_array_object
* obj
;
528 GET_CURRENT_CONTEXT(ctx
);
529 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
534 obj
= lookup_arrayobj(ctx
, id
);
536 return (obj
!= NULL
) ? GL_TRUE
: GL_FALSE
;