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