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