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