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