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