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