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.
126 * Note: this should only be called from the _mesa_reference_array_object()
130 _mesa_reference_array_object_(struct gl_context
*ctx
,
131 struct gl_array_object
**ptr
,
132 struct gl_array_object
*arrayObj
)
134 assert(*ptr
!= arrayObj
);
137 /* Unreference the old array object */
138 GLboolean deleteFlag
= GL_FALSE
;
139 struct gl_array_object
*oldObj
= *ptr
;
141 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
142 ASSERT(oldObj
->RefCount
> 0);
145 printf("ArrayObj %p %d DECR to %d\n",
146 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
148 deleteFlag
= (oldObj
->RefCount
== 0);
149 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
152 ASSERT(ctx
->Driver
.DeleteArrayObject
);
153 ctx
->Driver
.DeleteArrayObject(ctx
, oldObj
);
161 /* reference new array object */
162 _glthread_LOCK_MUTEX(arrayObj
->Mutex
);
163 if (arrayObj
->RefCount
== 0) {
164 /* this array's being deleted (look just above) */
165 /* Not sure this can every really happen. Warn if it does. */
166 _mesa_problem(NULL
, "referencing deleted array object");
170 arrayObj
->RefCount
++;
172 printf("ArrayObj %p %d INCR to %d\n",
173 (void *) arrayObj
, arrayObj
->Name
, arrayObj
->RefCount
);
177 _glthread_UNLOCK_MUTEX(arrayObj
->Mutex
);
184 init_array(struct gl_context
*ctx
,
185 struct gl_client_array
*array
, GLint size
, GLint type
)
189 array
->Format
= GL_RGBA
; /* only significant for GL_EXT_vertex_array_bgra */
193 array
->Enabled
= GL_FALSE
;
194 array
->Normalized
= GL_FALSE
;
195 array
->Integer
= GL_FALSE
;
196 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
197 /* Vertex array buffers */
198 _mesa_reference_buffer_object(ctx
, &array
->BufferObj
,
199 ctx
->Shared
->NullBufferObj
);
204 * Initialize a gl_array_object's arrays.
207 _mesa_initialize_array_object( struct gl_context
*ctx
,
208 struct gl_array_object
*obj
,
215 _glthread_INIT_MUTEX(obj
->Mutex
);
218 /* Init the individual arrays */
219 for (i
= 0; i
< Elements(obj
->VertexAttrib
); i
++) {
221 case VERT_ATTRIB_WEIGHT
:
222 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_WEIGHT
], 1, GL_FLOAT
);
224 case VERT_ATTRIB_NORMAL
:
225 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_NORMAL
], 3, GL_FLOAT
);
227 case VERT_ATTRIB_COLOR1
:
228 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_COLOR1
], 3, GL_FLOAT
);
230 case VERT_ATTRIB_FOG
:
231 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_FOG
], 1, GL_FLOAT
);
233 case VERT_ATTRIB_COLOR_INDEX
:
234 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
], 1, GL_FLOAT
);
236 case VERT_ATTRIB_EDGEFLAG
:
237 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
], 1, GL_BOOL
);
239 #if FEATURE_point_size_array
240 case VERT_ATTRIB_POINT_SIZE
:
241 init_array(ctx
, &obj
->VertexAttrib
[VERT_ATTRIB_POINT_SIZE
], 1, GL_FLOAT
);
245 init_array(ctx
, &obj
->VertexAttrib
[i
], 4, GL_FLOAT
);
250 _mesa_reference_buffer_object(ctx
, &obj
->ElementArrayBufferObj
,
251 ctx
->Shared
->NullBufferObj
);
256 * Add the given array object to the array object pool.
259 save_array_object( struct gl_context
*ctx
, struct gl_array_object
*obj
)
262 /* insert into hash table */
263 _mesa_HashInsert(ctx
->Array
.Objects
, obj
->Name
, obj
);
269 * Remove the given array object from the array object pool.
270 * Do not deallocate the array object though.
273 remove_array_object( struct gl_context
*ctx
, struct gl_array_object
*obj
)
276 /* remove from hash table */
277 _mesa_HashRemove(ctx
->Array
.Objects
, obj
->Name
);
284 * Helper for _mesa_update_array_object_max_element().
285 * \return min(arrayObj->VertexAttrib[*]._MaxElement).
288 compute_max_element(struct gl_array_object
*arrayObj
, GLbitfield64 enabled
)
290 GLuint min
= ~((GLuint
)0);
293 struct gl_client_array
*client_array
;
294 GLint attrib
= ffsll(enabled
) - 1;
295 enabled
^= BITFIELD64_BIT(attrib
);
297 client_array
= &arrayObj
->VertexAttrib
[attrib
];
298 assert(client_array
->Enabled
);
299 _mesa_update_array_max_element(client_array
);
300 min
= MIN2(min
, client_array
->_MaxElement
);
308 * Examine vertex arrays to update the gl_array_object::_MaxElement field.
311 _mesa_update_array_object_max_element(struct gl_context
*ctx
,
312 struct gl_array_object
*arrayObj
)
314 GLbitfield64 enabled
;
316 if (!ctx
->VertexProgram
._Current
||
317 ctx
->VertexProgram
._Current
== ctx
->VertexProgram
._TnlProgram
) {
318 enabled
= _mesa_array_object_get_enabled_ff(arrayObj
);
319 } else if (ctx
->VertexProgram
._Current
->IsNVProgram
) {
320 enabled
= _mesa_array_object_get_enabled_nv(arrayObj
);
322 enabled
= _mesa_array_object_get_enabled_arb(arrayObj
);
325 /* _MaxElement is one past the last legal array element */
326 arrayObj
->_MaxElement
= compute_max_element(arrayObj
, enabled
);
330 /**********************************************************************/
332 /**********************************************************************/
336 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
337 * \param genRequired specifies behavour when id was not generated with
338 * glGenVertexArrays().
341 bind_vertex_array(struct gl_context
*ctx
, GLuint id
, GLboolean genRequired
)
343 struct gl_array_object
* const oldObj
= ctx
->Array
.ArrayObj
;
344 struct gl_array_object
*newObj
= NULL
;
345 ASSERT_OUTSIDE_BEGIN_END(ctx
);
347 ASSERT(oldObj
!= NULL
);
349 if ( oldObj
->Name
== id
)
350 return; /* rebinding the same array object- no change */
353 * Get pointer to new array object (newObj)
356 /* The spec says there is no array object named 0, but we use
357 * one internally because it simplifies things.
359 newObj
= ctx
->Array
.DefaultArrayObj
;
362 /* non-default array object */
363 newObj
= lookup_arrayobj(ctx
, id
);
366 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindVertexArray(id)");
370 /* For APPLE version, generate a new array object now */
371 newObj
= (*ctx
->Driver
.NewArrayObject
)(ctx
, id
);
373 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
377 save_array_object(ctx
, newObj
);
380 if (!newObj
->_Used
) {
381 /* The "Interactions with APPLE_vertex_array_object" section of the
382 * GL_ARB_vertex_array_object spec says:
384 * "The first bind call, either BindVertexArray or
385 * BindVertexArrayAPPLE, determines the semantic of the object."
387 newObj
->ARBsemantics
= genRequired
;
388 newObj
->_Used
= GL_TRUE
;
392 ctx
->NewState
|= _NEW_ARRAY
;
393 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
, newObj
);
395 /* Pass BindVertexArray call to device driver */
396 if (ctx
->Driver
.BindArrayObject
&& newObj
)
397 ctx
->Driver
.BindArrayObject(ctx
, newObj
);
402 * ARB version of glBindVertexArray()
403 * This function behaves differently from glBindVertexArrayAPPLE() in
404 * that this function requires all ids to have been previously generated
405 * by glGenVertexArrays[APPLE]().
408 _mesa_BindVertexArray( GLuint id
)
410 GET_CURRENT_CONTEXT(ctx
);
411 bind_vertex_array(ctx
, id
, GL_TRUE
);
419 * The binding could be done more efficiently by comparing the non-NULL
420 * pointers in the old and new objects. The only arrays that are "dirty" are
421 * the ones that are non-NULL in either object.
424 _mesa_BindVertexArrayAPPLE( GLuint id
)
426 GET_CURRENT_CONTEXT(ctx
);
427 bind_vertex_array(ctx
, id
, GL_FALSE
);
432 * Delete a set of array objects.
434 * \param n Number of array objects to delete.
435 * \param ids Array of \c n array object IDs.
438 _mesa_DeleteVertexArraysAPPLE(GLsizei n
, const GLuint
*ids
)
440 GET_CURRENT_CONTEXT(ctx
);
442 ASSERT_OUTSIDE_BEGIN_END(ctx
);
445 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArrayAPPLE(n)");
449 for (i
= 0; i
< n
; i
++) {
450 struct gl_array_object
*obj
= lookup_arrayobj(ctx
, ids
[i
]);
453 ASSERT( obj
->Name
== ids
[i
] );
455 /* If the array object is currently bound, the spec says "the binding
456 * for that object reverts to zero and the default vertex array
459 if ( obj
== ctx
->Array
.ArrayObj
) {
460 CALL_BindVertexArrayAPPLE( ctx
->Exec
, (0) );
463 /* The ID is immediately freed for re-use */
464 remove_array_object(ctx
, obj
);
466 /* Unreference the array object.
467 * If refcount hits zero, the object will be deleted.
469 _mesa_reference_array_object(ctx
, &obj
, NULL
);
476 * Generate a set of unique array object IDs and store them in \c arrays.
477 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
478 * \param n Number of IDs to generate.
479 * \param arrays Array of \c n locations to store the IDs.
480 * \param vboOnly Will arrays have to reside in VBOs?
483 gen_vertex_arrays(struct gl_context
*ctx
, GLsizei n
, GLuint
*arrays
)
487 ASSERT_OUTSIDE_BEGIN_END(ctx
);
490 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenVertexArraysAPPLE");
498 first
= _mesa_HashFindFreeKeyBlock(ctx
->Array
.Objects
, n
);
500 /* Allocate new, empty array objects and return identifiers */
501 for (i
= 0; i
< n
; i
++) {
502 struct gl_array_object
*obj
;
503 GLuint name
= first
+ i
;
505 obj
= (*ctx
->Driver
.NewArrayObject
)( ctx
, name
);
507 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenVertexArraysAPPLE");
510 save_array_object(ctx
, obj
);
511 arrays
[i
] = first
+ i
;
517 * ARB version of glGenVertexArrays()
518 * All arrays will be required to live in VBOs.
521 _mesa_GenVertexArrays(GLsizei n
, GLuint
*arrays
)
523 GET_CURRENT_CONTEXT(ctx
);
524 gen_vertex_arrays(ctx
, n
, arrays
);
529 * APPLE version of glGenVertexArraysAPPLE()
530 * Arrays may live in VBOs or ordinary memory.
533 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
535 GET_CURRENT_CONTEXT(ctx
);
536 gen_vertex_arrays(ctx
, n
, arrays
);
541 * Determine if ID is the name of an array object.
543 * \param id ID of the potential array object.
544 * \return \c GL_TRUE if \c id is the name of a array object,
545 * \c GL_FALSE otherwise.
548 _mesa_IsVertexArrayAPPLE( GLuint id
)
550 struct gl_array_object
* obj
;
551 GET_CURRENT_CONTEXT(ctx
);
552 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
557 obj
= lookup_arrayobj(ctx
, id
);
559 return (obj
!= NULL
) ? GL_TRUE
: GL_FALSE
;