3afbea8ae0681616acd85d8c0952bedf1d8a9679
[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, GLuint index, GLenum pname,
855 const char *caller)
856 {
857 const struct gl_vertex_array_object *vao = ctx->Array.VAO;
858 const struct gl_vertex_attrib_array *array;
859
860 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
861 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%u)", caller, index);
862 return 0;
863 }
864
865 assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(vao->VertexAttrib));
866
867 array = &vao->VertexAttrib[VERT_ATTRIB_GENERIC(index)];
868
869 switch (pname) {
870 case GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB:
871 return array->Enabled;
872 case GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB:
873 return (array->Format == GL_BGRA) ? GL_BGRA : array->Size;
874 case GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB:
875 return array->Stride;
876 case GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB:
877 return array->Type;
878 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB:
879 return array->Normalized;
880 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB:
881 return vao->VertexBinding[array->VertexBinding].BufferObj->Name;
882 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
883 if ((_mesa_is_desktop_gl(ctx)
884 && (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))
885 || _mesa_is_gles3(ctx)) {
886 return array->Integer;
887 }
888 goto error;
889 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ARB:
890 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_instanced_arrays)
891 || _mesa_is_gles3(ctx)) {
892 return vao->VertexBinding[array->VertexBinding].InstanceDivisor;
893 }
894 goto error;
895 case GL_VERTEX_ATTRIB_BINDING:
896 if (_mesa_is_desktop_gl(ctx)) {
897 return array->VertexBinding - VERT_ATTRIB_GENERIC0;
898 }
899 goto error;
900 case GL_VERTEX_ATTRIB_RELATIVE_OFFSET:
901 if (_mesa_is_desktop_gl(ctx)) {
902 return array->RelativeOffset;
903 }
904 goto error;
905 default:
906 ; /* fall-through */
907 }
908
909 error:
910 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", caller, pname);
911 return 0;
912 }
913
914
915 static const GLfloat *
916 get_current_attrib(struct gl_context *ctx, GLuint index, const char *function)
917 {
918 if (index == 0) {
919 if (_mesa_attr_zero_aliases_vertex(ctx)) {
920 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(index==0)", function);
921 return NULL;
922 }
923 }
924 else if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
925 _mesa_error(ctx, GL_INVALID_VALUE,
926 "%s(index>=GL_MAX_VERTEX_ATTRIBS)", function);
927 return NULL;
928 }
929
930 assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(ctx->Array.VAO->VertexAttrib));
931
932 FLUSH_CURRENT(ctx, 0);
933 return ctx->Current.Attrib[VERT_ATTRIB_GENERIC(index)];
934 }
935
936 void GLAPIENTRY
937 _mesa_GetVertexAttribfv(GLuint index, GLenum pname, GLfloat *params)
938 {
939 GET_CURRENT_CONTEXT(ctx);
940
941 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
942 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribfv");
943 if (v != NULL) {
944 COPY_4V(params, v);
945 }
946 }
947 else {
948 params[0] = (GLfloat) get_vertex_array_attrib(ctx, index, pname,
949 "glGetVertexAttribfv");
950 }
951 }
952
953
954 void GLAPIENTRY
955 _mesa_GetVertexAttribdv(GLuint index, GLenum pname, GLdouble *params)
956 {
957 GET_CURRENT_CONTEXT(ctx);
958
959 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
960 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribdv");
961 if (v != NULL) {
962 params[0] = (GLdouble) v[0];
963 params[1] = (GLdouble) v[1];
964 params[2] = (GLdouble) v[2];
965 params[3] = (GLdouble) v[3];
966 }
967 }
968 else {
969 params[0] = (GLdouble) get_vertex_array_attrib(ctx, index, pname,
970 "glGetVertexAttribdv");
971 }
972 }
973
974 void GLAPIENTRY
975 _mesa_GetVertexAttribLdv(GLuint index, GLenum pname, GLdouble *params)
976 {
977 GET_CURRENT_CONTEXT(ctx);
978
979 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
980 const GLdouble *v = (const GLdouble *)get_current_attrib(ctx, index, "glGetVertexAttribLdv");
981 if (v != NULL) {
982 params[0] = v[0];
983 params[1] = v[1];
984 params[2] = v[2];
985 params[3] = v[3];
986 }
987 }
988 else {
989 params[0] = (GLdouble) get_vertex_array_attrib(ctx, index, pname,
990 "glGetVertexAttribLdv");
991 }
992 }
993
994 void GLAPIENTRY
995 _mesa_GetVertexAttribiv(GLuint index, GLenum pname, GLint *params)
996 {
997 GET_CURRENT_CONTEXT(ctx);
998
999 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
1000 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribiv");
1001 if (v != NULL) {
1002 /* XXX should floats in[0,1] be scaled to full int range? */
1003 params[0] = (GLint) v[0];
1004 params[1] = (GLint) v[1];
1005 params[2] = (GLint) v[2];
1006 params[3] = (GLint) v[3];
1007 }
1008 }
1009 else {
1010 params[0] = (GLint) get_vertex_array_attrib(ctx, index, pname,
1011 "glGetVertexAttribiv");
1012 }
1013 }
1014
1015
1016 /** GL 3.0 */
1017 void GLAPIENTRY
1018 _mesa_GetVertexAttribIiv(GLuint index, GLenum pname, GLint *params)
1019 {
1020 GET_CURRENT_CONTEXT(ctx);
1021
1022 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
1023 const GLint *v = (const GLint *)
1024 get_current_attrib(ctx, index, "glGetVertexAttribIiv");
1025 if (v != NULL) {
1026 COPY_4V(params, v);
1027 }
1028 }
1029 else {
1030 params[0] = (GLint) get_vertex_array_attrib(ctx, index, pname,
1031 "glGetVertexAttribIiv");
1032 }
1033 }
1034
1035
1036 /** GL 3.0 */
1037 void GLAPIENTRY
1038 _mesa_GetVertexAttribIuiv(GLuint index, GLenum pname, GLuint *params)
1039 {
1040 GET_CURRENT_CONTEXT(ctx);
1041
1042 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
1043 const GLuint *v = (const GLuint *)
1044 get_current_attrib(ctx, index, "glGetVertexAttribIuiv");
1045 if (v != NULL) {
1046 COPY_4V(params, v);
1047 }
1048 }
1049 else {
1050 params[0] = get_vertex_array_attrib(ctx, index, pname,
1051 "glGetVertexAttribIuiv");
1052 }
1053 }
1054
1055
1056 void GLAPIENTRY
1057 _mesa_GetVertexAttribPointerv(GLuint index, GLenum pname, GLvoid **pointer)
1058 {
1059 GET_CURRENT_CONTEXT(ctx);
1060
1061 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1062 _mesa_error(ctx, GL_INVALID_VALUE, "glGetVertexAttribPointerARB(index)");
1063 return;
1064 }
1065
1066 if (pname != GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB) {
1067 _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexAttribPointerARB(pname)");
1068 return;
1069 }
1070
1071 assert(VERT_ATTRIB_GENERIC(index) < ARRAY_SIZE(ctx->Array.VAO->VertexAttrib));
1072
1073 *pointer = (GLvoid *) ctx->Array.VAO->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Ptr;
1074 }
1075
1076
1077 void GLAPIENTRY
1078 _mesa_VertexPointerEXT(GLint size, GLenum type, GLsizei stride,
1079 GLsizei count, const GLvoid *ptr)
1080 {
1081 (void) count;
1082 _mesa_VertexPointer(size, type, stride, ptr);
1083 }
1084
1085
1086 void GLAPIENTRY
1087 _mesa_NormalPointerEXT(GLenum type, GLsizei stride, GLsizei count,
1088 const GLvoid *ptr)
1089 {
1090 (void) count;
1091 _mesa_NormalPointer(type, stride, ptr);
1092 }
1093
1094
1095 void GLAPIENTRY
1096 _mesa_ColorPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count,
1097 const GLvoid *ptr)
1098 {
1099 (void) count;
1100 _mesa_ColorPointer(size, type, stride, ptr);
1101 }
1102
1103
1104 void GLAPIENTRY
1105 _mesa_IndexPointerEXT(GLenum type, GLsizei stride, GLsizei count,
1106 const GLvoid *ptr)
1107 {
1108 (void) count;
1109 _mesa_IndexPointer(type, stride, ptr);
1110 }
1111
1112
1113 void GLAPIENTRY
1114 _mesa_TexCoordPointerEXT(GLint size, GLenum type, GLsizei stride,
1115 GLsizei count, const GLvoid *ptr)
1116 {
1117 (void) count;
1118 _mesa_TexCoordPointer(size, type, stride, ptr);
1119 }
1120
1121
1122 void GLAPIENTRY
1123 _mesa_EdgeFlagPointerEXT(GLsizei stride, GLsizei count, const GLboolean *ptr)
1124 {
1125 (void) count;
1126 _mesa_EdgeFlagPointer(stride, ptr);
1127 }
1128
1129
1130 void GLAPIENTRY
1131 _mesa_InterleavedArrays(GLenum format, GLsizei stride, const GLvoid *pointer)
1132 {
1133 GET_CURRENT_CONTEXT(ctx);
1134 GLboolean tflag, cflag, nflag; /* enable/disable flags */
1135 GLint tcomps, ccomps, vcomps; /* components per texcoord, color, vertex */
1136 GLenum ctype = 0; /* color type */
1137 GLint coffset = 0, noffset = 0, voffset;/* color, normal, vertex offsets */
1138 const GLint toffset = 0; /* always zero */
1139 GLint defstride; /* default stride */
1140 GLint c, f;
1141
1142 FLUSH_VERTICES(ctx, 0);
1143
1144 f = sizeof(GLfloat);
1145 c = f * ((4 * sizeof(GLubyte) + (f - 1)) / f);
1146
1147 if (stride < 0) {
1148 _mesa_error( ctx, GL_INVALID_VALUE, "glInterleavedArrays(stride)" );
1149 return;
1150 }
1151
1152 switch (format) {
1153 case GL_V2F:
1154 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
1155 tcomps = 0; ccomps = 0; vcomps = 2;
1156 voffset = 0;
1157 defstride = 2*f;
1158 break;
1159 case GL_V3F:
1160 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
1161 tcomps = 0; ccomps = 0; vcomps = 3;
1162 voffset = 0;
1163 defstride = 3*f;
1164 break;
1165 case GL_C4UB_V2F:
1166 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1167 tcomps = 0; ccomps = 4; vcomps = 2;
1168 ctype = GL_UNSIGNED_BYTE;
1169 coffset = 0;
1170 voffset = c;
1171 defstride = c + 2*f;
1172 break;
1173 case GL_C4UB_V3F:
1174 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1175 tcomps = 0; ccomps = 4; vcomps = 3;
1176 ctype = GL_UNSIGNED_BYTE;
1177 coffset = 0;
1178 voffset = c;
1179 defstride = c + 3*f;
1180 break;
1181 case GL_C3F_V3F:
1182 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1183 tcomps = 0; ccomps = 3; vcomps = 3;
1184 ctype = GL_FLOAT;
1185 coffset = 0;
1186 voffset = 3*f;
1187 defstride = 6*f;
1188 break;
1189 case GL_N3F_V3F:
1190 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_TRUE;
1191 tcomps = 0; ccomps = 0; vcomps = 3;
1192 noffset = 0;
1193 voffset = 3*f;
1194 defstride = 6*f;
1195 break;
1196 case GL_C4F_N3F_V3F:
1197 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_TRUE;
1198 tcomps = 0; ccomps = 4; vcomps = 3;
1199 ctype = GL_FLOAT;
1200 coffset = 0;
1201 noffset = 4*f;
1202 voffset = 7*f;
1203 defstride = 10*f;
1204 break;
1205 case GL_T2F_V3F:
1206 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
1207 tcomps = 2; ccomps = 0; vcomps = 3;
1208 voffset = 2*f;
1209 defstride = 5*f;
1210 break;
1211 case GL_T4F_V4F:
1212 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
1213 tcomps = 4; ccomps = 0; vcomps = 4;
1214 voffset = 4*f;
1215 defstride = 8*f;
1216 break;
1217 case GL_T2F_C4UB_V3F:
1218 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1219 tcomps = 2; ccomps = 4; vcomps = 3;
1220 ctype = GL_UNSIGNED_BYTE;
1221 coffset = 2*f;
1222 voffset = c+2*f;
1223 defstride = c+5*f;
1224 break;
1225 case GL_T2F_C3F_V3F:
1226 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1227 tcomps = 2; ccomps = 3; vcomps = 3;
1228 ctype = GL_FLOAT;
1229 coffset = 2*f;
1230 voffset = 5*f;
1231 defstride = 8*f;
1232 break;
1233 case GL_T2F_N3F_V3F:
1234 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_TRUE;
1235 tcomps = 2; ccomps = 0; vcomps = 3;
1236 noffset = 2*f;
1237 voffset = 5*f;
1238 defstride = 8*f;
1239 break;
1240 case GL_T2F_C4F_N3F_V3F:
1241 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1242 tcomps = 2; ccomps = 4; vcomps = 3;
1243 ctype = GL_FLOAT;
1244 coffset = 2*f;
1245 noffset = 6*f;
1246 voffset = 9*f;
1247 defstride = 12*f;
1248 break;
1249 case GL_T4F_C4F_N3F_V4F:
1250 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1251 tcomps = 4; ccomps = 4; vcomps = 4;
1252 ctype = GL_FLOAT;
1253 coffset = 4*f;
1254 noffset = 8*f;
1255 voffset = 11*f;
1256 defstride = 15*f;
1257 break;
1258 default:
1259 _mesa_error( ctx, GL_INVALID_ENUM, "glInterleavedArrays(format)" );
1260 return;
1261 }
1262
1263 if (stride==0) {
1264 stride = defstride;
1265 }
1266
1267 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY );
1268 _mesa_DisableClientState( GL_INDEX_ARRAY );
1269 /* XXX also disable secondary color and generic arrays? */
1270
1271 /* Texcoords */
1272 if (tflag) {
1273 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY );
1274 _mesa_TexCoordPointer( tcomps, GL_FLOAT, stride,
1275 (GLubyte *) pointer + toffset );
1276 }
1277 else {
1278 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
1279 }
1280
1281 /* Color */
1282 if (cflag) {
1283 _mesa_EnableClientState( GL_COLOR_ARRAY );
1284 _mesa_ColorPointer( ccomps, ctype, stride,
1285 (GLubyte *) pointer + coffset );
1286 }
1287 else {
1288 _mesa_DisableClientState( GL_COLOR_ARRAY );
1289 }
1290
1291
1292 /* Normals */
1293 if (nflag) {
1294 _mesa_EnableClientState( GL_NORMAL_ARRAY );
1295 _mesa_NormalPointer( GL_FLOAT, stride, (GLubyte *) pointer + noffset );
1296 }
1297 else {
1298 _mesa_DisableClientState( GL_NORMAL_ARRAY );
1299 }
1300
1301 /* Vertices */
1302 _mesa_EnableClientState( GL_VERTEX_ARRAY );
1303 _mesa_VertexPointer( vcomps, GL_FLOAT, stride,
1304 (GLubyte *) pointer + voffset );
1305 }
1306
1307
1308 void GLAPIENTRY
1309 _mesa_LockArraysEXT(GLint first, GLsizei count)
1310 {
1311 GET_CURRENT_CONTEXT(ctx);
1312
1313 FLUSH_VERTICES(ctx, 0);
1314
1315 if (MESA_VERBOSE & VERBOSE_API)
1316 _mesa_debug(ctx, "glLockArrays %d %d\n", first, count);
1317
1318 if (first < 0) {
1319 _mesa_error( ctx, GL_INVALID_VALUE, "glLockArraysEXT(first)" );
1320 return;
1321 }
1322 if (count <= 0) {
1323 _mesa_error( ctx, GL_INVALID_VALUE, "glLockArraysEXT(count)" );
1324 return;
1325 }
1326 if (ctx->Array.LockCount != 0) {
1327 _mesa_error( ctx, GL_INVALID_OPERATION, "glLockArraysEXT(reentry)" );
1328 return;
1329 }
1330
1331 ctx->Array.LockFirst = first;
1332 ctx->Array.LockCount = count;
1333
1334 ctx->NewState |= _NEW_ARRAY;
1335 }
1336
1337
1338 void GLAPIENTRY
1339 _mesa_UnlockArraysEXT( void )
1340 {
1341 GET_CURRENT_CONTEXT(ctx);
1342
1343 FLUSH_VERTICES(ctx, 0);
1344
1345 if (MESA_VERBOSE & VERBOSE_API)
1346 _mesa_debug(ctx, "glUnlockArrays\n");
1347
1348 if (ctx->Array.LockCount == 0) {
1349 _mesa_error( ctx, GL_INVALID_OPERATION, "glUnlockArraysEXT(reexit)" );
1350 return;
1351 }
1352
1353 ctx->Array.LockFirst = 0;
1354 ctx->Array.LockCount = 0;
1355 ctx->NewState |= _NEW_ARRAY;
1356 }
1357
1358
1359 /* GL_EXT_multi_draw_arrays */
1360 void GLAPIENTRY
1361 _mesa_MultiDrawArrays( GLenum mode, const GLint *first,
1362 const GLsizei *count, GLsizei primcount )
1363 {
1364 GET_CURRENT_CONTEXT(ctx);
1365 GLint i;
1366
1367 FLUSH_VERTICES(ctx, 0);
1368
1369 for (i = 0; i < primcount; i++) {
1370 if (count[i] > 0) {
1371 CALL_DrawArrays(ctx->CurrentDispatch, (mode, first[i], count[i]));
1372 }
1373 }
1374 }
1375
1376
1377 /* GL_IBM_multimode_draw_arrays */
1378 void GLAPIENTRY
1379 _mesa_MultiModeDrawArraysIBM( const GLenum * mode, const GLint * first,
1380 const GLsizei * count,
1381 GLsizei primcount, GLint modestride )
1382 {
1383 GET_CURRENT_CONTEXT(ctx);
1384 GLint i;
1385
1386 FLUSH_VERTICES(ctx, 0);
1387
1388 for ( i = 0 ; i < primcount ; i++ ) {
1389 if ( count[i] > 0 ) {
1390 GLenum m = *((GLenum *) ((GLubyte *) mode + i * modestride));
1391 CALL_DrawArrays(ctx->CurrentDispatch, ( m, first[i], count[i] ));
1392 }
1393 }
1394 }
1395
1396
1397 /* GL_IBM_multimode_draw_arrays */
1398 void GLAPIENTRY
1399 _mesa_MultiModeDrawElementsIBM( const GLenum * mode, const GLsizei * count,
1400 GLenum type, const GLvoid * const * indices,
1401 GLsizei primcount, GLint modestride )
1402 {
1403 GET_CURRENT_CONTEXT(ctx);
1404 GLint i;
1405
1406 FLUSH_VERTICES(ctx, 0);
1407
1408 /* XXX not sure about ARB_vertex_buffer_object handling here */
1409
1410 for ( i = 0 ; i < primcount ; i++ ) {
1411 if ( count[i] > 0 ) {
1412 GLenum m = *((GLenum *) ((GLubyte *) mode + i * modestride));
1413 CALL_DrawElements(ctx->CurrentDispatch, ( m, count[i], type,
1414 indices[i] ));
1415 }
1416 }
1417 }
1418
1419
1420 /**
1421 * GL_NV_primitive_restart and GL 3.1
1422 */
1423 void GLAPIENTRY
1424 _mesa_PrimitiveRestartIndex(GLuint index)
1425 {
1426 GET_CURRENT_CONTEXT(ctx);
1427
1428 if (!ctx->Extensions.NV_primitive_restart && ctx->Version < 31) {
1429 _mesa_error(ctx, GL_INVALID_OPERATION, "glPrimitiveRestartIndexNV()");
1430 return;
1431 }
1432
1433 if (ctx->Array.RestartIndex != index) {
1434 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
1435 ctx->Array.RestartIndex = index;
1436 }
1437 }
1438
1439
1440 /**
1441 * See GL_ARB_instanced_arrays.
1442 * Note that the instance divisor only applies to generic arrays, not
1443 * the legacy vertex arrays.
1444 */
1445 void GLAPIENTRY
1446 _mesa_VertexAttribDivisor(GLuint index, GLuint divisor)
1447 {
1448 GET_CURRENT_CONTEXT(ctx);
1449
1450 const GLuint genericIndex = VERT_ATTRIB_GENERIC(index);
1451 struct gl_vertex_array_object * const vao = ctx->Array.VAO;
1452
1453 if (!ctx->Extensions.ARB_instanced_arrays) {
1454 _mesa_error(ctx, GL_INVALID_OPERATION, "glVertexAttribDivisor()");
1455 return;
1456 }
1457
1458 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1459 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribDivisor(index = %u)",
1460 index);
1461 return;
1462 }
1463
1464 assert(genericIndex < ARRAY_SIZE(vao->VertexAttrib));
1465
1466 /* The ARB_vertex_attrib_binding spec says:
1467 *
1468 * "The command
1469 *
1470 * void VertexAttribDivisor(uint index, uint divisor);
1471 *
1472 * is equivalent to (assuming no errors are generated):
1473 *
1474 * VertexAttribBinding(index, index);
1475 * VertexBindingDivisor(index, divisor);"
1476 */
1477 vertex_attrib_binding(ctx, vao, genericIndex, genericIndex);
1478 vertex_binding_divisor(ctx, vao, genericIndex, divisor);
1479 }
1480
1481
1482 unsigned
1483 _mesa_primitive_restart_index(const struct gl_context *ctx, GLenum ib_type)
1484 {
1485 /* From the OpenGL 4.3 core specification, page 302:
1486 * "If both PRIMITIVE_RESTART and PRIMITIVE_RESTART_FIXED_INDEX are
1487 * enabled, the index value determined by PRIMITIVE_RESTART_FIXED_INDEX
1488 * is used."
1489 */
1490 if (ctx->Array.PrimitiveRestartFixedIndex) {
1491 switch (ib_type) {
1492 case GL_UNSIGNED_BYTE:
1493 return 0xff;
1494 case GL_UNSIGNED_SHORT:
1495 return 0xffff;
1496 case GL_UNSIGNED_INT:
1497 return 0xffffffff;
1498 default:
1499 assert(!"_mesa_primitive_restart_index: Invalid index buffer type.");
1500 }
1501 }
1502
1503 return ctx->Array.RestartIndex;
1504 }
1505
1506
1507 /**
1508 * GL_ARB_vertex_attrib_binding
1509 */
1510 static void
1511 vertex_array_vertex_buffer(struct gl_context *ctx, struct gl_vertex_array_object *vao,
1512 GLuint bindingIndex, GLuint buffer, GLintptr offset,
1513 GLsizei stride, const char *func)
1514 {
1515 struct gl_buffer_object *vbo;
1516
1517 ASSERT_OUTSIDE_BEGIN_END(ctx);
1518
1519 /* The ARB_vertex_attrib_binding spec says:
1520 *
1521 * "An INVALID_VALUE error is generated if <bindingindex> is greater than
1522 * the value of MAX_VERTEX_ATTRIB_BINDINGS."
1523 */
1524 if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
1525 _mesa_error(ctx, GL_INVALID_VALUE,
1526 "%s(bindingindex=%u > "
1527 "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
1528 func, bindingIndex);
1529 return;
1530 }
1531
1532 /* The ARB_vertex_attrib_binding spec says:
1533 *
1534 * "The error INVALID_VALUE is generated if <stride> or <offset>
1535 * are negative."
1536 */
1537 if (offset < 0) {
1538 _mesa_error(ctx, GL_INVALID_VALUE,
1539 "%s(offset=%" PRId64 " < 0)",
1540 func, (int64_t) offset);
1541 return;
1542 }
1543
1544 if (stride < 0) {
1545 _mesa_error(ctx, GL_INVALID_VALUE,
1546 "%s(stride=%d < 0)", func, stride);
1547 return;
1548 }
1549
1550 if (ctx->API == API_OPENGL_CORE && ctx->Version >= 44 &&
1551 stride > ctx->Const.MaxVertexAttribStride) {
1552 _mesa_error(ctx, GL_INVALID_VALUE, "%s(stride=%d > "
1553 "GL_MAX_VERTEX_ATTRIB_STRIDE)", func, stride);
1554 return;
1555 }
1556
1557 if (buffer == vao->VertexBinding[VERT_ATTRIB_GENERIC(bindingIndex)].BufferObj->Name) {
1558 vbo = vao->VertexBinding[VERT_ATTRIB_GENERIC(bindingIndex)].BufferObj;
1559 } else if (buffer != 0) {
1560 vbo = _mesa_lookup_bufferobj(ctx, buffer);
1561
1562 /* From the GL_ARB_vertex_attrib_array spec:
1563 *
1564 * "[Core profile only:]
1565 * An INVALID_OPERATION error is generated if buffer is not zero or a
1566 * name returned from a previous call to GenBuffers, or if such a name
1567 * has since been deleted with DeleteBuffers.
1568 *
1569 * Otherwise, we fall back to the same compat profile behavior as other
1570 * object references (automatically gen it).
1571 */
1572 if (!_mesa_handle_bind_buffer_gen(ctx, GL_ARRAY_BUFFER, buffer,
1573 &vbo, func))
1574 return;
1575 } else {
1576 /* The ARB_vertex_attrib_binding spec says:
1577 *
1578 * "If <buffer> is zero, any buffer object attached to this
1579 * bindpoint is detached."
1580 */
1581 vbo = ctx->Shared->NullBufferObj;
1582 }
1583
1584 bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(bindingIndex),
1585 vbo, offset, stride);
1586 }
1587
1588
1589 void GLAPIENTRY
1590 _mesa_BindVertexBuffer(GLuint bindingIndex, GLuint buffer, GLintptr offset,
1591 GLsizei stride)
1592 {
1593 GET_CURRENT_CONTEXT(ctx);
1594
1595 /* The ARB_vertex_attrib_binding spec says:
1596 *
1597 * "An INVALID_OPERATION error is generated if no vertex array object
1598 * is bound."
1599 */
1600 if (ctx->API == API_OPENGL_CORE &&
1601 ctx->Array.VAO == ctx->Array.DefaultVAO) {
1602 _mesa_error(ctx, GL_INVALID_OPERATION,
1603 "glBindVertexBuffer(No array object bound)");
1604 return;
1605 }
1606
1607 vertex_array_vertex_buffer(ctx, ctx->Array.VAO, bindingIndex,
1608 buffer, offset, stride, "glBindVertexBuffer");
1609 }
1610
1611
1612 void GLAPIENTRY
1613 _mesa_VertexArrayVertexBuffer(GLuint vaobj, GLuint bindingIndex, GLuint buffer,
1614 GLintptr offset, GLsizei stride)
1615 {
1616 GET_CURRENT_CONTEXT(ctx);
1617 struct gl_vertex_array_object *vao;
1618
1619 /* The ARB_direct_state_access specification says:
1620 *
1621 * "An INVALID_OPERATION error is generated by VertexArrayVertexBuffer
1622 * if <vaobj> is not [compatibility profile: zero or] the name of an
1623 * existing vertex array object."
1624 */
1625 vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayVertexBuffer");
1626 if (!vao)
1627 return;
1628
1629 vertex_array_vertex_buffer(ctx, vao, bindingIndex,
1630 buffer, offset, stride,
1631 "glVertexArrayVertexBuffer");
1632 }
1633
1634
1635 static void
1636 vertex_array_vertex_buffers(struct gl_context *ctx,
1637 struct gl_vertex_array_object *vao,
1638 GLuint first, GLsizei count, const GLuint *buffers,
1639 const GLintptr *offsets, const GLsizei *strides,
1640 const char *func)
1641 {
1642 GLuint i;
1643
1644 ASSERT_OUTSIDE_BEGIN_END(ctx);
1645
1646 /* The ARB_multi_bind spec says:
1647 *
1648 * "An INVALID_OPERATION error is generated if <first> + <count>
1649 * is greater than the value of MAX_VERTEX_ATTRIB_BINDINGS."
1650 */
1651 if (first + count > ctx->Const.MaxVertexAttribBindings) {
1652 _mesa_error(ctx, GL_INVALID_OPERATION,
1653 "%s(first=%u + count=%d > the value of "
1654 "GL_MAX_VERTEX_ATTRIB_BINDINGS=%u)",
1655 func, first, count, ctx->Const.MaxVertexAttribBindings);
1656 return;
1657 }
1658
1659 if (!buffers) {
1660 /**
1661 * The ARB_multi_bind spec says:
1662 *
1663 * "If <buffers> is NULL, each affected vertex buffer binding point
1664 * from <first> through <first>+<count>-1 will be reset to have no
1665 * bound buffer object. In this case, the offsets and strides
1666 * associated with the binding points are set to default values,
1667 * ignoring <offsets> and <strides>."
1668 */
1669 struct gl_buffer_object *vbo = ctx->Shared->NullBufferObj;
1670
1671 for (i = 0; i < count; i++)
1672 bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(first + i),
1673 vbo, 0, 16);
1674
1675 return;
1676 }
1677
1678 /* Note that the error semantics for multi-bind commands differ from
1679 * those of other GL commands.
1680 *
1681 * The Issues section in the ARB_multi_bind spec says:
1682 *
1683 * "(11) Typically, OpenGL specifies that if an error is generated by
1684 * a command, that command has no effect. This is somewhat
1685 * unfortunate for multi-bind commands, because it would require
1686 * a first pass to scan the entire list of bound objects for
1687 * errors and then a second pass to actually perform the
1688 * bindings. Should we have different error semantics?
1689 *
1690 * RESOLVED: Yes. In this specification, when the parameters for
1691 * one of the <count> binding points are invalid, that binding
1692 * point is not updated and an error will be generated. However,
1693 * other binding points in the same command will be updated if
1694 * their parameters are valid and no other error occurs."
1695 */
1696
1697 _mesa_begin_bufferobj_lookups(ctx);
1698
1699 for (i = 0; i < count; i++) {
1700 struct gl_buffer_object *vbo;
1701
1702 /* The ARB_multi_bind spec says:
1703 *
1704 * "An INVALID_VALUE error is generated if any value in
1705 * <offsets> or <strides> is negative (per binding)."
1706 */
1707 if (offsets[i] < 0) {
1708 _mesa_error(ctx, GL_INVALID_VALUE,
1709 "%s(offsets[%u]=%" PRId64 " < 0)",
1710 func, i, (int64_t) offsets[i]);
1711 continue;
1712 }
1713
1714 if (strides[i] < 0) {
1715 _mesa_error(ctx, GL_INVALID_VALUE,
1716 "%s(strides[%u]=%d < 0)",
1717 func, i, strides[i]);
1718 continue;
1719 }
1720
1721 if (ctx->API == API_OPENGL_CORE && ctx->Version >= 44 &&
1722 strides[i] > ctx->Const.MaxVertexAttribStride) {
1723 _mesa_error(ctx, GL_INVALID_VALUE,
1724 "%s(strides[%u]=%d > "
1725 "GL_MAX_VERTEX_ATTRIB_STRIDE)", func, i, strides[i]);
1726 continue;
1727 }
1728
1729 if (buffers[i]) {
1730 struct gl_vertex_buffer_binding *binding =
1731 &vao->VertexBinding[VERT_ATTRIB_GENERIC(first + i)];
1732
1733 if (buffers[i] == binding->BufferObj->Name)
1734 vbo = binding->BufferObj;
1735 else
1736 vbo = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, func);
1737
1738 if (!vbo)
1739 continue;
1740 } else {
1741 vbo = ctx->Shared->NullBufferObj;
1742 }
1743
1744 bind_vertex_buffer(ctx, vao, VERT_ATTRIB_GENERIC(first + i),
1745 vbo, offsets[i], strides[i]);
1746 }
1747
1748 _mesa_end_bufferobj_lookups(ctx);
1749 }
1750
1751
1752 void GLAPIENTRY
1753 _mesa_BindVertexBuffers(GLuint first, GLsizei count, const GLuint *buffers,
1754 const GLintptr *offsets, const GLsizei *strides)
1755 {
1756 GET_CURRENT_CONTEXT(ctx);
1757
1758 /* The ARB_vertex_attrib_binding spec says:
1759 *
1760 * "An INVALID_OPERATION error is generated if no
1761 * vertex array object is bound."
1762 */
1763 if (ctx->API == API_OPENGL_CORE &&
1764 ctx->Array.VAO == ctx->Array.DefaultVAO) {
1765 _mesa_error(ctx, GL_INVALID_OPERATION,
1766 "glBindVertexBuffers(No array object bound)");
1767 return;
1768 }
1769
1770 vertex_array_vertex_buffers(ctx, ctx->Array.VAO, first, count,
1771 buffers, offsets, strides,
1772 "glBindVertexBuffers");
1773 }
1774
1775
1776 void GLAPIENTRY
1777 _mesa_VertexArrayVertexBuffers(GLuint vaobj, GLuint first, GLsizei count,
1778 const GLuint *buffers,
1779 const GLintptr *offsets, const GLsizei *strides)
1780 {
1781 GET_CURRENT_CONTEXT(ctx);
1782 struct gl_vertex_array_object *vao;
1783
1784 /* The ARB_direct_state_access specification says:
1785 *
1786 * "An INVALID_OPERATION error is generated by VertexArrayVertexBuffer
1787 * if <vaobj> is not [compatibility profile: zero or] the name of an
1788 * existing vertex array object."
1789 */
1790 vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayVertexBuffers");
1791 if (!vao)
1792 return;
1793
1794 vertex_array_vertex_buffers(ctx, vao, first, count,
1795 buffers, offsets, strides,
1796 "glVertexArrayVertexBuffers");
1797 }
1798
1799
1800 static void
1801 vertex_attrib_format(GLuint attribIndex, GLint size, GLenum type,
1802 GLboolean normalized, GLboolean integer,
1803 GLboolean doubles, GLbitfield legalTypes,
1804 GLsizei maxSize, GLuint relativeOffset,
1805 const char *func)
1806 {
1807 GET_CURRENT_CONTEXT(ctx);
1808 ASSERT_OUTSIDE_BEGIN_END(ctx);
1809
1810 /* The ARB_vertex_attrib_binding spec says:
1811 *
1812 * "An INVALID_OPERATION error is generated under any of the following
1813 * conditions:
1814 * - if no vertex array object is currently bound (see section 2.10);
1815 * - ..."
1816 *
1817 * This error condition only applies to VertexAttribFormat and
1818 * VertexAttribIFormat in the extension spec, but we assume that this
1819 * is an oversight. In the OpenGL 4.3 (Core Profile) spec, it applies
1820 * to all three functions.
1821 */
1822 if (ctx->API == API_OPENGL_CORE &&
1823 ctx->Array.VAO == ctx->Array.DefaultVAO) {
1824 _mesa_error(ctx, GL_INVALID_OPERATION,
1825 "%s(No array object bound)", func);
1826 return;
1827 }
1828
1829 /* The ARB_vertex_attrib_binding spec says:
1830 *
1831 * "The error INVALID_VALUE is generated if index is greater than or equal
1832 * to the value of MAX_VERTEX_ATTRIBS."
1833 */
1834 if (attribIndex >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1835 _mesa_error(ctx, GL_INVALID_VALUE,
1836 "%s(attribindex=%u > "
1837 "GL_MAX_VERTEX_ATTRIBS)",
1838 func, attribIndex);
1839 return;
1840 }
1841
1842 FLUSH_VERTICES(ctx, 0);
1843
1844 update_array_format(ctx, func, ctx->Array.VAO,
1845 VERT_ATTRIB_GENERIC(attribIndex),
1846 legalTypes, 1, maxSize, size, type,
1847 normalized, integer, doubles, relativeOffset);
1848 }
1849
1850
1851 void GLAPIENTRY
1852 _mesa_VertexAttribFormat(GLuint attribIndex, GLint size, GLenum type,
1853 GLboolean normalized, GLuint relativeOffset)
1854 {
1855 vertex_attrib_format(attribIndex, size, type, normalized,
1856 GL_FALSE, GL_FALSE, ATTRIB_FORMAT_TYPES_MASK,
1857 BGRA_OR_4, relativeOffset,
1858 "glVertexAttribFormat");
1859 }
1860
1861
1862 void GLAPIENTRY
1863 _mesa_VertexAttribIFormat(GLuint attribIndex, GLint size, GLenum type,
1864 GLuint relativeOffset)
1865 {
1866 vertex_attrib_format(attribIndex, size, type, GL_FALSE,
1867 GL_TRUE, GL_FALSE, ATTRIB_IFORMAT_TYPES_MASK, 4,
1868 relativeOffset, "glVertexAttribIFormat");
1869 }
1870
1871
1872 void GLAPIENTRY
1873 _mesa_VertexAttribLFormat(GLuint attribIndex, GLint size, GLenum type,
1874 GLuint relativeOffset)
1875 {
1876 vertex_attrib_format(attribIndex, size, type, GL_FALSE, GL_FALSE,
1877 GL_TRUE, ATTRIB_LFORMAT_TYPES_MASK, 4,
1878 relativeOffset, "glVertexAttribLFormat");
1879 }
1880
1881
1882 static void
1883 vertex_array_attrib_format(GLuint vaobj, GLuint attribIndex, GLint size,
1884 GLenum type, GLboolean normalized,
1885 GLboolean integer, GLboolean doubles,
1886 GLbitfield legalTypes, GLsizei maxSize,
1887 GLuint relativeOffset, const char *func)
1888 {
1889 GET_CURRENT_CONTEXT(ctx);
1890 struct gl_vertex_array_object *vao;
1891
1892 ASSERT_OUTSIDE_BEGIN_END(ctx);
1893
1894 /* The ARB_direct_state_access spec says:
1895 *
1896 * "An INVALID_OPERATION error is generated by VertexArrayAttrib*Format
1897 * if <vaobj> is not [compatibility profile: zero or] the name of an
1898 * existing vertex array object."
1899 */
1900 vao = _mesa_lookup_vao_err(ctx, vaobj, func);
1901 if (!vao)
1902 return;
1903
1904 /* The ARB_vertex_attrib_binding spec says:
1905 *
1906 * "The error INVALID_VALUE is generated if index is greater than or equal
1907 * to the value of MAX_VERTEX_ATTRIBS."
1908 */
1909 if (attribIndex >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1910 _mesa_error(ctx, GL_INVALID_VALUE,
1911 "%s(attribindex=%u > GL_MAX_VERTEX_ATTRIBS)",
1912 func, attribIndex);
1913 return;
1914 }
1915
1916 FLUSH_VERTICES(ctx, 0);
1917
1918 update_array_format(ctx, func, vao,
1919 VERT_ATTRIB_GENERIC(attribIndex),
1920 legalTypes, 1, maxSize, size, type, normalized,
1921 integer, doubles, relativeOffset);
1922 }
1923
1924
1925 void GLAPIENTRY
1926 _mesa_VertexArrayAttribFormat(GLuint vaobj, GLuint attribIndex, GLint size,
1927 GLenum type, GLboolean normalized,
1928 GLuint relativeOffset)
1929 {
1930 vertex_array_attrib_format(vaobj, attribIndex, size, type, normalized,
1931 GL_FALSE, GL_FALSE, ATTRIB_FORMAT_TYPES_MASK,
1932 BGRA_OR_4, relativeOffset,
1933 "glVertexArrayAttribFormat");
1934 }
1935
1936
1937 void GLAPIENTRY
1938 _mesa_VertexArrayAttribIFormat(GLuint vaobj, GLuint attribIndex,
1939 GLint size, GLenum type,
1940 GLuint relativeOffset)
1941 {
1942 vertex_array_attrib_format(vaobj, attribIndex, size, type, GL_FALSE,
1943 GL_TRUE, GL_FALSE, ATTRIB_IFORMAT_TYPES_MASK,
1944 4, relativeOffset,
1945 "glVertexArrayAttribIFormat");
1946 }
1947
1948
1949 void GLAPIENTRY
1950 _mesa_VertexArrayAttribLFormat(GLuint vaobj, GLuint attribIndex,
1951 GLint size, GLenum type,
1952 GLuint relativeOffset)
1953 {
1954 vertex_array_attrib_format(vaobj, attribIndex, size, type, GL_FALSE,
1955 GL_FALSE, GL_TRUE, ATTRIB_LFORMAT_TYPES_MASK,
1956 4, relativeOffset,
1957 "glVertexArrayAttribLFormat");
1958 }
1959
1960
1961 static void
1962 vertex_array_attrib_binding(struct gl_context *ctx,
1963 struct gl_vertex_array_object *vao,
1964 GLuint attribIndex, GLuint bindingIndex,
1965 const char *func)
1966 {
1967 ASSERT_OUTSIDE_BEGIN_END(ctx);
1968
1969 /* The ARB_vertex_attrib_binding spec says:
1970 *
1971 * "<attribindex> must be less than the value of MAX_VERTEX_ATTRIBS and
1972 * <bindingindex> must be less than the value of
1973 * MAX_VERTEX_ATTRIB_BINDINGS, otherwise the error INVALID_VALUE
1974 * is generated."
1975 */
1976 if (attribIndex >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
1977 _mesa_error(ctx, GL_INVALID_VALUE,
1978 "%s(attribindex=%u >= "
1979 "GL_MAX_VERTEX_ATTRIBS)",
1980 func, attribIndex);
1981 return;
1982 }
1983
1984 if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
1985 _mesa_error(ctx, GL_INVALID_VALUE,
1986 "%s(bindingindex=%u >= "
1987 "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
1988 func, bindingIndex);
1989 return;
1990 }
1991
1992 assert(VERT_ATTRIB_GENERIC(attribIndex) < ARRAY_SIZE(vao->VertexAttrib));
1993
1994 vertex_attrib_binding(ctx, vao,
1995 VERT_ATTRIB_GENERIC(attribIndex),
1996 VERT_ATTRIB_GENERIC(bindingIndex));
1997 }
1998
1999
2000 void GLAPIENTRY
2001 _mesa_VertexAttribBinding(GLuint attribIndex, GLuint bindingIndex)
2002 {
2003 GET_CURRENT_CONTEXT(ctx);
2004
2005 /* The ARB_vertex_attrib_binding spec says:
2006 *
2007 * "An INVALID_OPERATION error is generated if no vertex array object
2008 * is bound."
2009 */
2010 if (ctx->API == API_OPENGL_CORE &&
2011 ctx->Array.VAO == ctx->Array.DefaultVAO) {
2012 _mesa_error(ctx, GL_INVALID_OPERATION,
2013 "glVertexAttribBinding(No array object bound)");
2014 return;
2015 }
2016
2017 vertex_array_attrib_binding(ctx, ctx->Array.VAO,
2018 attribIndex, bindingIndex,
2019 "glVertexAttribBinding");
2020 }
2021
2022
2023 void GLAPIENTRY
2024 _mesa_VertexArrayAttribBinding(GLuint vaobj, GLuint attribIndex, GLuint bindingIndex)
2025 {
2026 GET_CURRENT_CONTEXT(ctx);
2027 struct gl_vertex_array_object *vao;
2028
2029 /* The ARB_direct_state_access specification says:
2030 *
2031 * "An INVALID_OPERATION error is generated by VertexArrayAttribBinding
2032 * if <vaobj> is not [compatibility profile: zero or] the name of an
2033 * existing vertex array object."
2034 */
2035 vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayAttribBinding");
2036 if (!vao)
2037 return;
2038
2039 vertex_array_attrib_binding(ctx, vao, attribIndex, bindingIndex,
2040 "glVertexArrayAttribBinding");
2041 }
2042
2043
2044 static void
2045 vertex_array_binding_divisor(struct gl_context *ctx,
2046 struct gl_vertex_array_object *vao,
2047 GLuint bindingIndex, GLuint divisor,
2048 const char *func)
2049 {
2050 ASSERT_OUTSIDE_BEGIN_END(ctx);
2051
2052 if (!ctx->Extensions.ARB_instanced_arrays) {
2053 _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", func);
2054 return;
2055 }
2056
2057 /* The ARB_vertex_attrib_binding spec says:
2058 *
2059 * "An INVALID_VALUE error is generated if <bindingindex> is greater
2060 * than or equal to the value of MAX_VERTEX_ATTRIB_BINDINGS."
2061 */
2062 if (bindingIndex >= ctx->Const.MaxVertexAttribBindings) {
2063 _mesa_error(ctx, GL_INVALID_VALUE,
2064 "%s(bindingindex=%u > "
2065 "GL_MAX_VERTEX_ATTRIB_BINDINGS)",
2066 func, bindingIndex);
2067 return;
2068 }
2069
2070 vertex_binding_divisor(ctx, vao, VERT_ATTRIB_GENERIC(bindingIndex), divisor);
2071 }
2072
2073
2074 void GLAPIENTRY
2075 _mesa_VertexBindingDivisor(GLuint bindingIndex, GLuint divisor)
2076 {
2077 GET_CURRENT_CONTEXT(ctx);
2078
2079 /* The ARB_vertex_attrib_binding spec says:
2080 *
2081 * "An INVALID_OPERATION error is generated if no vertex array object
2082 * is bound."
2083 */
2084 if (ctx->API == API_OPENGL_CORE &&
2085 ctx->Array.VAO == ctx->Array.DefaultVAO) {
2086 _mesa_error(ctx, GL_INVALID_OPERATION,
2087 "glVertexBindingDivisor(No array object bound)");
2088 return;
2089 }
2090
2091 vertex_array_binding_divisor(ctx, ctx->Array.VAO,
2092 bindingIndex, divisor,
2093 "glVertexBindingDivisor");
2094 }
2095
2096
2097 void GLAPIENTRY
2098 _mesa_VertexArrayBindingDivisor(GLuint vaobj, GLuint bindingIndex, GLuint divisor)
2099 {
2100 struct gl_vertex_array_object *vao;
2101 GET_CURRENT_CONTEXT(ctx);
2102
2103 /* The ARB_direct_state_access specification says:
2104 *
2105 * "An INVALID_OPERATION error is generated by VertexArrayBindingDivisor
2106 * if <vaobj> is not [compatibility profile: zero or] the name of an
2107 * existing vertex array object."
2108 */
2109 vao = _mesa_lookup_vao_err(ctx, vaobj, "glVertexArrayBindingDivisor");
2110 if (!vao)
2111 return;
2112
2113 vertex_array_binding_divisor(ctx, vao, bindingIndex, divisor,
2114 "glVertexArrayBindingDivisor");
2115 }
2116
2117
2118 /**
2119 * Copy one client vertex array to another.
2120 */
2121 void
2122 _mesa_copy_client_array(struct gl_context *ctx,
2123 struct gl_client_array *dst,
2124 struct gl_client_array *src)
2125 {
2126 dst->Size = src->Size;
2127 dst->Type = src->Type;
2128 dst->Format = src->Format;
2129 dst->Stride = src->Stride;
2130 dst->StrideB = src->StrideB;
2131 dst->Ptr = src->Ptr;
2132 dst->Enabled = src->Enabled;
2133 dst->Normalized = src->Normalized;
2134 dst->Integer = src->Integer;
2135 dst->Doubles = src->Doubles;
2136 dst->InstanceDivisor = src->InstanceDivisor;
2137 dst->_ElementSize = src->_ElementSize;
2138 _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
2139 }
2140
2141 void
2142 _mesa_copy_vertex_attrib_array(struct gl_context *ctx,
2143 struct gl_vertex_attrib_array *dst,
2144 const struct gl_vertex_attrib_array *src)
2145 {
2146 dst->Size = src->Size;
2147 dst->Type = src->Type;
2148 dst->Format = src->Format;
2149 dst->VertexBinding = src->VertexBinding;
2150 dst->RelativeOffset = src->RelativeOffset;
2151 dst->Format = src->Format;
2152 dst->Integer = src->Integer;
2153 dst->Doubles = src->Doubles;
2154 dst->Normalized = src->Normalized;
2155 dst->Ptr = src->Ptr;
2156 dst->Enabled = src->Enabled;
2157 dst->_ElementSize = src->_ElementSize;
2158 }
2159
2160 void
2161 _mesa_copy_vertex_buffer_binding(struct gl_context *ctx,
2162 struct gl_vertex_buffer_binding *dst,
2163 const struct gl_vertex_buffer_binding *src)
2164 {
2165 dst->Offset = src->Offset;
2166 dst->Stride = src->Stride;
2167 dst->InstanceDivisor = src->InstanceDivisor;
2168 dst->_BoundArrays = src->_BoundArrays;
2169
2170 _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
2171 }
2172
2173 /**
2174 * Print vertex array's fields.
2175 */
2176 static void
2177 print_array(const char *name, GLint index, const struct gl_client_array *array)
2178 {
2179 if (index >= 0)
2180 fprintf(stderr, " %s[%d]: ", name, index);
2181 else
2182 fprintf(stderr, " %s: ", name);
2183 fprintf(stderr, "Ptr=%p, Type=0x%x, Size=%d, ElemSize=%u, Stride=%d, Buffer=%u(Size %lu)\n",
2184 array->Ptr, array->Type, array->Size,
2185 array->_ElementSize, array->StrideB,
2186 array->BufferObj->Name, (unsigned long) array->BufferObj->Size);
2187 }
2188
2189
2190 /**
2191 * Print current vertex object/array info. For debug.
2192 */
2193 void
2194 _mesa_print_arrays(struct gl_context *ctx)
2195 {
2196 struct gl_vertex_array_object *vao = ctx->Array.VAO;
2197 GLuint i;
2198
2199 printf("Array Object %u\n", vao->Name);
2200 if (vao->_VertexAttrib[VERT_ATTRIB_POS].Enabled)
2201 print_array("Vertex", -1, &vao->_VertexAttrib[VERT_ATTRIB_POS]);
2202 if (vao->_VertexAttrib[VERT_ATTRIB_NORMAL].Enabled)
2203 print_array("Normal", -1, &vao->_VertexAttrib[VERT_ATTRIB_NORMAL]);
2204 if (vao->_VertexAttrib[VERT_ATTRIB_COLOR0].Enabled)
2205 print_array("Color", -1, &vao->_VertexAttrib[VERT_ATTRIB_COLOR0]);
2206 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++)
2207 if (vao->_VertexAttrib[VERT_ATTRIB_TEX(i)].Enabled)
2208 print_array("TexCoord", i, &vao->_VertexAttrib[VERT_ATTRIB_TEX(i)]);
2209 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++)
2210 if (vao->_VertexAttrib[VERT_ATTRIB_GENERIC(i)].Enabled)
2211 print_array("Attrib", i, &vao->_VertexAttrib[VERT_ATTRIB_GENERIC(i)]);
2212 }
2213
2214
2215 /**
2216 * Initialize vertex array state for given context.
2217 */
2218 void
2219 _mesa_init_varray(struct gl_context *ctx)
2220 {
2221 ctx->Array.DefaultVAO = ctx->Driver.NewArrayObject(ctx, 0);
2222 _mesa_reference_vao(ctx, &ctx->Array.VAO, ctx->Array.DefaultVAO);
2223 ctx->Array.ActiveTexture = 0; /* GL_ARB_multitexture */
2224
2225 ctx->Array.Objects = _mesa_NewHashTable();
2226 }
2227
2228
2229 /**
2230 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
2231 */
2232 static void
2233 delete_arrayobj_cb(GLuint id, void *data, void *userData)
2234 {
2235 struct gl_vertex_array_object *vao = (struct gl_vertex_array_object *) data;
2236 struct gl_context *ctx = (struct gl_context *) userData;
2237 _mesa_delete_vao(ctx, vao);
2238 }
2239
2240
2241 /**
2242 * Free vertex array state for given context.
2243 */
2244 void
2245 _mesa_free_varray_data(struct gl_context *ctx)
2246 {
2247 _mesa_HashDeleteAll(ctx->Array.Objects, delete_arrayobj_cb, ctx);
2248 _mesa_DeleteHashTable(ctx->Array.Objects);
2249 }