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