Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / main / arrayobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.2
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * (C) Copyright IBM Corporation 2006
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 * BRIAN PAUL OR IBM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * 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 "imports.h"
44 #include "context.h"
45 #if FEATURE_ARB_vertex_buffer_object
46 #include "bufferobj.h"
47 #endif
48 #include "arrayobj.h"
49 #include "glapi/dispatch.h"
50
51
52 /**
53 * Look up the array object for the given ID.
54 *
55 * \returns
56 * Either a pointer to the array object with the specified ID or \c NULL for
57 * a non-existent ID. The spec defines ID 0 as being technically
58 * non-existent.
59 */
60
61 static INLINE struct gl_array_object *
62 lookup_arrayobj(GLcontext *ctx, GLuint id)
63 {
64 return (id == 0)
65 ? NULL
66 : (struct gl_array_object *) _mesa_HashLookup(ctx->Shared->ArrayObjects,
67 id);
68 }
69
70
71 /**
72 * For all the vertex arrays in the array object, unbind any pointers
73 * to any buffer objects (VBOs).
74 * This is done just prior to array object destruction.
75 */
76 static void
77 unbind_array_object_vbos(GLcontext *ctx, struct gl_array_object *obj)
78 {
79 GLuint i;
80
81 _mesa_reference_buffer_object(ctx, &obj->Vertex.BufferObj, NULL);
82 _mesa_reference_buffer_object(ctx, &obj->Normal.BufferObj, NULL);
83 _mesa_reference_buffer_object(ctx, &obj->Color.BufferObj, NULL);
84 _mesa_reference_buffer_object(ctx, &obj->SecondaryColor.BufferObj, NULL);
85 _mesa_reference_buffer_object(ctx, &obj->FogCoord.BufferObj, NULL);
86 _mesa_reference_buffer_object(ctx, &obj->Index.BufferObj, NULL);
87 _mesa_reference_buffer_object(ctx, &obj->EdgeFlag.BufferObj, NULL);
88
89 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++)
90 _mesa_reference_buffer_object(ctx, &obj->TexCoord[i].BufferObj, NULL);
91
92 for (i = 0; i < VERT_ATTRIB_MAX; i++)
93 _mesa_reference_buffer_object(ctx, &obj->VertexAttrib[i].BufferObj,NULL);
94 }
95
96
97 /**
98 * Allocate and initialize a new vertex array object.
99 *
100 * This function is intended to be called via
101 * \c dd_function_table::NewArrayObject.
102 */
103 struct gl_array_object *
104 _mesa_new_array_object( GLcontext *ctx, GLuint name )
105 {
106 struct gl_array_object *obj = CALLOC_STRUCT(gl_array_object);
107 if (obj)
108 _mesa_initialize_array_object(ctx, obj, name);
109 return obj;
110 }
111
112
113 /**
114 * Delete an array object.
115 *
116 * This function is intended to be called via
117 * \c dd_function_table::DeleteArrayObject.
118 */
119 void
120 _mesa_delete_array_object( GLcontext *ctx, struct gl_array_object *obj )
121 {
122 (void) ctx;
123 unbind_array_object_vbos(ctx, obj);
124 _glthread_DESTROY_MUTEX(obj->Mutex);
125 _mesa_free(obj);
126 }
127
128
129 /**
130 * Set ptr to arrayObj w/ reference counting.
131 */
132 void
133 _mesa_reference_array_object(GLcontext *ctx,
134 struct gl_array_object **ptr,
135 struct gl_array_object *arrayObj)
136 {
137 if (*ptr == arrayObj)
138 return;
139
140 if (*ptr) {
141 /* Unreference the old array object */
142 GLboolean deleteFlag = GL_FALSE;
143 struct gl_array_object *oldObj = *ptr;
144
145 _glthread_LOCK_MUTEX(oldObj->Mutex);
146 ASSERT(oldObj->RefCount > 0);
147 oldObj->RefCount--;
148 #if 0
149 printf("ArrayObj %p %d DECR to %d\n",
150 (void *) oldObj, oldObj->Name, oldObj->RefCount);
151 #endif
152 deleteFlag = (oldObj->RefCount == 0);
153 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
154
155 if (deleteFlag) {
156 ASSERT(ctx->Driver.DeleteArrayObject);
157 ctx->Driver.DeleteArrayObject(ctx, oldObj);
158 }
159
160 *ptr = NULL;
161 }
162 ASSERT(!*ptr);
163
164 if (arrayObj) {
165 /* reference new array object */
166 _glthread_LOCK_MUTEX(arrayObj->Mutex);
167 if (arrayObj->RefCount == 0) {
168 /* this array's being deleted (look just above) */
169 /* Not sure this can every really happen. Warn if it does. */
170 _mesa_problem(NULL, "referencing deleted array object");
171 *ptr = NULL;
172 }
173 else {
174 arrayObj->RefCount++;
175 #if 0
176 printf("ArrayObj %p %d INCR to %d\n",
177 (void *) arrayObj, arrayObj->Name, arrayObj->RefCount);
178 #endif
179 *ptr = arrayObj;
180 }
181 _glthread_UNLOCK_MUTEX(arrayObj->Mutex);
182 }
183 }
184
185
186
187 static void
188 init_array(GLcontext *ctx,
189 struct gl_client_array *array, GLint size, GLint type)
190 {
191 array->Size = size;
192 array->Type = type;
193 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
194 array->Stride = 0;
195 array->StrideB = 0;
196 array->Ptr = NULL;
197 array->Enabled = GL_FALSE;
198 array->Normalized = GL_FALSE;
199 #if FEATURE_ARB_vertex_buffer_object
200 /* Vertex array buffers */
201 _mesa_reference_buffer_object(ctx, &array->BufferObj,
202 ctx->Shared->NullBufferObj);
203 #endif
204 }
205
206
207 /**
208 * Initialize a gl_array_object's arrays.
209 */
210 void
211 _mesa_initialize_array_object( GLcontext *ctx,
212 struct gl_array_object *obj,
213 GLuint name )
214 {
215 GLuint i;
216
217 obj->Name = name;
218
219 _glthread_INIT_MUTEX(obj->Mutex);
220 obj->RefCount = 1;
221
222 /* Init the individual arrays */
223 init_array(ctx, &obj->Vertex, 4, GL_FLOAT);
224 init_array(ctx, &obj->Normal, 3, GL_FLOAT);
225 init_array(ctx, &obj->Color, 4, GL_FLOAT);
226 init_array(ctx, &obj->SecondaryColor, 4, GL_FLOAT);
227 init_array(ctx, &obj->FogCoord, 1, GL_FLOAT);
228 init_array(ctx, &obj->Index, 1, GL_FLOAT);
229 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
230 init_array(ctx, &obj->TexCoord[i], 4, GL_FLOAT);
231 }
232 init_array(ctx, &obj->EdgeFlag, 1, GL_BOOL);
233 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
234 init_array(ctx, &obj->VertexAttrib[i], 4, GL_FLOAT);
235 }
236
237 #if FEATURE_point_size_array
238 init_array(ctx, &obj->PointSize, 1, GL_FLOAT);
239 #endif
240 }
241
242
243 /**
244 * Add the given array object to the array object pool.
245 */
246 static void
247 save_array_object( GLcontext *ctx, struct gl_array_object *obj )
248 {
249 if (obj->Name > 0) {
250 /* insert into hash table */
251 _mesa_HashInsert(ctx->Shared->ArrayObjects, obj->Name, obj);
252 }
253 }
254
255
256 /**
257 * Remove the given array object from the array object pool.
258 * Do not deallocate the array object though.
259 */
260 static void
261 remove_array_object( GLcontext *ctx, struct gl_array_object *obj )
262 {
263 if (obj->Name > 0) {
264 /* remove from hash table */
265 _mesa_HashRemove(ctx->Shared->ArrayObjects, obj->Name);
266 }
267 }
268
269
270 /**********************************************************************/
271 /* API Functions */
272 /**********************************************************************/
273
274 /**
275 * Bind a new array.
276 *
277 * \todo
278 * The binding could be done more efficiently by comparing the non-NULL
279 * pointers in the old and new objects. The only arrays that are "dirty" are
280 * the ones that are non-NULL in either object.
281 */
282 void GLAPIENTRY
283 _mesa_BindVertexArrayAPPLE( GLuint id )
284 {
285 GET_CURRENT_CONTEXT(ctx);
286 struct gl_array_object * const oldObj = ctx->Array.ArrayObj;
287 struct gl_array_object *newObj = NULL;
288 ASSERT_OUTSIDE_BEGIN_END(ctx);
289
290 ASSERT(oldObj != NULL);
291
292 if ( oldObj->Name == id )
293 return; /* rebinding the same array object- no change */
294
295 /*
296 * Get pointer to new array object (newObj)
297 */
298 if (id == 0) {
299 /* The spec says there is no array object named 0, but we use
300 * one internally because it simplifies things.
301 */
302 newObj = ctx->Array.DefaultArrayObj;
303 }
304 else {
305 /* non-default array object */
306 newObj = lookup_arrayobj(ctx, id);
307 if (!newObj) {
308 /* If this is a new array object id, allocate an array object now.
309 */
310 newObj = (*ctx->Driver.NewArrayObject)(ctx, id);
311 if (!newObj) {
312 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
313 return;
314 }
315 save_array_object(ctx, newObj);
316 }
317 }
318
319 ctx->NewState |= _NEW_ARRAY;
320 ctx->Array.NewState |= _NEW_ARRAY_ALL;
321 _mesa_reference_array_object(ctx, &ctx->Array.ArrayObj, newObj);
322
323 /* Pass BindVertexArray call to device driver */
324 if (ctx->Driver.BindArrayObject && newObj)
325 (*ctx->Driver.BindArrayObject)( ctx, newObj );
326 }
327
328
329 /**
330 * Delete a set of array objects.
331 *
332 * \param n Number of array objects to delete.
333 * \param ids Array of \c n array object IDs.
334 */
335 void GLAPIENTRY
336 _mesa_DeleteVertexArraysAPPLE(GLsizei n, const GLuint *ids)
337 {
338 GET_CURRENT_CONTEXT(ctx);
339 GLsizei i;
340 ASSERT_OUTSIDE_BEGIN_END(ctx);
341
342 if (n < 0) {
343 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)");
344 return;
345 }
346
347 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
348
349 for (i = 0; i < n; i++) {
350 struct gl_array_object *obj = lookup_arrayobj(ctx, ids[i]);
351
352 if ( obj != NULL ) {
353 ASSERT( obj->Name == ids[i] );
354
355 /* If the array object is currently bound, the spec says "the binding
356 * for that object reverts to zero and the default vertex array
357 * becomes current."
358 */
359 if ( obj == ctx->Array.ArrayObj ) {
360 CALL_BindVertexArrayAPPLE( ctx->Exec, (0) );
361 }
362
363 /* The ID is immediately freed for re-use */
364 remove_array_object(ctx, obj);
365
366 /* Unreference the array object.
367 * If refcount hits zero, the object will be deleted.
368 */
369 _mesa_reference_array_object(ctx, &obj, NULL);
370 }
371 }
372
373 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
374 }
375
376
377 /**
378 * Generate a set of unique array object IDs and store them in \c arrays.
379 *
380 * \param n Number of IDs to generate.
381 * \param arrays Array of \c n locations to store the IDs.
382 */
383 void GLAPIENTRY
384 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
385 {
386 GET_CURRENT_CONTEXT(ctx);
387 GLuint first;
388 GLint i;
389 ASSERT_OUTSIDE_BEGIN_END(ctx);
390
391 if (n < 0) {
392 _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE");
393 return;
394 }
395
396 if (!arrays) {
397 return;
398 }
399
400 /*
401 * This must be atomic (generation and allocation of array object IDs)
402 */
403 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
404
405 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->ArrayObjects, n);
406
407 /* Allocate new, empty array objects and return identifiers */
408 for (i = 0; i < n; i++) {
409 struct gl_array_object *obj;
410 GLuint name = first + i;
411
412 obj = (*ctx->Driver.NewArrayObject)( ctx, name );
413 if (!obj) {
414 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
415 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE");
416 return;
417 }
418 save_array_object(ctx, obj);
419 arrays[i] = first + i;
420 }
421
422 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
423 }
424
425
426 /**
427 * Determine if ID is the name of an array object.
428 *
429 * \param id ID of the potential array object.
430 * \return \c GL_TRUE if \c id is the name of a array object,
431 * \c GL_FALSE otherwise.
432 */
433 GLboolean GLAPIENTRY
434 _mesa_IsVertexArrayAPPLE( GLuint id )
435 {
436 struct gl_array_object * obj;
437 GET_CURRENT_CONTEXT(ctx);
438 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
439
440 if (id == 0)
441 return GL_FALSE;
442
443 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
444 obj = lookup_arrayobj(ctx, id);
445 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
446
447 return (obj != NULL) ? GL_TRUE : GL_FALSE;
448 }