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