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