mesa: Rename _mesa_lookup_arrayobj to _mesa_lookup_vao.
[mesa.git] / src / mesa / main / arrayobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * (C) Copyright IBM Corporation 2006
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /**
29 * \file arrayobj.c
30 * Functions for the GL_APPLE_vertex_array_object extension.
31 *
32 * \todo
33 * The code in this file borrows a lot from bufferobj.c. There's a certain
34 * amount of cruft left over from that origin that may be unnecessary.
35 *
36 * \author Ian Romanick <idr@us.ibm.com>
37 * \author Brian Paul
38 */
39
40
41 #include "glheader.h"
42 #include "hash.h"
43 #include "image.h"
44 #include "imports.h"
45 #include "context.h"
46 #include "bufferobj.h"
47 #include "arrayobj.h"
48 #include "macros.h"
49 #include "mtypes.h"
50 #include "varray.h"
51 #include "main/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 struct gl_vertex_array_object *
64 _mesa_lookup_vao(struct gl_context *ctx, GLuint id)
65 {
66 if (id == 0)
67 return NULL;
68 else
69 return (struct gl_vertex_array_object *)
70 _mesa_HashLookup(ctx->Array.Objects, id);
71 }
72
73
74 /**
75 * For all the vertex binding points 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(struct gl_context *ctx, struct gl_vertex_array_object *obj)
81 {
82 GLuint i;
83
84 for (i = 0; i < Elements(obj->VertexBinding); i++)
85 _mesa_reference_buffer_object(ctx, &obj->VertexBinding[i].BufferObj, NULL);
86
87 for (i = 0; i < Elements(obj->_VertexAttrib); i++)
88 _mesa_reference_buffer_object(ctx, &obj->_VertexAttrib[i].BufferObj, NULL);
89 }
90
91
92 /**
93 * Allocate and initialize a new vertex array object.
94 *
95 * This function is intended to be called via
96 * \c dd_function_table::NewArrayObject.
97 */
98 struct gl_vertex_array_object *
99 _mesa_new_vao(struct gl_context *ctx, GLuint name)
100 {
101 struct gl_vertex_array_object *obj = CALLOC_STRUCT(gl_vertex_array_object);
102 if (obj)
103 _mesa_initialize_vao(ctx, obj, name);
104 return obj;
105 }
106
107
108 /**
109 * Delete an array object.
110 *
111 * This function is intended to be called via
112 * \c dd_function_table::DeleteArrayObject.
113 */
114 void
115 _mesa_delete_vao(struct gl_context *ctx, struct gl_vertex_array_object *obj)
116 {
117 (void) ctx;
118 unbind_array_object_vbos(ctx, obj);
119 _mesa_reference_buffer_object(ctx, &obj->ElementArrayBufferObj, NULL);
120 _glthread_DESTROY_MUTEX(obj->Mutex);
121 free(obj->Label);
122 free(obj);
123 }
124
125
126 /**
127 * Set ptr to vao w/ reference counting.
128 * Note: this should only be called from the _mesa_reference_vao()
129 * inline function.
130 */
131 void
132 _mesa_reference_vao_(struct gl_context *ctx,
133 struct gl_vertex_array_object **ptr,
134 struct gl_vertex_array_object *vao)
135 {
136 assert(*ptr != vao);
137
138 if (*ptr) {
139 /* Unreference the old array object */
140 GLboolean deleteFlag = GL_FALSE;
141 struct gl_vertex_array_object *oldObj = *ptr;
142
143 _glthread_LOCK_MUTEX(oldObj->Mutex);
144 ASSERT(oldObj->RefCount > 0);
145 oldObj->RefCount--;
146 #if 0
147 printf("ArrayObj %p %d DECR to %d\n",
148 (void *) oldObj, oldObj->Name, oldObj->RefCount);
149 #endif
150 deleteFlag = (oldObj->RefCount == 0);
151 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
152
153 if (deleteFlag) {
154 ASSERT(ctx->Driver.DeleteArrayObject);
155 ctx->Driver.DeleteArrayObject(ctx, oldObj);
156 }
157
158 *ptr = NULL;
159 }
160 ASSERT(!*ptr);
161
162 if (vao) {
163 /* reference new array object */
164 _glthread_LOCK_MUTEX(vao->Mutex);
165 if (vao->RefCount == 0) {
166 /* this array's being deleted (look just above) */
167 /* Not sure this can every really happen. Warn if it does. */
168 _mesa_problem(NULL, "referencing deleted array object");
169 *ptr = NULL;
170 }
171 else {
172 vao->RefCount++;
173 #if 0
174 printf("ArrayObj %p %d INCR to %d\n",
175 (void *) vao, vao->Name, vao->RefCount);
176 #endif
177 *ptr = vao;
178 }
179 _glthread_UNLOCK_MUTEX(vao->Mutex);
180 }
181 }
182
183
184
185 static void
186 init_array(struct gl_context *ctx,
187 struct gl_vertex_array_object *obj, GLuint index, GLint size, GLint type)
188 {
189 struct gl_vertex_attrib_array *array = &obj->VertexAttrib[index];
190 struct gl_vertex_buffer_binding *binding = &obj->VertexBinding[index];
191
192 array->Size = size;
193 array->Type = type;
194 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
195 array->Stride = 0;
196 array->Ptr = NULL;
197 array->RelativeOffset = 0;
198 array->Enabled = GL_FALSE;
199 array->Normalized = GL_FALSE;
200 array->Integer = GL_FALSE;
201 array->_ElementSize = size * _mesa_sizeof_type(type);
202 array->VertexBinding = index;
203
204 binding->Offset = 0;
205 binding->Stride = array->_ElementSize;
206 binding->BufferObj = NULL;
207 binding->_BoundArrays = BITFIELD64_BIT(index);
208
209 /* Vertex array buffers */
210 _mesa_reference_buffer_object(ctx, &binding->BufferObj,
211 ctx->Shared->NullBufferObj);
212 }
213
214
215 /**
216 * Initialize a gl_vertex_array_object's arrays.
217 */
218 void
219 _mesa_initialize_vao(struct gl_context *ctx,
220 struct gl_vertex_array_object *obj,
221 GLuint name)
222 {
223 GLuint i;
224
225 obj->Name = name;
226
227 _glthread_INIT_MUTEX(obj->Mutex);
228 obj->RefCount = 1;
229
230 /* Init the individual arrays */
231 for (i = 0; i < Elements(obj->_VertexAttrib); i++) {
232 switch (i) {
233 case VERT_ATTRIB_WEIGHT:
234 init_array(ctx, obj, VERT_ATTRIB_WEIGHT, 1, GL_FLOAT);
235 break;
236 case VERT_ATTRIB_NORMAL:
237 init_array(ctx, obj, VERT_ATTRIB_NORMAL, 3, GL_FLOAT);
238 break;
239 case VERT_ATTRIB_COLOR1:
240 init_array(ctx, obj, VERT_ATTRIB_COLOR1, 3, GL_FLOAT);
241 break;
242 case VERT_ATTRIB_FOG:
243 init_array(ctx, obj, VERT_ATTRIB_FOG, 1, GL_FLOAT);
244 break;
245 case VERT_ATTRIB_COLOR_INDEX:
246 init_array(ctx, obj, VERT_ATTRIB_COLOR_INDEX, 1, GL_FLOAT);
247 break;
248 case VERT_ATTRIB_EDGEFLAG:
249 init_array(ctx, obj, VERT_ATTRIB_EDGEFLAG, 1, GL_BOOL);
250 break;
251 case VERT_ATTRIB_POINT_SIZE:
252 init_array(ctx, obj, VERT_ATTRIB_POINT_SIZE, 1, GL_FLOAT);
253 break;
254 default:
255 init_array(ctx, obj, i, 4, GL_FLOAT);
256 break;
257 }
258 }
259
260 _mesa_reference_buffer_object(ctx, &obj->ElementArrayBufferObj,
261 ctx->Shared->NullBufferObj);
262 }
263
264
265 /**
266 * Add the given array object to the array object pool.
267 */
268 static void
269 save_array_object( struct gl_context *ctx, struct gl_vertex_array_object *obj )
270 {
271 if (obj->Name > 0) {
272 /* insert into hash table */
273 _mesa_HashInsert(ctx->Array.Objects, obj->Name, obj);
274 }
275 }
276
277
278 /**
279 * Remove the given array object from the array object pool.
280 * Do not deallocate the array object though.
281 */
282 static void
283 remove_array_object( struct gl_context *ctx, struct gl_vertex_array_object *obj )
284 {
285 if (obj->Name > 0) {
286 /* remove from hash table */
287 _mesa_HashRemove(ctx->Array.Objects, obj->Name);
288 }
289 }
290
291
292
293 /**
294 * Helper for _mesa_update_vao_max_element().
295 * \return min(vao->_VertexAttrib[*]._MaxElement).
296 */
297 static GLuint
298 compute_max_element(struct gl_vertex_array_object *vao, GLbitfield64 enabled)
299 {
300 GLuint min = ~((GLuint)0);
301
302 while (enabled) {
303 struct gl_client_array *client_array;
304 GLint attrib = ffsll(enabled) - 1;
305 enabled ^= BITFIELD64_BIT(attrib);
306
307 client_array = &vao->_VertexAttrib[attrib];
308 assert(client_array->Enabled);
309 _mesa_update_array_max_element(client_array);
310 min = MIN2(min, client_array->_MaxElement);
311 }
312
313 return min;
314 }
315
316
317 /**
318 * Examine vertex arrays to update the gl_vertex_array_object::_MaxElement field.
319 */
320 void
321 _mesa_update_vao_max_element(struct gl_context *ctx,
322 struct gl_vertex_array_object *vao)
323 {
324 GLbitfield64 enabled;
325
326 if (!ctx->VertexProgram._Current ||
327 ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram) {
328 enabled = _mesa_array_object_get_enabled_ff(vao);
329 } else {
330 enabled = _mesa_array_object_get_enabled_arb(vao);
331 }
332
333 /* _MaxElement is one past the last legal array element */
334 vao->_MaxElement = compute_max_element(vao, enabled);
335 }
336
337
338 /**
339 * Updates the derived gl_client_arrays when a gl_vertex_attrib_array
340 * or a gl_vertex_buffer_binding has changed.
341 */
342 void
343 _mesa_update_vao_client_arrays(struct gl_context *ctx,
344 struct gl_vertex_array_object *vao)
345 {
346 GLbitfield64 arrays = vao->NewArrays;
347
348 while (arrays) {
349 struct gl_client_array *client_array;
350 struct gl_vertex_attrib_array *attrib_array;
351 struct gl_vertex_buffer_binding *buffer_binding;
352
353 GLint attrib = ffsll(arrays) - 1;
354 arrays ^= BITFIELD64_BIT(attrib);
355
356 attrib_array = &vao->VertexAttrib[attrib];
357 buffer_binding = &vao->VertexBinding[attrib_array->VertexBinding];
358 client_array = &vao->_VertexAttrib[attrib];
359
360 _mesa_update_client_array(ctx, client_array, attrib_array,
361 buffer_binding);
362 }
363 }
364
365
366 /**********************************************************************/
367 /* API Functions */
368 /**********************************************************************/
369
370
371 /**
372 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
373 * \param genRequired specifies behavour when id was not generated with
374 * glGenVertexArrays().
375 */
376 static void
377 bind_vertex_array(struct gl_context *ctx, GLuint id, GLboolean genRequired)
378 {
379 struct gl_vertex_array_object * const oldObj = ctx->Array.VAO;
380 struct gl_vertex_array_object *newObj = NULL;
381
382 ASSERT(oldObj != NULL);
383
384 if ( oldObj->Name == id )
385 return; /* rebinding the same array object- no change */
386
387 /*
388 * Get pointer to new array object (newObj)
389 */
390 if (id == 0) {
391 /* The spec says there is no array object named 0, but we use
392 * one internally because it simplifies things.
393 */
394 newObj = ctx->Array.DefaultVAO;
395 }
396 else {
397 /* non-default array object */
398 newObj = _mesa_lookup_vao(ctx, id);
399 if (!newObj) {
400 if (genRequired) {
401 _mesa_error(ctx, GL_INVALID_OPERATION,
402 "glBindVertexArray(non-gen name)");
403 return;
404 }
405
406 /* For APPLE version, generate a new array object now */
407 newObj = (*ctx->Driver.NewArrayObject)(ctx, id);
408 if (!newObj) {
409 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
410 return;
411 }
412
413 save_array_object(ctx, newObj);
414 }
415
416 if (!newObj->EverBound) {
417 /* The "Interactions with APPLE_vertex_array_object" section of the
418 * GL_ARB_vertex_array_object spec says:
419 *
420 * "The first bind call, either BindVertexArray or
421 * BindVertexArrayAPPLE, determines the semantic of the object."
422 */
423 newObj->ARBsemantics = genRequired;
424 newObj->EverBound = GL_TRUE;
425 }
426 }
427
428 ctx->NewState |= _NEW_ARRAY;
429 _mesa_reference_vao(ctx, &ctx->Array.VAO, newObj);
430
431 /* Pass BindVertexArray call to device driver */
432 if (ctx->Driver.BindArrayObject && newObj)
433 ctx->Driver.BindArrayObject(ctx, newObj);
434 }
435
436
437 /**
438 * ARB version of glBindVertexArray()
439 * This function behaves differently from glBindVertexArrayAPPLE() in
440 * that this function requires all ids to have been previously generated
441 * by glGenVertexArrays[APPLE]().
442 */
443 void GLAPIENTRY
444 _mesa_BindVertexArray( GLuint id )
445 {
446 GET_CURRENT_CONTEXT(ctx);
447 bind_vertex_array(ctx, id, GL_TRUE);
448 }
449
450
451 /**
452 * Bind a new array.
453 *
454 * \todo
455 * The binding could be done more efficiently by comparing the non-NULL
456 * pointers in the old and new objects. The only arrays that are "dirty" are
457 * the ones that are non-NULL in either object.
458 */
459 void GLAPIENTRY
460 _mesa_BindVertexArrayAPPLE( GLuint id )
461 {
462 GET_CURRENT_CONTEXT(ctx);
463 bind_vertex_array(ctx, id, GL_FALSE);
464 }
465
466
467 /**
468 * Delete a set of array objects.
469 *
470 * \param n Number of array objects to delete.
471 * \param ids Array of \c n array object IDs.
472 */
473 void GLAPIENTRY
474 _mesa_DeleteVertexArrays(GLsizei n, const GLuint *ids)
475 {
476 GET_CURRENT_CONTEXT(ctx);
477 GLsizei i;
478
479 if (n < 0) {
480 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)");
481 return;
482 }
483
484 for (i = 0; i < n; i++) {
485 struct gl_vertex_array_object *obj = _mesa_lookup_vao(ctx, ids[i]);
486
487 if ( obj != NULL ) {
488 ASSERT( obj->Name == ids[i] );
489
490 /* If the array object is currently bound, the spec says "the binding
491 * for that object reverts to zero and the default vertex array
492 * becomes current."
493 */
494 if ( obj == ctx->Array.VAO ) {
495 _mesa_BindVertexArray(0);
496 }
497
498 /* The ID is immediately freed for re-use */
499 remove_array_object(ctx, obj);
500
501 /* Unreference the array object.
502 * If refcount hits zero, the object will be deleted.
503 */
504 _mesa_reference_vao(ctx, &obj, NULL);
505 }
506 }
507 }
508
509
510 /**
511 * Generate a set of unique array object IDs and store them in \c arrays.
512 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
513 * \param n Number of IDs to generate.
514 * \param arrays Array of \c n locations to store the IDs.
515 * \param vboOnly Will arrays have to reside in VBOs?
516 */
517 static void
518 gen_vertex_arrays(struct gl_context *ctx, GLsizei n, GLuint *arrays)
519 {
520 GLuint first;
521 GLint i;
522
523 if (n < 0) {
524 _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE");
525 return;
526 }
527
528 if (!arrays) {
529 return;
530 }
531
532 first = _mesa_HashFindFreeKeyBlock(ctx->Array.Objects, n);
533
534 /* Allocate new, empty array objects and return identifiers */
535 for (i = 0; i < n; i++) {
536 struct gl_vertex_array_object *obj;
537 GLuint name = first + i;
538
539 obj = (*ctx->Driver.NewArrayObject)( ctx, name );
540 if (!obj) {
541 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE");
542 return;
543 }
544 save_array_object(ctx, obj);
545 arrays[i] = first + i;
546 }
547 }
548
549
550 /**
551 * ARB version of glGenVertexArrays()
552 * All arrays will be required to live in VBOs.
553 */
554 void GLAPIENTRY
555 _mesa_GenVertexArrays(GLsizei n, GLuint *arrays)
556 {
557 GET_CURRENT_CONTEXT(ctx);
558 gen_vertex_arrays(ctx, n, arrays);
559 }
560
561
562 /**
563 * APPLE version of glGenVertexArraysAPPLE()
564 * Arrays may live in VBOs or ordinary memory.
565 */
566 void GLAPIENTRY
567 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
568 {
569 GET_CURRENT_CONTEXT(ctx);
570 gen_vertex_arrays(ctx, n, arrays);
571 }
572
573
574 /**
575 * Determine if ID is the name of an array object.
576 *
577 * \param id ID of the potential array object.
578 * \return \c GL_TRUE if \c id is the name of a array object,
579 * \c GL_FALSE otherwise.
580 */
581 GLboolean GLAPIENTRY
582 _mesa_IsVertexArray( GLuint id )
583 {
584 struct gl_vertex_array_object * obj;
585 GET_CURRENT_CONTEXT(ctx);
586 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
587
588 if (id == 0)
589 return GL_FALSE;
590
591 obj = _mesa_lookup_vao(ctx, id);
592 if (obj == NULL)
593 return GL_FALSE;
594
595 return obj->EverBound;
596 }