mesa: remove Driver.BindArrayObject
[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 * Looks up the array object for the given ID.
79 *
80 * Unlike _mesa_lookup_vao, this function generates a GL_INVALID_OPERATION
81 * error if the array object does not exist. It also returns the default
82 * array object when ctx is a compatibility profile context and id is zero.
83 */
84 struct gl_vertex_array_object *
85 _mesa_lookup_vao_err(struct gl_context *ctx, GLuint id, const char *caller)
86 {
87 /* The ARB_direct_state_access specification says:
88 *
89 * "<vaobj> is [compatibility profile:
90 * zero, indicating the default vertex array object, or]
91 * the name of the vertex array object."
92 */
93 if (id == 0) {
94 if (ctx->API == API_OPENGL_CORE) {
95 _mesa_error(ctx, GL_INVALID_OPERATION,
96 "%s(zero is not valid vaobj name in a core profile "
97 "context)", caller);
98 return NULL;
99 }
100
101 return ctx->Array.DefaultVAO;
102 } else {
103 struct gl_vertex_array_object *vao;
104
105 if (ctx->Array.LastLookedUpVAO &&
106 ctx->Array.LastLookedUpVAO->Name == id) {
107 vao = ctx->Array.LastLookedUpVAO;
108 } else {
109 vao = (struct gl_vertex_array_object *)
110 _mesa_HashLookup(ctx->Array.Objects, id);
111
112 /* The ARB_direct_state_access specification says:
113 *
114 * "An INVALID_OPERATION error is generated if <vaobj> is not
115 * [compatibility profile: zero or] the name of an existing
116 * vertex array object."
117 */
118 if (!vao || !vao->EverBound) {
119 _mesa_error(ctx, GL_INVALID_OPERATION,
120 "%s(non-existent vaobj=%u)", caller, id);
121 return NULL;
122 }
123
124 _mesa_reference_vao(ctx, &ctx->Array.LastLookedUpVAO, vao);
125 }
126
127 return vao;
128 }
129 }
130
131
132 /**
133 * For all the vertex binding points in the array object, unbind any pointers
134 * to any buffer objects (VBOs).
135 * This is done just prior to array object destruction.
136 */
137 static void
138 unbind_array_object_vbos(struct gl_context *ctx, struct gl_vertex_array_object *obj)
139 {
140 GLuint i;
141
142 for (i = 0; i < ARRAY_SIZE(obj->VertexBinding); i++)
143 _mesa_reference_buffer_object(ctx, &obj->VertexBinding[i].BufferObj, NULL);
144
145 for (i = 0; i < ARRAY_SIZE(obj->_VertexAttrib); i++)
146 _mesa_reference_buffer_object(ctx, &obj->_VertexAttrib[i].BufferObj, NULL);
147 }
148
149
150 /**
151 * Allocate and initialize a new vertex array object.
152 *
153 * This function is intended to be called via
154 */
155 struct gl_vertex_array_object *
156 _mesa_new_vao(struct gl_context *ctx, GLuint name)
157 {
158 struct gl_vertex_array_object *obj = CALLOC_STRUCT(gl_vertex_array_object);
159 if (obj)
160 _mesa_initialize_vao(ctx, obj, name);
161 return obj;
162 }
163
164
165 /**
166 * Delete an array object.
167 *
168 * This function is intended to be called via
169 * \c dd_function_table::DeleteArrayObject.
170 */
171 void
172 _mesa_delete_vao(struct gl_context *ctx, struct gl_vertex_array_object *obj)
173 {
174 unbind_array_object_vbos(ctx, obj);
175 _mesa_reference_buffer_object(ctx, &obj->IndexBufferObj, NULL);
176 mtx_destroy(&obj->Mutex);
177 free(obj->Label);
178 free(obj);
179 }
180
181
182 /**
183 * Set ptr to vao w/ reference counting.
184 * Note: this should only be called from the _mesa_reference_vao()
185 * inline function.
186 */
187 void
188 _mesa_reference_vao_(struct gl_context *ctx,
189 struct gl_vertex_array_object **ptr,
190 struct gl_vertex_array_object *vao)
191 {
192 assert(*ptr != vao);
193
194 if (*ptr) {
195 /* Unreference the old array object */
196 GLboolean deleteFlag = GL_FALSE;
197 struct gl_vertex_array_object *oldObj = *ptr;
198
199 mtx_lock(&oldObj->Mutex);
200 assert(oldObj->RefCount > 0);
201 oldObj->RefCount--;
202 deleteFlag = (oldObj->RefCount == 0);
203 mtx_unlock(&oldObj->Mutex);
204
205 if (deleteFlag)
206 _mesa_delete_vao(ctx, oldObj);
207
208 *ptr = NULL;
209 }
210 assert(!*ptr);
211
212 if (vao) {
213 /* reference new array object */
214 mtx_lock(&vao->Mutex);
215 if (vao->RefCount == 0) {
216 /* this array's being deleted (look just above) */
217 /* Not sure this can every really happen. Warn if it does. */
218 _mesa_problem(NULL, "referencing deleted array object");
219 *ptr = NULL;
220 }
221 else {
222 vao->RefCount++;
223 *ptr = vao;
224 }
225 mtx_unlock(&vao->Mutex);
226 }
227 }
228
229
230
231 static void
232 init_array(struct gl_context *ctx,
233 struct gl_vertex_array_object *obj, GLuint index, GLint size, GLint type)
234 {
235 struct gl_vertex_attrib_array *array = &obj->VertexAttrib[index];
236 struct gl_vertex_buffer_binding *binding = &obj->VertexBinding[index];
237
238 array->Size = size;
239 array->Type = type;
240 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
241 array->Stride = 0;
242 array->Ptr = NULL;
243 array->RelativeOffset = 0;
244 array->Enabled = GL_FALSE;
245 array->Normalized = GL_FALSE;
246 array->Integer = GL_FALSE;
247 array->Doubles = GL_FALSE;
248 array->_ElementSize = size * _mesa_sizeof_type(type);
249 array->VertexBinding = index;
250
251 binding->Offset = 0;
252 binding->Stride = array->_ElementSize;
253 binding->BufferObj = NULL;
254 binding->_BoundArrays = BITFIELD64_BIT(index);
255
256 /* Vertex array buffers */
257 _mesa_reference_buffer_object(ctx, &binding->BufferObj,
258 ctx->Shared->NullBufferObj);
259 }
260
261
262 /**
263 * Initialize a gl_vertex_array_object's arrays.
264 */
265 void
266 _mesa_initialize_vao(struct gl_context *ctx,
267 struct gl_vertex_array_object *obj,
268 GLuint name)
269 {
270 GLuint i;
271
272 obj->Name = name;
273
274 mtx_init(&obj->Mutex, mtx_plain);
275 obj->RefCount = 1;
276
277 /* Init the individual arrays */
278 for (i = 0; i < ARRAY_SIZE(obj->VertexAttrib); i++) {
279 switch (i) {
280 case VERT_ATTRIB_WEIGHT:
281 init_array(ctx, obj, VERT_ATTRIB_WEIGHT, 1, GL_FLOAT);
282 break;
283 case VERT_ATTRIB_NORMAL:
284 init_array(ctx, obj, VERT_ATTRIB_NORMAL, 3, GL_FLOAT);
285 break;
286 case VERT_ATTRIB_COLOR1:
287 init_array(ctx, obj, VERT_ATTRIB_COLOR1, 3, GL_FLOAT);
288 break;
289 case VERT_ATTRIB_FOG:
290 init_array(ctx, obj, VERT_ATTRIB_FOG, 1, GL_FLOAT);
291 break;
292 case VERT_ATTRIB_COLOR_INDEX:
293 init_array(ctx, obj, VERT_ATTRIB_COLOR_INDEX, 1, GL_FLOAT);
294 break;
295 case VERT_ATTRIB_EDGEFLAG:
296 init_array(ctx, obj, VERT_ATTRIB_EDGEFLAG, 1, GL_BOOL);
297 break;
298 case VERT_ATTRIB_POINT_SIZE:
299 init_array(ctx, obj, VERT_ATTRIB_POINT_SIZE, 1, GL_FLOAT);
300 break;
301 default:
302 init_array(ctx, obj, i, 4, GL_FLOAT);
303 break;
304 }
305 }
306
307 _mesa_reference_buffer_object(ctx, &obj->IndexBufferObj,
308 ctx->Shared->NullBufferObj);
309 }
310
311
312 /**
313 * Add the given array object to the array object pool.
314 */
315 static void
316 save_array_object( struct gl_context *ctx, struct gl_vertex_array_object *obj )
317 {
318 if (obj->Name > 0) {
319 /* insert into hash table */
320 _mesa_HashInsert(ctx->Array.Objects, obj->Name, obj);
321 }
322 }
323
324
325 /**
326 * Remove the given array object from the array object pool.
327 * Do not deallocate the array object though.
328 */
329 static void
330 remove_array_object( struct gl_context *ctx, struct gl_vertex_array_object *obj )
331 {
332 if (obj->Name > 0) {
333 /* remove from hash table */
334 _mesa_HashRemove(ctx->Array.Objects, obj->Name);
335 }
336 }
337
338
339 /**
340 * Updates the derived gl_client_arrays when a gl_vertex_attrib_array
341 * or a gl_vertex_buffer_binding has changed.
342 */
343 void
344 _mesa_update_vao_client_arrays(struct gl_context *ctx,
345 struct gl_vertex_array_object *vao)
346 {
347 GLbitfield64 arrays = vao->NewArrays;
348
349 while (arrays) {
350 struct gl_client_array *client_array;
351 struct gl_vertex_attrib_array *attrib_array;
352 struct gl_vertex_buffer_binding *buffer_binding;
353
354 GLint attrib = ffsll(arrays) - 1;
355 arrays ^= BITFIELD64_BIT(attrib);
356
357 attrib_array = &vao->VertexAttrib[attrib];
358 buffer_binding = &vao->VertexBinding[attrib_array->VertexBinding];
359 client_array = &vao->_VertexAttrib[attrib];
360
361 _mesa_update_client_array(ctx, client_array, attrib_array,
362 buffer_binding);
363 }
364 }
365
366
367 /**********************************************************************/
368 /* API Functions */
369 /**********************************************************************/
370
371
372 /**
373 * Helper for _mesa_BindVertexArray() and _mesa_BindVertexArrayAPPLE().
374 * \param genRequired specifies behavour when id was not generated with
375 * glGenVertexArrays().
376 */
377 static void
378 bind_vertex_array(struct gl_context *ctx, GLuint id, GLboolean genRequired)
379 {
380 struct gl_vertex_array_object * const oldObj = ctx->Array.VAO;
381 struct gl_vertex_array_object *newObj = NULL;
382
383 assert(oldObj != NULL);
384
385 if ( oldObj->Name == id )
386 return; /* rebinding the same array object- no change */
387
388 /*
389 * Get pointer to new array object (newObj)
390 */
391 if (id == 0) {
392 /* The spec says there is no array object named 0, but we use
393 * one internally because it simplifies things.
394 */
395 newObj = ctx->Array.DefaultVAO;
396 }
397 else {
398 /* non-default array object */
399 newObj = _mesa_lookup_vao(ctx, id);
400 if (!newObj) {
401 if (genRequired) {
402 _mesa_error(ctx, GL_INVALID_OPERATION,
403 "glBindVertexArray(non-gen name)");
404 return;
405 }
406
407 /* For APPLE version, generate a new array object now */
408 newObj = _mesa_new_vao(ctx, id);
409 if (!newObj) {
410 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
411 return;
412 }
413
414 save_array_object(ctx, newObj);
415 }
416
417 if (!newObj->EverBound) {
418 /* The "Interactions with APPLE_vertex_array_object" section of the
419 * GL_ARB_vertex_array_object spec says:
420 *
421 * "The first bind call, either BindVertexArray or
422 * BindVertexArrayAPPLE, determines the semantic of the object."
423 */
424 newObj->ARBsemantics = genRequired;
425 newObj->EverBound = GL_TRUE;
426 }
427 }
428
429 if (ctx->Array.DrawMethod == DRAW_ARRAYS) {
430 /* The _DrawArrays pointer is pointing at the VAO being unbound and
431 * that VAO may be in the process of being deleted. If it's not going
432 * to be deleted, this will have no effect, because the pointer needs
433 * to be updated by the VBO module anyway.
434 *
435 * Before the VBO module can update the pointer, we have to set it
436 * to NULL for drivers not to set up arrays which are not bound,
437 * or to prevent a crash if the VAO being unbound is going to be
438 * deleted.
439 */
440 ctx->Array._DrawArrays = NULL;
441 ctx->Array.DrawMethod = DRAW_NONE;
442 }
443
444 ctx->NewState |= _NEW_ARRAY;
445 _mesa_reference_vao(ctx, &ctx->Array.VAO, newObj);
446 }
447
448
449 /**
450 * ARB version of glBindVertexArray()
451 * This function behaves differently from glBindVertexArrayAPPLE() in
452 * that this function requires all ids to have been previously generated
453 * by glGenVertexArrays[APPLE]().
454 */
455 void GLAPIENTRY
456 _mesa_BindVertexArray( GLuint id )
457 {
458 GET_CURRENT_CONTEXT(ctx);
459 bind_vertex_array(ctx, id, GL_TRUE);
460 }
461
462
463 /**
464 * Bind a new array.
465 *
466 * \todo
467 * The binding could be done more efficiently by comparing the non-NULL
468 * pointers in the old and new objects. The only arrays that are "dirty" are
469 * the ones that are non-NULL in either object.
470 */
471 void GLAPIENTRY
472 _mesa_BindVertexArrayAPPLE( GLuint id )
473 {
474 GET_CURRENT_CONTEXT(ctx);
475 bind_vertex_array(ctx, id, GL_FALSE);
476 }
477
478
479 /**
480 * Delete a set of array objects.
481 *
482 * \param n Number of array objects to delete.
483 * \param ids Array of \c n array object IDs.
484 */
485 void GLAPIENTRY
486 _mesa_DeleteVertexArrays(GLsizei n, const GLuint *ids)
487 {
488 GET_CURRENT_CONTEXT(ctx);
489 GLsizei i;
490
491 if (n < 0) {
492 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArray(n)");
493 return;
494 }
495
496 for (i = 0; i < n; i++) {
497 struct gl_vertex_array_object *obj = _mesa_lookup_vao(ctx, ids[i]);
498
499 if ( obj != NULL ) {
500 assert( obj->Name == ids[i] );
501
502 /* If the array object is currently bound, the spec says "the binding
503 * for that object reverts to zero and the default vertex array
504 * becomes current."
505 */
506 if ( obj == ctx->Array.VAO ) {
507 _mesa_BindVertexArray(0);
508 }
509
510 /* The ID is immediately freed for re-use */
511 remove_array_object(ctx, obj);
512
513 if (ctx->Array.LastLookedUpVAO == obj)
514 _mesa_reference_vao(ctx, &ctx->Array.LastLookedUpVAO, NULL);
515
516 /* Unreference the array object.
517 * If refcount hits zero, the object will be deleted.
518 */
519 _mesa_reference_vao(ctx, &obj, NULL);
520 }
521 }
522 }
523
524
525 /**
526 * Generate a set of unique array object IDs and store them in \c arrays.
527 * Helper for _mesa_GenVertexArrays[APPLE]() and _mesa_CreateVertexArrays()
528 * below.
529 *
530 * \param n Number of IDs to generate.
531 * \param arrays Array of \c n locations to store the IDs.
532 * \param create Indicates that the objects should also be created.
533 * \param func The name of the GL entry point.
534 */
535 static void
536 gen_vertex_arrays(struct gl_context *ctx, GLsizei n, GLuint *arrays,
537 bool create, const char *func)
538 {
539 GLuint first;
540 GLint i;
541
542 if (n < 0) {
543 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
544 return;
545 }
546
547 if (!arrays) {
548 return;
549 }
550
551 first = _mesa_HashFindFreeKeyBlock(ctx->Array.Objects, n);
552
553 /* For the sake of simplicity we create the array objects in both
554 * the Gen* and Create* cases. The only difference is the value of
555 * EverBound, which is set to true in the Create* case.
556 */
557 for (i = 0; i < n; i++) {
558 struct gl_vertex_array_object *obj;
559 GLuint name = first + i;
560
561 obj = _mesa_new_vao(ctx, name);
562 if (!obj) {
563 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
564 return;
565 }
566 obj->EverBound = create;
567 save_array_object(ctx, obj);
568 arrays[i] = first + i;
569 }
570 }
571
572
573 /**
574 * ARB version of glGenVertexArrays()
575 * All arrays will be required to live in VBOs.
576 */
577 void GLAPIENTRY
578 _mesa_GenVertexArrays(GLsizei n, GLuint *arrays)
579 {
580 GET_CURRENT_CONTEXT(ctx);
581 gen_vertex_arrays(ctx, n, arrays, false, "glGenVertexArrays");
582 }
583
584
585 /**
586 * APPLE version of glGenVertexArraysAPPLE()
587 * Arrays may live in VBOs or ordinary memory.
588 */
589 void GLAPIENTRY
590 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
591 {
592 GET_CURRENT_CONTEXT(ctx);
593 gen_vertex_arrays(ctx, n, arrays, false, "glGenVertexArraysAPPLE");
594 }
595
596
597 /**
598 * ARB_direct_state_access
599 * Generates ID's and creates the array objects.
600 */
601 void GLAPIENTRY
602 _mesa_CreateVertexArrays(GLsizei n, GLuint *arrays)
603 {
604 GET_CURRENT_CONTEXT(ctx);
605 gen_vertex_arrays(ctx, n, arrays, true, "glCreateVertexArrays");
606 }
607
608
609 /**
610 * Determine if ID is the name of an array object.
611 *
612 * \param id ID of the potential array object.
613 * \return \c GL_TRUE if \c id is the name of a array object,
614 * \c GL_FALSE otherwise.
615 */
616 GLboolean GLAPIENTRY
617 _mesa_IsVertexArray( GLuint id )
618 {
619 struct gl_vertex_array_object * obj;
620 GET_CURRENT_CONTEXT(ctx);
621 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
622
623 if (id == 0)
624 return GL_FALSE;
625
626 obj = _mesa_lookup_vao(ctx, id);
627 if (obj == NULL)
628 return GL_FALSE;
629
630 return obj->EverBound;
631 }
632
633
634 /**
635 * Sets the element array buffer binding of a vertex array object.
636 *
637 * This is the ARB_direct_state_access equivalent of
638 * glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer).
639 */
640 void GLAPIENTRY
641 _mesa_VertexArrayElementBuffer(GLuint vaobj, GLuint buffer)
642 {
643 GET_CURRENT_CONTEXT(ctx);
644 struct gl_vertex_array_object *vao;
645 struct gl_buffer_object *bufObj;
646
647 ASSERT_OUTSIDE_BEGIN_END(ctx);
648
649 /* The GL_ARB_direct_state_access specification says:
650 *
651 * "An INVALID_OPERATION error is generated by VertexArrayElementBuffer
652 * if <vaobj> is not [compatibility profile: zero or] the name of an
653 * existing vertex array object."
654 */
655 vao =_mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayElementBuffer");
656 if (!vao)
657 return;
658
659 /* The GL_ARB_direct_state_access specification says:
660 *
661 * "An INVALID_OPERATION error is generated if <buffer> is not zero or
662 * the name of an existing buffer object."
663 */
664 if (buffer != 0)
665 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
666 "glVertexArrayElementBuffer");
667 else
668 bufObj = ctx->Shared->NullBufferObj;
669
670 if (bufObj)
671 _mesa_reference_buffer_object(ctx, &vao->IndexBufferObj, bufObj);
672 }
673
674
675 void GLAPIENTRY
676 _mesa_GetVertexArrayiv(GLuint vaobj, GLenum pname, GLint *param)
677 {
678 GET_CURRENT_CONTEXT(ctx);
679 struct gl_vertex_array_object *vao;
680
681 ASSERT_OUTSIDE_BEGIN_END(ctx);
682
683 /* The GL_ARB_direct_state_access specification says:
684 *
685 * "An INVALID_OPERATION error is generated if <vaobj> is not
686 * [compatibility profile: zero or] the name of an existing
687 * vertex array object."
688 */
689 vao =_mesa_lookup_vao_err(ctx, vaobj, "glGetVertexArrayiv");
690 if (!vao)
691 return;
692
693 /* The GL_ARB_direct_state_access specification says:
694 *
695 * "An INVALID_ENUM error is generated if <pname> is not
696 * ELEMENT_ARRAY_BUFFER_BINDING."
697 */
698 if (pname != GL_ELEMENT_ARRAY_BUFFER_BINDING) {
699 _mesa_error(ctx, GL_INVALID_ENUM,
700 "glGetVertexArrayiv(pname != "
701 "GL_ELEMENT_ARRAY_BUFFER_BINDING)");
702 return;
703 }
704
705 param[0] = vao->IndexBufferObj->Name;
706 }