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