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