mesa: Refactor VertexAttrib[I|L]Format
[mesa.git] / src / mesa / main / varray.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include <stdio.h>
28 #include <inttypes.h> /* for PRId64 macro */
29
30 #include "glheader.h"
31 #include "imports.h"
32 #include "bufferobj.h"
33 #include "context.h"
34 #include "enable.h"
35 #include "enums.h"
36 #include "hash.h"
37 #include "image.h"
38 #include "macros.h"
39 #include "mtypes.h"
40 #include "varray.h"
41 #include "arrayobj.h"
42 #include "main/dispatch.h"
43
44
45 /** Used to do error checking for GL_EXT_vertex_array_bgra */
46 #define BGRA_OR_4 5
47
48
49 /** Used to indicate which GL datatypes are accepted by each of the
50 * glVertex/Color/Attrib/EtcPointer() functions.
51 */
52 #define BOOL_BIT (1 << 0)
53 #define BYTE_BIT (1 << 1)
54 #define UNSIGNED_BYTE_BIT (1 << 2)
55 #define SHORT_BIT (1 << 3)
56 #define UNSIGNED_SHORT_BIT (1 << 4)
57 #define INT_BIT (1 << 5)
58 #define UNSIGNED_INT_BIT (1 << 6)
59 #define HALF_BIT (1 << 7)
60 #define FLOAT_BIT (1 << 8)
61 #define DOUBLE_BIT (1 << 9)
62 #define FIXED_ES_BIT (1 << 10)
63 #define FIXED_GL_BIT (1 << 11)
64 #define UNSIGNED_INT_2_10_10_10_REV_BIT (1 << 12)
65 #define INT_2_10_10_10_REV_BIT (1 << 13)
66 #define UNSIGNED_INT_10F_11F_11F_REV_BIT (1 << 14)
67 #define ALL_TYPE_BITS ((1 << 15) - 1)
68
69 #define ATTRIB_FORMAT_TYPES_MASK (BYTE_BIT | UNSIGNED_BYTE_BIT | \
70 SHORT_BIT | UNSIGNED_SHORT_BIT | \
71 INT_BIT | UNSIGNED_INT_BIT | \
72 HALF_BIT | FLOAT_BIT | DOUBLE_BIT | \
73 FIXED_GL_BIT | \
74 UNSIGNED_INT_2_10_10_10_REV_BIT | \
75 INT_2_10_10_10_REV_BIT | \
76 UNSIGNED_INT_10F_11F_11F_REV_BIT)
77
78 #define ATTRIB_IFORMAT_TYPES_MASK (BYTE_BIT | UNSIGNED_BYTE_BIT | \
79 SHORT_BIT | UNSIGNED_SHORT_BIT | \
80 INT_BIT | UNSIGNED_INT_BIT)
81
82 #define ATTRIB_LFORMAT_TYPES_MASK DOUBLE_BIT
83
84
85 /** Convert GL datatype enum into a <type>_BIT value seen above */
86 static GLbitfield
87 type_to_bit(const struct gl_context *ctx, GLenum type)
88 {
89 switch (type) {
90 case GL_BOOL:
91 return BOOL_BIT;
92 case GL_BYTE:
93 return BYTE_BIT;
94 case GL_UNSIGNED_BYTE:
95 return UNSIGNED_BYTE_BIT;
96 case GL_SHORT:
97 return SHORT_BIT;
98 case GL_UNSIGNED_SHORT:
99 return UNSIGNED_SHORT_BIT;
100 case GL_INT:
101 return INT_BIT;
102 case GL_UNSIGNED_INT:
103 return UNSIGNED_INT_BIT;
104 case GL_HALF_FLOAT:
105 if (ctx->Extensions.ARB_half_float_vertex)
106 return HALF_BIT;
107 else
108 return 0x0;
109 case GL_FLOAT:
110 return FLOAT_BIT;
111 case GL_DOUBLE:
112 return DOUBLE_BIT;
113 case GL_FIXED:
114 return _mesa_is_desktop_gl(ctx) ? FIXED_GL_BIT : FIXED_ES_BIT;
115 case GL_UNSIGNED_INT_2_10_10_10_REV:
116 return UNSIGNED_INT_2_10_10_10_REV_BIT;
117 case GL_INT_2_10_10_10_REV:
118 return INT_2_10_10_10_REV_BIT;
119 case GL_UNSIGNED_INT_10F_11F_11F_REV:
120 return UNSIGNED_INT_10F_11F_11F_REV_BIT;
121 default:
122 return 0;
123 }
124 }
125
126
127 /**
128 * Sets the VertexBinding field in the vertex attribute given by attribIndex.
129 */
130 static void
131 vertex_attrib_binding(struct gl_context *ctx, GLuint attribIndex,
132 GLuint bindingIndex)
133 {
134 struct gl_vertex_array_object *vao = ctx->Array.VAO;
135 struct gl_vertex_attrib_array *array = &vao->VertexAttrib[attribIndex];
136
137 if (array->VertexBinding != bindingIndex) {
138 const GLbitfield64 array_bit = VERT_BIT(attribIndex);
139
140 FLUSH_VERTICES(ctx, _NEW_ARRAY);
141
142 vao->VertexBinding[array->VertexBinding]._BoundArrays &= ~array_bit;
143 vao->VertexBinding[bindingIndex]._BoundArrays |= array_bit;
144
145 array->VertexBinding = bindingIndex;
146
147 vao->NewArrays |= array_bit;
148 }
149 }
150
151
152 /**
153 * Binds a buffer object to the vertex buffer binding point given by index,
154 * and sets the Offset and Stride fields.
155 */
156 static void
157 bind_vertex_buffer(struct gl_context *ctx,
158 struct gl_vertex_array_object *vao,
159 GLuint index,
160 struct gl_buffer_object *vbo,
161 GLintptr offset, GLsizei stride)
162 {
163 struct gl_vertex_buffer_binding *binding = &vao->VertexBinding[index];
164
165 if (binding->BufferObj != vbo ||
166 binding->Offset != offset ||
167 binding->Stride != stride) {
168
169 FLUSH_VERTICES(ctx, _NEW_ARRAY);
170
171 _mesa_reference_buffer_object(ctx, &binding->BufferObj, vbo);
172
173 binding->Offset = offset;
174 binding->Stride = stride;
175
176 vao->NewArrays |= binding->_BoundArrays;
177 }
178 }
179
180
181 /**
182 * Sets the InstanceDivisor field in the vertex buffer binding point
183 * given by bindingIndex.
184 */
185 static void
186 vertex_binding_divisor(struct gl_context *ctx, GLuint bindingIndex,
187 GLuint divisor)
188 {
189 struct gl_vertex_array_object *vao = ctx->Array.VAO;
190 struct gl_vertex_buffer_binding *binding =
191 &vao->VertexBinding[bindingIndex];
192
193 if (binding->InstanceDivisor != divisor) {
194 FLUSH_VERTICES(ctx, _NEW_ARRAY);
195 binding->InstanceDivisor = divisor;
196 vao->NewArrays |= binding->_BoundArrays;
197 }
198 }
199
200
201 /**
202 * Examine the API profile and extensions to determine which types are legal
203 * for vertex arrays. This is called once from update_array_format().
204 */
205 static GLbitfield
206 get_legal_types_mask(const struct gl_context *ctx)
207 {
208 GLbitfield legalTypesMask = ALL_TYPE_BITS;
209
210 if (_mesa_is_gles(ctx)) {
211 legalTypesMask &= ~(FIXED_GL_BIT |
212 DOUBLE_BIT |
213 UNSIGNED_INT_10F_11F_11F_REV_BIT);
214
215 /* GL_INT and GL_UNSIGNED_INT data is not allowed in OpenGL ES until
216 * 3.0. The 2_10_10_10 types are added in OpenGL ES 3.0 or
217 * GL_OES_vertex_type_10_10_10_2. GL_HALF_FLOAT data is not allowed
218 * until 3.0 or with the GL_OES_vertex_half float extension, which isn't
219 * quite as trivial as we'd like because it uses a different enum value
220 * for GL_HALF_FLOAT_OES.
221 */
222 if (ctx->Version < 30) {
223 legalTypesMask &= ~(UNSIGNED_INT_BIT |
224 INT_BIT |
225 UNSIGNED_INT_2_10_10_10_REV_BIT |
226 INT_2_10_10_10_REV_BIT |
227 HALF_BIT);
228 }
229 }
230 else {
231 legalTypesMask &= ~FIXED_ES_BIT;
232
233 if (!ctx->Extensions.ARB_ES2_compatibility)
234 legalTypesMask &= ~FIXED_GL_BIT;
235
236 if (!ctx->Extensions.ARB_vertex_type_2_10_10_10_rev)
237 legalTypesMask &= ~(UNSIGNED_INT_2_10_10_10_REV_BIT |
238 INT_2_10_10_10_REV_BIT);
239
240 if (!ctx->Extensions.ARB_vertex_type_10f_11f_11f_rev)
241 legalTypesMask &= ~UNSIGNED_INT_10F_11F_11F_REV_BIT;
242 }
243
244 return legalTypesMask;
245 }
246
247
248 /**
249 * Does error checking and updates the format in an attrib array.
250 *
251 * Called by update_array() and VertexAttrib*Format().
252 *
253 * \param func Name of calling function used for error reporting
254 * \param attrib The index of the attribute array
255 * \param legalTypes Bitmask of *_BIT above indicating legal datatypes
256 * \param sizeMin Min allowable size value
257 * \param sizeMax Max allowable size value (may also be BGRA_OR_4)
258 * \param size Components per element (1, 2, 3 or 4)
259 * \param type Datatype of each component (GL_FLOAT, GL_INT, etc)
260 * \param normalized Whether integer types are converted to floats in [-1, 1]
261 * \param integer Integer-valued values (will not be normalized to [-1, 1])
262 * \param doubles Double values not reduced to floats
263 * \param relativeOffset Offset of the first element relative to the binding offset.
264 */
265 static bool
266 update_array_format(struct gl_context *ctx,
267 const char *func,
268 GLuint attrib, GLbitfield legalTypesMask,
269 GLint sizeMin, GLint sizeMax,
270 GLint size, GLenum type,
271 GLboolean normalized, GLboolean integer, GLboolean doubles,
272 GLuint relativeOffset)
273 {
274 struct gl_vertex_attrib_array *array;
275 GLbitfield typeBit;
276 GLint elementSize;
277 GLenum format = GL_RGBA;
278
279 if (ctx->Array.LegalTypesMask == 0 || ctx->Array.LegalTypesMaskAPI != ctx->API) {
280 /* Compute the LegalTypesMask only once, unless the context API has
281 * changed, in which case we want to compute it again. We can't do this
282 * in _mesa_init_varrays() below because extensions are not yet enabled
283 * at that point.
284 */
285 ctx->Array.LegalTypesMask = get_legal_types_mask(ctx);
286 ctx->Array.LegalTypesMaskAPI = ctx->API;
287 }
288
289 legalTypesMask &= ctx->Array.LegalTypesMask;
290
291 if (_mesa_is_gles(ctx) && sizeMax == BGRA_OR_4) {
292 /* BGRA ordering is not supported in ES contexts.
293 */
294 sizeMax = 4;
295 }
296
297 typeBit = type_to_bit(ctx, type);
298 if (typeBit == 0x0 || (typeBit & legalTypesMask) == 0x0) {
299 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)",
300 func, _mesa_lookup_enum_by_nr(type));
301 return false;
302 }
303
304 /* Do size parameter checking.
305 * If sizeMax = BGRA_OR_4 it means that size = GL_BGRA is legal and
306 * must be handled specially.
307 */
308 if (ctx->Extensions.EXT_vertex_array_bgra &&
309 sizeMax == BGRA_OR_4 &&
310 size == GL_BGRA) {
311 /* Page 298 of the PDF of the OpenGL 4.3 (Core Profile) spec says:
312 *
313 * "An INVALID_OPERATION error is generated under any of the following
314 * conditions:
315 * ...
316 * • size is BGRA and type is not UNSIGNED_BYTE, INT_2_10_10_10_REV
317 * or UNSIGNED_INT_2_10_10_10_REV;
318 * ...
319 * • size is BGRA and normalized is FALSE;"
320 */
321 bool bgra_error = false;
322
323 if (ctx->Extensions.ARB_vertex_type_2_10_10_10_rev) {
324 if (type != GL_UNSIGNED_INT_2_10_10_10_REV &&
325 type != GL_INT_2_10_10_10_REV &&
326 type != GL_UNSIGNED_BYTE)
327 bgra_error = true;
328 } else if (type != GL_UNSIGNED_BYTE)
329 bgra_error = true;
330
331 if (bgra_error) {
332 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(size=GL_BGRA and type=%s)",
333 func, _mesa_lookup_enum_by_nr(type));
334 return false;
335 }
336
337 if (!normalized) {
338 _mesa_error(ctx, GL_INVALID_OPERATION,
339 "%s(size=GL_BGRA and normalized=GL_FALSE)", func);
340 return false;
341 }
342
343 format = GL_BGRA;
344 size = 4;
345 }
346 else if (size < sizeMin || size > sizeMax || size > 4) {
347 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d)", func, size);
348 return false;
349 }
350
351 if (ctx->Extensions.ARB_vertex_type_2_10_10_10_rev &&
352 (type == GL_UNSIGNED_INT_2_10_10_10_REV ||
353 type == GL_INT_2_10_10_10_REV) && size != 4) {
354 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(size=%d)", func, size);
355 return false;
356 }
357
358 /* The ARB_vertex_attrib_binding_spec says:
359 *
360 * An INVALID_VALUE error is generated if <relativeoffset> is larger than
361 * the value of MAX_VERTEX_ATTRIB_RELATIVE_OFFSET.
362 */
363 if (relativeOffset > ctx->Const.MaxVertexAttribRelativeOffset) {
364 _mesa_error(ctx, GL_INVALID_VALUE,
365 "%s(relativeOffset=%d > "
366 "GL_MAX_VERTEX_ATTRIB_RELATIVE_OFFSET)",
367 func, relativeOffset);
368 return false;
369 }
370
371 if (ctx->Extensions.ARB_vertex_type_10f_11f_11f_rev &&
372 type == GL_UNSIGNED_INT_10F_11F_11F_REV && size != 3) {
373 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(size=%d)", func, size);
374 return false;
375 }
376
377 assert(size <= 4);
378
379 elementSize = _mesa_bytes_per_vertex_attrib(size, type);
380 assert(elementSize != -1);
381
382 array = &ctx->Array.VAO->VertexAttrib[attrib];
383 array->Size = size;
384 array->Type = type;
385 array->Format = format;
386 array->Normalized = normalized;
387 array->Integer = integer;
388 array->Doubles = doubles;
389 array->RelativeOffset = relativeOffset;
390 array->_ElementSize = elementSize;
391
392 ctx->Array.VAO->NewArrays |= VERT_BIT(attrib);
393 ctx->NewState |= _NEW_ARRAY;
394
395 return true;
396 }
397
398
399 /**
400 * Do error checking and update state for glVertex/Color/TexCoord/...Pointer
401 * functions.
402 *
403 * \param func name of calling function used for error reporting
404 * \param attrib the attribute array index to update
405 * \param legalTypes bitmask of *_BIT above indicating legal datatypes
406 * \param sizeMin min allowable size value
407 * \param sizeMax max allowable size value (may also be BGRA_OR_4)
408 * \param size components per element (1, 2, 3 or 4)
409 * \param type datatype of each component (GL_FLOAT, GL_INT, etc)
410 * \param stride stride between elements, in elements
411 * \param normalized are integer types converted to floats in [-1, 1]?
412 * \param integer integer-valued values (will not be normalized to [-1,1])
413 * \param doubles Double values not reduced to floats
414 * \param ptr the address (or offset inside VBO) of the array data
415 */
416 static void
417 update_array(struct gl_context *ctx,
418 const char *func,
419 GLuint attrib, GLbitfield legalTypesMask,
420 GLint sizeMin, GLint sizeMax,
421 GLint size, GLenum type, GLsizei stride,
422 GLboolean normalized, GLboolean integer, GLboolean doubles,
423 const GLvoid *ptr)
424 {
425 struct gl_vertex_attrib_array *array;
426 GLsizei effectiveStride;
427
428 /* Page 407 (page 423 of the PDF) of the OpenGL 3.0 spec says:
429 *
430 * "Client vertex arrays - all vertex array attribute pointers must
431 * refer to buffer objects (section 2.9.2). The default vertex array
432 * object (the name zero) is also deprecated. Calling
433 * VertexAttribPointer when no buffer object or no vertex array object
434 * is bound will generate an INVALID_OPERATION error..."
435 *
436 * The check for VBOs is handled below.
437 */
438 if (ctx->API == API_OPENGL_CORE
439 && (ctx->Array.VAO == ctx->Array.DefaultVAO)) {
440 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no array object bound)",
441 func);
442 return;
443 }
444
445 if (stride < 0) {
446 _mesa_error( ctx, GL_INVALID_VALUE, "%s(stride=%d)", func, stride );
447 return;
448 }
449
450 if (ctx->API == API_OPENGL_CORE && ctx->Version >= 44 &&
451 stride > ctx->Const.MaxVertexAttribStride) {
452 _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride=%d > "
453 "GL_MAX_VERTEX_ATTRIB_STRIDE)", func, stride);
454 return;
455 }
456
457 /* Page 29 (page 44 of the PDF) of the OpenGL 3.3 spec says:
458 *
459 * "An INVALID_OPERATION error is generated under any of the following
460 * conditions:
461 *
462 * ...
463 *
464 * * any of the *Pointer commands specifying the location and
465 * organization of vertex array data are called while zero is bound
466 * to the ARRAY_BUFFER buffer object binding point (see section
467 * 2.9.6), and the pointer argument is not NULL."
468 */
469 if (ptr != NULL && ctx->Array.VAO->ARBsemantics &&
470 !_mesa_is_bufferobj(ctx->Array.ArrayBufferObj)) {
471 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-VBO array)", func);
472 return;
473 }
474
475 if (!update_array_format(ctx, func, attrib, legalTypesMask, sizeMin,
476 sizeMax, size, type, normalized, integer, doubles, 0)) {
477 return;
478 }
479
480 /* Reset the vertex attrib binding */
481 vertex_attrib_binding(ctx, attrib, attrib);
482
483 /* The Stride and Ptr fields are not set by update_array_format() */
484 array = &ctx->Array.VAO->VertexAttrib[attrib];
485 array->Stride = stride;
486 array->Ptr = (const GLvoid *) ptr;
487
488 /* Update the vertex buffer binding */
489 effectiveStride = stride != 0 ? stride : array->_ElementSize;
490 bind_vertex_buffer(ctx, ctx->Array.VAO, attrib, ctx->Array.ArrayBufferObj,
491 (GLintptr) ptr, effectiveStride);
492 }
493
494
495 void GLAPIENTRY
496 _mesa_VertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
497 {
498 GET_CURRENT_CONTEXT(ctx);
499 GLbitfield legalTypes = (ctx->API == API_OPENGLES)
500 ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
501 : (SHORT_BIT | INT_BIT | FLOAT_BIT |
502 DOUBLE_BIT | HALF_BIT |
503 UNSIGNED_INT_2_10_10_10_REV_BIT |
504 INT_2_10_10_10_REV_BIT);
505
506 FLUSH_VERTICES(ctx, 0);
507
508 update_array(ctx, "glVertexPointer", VERT_ATTRIB_POS,
509 legalTypes, 2, 4,
510 size, type, stride, GL_FALSE, GL_FALSE, GL_FALSE, ptr);
511 }
512
513
514 void GLAPIENTRY
515 _mesa_NormalPointer(GLenum type, GLsizei stride, const GLvoid *ptr )
516 {
517 GET_CURRENT_CONTEXT(ctx);
518 const GLbitfield legalTypes = (ctx->API == API_OPENGLES)
519 ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
520 : (BYTE_BIT | SHORT_BIT | INT_BIT |
521 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
522 UNSIGNED_INT_2_10_10_10_REV_BIT |
523 INT_2_10_10_10_REV_BIT);
524
525 FLUSH_VERTICES(ctx, 0);
526
527 update_array(ctx, "glNormalPointer", VERT_ATTRIB_NORMAL,
528 legalTypes, 3, 3,
529 3, type, stride, GL_TRUE, GL_FALSE, GL_FALSE, ptr);
530 }
531
532
533 void GLAPIENTRY
534 _mesa_ColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
535 {
536 GET_CURRENT_CONTEXT(ctx);
537 const GLbitfield legalTypes = (ctx->API == API_OPENGLES)
538 ? (UNSIGNED_BYTE_BIT | HALF_BIT | FLOAT_BIT | FIXED_ES_BIT)
539 : (BYTE_BIT | UNSIGNED_BYTE_BIT |
540 SHORT_BIT | UNSIGNED_SHORT_BIT |
541 INT_BIT | UNSIGNED_INT_BIT |
542 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
543 UNSIGNED_INT_2_10_10_10_REV_BIT |
544 INT_2_10_10_10_REV_BIT);
545 const GLint sizeMin = (ctx->API == API_OPENGLES) ? 4 : 3;
546
547 FLUSH_VERTICES(ctx, 0);
548
549 update_array(ctx, "glColorPointer", VERT_ATTRIB_COLOR0,
550 legalTypes, sizeMin, BGRA_OR_4,
551 size, type, stride, GL_TRUE, GL_FALSE, GL_FALSE, ptr);
552 }
553
554
555 void GLAPIENTRY
556 _mesa_FogCoordPointer(GLenum type, GLsizei stride, const GLvoid *ptr)
557 {
558 const GLbitfield legalTypes = (HALF_BIT | FLOAT_BIT | DOUBLE_BIT);
559 GET_CURRENT_CONTEXT(ctx);
560
561 FLUSH_VERTICES(ctx, 0);
562
563 update_array(ctx, "glFogCoordPointer", VERT_ATTRIB_FOG,
564 legalTypes, 1, 1,
565 1, type, stride, GL_FALSE, GL_FALSE, GL_FALSE, ptr);
566 }
567
568
569 void GLAPIENTRY
570 _mesa_IndexPointer(GLenum type, GLsizei stride, const GLvoid *ptr)
571 {
572 const GLbitfield legalTypes = (UNSIGNED_BYTE_BIT | SHORT_BIT | INT_BIT |
573 FLOAT_BIT | DOUBLE_BIT);
574 GET_CURRENT_CONTEXT(ctx);
575
576 FLUSH_VERTICES(ctx, 0);
577
578 update_array(ctx, "glIndexPointer", VERT_ATTRIB_COLOR_INDEX,
579 legalTypes, 1, 1,
580 1, type, stride, GL_FALSE, GL_FALSE, GL_FALSE, ptr);
581 }
582
583
584 void GLAPIENTRY
585 _mesa_SecondaryColorPointer(GLint size, GLenum type,
586 GLsizei stride, const GLvoid *ptr)
587 {
588 const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
589 SHORT_BIT | UNSIGNED_SHORT_BIT |
590 INT_BIT | UNSIGNED_INT_BIT |
591 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
592 UNSIGNED_INT_2_10_10_10_REV_BIT |
593 INT_2_10_10_10_REV_BIT);
594 GET_CURRENT_CONTEXT(ctx);
595
596 FLUSH_VERTICES(ctx, 0);
597
598 update_array(ctx, "glSecondaryColorPointer", VERT_ATTRIB_COLOR1,
599 legalTypes, 3, BGRA_OR_4,
600 size, type, stride, GL_TRUE, GL_FALSE, GL_FALSE, ptr);
601 }
602
603
604 void GLAPIENTRY
605 _mesa_TexCoordPointer(GLint size, GLenum type, GLsizei stride,
606 const GLvoid *ptr)
607 {
608 GET_CURRENT_CONTEXT(ctx);
609 GLbitfield legalTypes = (ctx->API == API_OPENGLES)
610 ? (BYTE_BIT | SHORT_BIT | FLOAT_BIT | FIXED_ES_BIT)
611 : (SHORT_BIT | INT_BIT |
612 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
613 UNSIGNED_INT_2_10_10_10_REV_BIT |
614 INT_2_10_10_10_REV_BIT);
615 const GLint sizeMin = (ctx->API == API_OPENGLES) ? 2 : 1;
616 const GLuint unit = ctx->Array.ActiveTexture;
617
618 FLUSH_VERTICES(ctx, 0);
619
620 update_array(ctx, "glTexCoordPointer", VERT_ATTRIB_TEX(unit),
621 legalTypes, sizeMin, 4,
622 size, type, stride, GL_FALSE, GL_FALSE, GL_FALSE,
623 ptr);
624 }
625
626
627 void GLAPIENTRY
628 _mesa_EdgeFlagPointer(GLsizei stride, const GLvoid *ptr)
629 {
630 const GLbitfield legalTypes = UNSIGNED_BYTE_BIT;
631 /* this is the same type that glEdgeFlag uses */
632 const GLboolean integer = GL_FALSE;
633 GET_CURRENT_CONTEXT(ctx);
634
635 FLUSH_VERTICES(ctx, 0);
636
637 update_array(ctx, "glEdgeFlagPointer", VERT_ATTRIB_EDGEFLAG,
638 legalTypes, 1, 1,
639 1, GL_UNSIGNED_BYTE, stride, GL_FALSE, integer, GL_FALSE, ptr);
640 }
641
642
643 void GLAPIENTRY
644 _mesa_PointSizePointerOES(GLenum type, GLsizei stride, const GLvoid *ptr)
645 {
646 const GLbitfield legalTypes = (FLOAT_BIT | FIXED_ES_BIT);
647 GET_CURRENT_CONTEXT(ctx);
648
649 FLUSH_VERTICES(ctx, 0);
650
651 if (ctx->API != API_OPENGLES) {
652 _mesa_error(ctx, GL_INVALID_OPERATION,
653 "glPointSizePointer(ES 1.x only)");
654 return;
655 }
656
657 update_array(ctx, "glPointSizePointer", VERT_ATTRIB_POINT_SIZE,
658 legalTypes, 1, 1,
659 1, type, stride, GL_FALSE, GL_FALSE, GL_FALSE, ptr);
660 }
661
662
663 /**
664 * Set a generic vertex attribute array.
665 * Note that these arrays DO NOT alias the conventional GL vertex arrays
666 * (position, normal, color, fog, texcoord, etc).
667 */
668 void GLAPIENTRY
669 _mesa_VertexAttribPointer(GLuint index, GLint size, GLenum type,
670 GLboolean normalized,
671 GLsizei stride, const GLvoid *ptr)
672 {
673 const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
674 SHORT_BIT | UNSIGNED_SHORT_BIT |
675 INT_BIT | UNSIGNED_INT_BIT |
676 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
677 FIXED_ES_BIT | FIXED_GL_BIT |
678 UNSIGNED_INT_2_10_10_10_REV_BIT |
679 INT_2_10_10_10_REV_BIT |
680 UNSIGNED_INT_10F_11F_11F_REV_BIT);
681 GET_CURRENT_CONTEXT(ctx);
682
683 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
684 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribPointerARB(index)");
685 return;
686 }
687
688 update_array(ctx, "glVertexAttribPointer", VERT_ATTRIB_GENERIC(index),
689 legalTypes, 1, BGRA_OR_4,
690 size, type, stride, normalized, GL_FALSE, GL_FALSE, ptr);
691 }
692
693
694 /**
695 * GL_EXT_gpu_shader4 / GL 3.0.
696 * Set an integer-valued vertex attribute array.
697 * Note that these arrays DO NOT alias the conventional GL vertex arrays
698 * (position, normal, color, fog, texcoord, etc).
699 */
700 void GLAPIENTRY
701 _mesa_VertexAttribIPointer(GLuint index, GLint size, GLenum type,
702 GLsizei stride, const GLvoid *ptr)
703 {
704 const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
705 SHORT_BIT | UNSIGNED_SHORT_BIT |
706 INT_BIT | UNSIGNED_INT_BIT);
707 const GLboolean normalized = GL_FALSE;
708 const GLboolean integer = GL_TRUE;
709 GET_CURRENT_CONTEXT(ctx);
710
711 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
712 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribIPointer(index)");
713 return;
714 }
715
716 update_array(ctx, "glVertexAttribIPointer", VERT_ATTRIB_GENERIC(index),
717 legalTypes, 1, 4,
718 size, type, stride, normalized, integer, GL_FALSE, ptr);
719 }
720
721 void GLAPIENTRY
722 _mesa_VertexAttribLPointer(GLuint index, GLint size, GLenum type,
723 GLsizei stride, const GLvoid *ptr)
724 {
725 GET_CURRENT_CONTEXT(ctx);
726 const GLbitfield legalTypes = (DOUBLE_BIT);
727 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
728 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribLPointer(index)");
729 return;
730 }
731
732 update_array(ctx, "glVertexAttribLPointer", VERT_ATTRIB_GENERIC(index),
733 legalTypes, 1, 4,
734 size, type, stride, GL_TRUE, GL_FALSE, GL_TRUE, ptr);
735 }
736
737
738 static void
739 enable_vertex_array_attrib(struct gl_context *ctx,
740 struct gl_vertex_array_object *vao,
741 GLuint index,
742 const char *func)
743 {
744 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
745 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index)", func);
746 return;
747 }
748
749 assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(vao->VertexAttrib));
750
751 if (!vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled) {
752 /* was disabled, now being enabled */
753 FLUSH_VERTICES(ctx, _NEW_ARRAY);
754 vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled = GL_TRUE;
755 vao->_Enabled |= VERT_BIT_GENERIC(index);
756 vao->NewArrays |= VERT_BIT_GENERIC(index);
757 }
758 }
759
760
761 void GLAPIENTRY
762 _mesa_EnableVertexAttribArray(GLuint index)
763 {
764 GET_CURRENT_CONTEXT(ctx);
765 enable_vertex_array_attrib(ctx, ctx->Array.VAO, index,
766 "glEnableVertexAttribArray");
767 }
768
769
770 void GLAPIENTRY
771 _mesa_EnableVertexArrayAttrib(GLuint vaobj, GLuint index)
772 {
773 GET_CURRENT_CONTEXT(ctx);
774 struct gl_vertex_array_object *vao;
775
776 /* The ARB_direct_state_access specification says:
777 *
778 * "An INVALID_OPERATION error is generated by EnableVertexArrayAttrib
779 * and DisableVertexArrayAttrib if <vaobj> is not
780 * [compatibility profile: zero or] the name of an existing vertex
781 * array object."
782 */
783 vao = _mesa_lookup_vao_err(ctx, vaobj, "glEnableVertexArrayAttrib");
784 if (!vao)
785 return;
786
787 enable_vertex_array_attrib(ctx, vao, index, "glEnableVertexArrayAttrib");
788 }
789
790
791 static void
792 disable_vertex_array_attrib(struct gl_context *ctx,
793 struct gl_vertex_array_object *vao,
794 GLuint index,
795 const char *func)
796 {
797 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
798 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index)", func);
799 return;
800 }
801
802 assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(vao->VertexAttrib));
803
804 if (vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled) {
805 /* was enabled, now being disabled */
806 FLUSH_VERTICES(ctx, _NEW_ARRAY);
807 vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled = GL_FALSE;
808 vao->_Enabled &= ~VERT_BIT_GENERIC(index);
809 vao->NewArrays |= VERT_BIT_GENERIC(index);
810 }
811 }
812
813
814 void GLAPIENTRY
815 _mesa_DisableVertexAttribArray(GLuint index)
816 {
817 GET_CURRENT_CONTEXT(ctx);
818 disable_vertex_array_attrib(ctx, ctx->Array.VAO, index,
819 "glDisableVertexAttribArray");
820 }
821
822
823 void GLAPIENTRY
824 _mesa_DisableVertexArrayAttrib(GLuint vaobj, GLuint index)
825 {
826 GET_CURRENT_CONTEXT(ctx);
827 struct gl_vertex_array_object *vao;
828
829 /* The ARB_direct_state_access specification says:
830 *
831 * "An INVALID_OPERATION error is generated by EnableVertexArrayAttrib
832 * and DisableVertexArrayAttrib if <vaobj> is not
833 * [compatibility profile: zero or] the name of an existing vertex
834 * array object."
835 */
836 vao = _mesa_lookup_vao_err(ctx, vaobj, "glDisableVertexArrayAttrib");
837 if (!vao)
838 return;
839
840 disable_vertex_array_attrib(ctx, vao, index, "glDisableVertexArrayAttrib");
841 }
842
843
844 /**
845 * Return info for a vertex attribute array (no alias with legacy
846 * vertex attributes (pos, normal, color, etc)). This function does
847 * not handle the 4-element GL_CURRENT_VERTEX_ATTRIB_ARB query.
848 */
849 static GLuint
850 get_vertex_array_attrib(struct gl_context *ctx, GLuint index, GLenum pname,
851 const char *caller)
852 {
853 const struct gl_vertex_array_object *vao = ctx->Array.VAO;
854 const struct gl_vertex_attrib_array *array;
855
856 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
857 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%u)", caller, index);
858 return 0;
859 }
860
861 assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(vao->VertexAttrib));
862
863 array = &vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)];
864
865 switch (pname) {
866 case GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB:
867 return array->Enabled;
868 case GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB:
869 return (array->Format == GL_BGRA) ? GL_BGRA : array->Size;
870 case GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB:
871 return array->Stride;
872 case GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB:
873 return array->Type;
874 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB:
875 return array->Normalized;
876 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB:
877 return vao->VertexBinding[array->VertexBinding].BufferObj->Name;
878 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
879 if ((_mesa_is_desktop_gl(ctx)
880 && (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))
881 || _mesa_is_gles3(ctx)) {
882 return array->Integer;
883 }
884 goto error;
885 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ARB:
886 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_instanced_arrays)
887 || _mesa_is_gles3(ctx)) {
888 return vao->VertexBinding[array->VertexBinding].InstanceDivisor;
889 }
890 goto error;
891 case GL_VERTEX_ATTRIB_BINDING:
892 if (_mesa_is_desktop_gl(ctx)) {
893 return array->VertexBinding - VERT_ATTRIB_GENERIC0;
894 }
895 goto error;
896 case GL_VERTEX_ATTRIB_RELATIVE_OFFSET:
897 if (_mesa_is_desktop_gl(ctx)) {
898 return array->RelativeOffset;
899 }
900 goto error;
901 default:
902 ; /* fall-through */
903 }
904
905 error:
906 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", caller, pname);
907 return 0;
908 }
909
910
911 static const GLfloat *
912 get_current_attrib(struct gl_context *ctx, GLuint index, const char *function)
913 {
914 if (index == 0) {
915 if (_mesa_attr_zero_aliases_vertex(ctx)) {
916 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(index==0)", function);
917 return NULL;
918 }
919 }
920 else if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
921 _mesa_error(ctx, GL_INVALID_VALUE,
922 "%s(index>=GL_MAX_VERTEX_ATTRIBS)", function);
923 return NULL;
924 }
925
926 assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(ctx->Array.VAO->VertexAttrib));
927
928 FLUSH_CURRENT(ctx, 0);
929 return ctx->Current.Attrib[VERT_ATTRIB_GENERIC(index)];
930 }
931
932 void GLAPIENTRY
933 _mesa_GetVertexAttribfv(GLuint index, GLenum pname, GLfloat *params)
934 {
935 GET_CURRENT_CONTEXT(ctx);
936
937 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
938 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribfv");
939 if (v != NULL) {
940 COPY_4V(params, v);
941 }
942 }
943 else {
944 params[0] = (GLfloat) get_vertex_array_attrib(ctx, index, pname,
945 "glGetVertexAttribfv");
946 }
947 }
948
949
950 void GLAPIENTRY
951 _mesa_GetVertexAttribdv(GLuint index, GLenum pname, GLdouble *params)
952 {
953 GET_CURRENT_CONTEXT(ctx);
954
955 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
956 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribdv");
957 if (v != NULL) {
958 params[0] = (GLdouble) v[0];
959 params[1] = (GLdouble) v[1];
960 params[2] = (GLdouble) v[2];
961 params[3] = (GLdouble) v[3];
962 }
963 }
964 else {
965 params[0] = (GLdouble) get_vertex_array_attrib(ctx, index, pname,
966 "glGetVertexAttribdv");
967 }
968 }
969
970 void GLAPIENTRY
971 _mesa_GetVertexAttribLdv(GLuint index, GLenum pname, GLdouble *params)
972 {
973 GET_CURRENT_CONTEXT(ctx);
974
975 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
976 const GLdouble *v = (const GLdouble *)get_current_attrib(ctx, index, "glGetVertexAttribLdv");
977 if (v != NULL) {
978 params[0] = v[0];
979 params[1] = v[1];
980 params[2] = v[2];
981 params[3] = v[3];
982 }
983 }
984 else {
985 params[0] = (GLdouble) get_vertex_array_attrib(ctx, index, pname,
986 "glGetVertexAttribLdv");
987 }
988 }
989
990 void GLAPIENTRY
991 _mesa_GetVertexAttribiv(GLuint index, GLenum pname, GLint *params)
992 {
993 GET_CURRENT_CONTEXT(ctx);
994
995 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
996 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribiv");
997 if (v != NULL) {
998 /* XXX should floats in[0,1] be scaled to full int range? */
999 params[0] = (GLint) v[0];
1000 params[1] = (GLint) v[1];
1001 params[2] = (GLint) v[2];
1002 params[3] = (GLint) v[3];
1003 }
1004 }
1005 else {
1006 params[0] = (GLint) get_vertex_array_attrib(ctx, index, pname,
1007 "glGetVertexAttribiv");
1008 }
1009 }
1010
1011
1012 /** GL 3.0 */
1013 void GLAPIENTRY
1014 _mesa_GetVertexAttribIiv(GLuint index, GLenum pname, GLint *params)
1015 {
1016 GET_CURRENT_CONTEXT(ctx);
1017
1018 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
1019 const GLint *v = (const GLint *)
1020 get_current_attrib(ctx, index, "glGetVertexAttribIiv");
1021 if (v != NULL) {
1022 COPY_4V(params, v);
1023 }
1024 }
1025 else {
1026 params[0] = (GLint) get_vertex_array_attrib(ctx, index, pname,
1027 "glGetVertexAttribIiv");
1028 }
1029 }
1030
1031
1032 /** GL 3.0 */
1033 void GLAPIENTRY
1034 _mesa_GetVertexAttribIuiv(GLuint index, GLenum pname, GLuint *params)
1035 {
1036 GET_CURRENT_CONTEXT(ctx);
1037
1038 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
1039 const GLuint *v = (const GLuint *)
1040 get_current_attrib(ctx, index, "glGetVertexAttribIuiv");
1041 if (v != NULL) {
1042 COPY_4V(params, v);
1043 }
1044 }
1045 else {
1046 params[0] = get_vertex_array_attrib(ctx, index, pname,
1047 "glGetVertexAttribIuiv");
1048 }
1049 }
1050
1051
1052 void GLAPIENTRY
1053 _mesa_GetVertexAttribPointerv(GLuint index, GLenum pname, GLvoid **pointer)
1054 {
1055 GET_CURRENT_CONTEXT(ctx);
1056
1057 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1058 _mesa_error(ctx, GL_INVALID_VALUE, "glGetVertexAttribPointerARB(index)");
1059 return;
1060 }
1061
1062 if (pname != GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB) {
1063 _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexAttribPointerARB(pname)");
1064 return;
1065 }
1066
1067 assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(ctx->Array.VAO->VertexAttrib));
1068
1069 *pointer = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Ptr;
1070 }
1071
1072
1073 void GLAPIENTRY
1074 _mesa_VertexPointerEXT(GLint size, GLenum type, GLsizei stride,
1075 GLsizei count, const GLvoid *ptr)
1076 {
1077 (void) count;
1078 _mesa_VertexPointer(size, type, stride, ptr);
1079 }
1080
1081
1082 void GLAPIENTRY
1083 _mesa_NormalPointerEXT(GLenum type, GLsizei stride, GLsizei count,
1084 const GLvoid *ptr)
1085 {
1086 (void) count;
1087 _mesa_NormalPointer(type, stride, ptr);
1088 }
1089
1090
1091 void GLAPIENTRY
1092 _mesa_ColorPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count,
1093 const GLvoid *ptr)
1094 {
1095 (void) count;
1096 _mesa_ColorPointer(size, type, stride, ptr);
1097 }
1098
1099
1100 void GLAPIENTRY
1101 _mesa_IndexPointerEXT(GLenum type, GLsizei stride, GLsizei count,
1102 const GLvoid *ptr)
1103 {
1104 (void) count;
1105 _mesa_IndexPointer(type, stride, ptr);
1106 }
1107
1108
1109 void GLAPIENTRY
1110 _mesa_TexCoordPointerEXT(GLint size, GLenum type, GLsizei stride,
1111 GLsizei count, const GLvoid *ptr)
1112 {
1113 (void) count;
1114 _mesa_TexCoordPointer(size, type, stride, ptr);
1115 }
1116
1117
1118 void GLAPIENTRY
1119 _mesa_EdgeFlagPointerEXT(GLsizei stride, GLsizei count, const GLboolean *ptr)
1120 {
1121 (void) count;
1122 _mesa_EdgeFlagPointer(stride, ptr);
1123 }
1124
1125
1126 void GLAPIENTRY
1127 _mesa_InterleavedArrays(GLenum format, GLsizei stride, const GLvoid *pointer)
1128 {
1129 GET_CURRENT_CONTEXT(ctx);
1130 GLboolean tflag, cflag, nflag; /* enable/disable flags */
1131 GLint tcomps, ccomps, vcomps; /* components per texcoord, color, vertex */
1132 GLenum ctype = 0; /* color type */
1133 GLint coffset = 0, noffset = 0, voffset;/* color, normal, vertex offsets */
1134 const GLint toffset = 0; /* always zero */
1135 GLint defstride; /* default stride */
1136 GLint c, f;
1137
1138 FLUSH_VERTICES(ctx, 0);
1139
1140 f = sizeof(GLfloat);
1141 c = f * ((4 * sizeof(GLubyte) + (f - 1)) / f);
1142
1143 if (stride < 0) {
1144 _mesa_error( ctx, GL_INVALID_VALUE, "glInterleavedArrays(stride)" );
1145 return;
1146 }
1147
1148 switch (format) {
1149 case GL_V2F:
1150 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
1151 tcomps = 0; ccomps = 0; vcomps = 2;
1152 voffset = 0;
1153 defstride = 2*f;
1154 break;
1155 case GL_V3F:
1156 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
1157 tcomps = 0; ccomps = 0; vcomps = 3;
1158 voffset = 0;
1159 defstride = 3*f;
1160 break;
1161 case GL_C4UB_V2F:
1162 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1163 tcomps = 0; ccomps = 4; vcomps = 2;
1164 ctype = GL_UNSIGNED_BYTE;
1165 coffset = 0;
1166 voffset = c;
1167 defstride = c + 2*f;
1168 break;
1169 case GL_C4UB_V3F:
1170 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1171 tcomps = 0; ccomps = 4; vcomps = 3;
1172 ctype = GL_UNSIGNED_BYTE;
1173 coffset = 0;
1174 voffset = c;
1175 defstride = c + 3*f;
1176 break;
1177 case GL_C3F_V3F:
1178 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1179 tcomps = 0; ccomps = 3; vcomps = 3;
1180 ctype = GL_FLOAT;
1181 coffset = 0;
1182 voffset = 3*f;
1183 defstride = 6*f;
1184 break;
1185 case GL_N3F_V3F:
1186 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_TRUE;
1187 tcomps = 0; ccomps = 0; vcomps = 3;
1188 noffset = 0;
1189 voffset = 3*f;
1190 defstride = 6*f;
1191 break;
1192 case GL_C4F_N3F_V3F:
1193 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_TRUE;
1194 tcomps = 0; ccomps = 4; vcomps = 3;
1195 ctype = GL_FLOAT;
1196 coffset = 0;
1197 noffset = 4*f;
1198 voffset = 7*f;
1199 defstride = 10*f;
1200 break;
1201 case GL_T2F_V3F:
1202 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
1203 tcomps = 2; ccomps = 0; vcomps = 3;
1204 voffset = 2*f;
1205 defstride = 5*f;
1206 break;
1207 case GL_T4F_V4F:
1208 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
1209 tcomps = 4; ccomps = 0; vcomps = 4;
1210 voffset = 4*f;
1211 defstride = 8*f;
1212 break;
1213 case GL_T2F_C4UB_V3F:
1214 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1215 tcomps = 2; ccomps = 4; vcomps = 3;
1216 ctype = GL_UNSIGNED_BYTE;
1217 coffset = 2*f;
1218 voffset = c+2*f;
1219 defstride = c+5*f;
1220 break;
1221 case GL_T2F_C3F_V3F:
1222 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1223 tcomps = 2; ccomps = 3; vcomps = 3;
1224 ctype = GL_FLOAT;
1225 coffset = 2*f;
1226 voffset = 5*f;
1227 defstride = 8*f;
1228 break;
1229 case GL_T2F_N3F_V3F:
1230 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_TRUE;
1231 tcomps = 2; ccomps = 0; vcomps = 3;
1232 noffset = 2*f;
1233 voffset = 5*f;
1234 defstride = 8*f;
1235 break;
1236 case GL_T2F_C4F_N3F_V3F:
1237 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1238 tcomps = 2; ccomps = 4; vcomps = 3;
1239 ctype = GL_FLOAT;
1240 coffset = 2*f;
1241 noffset = 6*f;
1242 voffset = 9*f;
1243 defstride = 12*f;
1244 break;
1245 case GL_T4F_C4F_N3F_V4F:
1246 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1247 tcomps = 4; ccomps = 4; vcomps = 4;
1248 ctype = GL_FLOAT;
1249 coffset = 4*f;
1250 noffset = 8*f;
1251 voffset = 11*f;
1252 defstride = 15*f;
1253 break;
1254 default:
1255 _mesa_error( ctx, GL_INVALID_ENUM, "glInterleavedArrays(format)" );
1256 return;
1257 }
1258
1259 if (stride==0) {
1260 stride = defstride;
1261 }
1262
1263 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY );
1264 _mesa_DisableClientState( GL_INDEX_ARRAY );
1265 /* XXX also disable secondary color and generic arrays? */
1266
1267 /* Texcoords */
1268 if (tflag) {
1269 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY );
1270 _mesa_TexCoordPointer( tcomps, GL_FLOAT, stride,
1271 (GLubyte *) pointer + toffset );
1272 }
1273 else {
1274 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
1275 }
1276
1277 /* Color */
1278 if (cflag) {
1279 _mesa_EnableClientState( GL_COLOR_ARRAY );
1280 _mesa_ColorPointer( ccomps, ctype, stride,
1281 (GLubyte *) pointer + coffset );
1282 }
1283 else {
1284 _mesa_DisableClientState( GL_COLOR_ARRAY );
1285 }
1286
1287
1288 /* Normals */
1289 if (nflag) {
1290 _mesa_EnableClientState( GL_NORMAL_ARRAY );
1291 _mesa_NormalPointer( GL_FLOAT, stride, (GLubyte *) pointer + noffset );
1292 }
1293 else {
1294 _mesa_DisableClientState( GL_NORMAL_ARRAY );
1295 }
1296
1297 /* Vertices */
1298 _mesa_EnableClientState( GL_VERTEX_ARRAY );
1299 _mesa_VertexPointer( vcomps, GL_FLOAT, stride,
1300 (GLubyte *) pointer + voffset );
1301 }
1302
1303
1304 void GLAPIENTRY
1305 _mesa_LockArraysEXT(GLint first, GLsizei count)
1306 {
1307 GET_CURRENT_CONTEXT(ctx);
1308
1309 FLUSH_VERTICES(ctx, 0);
1310
1311 if (MESA_VERBOSE & VERBOSE_API)
1312 _mesa_debug(ctx, "glLockArrays %d %d\n", first, count);
1313
1314 if (first < 0) {
1315 _mesa_error( ctx, GL_INVALID_VALUE, "glLockArraysEXT(first)" );
1316 return;
1317 }
1318 if (count <= 0) {
1319 _mesa_error( ctx, GL_INVALID_VALUE, "glLockArraysEXT(count)" );
1320 return;
1321 }
1322 if (ctx->Array.LockCount != 0) {
1323 _mesa_error( ctx, GL_INVALID_OPERATION, "glLockArraysEXT(reentry)" );
1324 return;
1325 }
1326
1327 ctx->Array.LockFirst = first;
1328 ctx->Array.LockCount = count;
1329
1330 ctx->NewState |= _NEW_ARRAY;
1331 }
1332
1333
1334 void GLAPIENTRY
1335 _mesa_UnlockArraysEXT( void )
1336 {
1337 GET_CURRENT_CONTEXT(ctx);
1338
1339 FLUSH_VERTICES(ctx, 0);
1340
1341 if (MESA_VERBOSE & VERBOSE_API)
1342 _mesa_debug(ctx, "glUnlockArrays\n");
1343
1344 if (ctx->Array.LockCount == 0) {
1345 _mesa_error( ctx, GL_INVALID_OPERATION, "glUnlockArraysEXT(reexit)" );
1346 return;
1347 }
1348
1349 ctx->Array.LockFirst = 0;
1350 ctx->Array.LockCount = 0;
1351 ctx->NewState |= _NEW_ARRAY;
1352 }
1353
1354
1355 /* GL_EXT_multi_draw_arrays */
1356 void GLAPIENTRY
1357 _mesa_MultiDrawArrays( GLenum mode, const GLint *first,
1358 const GLsizei *count, GLsizei primcount )
1359 {
1360 GET_CURRENT_CONTEXT(ctx);
1361 GLint i;
1362
1363 FLUSH_VERTICES(ctx, 0);
1364
1365 for (i = 0; i < primcount; i++) {
1366 if (count[i] > 0) {
1367 CALL_DrawArrays(ctx->CurrentDispatch, (mode, first[i], count[i]));
1368 }
1369 }
1370 }
1371
1372
1373 /* GL_IBM_multimode_draw_arrays */
1374 void GLAPIENTRY
1375 _mesa_MultiModeDrawArraysIBM( const GLenum * mode, const GLint * first,
1376 const GLsizei * count,
1377 GLsizei primcount, GLint modestride )
1378 {
1379 GET_CURRENT_CONTEXT(ctx);
1380 GLint i;
1381
1382 FLUSH_VERTICES(ctx, 0);
1383
1384 for ( i = 0 ; i < primcount ; i++ ) {
1385 if ( count[i] > 0 ) {
1386 GLenum m = *((GLenum *) ((GLubyte *) mode + i * modestride));
1387 CALL_DrawArrays(ctx->CurrentDispatch, ( m, first[i], count[i] ));
1388 }
1389 }
1390 }
1391
1392
1393 /* GL_IBM_multimode_draw_arrays */
1394 void GLAPIENTRY
1395 _mesa_MultiModeDrawElementsIBM( const GLenum * mode, const GLsizei * count,
1396 GLenum type, const GLvoid * const * indices,
1397 GLsizei primcount, GLint modestride )
1398 {
1399 GET_CURRENT_CONTEXT(ctx);
1400 GLint i;
1401
1402 FLUSH_VERTICES(ctx, 0);
1403
1404 /* XXX not sure about ARB_vertex_buffer_object handling here */
1405
1406 for ( i = 0 ; i < primcount ; i++ ) {
1407 if ( count[i] > 0 ) {
1408 GLenum m = *((GLenum *) ((GLubyte *) mode + i * modestride));
1409 CALL_DrawElements(ctx->CurrentDispatch, ( m, count[i], type,
1410 indices[i] ));
1411 }
1412 }
1413 }
1414
1415
1416 /**
1417 * GL_NV_primitive_restart and GL 3.1
1418 */
1419 void GLAPIENTRY
1420 _mesa_PrimitiveRestartIndex(GLuint index)
1421 {
1422 GET_CURRENT_CONTEXT(ctx);
1423
1424 if (!ctx->Extensions.NV_primitive_restart && ctx->Version < 31) {
1425 _mesa_error(ctx, GL_INVALID_OPERATION, "glPrimitiveRestartIndexNV()");
1426 return;
1427 }
1428
1429 if (ctx->Array.RestartIndex != index) {
1430 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
1431 ctx->Array.RestartIndex = index;
1432 }
1433 }
1434
1435
1436 /**
1437 * See GL_ARB_instanced_arrays.
1438 * Note that the instance divisor only applies to generic arrays, not
1439 * the legacy vertex arrays.
1440 */
1441 void GLAPIENTRY
1442 _mesa_VertexAttribDivisor(GLuint index, GLuint divisor)
1443 {
1444 GET_CURRENT_CONTEXT(ctx);
1445
1446 const GLuint genericIndex = VERT_ATTRIB_GENERIC(index);
1447
1448 if (!ctx->Extensions.ARB_instanced_arrays) {
1449 _mesa_error(ctx, GL_INVALID_OPERATION, "glVertexAttribDivisor()");
1450 return;
1451 }
1452
1453 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1454 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribDivisor(index = %u)",
1455 index);
1456 return;
1457 }
1458
1459 assert(genericIndex < ARRAY_SIZE(ctx->Array.VAO->VertexAttrib));
1460
1461 /* The ARB_vertex_attrib_binding spec says:
1462 *
1463 * "The command
1464 *
1465 * void VertexAttribDivisor(uint index, uint divisor);
1466 *
1467 * is equivalent to (assuming no errors are generated):
1468 *
1469 * VertexAttribBinding(index, index);
1470 * VertexBindingDivisor(index, divisor);"
1471 */
1472 vertex_attrib_binding(ctx, genericIndex, genericIndex);
1473 vertex_binding_divisor(ctx, genericIndex, divisor);
1474 }
1475
1476
1477 unsigned
1478 _mesa_primitive_restart_index(const struct gl_context *ctx, GLenum ib_type)
1479 {
1480 /* From the OpenGL 4.3 core specification, page 302:
1481 * "If both PRIMITIVE_RESTART and PRIMITIVE_RESTART_FIXED_INDEX are
1482 * enabled, the index value determined by PRIMITIVE_RESTART_FIXED_INDEX
1483 * is used."
1484 */
1485 if (ctx->Array.PrimitiveRestartFixedIndex) {
1486 switch (ib_type) {
1487 case GL_UNSIGNED_BYTE:
1488 return 0xff;
1489 case GL_UNSIGNED_SHORT:
1490 return 0xffff;
1491 case GL_UNSIGNED_INT:
1492 return 0xffffffff;
1493 default:
1494 assert(!"_mesa_primitive_restart_index: Invalid index buffer type.");
1495 }
1496 }
1497
1498 return ctx->Array.RestartIndex;
1499 }
1500
1501
1502 /**
1503 * GL_ARB_vertex_attrib_binding
1504 */
1505 static void
1506 vertex_array_vertex_buffer(struct gl_context *ctx, struct gl_vertex_array_object *vao,
1507 GLuint bindingIndex, GLuint buffer, GLintptr offset,
1508 GLsizei stride, const char *func)
1509 {
1510 struct gl_buffer_object *vbo;
1511
1512 ASSERT_OUTSIDE_BEGIN_END(ctx);
1513
1514 /* The ARB_vertex_attrib_binding spec says:
1515 *
1516 * "An INVALID_VALUE error is generated if <bindingindex> is greater than
1517 * the value of MAX_VERTEX_ATTRIB_BINDINGS."
1518 */
1519 if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
1520 _mesa_error(ctx, GL_INVALID_VALUE,
1521 "%s(bindingindex=%u > "
1522 "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
1523 func, bindingIndex);
1524 return;
1525 }
1526
1527 /* The ARB_vertex_attrib_binding spec says:
1528 *
1529 * "The error INVALID_VALUE is generated if <stride> or <offset>
1530 * are negative."
1531 */
1532 if (offset < 0) {
1533 _mesa_error(ctx, GL_INVALID_VALUE,
1534 "%s(offset=%" PRId64 " < 0)",
1535 func, (int64_t) offset);
1536 return;
1537 }
1538
1539 if (stride < 0) {
1540 _mesa_error(ctx, GL_INVALID_VALUE,
1541 "%s(stride=%d < 0)", func, stride);
1542 return;
1543 }
1544
1545 if (ctx->API == API_OPENGL_CORE && ctx->Version >= 44 &&
1546 stride > ctx->Const.MaxVertexAttribStride) {
1547 _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride=%d > "
1548 "GL_MAX_VERTEX_ATTRIB_STRIDE)", func, stride);
1549 return;
1550 }
1551
1552 if (buffer == vao->VertexBinding[VERT_ATTRIB_GENERIC(bindingIndex)].BufferObj->Name) {
1553 vbo = vao->VertexBinding[VERT_ATTRIB_GENERIC(bindingIndex)].BufferObj;
1554 } else if (buffer != 0) {
1555 vbo = _mesa_lookup_bufferobj(ctx, buffer);
1556
1557 /* From the GL_ARB_vertex_attrib_array spec:
1558 *
1559 * "[Core profile only:]
1560 * An INVALID_OPERATION error is generated if buffer is not zero or a
1561 * name returned from a previous call to GenBuffers, or if such a name
1562 * has since been deleted with DeleteBuffers.
1563 *
1564 * Otherwise, we fall back to the same compat profile behavior as other
1565 * object references (automatically gen it).
1566 */
1567 if (!_mesa_handle_bind_buffer_gen(ctx, GL_ARRAY_BUFFER, buffer,
1568 &vbo, func))
1569 return;
1570 } else {
1571 /* The ARB_vertex_attrib_binding spec says:
1572 *
1573 * "If <buffer> is zero, any buffer object attached to this
1574 * bindpoint is detached."
1575 */
1576 vbo = ctx->Shared->NullBufferObj;
1577 }
1578
1579 bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(bindingIndex),
1580 vbo, offset, stride);
1581 }
1582
1583
1584 void GLAPIENTRY
1585 _mesa_BindVertexBuffer(GLuint bindingIndex, GLuint buffer, GLintptr offset,
1586 GLsizei stride)
1587 {
1588 GET_CURRENT_CONTEXT(ctx);
1589
1590 /* The ARB_vertex_attrib_binding spec says:
1591 *
1592 * "An INVALID_OPERATION error is generated if no vertex array object
1593 * is bound."
1594 */
1595 if (ctx->API == API_OPENGL_CORE &&
1596 ctx->Array.VAO == ctx->Array.DefaultVAO) {
1597 _mesa_error(ctx, GL_INVALID_OPERATION,
1598 "glBindVertexBuffer(No array object bound)");
1599 return;
1600 }
1601
1602 vertex_array_vertex_buffer(ctx, ctx->Array.VAO, bindingIndex,
1603 buffer, offset, stride, "glBindVertexBuffer");
1604 }
1605
1606
1607 void GLAPIENTRY
1608 _mesa_VertexArrayVertexBuffer(GLuint vaobj, GLuint bindingIndex, GLuint buffer,
1609 GLintptr offset, GLsizei stride)
1610 {
1611 GET_CURRENT_CONTEXT(ctx);
1612 struct gl_vertex_array_object *vao;
1613
1614 /* The ARB_direct_state_access specification says:
1615 *
1616 * "An INVALID_OPERATION error is generated by VertexArrayVertexBuffer
1617 * if <vaobj> is not [compatibility profile: zero or] the name of an
1618 * existing vertex array object."
1619 */
1620 vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayVertexBuffer");
1621 if (!vao)
1622 return;
1623
1624 vertex_array_vertex_buffer(ctx, vao, bindingIndex,
1625 buffer, offset, stride,
1626 "glVertexArrayVertexBuffer");
1627 }
1628
1629
1630 static void
1631 vertex_array_vertex_buffers(struct gl_context *ctx,
1632 struct gl_vertex_array_object *vao,
1633 GLuint first, GLsizei count, const GLuint *buffers,
1634 const GLintptr *offsets, const GLsizei *strides,
1635 const char *func)
1636 {
1637 GLuint i;
1638
1639 ASSERT_OUTSIDE_BEGIN_END(ctx);
1640
1641 /* The ARB_multi_bind spec says:
1642 *
1643 * "An INVALID_OPERATION error is generated if <first> + <count>
1644 * is greater than the value of MAX_VERTEX_ATTRIB_BINDINGS."
1645 */
1646 if (first + count > ctx->Const.MaxVertexAttribBindings) {
1647 _mesa_error(ctx, GL_INVALID_OPERATION,
1648 "%s(first=%u + count=%d > the value of "
1649 "GL_MAX_VERTEX_ATTRIB_BINDINGS=%u)",
1650 func, first, count, ctx->Const.MaxVertexAttribBindings);
1651 return;
1652 }
1653
1654 if (!buffers) {
1655 /**
1656 * The ARB_multi_bind spec says:
1657 *
1658 * "If <buffers> is NULL, each affected vertex buffer binding point
1659 * from <first> through <first>+<count>-1 will be reset to have no
1660 * bound buffer object. In this case, the offsets and strides
1661 * associated with the binding points are set to default values,
1662 * ignoring <offsets> and <strides>."
1663 */
1664 struct gl_buffer_object *vbo = ctx->Shared->NullBufferObj;
1665
1666 for (i = 0; i < count; i++)
1667 bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(first + i),
1668 vbo, 0, 16);
1669
1670 return;
1671 }
1672
1673 /* Note that the error semantics for multi-bind commands differ from
1674 * those of other GL commands.
1675 *
1676 * The Issues section in the ARB_multi_bind spec says:
1677 *
1678 * "(11) Typically, OpenGL specifies that if an error is generated by
1679 * a command, that command has no effect. This is somewhat
1680 * unfortunate for multi-bind commands, because it would require
1681 * a first pass to scan the entire list of bound objects for
1682 * errors and then a second pass to actually perform the
1683 * bindings. Should we have different error semantics?
1684 *
1685 * RESOLVED: Yes. In this specification, when the parameters for
1686 * one of the <count> binding points are invalid, that binding
1687 * point is not updated and an error will be generated. However,
1688 * other binding points in the same command will be updated if
1689 * their parameters are valid and no other error occurs."
1690 */
1691
1692 _mesa_begin_bufferobj_lookups(ctx);
1693
1694 for (i = 0; i < count; i++) {
1695 struct gl_buffer_object *vbo;
1696
1697 /* The ARB_multi_bind spec says:
1698 *
1699 * "An INVALID_VALUE error is generated if any value in
1700 * <offsets> or <strides> is negative (per binding)."
1701 */
1702 if (offsets[i] < 0) {
1703 _mesa_error(ctx, GL_INVALID_VALUE,
1704 "%s(offsets[%u]=%" PRId64 " < 0)",
1705 func, i, (int64_t) offsets[i]);
1706 continue;
1707 }
1708
1709 if (strides[i] < 0) {
1710 _mesa_error(ctx, GL_INVALID_VALUE,
1711 "%s(strides[%u]=%d < 0)",
1712 func, i, strides[i]);
1713 continue;
1714 }
1715
1716 if (ctx->API == API_OPENGL_CORE && ctx->Version >= 44 &&
1717 strides[i] > ctx->Const.MaxVertexAttribStride) {
1718 _mesa_error(ctx, GL_INVALID_VALUE,
1719 "%s(strides[%u]=%d > "
1720 "GL_MAX_VERTEX_ATTRIB_STRIDE)", func, i, strides[i]);
1721 continue;
1722 }
1723
1724 if (buffers[i]) {
1725 struct gl_vertex_buffer_binding *binding =
1726 &vao->VertexBinding[VERT_ATTRIB_GENERIC(first + i)];
1727
1728 if (buffers[i] == binding->BufferObj->Name)
1729 vbo = binding->BufferObj;
1730 else
1731 vbo = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, func);
1732
1733 if (!vbo)
1734 continue;
1735 } else {
1736 vbo = ctx->Shared->NullBufferObj;
1737 }
1738
1739 bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(first + i),
1740 vbo, offsets[i], strides[i]);
1741 }
1742
1743 _mesa_end_bufferobj_lookups(ctx);
1744 }
1745
1746
1747 void GLAPIENTRY
1748 _mesa_BindVertexBuffers(GLuint first, GLsizei count, const GLuint *buffers,
1749 const GLintptr *offsets, const GLsizei *strides)
1750 {
1751 GET_CURRENT_CONTEXT(ctx);
1752
1753 /* The ARB_vertex_attrib_binding spec says:
1754 *
1755 * "An INVALID_OPERATION error is generated if no
1756 * vertex array object is bound."
1757 */
1758 if (ctx->API == API_OPENGL_CORE &&
1759 ctx->Array.VAO == ctx->Array.DefaultVAO) {
1760 _mesa_error(ctx, GL_INVALID_OPERATION,
1761 "glBindVertexBuffers(No array object bound)");
1762 return;
1763 }
1764
1765 vertex_array_vertex_buffers(ctx, ctx->Array.VAO, first, count,
1766 buffers, offsets, strides,
1767 "glBindVertexBuffers");
1768 }
1769
1770
1771 void GLAPIENTRY
1772 _mesa_VertexArrayVertexBuffers(GLuint vaobj, GLuint first, GLsizei count,
1773 const GLuint *buffers,
1774 const GLintptr *offsets, const GLsizei *strides)
1775 {
1776 GET_CURRENT_CONTEXT(ctx);
1777 struct gl_vertex_array_object *vao;
1778
1779 /* The ARB_direct_state_access specification says:
1780 *
1781 * "An INVALID_OPERATION error is generated by VertexArrayVertexBuffer
1782 * if <vaobj> is not [compatibility profile: zero or] the name of an
1783 * existing vertex array object."
1784 */
1785 vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayVertexBuffers");
1786 if (!vao)
1787 return;
1788
1789 vertex_array_vertex_buffers(ctx, vao, first, count,
1790 buffers, offsets, strides,
1791 "glVertexArrayVertexBuffers");
1792 }
1793
1794
1795 static void
1796 vertex_attrib_format(GLuint attribIndex, GLint size, GLenum type,
1797 GLboolean normalized, GLboolean integer,
1798 GLboolean doubles, GLbitfield legalTypes,
1799 GLsizei maxSize, GLuint relativeOffset,
1800 const char *func)
1801 {
1802 GET_CURRENT_CONTEXT(ctx);
1803 ASSERT_OUTSIDE_BEGIN_END(ctx);
1804
1805 /* The ARB_vertex_attrib_binding spec says:
1806 *
1807 * "An INVALID_OPERATION error is generated under any of the following
1808 * conditions:
1809 * - if no vertex array object is currently bound (see section 2.10);
1810 * - ..."
1811 *
1812 * This error condition only applies to VertexAttribFormat and
1813 * VertexAttribIFormat in the extension spec, but we assume that this
1814 * is an oversight. In the OpenGL 4.3 (Core Profile) spec, it applies
1815 * to all three functions.
1816 */
1817 if (ctx->API == API_OPENGL_CORE &&
1818 ctx->Array.VAO == ctx->Array.DefaultVAO) {
1819 _mesa_error(ctx, GL_INVALID_OPERATION,
1820 "%s(No array object bound)", func);
1821 return;
1822 }
1823
1824 /* The ARB_vertex_attrib_binding spec says:
1825 *
1826 * "The error INVALID_VALUE is generated if index is greater than or equal
1827 * to the value of MAX_VERTEX_ATTRIBS."
1828 */
1829 if (attribIndex >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1830 _mesa_error(ctx, GL_INVALID_VALUE,
1831 "%s(attribindex=%u > "
1832 "GL_MAX_VERTEX_ATTRIBS)",
1833 func, attribIndex);
1834 return;
1835 }
1836
1837 FLUSH_VERTICES(ctx, 0);
1838
1839 update_array_format(ctx, func, VERT_ATTRIB_GENERIC(attribIndex),
1840 legalTypes, 1, maxSize, size, type, normalized,
1841 integer, doubles, relativeOffset);
1842 }
1843
1844
1845 void GLAPIENTRY
1846 _mesa_VertexAttribFormat(GLuint attribIndex, GLint size, GLenum type,
1847 GLboolean normalized, GLuint relativeOffset)
1848 {
1849 vertex_attrib_format(attribIndex, size, type, normalized,
1850 GL_FALSE, GL_FALSE, ATTRIB_FORMAT_TYPES_MASK,
1851 BGRA_OR_4, relativeOffset,
1852 "glVertexAttribFormat");
1853 }
1854
1855
1856 void GLAPIENTRY
1857 _mesa_VertexAttribIFormat(GLuint attribIndex, GLint size, GLenum type,
1858 GLuint relativeOffset)
1859 {
1860 vertex_attrib_format(attribIndex, size, type, GL_FALSE,
1861 GL_TRUE, GL_FALSE, ATTRIB_IFORMAT_TYPES_MASK, 4,
1862 relativeOffset, "glVertexAttribIFormat");
1863 }
1864
1865
1866 void GLAPIENTRY
1867 _mesa_VertexAttribLFormat(GLuint attribIndex, GLint size, GLenum type,
1868 GLuint relativeOffset)
1869 {
1870 vertex_attrib_format(attribIndex, size, type, GL_FALSE, GL_FALSE,
1871 GL_TRUE, ATTRIB_LFORMAT_TYPES_MASK, 4,
1872 relativeOffset, "glVertexAttribLFormat");
1873 }
1874
1875
1876 void GLAPIENTRY
1877 _mesa_VertexAttribBinding(GLuint attribIndex, GLuint bindingIndex)
1878 {
1879 GET_CURRENT_CONTEXT(ctx);
1880 ASSERT_OUTSIDE_BEGIN_END(ctx);
1881
1882 /* The ARB_vertex_attrib_binding spec says:
1883 *
1884 * "An INVALID_OPERATION error is generated if no vertex array object
1885 * is bound."
1886 */
1887 if (ctx->API == API_OPENGL_CORE &&
1888 ctx->Array.VAO == ctx->Array.DefaultVAO) {
1889 _mesa_error(ctx, GL_INVALID_OPERATION,
1890 "glVertexAttribBinding(No array object bound)");
1891 return;
1892 }
1893
1894 /* The ARB_vertex_attrib_binding spec says:
1895 *
1896 * "<attribindex> must be less than the value of MAX_VERTEX_ATTRIBS and
1897 * <bindingindex> must be less than the value of
1898 * MAX_VERTEX_ATTRIB_BINDINGS, otherwise the error INVALID_VALUE
1899 * is generated."
1900 */
1901 if (attribIndex >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1902 _mesa_error(ctx, GL_INVALID_VALUE,
1903 "glVertexAttribBinding(attribindex=%u >= "
1904 "GL_MAX_VERTEX_ATTRIBS)",
1905 attribIndex);
1906 return;
1907 }
1908
1909 if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
1910 _mesa_error(ctx, GL_INVALID_VALUE,
1911 "glVertexAttribBinding(bindingindex=%u >= "
1912 "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
1913 bindingIndex);
1914 return;
1915 }
1916
1917 assert(VERT_ATTRIB_GENERIC(attribIndex) <
1918 ARRAY_SIZE(ctx->Array.VAO->VertexAttrib));
1919
1920 vertex_attrib_binding(ctx, VERT_ATTRIB_GENERIC(attribIndex),
1921 VERT_ATTRIB_GENERIC(bindingIndex));
1922 }
1923
1924
1925 void GLAPIENTRY
1926 _mesa_VertexBindingDivisor(GLuint bindingIndex, GLuint divisor)
1927 {
1928 GET_CURRENT_CONTEXT(ctx);
1929 ASSERT_OUTSIDE_BEGIN_END(ctx);
1930
1931 if (!ctx->Extensions.ARB_instanced_arrays) {
1932 _mesa_error(ctx, GL_INVALID_OPERATION, "glVertexBindingDivisor()");
1933 return;
1934 }
1935
1936 /* The ARB_vertex_attrib_binding spec says:
1937 *
1938 * "An INVALID_OPERATION error is generated if no vertex array object
1939 * is bound."
1940 */
1941 if (ctx->API == API_OPENGL_CORE &&
1942 ctx->Array.VAO == ctx->Array.DefaultVAO) {
1943 _mesa_error(ctx, GL_INVALID_OPERATION,
1944 "glVertexBindingDivisor(No array object bound)");
1945 return;
1946 }
1947
1948 /* The ARB_vertex_attrib_binding spec says:
1949 *
1950 * "An INVALID_VALUE error is generated if <bindingindex> is greater
1951 * than or equal to the value of MAX_VERTEX_ATTRIB_BINDINGS."
1952 */
1953 if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
1954 _mesa_error(ctx, GL_INVALID_VALUE,
1955 "glVertexBindingDivisor(bindingindex=%u > "
1956 "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
1957 bindingIndex);
1958 return;
1959 }
1960
1961 vertex_binding_divisor(ctx, VERT_ATTRIB_GENERIC(bindingIndex), divisor);
1962 }
1963
1964
1965 /**
1966 * Copy one client vertex array to another.
1967 */
1968 void
1969 _mesa_copy_client_array(struct gl_context *ctx,
1970 struct gl_client_array *dst,
1971 struct gl_client_array *src)
1972 {
1973 dst->Size = src->Size;
1974 dst->Type = src->Type;
1975 dst->Format = src->Format;
1976 dst->Stride = src->Stride;
1977 dst->StrideB = src->StrideB;
1978 dst->Ptr = src->Ptr;
1979 dst->Enabled = src->Enabled;
1980 dst->Normalized = src->Normalized;
1981 dst->Integer = src->Integer;
1982 dst->Doubles = src->Doubles;
1983 dst->InstanceDivisor = src->InstanceDivisor;
1984 dst->_ElementSize = src->_ElementSize;
1985 _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1986 }
1987
1988 void
1989 _mesa_copy_vertex_attrib_array(struct gl_context *ctx,
1990 struct gl_vertex_attrib_array *dst,
1991 const struct gl_vertex_attrib_array *src)
1992 {
1993 dst->Size = src->Size;
1994 dst->Type = src->Type;
1995 dst->Format = src->Format;
1996 dst->VertexBinding = src->VertexBinding;
1997 dst->RelativeOffset = src->RelativeOffset;
1998 dst->Format = src->Format;
1999 dst->Integer = src->Integer;
2000 dst->Doubles = src->Doubles;
2001 dst->Normalized = src->Normalized;
2002 dst->Ptr = src->Ptr;
2003 dst->Enabled = src->Enabled;
2004 dst->_ElementSize = src->_ElementSize;
2005 }
2006
2007 void
2008 _mesa_copy_vertex_buffer_binding(struct gl_context *ctx,
2009 struct gl_vertex_buffer_binding *dst,
2010 const struct gl_vertex_buffer_binding *src)
2011 {
2012 dst->Offset = src->Offset;
2013 dst->Stride = src->Stride;
2014 dst->InstanceDivisor = src->InstanceDivisor;
2015 dst->_BoundArrays = src->_BoundArrays;
2016
2017 _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
2018 }
2019
2020 /**
2021 * Print vertex array's fields.
2022 */
2023 static void
2024 print_array(const char *name, GLint index, const struct gl_client_array *array)
2025 {
2026 if (index >= 0)
2027 fprintf(stderr, " %s[%d]: ", name, index);
2028 else
2029 fprintf(stderr, " %s: ", name);
2030 fprintf(stderr, "Ptr=%p, Type=0x%x, Size=%d, ElemSize=%u, Stride=%d, Buffer=%u(Size %lu)\n",
2031 array->Ptr, array->Type, array->Size,
2032 array->_ElementSize, array->StrideB,
2033 array->BufferObj->Name, (unsigned long) array->BufferObj->Size);
2034 }
2035
2036
2037 /**
2038 * Print current vertex object/array info. For debug.
2039 */
2040 void
2041 _mesa_print_arrays(struct gl_context *ctx)
2042 {
2043 struct gl_vertex_array_object *vao = ctx->Array.VAO;
2044 GLuint i;
2045
2046 printf("Array Object %u\n", vao->Name);
2047 if (vao->_VertexAttrib[VERT_ATTRIB_POS].Enabled)
2048 print_array("Vertex", -1, &vao->_VertexAttrib[VERT_ATTRIB_POS]);
2049 if (vao->_VertexAttrib[VERT_ATTRIB_NORMAL].Enabled)
2050 print_array("Normal", -1, &vao->_VertexAttrib[VERT_ATTRIB_NORMAL]);
2051 if (vao->_VertexAttrib[VERT_ATTRIB_COLOR0].Enabled)
2052 print_array("Color", -1, &vao->_VertexAttrib[VERT_ATTRIB_COLOR0]);
2053 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++)
2054 if (vao->_VertexAttrib[VERT_ATTRIB_TEX(i)].Enabled)
2055 print_array("TexCoord", i, &vao->_VertexAttrib[VERT_ATTRIB_TEX(i)]);
2056 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++)
2057 if (vao->_VertexAttrib[VERT_ATTRIB_GENERIC(i)].Enabled)
2058 print_array("Attrib", i, &vao->_VertexAttrib[VERT_ATTRIB_GENERIC(i)]);
2059 }
2060
2061
2062 /**
2063 * Initialize vertex array state for given context.
2064 */
2065 void
2066 _mesa_init_varray(struct gl_context *ctx)
2067 {
2068 ctx->Array.DefaultVAO = ctx->Driver.NewArrayObject(ctx, 0);
2069 _mesa_reference_vao(ctx, &ctx->Array.VAO, ctx->Array.DefaultVAO);
2070 ctx->Array.ActiveTexture = 0; /* GL_ARB_multitexture */
2071
2072 ctx->Array.Objects = _mesa_NewHashTable();
2073 }
2074
2075
2076 /**
2077 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
2078 */
2079 static void
2080 delete_arrayobj_cb(GLuint id, void *data, void *userData)
2081 {
2082 struct gl_vertex_array_object *vao = (struct gl_vertex_array_object *) data;
2083 struct gl_context *ctx = (struct gl_context *) userData;
2084 _mesa_delete_vao(ctx, vao);
2085 }
2086
2087
2088 /**
2089 * Free vertex array state for given context.
2090 */
2091 void
2092 _mesa_free_varray_data(struct gl_context *ctx)
2093 {
2094 _mesa_HashDeleteAll(ctx->Array.Objects, delete_arrayobj_cb, ctx);
2095 _mesa_DeleteHashTable(ctx->Array.Objects);
2096 }