mesa: ignore VAO IDs equal to 0 in glDeleteVertexArrays
[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.
33 *
34 * \todo
35 * The code in this file borrows a lot from bufferobj.c. There's a certain
36 * amount of cruft left over from that origin that may be unnecessary.
37 *
38 * \author Ian Romanick <idr@us.ibm.com>
39 * \author Brian Paul
40 */
41
42
43 #include "glheader.h"
44 #include "hash.h"
45 #include "image.h"
46 #include "imports.h"
47 #include "context.h"
48 #include "bufferobj.h"
49 #include "arrayobj.h"
50 #include "macros.h"
51 #include "mtypes.h"
52 #include "state.h"
53 #include "varray.h"
54 #include "util/bitscan.h"
55 #include "util/u_atomic.h"
56
57
58 const GLubyte
59 _mesa_vao_attribute_map[ATTRIBUTE_MAP_MODE_MAX][VERT_ATTRIB_MAX] =
60 {
61 /* ATTRIBUTE_MAP_MODE_IDENTITY
62 *
63 * Grab vertex processing attribute VERT_ATTRIB_POS from
64 * the VAO attribute VERT_ATTRIB_POS, and grab vertex processing
65 * attribute VERT_ATTRIB_GENERIC0 from the VAO attribute
66 * VERT_ATTRIB_GENERIC0.
67 */
68 {
69 VERT_ATTRIB_POS, /* VERT_ATTRIB_POS */
70 VERT_ATTRIB_NORMAL, /* VERT_ATTRIB_NORMAL */
71 VERT_ATTRIB_COLOR0, /* VERT_ATTRIB_COLOR0 */
72 VERT_ATTRIB_COLOR1, /* VERT_ATTRIB_COLOR1 */
73 VERT_ATTRIB_FOG, /* VERT_ATTRIB_FOG */
74 VERT_ATTRIB_COLOR_INDEX, /* VERT_ATTRIB_COLOR_INDEX */
75 VERT_ATTRIB_EDGEFLAG, /* VERT_ATTRIB_EDGEFLAG */
76 VERT_ATTRIB_TEX0, /* VERT_ATTRIB_TEX0 */
77 VERT_ATTRIB_TEX1, /* VERT_ATTRIB_TEX1 */
78 VERT_ATTRIB_TEX2, /* VERT_ATTRIB_TEX2 */
79 VERT_ATTRIB_TEX3, /* VERT_ATTRIB_TEX3 */
80 VERT_ATTRIB_TEX4, /* VERT_ATTRIB_TEX4 */
81 VERT_ATTRIB_TEX5, /* VERT_ATTRIB_TEX5 */
82 VERT_ATTRIB_TEX6, /* VERT_ATTRIB_TEX6 */
83 VERT_ATTRIB_TEX7, /* VERT_ATTRIB_TEX7 */
84 VERT_ATTRIB_POINT_SIZE, /* VERT_ATTRIB_POINT_SIZE */
85 VERT_ATTRIB_GENERIC0, /* VERT_ATTRIB_GENERIC0 */
86 VERT_ATTRIB_GENERIC1, /* VERT_ATTRIB_GENERIC1 */
87 VERT_ATTRIB_GENERIC2, /* VERT_ATTRIB_GENERIC2 */
88 VERT_ATTRIB_GENERIC3, /* VERT_ATTRIB_GENERIC3 */
89 VERT_ATTRIB_GENERIC4, /* VERT_ATTRIB_GENERIC4 */
90 VERT_ATTRIB_GENERIC5, /* VERT_ATTRIB_GENERIC5 */
91 VERT_ATTRIB_GENERIC6, /* VERT_ATTRIB_GENERIC6 */
92 VERT_ATTRIB_GENERIC7, /* VERT_ATTRIB_GENERIC7 */
93 VERT_ATTRIB_GENERIC8, /* VERT_ATTRIB_GENERIC8 */
94 VERT_ATTRIB_GENERIC9, /* VERT_ATTRIB_GENERIC9 */
95 VERT_ATTRIB_GENERIC10, /* VERT_ATTRIB_GENERIC10 */
96 VERT_ATTRIB_GENERIC11, /* VERT_ATTRIB_GENERIC11 */
97 VERT_ATTRIB_GENERIC12, /* VERT_ATTRIB_GENERIC12 */
98 VERT_ATTRIB_GENERIC13, /* VERT_ATTRIB_GENERIC13 */
99 VERT_ATTRIB_GENERIC14, /* VERT_ATTRIB_GENERIC14 */
100 VERT_ATTRIB_GENERIC15 /* VERT_ATTRIB_GENERIC15 */
101 },
102
103 /* ATTRIBUTE_MAP_MODE_POSITION
104 *
105 * Grab vertex processing attribute VERT_ATTRIB_POS as well as
106 * vertex processing attribute VERT_ATTRIB_GENERIC0 from the
107 * VAO attribute VERT_ATTRIB_POS.
108 */
109 {
110 VERT_ATTRIB_POS, /* VERT_ATTRIB_POS */
111 VERT_ATTRIB_NORMAL, /* VERT_ATTRIB_NORMAL */
112 VERT_ATTRIB_COLOR0, /* VERT_ATTRIB_COLOR0 */
113 VERT_ATTRIB_COLOR1, /* VERT_ATTRIB_COLOR1 */
114 VERT_ATTRIB_FOG, /* VERT_ATTRIB_FOG */
115 VERT_ATTRIB_COLOR_INDEX, /* VERT_ATTRIB_COLOR_INDEX */
116 VERT_ATTRIB_EDGEFLAG, /* VERT_ATTRIB_EDGEFLAG */
117 VERT_ATTRIB_TEX0, /* VERT_ATTRIB_TEX0 */
118 VERT_ATTRIB_TEX1, /* VERT_ATTRIB_TEX1 */
119 VERT_ATTRIB_TEX2, /* VERT_ATTRIB_TEX2 */
120 VERT_ATTRIB_TEX3, /* VERT_ATTRIB_TEX3 */
121 VERT_ATTRIB_TEX4, /* VERT_ATTRIB_TEX4 */
122 VERT_ATTRIB_TEX5, /* VERT_ATTRIB_TEX5 */
123 VERT_ATTRIB_TEX6, /* VERT_ATTRIB_TEX6 */
124 VERT_ATTRIB_TEX7, /* VERT_ATTRIB_TEX7 */
125 VERT_ATTRIB_POINT_SIZE, /* VERT_ATTRIB_POINT_SIZE */
126 VERT_ATTRIB_POS, /* VERT_ATTRIB_GENERIC0 */
127 VERT_ATTRIB_GENERIC1, /* VERT_ATTRIB_GENERIC1 */
128 VERT_ATTRIB_GENERIC2, /* VERT_ATTRIB_GENERIC2 */
129 VERT_ATTRIB_GENERIC3, /* VERT_ATTRIB_GENERIC3 */
130 VERT_ATTRIB_GENERIC4, /* VERT_ATTRIB_GENERIC4 */
131 VERT_ATTRIB_GENERIC5, /* VERT_ATTRIB_GENERIC5 */
132 VERT_ATTRIB_GENERIC6, /* VERT_ATTRIB_GENERIC6 */
133 VERT_ATTRIB_GENERIC7, /* VERT_ATTRIB_GENERIC7 */
134 VERT_ATTRIB_GENERIC8, /* VERT_ATTRIB_GENERIC8 */
135 VERT_ATTRIB_GENERIC9, /* VERT_ATTRIB_GENERIC9 */
136 VERT_ATTRIB_GENERIC10, /* VERT_ATTRIB_GENERIC10 */
137 VERT_ATTRIB_GENERIC11, /* VERT_ATTRIB_GENERIC11 */
138 VERT_ATTRIB_GENERIC12, /* VERT_ATTRIB_GENERIC12 */
139 VERT_ATTRIB_GENERIC13, /* VERT_ATTRIB_GENERIC13 */
140 VERT_ATTRIB_GENERIC14, /* VERT_ATTRIB_GENERIC14 */
141 VERT_ATTRIB_GENERIC15 /* VERT_ATTRIB_GENERIC15 */
142 },
143
144 /* ATTRIBUTE_MAP_MODE_GENERIC0
145 *
146 * Grab vertex processing attribute VERT_ATTRIB_POS as well as
147 * vertex processing attribute VERT_ATTRIB_GENERIC0 from the
148 * VAO attribute VERT_ATTRIB_GENERIC0.
149 */
150 {
151 VERT_ATTRIB_GENERIC0, /* VERT_ATTRIB_POS */
152 VERT_ATTRIB_NORMAL, /* VERT_ATTRIB_NORMAL */
153 VERT_ATTRIB_COLOR0, /* VERT_ATTRIB_COLOR0 */
154 VERT_ATTRIB_COLOR1, /* VERT_ATTRIB_COLOR1 */
155 VERT_ATTRIB_FOG, /* VERT_ATTRIB_FOG */
156 VERT_ATTRIB_COLOR_INDEX, /* VERT_ATTRIB_COLOR_INDEX */
157 VERT_ATTRIB_EDGEFLAG, /* VERT_ATTRIB_EDGEFLAG */
158 VERT_ATTRIB_TEX0, /* VERT_ATTRIB_TEX0 */
159 VERT_ATTRIB_TEX1, /* VERT_ATTRIB_TEX1 */
160 VERT_ATTRIB_TEX2, /* VERT_ATTRIB_TEX2 */
161 VERT_ATTRIB_TEX3, /* VERT_ATTRIB_TEX3 */
162 VERT_ATTRIB_TEX4, /* VERT_ATTRIB_TEX4 */
163 VERT_ATTRIB_TEX5, /* VERT_ATTRIB_TEX5 */
164 VERT_ATTRIB_TEX6, /* VERT_ATTRIB_TEX6 */
165 VERT_ATTRIB_TEX7, /* VERT_ATTRIB_TEX7 */
166 VERT_ATTRIB_POINT_SIZE, /* VERT_ATTRIB_POINT_SIZE */
167 VERT_ATTRIB_GENERIC0, /* VERT_ATTRIB_GENERIC0 */
168 VERT_ATTRIB_GENERIC1, /* VERT_ATTRIB_GENERIC1 */
169 VERT_ATTRIB_GENERIC2, /* VERT_ATTRIB_GENERIC2 */
170 VERT_ATTRIB_GENERIC3, /* VERT_ATTRIB_GENERIC3 */
171 VERT_ATTRIB_GENERIC4, /* VERT_ATTRIB_GENERIC4 */
172 VERT_ATTRIB_GENERIC5, /* VERT_ATTRIB_GENERIC5 */
173 VERT_ATTRIB_GENERIC6, /* VERT_ATTRIB_GENERIC6 */
174 VERT_ATTRIB_GENERIC7, /* VERT_ATTRIB_GENERIC7 */
175 VERT_ATTRIB_GENERIC8, /* VERT_ATTRIB_GENERIC8 */
176 VERT_ATTRIB_GENERIC9, /* VERT_ATTRIB_GENERIC9 */
177 VERT_ATTRIB_GENERIC10, /* VERT_ATTRIB_GENERIC10 */
178 VERT_ATTRIB_GENERIC11, /* VERT_ATTRIB_GENERIC11 */
179 VERT_ATTRIB_GENERIC12, /* VERT_ATTRIB_GENERIC12 */
180 VERT_ATTRIB_GENERIC13, /* VERT_ATTRIB_GENERIC13 */
181 VERT_ATTRIB_GENERIC14, /* VERT_ATTRIB_GENERIC14 */
182 VERT_ATTRIB_GENERIC15 /* VERT_ATTRIB_GENERIC15 */
183 }
184 };
185
186
187 /**
188 * Look up the array object for the given ID.
189 *
190 * \returns
191 * Either a pointer to the array object with the specified ID or \c NULL for
192 * a non-existent ID. The spec defines ID 0 as being technically
193 * non-existent.
194 */
195
196 struct gl_vertex_array_object *
197 _mesa_lookup_vao(struct gl_context *ctx, GLuint id)
198 {
199 /* The ARB_direct_state_access specification says:
200 *
201 * "<vaobj> is [compatibility profile:
202 * zero, indicating the default vertex array object, or]
203 * the name of the vertex array object."
204 */
205 if (id == 0) {
206 if (ctx->API == API_OPENGL_COMPAT)
207 return ctx->Array.DefaultVAO;
208
209 return NULL;
210 } else {
211 struct gl_vertex_array_object *vao;
212
213 if (ctx->Array.LastLookedUpVAO &&
214 ctx->Array.LastLookedUpVAO->Name == id) {
215 vao = ctx->Array.LastLookedUpVAO;
216 } else {
217 vao = (struct gl_vertex_array_object *)
218 _mesa_HashLookupLocked(ctx->Array.Objects, id);
219
220 _mesa_reference_vao(ctx, &ctx->Array.LastLookedUpVAO, vao);
221 }
222
223 return vao;
224 }
225 }
226
227
228 /**
229 * Looks up the array object for the given ID.
230 *
231 * Unlike _mesa_lookup_vao, this function generates a GL_INVALID_OPERATION
232 * error if the array object does not exist. It also returns the default
233 * array object when ctx is a compatibility profile context and id is zero.
234 */
235 struct gl_vertex_array_object *
236 _mesa_lookup_vao_err(struct gl_context *ctx, GLuint id, const char *caller)
237 {
238 /* The ARB_direct_state_access specification says:
239 *
240 * "<vaobj> is [compatibility profile:
241 * zero, indicating the default vertex array object, or]
242 * the name of the vertex array object."
243 */
244 if (id == 0) {
245 if (ctx->API == API_OPENGL_CORE) {
246 _mesa_error(ctx, GL_INVALID_OPERATION,
247 "%s(zero is not valid vaobj name in a core profile "
248 "context)", caller);
249 return NULL;
250 }
251
252 return ctx->Array.DefaultVAO;
253 } else {
254 struct gl_vertex_array_object *vao;
255
256 if (ctx->Array.LastLookedUpVAO &&
257 ctx->Array.LastLookedUpVAO->Name == id) {
258 vao = ctx->Array.LastLookedUpVAO;
259 } else {
260 vao = (struct gl_vertex_array_object *)
261 _mesa_HashLookupLocked(ctx->Array.Objects, id);
262
263 /* The ARB_direct_state_access specification says:
264 *
265 * "An INVALID_OPERATION error is generated if <vaobj> is not
266 * [compatibility profile: zero or] the name of an existing
267 * vertex array object."
268 */
269 if (!vao || !vao->EverBound) {
270 _mesa_error(ctx, GL_INVALID_OPERATION,
271 "%s(non-existent vaobj=%u)", caller, id);
272 return NULL;
273 }
274
275 _mesa_reference_vao(ctx, &ctx->Array.LastLookedUpVAO, vao);
276 }
277
278 return vao;
279 }
280 }
281
282
283 /**
284 * For all the vertex binding points in the array object, unbind any pointers
285 * to any buffer objects (VBOs).
286 * This is done just prior to array object destruction.
287 */
288 static void
289 unbind_array_object_vbos(struct gl_context *ctx, struct gl_vertex_array_object *obj)
290 {
291 GLuint i;
292
293 for (i = 0; i < ARRAY_SIZE(obj->BufferBinding); i++)
294 _mesa_reference_buffer_object(ctx, &obj->BufferBinding[i].BufferObj, NULL);
295 }
296
297
298 /**
299 * Allocate and initialize a new vertex array object.
300 */
301 struct gl_vertex_array_object *
302 _mesa_new_vao(struct gl_context *ctx, GLuint name)
303 {
304 struct gl_vertex_array_object *obj = CALLOC_STRUCT(gl_vertex_array_object);
305 if (obj)
306 _mesa_initialize_vao(ctx, obj, name);
307 return obj;
308 }
309
310
311 /**
312 * Delete an array object.
313 */
314 void
315 _mesa_delete_vao(struct gl_context *ctx, struct gl_vertex_array_object *obj)
316 {
317 unbind_array_object_vbos(ctx, obj);
318 _mesa_reference_buffer_object(ctx, &obj->IndexBufferObj, NULL);
319 free(obj->Label);
320 free(obj);
321 }
322
323
324 /**
325 * Set ptr to vao w/ reference counting.
326 * Note: this should only be called from the _mesa_reference_vao()
327 * inline function.
328 */
329 void
330 _mesa_reference_vao_(struct gl_context *ctx,
331 struct gl_vertex_array_object **ptr,
332 struct gl_vertex_array_object *vao)
333 {
334 assert(*ptr != vao);
335
336 if (*ptr) {
337 /* Unreference the old array object */
338 struct gl_vertex_array_object *oldObj = *ptr;
339
340 bool deleteFlag;
341 if (oldObj->SharedAndImmutable) {
342 deleteFlag = p_atomic_dec_zero(&oldObj->RefCount);
343 } else {
344 assert(oldObj->RefCount > 0);
345 oldObj->RefCount--;
346 deleteFlag = (oldObj->RefCount == 0);
347 }
348
349 if (deleteFlag)
350 _mesa_delete_vao(ctx, oldObj);
351
352 *ptr = NULL;
353 }
354 assert(!*ptr);
355
356 if (vao) {
357 /* reference new array object */
358 if (vao->SharedAndImmutable) {
359 p_atomic_inc(&vao->RefCount);
360 } else {
361 assert(vao->RefCount > 0);
362 vao->RefCount++;
363 }
364
365 *ptr = vao;
366 }
367 }
368
369
370 /**
371 * Initialize attributes of a vertex array within a vertex array object.
372 * \param vao the container vertex array object
373 * \param index which array in the VAO to initialize
374 * \param size number of components (1, 2, 3 or 4) per attribute
375 * \param type datatype of the attribute (GL_FLOAT, GL_INT, etc).
376 */
377 static void
378 init_array(struct gl_context *ctx,
379 struct gl_vertex_array_object *vao,
380 gl_vert_attrib index, GLint size, GLint type)
381 {
382 assert(index < ARRAY_SIZE(vao->VertexAttrib));
383 struct gl_array_attributes *array = &vao->VertexAttrib[index];
384 assert(index < ARRAY_SIZE(vao->BufferBinding));
385 struct gl_vertex_buffer_binding *binding = &vao->BufferBinding[index];
386
387 array->Size = size;
388 array->Type = type;
389 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
390 array->Stride = 0;
391 array->Ptr = NULL;
392 array->RelativeOffset = 0;
393 array->Enabled = GL_FALSE;
394 array->Normalized = GL_FALSE;
395 array->Integer = GL_FALSE;
396 array->Doubles = GL_FALSE;
397 array->_ElementSize = size * _mesa_sizeof_type(type);
398 ASSERT_BITFIELD_SIZE(struct gl_array_attributes, BufferBindingIndex,
399 VERT_ATTRIB_MAX - 1);
400 array->BufferBindingIndex = index;
401
402 binding->Offset = 0;
403 binding->Stride = array->_ElementSize;
404 binding->BufferObj = NULL;
405 binding->_BoundArrays = BITFIELD_BIT(index);
406
407 /* Vertex array buffers */
408 _mesa_reference_buffer_object(ctx, &binding->BufferObj,
409 ctx->Shared->NullBufferObj);
410 }
411
412
413 /**
414 * Initialize a gl_vertex_array_object's arrays.
415 */
416 void
417 _mesa_initialize_vao(struct gl_context *ctx,
418 struct gl_vertex_array_object *vao,
419 GLuint name)
420 {
421 GLuint i;
422
423 vao->Name = name;
424
425 vao->RefCount = 1;
426 vao->SharedAndImmutable = false;
427
428 /* Init the individual arrays */
429 for (i = 0; i < ARRAY_SIZE(vao->VertexAttrib); i++) {
430 switch (i) {
431 case VERT_ATTRIB_NORMAL:
432 init_array(ctx, vao, VERT_ATTRIB_NORMAL, 3, GL_FLOAT);
433 break;
434 case VERT_ATTRIB_COLOR1:
435 init_array(ctx, vao, VERT_ATTRIB_COLOR1, 3, GL_FLOAT);
436 break;
437 case VERT_ATTRIB_FOG:
438 init_array(ctx, vao, VERT_ATTRIB_FOG, 1, GL_FLOAT);
439 break;
440 case VERT_ATTRIB_COLOR_INDEX:
441 init_array(ctx, vao, VERT_ATTRIB_COLOR_INDEX, 1, GL_FLOAT);
442 break;
443 case VERT_ATTRIB_EDGEFLAG:
444 init_array(ctx, vao, VERT_ATTRIB_EDGEFLAG, 1, GL_BOOL);
445 break;
446 case VERT_ATTRIB_POINT_SIZE:
447 init_array(ctx, vao, VERT_ATTRIB_POINT_SIZE, 1, GL_FLOAT);
448 break;
449 default:
450 init_array(ctx, vao, i, 4, GL_FLOAT);
451 break;
452 }
453 }
454
455 vao->_AttributeMapMode = ATTRIBUTE_MAP_MODE_IDENTITY;
456
457 _mesa_reference_buffer_object(ctx, &vao->IndexBufferObj,
458 ctx->Shared->NullBufferObj);
459 }
460
461
462 /**
463 * Compute the offset range for the provided binding.
464 *
465 * This is a helper function for the below.
466 */
467 static void
468 compute_vbo_offset_range(const struct gl_vertex_array_object *vao,
469 const struct gl_vertex_buffer_binding *binding,
470 GLsizeiptr* min, GLsizeiptr* max)
471 {
472 /* The function is meant to work on VBO bindings */
473 assert(_mesa_is_bufferobj(binding->BufferObj));
474
475 /* Start with an inverted range of relative offsets. */
476 GLuint min_offset = ~(GLuint)0;
477 GLuint max_offset = 0;
478
479 /* We work on the unmapped originaly VAO array entries. */
480 GLbitfield mask = vao->_Enabled & binding->_BoundArrays;
481 /* The binding should be active somehow, not to return inverted ranges */
482 assert(mask);
483 while (mask) {
484 const int i = u_bit_scan(&mask);
485 const GLuint off = vao->VertexAttrib[i].RelativeOffset;
486 min_offset = MIN2(off, min_offset);
487 max_offset = MAX2(off, max_offset);
488 }
489
490 *min = binding->Offset + (GLsizeiptr)min_offset;
491 *max = binding->Offset + (GLsizeiptr)max_offset;
492 }
493
494
495 /**
496 * Update the unique binding and pos/generic0 map tracking in the vao.
497 *
498 * The idea is to build up information in the vao so that a consuming
499 * backend can execute the following to set up buffer and vertex element
500 * information:
501 *
502 * const GLbitfield inputs_read = VERT_BIT_ALL; // backend vp inputs
503 *
504 * // Attribute data is in a VBO.
505 * GLbitfield vbomask = inputs_read & _mesa_draw_vbo_array_bits(ctx);
506 * while (vbomask) {
507 * // The attribute index to start pulling a binding
508 * const gl_vert_attrib i = ffs(vbomask) - 1;
509 * const struct gl_vertex_buffer_binding *const binding
510 * = _mesa_draw_buffer_binding(vao, i);
511 *
512 * <insert code to handle the vertex buffer object at binding>
513 *
514 * const GLbitfield boundmask = _mesa_draw_bound_attrib_bits(binding);
515 * GLbitfield attrmask = vbomask & boundmask;
516 * assert(attrmask);
517 * // Walk attributes belonging to the binding
518 * while (attrmask) {
519 * const gl_vert_attrib attr = u_bit_scan(&attrmask);
520 * const struct gl_array_attributes *const attrib
521 * = _mesa_draw_array_attrib(vao, attr);
522 *
523 * <insert code to handle the vertex element refering to the binding>
524 * }
525 * vbomask &= ~boundmask;
526 * }
527 *
528 * // Process user space buffers
529 * GLbitfield usermask = inputs_read & _mesa_draw_user_array_bits(ctx);
530 * while (usermask) {
531 * // The attribute index to start pulling a binding
532 * const gl_vert_attrib i = ffs(usermask) - 1;
533 * const struct gl_vertex_buffer_binding *const binding
534 * = _mesa_draw_buffer_binding(vao, i);
535 *
536 * <insert code to handle a set of interleaved user space arrays at binding>
537 *
538 * const GLbitfield boundmask = _mesa_draw_bound_attrib_bits(binding);
539 * GLbitfield attrmask = usermask & boundmask;
540 * assert(attrmask);
541 * // Walk interleaved attributes with a common stride and instance divisor
542 * while (attrmask) {
543 * const gl_vert_attrib attr = u_bit_scan(&attrmask);
544 * const struct gl_array_attributes *const attrib
545 * = _mesa_draw_array_attrib(vao, attr);
546 *
547 * <insert code to handle non vbo vertex arrays>
548 * }
549 * usermask &= ~boundmask;
550 * }
551 *
552 * // Process values that should have better been uniforms in the application
553 * GLbitfield curmask = inputs_read & _mesa_draw_current_bits(ctx);
554 * while (curmask) {
555 * const gl_vert_attrib attr = u_bit_scan(&curmask);
556 * const struct gl_array_attributes *const attrib
557 * = _mesa_draw_current_attrib(ctx, attr);
558 *
559 * <insert code to handle current values>
560 * }
561 *
562 *
563 * Note that the scan below must not incoporate any context state.
564 * The rationale is that once a VAO is finalized it should not
565 * be touched anymore. That means, do not incorporate the
566 * gl_context::Array._DrawVAOEnabledAttribs bitmask into this scan.
567 * A backend driver may further reduce the handled vertex processing
568 * inputs based on their vertex shader inputs. But scanning for
569 * collapsable binding points to reduce relocs is done based on the
570 * enabled arrays.
571 * Also VAOs may be shared between contexts due to their use in dlists
572 * thus no context state should bleed into the VAO.
573 */
574 void
575 _mesa_update_vao_derived_arrays(struct gl_context *ctx,
576 struct gl_vertex_array_object *vao)
577 {
578 /* Make sure we do not run into problems with shared objects */
579 assert(!vao->SharedAndImmutable || vao->NewArrays == 0);
580
581 /* Limit used for common binding scanning below. */
582 const GLsizeiptr MaxRelativeOffset =
583 ctx->Const.MaxVertexAttribRelativeOffset;
584
585 /* The gl_vertex_array_object::_AttributeMapMode denotes the way
586 * VERT_ATTRIB_{POS,GENERIC0} mapping is done.
587 *
588 * This mapping is used to map between the OpenGL api visible
589 * VERT_ATTRIB_* arrays to mesa driver arrayinputs or shader inputs.
590 * The mapping only depends on the enabled bits of the
591 * VERT_ATTRIB_{POS,GENERIC0} arrays and is tracked in the VAO.
592 *
593 * This map needs to be applied when finally translating to the bitmasks
594 * as consumed by the driver backends. The duplicate scanning is here
595 * can as well be done in the OpenGL API numbering without this map.
596 */
597 const gl_attribute_map_mode mode = vao->_AttributeMapMode;
598 /* Enabled array bits. */
599 const GLbitfield enabled = vao->_Enabled;
600 /* VBO array bits. */
601 const GLbitfield vbos = vao->VertexAttribBufferMask;
602
603 /* Compute and store effectively enabled and mapped vbo arrays */
604 vao->_EffEnabledVBO = _mesa_vao_enable_to_vp_inputs(mode, enabled & vbos);
605 /* Walk those enabled arrays that have a real vbo attached */
606 GLbitfield mask = enabled;
607 while (mask) {
608 /* Do not use u_bit_scan as we can walk multiple attrib arrays at once */
609 const int i = ffs(mask) - 1;
610 /* The binding from the first to be processed attribute. */
611 const GLuint bindex = vao->VertexAttrib[i].BufferBindingIndex;
612 struct gl_vertex_buffer_binding *binding = &vao->BufferBinding[bindex];
613
614 /* The scan goes different for user space arrays than vbos */
615 if (_mesa_is_bufferobj(binding->BufferObj)) {
616 /* The bound arrays. */
617 const GLbitfield bound = enabled & binding->_BoundArrays;
618
619 /* Start this current effective binding with the actual bound arrays */
620 GLbitfield eff_bound_arrays = bound;
621
622 /*
623 * If there is nothing left to scan just update the effective binding
624 * information. If the VAO is already only using a single binding point
625 * we end up here. So the overhead of this scan for an application
626 * carefully preparing the VAO for draw is low.
627 */
628
629 GLbitfield scanmask = mask & vbos & ~bound;
630 /* Is there something left to scan? */
631 if (scanmask == 0) {
632 /* Just update the back reference from the attrib to the binding and
633 * the effective offset.
634 */
635 GLbitfield attrmask = eff_bound_arrays;
636 while (attrmask) {
637 const int j = u_bit_scan(&attrmask);
638 struct gl_array_attributes *attrib2 = &vao->VertexAttrib[j];
639
640 /* Update the index into the common binding point and offset */
641 attrib2->_EffBufferBindingIndex = bindex;
642 attrib2->_EffRelativeOffset = attrib2->RelativeOffset;
643 assert(attrib2->_EffRelativeOffset <= MaxRelativeOffset);
644
645 /* Only enabled arrays shall appear in the unique bindings */
646 assert(attrib2->Enabled);
647 }
648 /* Finally this is the set of effectively bound arrays with the
649 * original binding offset.
650 */
651 binding->_EffOffset = binding->Offset;
652 /* The bound arrays past the VERT_ATTRIB_{POS,GENERIC0} mapping. */
653 binding->_EffBoundArrays =
654 _mesa_vao_enable_to_vp_inputs(mode, eff_bound_arrays);
655
656 } else {
657 /* In the VBO case, scan for attribute/binding
658 * combinations with relative bindings in the range of
659 * [0, ctx->Const.MaxVertexAttribRelativeOffset].
660 * Note that this does also go beyond just interleaved arrays
661 * as long as they use the same VBO, binding parameters and the
662 * offsets stay within bounds that the backend still can handle.
663 */
664
665 GLsizeiptr min_offset, max_offset;
666 compute_vbo_offset_range(vao, binding, &min_offset, &max_offset);
667 assert(max_offset <= min_offset + MaxRelativeOffset);
668
669 /* Now scan. */
670 while (scanmask) {
671 /* Do not use u_bit_scan as we can walk multiple
672 * attrib arrays at once
673 */
674 const int j = ffs(scanmask) - 1;
675 const struct gl_array_attributes *attrib2 =
676 &vao->VertexAttrib[j];
677 const struct gl_vertex_buffer_binding *binding2 =
678 &vao->BufferBinding[attrib2->BufferBindingIndex];
679
680 /* Remove those attrib bits from the mask that are bound to the
681 * same effective binding point.
682 */
683 const GLbitfield bound2 = enabled & binding2->_BoundArrays;
684 scanmask &= ~bound2;
685
686 /* Check if we have an identical binding */
687 if (binding->Stride != binding2->Stride)
688 continue;
689 if (binding->InstanceDivisor != binding2->InstanceDivisor)
690 continue;
691 if (binding->BufferObj != binding2->BufferObj)
692 continue;
693 /* Check if we can fold both bindings into a common binding */
694 GLsizeiptr min_offset2, max_offset2;
695 compute_vbo_offset_range(vao, binding2,
696 &min_offset2, &max_offset2);
697 /* If the relative offset is within the limits ... */
698 if (min_offset + MaxRelativeOffset < max_offset2)
699 continue;
700 if (min_offset2 + MaxRelativeOffset < max_offset)
701 continue;
702 /* ... add this array to the effective binding */
703 eff_bound_arrays |= bound2;
704 min_offset = MIN2(min_offset, min_offset2);
705 max_offset = MAX2(max_offset, max_offset2);
706 assert(max_offset <= min_offset + MaxRelativeOffset);
707 }
708
709 /* Update the back reference from the attrib to the binding */
710 GLbitfield attrmask = eff_bound_arrays;
711 while (attrmask) {
712 const int j = u_bit_scan(&attrmask);
713 struct gl_array_attributes *attrib2 = &vao->VertexAttrib[j];
714 const struct gl_vertex_buffer_binding *binding2 =
715 &vao->BufferBinding[attrib2->BufferBindingIndex];
716
717 /* Update the index into the common binding point and offset */
718 attrib2->_EffBufferBindingIndex = bindex;
719 attrib2->_EffRelativeOffset =
720 binding2->Offset + attrib2->RelativeOffset - min_offset;
721 assert(attrib2->_EffRelativeOffset <= MaxRelativeOffset);
722
723 /* Only enabled arrays shall appear in the unique bindings */
724 assert(attrib2->Enabled);
725 }
726 /* Finally this is the set of effectively bound arrays */
727 binding->_EffOffset = min_offset;
728 /* The bound arrays past the VERT_ATTRIB_{POS,GENERIC0} mapping. */
729 binding->_EffBoundArrays =
730 _mesa_vao_enable_to_vp_inputs(mode, eff_bound_arrays);
731 }
732
733 /* Mark all the effective bound arrays as processed. */
734 mask &= ~eff_bound_arrays;
735
736 } else {
737 /* Scanning of common bindings for user space arrays.
738 */
739
740 const struct gl_array_attributes *attrib = &vao->VertexAttrib[i];
741 const GLbitfield bound = VERT_BIT(i);
742
743 /* Note that user space array pointers can only happen using a one
744 * to one binding point to array mapping.
745 * The OpenGL 4.x/ARB_vertex_attrib_binding api does not support
746 * user space arrays collected at multiple binding points.
747 * The only provider of user space interleaved arrays with a single
748 * binding point is the mesa internal vbo module. But that one
749 * provides a perfect interleaved set of arrays.
750 *
751 * If this would not be true we would potentially get attribute arrays
752 * with user space pointers that may not lie within the
753 * MaxRelativeOffset range but still attached to a single binding.
754 * Then we would need to store the effective attribute and binding
755 * grouping information in a seperate array beside
756 * gl_array_attributes/gl_vertex_buffer_binding.
757 */
758 assert(_mesa_bitcount(binding->_BoundArrays & vao->_Enabled) == 1
759 || (vao->_Enabled & ~binding->_BoundArrays) == 0);
760
761 /* Start this current effective binding with the array */
762 GLbitfield eff_bound_arrays = bound;
763
764 const GLubyte *ptr = attrib->Ptr;
765 unsigned vertex_end = attrib->_ElementSize;
766
767 /* Walk other user space arrays and see which are interleaved
768 * using the same binding parameters.
769 */
770 GLbitfield scanmask = mask & ~vbos & ~bound;
771 while (scanmask) {
772 const int j = u_bit_scan(&scanmask);
773 const struct gl_array_attributes *attrib2 = &vao->VertexAttrib[j];
774 const struct gl_vertex_buffer_binding *binding2 =
775 &vao->BufferBinding[attrib2->BufferBindingIndex];
776
777 /* See the comment at the same assert above. */
778 assert(_mesa_bitcount(binding2->_BoundArrays & vao->_Enabled) == 1
779 || (vao->_Enabled & ~binding->_BoundArrays) == 0);
780
781 /* Check if we have an identical binding */
782 if (binding->Stride != binding2->Stride)
783 continue;
784 if (binding->InstanceDivisor != binding2->InstanceDivisor)
785 continue;
786 if (ptr <= attrib2->Ptr) {
787 if (ptr + binding->Stride < attrib2->Ptr + attrib2->_ElementSize)
788 continue;
789 unsigned end = attrib2->Ptr + attrib2->_ElementSize - ptr;
790 vertex_end = MAX2(vertex_end, end);
791 } else {
792 if (attrib2->Ptr + binding->Stride < ptr + vertex_end)
793 continue;
794 vertex_end += (GLsizei)(ptr - attrib2->Ptr);
795 ptr = attrib2->Ptr;
796 }
797
798 /* User space buffer object */
799 assert(!_mesa_is_bufferobj(binding2->BufferObj));
800
801 eff_bound_arrays |= VERT_BIT(j);
802 }
803
804 /* Update the back reference from the attrib to the binding */
805 GLbitfield attrmask = eff_bound_arrays;
806 while (attrmask) {
807 const int j = u_bit_scan(&attrmask);
808 struct gl_array_attributes *attrib2 = &vao->VertexAttrib[j];
809
810 /* Update the index into the common binding point and the offset */
811 attrib2->_EffBufferBindingIndex = bindex;
812 attrib2->_EffRelativeOffset = attrib2->Ptr - ptr;
813 assert(attrib2->_EffRelativeOffset <= binding->Stride);
814
815 /* Only enabled arrays shall appear in the unique bindings */
816 assert(attrib2->Enabled);
817 }
818 /* Finally this is the set of effectively bound arrays */
819 binding->_EffOffset = (GLintptr)ptr;
820 /* The bound arrays past the VERT_ATTRIB_{POS,GENERIC0} mapping. */
821 binding->_EffBoundArrays =
822 _mesa_vao_enable_to_vp_inputs(mode, eff_bound_arrays);
823
824 /* Mark all the effective bound arrays as processed. */
825 mask &= ~eff_bound_arrays;
826 }
827 }
828
829 #ifndef NDEBUG
830 /* Make sure the above code works as expected. */
831 for (gl_vert_attrib attr = 0; attr < VERT_ATTRIB_MAX; ++attr) {
832 /* Query the original api defined attrib/binding information ... */
833 const unsigned char *const map =_mesa_vao_attribute_map[mode];
834 const struct gl_array_attributes *attrib = &vao->VertexAttrib[map[attr]];
835 if (attrib->Enabled) {
836 const struct gl_vertex_buffer_binding *binding =
837 &vao->BufferBinding[attrib->BufferBindingIndex];
838 /* ... and compare that with the computed attrib/binding */
839 const struct gl_vertex_buffer_binding *binding2 =
840 &vao->BufferBinding[attrib->_EffBufferBindingIndex];
841 assert(binding->Stride == binding2->Stride);
842 assert(binding->InstanceDivisor == binding2->InstanceDivisor);
843 assert(binding->BufferObj == binding2->BufferObj);
844 if (_mesa_is_bufferobj(binding->BufferObj)) {
845 assert(attrib->_EffRelativeOffset <= MaxRelativeOffset);
846 assert(binding->Offset + attrib->RelativeOffset ==
847 binding2->_EffOffset + attrib->_EffRelativeOffset);
848 } else {
849 assert(attrib->_EffRelativeOffset < binding->Stride);
850 assert((GLintptr)attrib->Ptr ==
851 binding2->_EffOffset + attrib->_EffRelativeOffset);
852 }
853 }
854 }
855 #endif
856 }
857
858
859 void
860 _mesa_set_vao_immutable(struct gl_context *ctx,
861 struct gl_vertex_array_object *vao)
862 {
863 _mesa_update_vao_derived_arrays(ctx, vao);
864 vao->NewArrays = 0;
865 vao->SharedAndImmutable = true;
866 }
867
868
869 bool
870 _mesa_all_varyings_in_vbos(const struct gl_vertex_array_object *vao)
871 {
872 /* Walk those enabled arrays that have the default vbo attached */
873 GLbitfield mask = vao->_Enabled & ~vao->VertexAttribBufferMask;
874
875 while (mask) {
876 /* Do not use u_bit_scan64 as we can walk multiple
877 * attrib arrays at once
878 */
879 const int i = ffs(mask) - 1;
880 const struct gl_array_attributes *attrib_array =
881 &vao->VertexAttrib[i];
882 const struct gl_vertex_buffer_binding *buffer_binding =
883 &vao->BufferBinding[attrib_array->BufferBindingIndex];
884
885 /* Only enabled arrays shall appear in the _Enabled bitmask */
886 assert(attrib_array->Enabled);
887 /* We have already masked out vao->VertexAttribBufferMask */
888 assert(!_mesa_is_bufferobj(buffer_binding->BufferObj));
889
890 /* Bail out once we find the first non vbo with a non zero stride */
891 if (buffer_binding->Stride != 0)
892 return false;
893
894 /* Note that we cannot use the xor variant since the _BoundArray mask
895 * may contain array attributes that are bound but not enabled.
896 */
897 mask &= ~buffer_binding->_BoundArrays;
898 }
899
900 return true;
901 }
902
903 bool
904 _mesa_all_buffers_are_unmapped(const struct gl_vertex_array_object *vao)
905 {
906 /* Walk the enabled arrays that have a vbo attached */
907 GLbitfield mask = vao->_Enabled & vao->VertexAttribBufferMask;
908
909 while (mask) {
910 const int i = ffs(mask) - 1;
911 const struct gl_array_attributes *attrib_array =
912 &vao->VertexAttrib[i];
913 const struct gl_vertex_buffer_binding *buffer_binding =
914 &vao->BufferBinding[attrib_array->BufferBindingIndex];
915
916 /* Only enabled arrays shall appear in the _Enabled bitmask */
917 assert(attrib_array->Enabled);
918 /* We have already masked with vao->VertexAttribBufferMask */
919 assert(_mesa_is_bufferobj(buffer_binding->BufferObj));
920
921 /* Bail out once we find the first disallowed mapping */
922 if (_mesa_check_disallowed_mapping(buffer_binding->BufferObj))
923 return false;
924
925 /* We have handled everything that is bound to this buffer_binding. */
926 mask &= ~buffer_binding->_BoundArrays;
927 }
928
929 return true;
930 }
931
932 /**********************************************************************/
933 /* API Functions */
934 /**********************************************************************/
935
936
937 /**
938 * ARB version of glBindVertexArray()
939 */
940 static ALWAYS_INLINE void
941 bind_vertex_array(struct gl_context *ctx, GLuint id, bool no_error)
942 {
943 struct gl_vertex_array_object *const oldObj = ctx->Array.VAO;
944 struct gl_vertex_array_object *newObj = NULL;
945
946 assert(oldObj != NULL);
947
948 if (oldObj->Name == id)
949 return; /* rebinding the same array object- no change */
950
951 /*
952 * Get pointer to new array object (newObj)
953 */
954 if (id == 0) {
955 /* The spec says there is no array object named 0, but we use
956 * one internally because it simplifies things.
957 */
958 newObj = ctx->Array.DefaultVAO;
959 }
960 else {
961 /* non-default array object */
962 newObj = _mesa_lookup_vao(ctx, id);
963 if (!no_error && !newObj) {
964 _mesa_error(ctx, GL_INVALID_OPERATION,
965 "glBindVertexArray(non-gen name)");
966 return;
967 }
968
969 newObj->EverBound = GL_TRUE;
970 }
971
972 /* The _DrawArrays pointer is pointing at the VAO being unbound and
973 * that VAO may be in the process of being deleted. If it's not going
974 * to be deleted, this will have no effect, because the pointer needs
975 * to be updated by the VBO module anyway.
976 *
977 * Before the VBO module can update the pointer, we have to set it
978 * to NULL for drivers not to set up arrays which are not bound,
979 * or to prevent a crash if the VAO being unbound is going to be
980 * deleted.
981 */
982 _mesa_set_draw_vao(ctx, ctx->Array._EmptyVAO, 0);
983
984 ctx->NewState |= _NEW_ARRAY;
985 _mesa_reference_vao(ctx, &ctx->Array.VAO, newObj);
986 }
987
988
989 void GLAPIENTRY
990 _mesa_BindVertexArray_no_error(GLuint id)
991 {
992 GET_CURRENT_CONTEXT(ctx);
993 bind_vertex_array(ctx, id, true);
994 }
995
996
997 void GLAPIENTRY
998 _mesa_BindVertexArray(GLuint id)
999 {
1000 GET_CURRENT_CONTEXT(ctx);
1001 bind_vertex_array(ctx, id, false);
1002 }
1003
1004
1005 /**
1006 * Delete a set of array objects.
1007 *
1008 * \param n Number of array objects to delete.
1009 * \param ids Array of \c n array object IDs.
1010 */
1011 static void
1012 delete_vertex_arrays(struct gl_context *ctx, GLsizei n, const GLuint *ids)
1013 {
1014 GLsizei i;
1015
1016 for (i = 0; i < n; i++) {
1017 /* IDs equal to 0 should be silently ignored. */
1018 if (!ids[i])
1019 continue;
1020
1021 struct gl_vertex_array_object *obj = _mesa_lookup_vao(ctx, ids[i]);
1022
1023 if (obj) {
1024 assert(obj->Name == ids[i]);
1025
1026 /* If the array object is currently bound, the spec says "the binding
1027 * for that object reverts to zero and the default vertex array
1028 * becomes current."
1029 */
1030 if (obj == ctx->Array.VAO)
1031 _mesa_BindVertexArray_no_error(0);
1032
1033 /* The ID is immediately freed for re-use */
1034 _mesa_HashRemoveLocked(ctx->Array.Objects, obj->Name);
1035
1036 if (ctx->Array.LastLookedUpVAO == obj)
1037 _mesa_reference_vao(ctx, &ctx->Array.LastLookedUpVAO, NULL);
1038 if (ctx->Array._DrawVAO == obj)
1039 _mesa_set_draw_vao(ctx, ctx->Array._EmptyVAO, 0);
1040
1041 /* Unreference the array object.
1042 * If refcount hits zero, the object will be deleted.
1043 */
1044 _mesa_reference_vao(ctx, &obj, NULL);
1045 }
1046 }
1047 }
1048
1049
1050 void GLAPIENTRY
1051 _mesa_DeleteVertexArrays_no_error(GLsizei n, const GLuint *ids)
1052 {
1053 GET_CURRENT_CONTEXT(ctx);
1054 delete_vertex_arrays(ctx, n, ids);
1055 }
1056
1057
1058 void GLAPIENTRY
1059 _mesa_DeleteVertexArrays(GLsizei n, const GLuint *ids)
1060 {
1061 GET_CURRENT_CONTEXT(ctx);
1062
1063 if (n < 0) {
1064 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArray(n)");
1065 return;
1066 }
1067
1068 delete_vertex_arrays(ctx, n, ids);
1069 }
1070
1071
1072 /**
1073 * Generate a set of unique array object IDs and store them in \c arrays.
1074 * Helper for _mesa_GenVertexArrays() and _mesa_CreateVertexArrays()
1075 * below.
1076 *
1077 * \param n Number of IDs to generate.
1078 * \param arrays Array of \c n locations to store the IDs.
1079 * \param create Indicates that the objects should also be created.
1080 * \param func The name of the GL entry point.
1081 */
1082 static void
1083 gen_vertex_arrays(struct gl_context *ctx, GLsizei n, GLuint *arrays,
1084 bool create, const char *func)
1085 {
1086 GLuint first;
1087 GLint i;
1088
1089 if (!arrays)
1090 return;
1091
1092 first = _mesa_HashFindFreeKeyBlock(ctx->Array.Objects, n);
1093
1094 /* For the sake of simplicity we create the array objects in both
1095 * the Gen* and Create* cases. The only difference is the value of
1096 * EverBound, which is set to true in the Create* case.
1097 */
1098 for (i = 0; i < n; i++) {
1099 struct gl_vertex_array_object *obj;
1100 GLuint name = first + i;
1101
1102 obj = _mesa_new_vao(ctx, name);
1103 if (!obj) {
1104 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1105 return;
1106 }
1107 obj->EverBound = create;
1108 _mesa_HashInsertLocked(ctx->Array.Objects, obj->Name, obj);
1109 arrays[i] = first + i;
1110 }
1111 }
1112
1113
1114 static void
1115 gen_vertex_arrays_err(struct gl_context *ctx, GLsizei n, GLuint *arrays,
1116 bool create, const char *func)
1117 {
1118 if (n < 0) {
1119 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
1120 return;
1121 }
1122
1123 gen_vertex_arrays(ctx, n, arrays, create, func);
1124 }
1125
1126
1127 /**
1128 * ARB version of glGenVertexArrays()
1129 * All arrays will be required to live in VBOs.
1130 */
1131 void GLAPIENTRY
1132 _mesa_GenVertexArrays_no_error(GLsizei n, GLuint *arrays)
1133 {
1134 GET_CURRENT_CONTEXT(ctx);
1135 gen_vertex_arrays(ctx, n, arrays, false, "glGenVertexArrays");
1136 }
1137
1138
1139 void GLAPIENTRY
1140 _mesa_GenVertexArrays(GLsizei n, GLuint *arrays)
1141 {
1142 GET_CURRENT_CONTEXT(ctx);
1143 gen_vertex_arrays_err(ctx, n, arrays, false, "glGenVertexArrays");
1144 }
1145
1146
1147 /**
1148 * ARB_direct_state_access
1149 * Generates ID's and creates the array objects.
1150 */
1151 void GLAPIENTRY
1152 _mesa_CreateVertexArrays_no_error(GLsizei n, GLuint *arrays)
1153 {
1154 GET_CURRENT_CONTEXT(ctx);
1155 gen_vertex_arrays(ctx, n, arrays, true, "glCreateVertexArrays");
1156 }
1157
1158
1159 void GLAPIENTRY
1160 _mesa_CreateVertexArrays(GLsizei n, GLuint *arrays)
1161 {
1162 GET_CURRENT_CONTEXT(ctx);
1163 gen_vertex_arrays_err(ctx, n, arrays, true, "glCreateVertexArrays");
1164 }
1165
1166
1167 /**
1168 * Determine if ID is the name of an array object.
1169 *
1170 * \param id ID of the potential array object.
1171 * \return \c GL_TRUE if \c id is the name of a array object,
1172 * \c GL_FALSE otherwise.
1173 */
1174 GLboolean GLAPIENTRY
1175 _mesa_IsVertexArray( GLuint id )
1176 {
1177 struct gl_vertex_array_object * obj;
1178 GET_CURRENT_CONTEXT(ctx);
1179 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1180
1181 obj = _mesa_lookup_vao(ctx, id);
1182
1183 return obj != NULL && obj->EverBound;
1184 }
1185
1186
1187 /**
1188 * Sets the element array buffer binding of a vertex array object.
1189 *
1190 * This is the ARB_direct_state_access equivalent of
1191 * glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer).
1192 */
1193 static ALWAYS_INLINE void
1194 vertex_array_element_buffer(struct gl_context *ctx, GLuint vaobj, GLuint buffer,
1195 bool no_error)
1196 {
1197 struct gl_vertex_array_object *vao;
1198 struct gl_buffer_object *bufObj;
1199
1200 ASSERT_OUTSIDE_BEGIN_END(ctx);
1201
1202 if (!no_error) {
1203 /* The GL_ARB_direct_state_access specification says:
1204 *
1205 * "An INVALID_OPERATION error is generated by
1206 * VertexArrayElementBuffer if <vaobj> is not [compatibility profile:
1207 * zero or] the name of an existing vertex array object."
1208 */
1209 vao =_mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayElementBuffer");
1210 if (!vao)
1211 return;
1212 } else {
1213 vao = _mesa_lookup_vao(ctx, vaobj);
1214 }
1215
1216 if (buffer != 0) {
1217 if (!no_error) {
1218 /* The GL_ARB_direct_state_access specification says:
1219 *
1220 * "An INVALID_OPERATION error is generated if <buffer> is not zero
1221 * or the name of an existing buffer object."
1222 */
1223 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
1224 "glVertexArrayElementBuffer");
1225 } else {
1226 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1227 }
1228 } else {
1229 bufObj = ctx->Shared->NullBufferObj;
1230 }
1231
1232 if (bufObj)
1233 _mesa_reference_buffer_object(ctx, &vao->IndexBufferObj, bufObj);
1234 }
1235
1236
1237 void GLAPIENTRY
1238 _mesa_VertexArrayElementBuffer_no_error(GLuint vaobj, GLuint buffer)
1239 {
1240 GET_CURRENT_CONTEXT(ctx);
1241 vertex_array_element_buffer(ctx, vaobj, buffer, true);
1242 }
1243
1244
1245 void GLAPIENTRY
1246 _mesa_VertexArrayElementBuffer(GLuint vaobj, GLuint buffer)
1247 {
1248 GET_CURRENT_CONTEXT(ctx);
1249 vertex_array_element_buffer(ctx, vaobj, buffer, false);
1250 }
1251
1252
1253 void GLAPIENTRY
1254 _mesa_GetVertexArrayiv(GLuint vaobj, GLenum pname, GLint *param)
1255 {
1256 GET_CURRENT_CONTEXT(ctx);
1257 struct gl_vertex_array_object *vao;
1258
1259 ASSERT_OUTSIDE_BEGIN_END(ctx);
1260
1261 /* The GL_ARB_direct_state_access specification says:
1262 *
1263 * "An INVALID_OPERATION error is generated if <vaobj> is not
1264 * [compatibility profile: zero or] the name of an existing
1265 * vertex array object."
1266 */
1267 vao =_mesa_lookup_vao_err(ctx, vaobj, "glGetVertexArrayiv");
1268 if (!vao)
1269 return;
1270
1271 /* The GL_ARB_direct_state_access specification says:
1272 *
1273 * "An INVALID_ENUM error is generated if <pname> is not
1274 * ELEMENT_ARRAY_BUFFER_BINDING."
1275 */
1276 if (pname != GL_ELEMENT_ARRAY_BUFFER_BINDING) {
1277 _mesa_error(ctx, GL_INVALID_ENUM,
1278 "glGetVertexArrayiv(pname != "
1279 "GL_ELEMENT_ARRAY_BUFFER_BINDING)");
1280 return;
1281 }
1282
1283 param[0] = vao->IndexBufferObj->Name;
1284 }