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
;
63 if (ctx
->Extensions
.ARB_half_float_vertex
)
78 * Do error checking and update state for glVertex/Color/TexCoord/...Pointer
81 * \param func name of calling function used for error reporting
82 * \param attrib the attribute array index to update
83 * \param legalTypes bitmask of *_BIT above indicating legal datatypes
84 * \param sizeMin min allowable size value
85 * \param sizeMax max allowable size value
86 * \param size components per element (1, 2, 3 or 4)
87 * \param type datatype of each component (GL_FLOAT, GL_INT, etc)
88 * \param stride stride between elements, in elements
89 * \param normalized are integer types converted to floats in [-1, 1]?
90 * \param integer integer-valued values (will not be normalized to [-1,1])
91 * \param ptr the address (or offset inside VBO) of the array data
94 update_array(struct gl_context
*ctx
,
96 GLuint attrib
, GLbitfield legalTypesMask
,
97 GLint sizeMin
, GLint sizeMax
,
98 GLint size
, GLenum type
, GLsizei stride
,
99 GLboolean normalized
, GLboolean integer
,
102 struct gl_client_array
*array
;
106 typeBit
= type_to_bit(ctx
, type
);
107 if (typeBit
== 0x0 || (typeBit
& legalTypesMask
) == 0x0) {
108 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(type = %s)",
109 func
, _mesa_lookup_enum_by_nr(type
));
113 /* Do size parameter checking. */
114 if (size
< sizeMin
|| size
> sizeMax
|| size
> 4) {
115 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d)", func
, size
);
122 _mesa_error( ctx
, GL_INVALID_VALUE
, "%s(stride=%d)", func
, stride
);
126 if (ctx
->Array
.ArrayObj
->ARBsemantics
&&
127 !_mesa_is_bufferobj(ctx
->Array
.ArrayBufferObj
)) {
128 /* GL_ARB_vertex_array_object requires that all arrays reside in VBOs.
129 * Generate GL_INVALID_OPERATION if that's not true.
131 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-VBO array)", func
);
135 elementSize
= _mesa_sizeof_type(type
) * size
;
137 array
= &ctx
->Array
.ArrayObj
->VertexAttrib
[attrib
];
140 array
->Stride
= stride
;
141 array
->StrideB
= stride
? stride
: elementSize
;
142 array
->Normalized
= normalized
;
143 array
->Integer
= integer
;
144 array
->Ptr
= (const GLubyte
*) ptr
;
145 array
->_ElementSize
= elementSize
;
147 _mesa_reference_buffer_object(ctx
, &array
->BufferObj
,
148 ctx
->Array
.ArrayBufferObj
);
150 ctx
->NewState
|= _NEW_ARRAY
;
151 ctx
->Array
.NewState
|= VERT_BIT(attrib
);
156 _mesa_VertexPointer(GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
158 GLbitfield legalTypes
= (SHORT_BIT
| INT_BIT
| FLOAT_BIT
|
159 DOUBLE_BIT
| HALF_BIT
);
160 GET_CURRENT_CONTEXT(ctx
);
161 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
163 update_array(ctx
, "glVertexPointer", VERT_ATTRIB_POS
,
165 size
, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
170 _mesa_NormalPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
172 const GLbitfield legalTypes
= (BYTE_BIT
| SHORT_BIT
| INT_BIT
|
173 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
174 GET_CURRENT_CONTEXT(ctx
);
175 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
177 update_array(ctx
, "glNormalPointer", VERT_ATTRIB_NORMAL
,
179 3, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
184 _mesa_ColorPointer(GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
186 const GLbitfield legalTypes
= (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
187 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
188 INT_BIT
| UNSIGNED_INT_BIT
|
189 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
190 GET_CURRENT_CONTEXT(ctx
);
191 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
193 update_array(ctx
, "glColorPointer", VERT_ATTRIB_COLOR0
,
195 size
, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
200 _mesa_FogCoordPointerEXT(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
202 const GLbitfield legalTypes
= (HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
203 GET_CURRENT_CONTEXT(ctx
);
204 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
206 update_array(ctx
, "glFogCoordPointer", VERT_ATTRIB_FOG
,
208 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
213 _mesa_IndexPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
215 const GLbitfield legalTypes
= (UNSIGNED_BYTE_BIT
| SHORT_BIT
| INT_BIT
|
216 FLOAT_BIT
| DOUBLE_BIT
);
217 GET_CURRENT_CONTEXT(ctx
);
218 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
220 update_array(ctx
, "glIndexPointer", VERT_ATTRIB_COLOR_INDEX
,
222 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
227 _mesa_SecondaryColorPointerEXT(GLint size
, GLenum type
,
228 GLsizei stride
, const GLvoid
*ptr
)
230 const GLbitfield legalTypes
= (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
231 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
232 INT_BIT
| UNSIGNED_INT_BIT
|
233 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
234 GET_CURRENT_CONTEXT(ctx
);
235 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
237 update_array(ctx
, "glSecondaryColorPointer", VERT_ATTRIB_COLOR1
,
239 size
, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
244 _mesa_TexCoordPointer(GLint size
, GLenum type
, GLsizei stride
,
247 GLbitfield legalTypes
= (SHORT_BIT
| INT_BIT
|
248 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
249 GET_CURRENT_CONTEXT(ctx
);
250 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
252 update_array(ctx
, "glTexCoordPointer", VERT_ATTRIB_TEX
,
254 size
, type
, stride
, GL_FALSE
, GL_FALSE
,
260 _mesa_EdgeFlagPointer(GLsizei stride
, const GLvoid
*ptr
)
262 const GLbitfield legalTypes
= UNSIGNED_BYTE_BIT
;
263 /* see table 2.4 edits in GL_EXT_gpu_shader4 spec: */
264 const GLboolean integer
= GL_TRUE
;
265 GET_CURRENT_CONTEXT(ctx
);
266 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
268 update_array(ctx
, "glEdgeFlagPointer", VERT_ATTRIB_EDGEFLAG
,
270 1, GL_UNSIGNED_BYTE
, stride
, GL_FALSE
, integer
, ptr
);
274 _mesa_VertexPointerEXT(GLint size
, GLenum type
, GLsizei stride
,
275 GLsizei count
, const GLvoid
*ptr
)
278 _mesa_VertexPointer(size
, type
, stride
, ptr
);
283 _mesa_NormalPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
,
287 _mesa_NormalPointer(type
, stride
, ptr
);
292 _mesa_ColorPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
,
296 _mesa_ColorPointer(size
, type
, stride
, ptr
);
301 _mesa_IndexPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
,
305 _mesa_IndexPointer(type
, stride
, ptr
);
310 _mesa_TexCoordPointerEXT(GLint size
, GLenum type
, GLsizei stride
,
311 GLsizei count
, const GLvoid
*ptr
)
314 _mesa_TexCoordPointer(size
, type
, stride
, ptr
);
319 _mesa_EdgeFlagPointerEXT(GLsizei stride
, GLsizei count
, const GLboolean
*ptr
)
322 _mesa_EdgeFlagPointer(stride
, ptr
);
327 _mesa_InterleavedArrays(GLenum format
, GLsizei stride
, const GLvoid
*pointer
)
329 GET_CURRENT_CONTEXT(ctx
);
330 GLboolean tflag
, cflag
, nflag
; /* enable/disable flags */
331 GLint tcomps
, ccomps
, vcomps
; /* components per texcoord, color, vertex */
332 GLenum ctype
= 0; /* color type */
333 GLint coffset
= 0, noffset
= 0, voffset
;/* color, normal, vertex offsets */
334 const GLint toffset
= 0; /* always zero */
335 GLint defstride
; /* default stride */
338 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
341 c
= f
* ((4 * sizeof(GLubyte
) + (f
- 1)) / f
);
344 _mesa_error( ctx
, GL_INVALID_VALUE
, "glInterleavedArrays(stride)" );
350 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
351 tcomps
= 0; ccomps
= 0; vcomps
= 2;
356 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
357 tcomps
= 0; ccomps
= 0; vcomps
= 3;
362 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
363 tcomps
= 0; ccomps
= 4; vcomps
= 2;
364 ctype
= GL_UNSIGNED_BYTE
;
370 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
371 tcomps
= 0; ccomps
= 4; vcomps
= 3;
372 ctype
= GL_UNSIGNED_BYTE
;
378 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
379 tcomps
= 0; ccomps
= 3; vcomps
= 3;
386 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_TRUE
;
387 tcomps
= 0; ccomps
= 0; vcomps
= 3;
393 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
394 tcomps
= 0; ccomps
= 4; vcomps
= 3;
402 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
403 tcomps
= 2; ccomps
= 0; vcomps
= 3;
408 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
409 tcomps
= 4; ccomps
= 0; vcomps
= 4;
413 case GL_T2F_C4UB_V3F
:
414 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
415 tcomps
= 2; ccomps
= 4; vcomps
= 3;
416 ctype
= GL_UNSIGNED_BYTE
;
422 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
423 tcomps
= 2; ccomps
= 3; vcomps
= 3;
430 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_TRUE
;
431 tcomps
= 2; ccomps
= 0; vcomps
= 3;
436 case GL_T2F_C4F_N3F_V3F
:
437 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
438 tcomps
= 2; ccomps
= 4; vcomps
= 3;
445 case GL_T4F_C4F_N3F_V4F
:
446 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
447 tcomps
= 4; ccomps
= 4; vcomps
= 4;
455 _mesa_error( ctx
, GL_INVALID_ENUM
, "glInterleavedArrays(format)" );
463 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY
);
464 _mesa_DisableClientState( GL_INDEX_ARRAY
);
465 /* XXX also disable secondary color and generic arrays? */
469 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY
);
470 _mesa_TexCoordPointer( tcomps
, GL_FLOAT
, stride
,
471 (GLubyte
*) pointer
+ toffset
);
474 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY
);
479 _mesa_EnableClientState( GL_COLOR_ARRAY
);
480 _mesa_ColorPointer( ccomps
, ctype
, stride
,
481 (GLubyte
*) pointer
+ coffset
);
484 _mesa_DisableClientState( GL_COLOR_ARRAY
);
490 _mesa_EnableClientState( GL_NORMAL_ARRAY
);
491 _mesa_NormalPointer( GL_FLOAT
, stride
, (GLubyte
*) pointer
+ noffset
);
494 _mesa_DisableClientState( GL_NORMAL_ARRAY
);
498 _mesa_EnableClientState( GL_VERTEX_ARRAY
);
499 _mesa_VertexPointer( vcomps
, GL_FLOAT
, stride
,
500 (GLubyte
*) pointer
+ voffset
);
505 _mesa_LockArraysEXT(GLint first
, GLsizei count
)
507 GET_CURRENT_CONTEXT(ctx
);
508 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
510 if (MESA_VERBOSE
& VERBOSE_API
)
511 _mesa_debug(ctx
, "glLockArrays %d %d\n", first
, count
);
514 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLockArraysEXT(first)" );
518 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLockArraysEXT(count)" );
521 if (ctx
->Array
.LockCount
!= 0) {
522 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLockArraysEXT(reentry)" );
526 ctx
->Array
.LockFirst
= first
;
527 ctx
->Array
.LockCount
= count
;
529 ctx
->NewState
|= _NEW_ARRAY
;
530 ctx
->Array
.NewState
|= VERT_BIT_ALL
;
535 _mesa_UnlockArraysEXT( void )
537 GET_CURRENT_CONTEXT(ctx
);
538 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
540 if (MESA_VERBOSE
& VERBOSE_API
)
541 _mesa_debug(ctx
, "glUnlockArrays\n");
543 if (ctx
->Array
.LockCount
== 0) {
544 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glUnlockArraysEXT(reexit)" );
548 ctx
->Array
.LockFirst
= 0;
549 ctx
->Array
.LockCount
= 0;
550 ctx
->NewState
|= _NEW_ARRAY
;
551 ctx
->Array
.NewState
|= VERT_BIT_ALL
;
554 /* GL_IBM_multimode_draw_arrays */
556 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
557 const GLsizei
* count
,
558 GLsizei primcount
, GLint modestride
)
560 GET_CURRENT_CONTEXT(ctx
);
563 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
565 for ( i
= 0 ; i
< primcount
; i
++ ) {
566 if ( count
[i
] > 0 ) {
567 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
568 CALL_DrawArrays(ctx
->Exec
, ( m
, first
[i
], count
[i
] ));
574 /* GL_IBM_multimode_draw_arrays */
576 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
577 GLenum type
, const GLvoid
* const * indices
,
578 GLsizei primcount
, GLint modestride
)
580 GET_CURRENT_CONTEXT(ctx
);
583 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
585 /* XXX not sure about ARB_vertex_buffer_object handling here */
587 for ( i
= 0 ; i
< primcount
; i
++ ) {
588 if ( count
[i
] > 0 ) {
589 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
590 CALL_DrawElements(ctx
->Exec
, ( m
, count
[i
], type
, indices
[i
] ));
597 * Copy one client vertex array to another.
600 _mesa_copy_client_array(struct gl_context
*ctx
,
601 struct gl_client_array
*dst
,
602 struct gl_client_array
*src
)
604 dst
->Size
= src
->Size
;
605 dst
->Type
= src
->Type
;
606 dst
->Stride
= src
->Stride
;
607 dst
->StrideB
= src
->StrideB
;
609 dst
->Enabled
= src
->Enabled
;
610 dst
->Normalized
= src
->Normalized
;
611 dst
->Integer
= src
->Integer
;
612 dst
->_ElementSize
= src
->_ElementSize
;
613 _mesa_reference_buffer_object(ctx
, &dst
->BufferObj
, src
->BufferObj
);
614 dst
->_MaxElement
= src
->_MaxElement
;
620 * Print vertex array's fields.
623 print_array(const char *name
, GLint index
, const struct gl_client_array
*array
)
626 printf(" %s[%d]: ", name
, index
);
628 printf(" %s: ", name
);
629 printf("Ptr=%p, Type=0x%x, Size=%d, ElemSize=%u, Stride=%d, Buffer=%u(Size %lu), MaxElem=%u\n",
630 array
->Ptr
, array
->Type
, array
->Size
,
631 array
->_ElementSize
, array
->StrideB
,
632 array
->BufferObj
->Name
, (unsigned long) array
->BufferObj
->Size
,
638 * Print current vertex object/array info. For debug.
641 _mesa_print_arrays(struct gl_context
*ctx
)
643 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
645 _mesa_update_array_object_max_element(ctx
, arrayObj
);
647 printf("Array Object %u\n", arrayObj
->Name
);
648 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
649 print_array("Vertex", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
]);
650 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
].Enabled
)
651 print_array("Normal", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
]);
652 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
].Enabled
)
653 print_array("Color", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
]);
654 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX
].Enabled
)
655 print_array("TexCoord", -1, &arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX
]);
656 printf(" _MaxElement = %u\n", arrayObj
->_MaxElement
);
661 * Initialize vertex array state for given context.
664 _mesa_init_varray(struct gl_context
*ctx
)
666 ctx
->Array
.DefaultArrayObj
= _mesa_new_array_object(ctx
, 0);
667 _mesa_reference_array_object(ctx
, &ctx
->Array
.ArrayObj
,
668 ctx
->Array
.DefaultArrayObj
);
670 ctx
->Array
.Objects
= _mesa_NewHashTable();
675 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
678 delete_arrayobj_cb(GLuint id
, void *data
, void *userData
)
680 struct gl_array_object
*arrayObj
= (struct gl_array_object
*) data
;
681 struct gl_context
*ctx
= (struct gl_context
*) userData
;
682 _mesa_delete_array_object(ctx
, arrayObj
);
687 * Free vertex array state for given context.
690 _mesa_free_varray_data(struct gl_context
*ctx
)
692 _mesa_HashDeleteAll(ctx
->Array
.Objects
, delete_arrayobj_cb
, ctx
);
693 _mesa_DeleteHashTable(ctx
->Array
.Objects
);