2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
29 #include "bufferobj.h"
39 #include "main/dispatch.h"
42 /** Used to do error checking for GL_EXT_vertex_array_bgra */
46 /** Used to indicate which GL datatypes are accepted by each of the
47 * glVertex/Color/Attrib/EtcPointer() functions.
51 #define UNSIGNED_BYTE_BIT 0x4
53 #define UNSIGNED_SHORT_BIT 0x10
55 #define UNSIGNED_INT_BIT 0x40
57 #define FLOAT_BIT 0x100
58 #define DOUBLE_BIT 0x200
59 #define FIXED_ES_BIT 0x400
60 #define FIXED_GL_BIT 0x800
61 #define UNSIGNED_INT_2_10_10_10_REV_BIT 0x1000
62 #define INT_2_10_10_10_REV_BIT 0x2000
65 /** Convert GL datatype enum into a <type>_BIT value seen above */
67 type_to_bit(const struct gl_context
*ctx
, GLenum type
)
74 case GL_UNSIGNED_BYTE
:
75 return UNSIGNED_BYTE_BIT
;
78 case GL_UNSIGNED_SHORT
:
79 return UNSIGNED_SHORT_BIT
;
83 return UNSIGNED_INT_BIT
;
85 if (ctx
->Extensions
.ARB_half_float_vertex
)
94 return _mesa_is_desktop_gl(ctx
) ? FIXED_GL_BIT
: FIXED_ES_BIT
;
95 case GL_UNSIGNED_INT_2_10_10_10_REV
:
96 return UNSIGNED_INT_2_10_10_10_REV_BIT
;
97 case GL_INT_2_10_10_10_REV
:
98 return INT_2_10_10_10_REV_BIT
;
106 * Do error checking and update state for glVertex/Color/TexCoord/...Pointer
109 * \param func name of calling function used for error reporting
110 * \param attrib the attribute array index to update
111 * \param legalTypes bitmask of *_BIT above indicating legal datatypes
112 * \param sizeMin min allowable size value
113 * \param sizeMax max allowable size value (may also be BGRA_OR_4)
114 * \param size components per element (1, 2, 3 or 4)
115 * \param type datatype of each component (GL_FLOAT, GL_INT, etc)
116 * \param stride stride between elements, in elements
117 * \param normalized are integer types converted to floats in [-1, 1]?
118 * \param integer integer-valued values (will not be normalized to [-1,1])
119 * \param ptr the address (or offset inside VBO) of the array data
122 update_array(struct gl_context
*ctx
,
124 GLuint attrib
, GLbitfield legalTypesMask
,
125 GLint sizeMin
, GLint sizeMax
,
126 GLint size
, GLenum type
, GLsizei stride
,
127 GLboolean normalized
, GLboolean integer
,
130 struct gl_client_array
*array
;
133 GLenum format
= GL_RGBA
;
135 /* Page 407 (page 423 of the PDF) of the OpenGL 3.0 spec says:
137 * "Client vertex arrays - all vertex array attribute pointers must
138 * refer to buffer objects (section 2.9.2). The default vertex array
139 * object (the name zero) is also deprecated. Calling
140 * VertexAttribPointer when no buffer object or no vertex array object
141 * is bound will generate an INVALID_OPERATION error..."
143 * The check for VBOs is handled below.
145 if (ctx
->API
== API_OPENGL_CORE
146 && (ctx
->Array
.ArrayObj
== ctx
->Array
.DefaultArrayObj
)) {
147 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no array object bound)",
152 if (_mesa_is_gles(ctx
)) {
153 legalTypesMask
&= ~(FIXED_GL_BIT
| DOUBLE_BIT
);
155 /* GL_INT and GL_UNSIGNED_INT data is not allowed in OpenGL ES until
156 * 3.0. The 2_10_10_10 types are added in OpenGL ES 3.0 or
157 * GL_OES_vertex_type_10_10_10_2. GL_HALF_FLOAT data is not allowed
158 * until 3.0 or with the GL_OES_vertex_half float extension, which isn't
159 * quite as trivial as we'd like because it uses a different enum value
160 * for GL_HALF_FLOAT_OES.
162 if (ctx
->Version
< 30) {
163 legalTypesMask
&= ~(UNSIGNED_INT_BIT
165 | UNSIGNED_INT_2_10_10_10_REV_BIT
166 | INT_2_10_10_10_REV_BIT
170 /* BGRA ordering is not supported in ES contexts.
172 if (sizeMax
== BGRA_OR_4
)
175 legalTypesMask
&= ~FIXED_ES_BIT
;
177 if (!ctx
->Extensions
.ARB_ES2_compatibility
)
178 legalTypesMask
&= ~FIXED_GL_BIT
;
180 if (!ctx
->Extensions
.ARB_vertex_type_2_10_10_10_rev
)
181 legalTypesMask
&= ~(UNSIGNED_INT_2_10_10_10_REV_BIT
|
182 INT_2_10_10_10_REV_BIT
);
185 typeBit
= type_to_bit(ctx
, type
);
186 if (typeBit
== 0x0 || (typeBit
& legalTypesMask
) == 0x0) {
187 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)",
188 func
, _mesa_lookup_enum_by_nr(type
));
192 /* Do size parameter checking.
193 * If sizeMax = BGRA_OR_4 it means that size = GL_BGRA is legal and
194 * must be handled specially.
196 if (ctx
->Extensions
.EXT_vertex_array_bgra
&&
197 sizeMax
== BGRA_OR_4
&&
199 /* Page 298 of the PDF of the OpenGL 4.3 (Core Profile) spec says:
201 * "An INVALID_OPERATION error is generated under any of the following
204 * • size is BGRA and type is not UNSIGNED_BYTE, INT_2_10_10_10_REV
205 * or UNSIGNED_INT_2_10_10_10_REV;
207 * • size is BGRA and normalized is FALSE;"
209 GLboolean bgra_error
= GL_FALSE
;
211 if (ctx
->Extensions
.ARB_vertex_type_2_10_10_10_rev
) {
212 if (type
!= GL_UNSIGNED_INT_2_10_10_10_REV
&&
213 type
!= GL_INT_2_10_10_10_REV
&&
214 type
!= GL_UNSIGNED_BYTE
)
215 bgra_error
= GL_TRUE
;
216 } else if (type
!= GL_UNSIGNED_BYTE
)
217 bgra_error
= GL_TRUE
;
220 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(size=GL_BGRA and type=%s)",
221 func
, _mesa_lookup_enum_by_nr(type
));
226 _mesa_error(ctx
, GL_INVALID_OPERATION
,
227 "%s(size=GL_BGRA and normalized=GL_FALSE)", func
);
234 else if (size
< sizeMin
|| size
> sizeMax
|| size
> 4) {
235 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d)", func
, size
);
239 if (ctx
->Extensions
.ARB_vertex_type_2_10_10_10_rev
&&
240 (type
== GL_UNSIGNED_INT_2_10_10_10_REV
||
241 type
== GL_INT_2_10_10_10_REV
) && size
!= 4) {
242 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(size=%d)", func
, size
);
249 _mesa_error( ctx
, GL_INVALID_VALUE
, "%s(stride=%d)", func
, stride
);
253 /* Page 29 (page 44 of the PDF) of the OpenGL 3.3 spec says:
255 * "An INVALID_OPERATION error is generated under any of the following
260 * * any of the *Pointer commands specifying the location and
261 * organization of vertex array data are called while zero is bound
262 * to the ARRAY_BUFFER buffer object binding point (see section
263 * 2.9.6), and the pointer argument is not NULL."
265 if (ptr
!= NULL
&& ctx
->Array
.ArrayObj
->ARBsemantics
&&
266 !_mesa_is_bufferobj(ctx
->Array
.ArrayBufferObj
)) {
267 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-VBO array)", func
);
271 elementSize
= _mesa_bytes_per_vertex_attrib(size
, type
);
272 assert(elementSize
!= -1);
274 array
= &ctx
->Array
.ArrayObj
->VertexAttrib
[attrib
];
277 array
->Format
= format
;
278 array
->Stride
= stride
;
279 array
->StrideB
= stride
? stride
: elementSize
;
280 array
->Normalized
= normalized
;
281 array
->Integer
= integer
;
282 array
->Ptr
= (const GLubyte
*) ptr
;
283 array
->_ElementSize
= elementSize
;
285 _mesa_reference_buffer_object(ctx
, &array
->BufferObj
,
286 ctx
->Array
.ArrayBufferObj
);
288 ctx
->NewState
|= _NEW_ARRAY
;
293 _mesa_VertexPointer(GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
295 GET_CURRENT_CONTEXT(ctx
);
296 GLbitfield legalTypes
= (ctx
->API
== API_OPENGLES
)
297 ? (BYTE_BIT
| SHORT_BIT
| FLOAT_BIT
| FIXED_ES_BIT
)
298 : (SHORT_BIT
| INT_BIT
| FLOAT_BIT
|
299 DOUBLE_BIT
| HALF_BIT
|
300 UNSIGNED_INT_2_10_10_10_REV_BIT
|
301 INT_2_10_10_10_REV_BIT
);
303 FLUSH_VERTICES(ctx
, 0);
305 update_array(ctx
, "glVertexPointer", VERT_ATTRIB_POS
,
307 size
, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
312 _mesa_NormalPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
314 GET_CURRENT_CONTEXT(ctx
);
315 const GLbitfield legalTypes
= (ctx
->API
== API_OPENGLES
)
316 ? (BYTE_BIT
| SHORT_BIT
| FLOAT_BIT
| FIXED_ES_BIT
)
317 : (BYTE_BIT
| SHORT_BIT
| INT_BIT
|
318 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
|
319 UNSIGNED_INT_2_10_10_10_REV_BIT
|
320 INT_2_10_10_10_REV_BIT
);
322 FLUSH_VERTICES(ctx
, 0);
324 update_array(ctx
, "glNormalPointer", VERT_ATTRIB_NORMAL
,
326 3, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
331 _mesa_ColorPointer(GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
333 GET_CURRENT_CONTEXT(ctx
);
334 const GLbitfield legalTypes
= (ctx
->API
== API_OPENGLES
)
335 ? (UNSIGNED_BYTE_BIT
| HALF_BIT
| FLOAT_BIT
| FIXED_ES_BIT
)
336 : (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
337 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
338 INT_BIT
| UNSIGNED_INT_BIT
|
339 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
|
340 UNSIGNED_INT_2_10_10_10_REV_BIT
|
341 INT_2_10_10_10_REV_BIT
);
342 const GLint sizeMin
= (ctx
->API
== API_OPENGLES
) ? 4 : 3;
344 FLUSH_VERTICES(ctx
, 0);
346 update_array(ctx
, "glColorPointer", VERT_ATTRIB_COLOR0
,
347 legalTypes
, sizeMin
, BGRA_OR_4
,
348 size
, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
353 _mesa_FogCoordPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
355 const GLbitfield legalTypes
= (HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
356 GET_CURRENT_CONTEXT(ctx
);
358 FLUSH_VERTICES(ctx
, 0);
360 update_array(ctx
, "glFogCoordPointer", VERT_ATTRIB_FOG
,
362 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
367 _mesa_IndexPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
369 const GLbitfield legalTypes
= (UNSIGNED_BYTE_BIT
| SHORT_BIT
| INT_BIT
|
370 FLOAT_BIT
| DOUBLE_BIT
);
371 GET_CURRENT_CONTEXT(ctx
);
373 FLUSH_VERTICES(ctx
, 0);
375 update_array(ctx
, "glIndexPointer", VERT_ATTRIB_COLOR_INDEX
,
377 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
382 _mesa_SecondaryColorPointer(GLint size
, GLenum type
,
383 GLsizei stride
, const GLvoid
*ptr
)
385 const GLbitfield legalTypes
= (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
386 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
387 INT_BIT
| UNSIGNED_INT_BIT
|
388 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
|
389 UNSIGNED_INT_2_10_10_10_REV_BIT
|
390 INT_2_10_10_10_REV_BIT
);
391 GET_CURRENT_CONTEXT(ctx
);
393 FLUSH_VERTICES(ctx
, 0);
395 update_array(ctx
, "glSecondaryColorPointer", VERT_ATTRIB_COLOR1
,
396 legalTypes
, 3, BGRA_OR_4
,
397 size
, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
402 _mesa_TexCoordPointer(GLint size
, GLenum type
, GLsizei stride
,
405 GET_CURRENT_CONTEXT(ctx
);
406 GLbitfield legalTypes
= (ctx
->API
== API_OPENGLES
)
407 ? (BYTE_BIT
| SHORT_BIT
| FLOAT_BIT
| FIXED_ES_BIT
)
408 : (SHORT_BIT
| INT_BIT
|
409 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
|
410 UNSIGNED_INT_2_10_10_10_REV_BIT
|
411 INT_2_10_10_10_REV_BIT
);
412 const GLint sizeMin
= (ctx
->API
== API_OPENGLES
) ? 2 : 1;
413 const GLuint unit
= ctx
->Array
.ActiveTexture
;
415 FLUSH_VERTICES(ctx
, 0);
417 update_array(ctx
, "glTexCoordPointer", VERT_ATTRIB_TEX(unit
),
418 legalTypes
, sizeMin
, 4,
419 size
, type
, stride
, GL_FALSE
, GL_FALSE
,
425 _mesa_EdgeFlagPointer(GLsizei stride
, const GLvoid
*ptr
)
427 const GLbitfield legalTypes
= UNSIGNED_BYTE_BIT
;
428 /* see table 2.4 edits in GL_EXT_gpu_shader4 spec: */
429 const GLboolean integer
= GL_TRUE
;
430 GET_CURRENT_CONTEXT(ctx
);
432 FLUSH_VERTICES(ctx
, 0);
434 update_array(ctx
, "glEdgeFlagPointer", VERT_ATTRIB_EDGEFLAG
,
436 1, GL_UNSIGNED_BYTE
, stride
, GL_FALSE
, integer
, ptr
);
441 _mesa_PointSizePointerOES(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
443 const GLbitfield legalTypes
= (FLOAT_BIT
| FIXED_ES_BIT
);
444 GET_CURRENT_CONTEXT(ctx
);
446 FLUSH_VERTICES(ctx
, 0);
448 if (ctx
->API
!= API_OPENGLES
) {
449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
450 "glPointSizePointer(ES 1.x only)");
454 update_array(ctx
, "glPointSizePointer", VERT_ATTRIB_POINT_SIZE
,
456 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
461 * Set a generic vertex attribute array.
462 * Note that these arrays DO NOT alias the conventional GL vertex arrays
463 * (position, normal, color, fog, texcoord, etc).
466 _mesa_VertexAttribPointer(GLuint index
, GLint size
, GLenum type
,
467 GLboolean normalized
,
468 GLsizei stride
, const GLvoid
*ptr
)
470 const GLbitfield legalTypes
= (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
471 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
472 INT_BIT
| UNSIGNED_INT_BIT
|
473 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
|
474 FIXED_ES_BIT
| FIXED_GL_BIT
|
475 UNSIGNED_INT_2_10_10_10_REV_BIT
|
476 INT_2_10_10_10_REV_BIT
);
477 GET_CURRENT_CONTEXT(ctx
);
479 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
480 _mesa_error(ctx
, GL_INVALID_VALUE
, "glVertexAttribPointerARB(index)");
484 update_array(ctx
, "glVertexAttribPointer", VERT_ATTRIB_GENERIC(index
),
485 legalTypes
, 1, BGRA_OR_4
,
486 size
, type
, stride
, normalized
, GL_FALSE
, ptr
);
491 * GL_EXT_gpu_shader4 / GL 3.0.
492 * Set an integer-valued vertex attribute array.
493 * Note that these arrays DO NOT alias the conventional GL vertex arrays
494 * (position, normal, color, fog, texcoord, etc).
497 _mesa_VertexAttribIPointer(GLuint index
, GLint size
, GLenum type
,
498 GLsizei stride
, const GLvoid
*ptr
)
500 const GLbitfield legalTypes
= (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
501 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
502 INT_BIT
| UNSIGNED_INT_BIT
);
503 const GLboolean normalized
= GL_FALSE
;
504 const GLboolean integer
= GL_TRUE
;
505 GET_CURRENT_CONTEXT(ctx
);
507 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
508 _mesa_error(ctx
, GL_INVALID_VALUE
, "glVertexAttribIPointer(index)");
512 update_array(ctx
, "glVertexAttribIPointer", VERT_ATTRIB_GENERIC(index
),
514 size
, type
, stride
, normalized
, integer
, ptr
);
520 _mesa_EnableVertexAttribArray(GLuint index
)
522 struct gl_array_object
*arrayObj
;
523 GET_CURRENT_CONTEXT(ctx
);
525 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
526 _mesa_error(ctx
, GL_INVALID_VALUE
,
527 "glEnableVertexAttribArrayARB(index)");
531 arrayObj
= ctx
->Array
.ArrayObj
;
533 ASSERT(VERT_ATTRIB_GENERIC(index
) < Elements(arrayObj
->VertexAttrib
));
535 if (!arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(index
)].Enabled
) {
536 /* was disabled, now being enabled */
537 FLUSH_VERTICES(ctx
, _NEW_ARRAY
);
538 arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(index
)].Enabled
= GL_TRUE
;
539 arrayObj
->_Enabled
|= VERT_BIT_GENERIC(index
);
545 _mesa_DisableVertexAttribArray(GLuint index
)
547 struct gl_array_object
*arrayObj
;
548 GET_CURRENT_CONTEXT(ctx
);
550 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
551 _mesa_error(ctx
, GL_INVALID_VALUE
,
552 "glDisableVertexAttribArrayARB(index)");
556 arrayObj
= ctx
->Array
.ArrayObj
;
558 ASSERT(VERT_ATTRIB_GENERIC(index
) < Elements(arrayObj
->VertexAttrib
));
560 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(index
)].Enabled
) {
561 /* was enabled, now being disabled */
562 FLUSH_VERTICES(ctx
, _NEW_ARRAY
);
563 arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(index
)].Enabled
= GL_FALSE
;
564 arrayObj
->_Enabled
&= ~VERT_BIT_GENERIC(index
);
570 * Return info for a vertex attribute array (no alias with legacy
571 * vertex attributes (pos, normal, color, etc)). This function does
572 * not handle the 4-element GL_CURRENT_VERTEX_ATTRIB_ARB query.
575 get_vertex_array_attrib(struct gl_context
*ctx
, GLuint index
, GLenum pname
,
578 const struct gl_client_array
*array
;
580 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
581 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%u)", caller
, index
);
585 ASSERT(VERT_ATTRIB_GENERIC(index
) < Elements(ctx
->Array
.ArrayObj
->VertexAttrib
));
587 array
= &ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(index
)];
590 case GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB
:
591 return array
->Enabled
;
592 case GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB
:
594 case GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB
:
595 return array
->Stride
;
596 case GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB
:
598 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB
:
599 return array
->Normalized
;
600 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB
:
601 return array
->BufferObj
->Name
;
602 case GL_VERTEX_ATTRIB_ARRAY_INTEGER
:
603 if ((_mesa_is_desktop_gl(ctx
)
604 && (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
))
605 || _mesa_is_gles3(ctx
)) {
606 return array
->Integer
;
609 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ARB
:
610 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_instanced_arrays
)
611 || _mesa_is_gles3(ctx
)) {
612 return array
->InstanceDivisor
;
620 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", caller
, pname
);
625 static const GLfloat
*
626 get_current_attrib(struct gl_context
*ctx
, GLuint index
, const char *function
)
629 /* In OpenGL 3.1 attribute 0 becomes non-magic, just like in OpenGL ES
630 * 2.0. Note that we cannot just check for API_OPENGL_CORE here because
631 * that will erroneously allow this usage in a 3.0 forward-compatible
634 if ((ctx
->API
!= API_OPENGL_CORE
|| ctx
->Version
< 31)
635 && ctx
->API
!= API_OPENGLES2
) {
636 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(index==0)", function
);
640 else if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
641 _mesa_error(ctx
, GL_INVALID_VALUE
,
642 "%s(index>=GL_MAX_VERTEX_ATTRIBS)", function
);
646 ASSERT(VERT_ATTRIB_GENERIC(index
) < Elements(ctx
->Array
.ArrayObj
->VertexAttrib
));
648 FLUSH_CURRENT(ctx
, 0);
649 return ctx
->Current
.Attrib
[VERT_ATTRIB_GENERIC(index
)];
653 _mesa_GetVertexAttribfv(GLuint index
, GLenum pname
, GLfloat
*params
)
655 GET_CURRENT_CONTEXT(ctx
);
657 if (pname
== GL_CURRENT_VERTEX_ATTRIB_ARB
) {
658 const GLfloat
*v
= get_current_attrib(ctx
, index
, "glGetVertexAttribfv");
664 params
[0] = (GLfloat
) get_vertex_array_attrib(ctx
, index
, pname
,
665 "glGetVertexAttribfv");
671 _mesa_GetVertexAttribdv(GLuint index
, GLenum pname
, GLdouble
*params
)
673 GET_CURRENT_CONTEXT(ctx
);
675 if (pname
== GL_CURRENT_VERTEX_ATTRIB_ARB
) {
676 const GLfloat
*v
= get_current_attrib(ctx
, index
, "glGetVertexAttribdv");
678 params
[0] = (GLdouble
) v
[0];
679 params
[1] = (GLdouble
) v
[1];
680 params
[2] = (GLdouble
) v
[2];
681 params
[3] = (GLdouble
) v
[3];
685 params
[0] = (GLdouble
) get_vertex_array_attrib(ctx
, index
, pname
,
686 "glGetVertexAttribdv");
692 _mesa_GetVertexAttribiv(GLuint index
, GLenum pname
, GLint
*params
)
694 GET_CURRENT_CONTEXT(ctx
);
696 if (pname
== GL_CURRENT_VERTEX_ATTRIB_ARB
) {
697 const GLfloat
*v
= get_current_attrib(ctx
, index
, "glGetVertexAttribiv");
699 /* XXX should floats in[0,1] be scaled to full int range? */
700 params
[0] = (GLint
) v
[0];
701 params
[1] = (GLint
) v
[1];
702 params
[2] = (GLint
) v
[2];
703 params
[3] = (GLint
) v
[3];
707 params
[0] = (GLint
) get_vertex_array_attrib(ctx
, index
, pname
,
708 "glGetVertexAttribiv");
715 _mesa_GetVertexAttribIiv(GLuint index
, GLenum pname
, GLint
*params
)
717 GET_CURRENT_CONTEXT(ctx
);
719 if (pname
== GL_CURRENT_VERTEX_ATTRIB_ARB
) {
720 const GLint
*v
= (const GLint
*)
721 get_current_attrib(ctx
, index
, "glGetVertexAttribIiv");
727 params
[0] = (GLint
) get_vertex_array_attrib(ctx
, index
, pname
,
728 "glGetVertexAttribIiv");
735 _mesa_GetVertexAttribIuiv(GLuint index
, GLenum pname
, GLuint
*params
)
737 GET_CURRENT_CONTEXT(ctx
);
739 if (pname
== GL_CURRENT_VERTEX_ATTRIB_ARB
) {
740 const GLuint
*v
= (const GLuint
*)
741 get_current_attrib(ctx
, index
, "glGetVertexAttribIuiv");
747 params
[0] = get_vertex_array_attrib(ctx
, index
, pname
,
748 "glGetVertexAttribIuiv");
754 _mesa_GetVertexAttribPointerv(GLuint index
, GLenum pname
, GLvoid
**pointer
)
756 GET_CURRENT_CONTEXT(ctx
);
758 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
759 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetVertexAttribPointerARB(index)");
763 if (pname
!= GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB
) {
764 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetVertexAttribPointerARB(pname)");
768 ASSERT(VERT_ATTRIB_GENERIC(index
) < Elements(ctx
->Array
.ArrayObj
->VertexAttrib
));
770 *pointer
= (GLvoid
*) ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(index
)].Ptr
;
775 _mesa_VertexPointerEXT(GLint size
, GLenum type
, GLsizei stride
,
776 GLsizei count
, const GLvoid
*ptr
)
779 _mesa_VertexPointer(size
, type
, stride
, ptr
);
784 _mesa_NormalPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
,
788 _mesa_NormalPointer(type
, stride
, ptr
);
793 _mesa_ColorPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
,
797 _mesa_ColorPointer(size
, type
, stride
, ptr
);
802 _mesa_IndexPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
,
806 _mesa_IndexPointer(type
, stride
, ptr
);
811 _mesa_TexCoordPointerEXT(GLint size
, GLenum type
, GLsizei stride
,
812 GLsizei count
, const GLvoid
*ptr
)
815 _mesa_TexCoordPointer(size
, type
, stride
, ptr
);
820 _mesa_EdgeFlagPointerEXT(GLsizei stride
, GLsizei count
, const GLboolean
*ptr
)
823 _mesa_EdgeFlagPointer(stride
, ptr
);
828 _mesa_InterleavedArrays(GLenum format
, GLsizei stride
, const GLvoid
*pointer
)
830 GET_CURRENT_CONTEXT(ctx
);
831 GLboolean tflag
, cflag
, nflag
; /* enable/disable flags */
832 GLint tcomps
, ccomps
, vcomps
; /* components per texcoord, color, vertex */
833 GLenum ctype
= 0; /* color type */
834 GLint coffset
= 0, noffset
= 0, voffset
;/* color, normal, vertex offsets */
835 const GLint toffset
= 0; /* always zero */
836 GLint defstride
; /* default stride */
839 FLUSH_VERTICES(ctx
, 0);
842 c
= f
* ((4 * sizeof(GLubyte
) + (f
- 1)) / f
);
845 _mesa_error( ctx
, GL_INVALID_VALUE
, "glInterleavedArrays(stride)" );
851 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
852 tcomps
= 0; ccomps
= 0; vcomps
= 2;
857 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
858 tcomps
= 0; ccomps
= 0; vcomps
= 3;
863 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
864 tcomps
= 0; ccomps
= 4; vcomps
= 2;
865 ctype
= GL_UNSIGNED_BYTE
;
871 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
872 tcomps
= 0; ccomps
= 4; vcomps
= 3;
873 ctype
= GL_UNSIGNED_BYTE
;
879 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
880 tcomps
= 0; ccomps
= 3; vcomps
= 3;
887 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_TRUE
;
888 tcomps
= 0; ccomps
= 0; vcomps
= 3;
894 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
895 tcomps
= 0; ccomps
= 4; vcomps
= 3;
903 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
904 tcomps
= 2; ccomps
= 0; vcomps
= 3;
909 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
910 tcomps
= 4; ccomps
= 0; vcomps
= 4;
914 case GL_T2F_C4UB_V3F
:
915 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
916 tcomps
= 2; ccomps
= 4; vcomps
= 3;
917 ctype
= GL_UNSIGNED_BYTE
;
923 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
924 tcomps
= 2; ccomps
= 3; vcomps
= 3;
931 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_TRUE
;
932 tcomps
= 2; ccomps
= 0; vcomps
= 3;
937 case GL_T2F_C4F_N3F_V3F
:
938 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
939 tcomps
= 2; ccomps
= 4; vcomps
= 3;
946 case GL_T4F_C4F_N3F_V4F
:
947 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
948 tcomps
= 4; ccomps
= 4; vcomps
= 4;
956 _mesa_error( ctx
, GL_INVALID_ENUM
, "glInterleavedArrays(format)" );
964 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY
);
965 _mesa_DisableClientState( GL_INDEX_ARRAY
);
966 /* XXX also disable secondary color and generic arrays? */
970 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY
);
971 _mesa_TexCoordPointer( tcomps
, GL_FLOAT
, stride
,
972 (GLubyte
*) pointer
+ toffset
);
975 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY
);
980 _mesa_EnableClientState( GL_COLOR_ARRAY
);
981 _mesa_ColorPointer( ccomps
, ctype
, stride
,
982 (GLubyte
*) pointer
+ coffset
);
985 _mesa_DisableClientState( GL_COLOR_ARRAY
);
991 _mesa_EnableClientState( GL_NORMAL_ARRAY
);
992 _mesa_NormalPointer( GL_FLOAT
, stride
, (GLubyte
*) pointer
+ noffset
);
995 _mesa_DisableClientState( GL_NORMAL_ARRAY
);
999 _mesa_EnableClientState( GL_VERTEX_ARRAY
);
1000 _mesa_VertexPointer( vcomps
, GL_FLOAT
, stride
,
1001 (GLubyte
*) pointer
+ voffset
);
1006 _mesa_LockArraysEXT(GLint first
, GLsizei count
)
1008 GET_CURRENT_CONTEXT(ctx
);
1010 FLUSH_VERTICES(ctx
, 0);
1012 if (MESA_VERBOSE
& VERBOSE_API
)
1013 _mesa_debug(ctx
, "glLockArrays %d %d\n", first
, count
);
1016 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLockArraysEXT(first)" );
1020 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLockArraysEXT(count)" );
1023 if (ctx
->Array
.LockCount
!= 0) {
1024 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLockArraysEXT(reentry)" );
1028 ctx
->Array
.LockFirst
= first
;
1029 ctx
->Array
.LockCount
= count
;
1031 ctx
->NewState
|= _NEW_ARRAY
;
1036 _mesa_UnlockArraysEXT( void )
1038 GET_CURRENT_CONTEXT(ctx
);
1040 FLUSH_VERTICES(ctx
, 0);
1042 if (MESA_VERBOSE
& VERBOSE_API
)
1043 _mesa_debug(ctx
, "glUnlockArrays\n");
1045 if (ctx
->Array
.LockCount
== 0) {
1046 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glUnlockArraysEXT(reexit)" );
1050 ctx
->Array
.LockFirst
= 0;
1051 ctx
->Array
.LockCount
= 0;
1052 ctx
->NewState
|= _NEW_ARRAY
;
1056 /* GL_EXT_multi_draw_arrays */
1058 _mesa_MultiDrawArrays( GLenum mode
, const GLint
*first
,
1059 const GLsizei
*count
, GLsizei primcount
)
1061 GET_CURRENT_CONTEXT(ctx
);
1064 FLUSH_VERTICES(ctx
, 0);
1066 for (i
= 0; i
< primcount
; i
++) {
1068 CALL_DrawArrays(ctx
->Exec
, (mode
, first
[i
], count
[i
]));
1074 /* GL_IBM_multimode_draw_arrays */
1076 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
1077 const GLsizei
* count
,
1078 GLsizei primcount
, GLint modestride
)
1080 GET_CURRENT_CONTEXT(ctx
);
1083 FLUSH_VERTICES(ctx
, 0);
1085 for ( i
= 0 ; i
< primcount
; i
++ ) {
1086 if ( count
[i
] > 0 ) {
1087 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
1088 CALL_DrawArrays(ctx
->Exec
, ( m
, first
[i
], count
[i
] ));
1094 /* GL_IBM_multimode_draw_arrays */
1096 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
1097 GLenum type
, const GLvoid
* const * indices
,
1098 GLsizei primcount
, GLint modestride
)
1100 GET_CURRENT_CONTEXT(ctx
);
1103 FLUSH_VERTICES(ctx
, 0);
1105 /* XXX not sure about ARB_vertex_buffer_object handling here */
1107 for ( i
= 0 ; i
< primcount
; i
++ ) {
1108 if ( count
[i
] > 0 ) {
1109 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
1110 CALL_DrawElements(ctx
->Exec
, ( m
, count
[i
], type
, indices
[i
] ));
1117 * GL_NV_primitive_restart and GL 3.1
1120 _mesa_PrimitiveRestartIndex(GLuint index
)
1122 GET_CURRENT_CONTEXT(ctx
);
1124 if (!ctx
->Extensions
.NV_primitive_restart
&& ctx
->Version
< 31) {
1125 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glPrimitiveRestartIndexNV()");
1129 if (ctx
->Array
.RestartIndex
!= index
) {
1130 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
);
1131 ctx
->Array
.RestartIndex
= index
;
1137 * See GL_ARB_instanced_arrays.
1138 * Note that the instance divisor only applies to generic arrays, not
1139 * the legacy vertex arrays.
1142 _mesa_VertexAttribDivisor(GLuint index
, GLuint divisor
)
1144 struct gl_client_array
*array
;
1145 GET_CURRENT_CONTEXT(ctx
);
1147 if (!ctx
->Extensions
.ARB_instanced_arrays
) {
1148 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glVertexAttribDivisor()");
1152 if (index
>= ctx
->Const
.VertexProgram
.MaxAttribs
) {
1153 _mesa_error(ctx
, GL_INVALID_VALUE
, "glVertexAttribDivisor(index = %u)",
1158 ASSERT(VERT_ATTRIB_GENERIC(index
) < Elements(ctx
->Array
.ArrayObj
->VertexAttrib
));
1160 array
= &ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(index
)];
1161 if (array
->InstanceDivisor
!= divisor
) {
1162 FLUSH_VERTICES(ctx
, _NEW_ARRAY
);
1163 array
->InstanceDivisor
= divisor
;
1169 _mesa_primitive_restart_index(const struct gl_context
*ctx
, GLenum ib_type
)
1171 /* From the OpenGL 4.3 core specification, page 302:
1172 * "If both PRIMITIVE_RESTART and PRIMITIVE_RESTART_FIXED_INDEX are
1173 * enabled, the index value determined by PRIMITIVE_RESTART_FIXED_INDEX
1176 if (ctx
->Array
.PrimitiveRestartFixedIndex
) {
1178 case GL_UNSIGNED_BYTE
:
1180 case GL_UNSIGNED_SHORT
:
1182 case GL_UNSIGNED_INT
:
1185 assert(!"_mesa_primitive_restart_index: Invalid index buffer type.");
1189 return ctx
->Array
.RestartIndex
;
1194 * Copy one client vertex array to another.
1197 _mesa_copy_client_array(struct gl_context
*ctx
,
1198 struct gl_client_array
*dst
,
1199 struct gl_client_array
*src
)
1201 dst
->Size
= src
->Size
;
1202 dst
->Type
= src
->Type
;
1203 dst
->Format
= src
->Format
;
1204 dst
->Stride
= src
->Stride
;
1205 dst
->StrideB
= src
->StrideB
;
1206 dst
->Ptr
= src
->Ptr
;
1207 dst
->Enabled
= src
->Enabled
;
1208 dst
->Normalized
= src
->Normalized
;
1209 dst
->Integer
= src
->Integer
;
1210 dst
->InstanceDivisor
= src
->InstanceDivisor
;
1211 dst
->_ElementSize
= src
->_ElementSize
;
1212 _mesa_reference_buffer_object(ctx
, &dst
->BufferObj
, src
->BufferObj
);
1213 dst
->_MaxElement
= src
->_MaxElement
;
1219 * Print vertex array's fields.
1222 print_array(const char *name
, GLint index
, const struct gl_client_array
*array
)
1225 printf(" %s[%d]: ", name
, index
);
1227 printf(" %s: ", name
);
1228 printf("Ptr=%p, Type=0x%x, Size=%d, ElemSize=%u, Stride=%d, Buffer=%u(Size %lu), MaxElem=%u\n",
1229 array
->Ptr
, array
->Type
, array
->Size
,
1230 array
->_ElementSize
, array
->StrideB
,
1231 array
->BufferObj
->Name
, (unsigned long) array
->BufferObj
->Size
,
1232 array
->_MaxElement
);
1237 * Print current vertex object/array info. For debug.
1240 _mesa_print_arrays(struct gl_context
*ctx
)
1242 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
1245 _mesa_update_array_object_max_element(ctx
, arrayObj
);
1247 printf("Array Object %u\n", arrayObj
->Name
);
1248 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
1249 print_array("Vertex", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
]);
1250 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
].Enabled
)
1251 print_array("Normal", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
]);
1252 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
].Enabled
)
1253 print_array("Color", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
]);
1254 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
1255 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX(i
)].Enabled
)
1256 print_array("TexCoord", i
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX(i
)]);
1257 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++)
1258 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
1259 print_array("Attrib", i
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(i
)]);
1260 printf(" _MaxElement = %u\n", arrayObj
->_MaxElement
);
1265 * Initialize vertex array state for given context.
1268 _mesa_init_varray(struct gl_context
*ctx
)
1270 ctx
->Array
.DefaultArrayObj
= ctx
->Driver
.NewArrayObject(ctx
, 0);
1271 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
,
1272 ctx
->Array
.DefaultArrayObj
);
1273 ctx
->Array
.ActiveTexture
= 0; /* GL_ARB_multitexture */
1275 ctx
->Array
.Objects
= _mesa_NewHashTable();
1280 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
1283 delete_arrayobj_cb(GLuint id
, void *data
, void *userData
)
1285 struct gl_array_object
*arrayObj
= (struct gl_array_object
*) data
;
1286 struct gl_context
*ctx
= (struct gl_context
*) userData
;
1287 _mesa_delete_array_object(ctx
, arrayObj
);
1292 * Free vertex array state for given context.
1295 _mesa_free_varray_data(struct gl_context
*ctx
)
1297 _mesa_HashDeleteAll(ctx
->Array
.Objects
, delete_arrayobj_cb
, ctx
);
1298 _mesa_DeleteHashTable(ctx
->Array
.Objects
);