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