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