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