mesa: 80-column wrapping
[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 "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 _mesa_reference_buffer_object(ctx, &obj->Vertex.BufferObj, NULL);
87 _mesa_reference_buffer_object(ctx, &obj->Weight.BufferObj, NULL);
88 _mesa_reference_buffer_object(ctx, &obj->Normal.BufferObj, NULL);
89 _mesa_reference_buffer_object(ctx, &obj->Color.BufferObj, NULL);
90 _mesa_reference_buffer_object(ctx, &obj->SecondaryColor.BufferObj, NULL);
91 _mesa_reference_buffer_object(ctx, &obj->FogCoord.BufferObj, NULL);
92 _mesa_reference_buffer_object(ctx, &obj->Index.BufferObj, NULL);
93 _mesa_reference_buffer_object(ctx, &obj->EdgeFlag.BufferObj, NULL);
94
95 for (i = 0; i < Elements(obj->TexCoord); i++)
96 _mesa_reference_buffer_object(ctx, &obj->TexCoord[i].BufferObj, NULL);
97
98 for (i = 0; i < Elements(obj->VertexAttrib); i++)
99 _mesa_reference_buffer_object(ctx, &obj->VertexAttrib[i].BufferObj,NULL);
100
101 #if FEATURE_point_size_array
102 _mesa_reference_buffer_object(ctx, &obj->PointSize.BufferObj, NULL);
103 #endif
104 }
105
106
107 /**
108 * Allocate and initialize a new vertex array object.
109 *
110 * This function is intended to be called via
111 * \c dd_function_table::NewArrayObject.
112 */
113 struct gl_array_object *
114 _mesa_new_array_object( struct gl_context *ctx, GLuint name )
115 {
116 struct gl_array_object *obj = CALLOC_STRUCT(gl_array_object);
117 if (obj)
118 _mesa_initialize_array_object(ctx, obj, name);
119 return obj;
120 }
121
122
123 /**
124 * Delete an array object.
125 *
126 * This function is intended to be called via
127 * \c dd_function_table::DeleteArrayObject.
128 */
129 void
130 _mesa_delete_array_object( struct gl_context *ctx, struct gl_array_object *obj )
131 {
132 (void) ctx;
133 unbind_array_object_vbos(ctx, obj);
134 _glthread_DESTROY_MUTEX(obj->Mutex);
135 free(obj);
136 }
137
138
139 /**
140 * Set ptr to arrayObj w/ reference counting.
141 */
142 void
143 _mesa_reference_array_object(struct gl_context *ctx,
144 struct gl_array_object **ptr,
145 struct gl_array_object *arrayObj)
146 {
147 if (*ptr == arrayObj)
148 return;
149
150 if (*ptr) {
151 /* Unreference the old array object */
152 GLboolean deleteFlag = GL_FALSE;
153 struct gl_array_object *oldObj = *ptr;
154
155 _glthread_LOCK_MUTEX(oldObj->Mutex);
156 ASSERT(oldObj->RefCount > 0);
157 oldObj->RefCount--;
158 #if 0
159 printf("ArrayObj %p %d DECR to %d\n",
160 (void *) oldObj, oldObj->Name, oldObj->RefCount);
161 #endif
162 deleteFlag = (oldObj->RefCount == 0);
163 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
164
165 if (deleteFlag) {
166 ASSERT(ctx->Driver.DeleteArrayObject);
167 ctx->Driver.DeleteArrayObject(ctx, oldObj);
168 }
169
170 *ptr = NULL;
171 }
172 ASSERT(!*ptr);
173
174 if (arrayObj) {
175 /* reference new array object */
176 _glthread_LOCK_MUTEX(arrayObj->Mutex);
177 if (arrayObj->RefCount == 0) {
178 /* this array's being deleted (look just above) */
179 /* Not sure this can every really happen. Warn if it does. */
180 _mesa_problem(NULL, "referencing deleted array object");
181 *ptr = NULL;
182 }
183 else {
184 arrayObj->RefCount++;
185 #if 0
186 printf("ArrayObj %p %d INCR to %d\n",
187 (void *) arrayObj, arrayObj->Name, arrayObj->RefCount);
188 #endif
189 *ptr = arrayObj;
190 }
191 _glthread_UNLOCK_MUTEX(arrayObj->Mutex);
192 }
193 }
194
195
196
197 static void
198 init_array(struct gl_context *ctx,
199 struct gl_client_array *array, GLint size, GLint type)
200 {
201 array->Size = size;
202 array->Type = type;
203 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
204 array->Stride = 0;
205 array->StrideB = 0;
206 array->Ptr = NULL;
207 array->Enabled = GL_FALSE;
208 array->Normalized = GL_FALSE;
209 #if FEATURE_ARB_vertex_buffer_object
210 /* Vertex array buffers */
211 _mesa_reference_buffer_object(ctx, &array->BufferObj,
212 ctx->Shared->NullBufferObj);
213 #endif
214 }
215
216
217 /**
218 * Initialize a gl_array_object's arrays.
219 */
220 void
221 _mesa_initialize_array_object( struct gl_context *ctx,
222 struct gl_array_object *obj,
223 GLuint name )
224 {
225 GLuint i;
226
227 obj->Name = name;
228
229 _glthread_INIT_MUTEX(obj->Mutex);
230 obj->RefCount = 1;
231
232 /* Init the individual arrays */
233 init_array(ctx, &obj->Vertex, 4, GL_FLOAT);
234 init_array(ctx, &obj->Weight, 1, GL_FLOAT);
235 init_array(ctx, &obj->Normal, 3, GL_FLOAT);
236 init_array(ctx, &obj->Color, 4, GL_FLOAT);
237 init_array(ctx, &obj->SecondaryColor, 3, GL_FLOAT);
238 init_array(ctx, &obj->FogCoord, 1, GL_FLOAT);
239 init_array(ctx, &obj->Index, 1, GL_FLOAT);
240 for (i = 0; i < Elements(obj->TexCoord); i++) {
241 init_array(ctx, &obj->TexCoord[i], 4, GL_FLOAT);
242 }
243 init_array(ctx, &obj->EdgeFlag, 1, GL_BOOL);
244 for (i = 0; i < Elements(obj->VertexAttrib); i++) {
245 init_array(ctx, &obj->VertexAttrib[i], 4, GL_FLOAT);
246 }
247
248 #if FEATURE_point_size_array
249 init_array(ctx, &obj->PointSize, 1, GL_FLOAT);
250 #endif
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 * Compute the index of the last array element that can be safely accessed
284 * in a vertex array. We can really only do this when the array lives in
285 * a VBO.
286 * The array->_MaxElement field will be updated.
287 * Later in glDrawArrays/Elements/etc we can do some bounds checking.
288 */
289 static void
290 compute_max_element(struct gl_client_array *array)
291 {
292 if (array->BufferObj->Name) {
293 /* Compute the max element we can access in the VBO without going
294 * out of bounds.
295 */
296 array->_MaxElement = ((GLsizeiptrARB) array->BufferObj->Size
297 - (GLsizeiptrARB) array->Ptr + array->StrideB
298 - array->_ElementSize) / array->StrideB;
299 if (0)
300 printf("%s Object %u Size %u MaxElement %u\n",
301 __FUNCTION__,
302 array->BufferObj->Name,
303 (GLuint) array->BufferObj->Size,
304 array->_MaxElement);
305 }
306 else {
307 /* user-space array, no idea how big it is */
308 array->_MaxElement = 2 * 1000 * 1000 * 1000; /* just a big number */
309 }
310 }
311
312
313 /**
314 * Helper for update_arrays().
315 * \return min(current min, array->_MaxElement).
316 */
317 static GLuint
318 update_min(GLuint min, struct gl_client_array *array)
319 {
320 compute_max_element(array);
321 if (array->Enabled)
322 return MIN2(min, array->_MaxElement);
323 else
324 return min;
325 }
326
327
328 /**
329 * Examine vertex arrays to update the gl_array_object::_MaxElement field.
330 */
331 void
332 _mesa_update_array_object_max_element(struct gl_context *ctx,
333 struct gl_array_object *arrayObj)
334 {
335 GLuint i, min = ~0;
336
337 min = update_min(min, &arrayObj->Vertex);
338 min = update_min(min, &arrayObj->Weight);
339 min = update_min(min, &arrayObj->Normal);
340 min = update_min(min, &arrayObj->Color);
341 min = update_min(min, &arrayObj->SecondaryColor);
342 min = update_min(min, &arrayObj->FogCoord);
343 min = update_min(min, &arrayObj->Index);
344 min = update_min(min, &arrayObj->EdgeFlag);
345 #if FEATURE_point_size_array
346 min = update_min(min, &arrayObj->PointSize);
347 #endif
348 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++)
349 min = update_min(min, &arrayObj->TexCoord[i]);
350 for (i = 0; i < Elements(arrayObj->VertexAttrib); i++)
351 min = update_min(min, &arrayObj->VertexAttrib[i]);
352
353 /* _MaxElement is one past the last legal array element */
354 arrayObj->_MaxElement = min;
355 }
356
357
358 /**********************************************************************/
359 /* API Functions */
360 /**********************************************************************/
361
362
363 /**
364 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
365 * \param genRequired specifies behavour when id was not generated with
366 * glGenVertexArrays().
367 */
368 static void
369 bind_vertex_array(struct gl_context *ctx, GLuint id, GLboolean genRequired)
370 {
371 struct gl_array_object * const oldObj = ctx->Array.ArrayObj;
372 struct gl_array_object *newObj = NULL;
373 ASSERT_OUTSIDE_BEGIN_END(ctx);
374
375 ASSERT(oldObj != NULL);
376
377 if ( oldObj->Name == id )
378 return; /* rebinding the same array object- no change */
379
380 /*
381 * Get pointer to new array object (newObj)
382 */
383 if (id == 0) {
384 /* The spec says there is no array object named 0, but we use
385 * one internally because it simplifies things.
386 */
387 newObj = ctx->Array.DefaultArrayObj;
388 }
389 else {
390 /* non-default array object */
391 newObj = lookup_arrayobj(ctx, id);
392 if (!newObj) {
393 if (genRequired) {
394 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindVertexArray(id)");
395 return;
396 }
397
398 /* For APPLE version, generate a new array object now */
399 newObj = (*ctx->Driver.NewArrayObject)(ctx, id);
400 if (!newObj) {
401 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
402 return;
403 }
404 save_array_object(ctx, newObj);
405 }
406 }
407
408 ctx->NewState |= _NEW_ARRAY;
409 ctx->Array.NewState |= _NEW_ARRAY_ALL;
410 _mesa_reference_array_object(ctx, &ctx->Array.ArrayObj, newObj);
411
412 /* Pass BindVertexArray call to device driver */
413 if (ctx->Driver.BindArrayObject && newObj)
414 ctx->Driver.BindArrayObject(ctx, newObj);
415 }
416
417
418 /**
419 * ARB version of glBindVertexArray()
420 * This function behaves differently from glBindVertexArrayAPPLE() in
421 * that this function requires all ids to have been previously generated
422 * by glGenVertexArrays[APPLE]().
423 */
424 void GLAPIENTRY
425 _mesa_BindVertexArray( GLuint id )
426 {
427 GET_CURRENT_CONTEXT(ctx);
428 bind_vertex_array(ctx, id, GL_TRUE);
429 }
430
431
432 /**
433 * Bind a new array.
434 *
435 * \todo
436 * The binding could be done more efficiently by comparing the non-NULL
437 * pointers in the old and new objects. The only arrays that are "dirty" are
438 * the ones that are non-NULL in either object.
439 */
440 void GLAPIENTRY
441 _mesa_BindVertexArrayAPPLE( GLuint id )
442 {
443 GET_CURRENT_CONTEXT(ctx);
444 bind_vertex_array(ctx, id, GL_FALSE);
445 }
446
447
448 /**
449 * Delete a set of array objects.
450 *
451 * \param n Number of array objects to delete.
452 * \param ids Array of \c n array object IDs.
453 */
454 void GLAPIENTRY
455 _mesa_DeleteVertexArraysAPPLE(GLsizei n, const GLuint *ids)
456 {
457 GET_CURRENT_CONTEXT(ctx);
458 GLsizei i;
459 ASSERT_OUTSIDE_BEGIN_END(ctx);
460
461 if (n < 0) {
462 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)");
463 return;
464 }
465
466 for (i = 0; i < n; i++) {
467 struct gl_array_object *obj = lookup_arrayobj(ctx, ids[i]);
468
469 if ( obj != NULL ) {
470 ASSERT( obj->Name == ids[i] );
471
472 /* If the array object is currently bound, the spec says "the binding
473 * for that object reverts to zero and the default vertex array
474 * becomes current."
475 */
476 if ( obj == ctx->Array.ArrayObj ) {
477 CALL_BindVertexArrayAPPLE( ctx->Exec, (0) );
478 }
479
480 /* The ID is immediately freed for re-use */
481 remove_array_object(ctx, obj);
482
483 /* Unreference the array object.
484 * If refcount hits zero, the object will be deleted.
485 */
486 _mesa_reference_array_object(ctx, &obj, NULL);
487 }
488 }
489 }
490
491
492 /**
493 * Generate a set of unique array object IDs and store them in \c arrays.
494 * Helper for _mesa_GenVertexArrays[APPLE]() functions below.
495 * \param n Number of IDs to generate.
496 * \param arrays Array of \c n locations to store the IDs.
497 * \param vboOnly Will arrays have to reside in VBOs?
498 */
499 static void
500 gen_vertex_arrays(struct gl_context *ctx, GLsizei n, GLuint *arrays,
501 GLboolean vboOnly)
502 {
503 GLuint first;
504 GLint i;
505 ASSERT_OUTSIDE_BEGIN_END(ctx);
506
507 if (n < 0) {
508 _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE");
509 return;
510 }
511
512 if (!arrays) {
513 return;
514 }
515
516 first = _mesa_HashFindFreeKeyBlock(ctx->Array.Objects, n);
517
518 /* Allocate new, empty array objects and return identifiers */
519 for (i = 0; i < n; i++) {
520 struct gl_array_object *obj;
521 GLuint name = first + i;
522
523 obj = (*ctx->Driver.NewArrayObject)( ctx, name );
524 if (!obj) {
525 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE");
526 return;
527 }
528 obj->VBOonly = vboOnly;
529 save_array_object(ctx, obj);
530 arrays[i] = first + i;
531 }
532 }
533
534
535 /**
536 * ARB version of glGenVertexArrays()
537 * All arrays will be required to live in VBOs.
538 */
539 void GLAPIENTRY
540 _mesa_GenVertexArrays(GLsizei n, GLuint *arrays)
541 {
542 GET_CURRENT_CONTEXT(ctx);
543 gen_vertex_arrays(ctx, n, arrays, GL_TRUE);
544 }
545
546
547 /**
548 * APPLE version of glGenVertexArraysAPPLE()
549 * Arrays may live in VBOs or ordinary memory.
550 */
551 void GLAPIENTRY
552 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
553 {
554 GET_CURRENT_CONTEXT(ctx);
555 gen_vertex_arrays(ctx, n, arrays, GL_FALSE);
556 }
557
558
559 /**
560 * Determine if ID is the name of an array object.
561 *
562 * \param id ID of the potential array object.
563 * \return \c GL_TRUE if \c id is the name of a array object,
564 * \c GL_FALSE otherwise.
565 */
566 GLboolean GLAPIENTRY
567 _mesa_IsVertexArrayAPPLE( GLuint id )
568 {
569 struct gl_array_object * obj;
570 GET_CURRENT_CONTEXT(ctx);
571 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
572
573 if (id == 0)
574 return GL_FALSE;
575
576 obj = lookup_arrayobj(ctx, id);
577
578 return (obj != NULL) ? GL_TRUE : GL_FALSE;
579 }