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