mesa: Add "OR COPYRIGHT HOLDERS" to license text disclaiming liability.
[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 * THE AUTHORS OR COPYRIGHT HOLDERS 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 "bufferobj.h"
48 #include "arrayobj.h"
49 #include "macros.h"
50 #include "mtypes.h"
51 #include "varray.h"
52 #include "main/dispatch.h"
53
54
55 /**
56 * Look up the array object for the given ID.
57 *
58 * \returns
59 * Either a pointer to the array object with the specified ID or \c NULL for
60 * a non-existent ID. The spec defines ID 0 as being technically
61 * non-existent.
62 */
63
64 static inline struct gl_array_object *
65 lookup_arrayobj(struct gl_context *ctx, GLuint id)
66 {
67 if (id == 0)
68 return NULL;
69 else
70 return (struct gl_array_object *)
71 _mesa_HashLookup(ctx->Array.Objects, id);
72 }
73
74
75 /**
76 * For all the vertex arrays in the array object, unbind any pointers
77 * to any buffer objects (VBOs).
78 * This is done just prior to array object destruction.
79 */
80 static void
81 unbind_array_object_vbos(struct gl_context *ctx, struct gl_array_object *obj)
82 {
83 GLuint i;
84
85 for (i = 0; i < Elements(obj->VertexAttrib); i++)
86 _mesa_reference_buffer_object(ctx, &obj->VertexAttrib[i].BufferObj, NULL);
87 }
88
89
90 /**
91 * Allocate and initialize a new vertex array object.
92 *
93 * This function is intended to be called via
94 * \c dd_function_table::NewArrayObject.
95 */
96 struct gl_array_object *
97 _mesa_new_array_object( struct gl_context *ctx, GLuint name )
98 {
99 struct gl_array_object *obj = CALLOC_STRUCT(gl_array_object);
100 if (obj)
101 _mesa_initialize_array_object(ctx, obj, name);
102 return obj;
103 }
104
105
106 /**
107 * Delete an array object.
108 *
109 * This function is intended to be called via
110 * \c dd_function_table::DeleteArrayObject.
111 */
112 void
113 _mesa_delete_array_object( struct gl_context *ctx, struct gl_array_object *obj )
114 {
115 (void) ctx;
116 unbind_array_object_vbos(ctx, obj);
117 _mesa_reference_buffer_object(ctx, &obj->ElementArrayBufferObj, NULL);
118 _glthread_DESTROY_MUTEX(obj->Mutex);
119 free(obj);
120 }
121
122
123 /**
124 * Set ptr to arrayObj w/ reference counting.
125 * Note: this should only be called from the _mesa_reference_array_object()
126 * inline function.
127 */
128 void
129 _mesa_reference_array_object_(struct gl_context *ctx,
130 struct gl_array_object **ptr,
131 struct gl_array_object *arrayObj)
132 {
133 assert(*ptr != arrayObj);
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 case VERT_ATTRIB_POINT_SIZE:
239 init_array(ctx, &obj->VertexAttrib[VERT_ATTRIB_POINT_SIZE], 1, GL_FLOAT);
240 break;
241 default:
242 init_array(ctx, &obj->VertexAttrib[i], 4, GL_FLOAT);
243 break;
244 }
245 }
246
247 _mesa_reference_buffer_object(ctx, &obj->ElementArrayBufferObj,
248 ctx->Shared->NullBufferObj);
249 }
250
251
252 /**
253 * Add the given array object to the array object pool.
254 */
255 static void
256 save_array_object( struct gl_context *ctx, struct gl_array_object *obj )
257 {
258 if (obj->Name > 0) {
259 /* insert into hash table */
260 _mesa_HashInsert(ctx->Array.Objects, obj->Name, obj);
261 }
262 }
263
264
265 /**
266 * Remove the given array object from the array object pool.
267 * Do not deallocate the array object though.
268 */
269 static void
270 remove_array_object( struct gl_context *ctx, struct gl_array_object *obj )
271 {
272 if (obj->Name > 0) {
273 /* remove from hash table */
274 _mesa_HashRemove(ctx->Array.Objects, obj->Name);
275 }
276 }
277
278
279
280 /**
281 * Helper for _mesa_update_array_object_max_element().
282 * \return min(arrayObj->VertexAttrib[*]._MaxElement).
283 */
284 static GLuint
285 compute_max_element(struct gl_array_object *arrayObj, GLbitfield64 enabled)
286 {
287 GLuint min = ~((GLuint)0);
288
289 while (enabled) {
290 struct gl_client_array *client_array;
291 GLint attrib = ffsll(enabled) - 1;
292 enabled ^= BITFIELD64_BIT(attrib);
293
294 client_array = &arrayObj->VertexAttrib[attrib];
295 assert(client_array->Enabled);
296 _mesa_update_array_max_element(client_array);
297 min = MIN2(min, client_array->_MaxElement);
298 }
299
300 return min;
301 }
302
303
304 /**
305 * Examine vertex arrays to update the gl_array_object::_MaxElement field.
306 */
307 void
308 _mesa_update_array_object_max_element(struct gl_context *ctx,
309 struct gl_array_object *arrayObj)
310 {
311 GLbitfield64 enabled;
312
313 if (!ctx->VertexProgram._Current ||
314 ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram) {
315 enabled = _mesa_array_object_get_enabled_ff(arrayObj);
316 } else {
317 enabled = _mesa_array_object_get_enabled_arb(arrayObj);
318 }
319
320 /* _MaxElement is one past the last legal array element */
321 arrayObj->_MaxElement = compute_max_element(arrayObj, enabled);
322 }
323
324
325 /**********************************************************************/
326 /* API Functions */
327 /**********************************************************************/
328
329
330 /**
331 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
332 * \param genRequired specifies behavour when id was not generated with
333 * glGenVertexArrays().
334 */
335 static void
336 bind_vertex_array(struct gl_context *ctx, GLuint id, GLboolean genRequired)
337 {
338 struct gl_array_object * const oldObj = ctx->Array.ArrayObj;
339 struct gl_array_object *newObj = NULL;
340
341 ASSERT(oldObj != NULL);
342
343 if ( oldObj->Name == id )
344 return; /* rebinding the same array object- no change */
345
346 /*
347 * Get pointer to new array object (newObj)
348 */
349 if (id == 0) {
350 /* The spec says there is no array object named 0, but we use
351 * one internally because it simplifies things.
352 */
353 newObj = ctx->Array.DefaultArrayObj;
354 }
355 else {
356 /* non-default array object */
357 newObj = lookup_arrayobj(ctx, id);
358 if (!newObj) {
359 if (genRequired) {
360 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindVertexArray(non-gen name)");
361 return;
362 }
363
364 /* For APPLE version, generate a new array object now */
365 newObj = (*ctx->Driver.NewArrayObject)(ctx, id);
366 if (!newObj) {
367 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
368 return;
369 }
370
371 save_array_object(ctx, newObj);
372 }
373
374 if (!newObj->EverBound) {
375 /* The "Interactions with APPLE_vertex_array_object" section of the
376 * GL_ARB_vertex_array_object spec says:
377 *
378 * "The first bind call, either BindVertexArray or
379 * BindVertexArrayAPPLE, determines the semantic of the object."
380 */
381 newObj->ARBsemantics = genRequired;
382 newObj->EverBound = GL_TRUE;
383 }
384 }
385
386 ctx->NewState |= _NEW_ARRAY;
387 _mesa_reference_array_object(ctx, &ctx->Array.ArrayObj, newObj);
388
389 /* Pass BindVertexArray call to device driver */
390 if (ctx->Driver.BindArrayObject && newObj)
391 ctx->Driver.BindArrayObject(ctx, newObj);
392 }
393
394
395 /**
396 * ARB version of glBindVertexArray()
397 * This function behaves differently from glBindVertexArrayAPPLE() in
398 * that this function requires all ids to have been previously generated
399 * by glGenVertexArrays[APPLE]().
400 */
401 void GLAPIENTRY
402 _mesa_BindVertexArray( GLuint id )
403 {
404 GET_CURRENT_CONTEXT(ctx);
405 bind_vertex_array(ctx, id, GL_TRUE);
406 }
407
408
409 /**
410 * Bind a new array.
411 *
412 * \todo
413 * The binding could be done more efficiently by comparing the non-NULL
414 * pointers in the old and new objects. The only arrays that are "dirty" are
415 * the ones that are non-NULL in either object.
416 */
417 void GLAPIENTRY
418 _mesa_BindVertexArrayAPPLE( GLuint id )
419 {
420 GET_CURRENT_CONTEXT(ctx);
421 bind_vertex_array(ctx, id, GL_FALSE);
422 }
423
424
425 /**
426 * Delete a set of array objects.
427 *
428 * \param n Number of array objects to delete.
429 * \param ids Array of \c n array object IDs.
430 */
431 void GLAPIENTRY
432 _mesa_DeleteVertexArrays(GLsizei n, const GLuint *ids)
433 {
434 GET_CURRENT_CONTEXT(ctx);
435 GLsizei i;
436
437 if (n < 0) {
438 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)");
439 return;
440 }
441
442 for (i = 0; i < n; i++) {
443 struct gl_array_object *obj = lookup_arrayobj(ctx, ids[i]);
444
445 if ( obj != NULL ) {
446 ASSERT( obj->Name == ids[i] );
447
448 /* If the array object is currently bound, the spec says "the binding
449 * for that object reverts to zero and the default vertex array
450 * becomes current."
451 */
452 if ( obj == ctx->Array.ArrayObj ) {
453 _mesa_BindVertexArray(0);
454 }
455
456 /* The ID is immediately freed for re-use */
457 remove_array_object(ctx, obj);
458
459 /* Unreference the array object.
460 * If refcount hits zero, the object will be deleted.
461 */
462 _mesa_reference_array_object(ctx, &obj, NULL);
463 }
464 }
465 }
466
467
468 /**
469 * Generate a set of unique array object IDs and store them in \c arrays.
470 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
471 * \param n Number of IDs to generate.
472 * \param arrays Array of \c n locations to store the IDs.
473 * \param vboOnly Will arrays have to reside in VBOs?
474 */
475 static void
476 gen_vertex_arrays(struct gl_context *ctx, GLsizei n, GLuint *arrays)
477 {
478 GLuint first;
479 GLint i;
480
481 if (n < 0) {
482 _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE");
483 return;
484 }
485
486 if (!arrays) {
487 return;
488 }
489
490 first = _mesa_HashFindFreeKeyBlock(ctx->Array.Objects, n);
491
492 /* Allocate new, empty array objects and return identifiers */
493 for (i = 0; i < n; i++) {
494 struct gl_array_object *obj;
495 GLuint name = first + i;
496
497 obj = (*ctx->Driver.NewArrayObject)( ctx, name );
498 if (!obj) {
499 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE");
500 return;
501 }
502 save_array_object(ctx, obj);
503 arrays[i] = first + i;
504 }
505 }
506
507
508 /**
509 * ARB version of glGenVertexArrays()
510 * All arrays will be required to live in VBOs.
511 */
512 void GLAPIENTRY
513 _mesa_GenVertexArrays(GLsizei n, GLuint *arrays)
514 {
515 GET_CURRENT_CONTEXT(ctx);
516 gen_vertex_arrays(ctx, n, arrays);
517 }
518
519
520 /**
521 * APPLE version of glGenVertexArraysAPPLE()
522 * Arrays may live in VBOs or ordinary memory.
523 */
524 void GLAPIENTRY
525 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
526 {
527 GET_CURRENT_CONTEXT(ctx);
528 gen_vertex_arrays(ctx, n, arrays);
529 }
530
531
532 /**
533 * Determine if ID is the name of an array object.
534 *
535 * \param id ID of the potential array object.
536 * \return \c GL_TRUE if \c id is the name of a array object,
537 * \c GL_FALSE otherwise.
538 */
539 GLboolean GLAPIENTRY
540 _mesa_IsVertexArray( GLuint id )
541 {
542 struct gl_array_object * obj;
543 GET_CURRENT_CONTEXT(ctx);
544 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
545
546 if (id == 0)
547 return GL_FALSE;
548
549 obj = lookup_arrayobj(ctx, id);
550 if (obj == NULL)
551 return GL_FALSE;
552
553 return obj->EverBound;
554 }