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 elementSize
= _mesa_sizeof_type(type
) * size
;
123 array
= &ctx
->Array
.VertexAttrib
[attrib
];
126 array
->Stride
= stride
;
127 array
->StrideB
= stride
? stride
: elementSize
;
128 array
->Normalized
= normalized
;
129 array
->Integer
= integer
;
130 array
->Ptr
= (const GLubyte
*) ptr
;
131 array
->_ElementSize
= elementSize
;
133 ctx
->NewState
|= _NEW_ARRAY
;
134 ctx
->Array
.NewState
|= VERT_BIT(attrib
);
139 _mesa_VertexPointer(GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
141 GLbitfield legalTypes
= (SHORT_BIT
| INT_BIT
| FLOAT_BIT
|
142 DOUBLE_BIT
| HALF_BIT
);
143 GET_CURRENT_CONTEXT(ctx
);
144 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
146 update_array(ctx
, "glVertexPointer", VERT_ATTRIB_POS
,
148 size
, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
153 _mesa_NormalPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
155 const GLbitfield legalTypes
= (BYTE_BIT
| SHORT_BIT
| INT_BIT
|
156 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
157 GET_CURRENT_CONTEXT(ctx
);
158 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
160 update_array(ctx
, "glNormalPointer", VERT_ATTRIB_NORMAL
,
162 3, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
167 _mesa_ColorPointer(GLint size
, GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
169 const GLbitfield legalTypes
= (BYTE_BIT
| UNSIGNED_BYTE_BIT
|
170 SHORT_BIT
| UNSIGNED_SHORT_BIT
|
171 INT_BIT
| UNSIGNED_INT_BIT
|
172 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
173 GET_CURRENT_CONTEXT(ctx
);
174 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
176 update_array(ctx
, "glColorPointer", VERT_ATTRIB_COLOR
,
178 size
, type
, stride
, GL_TRUE
, GL_FALSE
, ptr
);
183 _mesa_FogCoordPointerEXT(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
185 const GLbitfield legalTypes
= (HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
186 GET_CURRENT_CONTEXT(ctx
);
187 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
189 update_array(ctx
, "glFogCoordPointer", VERT_ATTRIB_FOG
,
191 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
196 _mesa_IndexPointer(GLenum type
, GLsizei stride
, const GLvoid
*ptr
)
198 const GLbitfield legalTypes
= (UNSIGNED_BYTE_BIT
| SHORT_BIT
| INT_BIT
|
199 FLOAT_BIT
| DOUBLE_BIT
);
200 GET_CURRENT_CONTEXT(ctx
);
201 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
203 update_array(ctx
, "glIndexPointer", VERT_ATTRIB_COLOR_INDEX
,
205 1, type
, stride
, GL_FALSE
, GL_FALSE
, ptr
);
210 _mesa_TexCoordPointer(GLint size
, GLenum type
, GLsizei stride
,
213 GLbitfield legalTypes
= (SHORT_BIT
| INT_BIT
|
214 HALF_BIT
| FLOAT_BIT
| DOUBLE_BIT
);
215 GET_CURRENT_CONTEXT(ctx
);
216 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
218 update_array(ctx
, "glTexCoordPointer", VERT_ATTRIB_TEX
,
220 size
, type
, stride
, GL_FALSE
, GL_FALSE
,
226 _mesa_EdgeFlagPointer(GLsizei stride
, const GLvoid
*ptr
)
228 const GLbitfield legalTypes
= UNSIGNED_BYTE_BIT
;
229 /* see table 2.4 edits in GL_EXT_gpu_shader4 spec: */
230 const GLboolean integer
= GL_TRUE
;
231 GET_CURRENT_CONTEXT(ctx
);
232 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
234 update_array(ctx
, "glEdgeFlagPointer", VERT_ATTRIB_EDGEFLAG
,
236 1, GL_UNSIGNED_BYTE
, stride
, GL_FALSE
, integer
, ptr
);
240 _mesa_VertexPointerEXT(GLint size
, GLenum type
, GLsizei stride
,
241 GLsizei count
, const GLvoid
*ptr
)
244 _mesa_VertexPointer(size
, type
, stride
, ptr
);
249 _mesa_NormalPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
,
253 _mesa_NormalPointer(type
, stride
, ptr
);
258 _mesa_ColorPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
,
262 _mesa_ColorPointer(size
, type
, stride
, ptr
);
267 _mesa_IndexPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
,
271 _mesa_IndexPointer(type
, stride
, ptr
);
276 _mesa_TexCoordPointerEXT(GLint size
, GLenum type
, GLsizei stride
,
277 GLsizei count
, const GLvoid
*ptr
)
280 _mesa_TexCoordPointer(size
, type
, stride
, ptr
);
285 _mesa_EdgeFlagPointerEXT(GLsizei stride
, GLsizei count
, const GLboolean
*ptr
)
288 _mesa_EdgeFlagPointer(stride
, ptr
);
293 _mesa_InterleavedArrays(GLenum format
, GLsizei stride
, const GLvoid
*pointer
)
295 GET_CURRENT_CONTEXT(ctx
);
296 GLboolean tflag
, cflag
, nflag
; /* enable/disable flags */
297 GLint tcomps
, ccomps
, vcomps
; /* components per texcoord, color, vertex */
298 GLenum ctype
= 0; /* color type */
299 GLint coffset
= 0, noffset
= 0, voffset
;/* color, normal, vertex offsets */
300 const GLint toffset
= 0; /* always zero */
301 GLint defstride
; /* default stride */
304 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
307 c
= f
* ((4 * sizeof(GLubyte
) + (f
- 1)) / f
);
310 _mesa_error( ctx
, GL_INVALID_VALUE
, "glInterleavedArrays(stride)" );
316 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
317 tcomps
= 0; ccomps
= 0; vcomps
= 2;
322 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
323 tcomps
= 0; ccomps
= 0; vcomps
= 3;
328 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
329 tcomps
= 0; ccomps
= 4; vcomps
= 2;
330 ctype
= GL_UNSIGNED_BYTE
;
336 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
337 tcomps
= 0; ccomps
= 4; vcomps
= 3;
338 ctype
= GL_UNSIGNED_BYTE
;
344 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
345 tcomps
= 0; ccomps
= 3; vcomps
= 3;
352 tflag
= GL_FALSE
; cflag
= GL_FALSE
; nflag
= GL_TRUE
;
353 tcomps
= 0; ccomps
= 0; vcomps
= 3;
359 tflag
= GL_FALSE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
360 tcomps
= 0; ccomps
= 4; vcomps
= 3;
368 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
369 tcomps
= 2; ccomps
= 0; vcomps
= 3;
374 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_FALSE
;
375 tcomps
= 4; ccomps
= 0; vcomps
= 4;
379 case GL_T2F_C4UB_V3F
:
380 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
381 tcomps
= 2; ccomps
= 4; vcomps
= 3;
382 ctype
= GL_UNSIGNED_BYTE
;
388 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_FALSE
;
389 tcomps
= 2; ccomps
= 3; vcomps
= 3;
396 tflag
= GL_TRUE
; cflag
= GL_FALSE
; nflag
= GL_TRUE
;
397 tcomps
= 2; ccomps
= 0; vcomps
= 3;
402 case GL_T2F_C4F_N3F_V3F
:
403 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
404 tcomps
= 2; ccomps
= 4; vcomps
= 3;
411 case GL_T4F_C4F_N3F_V4F
:
412 tflag
= GL_TRUE
; cflag
= GL_TRUE
; nflag
= GL_TRUE
;
413 tcomps
= 4; ccomps
= 4; vcomps
= 4;
421 _mesa_error( ctx
, GL_INVALID_ENUM
, "glInterleavedArrays(format)" );
429 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY
);
430 _mesa_DisableClientState( GL_INDEX_ARRAY
);
431 /* XXX also disable secondary color and generic arrays? */
435 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY
);
436 _mesa_TexCoordPointer( tcomps
, GL_FLOAT
, stride
,
437 (GLubyte
*) pointer
+ toffset
);
440 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY
);
445 _mesa_EnableClientState( GL_COLOR_ARRAY
);
446 _mesa_ColorPointer( ccomps
, ctype
, stride
,
447 (GLubyte
*) pointer
+ coffset
);
450 _mesa_DisableClientState( GL_COLOR_ARRAY
);
456 _mesa_EnableClientState( GL_NORMAL_ARRAY
);
457 _mesa_NormalPointer( GL_FLOAT
, stride
, (GLubyte
*) pointer
+ noffset
);
460 _mesa_DisableClientState( GL_NORMAL_ARRAY
);
464 _mesa_EnableClientState( GL_VERTEX_ARRAY
);
465 _mesa_VertexPointer( vcomps
, GL_FLOAT
, stride
,
466 (GLubyte
*) pointer
+ voffset
);
471 _mesa_LockArraysEXT(GLint first
, GLsizei count
)
473 GET_CURRENT_CONTEXT(ctx
);
474 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
476 if (MESA_VERBOSE
& VERBOSE_API
)
477 _mesa_debug(ctx
, "glLockArrays %d %d\n", first
, count
);
480 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLockArraysEXT(first)" );
484 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLockArraysEXT(count)" );
487 if (ctx
->Array
.LockCount
!= 0) {
488 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLockArraysEXT(reentry)" );
492 ctx
->Array
.LockFirst
= first
;
493 ctx
->Array
.LockCount
= count
;
495 ctx
->NewState
|= _NEW_ARRAY
;
496 ctx
->Array
.NewState
|= VERT_BIT_ALL
;
501 _mesa_UnlockArraysEXT( void )
503 GET_CURRENT_CONTEXT(ctx
);
504 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
506 if (MESA_VERBOSE
& VERBOSE_API
)
507 _mesa_debug(ctx
, "glUnlockArrays\n");
509 if (ctx
->Array
.LockCount
== 0) {
510 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glUnlockArraysEXT(reexit)" );
514 ctx
->Array
.LockFirst
= 0;
515 ctx
->Array
.LockCount
= 0;
516 ctx
->NewState
|= _NEW_ARRAY
;
517 ctx
->Array
.NewState
|= VERT_BIT_ALL
;
520 /* GL_IBM_multimode_draw_arrays */
522 _mesa_MultiModeDrawArraysIBM( const GLenum
* mode
, const GLint
* first
,
523 const GLsizei
* count
,
524 GLsizei primcount
, GLint modestride
)
526 GET_CURRENT_CONTEXT(ctx
);
529 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
531 for ( i
= 0 ; i
< primcount
; i
++ ) {
532 if ( count
[i
] > 0 ) {
533 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
534 CALL_DrawArrays(ctx
->Exec
, ( m
, first
[i
], count
[i
] ));
540 /* GL_IBM_multimode_draw_arrays */
542 _mesa_MultiModeDrawElementsIBM( const GLenum
* mode
, const GLsizei
* count
,
543 GLenum type
, const GLvoid
* const * indices
,
544 GLsizei primcount
, GLint modestride
)
546 GET_CURRENT_CONTEXT(ctx
);
549 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
551 /* XXX not sure about ARB_vertex_buffer_object handling here */
553 for ( i
= 0 ; i
< primcount
; i
++ ) {
554 if ( count
[i
] > 0 ) {
555 GLenum m
= *((GLenum
*) ((GLubyte
*) mode
+ i
* modestride
));
556 CALL_DrawElements(ctx
->Exec
, ( m
, count
[i
], type
, indices
[i
] ));
563 * Copy one client vertex array to another.
566 _mesa_copy_client_array(struct gl_context
*ctx
,
567 struct gl_client_array
*dst
,
568 struct gl_client_array
*src
)
570 dst
->Size
= src
->Size
;
571 dst
->Type
= src
->Type
;
572 dst
->Stride
= src
->Stride
;
573 dst
->StrideB
= src
->StrideB
;
575 dst
->Enabled
= src
->Enabled
;
576 dst
->Normalized
= src
->Normalized
;
577 dst
->Integer
= src
->Integer
;
578 dst
->_ElementSize
= src
->_ElementSize
;
579 _mesa_reference_buffer_object(ctx
, &dst
->BufferObj
, src
->BufferObj
);
580 dst
->_MaxElement
= src
->_MaxElement
;
584 init_array(struct gl_context
*ctx
,
585 struct gl_client_array
*array
, GLint size
, GLint type
)
592 array
->Enabled
= GL_FALSE
;
593 array
->Normalized
= GL_FALSE
;
594 array
->Integer
= GL_FALSE
;
595 array
->_ElementSize
= size
* _mesa_sizeof_type(type
);
596 /* Vertex array buffers */
597 _mesa_reference_buffer_object(ctx
, &array
->BufferObj
,
598 ctx
->Shared
->NullBufferObj
);
603 * Initialize vertex array state for given context.
606 _mesa_init_varray(struct gl_context
*ctx
, struct gl_array_attrib
*array
)
610 /* Init the individual arrays */
611 for (i
= 0; i
< Elements(array
->VertexAttrib
); i
++) {
613 case VERT_ATTRIB_WEIGHT
:
614 init_array(ctx
, &array
->VertexAttrib
[VERT_ATTRIB_WEIGHT
], 1, GL_FLOAT
);
616 case VERT_ATTRIB_NORMAL
:
617 init_array(ctx
, &array
->VertexAttrib
[VERT_ATTRIB_NORMAL
], 3, GL_FLOAT
);
619 case VERT_ATTRIB_FOG
:
620 init_array(ctx
, &array
->VertexAttrib
[VERT_ATTRIB_FOG
], 1, GL_FLOAT
);
622 case VERT_ATTRIB_COLOR_INDEX
:
623 init_array(ctx
, &array
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
], 1, GL_FLOAT
);
625 case VERT_ATTRIB_EDGEFLAG
:
626 init_array(ctx
, &array
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
], 1, GL_BOOL
);
629 init_array(ctx
, &array
->VertexAttrib
[i
], 4, GL_FLOAT
);
637 * Free vertex array state for given context.
640 _mesa_free_varray_data(struct gl_context
*ctx
, struct gl_array_attrib
* array
)
644 /* Uninit the individual arrays */
645 for (i
= 0; i
< Elements(array
->VertexAttrib
); i
++)
647 _mesa_reference_buffer_object(ctx
, &array
->VertexAttrib
[i
].BufferObj
, NULL
);
648 memset(&array
->VertexAttrib
[i
], 0, sizeof(struct gl_client_array
));