2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 /** Used to indicate which GL datatypes are accepted by each of the
29 * glVertex/Color/Attrib/EtcPointer() functions.
33 #define UNSIGNED_BYTE_BIT 0x4
35 #define UNSIGNED_SHORT_BIT 0x10
37 #define UNSIGNED_INT_BIT 0x40
39 #define FLOAT_BIT 0x100
40 #define DOUBLE_BIT 0x200
43 /** Convert GL datatype enum into a <type>_BIT value seen above */
45 type_to_bit(const struct gl_context
*ctx
, GLenum type
)
52 case GL_UNSIGNED_BYTE
:
53 return UNSIGNED_BYTE_BIT
;
56 case GL_UNSIGNED_SHORT
:
57 return UNSIGNED_SHORT_BIT
;
61 return UNSIGNED_INT_BIT
;
73 * Do error checking and update state for glVertex/Color/TexCoord/...Pointer
76 * \param func name of calling function used for error reporting
77 * \param attrib the attribute array index to update
78 * \param legalTypes bitmask of *_BIT above indicating legal datatypes
79 * \param sizeMin min allowable size value
80 * \param sizeMax max allowable size value
81 * \param size components per element (1, 2, 3 or 4)
82 * \param type datatype of each component (GL_FLOAT, GL_INT, etc)
83 * \param stride stride between elements, in elements
84 * \param normalized are integer types converted to floats in [-1, 1]?
85 * \param integer integer-valued values (will not be normalized to [-1,1])
86 * \param ptr the address (or offset inside VBO) of the array data
89 update_array(struct gl_context
*ctx
,
91 GLuint attrib
, GLbitfield legalTypesMask
,
92 GLint sizeMin
, GLint sizeMax
,
93 GLint size
, GLenum type
, GLsizei stride
,
94 GLboolean normalized
, GLboolean integer
,
97 struct gl_client_array
*array
;
101 typeBit
= type_to_bit(ctx
, type
);
102 if (typeBit
== 0x0 || (typeBit
& legalTypesMask
) == 0x0) {
103 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)",
104 func
, _mesa_lookup_enum_by_nr(type
));
108 /* Do size parameter checking. */
109 if (size
< sizeMin
|| size
> sizeMax
|| size
> 4) {
110 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d)", func
, size
);
117 _mesa_error( ctx
, GL_INVALID_VALUE
, "%s(stride=%d)", func
, stride
);
121 if (ctx
->Array
.ArrayObj
->ARBsemantics
&&
122 !_mesa_is_bufferobj(ctx
->Array
.ArrayBufferObj
)) {
123 /* GL_ARB_vertex_array_object requires that all arrays reside in VBOs.
124 * Generate GL_INVALID_OPERATION if that's not true.
126 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-VBO array)", func
);
130 elementSize
= _mesa_sizeof_type(type
) * size
;
132 array
= &ctx
->Array
.ArrayObj
->VertexAttrib
[attrib
];
135 array
->Stride
= stride
;
136 array
->StrideB
= stride
? stride
: elementSize
;
137 array
->Normalized
= normalized
;
138 array
->Integer
= integer
;
139 array
->Ptr
= (const GLubyte
*) ptr
;
140 array
->_ElementSize
= elementSize
;
142 _mesa_reference_buffer_object(ctx
, &array
->BufferObj
,
143 ctx
->Array
.ArrayBufferObj
);
145 ctx
->NewState
|= _NEW_ARRAY
;
146 ctx
->Array
.NewState
|= VERT_BIT(attrib
);
151 _mesa_VertexPointer(GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
153 GLbitfield legalTypes
= (SHORT_BIT
| INT_BIT
| FLOAT_BIT
|
154 DOUBLE_BIT
| HALF_BIT
);
155 GET_CURRENT_CONTEXT(ctx
);
156 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
158 update_array(ctx
, "glVertexPointer", VERT_ATTRIB_POS
,
160 size
, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
165 _mesa_NormalPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
167 const GLbitfield legalTypes
= (BYTE_BIT
| SHORT_BIT
| INT_BIT
|
168 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
169 GET_CURRENT_CONTEXT(ctx
);
170 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
172 update_array(ctx
, "glNormalPointer", VERT_ATTRIB_NORMAL
,
174 3, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
179 _mesa_ColorPointer(GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
181 const GLbitfield legalTypes
= (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
182 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
183 INT_BIT
| UNSIGNED_INT_BIT
|
184 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
185 GET_CURRENT_CONTEXT(ctx
);
186 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
188 update_array(ctx
, "glColorPointer", VERT_ATTRIB_COLOR0
,
190 size
, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
195 _mesa_FogCoordPointerEXT(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
197 const GLbitfield legalTypes
= (HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
198 GET_CURRENT_CONTEXT(ctx
);
199 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
201 update_array(ctx
, "glFogCoordPointer", VERT_ATTRIB_FOG
,
203 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
208 _mesa_IndexPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
210 const GLbitfield legalTypes
= (UNSIGNED_BYTE_BIT
| SHORT_BIT
| INT_BIT
|
211 FLOAT_BIT
| DOUBLE_BIT
);
212 GET_CURRENT_CONTEXT(ctx
);
213 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
215 update_array(ctx
, "glIndexPointer", VERT_ATTRIB_COLOR_INDEX
,
217 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
222 _mesa_SecondaryColorPointerEXT(GLint size
, GLenum type
,
223 GLsizei stride
, const GLvoid
*ptr
)
225 const GLbitfield legalTypes
= (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
226 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
227 INT_BIT
| UNSIGNED_INT_BIT
|
228 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
229 GET_CURRENT_CONTEXT(ctx
);
230 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
232 update_array(ctx
, "glSecondaryColorPointer", VERT_ATTRIB_COLOR1
,
234 size
, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
239 _mesa_TexCoordPointer(GLint size
, GLenum type
, GLsizei stride
,
242 GLbitfield legalTypes
= (SHORT_BIT
| INT_BIT
|
243 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
244 GET_CURRENT_CONTEXT(ctx
);
245 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
247 update_array(ctx
, "glTexCoordPointer", VERT_ATTRIB_TEX
,
249 size
, type
, stride
, GL_FALSE
, GL_FALSE
,
255 _mesa_EdgeFlagPointer(GLsizei stride
, const GLvoid
*ptr
)
257 const GLbitfield legalTypes
= UNSIGNED_BYTE_BIT
;
258 /* see table 2.4 edits in GL_EXT_gpu_shader4 spec: */
259 const GLboolean integer
= GL_TRUE
;
260 GET_CURRENT_CONTEXT(ctx
);
261 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
263 update_array(ctx
, "glEdgeFlagPointer", VERT_ATTRIB_EDGEFLAG
,
265 1, GL_UNSIGNED_BYTE
, stride
, GL_FALSE
, integer
, ptr
);
269 _mesa_VertexPointerEXT(GLint size
, GLenum type
, GLsizei stride
,
270 GLsizei count
, const GLvoid
*ptr
)
273 _mesa_VertexPointer(size
, type
, stride
, ptr
);
278 _mesa_NormalPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
,
282 _mesa_NormalPointer(type
, stride
, ptr
);
287 _mesa_ColorPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
,
291 _mesa_ColorPointer(size
, type
, stride
, ptr
);
296 _mesa_IndexPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
,
300 _mesa_IndexPointer(type
, stride
, ptr
);
305 _mesa_TexCoordPointerEXT(GLint size
, GLenum type
, GLsizei stride
,
306 GLsizei count
, const GLvoid
*ptr
)
309 _mesa_TexCoordPointer(size
, type
, stride
, ptr
);
314 _mesa_EdgeFlagPointerEXT(GLsizei stride
, GLsizei count
, const GLboolean
*ptr
)
317 _mesa_EdgeFlagPointer(stride
, ptr
);
322 _mesa_InterleavedArrays(GLenum format
, GLsizei stride
, const GLvoid
*pointer
)
324 GET_CURRENT_CONTEXT(ctx
);
325 GLboolean tflag
, cflag
, nflag
; /* enable/disable flags */
326 GLint tcomps
, ccomps
, vcomps
; /* components per texcoord, color, vertex */
327 GLenum ctype
= 0; /* color type */
328 GLint coffset
= 0, noffset
= 0, voffset
;/* color, normal, vertex offsets */
329 const GLint toffset
= 0; /* always zero */
330 GLint defstride
; /* default stride */
333 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
336 c
= f
* ((4 * sizeof(GLubyte
) + (f
- 1)) / f
);
339 _mesa_error( ctx
, GL_INVALID_VALUE
, "glInterleavedArrays(stride)" );
345 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
346 tcomps
= 0; ccomps
= 0; vcomps
= 2;
351 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
352 tcomps
= 0; ccomps
= 0; vcomps
= 3;
357 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
358 tcomps
= 0; ccomps
= 4; vcomps
= 2;
359 ctype
= GL_UNSIGNED_BYTE
;
365 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
366 tcomps
= 0; ccomps
= 4; vcomps
= 3;
367 ctype
= GL_UNSIGNED_BYTE
;
373 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
374 tcomps
= 0; ccomps
= 3; vcomps
= 3;
381 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_TRUE
;
382 tcomps
= 0; ccomps
= 0; vcomps
= 3;
388 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
389 tcomps
= 0; ccomps
= 4; vcomps
= 3;
397 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
398 tcomps
= 2; ccomps
= 0; vcomps
= 3;
403 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
404 tcomps
= 4; ccomps
= 0; vcomps
= 4;
408 case GL_T2F_C4UB_V3F
:
409 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
410 tcomps
= 2; ccomps
= 4; vcomps
= 3;
411 ctype
= GL_UNSIGNED_BYTE
;
417 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
418 tcomps
= 2; ccomps
= 3; vcomps
= 3;
425 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_TRUE
;
426 tcomps
= 2; ccomps
= 0; vcomps
= 3;
431 case GL_T2F_C4F_N3F_V3F
:
432 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
433 tcomps
= 2; ccomps
= 4; vcomps
= 3;
440 case GL_T4F_C4F_N3F_V4F
:
441 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
442 tcomps
= 4; ccomps
= 4; vcomps
= 4;
450 _mesa_error( ctx
, GL_INVALID_ENUM
, "glInterleavedArrays(format)" );
458 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY
);
459 _mesa_DisableClientState( GL_INDEX_ARRAY
);
460 /* XXX also disable secondary color and generic arrays? */
464 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY
);
465 _mesa_TexCoordPointer( tcomps
, GL_FLOAT
, stride
,
466 (GLubyte
*) pointer
+ toffset
);
469 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY
);
474 _mesa_EnableClientState( GL_COLOR_ARRAY
);
475 _mesa_ColorPointer( ccomps
, ctype
, stride
,
476 (GLubyte
*) pointer
+ coffset
);
479 _mesa_DisableClientState( GL_COLOR_ARRAY
);
485 _mesa_EnableClientState( GL_NORMAL_ARRAY
);
486 _mesa_NormalPointer( GL_FLOAT
, stride
, (GLubyte
*) pointer
+ noffset
);
489 _mesa_DisableClientState( GL_NORMAL_ARRAY
);
493 _mesa_EnableClientState( GL_VERTEX_ARRAY
);
494 _mesa_VertexPointer( vcomps
, GL_FLOAT
, stride
,
495 (GLubyte
*) pointer
+ voffset
);
500 _mesa_LockArraysEXT(GLint first
, GLsizei count
)
502 GET_CURRENT_CONTEXT(ctx
);
503 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
505 if (MESA_VERBOSE
& VERBOSE_API
)
506 _mesa_debug(ctx
, "glLockArrays %d %d\n", first
, count
);
509 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLockArraysEXT(first)" );
513 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLockArraysEXT(count)" );
516 if (ctx
->Array
.LockCount
!= 0) {
517 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLockArraysEXT(reentry)" );
521 ctx
->Array
.LockFirst
= first
;
522 ctx
->Array
.LockCount
= count
;
524 ctx
->NewState
|= _NEW_ARRAY
;
525 ctx
->Array
.NewState
|= VERT_BIT_ALL
;
530 _mesa_UnlockArraysEXT( void )
532 GET_CURRENT_CONTEXT(ctx
);
533 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
535 if (MESA_VERBOSE
& VERBOSE_API
)
536 _mesa_debug(ctx
, "glUnlockArrays\n");
538 if (ctx
->Array
.LockCount
== 0) {
539 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glUnlockArraysEXT(reexit)" );
543 ctx
->Array
.LockFirst
= 0;
544 ctx
->Array
.LockCount
= 0;
545 ctx
->NewState
|= _NEW_ARRAY
;
546 ctx
->Array
.NewState
|= VERT_BIT_ALL
;
549 /* GL_IBM_multimode_draw_arrays */
551 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
552 const GLsizei
* count
,
553 GLsizei primcount
, GLint modestride
)
555 GET_CURRENT_CONTEXT(ctx
);
558 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
560 for ( i
= 0 ; i
< primcount
; i
++ ) {
561 if ( count
[i
] > 0 ) {
562 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
563 CALL_DrawArrays(ctx
->Exec
, ( m
, first
[i
], count
[i
] ));
569 /* GL_IBM_multimode_draw_arrays */
571 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
572 GLenum type
, const GLvoid
* const * indices
,
573 GLsizei primcount
, GLint modestride
)
575 GET_CURRENT_CONTEXT(ctx
);
578 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
580 /* XXX not sure about ARB_vertex_buffer_object handling here */
582 for ( i
= 0 ; i
< primcount
; i
++ ) {
583 if ( count
[i
] > 0 ) {
584 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
585 CALL_DrawElements(ctx
->Exec
, ( m
, count
[i
], type
, indices
[i
] ));
592 * Copy one client vertex array to another.
595 _mesa_copy_client_array(struct gl_context
*ctx
,
596 struct gl_client_array
*dst
,
597 struct gl_client_array
*src
)
599 dst
->Size
= src
->Size
;
600 dst
->Type
= src
->Type
;
601 dst
->Stride
= src
->Stride
;
602 dst
->StrideB
= src
->StrideB
;
604 dst
->Enabled
= src
->Enabled
;
605 dst
->Normalized
= src
->Normalized
;
606 dst
->Integer
= src
->Integer
;
607 dst
->_ElementSize
= src
->_ElementSize
;
608 _mesa_reference_buffer_object(ctx
, &dst
->BufferObj
, src
->BufferObj
);
609 dst
->_MaxElement
= src
->_MaxElement
;
615 * Print vertex array's fields.
618 print_array(const char *name
, GLint index
, const struct gl_client_array
*array
)
621 printf(" %s[%d]: ", name
, index
);
623 printf(" %s: ", name
);
624 printf("Ptr=%p, Type=0x%x, Size=%d, ElemSize=%u, Stride=%d, Buffer=%u(Size %lu), MaxElem=%u\n",
625 array
->Ptr
, array
->Type
, array
->Size
,
626 array
->_ElementSize
, array
->StrideB
,
627 array
->BufferObj
->Name
, (unsigned long) array
->BufferObj
->Size
,
633 * Print current vertex object/array info. For debug.
636 _mesa_print_arrays(struct gl_context
*ctx
)
638 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
640 _mesa_update_array_object_max_element(ctx
, arrayObj
);
642 printf("Array Object %u\n", arrayObj
->Name
);
643 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
644 print_array("Vertex", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
]);
645 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
].Enabled
)
646 print_array("Normal", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
]);
647 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
].Enabled
)
648 print_array("Color", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
]);
649 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX
].Enabled
)
650 print_array("TexCoord", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX
]);
651 printf(" _MaxElement = %u\n", arrayObj
->_MaxElement
);
656 * Initialize vertex array state for given context.
659 _mesa_init_varray(struct gl_context
*ctx
)
661 ctx
->Array
.DefaultArrayObj
= _mesa_new_array_object(ctx
, 0);
662 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
,
663 ctx
->Array
.DefaultArrayObj
);
665 ctx
->Array
.Objects
= _mesa_NewHashTable();
670 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
673 delete_arrayobj_cb(GLuint id
, void *data
, void *userData
)
675 struct gl_array_object
*arrayObj
= (struct gl_array_object
*) data
;
676 struct gl_context
*ctx
= (struct gl_context
*) userData
;
677 _mesa_delete_array_object(ctx
, arrayObj
);
682 * Free vertex array state for given context.
685 _mesa_free_varray_data(struct gl_context
*ctx
)
687 _mesa_HashDeleteAll(ctx
->Array
.Objects
, delete_arrayobj_cb
, ctx
);
688 _mesa_DeleteHashTable(ctx
->Array
.Objects
);