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