[[MESA]
[reactos.git] / reactos / dll / opengl / mesa / src / mesa / main / varray.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 */
25
26
27 #include "glheader.h"
28 #include "imports.h"
29 #include "bufferobj.h"
30 #include "context.h"
31 #include "enable.h"
32 #include "enums.h"
33 #include "hash.h"
34 #include "image.h"
35 #include "macros.h"
36 #include "mfeatures.h"
37 #include "mtypes.h"
38 #include "varray.h"
39 #include "arrayobj.h"
40 #include "main/dispatch.h"
41
42
43 /** Used to indicate which GL datatypes are accepted by each of the
44 * glVertex/Color/Attrib/EtcPointer() functions.
45 */
46 #define BOOL_BIT 0x1
47 #define BYTE_BIT 0x2
48 #define UNSIGNED_BYTE_BIT 0x4
49 #define SHORT_BIT 0x8
50 #define UNSIGNED_SHORT_BIT 0x10
51 #define INT_BIT 0x20
52 #define UNSIGNED_INT_BIT 0x40
53 #define HALF_BIT 0x80
54 #define FLOAT_BIT 0x100
55 #define DOUBLE_BIT 0x200
56 #define FIXED_ES_BIT 0x400
57 #define FIXED_GL_BIT 0x800
58
59
60 /** Convert GL datatype enum into a <type>_BIT value seen above */
61 static GLbitfield
62 type_to_bit(const struct gl_context *ctx, GLenum type)
63 {
64 switch (type) {
65 case GL_BOOL:
66 return BOOL_BIT;
67 case GL_BYTE:
68 return BYTE_BIT;
69 case GL_UNSIGNED_BYTE:
70 return UNSIGNED_BYTE_BIT;
71 case GL_SHORT:
72 return SHORT_BIT;
73 case GL_UNSIGNED_SHORT:
74 return UNSIGNED_SHORT_BIT;
75 case GL_INT:
76 return INT_BIT;
77 case GL_UNSIGNED_INT:
78 return UNSIGNED_INT_BIT;
79 case GL_HALF_FLOAT:
80 if (ctx->Extensions.ARB_half_float_vertex)
81 return HALF_BIT;
82 else
83 return 0x0;
84 case GL_FLOAT:
85 return FLOAT_BIT;
86 case GL_DOUBLE:
87 return DOUBLE_BIT;
88 case GL_FIXED:
89 return ctx->API == API_OPENGL ? FIXED_GL_BIT : FIXED_ES_BIT;
90 default:
91 return 0;
92 }
93 }
94
95
96 /**
97 * Do error checking and update state for glVertex/Color/TexCoord/...Pointer
98 * functions.
99 *
100 * \param func name of calling function used for error reporting
101 * \param attrib the attribute array index to update
102 * \param legalTypes bitmask of *_BIT above indicating legal datatypes
103 * \param sizeMin min allowable size value
104 * \param sizeMax max allowable size value
105 * \param size components per element (1, 2, 3 or 4)
106 * \param type datatype of each component (GL_FLOAT, GL_INT, etc)
107 * \param stride stride between elements, in elements
108 * \param normalized are integer types converted to floats in [-1, 1]?
109 * \param integer integer-valued values (will not be normalized to [-1,1])
110 * \param ptr the address (or offset inside VBO) of the array data
111 */
112 static void
113 update_array(struct gl_context *ctx,
114 const char *func,
115 GLuint attrib, GLbitfield legalTypesMask,
116 GLint sizeMin, GLint sizeMax,
117 GLint size, GLenum type, GLsizei stride,
118 GLboolean normalized, GLboolean integer,
119 const GLvoid *ptr)
120 {
121 struct gl_client_array *array;
122 GLbitfield typeBit;
123 GLsizei elementSize;
124
125 if (ctx->API != API_OPENGLES && ctx->API != API_OPENGLES2) {
126 /* fixed point arrays / data is only allowed with OpenGL ES 1.x/2.0 */
127 legalTypesMask &= ~FIXED_ES_BIT;
128 }
129 if (!ctx->Extensions.ARB_ES2_compatibility) {
130 legalTypesMask &= ~FIXED_GL_BIT;
131 }
132
133 typeBit = type_to_bit(ctx, type);
134 if (typeBit == 0x0 || (typeBit & legalTypesMask) == 0x0) {
135 _mesa_error(ctx, GL_INVALID_ENUM, "%s(type = %s)",
136 func, _mesa_lookup_enum_by_nr(type));
137 return;
138 }
139
140 /* Do size parameter checking. */
141 if (size < sizeMin || size > sizeMax || size > 4) {
142 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d)", func, size);
143 return;
144 }
145
146 ASSERT(size <= 4);
147
148 if (stride < 0) {
149 _mesa_error( ctx, GL_INVALID_VALUE, "%s(stride=%d)", func, stride );
150 return;
151 }
152
153 if (ctx->Array.ArrayObj->ARBsemantics &&
154 !_mesa_is_bufferobj(ctx->Array.ArrayBufferObj)) {
155 /* GL_ARB_vertex_array_object requires that all arrays reside in VBOs.
156 * Generate GL_INVALID_OPERATION if that's not true.
157 */
158 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-VBO array)", func);
159 return;
160 }
161
162 elementSize = _mesa_sizeof_type(type) * size;
163
164 array = &ctx->Array.ArrayObj->VertexAttrib[attrib];
165 array->Size = size;
166 array->Type = type;
167 array->Stride = stride;
168 array->StrideB = stride ? stride : elementSize;
169 array->Normalized = normalized;
170 array->Integer = integer;
171 array->Ptr = (const GLubyte *) ptr;
172 array->_ElementSize = elementSize;
173
174 _mesa_reference_buffer_object(ctx, &array->BufferObj,
175 ctx->Array.ArrayBufferObj);
176
177 ctx->NewState |= _NEW_ARRAY;
178 ctx->Array.NewState |= VERT_BIT(attrib);
179 }
180
181
182 void GLAPIENTRY
183 _mesa_VertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
184 {
185 GLbitfield legalTypes = (SHORT_BIT | INT_BIT | FLOAT_BIT |
186 DOUBLE_BIT | HALF_BIT | FIXED_ES_BIT);
187 GET_CURRENT_CONTEXT(ctx);
188 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
189
190 if (ctx->API == API_OPENGLES)
191 legalTypes |= BYTE_BIT;
192
193 update_array(ctx, "glVertexPointer", VERT_ATTRIB_POS,
194 legalTypes, 2, 4,
195 size, type, stride, GL_FALSE, GL_FALSE, ptr);
196 }
197
198
199 void GLAPIENTRY
200 _mesa_NormalPointer(GLenum type, GLsizei stride, const GLvoid *ptr )
201 {
202 const GLbitfield legalTypes = (BYTE_BIT | SHORT_BIT | INT_BIT |
203 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
204 FIXED_ES_BIT);
205 GET_CURRENT_CONTEXT(ctx);
206 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
207
208 update_array(ctx, "glNormalPointer", VERT_ATTRIB_NORMAL,
209 legalTypes, 3, 3,
210 3, type, stride, GL_TRUE, GL_FALSE, ptr);
211 }
212
213
214 void GLAPIENTRY
215 _mesa_ColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
216 {
217 const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
218 SHORT_BIT | UNSIGNED_SHORT_BIT |
219 INT_BIT | UNSIGNED_INT_BIT |
220 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
221 FIXED_ES_BIT);
222 GET_CURRENT_CONTEXT(ctx);
223 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
224
225 update_array(ctx, "glColorPointer", VERT_ATTRIB_COLOR0,
226 legalTypes, 3, 4,
227 size, type, stride, GL_TRUE, GL_FALSE, ptr);
228 }
229
230
231 void GLAPIENTRY
232 _mesa_FogCoordPointerEXT(GLenum type, GLsizei stride, const GLvoid *ptr)
233 {
234 const GLbitfield legalTypes = (HALF_BIT | FLOAT_BIT | DOUBLE_BIT);
235 GET_CURRENT_CONTEXT(ctx);
236 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
237
238 update_array(ctx, "glFogCoordPointer", VERT_ATTRIB_FOG,
239 legalTypes, 1, 1,
240 1, type, stride, GL_FALSE, GL_FALSE, ptr);
241 }
242
243
244 void GLAPIENTRY
245 _mesa_IndexPointer(GLenum type, GLsizei stride, const GLvoid *ptr)
246 {
247 const GLbitfield legalTypes = (UNSIGNED_BYTE_BIT | SHORT_BIT | INT_BIT |
248 FLOAT_BIT | DOUBLE_BIT);
249 GET_CURRENT_CONTEXT(ctx);
250 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
251
252 update_array(ctx, "glIndexPointer", VERT_ATTRIB_COLOR_INDEX,
253 legalTypes, 1, 1,
254 1, type, stride, GL_FALSE, GL_FALSE, ptr);
255 }
256
257
258 void GLAPIENTRY
259 _mesa_SecondaryColorPointerEXT(GLint size, GLenum type,
260 GLsizei stride, const GLvoid *ptr)
261 {
262 const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
263 SHORT_BIT | UNSIGNED_SHORT_BIT |
264 INT_BIT | UNSIGNED_INT_BIT |
265 HALF_BIT | FLOAT_BIT | DOUBLE_BIT);
266 GET_CURRENT_CONTEXT(ctx);
267 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
268
269 update_array(ctx, "glSecondaryColorPointer", VERT_ATTRIB_COLOR1,
270 legalTypes, 3, 4,
271 size, type, stride, GL_TRUE, GL_FALSE, ptr);
272 }
273
274
275 void GLAPIENTRY
276 _mesa_TexCoordPointer(GLint size, GLenum type, GLsizei stride,
277 const GLvoid *ptr)
278 {
279 GLbitfield legalTypes = (SHORT_BIT | INT_BIT |
280 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
281 FIXED_ES_BIT);
282 GET_CURRENT_CONTEXT(ctx);
283 const GLuint unit = ctx->Array.ActiveTexture;
284 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
285
286 if (ctx->API == API_OPENGLES)
287 legalTypes |= BYTE_BIT;
288
289 update_array(ctx, "glTexCoordPointer", VERT_ATTRIB_TEX(unit),
290 legalTypes, 1, 4,
291 size, type, stride, GL_FALSE, GL_FALSE,
292 ptr);
293 }
294
295
296 void GLAPIENTRY
297 _mesa_EdgeFlagPointer(GLsizei stride, const GLvoid *ptr)
298 {
299 const GLbitfield legalTypes = UNSIGNED_BYTE_BIT;
300 /* see table 2.4 edits in GL_EXT_gpu_shader4 spec: */
301 const GLboolean integer = GL_TRUE;
302 GET_CURRENT_CONTEXT(ctx);
303 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
304
305 update_array(ctx, "glEdgeFlagPointer", VERT_ATTRIB_EDGEFLAG,
306 legalTypes, 1, 1,
307 1, GL_UNSIGNED_BYTE, stride, GL_FALSE, integer, ptr);
308 }
309
310
311 void GLAPIENTRY
312 _mesa_PointSizePointer(GLenum type, GLsizei stride, const GLvoid *ptr)
313 {
314 const GLbitfield legalTypes = (FLOAT_BIT | FIXED_ES_BIT);
315 GET_CURRENT_CONTEXT(ctx);
316 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
317
318 if (ctx->API != API_OPENGLES) {
319 _mesa_error(ctx, GL_INVALID_OPERATION,
320 "glPointSizePointer(ES 1.x only)");
321 return;
322 }
323
324 update_array(ctx, "glPointSizePointer", VERT_ATTRIB_POINT_SIZE,
325 legalTypes, 1, 1,
326 1, type, stride, GL_FALSE, GL_FALSE, ptr);
327 }
328
329
330 #if FEATURE_NV_vertex_program
331 /**
332 * Set a vertex attribute array.
333 * Note that these arrays DO alias the conventional GL vertex arrays
334 * (position, normal, color, fog, texcoord, etc).
335 * The generic attribute slots at #16 and above are not touched.
336 */
337 void GLAPIENTRY
338 _mesa_VertexAttribPointerNV(GLuint index, GLint size, GLenum type,
339 GLsizei stride, const GLvoid *ptr)
340 {
341 const GLbitfield legalTypes = (UNSIGNED_BYTE_BIT | SHORT_BIT |
342 FLOAT_BIT | DOUBLE_BIT);
343 GLboolean normalized = GL_FALSE;
344 GET_CURRENT_CONTEXT(ctx);
345 ASSERT_OUTSIDE_BEGIN_END(ctx);
346
347 if (index >= MAX_NV_VERTEX_PROGRAM_INPUTS) {
348 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribPointerNV(index)");
349 return;
350 }
351
352 if (type == GL_UNSIGNED_BYTE && size != 4) {
353 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribPointerNV(size!=4)");
354 return;
355 }
356
357 update_array(ctx, "glVertexAttribPointerNV", VERT_ATTRIB_GENERIC(index),
358 legalTypes, 1, 4,
359 size, type, stride, normalized, GL_FALSE, ptr);
360 }
361 #endif
362
363
364 #if FEATURE_ARB_vertex_program
365 /**
366 * Set a generic vertex attribute array.
367 * Note that these arrays DO NOT alias the conventional GL vertex arrays
368 * (position, normal, color, fog, texcoord, etc).
369 */
370 void GLAPIENTRY
371 _mesa_VertexAttribPointerARB(GLuint index, GLint size, GLenum type,
372 GLboolean normalized,
373 GLsizei stride, const GLvoid *ptr)
374 {
375 const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
376 SHORT_BIT | UNSIGNED_SHORT_BIT |
377 INT_BIT | UNSIGNED_INT_BIT |
378 HALF_BIT | FLOAT_BIT | DOUBLE_BIT |
379 FIXED_ES_BIT | FIXED_GL_BIT);
380 GET_CURRENT_CONTEXT(ctx);
381 ASSERT_OUTSIDE_BEGIN_END(ctx);
382
383 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
384 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribPointerARB(index)");
385 return;
386 }
387
388 update_array(ctx, "glVertexAttribPointer", VERT_ATTRIB_GENERIC(index),
389 legalTypes, 1, 4,
390 size, type, stride, normalized, GL_FALSE, ptr);
391 }
392 #endif
393
394
395 /**
396 * GL_EXT_gpu_shader4 / GL 3.0.
397 * Set an integer-valued vertex attribute array.
398 * Note that these arrays DO NOT alias the conventional GL vertex arrays
399 * (position, normal, color, fog, texcoord, etc).
400 */
401 void GLAPIENTRY
402 _mesa_VertexAttribIPointer(GLuint index, GLint size, GLenum type,
403 GLsizei stride, const GLvoid *ptr)
404 {
405 const GLbitfield legalTypes = (BYTE_BIT | UNSIGNED_BYTE_BIT |
406 SHORT_BIT | UNSIGNED_SHORT_BIT |
407 INT_BIT | UNSIGNED_INT_BIT);
408 const GLboolean normalized = GL_FALSE;
409 const GLboolean integer = GL_TRUE;
410 GET_CURRENT_CONTEXT(ctx);
411 ASSERT_OUTSIDE_BEGIN_END(ctx);
412
413 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
414 _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribIPointer(index)");
415 return;
416 }
417
418 update_array(ctx, "glVertexAttribIPointer", VERT_ATTRIB_GENERIC(index),
419 legalTypes, 1, 4,
420 size, type, stride, normalized, integer, ptr);
421 }
422
423
424
425 void GLAPIENTRY
426 _mesa_EnableVertexAttribArrayARB(GLuint index)
427 {
428 GET_CURRENT_CONTEXT(ctx);
429 ASSERT_OUTSIDE_BEGIN_END(ctx);
430
431 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
432 _mesa_error(ctx, GL_INVALID_VALUE,
433 "glEnableVertexAttribArrayARB(index)");
434 return;
435 }
436
437 ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
438
439 FLUSH_VERTICES(ctx, _NEW_ARRAY);
440 ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled = GL_TRUE;
441 ctx->Array.ArrayObj->_Enabled |= VERT_BIT_GENERIC(index);
442 ctx->Array.NewState |= VERT_BIT_GENERIC(index);
443 }
444
445
446 void GLAPIENTRY
447 _mesa_DisableVertexAttribArrayARB(GLuint index)
448 {
449 GET_CURRENT_CONTEXT(ctx);
450 ASSERT_OUTSIDE_BEGIN_END(ctx);
451
452 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
453 _mesa_error(ctx, GL_INVALID_VALUE,
454 "glDisableVertexAttribArrayARB(index)");
455 return;
456 }
457
458 ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
459
460 FLUSH_VERTICES(ctx, _NEW_ARRAY);
461 ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Enabled = GL_FALSE;
462 ctx->Array.ArrayObj->_Enabled &= ~VERT_BIT_GENERIC(index);
463 ctx->Array.NewState |= VERT_BIT_GENERIC(index);
464 }
465
466
467 /**
468 * Return info for a vertex attribute array (no alias with legacy
469 * vertex attributes (pos, normal, color, etc)). This function does
470 * not handle the 4-element GL_CURRENT_VERTEX_ATTRIB_ARB query.
471 */
472 static GLuint
473 get_vertex_array_attrib(struct gl_context *ctx, GLuint index, GLenum pname,
474 const char *caller)
475 {
476 const struct gl_client_array *array;
477
478 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
479 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%u)", caller, index);
480 return 0;
481 }
482
483 ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
484
485 array = &ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)];
486
487 switch (pname) {
488 case GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB:
489 return array->Enabled;
490 case GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB:
491 return array->Size;
492 case GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB:
493 return array->Stride;
494 case GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB:
495 return array->Type;
496 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB:
497 return array->Normalized;
498 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB:
499 return array->BufferObj->Name;
500 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
501 if (ctx->VersionMajor >= 3 || ctx->Extensions.EXT_gpu_shader4) {
502 return array->Integer;
503 }
504 goto error;
505 default:
506 ; /* fall-through */
507 }
508
509 error:
510 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", caller, pname);
511 return 0;
512 }
513
514
515 static const GLfloat *
516 get_current_attrib(struct gl_context *ctx, GLuint index, const char *function)
517 {
518 if (index == 0) {
519 if (ctx->API != API_OPENGLES2) {
520 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(index==0)", function);
521 return NULL;
522 }
523 }
524 else if (index >= ctx->Const.VertexProgram.MaxAttribs) {
525 _mesa_error(ctx, GL_INVALID_VALUE,
526 "%s(index>=GL_MAX_VERTEX_ATTRIBS)", function);
527 return NULL;
528 }
529
530 ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
531
532 FLUSH_CURRENT(ctx, 0);
533 return ctx->Current.Attrib[VERT_ATTRIB_GENERIC(index)];
534 }
535
536 void GLAPIENTRY
537 _mesa_GetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
538 {
539 GET_CURRENT_CONTEXT(ctx);
540 ASSERT_OUTSIDE_BEGIN_END(ctx);
541
542 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
543 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribfv");
544 if (v != NULL) {
545 COPY_4V(params, v);
546 }
547 }
548 else {
549 params[0] = (GLfloat) get_vertex_array_attrib(ctx, index, pname,
550 "glGetVertexAttribfv");
551 }
552 }
553
554
555 void GLAPIENTRY
556 _mesa_GetVertexAttribdvARB(GLuint index, GLenum pname, GLdouble *params)
557 {
558 GET_CURRENT_CONTEXT(ctx);
559 ASSERT_OUTSIDE_BEGIN_END(ctx);
560
561 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
562 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribdv");
563 if (v != NULL) {
564 params[0] = (GLdouble) v[0];
565 params[1] = (GLdouble) v[1];
566 params[2] = (GLdouble) v[2];
567 params[3] = (GLdouble) v[3];
568 }
569 }
570 else {
571 params[0] = (GLdouble) get_vertex_array_attrib(ctx, index, pname,
572 "glGetVertexAttribdv");
573 }
574 }
575
576
577 void GLAPIENTRY
578 _mesa_GetVertexAttribivARB(GLuint index, GLenum pname, GLint *params)
579 {
580 GET_CURRENT_CONTEXT(ctx);
581 ASSERT_OUTSIDE_BEGIN_END(ctx);
582
583 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
584 const GLfloat *v = get_current_attrib(ctx, index, "glGetVertexAttribiv");
585 if (v != NULL) {
586 /* XXX should floats in[0,1] be scaled to full int range? */
587 params[0] = (GLint) v[0];
588 params[1] = (GLint) v[1];
589 params[2] = (GLint) v[2];
590 params[3] = (GLint) v[3];
591 }
592 }
593 else {
594 params[0] = (GLint) get_vertex_array_attrib(ctx, index, pname,
595 "glGetVertexAttribiv");
596 }
597 }
598
599
600 /** GL 3.0 */
601 void GLAPIENTRY
602 _mesa_GetVertexAttribIiv(GLuint index, GLenum pname, GLint *params)
603 {
604 GET_CURRENT_CONTEXT(ctx);
605 ASSERT_OUTSIDE_BEGIN_END(ctx);
606
607 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
608 const GLfloat *v =
609 get_current_attrib(ctx, index, "glGetVertexAttribIiv");
610 if (v != NULL) {
611 /* XXX we don't have true integer-valued vertex attribs yet */
612 params[0] = (GLint) v[0];
613 params[1] = (GLint) v[1];
614 params[2] = (GLint) v[2];
615 params[3] = (GLint) v[3];
616 }
617 }
618 else {
619 params[0] = (GLint) get_vertex_array_attrib(ctx, index, pname,
620 "glGetVertexAttribIiv");
621 }
622 }
623
624
625 /** GL 3.0 */
626 void GLAPIENTRY
627 _mesa_GetVertexAttribIuiv(GLuint index, GLenum pname, GLuint *params)
628 {
629 GET_CURRENT_CONTEXT(ctx);
630 ASSERT_OUTSIDE_BEGIN_END(ctx);
631
632 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
633 const GLfloat *v =
634 get_current_attrib(ctx, index, "glGetVertexAttribIuiv");
635 if (v != NULL) {
636 /* XXX we don't have true integer-valued vertex attribs yet */
637 params[0] = (GLuint) v[0];
638 params[1] = (GLuint) v[1];
639 params[2] = (GLuint) v[2];
640 params[3] = (GLuint) v[3];
641 }
642 }
643 else {
644 params[0] = get_vertex_array_attrib(ctx, index, pname,
645 "glGetVertexAttribIuiv");
646 }
647 }
648
649
650 void GLAPIENTRY
651 _mesa_GetVertexAttribPointervARB(GLuint index, GLenum pname, GLvoid **pointer)
652 {
653 GET_CURRENT_CONTEXT(ctx);
654 ASSERT_OUTSIDE_BEGIN_END(ctx);
655
656 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
657 _mesa_error(ctx, GL_INVALID_VALUE, "glGetVertexAttribPointerARB(index)");
658 return;
659 }
660
661 if (pname != GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB) {
662 _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexAttribPointerARB(pname)");
663 return;
664 }
665
666 ASSERT(VERT_ATTRIB_GENERIC(index) < Elements(ctx->Array.ArrayObj->VertexAttrib));
667
668 *pointer = (GLvoid *) ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(index)].Ptr;
669 }
670
671
672 void GLAPIENTRY
673 _mesa_VertexPointerEXT(GLint size, GLenum type, GLsizei stride,
674 GLsizei count, const GLvoid *ptr)
675 {
676 (void) count;
677 _mesa_VertexPointer(size, type, stride, ptr);
678 }
679
680
681 void GLAPIENTRY
682 _mesa_NormalPointerEXT(GLenum type, GLsizei stride, GLsizei count,
683 const GLvoid *ptr)
684 {
685 (void) count;
686 _mesa_NormalPointer(type, stride, ptr);
687 }
688
689
690 void GLAPIENTRY
691 _mesa_ColorPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count,
692 const GLvoid *ptr)
693 {
694 (void) count;
695 _mesa_ColorPointer(size, type, stride, ptr);
696 }
697
698
699 void GLAPIENTRY
700 _mesa_IndexPointerEXT(GLenum type, GLsizei stride, GLsizei count,
701 const GLvoid *ptr)
702 {
703 (void) count;
704 _mesa_IndexPointer(type, stride, ptr);
705 }
706
707
708 void GLAPIENTRY
709 _mesa_TexCoordPointerEXT(GLint size, GLenum type, GLsizei stride,
710 GLsizei count, const GLvoid *ptr)
711 {
712 (void) count;
713 _mesa_TexCoordPointer(size, type, stride, ptr);
714 }
715
716
717 void GLAPIENTRY
718 _mesa_EdgeFlagPointerEXT(GLsizei stride, GLsizei count, const GLboolean *ptr)
719 {
720 (void) count;
721 _mesa_EdgeFlagPointer(stride, ptr);
722 }
723
724
725 void GLAPIENTRY
726 _mesa_InterleavedArrays(GLenum format, GLsizei stride, const GLvoid *pointer)
727 {
728 GET_CURRENT_CONTEXT(ctx);
729 GLboolean tflag, cflag, nflag; /* enable/disable flags */
730 GLint tcomps, ccomps, vcomps; /* components per texcoord, color, vertex */
731 GLenum ctype = 0; /* color type */
732 GLint coffset = 0, noffset = 0, voffset;/* color, normal, vertex offsets */
733 const GLint toffset = 0; /* always zero */
734 GLint defstride; /* default stride */
735 GLint c, f;
736
737 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
738
739 f = sizeof(GLfloat);
740 c = f * ((4 * sizeof(GLubyte) + (f - 1)) / f);
741
742 if (stride < 0) {
743 _mesa_error( ctx, GL_INVALID_VALUE, "glInterleavedArrays(stride)" );
744 return;
745 }
746
747 switch (format) {
748 case GL_V2F:
749 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
750 tcomps = 0; ccomps = 0; vcomps = 2;
751 voffset = 0;
752 defstride = 2*f;
753 break;
754 case GL_V3F:
755 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
756 tcomps = 0; ccomps = 0; vcomps = 3;
757 voffset = 0;
758 defstride = 3*f;
759 break;
760 case GL_C4UB_V2F:
761 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
762 tcomps = 0; ccomps = 4; vcomps = 2;
763 ctype = GL_UNSIGNED_BYTE;
764 coffset = 0;
765 voffset = c;
766 defstride = c + 2*f;
767 break;
768 case GL_C4UB_V3F:
769 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
770 tcomps = 0; ccomps = 4; vcomps = 3;
771 ctype = GL_UNSIGNED_BYTE;
772 coffset = 0;
773 voffset = c;
774 defstride = c + 3*f;
775 break;
776 case GL_C3F_V3F:
777 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
778 tcomps = 0; ccomps = 3; vcomps = 3;
779 ctype = GL_FLOAT;
780 coffset = 0;
781 voffset = 3*f;
782 defstride = 6*f;
783 break;
784 case GL_N3F_V3F:
785 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_TRUE;
786 tcomps = 0; ccomps = 0; vcomps = 3;
787 noffset = 0;
788 voffset = 3*f;
789 defstride = 6*f;
790 break;
791 case GL_C4F_N3F_V3F:
792 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_TRUE;
793 tcomps = 0; ccomps = 4; vcomps = 3;
794 ctype = GL_FLOAT;
795 coffset = 0;
796 noffset = 4*f;
797 voffset = 7*f;
798 defstride = 10*f;
799 break;
800 case GL_T2F_V3F:
801 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
802 tcomps = 2; ccomps = 0; vcomps = 3;
803 voffset = 2*f;
804 defstride = 5*f;
805 break;
806 case GL_T4F_V4F:
807 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
808 tcomps = 4; ccomps = 0; vcomps = 4;
809 voffset = 4*f;
810 defstride = 8*f;
811 break;
812 case GL_T2F_C4UB_V3F:
813 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
814 tcomps = 2; ccomps = 4; vcomps = 3;
815 ctype = GL_UNSIGNED_BYTE;
816 coffset = 2*f;
817 voffset = c+2*f;
818 defstride = c+5*f;
819 break;
820 case GL_T2F_C3F_V3F:
821 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
822 tcomps = 2; ccomps = 3; vcomps = 3;
823 ctype = GL_FLOAT;
824 coffset = 2*f;
825 voffset = 5*f;
826 defstride = 8*f;
827 break;
828 case GL_T2F_N3F_V3F:
829 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_TRUE;
830 tcomps = 2; ccomps = 0; vcomps = 3;
831 noffset = 2*f;
832 voffset = 5*f;
833 defstride = 8*f;
834 break;
835 case GL_T2F_C4F_N3F_V3F:
836 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
837 tcomps = 2; ccomps = 4; vcomps = 3;
838 ctype = GL_FLOAT;
839 coffset = 2*f;
840 noffset = 6*f;
841 voffset = 9*f;
842 defstride = 12*f;
843 break;
844 case GL_T4F_C4F_N3F_V4F:
845 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
846 tcomps = 4; ccomps = 4; vcomps = 4;
847 ctype = GL_FLOAT;
848 coffset = 4*f;
849 noffset = 8*f;
850 voffset = 11*f;
851 defstride = 15*f;
852 break;
853 default:
854 _mesa_error( ctx, GL_INVALID_ENUM, "glInterleavedArrays(format)" );
855 return;
856 }
857
858 if (stride==0) {
859 stride = defstride;
860 }
861
862 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY );
863 _mesa_DisableClientState( GL_INDEX_ARRAY );
864 /* XXX also disable secondary color and generic arrays? */
865
866 /* Texcoords */
867 if (tflag) {
868 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY );
869 _mesa_TexCoordPointer( tcomps, GL_FLOAT, stride,
870 (GLubyte *) pointer + toffset );
871 }
872 else {
873 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
874 }
875
876 /* Color */
877 if (cflag) {
878 _mesa_EnableClientState( GL_COLOR_ARRAY );
879 _mesa_ColorPointer( ccomps, ctype, stride,
880 (GLubyte *) pointer + coffset );
881 }
882 else {
883 _mesa_DisableClientState( GL_COLOR_ARRAY );
884 }
885
886
887 /* Normals */
888 if (nflag) {
889 _mesa_EnableClientState( GL_NORMAL_ARRAY );
890 _mesa_NormalPointer( GL_FLOAT, stride, (GLubyte *) pointer + noffset );
891 }
892 else {
893 _mesa_DisableClientState( GL_NORMAL_ARRAY );
894 }
895
896 /* Vertices */
897 _mesa_EnableClientState( GL_VERTEX_ARRAY );
898 _mesa_VertexPointer( vcomps, GL_FLOAT, stride,
899 (GLubyte *) pointer + voffset );
900 }
901
902
903 void GLAPIENTRY
904 _mesa_LockArraysEXT(GLint first, GLsizei count)
905 {
906 GET_CURRENT_CONTEXT(ctx);
907 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
908
909 if (MESA_VERBOSE & VERBOSE_API)
910 _mesa_debug(ctx, "glLockArrays %d %d\n", first, count);
911
912 if (first < 0) {
913 _mesa_error( ctx, GL_INVALID_VALUE, "glLockArraysEXT(first)" );
914 return;
915 }
916 if (count <= 0) {
917 _mesa_error( ctx, GL_INVALID_VALUE, "glLockArraysEXT(count)" );
918 return;
919 }
920 if (ctx->Array.LockCount != 0) {
921 _mesa_error( ctx, GL_INVALID_OPERATION, "glLockArraysEXT(reentry)" );
922 return;
923 }
924
925 ctx->Array.LockFirst = first;
926 ctx->Array.LockCount = count;
927
928 ctx->NewState |= _NEW_ARRAY;
929 ctx->Array.NewState |= VERT_BIT_ALL;
930 }
931
932
933 void GLAPIENTRY
934 _mesa_UnlockArraysEXT( void )
935 {
936 GET_CURRENT_CONTEXT(ctx);
937 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
938
939 if (MESA_VERBOSE & VERBOSE_API)
940 _mesa_debug(ctx, "glUnlockArrays\n");
941
942 if (ctx->Array.LockCount == 0) {
943 _mesa_error( ctx, GL_INVALID_OPERATION, "glUnlockArraysEXT(reexit)" );
944 return;
945 }
946
947 ctx->Array.LockFirst = 0;
948 ctx->Array.LockCount = 0;
949 ctx->NewState |= _NEW_ARRAY;
950 ctx->Array.NewState |= VERT_BIT_ALL;
951 }
952
953
954 /* GL_EXT_multi_draw_arrays */
955 void GLAPIENTRY
956 _mesa_MultiDrawArraysEXT( GLenum mode, const GLint *first,
957 const GLsizei *count, GLsizei primcount )
958 {
959 GET_CURRENT_CONTEXT(ctx);
960 GLint i;
961
962 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
963
964 for (i = 0; i < primcount; i++) {
965 if (count[i] > 0) {
966 CALL_DrawArrays(ctx->Exec, (mode, first[i], count[i]));
967 }
968 }
969 }
970
971
972 /* GL_IBM_multimode_draw_arrays */
973 void GLAPIENTRY
974 _mesa_MultiModeDrawArraysIBM( const GLenum * mode, const GLint * first,
975 const GLsizei * count,
976 GLsizei primcount, GLint modestride )
977 {
978 GET_CURRENT_CONTEXT(ctx);
979 GLint i;
980
981 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
982
983 for ( i = 0 ; i < primcount ; i++ ) {
984 if ( count[i] > 0 ) {
985 GLenum m = *((GLenum *) ((GLubyte *) mode + i * modestride));
986 CALL_DrawArrays(ctx->Exec, ( m, first[i], count[i] ));
987 }
988 }
989 }
990
991
992 /* GL_IBM_multimode_draw_arrays */
993 void GLAPIENTRY
994 _mesa_MultiModeDrawElementsIBM( const GLenum * mode, const GLsizei * count,
995 GLenum type, const GLvoid * const * indices,
996 GLsizei primcount, GLint modestride )
997 {
998 GET_CURRENT_CONTEXT(ctx);
999 GLint i;
1000
1001 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1002
1003 /* XXX not sure about ARB_vertex_buffer_object handling here */
1004
1005 for ( i = 0 ; i < primcount ; i++ ) {
1006 if ( count[i] > 0 ) {
1007 GLenum m = *((GLenum *) ((GLubyte *) mode + i * modestride));
1008 CALL_DrawElements(ctx->Exec, ( m, count[i], type, indices[i] ));
1009 }
1010 }
1011 }
1012
1013
1014 /**
1015 * GL_NV_primitive_restart and GL 3.1
1016 */
1017 void GLAPIENTRY
1018 _mesa_PrimitiveRestartIndex(GLuint index)
1019 {
1020 GET_CURRENT_CONTEXT(ctx);
1021
1022 if (!ctx->Extensions.NV_primitive_restart &&
1023 ctx->VersionMajor * 10 + ctx->VersionMinor < 31) {
1024 _mesa_error(ctx, GL_INVALID_OPERATION, "glPrimitiveRestartIndexNV()");
1025 return;
1026 }
1027
1028 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1029
1030 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
1031
1032 ctx->Array.RestartIndex = index;
1033 }
1034
1035
1036
1037 /**
1038 * Copy one client vertex array to another.
1039 */
1040 void
1041 _mesa_copy_client_array(struct gl_context *ctx,
1042 struct gl_client_array *dst,
1043 struct gl_client_array *src)
1044 {
1045 dst->Size = src->Size;
1046 dst->Type = src->Type;
1047 dst->Stride = src->Stride;
1048 dst->StrideB = src->StrideB;
1049 dst->Ptr = src->Ptr;
1050 dst->Enabled = src->Enabled;
1051 dst->Normalized = src->Normalized;
1052 dst->Integer = src->Integer;
1053 dst->_ElementSize = src->_ElementSize;
1054 _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
1055 dst->_MaxElement = src->_MaxElement;
1056 }
1057
1058
1059
1060 /**
1061 * Print vertex array's fields.
1062 */
1063 static void
1064 print_array(const char *name, GLint index, const struct gl_client_array *array)
1065 {
1066 if (index >= 0)
1067 printf(" %s[%d]: ", name, index);
1068 else
1069 printf(" %s: ", name);
1070 printf("Ptr=%p, Type=0x%x, Size=%d, ElemSize=%u, Stride=%d, Buffer=%u(Size %lu), MaxElem=%u\n",
1071 array->Ptr, array->Type, array->Size,
1072 array->_ElementSize, array->StrideB,
1073 array->BufferObj->Name, (unsigned long) array->BufferObj->Size,
1074 array->_MaxElement);
1075 }
1076
1077
1078 /**
1079 * Print current vertex object/array info. For debug.
1080 */
1081 void
1082 _mesa_print_arrays(struct gl_context *ctx)
1083 {
1084 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
1085 GLuint i;
1086
1087 _mesa_update_array_object_max_element(ctx, arrayObj);
1088
1089 printf("Array Object %u\n", arrayObj->Name);
1090 if (arrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled)
1091 print_array("Vertex", -1, &arrayObj->VertexAttrib[VERT_ATTRIB_POS]);
1092 if (arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL].Enabled)
1093 print_array("Normal", -1, &arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL]);
1094 if (arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0].Enabled)
1095 print_array("Color", -1, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0]);
1096 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++)
1097 if (arrayObj->VertexAttrib[VERT_ATTRIB_TEX(i)].Enabled)
1098 print_array("TexCoord", i, &arrayObj->VertexAttrib[VERT_ATTRIB_TEX(i)]);
1099 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++)
1100 if (arrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(i)].Enabled)
1101 print_array("Attrib", i, &arrayObj->VertexAttrib[VERT_ATTRIB_GENERIC(i)]);
1102 printf(" _MaxElement = %u\n", arrayObj->_MaxElement);
1103 }
1104
1105
1106 /**
1107 * Initialize vertex array state for given context.
1108 */
1109 void
1110 _mesa_init_varray(struct gl_context *ctx)
1111 {
1112 ctx->Array.DefaultArrayObj = _mesa_new_array_object(ctx, 0);
1113 _mesa_reference_array_object(ctx, &ctx->Array.ArrayObj,
1114 ctx->Array.DefaultArrayObj);
1115 ctx->Array.ActiveTexture = 0; /* GL_ARB_multitexture */
1116
1117 ctx->Array.Objects = _mesa_NewHashTable();
1118 }
1119
1120
1121 /**
1122 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
1123 */
1124 static void
1125 delete_arrayobj_cb(GLuint id, void *data, void *userData)
1126 {
1127 struct gl_array_object *arrayObj = (struct gl_array_object *) data;
1128 struct gl_context *ctx = (struct gl_context *) userData;
1129 _mesa_delete_array_object(ctx, arrayObj);
1130 }
1131
1132
1133 /**
1134 * Free vertex array state for given context.
1135 */
1136 void
1137 _mesa_free_varray_data(struct gl_context *ctx)
1138 {
1139 _mesa_HashDeleteAll(ctx->Array.Objects, delete_arrayobj_cb, ctx);
1140 _mesa_DeleteHashTable(ctx->Array.Objects);
1141 }