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