ea56154f1ca82f561ce1f0e4d9e1135d4d3471fe
[mesa.git] / src / mesa / main / arrayobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * (C) Copyright IBM Corporation 2006
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /**
29 * \file arrayobj.c
30 *
31 * Implementation of Vertex Array Objects (VAOs), from OpenGL 3.1+,
32 * the GL_ARB_vertex_array_object extension, or the older
33 * GL_APPLE_vertex_array_object extension.
34 *
35 * \todo
36 * The code in this file borrows a lot from bufferobj.c. There's a certain
37 * amount of cruft left over from that origin that may be unnecessary.
38 *
39 * \author Ian Romanick <idr@us.ibm.com>
40 * \author Brian Paul
41 */
42
43
44 #include "glheader.h"
45 #include "hash.h"
46 #include "image.h"
47 #include "imports.h"
48 #include "context.h"
49 #include "bufferobj.h"
50 #include "arrayobj.h"
51 #include "macros.h"
52 #include "mtypes.h"
53 #include "varray.h"
54 #include "main/dispatch.h"
55
56
57 /**
58 * Look up the array object for the given ID.
59 *
60 * \returns
61 * Either a pointer to the array object with the specified ID or \c NULL for
62 * a non-existent ID. The spec defines ID 0 as being technically
63 * non-existent.
64 */
65
66 struct gl_vertex_array_object *
67 _mesa_lookup_vao(struct gl_context *ctx, GLuint id)
68 {
69 if (id == 0)
70 return NULL;
71 else
72 return (struct gl_vertex_array_object *)
73 _mesa_HashLookup(ctx->Array.Objects, id);
74 }
75
76
77 /**
78 * For all the vertex binding points in the array object, unbind any pointers
79 * to any buffer objects (VBOs).
80 * This is done just prior to array object destruction.
81 */
82 static void
83 unbind_array_object_vbos(struct gl_context *ctx, struct gl_vertex_array_object *obj)
84 {
85 GLuint i;
86
87 for (i = 0; i < ARRAY_SIZE(obj->VertexBinding); i++)
88 _mesa_reference_buffer_object(ctx, &obj->VertexBinding[i].BufferObj, NULL);
89
90 for (i = 0; i < ARRAY_SIZE(obj->_VertexAttrib); i++)
91 _mesa_reference_buffer_object(ctx, &obj->_VertexAttrib[i].BufferObj, NULL);
92 }
93
94
95 /**
96 * Allocate and initialize a new vertex array object.
97 *
98 * This function is intended to be called via
99 * \c dd_function_table::NewArrayObject.
100 */
101 struct gl_vertex_array_object *
102 _mesa_new_vao(struct gl_context *ctx, GLuint name)
103 {
104 struct gl_vertex_array_object *obj = CALLOC_STRUCT(gl_vertex_array_object);
105 if (obj)
106 _mesa_initialize_vao(ctx, obj, name);
107 return obj;
108 }
109
110
111 /**
112 * Delete an array object.
113 *
114 * This function is intended to be called via
115 * \c dd_function_table::DeleteArrayObject.
116 */
117 void
118 _mesa_delete_vao(struct gl_context *ctx, struct gl_vertex_array_object *obj)
119 {
120 unbind_array_object_vbos(ctx, obj);
121 _mesa_reference_buffer_object(ctx, &obj->IndexBufferObj, NULL);
122 mtx_destroy(&obj->Mutex);
123 free(obj->Label);
124 free(obj);
125 }
126
127
128 /**
129 * Set ptr to vao w/ reference counting.
130 * Note: this should only be called from the _mesa_reference_vao()
131 * inline function.
132 */
133 void
134 _mesa_reference_vao_(struct gl_context *ctx,
135 struct gl_vertex_array_object **ptr,
136 struct gl_vertex_array_object *vao)
137 {
138 assert(*ptr != vao);
139
140 if (*ptr) {
141 /* Unreference the old array object */
142 GLboolean deleteFlag = GL_FALSE;
143 struct gl_vertex_array_object *oldObj = *ptr;
144
145 mtx_lock(&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 mtx_unlock(&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 (vao) {
165 /* reference new array object */
166 mtx_lock(&vao->Mutex);
167 if (vao->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 vao->RefCount++;
175 #if 0
176 printf("ArrayObj %p %d INCR to %d\n",
177 (void *) vao, vao->Name, vao->RefCount);
178 #endif
179 *ptr = vao;
180 }
181 mtx_unlock(&vao->Mutex);
182 }
183 }
184
185
186
187 static void
188 init_array(struct gl_context *ctx,
189 struct gl_vertex_array_object *obj, GLuint index, GLint size, GLint type)
190 {
191 struct gl_vertex_attrib_array *array = &obj->VertexAttrib[index];
192 struct gl_vertex_buffer_binding *binding = &obj->VertexBinding[index];
193
194 array->Size = size;
195 array->Type = type;
196 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
197 array->Stride = 0;
198 array->Ptr = NULL;
199 array->RelativeOffset = 0;
200 array->Enabled = GL_FALSE;
201 array->Normalized = GL_FALSE;
202 array->Integer = GL_FALSE;
203 array->Doubles = GL_FALSE;
204 array->_ElementSize = size * _mesa_sizeof_type(type);
205 array->VertexBinding = index;
206
207 binding->Offset = 0;
208 binding->Stride = array->_ElementSize;
209 binding->BufferObj = NULL;
210 binding->_BoundArrays = BITFIELD64_BIT(index);
211
212 /* Vertex array buffers */
213 _mesa_reference_buffer_object(ctx, &binding->BufferObj,
214 ctx->Shared->NullBufferObj);
215 }
216
217
218 /**
219 * Initialize a gl_vertex_array_object's arrays.
220 */
221 void
222 _mesa_initialize_vao(struct gl_context *ctx,
223 struct gl_vertex_array_object *obj,
224 GLuint name)
225 {
226 GLuint i;
227
228 obj->Name = name;
229
230 mtx_init(&obj->Mutex, mtx_plain);
231 obj->RefCount = 1;
232
233 /* Init the individual arrays */
234 for (i = 0; i < ARRAY_SIZE(obj->VertexAttrib); i++) {
235 switch (i) {
236 case VERT_ATTRIB_WEIGHT:
237 init_array(ctx, obj, VERT_ATTRIB_WEIGHT, 1, GL_FLOAT);
238 break;
239 case VERT_ATTRIB_NORMAL:
240 init_array(ctx, obj, VERT_ATTRIB_NORMAL, 3, GL_FLOAT);
241 break;
242 case VERT_ATTRIB_COLOR1:
243 init_array(ctx, obj, VERT_ATTRIB_COLOR1, 3, GL_FLOAT);
244 break;
245 case VERT_ATTRIB_FOG:
246 init_array(ctx, obj, VERT_ATTRIB_FOG, 1, GL_FLOAT);
247 break;
248 case VERT_ATTRIB_COLOR_INDEX:
249 init_array(ctx, obj, VERT_ATTRIB_COLOR_INDEX, 1, GL_FLOAT);
250 break;
251 case VERT_ATTRIB_EDGEFLAG:
252 init_array(ctx, obj, VERT_ATTRIB_EDGEFLAG, 1, GL_BOOL);
253 break;
254 case VERT_ATTRIB_POINT_SIZE:
255 init_array(ctx, obj, VERT_ATTRIB_POINT_SIZE, 1, GL_FLOAT);
256 break;
257 default:
258 init_array(ctx, obj, i, 4, GL_FLOAT);
259 break;
260 }
261 }
262
263 _mesa_reference_buffer_object(ctx, &obj->IndexBufferObj,
264 ctx->Shared->NullBufferObj);
265 }
266
267
268 /**
269 * Add the given array object to the array object pool.
270 */
271 static void
272 save_array_object( struct gl_context *ctx, struct gl_vertex_array_object *obj )
273 {
274 if (obj->Name > 0) {
275 /* insert into hash table */
276 _mesa_HashInsert(ctx->Array.Objects, obj->Name, obj);
277 }
278 }
279
280
281 /**
282 * Remove the given array object from the array object pool.
283 * Do not deallocate the array object though.
284 */
285 static void
286 remove_array_object( struct gl_context *ctx, struct gl_vertex_array_object *obj )
287 {
288 if (obj->Name > 0) {
289 /* remove from hash table */
290 _mesa_HashRemove(ctx->Array.Objects, obj->Name);
291 }
292 }
293
294
295 /**
296 * Updates the derived gl_client_arrays when a gl_vertex_attrib_array
297 * or a gl_vertex_buffer_binding has changed.
298 */
299 void
300 _mesa_update_vao_client_arrays(struct gl_context *ctx,
301 struct gl_vertex_array_object *vao)
302 {
303 GLbitfield64 arrays = vao->NewArrays;
304
305 while (arrays) {
306 struct gl_client_array *client_array;
307 struct gl_vertex_attrib_array *attrib_array;
308 struct gl_vertex_buffer_binding *buffer_binding;
309
310 GLint attrib = ffsll(arrays) - 1;
311 arrays ^= BITFIELD64_BIT(attrib);
312
313 attrib_array = &vao->VertexAttrib[attrib];
314 buffer_binding = &vao->VertexBinding[attrib_array->VertexBinding];
315 client_array = &vao->_VertexAttrib[attrib];
316
317 _mesa_update_client_array(ctx, client_array, attrib_array,
318 buffer_binding);
319 }
320 }
321
322
323 /**********************************************************************/
324 /* API Functions */
325 /**********************************************************************/
326
327
328 /**
329 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
330 * \param genRequired specifies behavour when id was not generated with
331 * glGenVertexArrays().
332 */
333 static void
334 bind_vertex_array(struct gl_context *ctx, GLuint id, GLboolean genRequired)
335 {
336 struct gl_vertex_array_object * const oldObj = ctx->Array.VAO;
337 struct gl_vertex_array_object *newObj = NULL;
338
339 assert(oldObj != NULL);
340
341 if ( oldObj->Name == id )
342 return; /* rebinding the same array object- no change */
343
344 /*
345 * Get pointer to new array object (newObj)
346 */
347 if (id == 0) {
348 /* The spec says there is no array object named 0, but we use
349 * one internally because it simplifies things.
350 */
351 newObj = ctx->Array.DefaultVAO;
352 }
353 else {
354 /* non-default array object */
355 newObj = _mesa_lookup_vao(ctx, id);
356 if (!newObj) {
357 if (genRequired) {
358 _mesa_error(ctx, GL_INVALID_OPERATION,
359 "glBindVertexArray(non-gen name)");
360 return;
361 }
362
363 /* For APPLE version, generate a new array object now */
364 newObj = (*ctx->Driver.NewArrayObject)(ctx, id);
365 if (!newObj) {
366 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
367 return;
368 }
369
370 save_array_object(ctx, newObj);
371 }
372
373 if (!newObj->EverBound) {
374 /* The "Interactions with APPLE_vertex_array_object" section of the
375 * GL_ARB_vertex_array_object spec says:
376 *
377 * "The first bind call, either BindVertexArray or
378 * BindVertexArrayAPPLE, determines the semantic of the object."
379 */
380 newObj->ARBsemantics = genRequired;
381 newObj->EverBound = GL_TRUE;
382 }
383 }
384
385 if (ctx->Array.DrawMethod == DRAW_ARRAYS) {
386 /* The _DrawArrays pointer is pointing at the VAO being unbound and
387 * that VAO may be in the process of being deleted. If it's not going
388 * to be deleted, this will have no effect, because the pointer needs
389 * to be updated by the VBO module anyway.
390 *
391 * Before the VBO module can update the pointer, we have to set it
392 * to NULL for drivers not to set up arrays which are not bound,
393 * or to prevent a crash if the VAO being unbound is going to be
394 * deleted.
395 */
396 ctx->Array._DrawArrays = NULL;
397 ctx->Array.DrawMethod = DRAW_NONE;
398 }
399
400 ctx->NewState |= _NEW_ARRAY;
401 _mesa_reference_vao(ctx, &ctx->Array.VAO, newObj);
402
403 /* Pass BindVertexArray call to device driver */
404 if (ctx->Driver.BindArrayObject && newObj)
405 ctx->Driver.BindArrayObject(ctx, newObj);
406 }
407
408
409 /**
410 * ARB version of glBindVertexArray()
411 * This function behaves differently from glBindVertexArrayAPPLE() in
412 * that this function requires all ids to have been previously generated
413 * by glGenVertexArrays[APPLE]().
414 */
415 void GLAPIENTRY
416 _mesa_BindVertexArray( GLuint id )
417 {
418 GET_CURRENT_CONTEXT(ctx);
419 bind_vertex_array(ctx, id, GL_TRUE);
420 }
421
422
423 /**
424 * Bind a new array.
425 *
426 * \todo
427 * The binding could be done more efficiently by comparing the non-NULL
428 * pointers in the old and new objects. The only arrays that are "dirty" are
429 * the ones that are non-NULL in either object.
430 */
431 void GLAPIENTRY
432 _mesa_BindVertexArrayAPPLE( GLuint id )
433 {
434 GET_CURRENT_CONTEXT(ctx);
435 bind_vertex_array(ctx, id, GL_FALSE);
436 }
437
438
439 /**
440 * Delete a set of array objects.
441 *
442 * \param n Number of array objects to delete.
443 * \param ids Array of \c n array object IDs.
444 */
445 void GLAPIENTRY
446 _mesa_DeleteVertexArrays(GLsizei n, const GLuint *ids)
447 {
448 GET_CURRENT_CONTEXT(ctx);
449 GLsizei i;
450
451 if (n < 0) {
452 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArray(n)");
453 return;
454 }
455
456 for (i = 0; i < n; i++) {
457 struct gl_vertex_array_object *obj = _mesa_lookup_vao(ctx, ids[i]);
458
459 if ( obj != NULL ) {
460 assert( obj->Name == ids[i] );
461
462 /* If the array object is currently bound, the spec says "the binding
463 * for that object reverts to zero and the default vertex array
464 * becomes current."
465 */
466 if ( obj == ctx->Array.VAO ) {
467 _mesa_BindVertexArray(0);
468 }
469
470 /* The ID is immediately freed for re-use */
471 remove_array_object(ctx, obj);
472
473 /* Unreference the array object.
474 * If refcount hits zero, the object will be deleted.
475 */
476 _mesa_reference_vao(ctx, &obj, NULL);
477 }
478 }
479 }
480
481
482 /**
483 * Generate a set of unique array object IDs and store them in \c arrays.
484 * Helper for _mesa_GenVertexArrays[APPLE]() and _mesa_CreateVertexArrays()
485 * below.
486 *
487 * \param n Number of IDs to generate.
488 * \param arrays Array of \c n locations to store the IDs.
489 * \param create Indicates that the objects should also be created.
490 * \param func The name of the GL entry point.
491 */
492 static void
493 gen_vertex_arrays(struct gl_context *ctx, GLsizei n, GLuint *arrays,
494 bool create, const char *func)
495 {
496 GLuint first;
497 GLint i;
498
499 if (n < 0) {
500 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
501 return;
502 }
503
504 if (!arrays) {
505 return;
506 }
507
508 first = _mesa_HashFindFreeKeyBlock(ctx->Array.Objects, n);
509
510 /* For the sake of simplicity we create the array objects in both
511 * the Gen* and Create* cases. The only difference is the value of
512 * EverBound, which is set to true in the Create* case.
513 */
514 for (i = 0; i < n; i++) {
515 struct gl_vertex_array_object *obj;
516 GLuint name = first + i;
517
518 obj = (*ctx->Driver.NewArrayObject)( ctx, name );
519 if (!obj) {
520 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
521 return;
522 }
523 obj->EverBound = create;
524 save_array_object(ctx, obj);
525 arrays[i] = first + i;
526 }
527 }
528
529
530 /**
531 * ARB version of glGenVertexArrays()
532 * All arrays will be required to live in VBOs.
533 */
534 void GLAPIENTRY
535 _mesa_GenVertexArrays(GLsizei n, GLuint *arrays)
536 {
537 GET_CURRENT_CONTEXT(ctx);
538 gen_vertex_arrays(ctx, n, arrays, false, "glGenVertexArrays");
539 }
540
541
542 /**
543 * APPLE version of glGenVertexArraysAPPLE()
544 * Arrays may live in VBOs or ordinary memory.
545 */
546 void GLAPIENTRY
547 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
548 {
549 GET_CURRENT_CONTEXT(ctx);
550 gen_vertex_arrays(ctx, n, arrays, false, "glGenVertexArraysAPPLE");
551 }
552
553
554 /**
555 * ARB_direct_state_access
556 * Generates ID's and creates the array objects.
557 */
558 void GLAPIENTRY
559 _mesa_CreateVertexArrays(GLsizei n, GLuint *arrays)
560 {
561 GET_CURRENT_CONTEXT(ctx);
562 gen_vertex_arrays(ctx, n, arrays, true, "glCreateVertexArrays");
563 }
564
565
566 /**
567 * Determine if ID is the name of an array object.
568 *
569 * \param id ID of the potential array object.
570 * \return \c GL_TRUE if \c id is the name of a array object,
571 * \c GL_FALSE otherwise.
572 */
573 GLboolean GLAPIENTRY
574 _mesa_IsVertexArray( GLuint id )
575 {
576 struct gl_vertex_array_object * obj;
577 GET_CURRENT_CONTEXT(ctx);
578 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
579
580 if (id == 0)
581 return GL_FALSE;
582
583 obj = _mesa_lookup_vao(ctx, id);
584 if (obj == NULL)
585 return GL_FALSE;
586
587 return obj->EverBound;
588 }