Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / main / arrayobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.6
4 *
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.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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
25 * SOFTWARE.
26 */
27
28
29 /**
30 * \file arrayobj.c
31 * Functions for the GL_APPLE_vertex_array_object extension.
32 *
33 * \todo
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.
36 *
37 * \author Ian Romanick <idr@us.ibm.com>
38 * \author Brian Paul
39 */
40
41
42 #include "glheader.h"
43 #include "hash.h"
44 #include "imports.h"
45 #include "context.h"
46 #if FEATURE_ARB_vertex_buffer_object
47 #include "bufferobj.h"
48 #endif
49 #include "arrayobj.h"
50 #include "macros.h"
51 #include "glapi/dispatch.h"
52
53
54 /**
55 * Look up the array object for the given ID.
56 *
57 * \returns
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
60 * non-existent.
61 */
62
63 static INLINE struct gl_array_object *
64 lookup_arrayobj(GLcontext *ctx, GLuint id)
65 {
66 return (id == 0)
67 ? NULL
68 : (struct gl_array_object *) _mesa_HashLookup(ctx->Shared->ArrayObjects,
69 id);
70 }
71
72
73 /**
74 * For all the vertex arrays in the array object, unbind any pointers
75 * to any buffer objects (VBOs).
76 * This is done just prior to array object destruction.
77 */
78 static void
79 unbind_array_object_vbos(GLcontext *ctx, struct gl_array_object *obj)
80 {
81 GLuint i;
82
83 _mesa_reference_buffer_object(ctx, &obj->Vertex.BufferObj, NULL);
84 _mesa_reference_buffer_object(ctx, &obj->Weight.BufferObj, NULL);
85 _mesa_reference_buffer_object(ctx, &obj->Normal.BufferObj, NULL);
86 _mesa_reference_buffer_object(ctx, &obj->Color.BufferObj, NULL);
87 _mesa_reference_buffer_object(ctx, &obj->SecondaryColor.BufferObj, NULL);
88 _mesa_reference_buffer_object(ctx, &obj->FogCoord.BufferObj, NULL);
89 _mesa_reference_buffer_object(ctx, &obj->Index.BufferObj, NULL);
90 _mesa_reference_buffer_object(ctx, &obj->EdgeFlag.BufferObj, NULL);
91
92 for (i = 0; i < Elements(obj->TexCoord); i++)
93 _mesa_reference_buffer_object(ctx, &obj->TexCoord[i].BufferObj, NULL);
94
95 for (i = 0; i < Elements(obj->VertexAttrib); i++)
96 _mesa_reference_buffer_object(ctx, &obj->VertexAttrib[i].BufferObj,NULL);
97 }
98
99
100 /**
101 * Allocate and initialize a new vertex array object.
102 *
103 * This function is intended to be called via
104 * \c dd_function_table::NewArrayObject.
105 */
106 struct gl_array_object *
107 _mesa_new_array_object( GLcontext *ctx, GLuint name )
108 {
109 struct gl_array_object *obj = CALLOC_STRUCT(gl_array_object);
110 if (obj)
111 _mesa_initialize_array_object(ctx, obj, name);
112 return obj;
113 }
114
115
116 /**
117 * Delete an array object.
118 *
119 * This function is intended to be called via
120 * \c dd_function_table::DeleteArrayObject.
121 */
122 void
123 _mesa_delete_array_object( GLcontext *ctx, struct gl_array_object *obj )
124 {
125 (void) ctx;
126 unbind_array_object_vbos(ctx, obj);
127 _glthread_DESTROY_MUTEX(obj->Mutex);
128 _mesa_free(obj);
129 }
130
131
132 /**
133 * Set ptr to arrayObj w/ reference counting.
134 */
135 void
136 _mesa_reference_array_object(GLcontext *ctx,
137 struct gl_array_object **ptr,
138 struct gl_array_object *arrayObj)
139 {
140 if (*ptr == arrayObj)
141 return;
142
143 if (*ptr) {
144 /* Unreference the old array object */
145 GLboolean deleteFlag = GL_FALSE;
146 struct gl_array_object *oldObj = *ptr;
147
148 _glthread_LOCK_MUTEX(oldObj->Mutex);
149 ASSERT(oldObj->RefCount > 0);
150 oldObj->RefCount--;
151 #if 0
152 printf("ArrayObj %p %d DECR to %d\n",
153 (void *) oldObj, oldObj->Name, oldObj->RefCount);
154 #endif
155 deleteFlag = (oldObj->RefCount == 0);
156 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
157
158 if (deleteFlag) {
159 ASSERT(ctx->Driver.DeleteArrayObject);
160 ctx->Driver.DeleteArrayObject(ctx, oldObj);
161 }
162
163 *ptr = NULL;
164 }
165 ASSERT(!*ptr);
166
167 if (arrayObj) {
168 /* reference new array object */
169 _glthread_LOCK_MUTEX(arrayObj->Mutex);
170 if (arrayObj->RefCount == 0) {
171 /* this array's being deleted (look just above) */
172 /* Not sure this can every really happen. Warn if it does. */
173 _mesa_problem(NULL, "referencing deleted array object");
174 *ptr = NULL;
175 }
176 else {
177 arrayObj->RefCount++;
178 #if 0
179 printf("ArrayObj %p %d INCR to %d\n",
180 (void *) arrayObj, arrayObj->Name, arrayObj->RefCount);
181 #endif
182 *ptr = arrayObj;
183 }
184 _glthread_UNLOCK_MUTEX(arrayObj->Mutex);
185 }
186 }
187
188
189
190 static void
191 init_array(GLcontext *ctx,
192 struct gl_client_array *array, GLint size, GLint type)
193 {
194 array->Size = size;
195 array->Type = type;
196 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
197 array->Stride = 0;
198 array->StrideB = 0;
199 array->Ptr = NULL;
200 array->Enabled = GL_FALSE;
201 array->Normalized = GL_FALSE;
202 #if FEATURE_ARB_vertex_buffer_object
203 /* Vertex array buffers */
204 _mesa_reference_buffer_object(ctx, &array->BufferObj,
205 ctx->Shared->NullBufferObj);
206 #endif
207 }
208
209
210 /**
211 * Initialize a gl_array_object's arrays.
212 */
213 void
214 _mesa_initialize_array_object( GLcontext *ctx,
215 struct gl_array_object *obj,
216 GLuint name )
217 {
218 GLuint i;
219
220 obj->Name = name;
221
222 _glthread_INIT_MUTEX(obj->Mutex);
223 obj->RefCount = 1;
224
225 /* Init the individual arrays */
226 init_array(ctx, &obj->Vertex, 4, GL_FLOAT);
227 init_array(ctx, &obj->Weight, 1, GL_FLOAT);
228 init_array(ctx, &obj->Normal, 3, GL_FLOAT);
229 init_array(ctx, &obj->Color, 4, GL_FLOAT);
230 init_array(ctx, &obj->SecondaryColor, 4, GL_FLOAT);
231 init_array(ctx, &obj->FogCoord, 1, GL_FLOAT);
232 init_array(ctx, &obj->Index, 1, GL_FLOAT);
233 for (i = 0; i < Elements(obj->TexCoord); i++) {
234 init_array(ctx, &obj->TexCoord[i], 4, GL_FLOAT);
235 }
236 init_array(ctx, &obj->EdgeFlag, 1, GL_BOOL);
237 for (i = 0; i < Elements(obj->VertexAttrib); i++) {
238 init_array(ctx, &obj->VertexAttrib[i], 4, GL_FLOAT);
239 }
240
241 #if FEATURE_point_size_array
242 init_array(ctx, &obj->PointSize, 1, GL_FLOAT);
243 #endif
244 }
245
246
247 /**
248 * Add the given array object to the array object pool.
249 */
250 static void
251 save_array_object( GLcontext *ctx, struct gl_array_object *obj )
252 {
253 if (obj->Name > 0) {
254 /* insert into hash table */
255 _mesa_HashInsert(ctx->Shared->ArrayObjects, obj->Name, obj);
256 }
257 }
258
259
260 /**
261 * Remove the given array object from the array object pool.
262 * Do not deallocate the array object though.
263 */
264 static void
265 remove_array_object( GLcontext *ctx, struct gl_array_object *obj )
266 {
267 if (obj->Name > 0) {
268 /* remove from hash table */
269 _mesa_HashRemove(ctx->Shared->ArrayObjects, obj->Name);
270 }
271 }
272
273
274
275 /**
276 * Compute the index of the last array element that can be safely accessed
277 * in a vertex array. We can really only do this when the array lives in
278 * a VBO.
279 * The array->_MaxElement field will be updated.
280 * Later in glDrawArrays/Elements/etc we can do some bounds checking.
281 */
282 static void
283 compute_max_element(struct gl_client_array *array)
284 {
285 if (array->BufferObj->Name) {
286 /* Compute the max element we can access in the VBO without going
287 * out of bounds.
288 */
289 array->_MaxElement = ((GLsizeiptrARB) array->BufferObj->Size
290 - (GLsizeiptrARB) array->Ptr + array->StrideB
291 - array->_ElementSize) / array->StrideB;
292 if (0)
293 _mesa_printf("%s Object %u Size %u MaxElement %u\n",
294 __FUNCTION__,
295 array->BufferObj->Name,
296 (GLuint) array->BufferObj->Size,
297 array->_MaxElement);
298 }
299 else {
300 /* user-space array, no idea how big it is */
301 array->_MaxElement = 2 * 1000 * 1000 * 1000; /* just a big number */
302 }
303 }
304
305
306 /**
307 * Helper for update_arrays().
308 * \return min(current min, array->_MaxElement).
309 */
310 static GLuint
311 update_min(GLuint min, struct gl_client_array *array)
312 {
313 compute_max_element(array);
314 if (array->Enabled)
315 return MIN2(min, array->_MaxElement);
316 else
317 return min;
318 }
319
320
321 /**
322 * Examine vertex arrays to update the gl_array_object::_MaxElement field.
323 */
324 void
325 _mesa_update_array_object_max_element(GLcontext *ctx,
326 struct gl_array_object *arrayObj)
327 {
328 GLuint i, min = ~0;
329
330 min = update_min(min, &arrayObj->Vertex);
331 min = update_min(min, &arrayObj->Weight);
332 min = update_min(min, &arrayObj->Normal);
333 min = update_min(min, &arrayObj->Color);
334 min = update_min(min, &arrayObj->SecondaryColor);
335 min = update_min(min, &arrayObj->FogCoord);
336 min = update_min(min, &arrayObj->Index);
337 min = update_min(min, &arrayObj->EdgeFlag);
338 #if FEATURE_point_size_array
339 min = update_min(min, &arrayObj->PointSize);
340 #endif
341 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++)
342 min = update_min(min, &arrayObj->TexCoord[i]);
343 for (i = 0; i < Elements(arrayObj->VertexAttrib); i++)
344 min = update_min(min, &arrayObj->VertexAttrib[i]);
345
346 /* _MaxElement is one past the last legal array element */
347 arrayObj->_MaxElement = min;
348 }
349
350
351 /**********************************************************************/
352 /* API Functions */
353 /**********************************************************************/
354
355 /**
356 * Bind a new array.
357 *
358 * \todo
359 * The binding could be done more efficiently by comparing the non-NULL
360 * pointers in the old and new objects. The only arrays that are "dirty" are
361 * the ones that are non-NULL in either object.
362 */
363 void GLAPIENTRY
364 _mesa_BindVertexArrayAPPLE( GLuint id )
365 {
366 GET_CURRENT_CONTEXT(ctx);
367 struct gl_array_object * const oldObj = ctx->Array.ArrayObj;
368 struct gl_array_object *newObj = NULL;
369 ASSERT_OUTSIDE_BEGIN_END(ctx);
370
371 ASSERT(oldObj != NULL);
372
373 if ( oldObj->Name == id )
374 return; /* rebinding the same array object- no change */
375
376 /*
377 * Get pointer to new array object (newObj)
378 */
379 if (id == 0) {
380 /* The spec says there is no array object named 0, but we use
381 * one internally because it simplifies things.
382 */
383 newObj = ctx->Array.DefaultArrayObj;
384 }
385 else {
386 /* non-default array object */
387 newObj = lookup_arrayobj(ctx, id);
388 if (!newObj) {
389 /* If this is a new array object id, allocate an array object now.
390 */
391 newObj = (*ctx->Driver.NewArrayObject)(ctx, id);
392 if (!newObj) {
393 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
394 return;
395 }
396 save_array_object(ctx, newObj);
397 }
398 }
399
400 ctx->NewState |= _NEW_ARRAY;
401 ctx->Array.NewState |= _NEW_ARRAY_ALL;
402 _mesa_reference_array_object(ctx, &ctx->Array.ArrayObj, newObj);
403
404 /* Pass BindVertexArray call to device driver */
405 if (ctx->Driver.BindArrayObject && newObj)
406 (*ctx->Driver.BindArrayObject)( ctx, newObj );
407 }
408
409
410 /**
411 * Delete a set of array objects.
412 *
413 * \param n Number of array objects to delete.
414 * \param ids Array of \c n array object IDs.
415 */
416 void GLAPIENTRY
417 _mesa_DeleteVertexArraysAPPLE(GLsizei n, const GLuint *ids)
418 {
419 GET_CURRENT_CONTEXT(ctx);
420 GLsizei i;
421 ASSERT_OUTSIDE_BEGIN_END(ctx);
422
423 if (n < 0) {
424 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)");
425 return;
426 }
427
428 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
429
430 for (i = 0; i < n; i++) {
431 struct gl_array_object *obj = lookup_arrayobj(ctx, ids[i]);
432
433 if ( obj != NULL ) {
434 ASSERT( obj->Name == ids[i] );
435
436 /* If the array object is currently bound, the spec says "the binding
437 * for that object reverts to zero and the default vertex array
438 * becomes current."
439 */
440 if ( obj == ctx->Array.ArrayObj ) {
441 CALL_BindVertexArrayAPPLE( ctx->Exec, (0) );
442 }
443
444 /* The ID is immediately freed for re-use */
445 remove_array_object(ctx, obj);
446
447 /* Unreference the array object.
448 * If refcount hits zero, the object will be deleted.
449 */
450 _mesa_reference_array_object(ctx, &obj, NULL);
451 }
452 }
453
454 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
455 }
456
457
458 /**
459 * Generate a set of unique array object IDs and store them in \c arrays.
460 *
461 * \param n Number of IDs to generate.
462 * \param arrays Array of \c n locations to store the IDs.
463 */
464 void GLAPIENTRY
465 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
466 {
467 GET_CURRENT_CONTEXT(ctx);
468 GLuint first;
469 GLint i;
470 ASSERT_OUTSIDE_BEGIN_END(ctx);
471
472 if (n < 0) {
473 _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE");
474 return;
475 }
476
477 if (!arrays) {
478 return;
479 }
480
481 /*
482 * This must be atomic (generation and allocation of array object IDs)
483 */
484 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
485
486 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->ArrayObjects, n);
487
488 /* Allocate new, empty array objects and return identifiers */
489 for (i = 0; i < n; i++) {
490 struct gl_array_object *obj;
491 GLuint name = first + i;
492
493 obj = (*ctx->Driver.NewArrayObject)( ctx, name );
494 if (!obj) {
495 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
496 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE");
497 return;
498 }
499 save_array_object(ctx, obj);
500 arrays[i] = first + i;
501 }
502
503 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
504 }
505
506
507 /**
508 * Determine if ID is the name of an array object.
509 *
510 * \param id ID of the potential array object.
511 * \return \c GL_TRUE if \c id is the name of a array object,
512 * \c GL_FALSE otherwise.
513 */
514 GLboolean GLAPIENTRY
515 _mesa_IsVertexArrayAPPLE( GLuint id )
516 {
517 struct gl_array_object * obj;
518 GET_CURRENT_CONTEXT(ctx);
519 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
520
521 if (id == 0)
522 return GL_FALSE;
523
524 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
525 obj = lookup_arrayobj(ctx, id);
526 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
527
528 return (obj != NULL) ? GL_TRUE : GL_FALSE;
529 }