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