disabled locked arrays in glDrawRangeElements()
[mesa.git] / src / mesa / main / varray.c
1 /* $Id: varray.c,v 1.20 2000/02/05 02:05:14 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #ifdef PC_HEADER
28 #include "all.h"
29 #else
30 #include "glheader.h"
31 #include "context.h"
32 #include "cva.h"
33 #include "enable.h"
34 #include "enums.h"
35 #include "dlist.h"
36 #include "light.h"
37 #include "macros.h"
38 #include "mmath.h"
39 #include "pipeline.h"
40 #include "state.h"
41 #include "texstate.h"
42 #include "translate.h"
43 #include "types.h"
44 #include "varray.h"
45 #include "vb.h"
46 #include "vbfill.h"
47 #include "vbrender.h"
48 #include "vbindirect.h"
49 #include "vbxform.h"
50 #include "xform.h"
51 #endif
52
53
54
55 void
56 _mesa_VertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
57 {
58 GET_CURRENT_CONTEXT(ctx);
59
60 if (size<2 || size>4) {
61 gl_error( ctx, GL_INVALID_VALUE, "glVertexPointer(size)" );
62 return;
63 }
64 if (stride<0) {
65 gl_error( ctx, GL_INVALID_VALUE, "glVertexPointer(stride)" );
66 return;
67 }
68
69 if (MESA_VERBOSE&(VERBOSE_VARRAY|VERBOSE_API))
70 fprintf(stderr, "glVertexPointer( sz %d type %s stride %d )\n", size,
71 gl_lookup_enum_by_nr( type ),
72 stride);
73
74 ctx->Array.Vertex.StrideB = stride;
75 if (!stride) {
76 switch (type) {
77 case GL_SHORT:
78 ctx->Array.Vertex.StrideB = size*sizeof(GLshort);
79 break;
80 case GL_INT:
81 ctx->Array.Vertex.StrideB = size*sizeof(GLint);
82 break;
83 case GL_FLOAT:
84 ctx->Array.Vertex.StrideB = size*sizeof(GLfloat);
85 break;
86 case GL_DOUBLE:
87 ctx->Array.Vertex.StrideB = size*sizeof(GLdouble);
88 break;
89 default:
90 gl_error( ctx, GL_INVALID_ENUM, "glVertexPointer(type)" );
91 return;
92 }
93 }
94 ctx->Array.Vertex.Size = size;
95 ctx->Array.Vertex.Type = type;
96 ctx->Array.Vertex.Stride = stride;
97 ctx->Array.Vertex.Ptr = (void *) ptr;
98 ctx->Array.VertexFunc = gl_trans_4f_tab[size][TYPE_IDX(type)];
99 ctx->Array.VertexEltFunc = gl_trans_elt_4f_tab[size][TYPE_IDX(type)];
100 ctx->Array.NewArrayState |= VERT_OBJ_ANY;
101 ctx->NewState |= NEW_CLIENT_STATE;
102 }
103
104
105
106
107 void
108 _mesa_NormalPointer(GLenum type, GLsizei stride, const GLvoid *ptr )
109 {
110 GET_CURRENT_CONTEXT(ctx);
111
112 if (stride<0) {
113 gl_error( ctx, GL_INVALID_VALUE, "glNormalPointer(stride)" );
114 return;
115 }
116
117 if (MESA_VERBOSE&(VERBOSE_VARRAY|VERBOSE_API))
118 fprintf(stderr, "glNormalPointer( type %s stride %d )\n",
119 gl_lookup_enum_by_nr( type ),
120 stride);
121
122 ctx->Array.Normal.StrideB = stride;
123 if (!stride) {
124 switch (type) {
125 case GL_BYTE:
126 ctx->Array.Normal.StrideB = 3*sizeof(GLbyte);
127 break;
128 case GL_SHORT:
129 ctx->Array.Normal.StrideB = 3*sizeof(GLshort);
130 break;
131 case GL_INT:
132 ctx->Array.Normal.StrideB = 3*sizeof(GLint);
133 break;
134 case GL_FLOAT:
135 ctx->Array.Normal.StrideB = 3*sizeof(GLfloat);
136 break;
137 case GL_DOUBLE:
138 ctx->Array.Normal.StrideB = 3*sizeof(GLdouble);
139 break;
140 default:
141 gl_error( ctx, GL_INVALID_ENUM, "glNormalPointer(type)" );
142 return;
143 }
144 }
145 ctx->Array.Normal.Type = type;
146 ctx->Array.Normal.Stride = stride;
147 ctx->Array.Normal.Ptr = (void *) ptr;
148 ctx->Array.NormalFunc = gl_trans_3f_tab[TYPE_IDX(type)];
149 ctx->Array.NormalEltFunc = gl_trans_elt_3f_tab[TYPE_IDX(type)];
150 ctx->Array.NewArrayState |= VERT_NORM;
151 ctx->NewState |= NEW_CLIENT_STATE;
152 }
153
154
155
156 void
157 _mesa_ColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
158 {
159 GET_CURRENT_CONTEXT(ctx);
160
161 if (size<3 || size>4) {
162 gl_error( ctx, GL_INVALID_VALUE, "glColorPointer(size)" );
163 return;
164 }
165 if (stride<0) {
166 gl_error( ctx, GL_INVALID_VALUE, "glColorPointer(stride)" );
167 return;
168 }
169
170 if (MESA_VERBOSE&(VERBOSE_VARRAY|VERBOSE_API))
171 fprintf(stderr, "glColorPointer( sz %d type %s stride %d )\n", size,
172 gl_lookup_enum_by_nr( type ),
173 stride);
174
175 ctx->Array.Color.StrideB = stride;
176 if (!stride) {
177 switch (type) {
178 case GL_BYTE:
179 ctx->Array.Color.StrideB = size*sizeof(GLbyte);
180 break;
181 case GL_UNSIGNED_BYTE:
182 ctx->Array.Color.StrideB = size*sizeof(GLubyte);
183 break;
184 case GL_SHORT:
185 ctx->Array.Color.StrideB = size*sizeof(GLshort);
186 break;
187 case GL_UNSIGNED_SHORT:
188 ctx->Array.Color.StrideB = size*sizeof(GLushort);
189 break;
190 case GL_INT:
191 ctx->Array.Color.StrideB = size*sizeof(GLint);
192 break;
193 case GL_UNSIGNED_INT:
194 ctx->Array.Color.StrideB = size*sizeof(GLuint);
195 break;
196 case GL_FLOAT:
197 ctx->Array.Color.StrideB = size*sizeof(GLfloat);
198 break;
199 case GL_DOUBLE:
200 ctx->Array.Color.StrideB = size*sizeof(GLdouble);
201 break;
202 default:
203 gl_error( ctx, GL_INVALID_ENUM, "glColorPointer(type)" );
204 return;
205 }
206 }
207 ctx->Array.Color.Size = size;
208 ctx->Array.Color.Type = type;
209 ctx->Array.Color.Stride = stride;
210 ctx->Array.Color.Ptr = (void *) ptr;
211 ctx->Array.ColorFunc = gl_trans_4ub_tab[size][TYPE_IDX(type)];
212 ctx->Array.ColorEltFunc = gl_trans_elt_4ub_tab[size][TYPE_IDX(type)];
213 ctx->Array.NewArrayState |= VERT_RGBA;
214 ctx->NewState |= NEW_CLIENT_STATE;
215 }
216
217
218
219 void
220 _mesa_IndexPointer(GLenum type, GLsizei stride, const GLvoid *ptr)
221 {
222 GET_CURRENT_CONTEXT(ctx);
223
224 if (stride<0) {
225 gl_error( ctx, GL_INVALID_VALUE, "glIndexPointer(stride)" );
226 return;
227 }
228
229 ctx->Array.Index.StrideB = stride;
230 if (!stride) {
231 switch (type) {
232 case GL_UNSIGNED_BYTE:
233 ctx->Array.Index.StrideB = sizeof(GLubyte);
234 break;
235 case GL_SHORT:
236 ctx->Array.Index.StrideB = sizeof(GLshort);
237 break;
238 case GL_INT:
239 ctx->Array.Index.StrideB = sizeof(GLint);
240 break;
241 case GL_FLOAT:
242 ctx->Array.Index.StrideB = sizeof(GLfloat);
243 break;
244 case GL_DOUBLE:
245 ctx->Array.Index.StrideB = sizeof(GLdouble);
246 break;
247 default:
248 gl_error( ctx, GL_INVALID_ENUM, "glIndexPointer(type)" );
249 return;
250 }
251 }
252 ctx->Array.Index.Type = type;
253 ctx->Array.Index.Stride = stride;
254 ctx->Array.Index.Ptr = (void *) ptr;
255 ctx->Array.IndexFunc = gl_trans_1ui_tab[TYPE_IDX(type)];
256 ctx->Array.IndexEltFunc = gl_trans_elt_1ui_tab[TYPE_IDX(type)];
257 ctx->Array.NewArrayState |= VERT_INDEX;
258 ctx->NewState |= NEW_CLIENT_STATE;
259 }
260
261
262
263 void
264 _mesa_TexCoordPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr)
265 {
266 GET_CURRENT_CONTEXT(ctx);
267 GLuint texUnit;
268
269 texUnit = ctx->Array.ActiveTexture;
270
271 if (size<1 || size>4) {
272 gl_error( ctx, GL_INVALID_VALUE, "glTexCoordPointer(size)" );
273 return;
274 }
275 if (stride<0) {
276 gl_error( ctx, GL_INVALID_VALUE, "glTexCoordPointer(stride)" );
277 return;
278 }
279
280 if (MESA_VERBOSE&(VERBOSE_VARRAY|VERBOSE_API))
281 fprintf(stderr, "glTexCoordPointer( unit %u sz %d type %s stride %d )\n",
282 texUnit,
283 size,
284 gl_lookup_enum_by_nr( type ),
285 stride);
286
287 ctx->Array.TexCoord[texUnit].StrideB = stride;
288 if (!stride) {
289 switch (type) {
290 case GL_SHORT:
291 ctx->Array.TexCoord[texUnit].StrideB = size*sizeof(GLshort);
292 break;
293 case GL_INT:
294 ctx->Array.TexCoord[texUnit].StrideB = size*sizeof(GLint);
295 break;
296 case GL_FLOAT:
297 ctx->Array.TexCoord[texUnit].StrideB = size*sizeof(GLfloat);
298 break;
299 case GL_DOUBLE:
300 ctx->Array.TexCoord[texUnit].StrideB = size*sizeof(GLdouble);
301 break;
302 default:
303 gl_error( ctx, GL_INVALID_ENUM, "glTexCoordPointer(type)" );
304 return;
305 }
306 }
307 ctx->Array.TexCoord[texUnit].Size = size;
308 ctx->Array.TexCoord[texUnit].Type = type;
309 ctx->Array.TexCoord[texUnit].Stride = stride;
310 ctx->Array.TexCoord[texUnit].Ptr = (void *) ptr;
311
312 ctx->Array.TexCoordFunc[texUnit] = gl_trans_4f_tab[size][TYPE_IDX(type)];
313 ctx->Array.TexCoordEltFunc[texUnit] = gl_trans_elt_4f_tab[size][TYPE_IDX(type)];
314 ctx->Array.NewArrayState |= PIPE_TEX(texUnit);
315 ctx->NewState |= NEW_CLIENT_STATE;
316 }
317
318
319
320
321 void
322 _mesa_EdgeFlagPointer(GLsizei stride, const void *vptr)
323 {
324 GET_CURRENT_CONTEXT(ctx);
325 const GLboolean *ptr = (GLboolean *)vptr;
326
327 if (stride<0) {
328 gl_error( ctx, GL_INVALID_VALUE, "glEdgeFlagPointer(stride)" );
329 return;
330 }
331 ctx->Array.EdgeFlag.Stride = stride;
332 ctx->Array.EdgeFlag.StrideB = stride ? stride : sizeof(GLboolean);
333 ctx->Array.EdgeFlag.Ptr = (GLboolean *) ptr;
334 if (stride != sizeof(GLboolean)) {
335 ctx->Array.EdgeFlagFunc = gl_trans_1ub_tab[TYPE_IDX(GL_UNSIGNED_BYTE)];
336 } else {
337 ctx->Array.EdgeFlagFunc = 0;
338 }
339 ctx->Array.EdgeFlagEltFunc = gl_trans_elt_1ub_tab[TYPE_IDX(GL_UNSIGNED_BYTE)];
340 ctx->Array.NewArrayState |= VERT_EDGE;
341 ctx->NewState |= NEW_CLIENT_STATE;
342 }
343
344
345 #if 0
346 /* Called only from gl_DrawElements
347 */
348 static void gl_CVAEltPointer( GLcontext *ctx, GLenum type, const GLvoid *ptr )
349 {
350 switch (type) {
351 case GL_UNSIGNED_BYTE:
352 ctx->CVA.Elt.StrideB = sizeof(GLubyte);
353 break;
354 case GL_UNSIGNED_SHORT:
355 ctx->CVA.Elt.StrideB = sizeof(GLushort);
356 break;
357 case GL_UNSIGNED_INT:
358 ctx->CVA.Elt.StrideB = sizeof(GLuint);
359 break;
360 default:
361 gl_error( ctx, GL_INVALID_ENUM, "glEltPointer(type)" );
362 return;
363 }
364 ctx->CVA.Elt.Type = type;
365 ctx->CVA.Elt.Stride = 0;
366 ctx->CVA.Elt.Ptr = (void *) ptr;
367 ctx->CVA.EltFunc = gl_trans_1ui_tab[TYPE_IDX(type)];
368 ctx->Array.NewArrayState |= VERT_ELT; /* ??? */
369 }
370 #endif
371
372
373 /* KW: Batch function to exec all the array elements in the input
374 * buffer prior to transform. Done only the first time a vertex
375 * buffer is executed or compiled.
376 *
377 * KW: Have to do this after each glEnd if cva isn't active. (also
378 * have to do it after each full buffer)
379 */
380 void gl_exec_array_elements( GLcontext *ctx, struct immediate *IM,
381 GLuint start,
382 GLuint count)
383 {
384 GLuint *flags = IM->Flag;
385 GLuint *elts = IM->Elt;
386 GLuint translate = ctx->Array.Flags;
387 GLuint i;
388
389 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
390 fprintf(stderr, "exec_array_elements %d .. %d\n", start, count);
391
392 if (translate & VERT_OBJ_ANY)
393 (ctx->Array.VertexEltFunc)( IM->Obj,
394 &ctx->Array.Vertex,
395 flags, elts, (VERT_ELT|VERT_OBJ_ANY),
396 start, count);
397
398 if (translate & VERT_NORM)
399 (ctx->Array.NormalEltFunc)( IM->Normal,
400 &ctx->Array.Normal,
401 flags, elts, (VERT_ELT|VERT_NORM),
402 start, count);
403
404 if (translate & VERT_EDGE)
405 (ctx->Array.EdgeFlagEltFunc)( IM->EdgeFlag,
406 &ctx->Array.EdgeFlag,
407 flags, elts, (VERT_ELT|VERT_EDGE),
408 start, count);
409
410 if (translate & VERT_RGBA)
411 (ctx->Array.ColorEltFunc)( IM->Color,
412 &ctx->Array.Color,
413 flags, elts, (VERT_ELT|VERT_RGBA),
414 start, count);
415
416 if (translate & VERT_INDEX)
417 (ctx->Array.IndexEltFunc)( IM->Index,
418 &ctx->Array.Index,
419 flags, elts, (VERT_ELT|VERT_INDEX),
420 start, count);
421
422 if (translate & VERT_TEX0_ANY)
423 (ctx->Array.TexCoordEltFunc[0])( IM->TexCoord[0],
424 &ctx->Array.TexCoord[0],
425 flags, elts, (VERT_ELT|VERT_TEX0_ANY),
426 start, count);
427
428 if (translate & VERT_TEX1_ANY)
429 (ctx->Array.TexCoordEltFunc[1])( IM->TexCoord[1],
430 &ctx->Array.TexCoord[1],
431 flags, elts, (VERT_ELT|VERT_TEX1_ANY),
432 start, count);
433
434
435 for (i = start ; i < count ; i++)
436 if (flags[i] & VERT_ELT)
437 flags[i] |= translate;
438
439 }
440
441
442
443 /* Enough funny business going on in here it might be quicker to use a
444 * function pointer.
445 */
446 #define ARRAY_ELT( IM, i ) \
447 { \
448 GLuint count = IM->Count; \
449 IM->Elt[count] = i; \
450 IM->Flag[count] = ((IM->Flag[count] & IM->ArrayAndFlags) | \
451 VERT_ELT); \
452 IM->FlushElt |= IM->ArrayEltFlush; \
453 IM->Count = count += IM->ArrayIncr; \
454 if (count == VB_MAX) \
455 IM->maybe_transform_vb( IM ); \
456 }
457
458
459 void
460 _mesa_ArrayElement( GLint i )
461 {
462 GET_IMMEDIATE;
463 ARRAY_ELT( IM, i );
464 }
465
466
467 static void
468 gl_ArrayElement( GLcontext *CC, GLint i )
469 {
470 struct immediate *im = CC->input;
471 ARRAY_ELT( im, i );
472 }
473
474
475
476 void
477 _mesa_DrawArrays(GLenum mode, GLint start, GLsizei count)
478 {
479 GET_CURRENT_CONTEXT(ctx);
480 struct vertex_buffer *VB = ctx->VB;
481 GLint i;
482
483 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawArrays");
484
485 if (count<0) {
486 gl_error( ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
487 return;
488 }
489
490 if (!ctx->CompileFlag && ctx->Array.Vertex.Enabled)
491 {
492 GLint remaining = count;
493 GLint i;
494 struct gl_client_array *Normal;
495 struct gl_client_array *Color;
496 struct gl_client_array *Index;
497 struct gl_client_array *TexCoord[MAX_TEXTURE_UNITS];
498 struct gl_client_array *EdgeFlag;
499 struct immediate *IM = VB->IM;
500 struct gl_pipeline *elt = &ctx->CVA.elt;
501 GLboolean relock;
502 GLuint fallback, required;
503
504 if (ctx->NewState)
505 gl_update_state( ctx );
506
507 /* Just turn off cva on this path. Could be useful for multipass
508 * rendering to keep it turned on.
509 */
510 relock = ctx->CompileCVAFlag;
511
512 if (relock) {
513 ctx->CompileCVAFlag = 0;
514 elt->pipeline_valid = 0;
515 }
516
517 if (!elt->pipeline_valid)
518 gl_build_immediate_pipeline( ctx );
519
520 required = elt->inputs;
521 fallback = (elt->inputs & ~ctx->Array.Summary);
522
523 /* The translate function doesn't do anything about size. It
524 * just ensures that type and stride come out right.
525 */
526 IM->v.Obj.size = ctx->Array.Vertex.Size;
527
528 if (required & VERT_RGBA)
529 {
530 Color = &ctx->Array.Color;
531 if (fallback & VERT_RGBA) {
532 Color = &ctx->Fallback.Color;
533 ctx->Array.ColorFunc =
534 gl_trans_4ub_tab[4][TYPE_IDX(GL_UNSIGNED_BYTE)];
535 }
536 }
537
538 if (required & VERT_INDEX)
539 {
540 Index = &ctx->Array.Index;
541 if (fallback & VERT_INDEX) {
542 Index = &ctx->Fallback.Index;
543 ctx->Array.IndexFunc = gl_trans_1ui_tab[TYPE_IDX(GL_UNSIGNED_INT)];
544 }
545 }
546
547 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++)
548 {
549 GLuint flag = VERT_TEX_ANY(i);
550
551 if (required & flag) {
552 TexCoord[i] = &ctx->Array.TexCoord[i];
553
554 if (fallback & flag)
555 {
556 TexCoord[i] = &ctx->Fallback.TexCoord[i];
557 TexCoord[i]->Size = gl_texcoord_size( ctx->Current.Flag, i );
558
559 ctx->Array.TexCoordFunc[i] =
560 gl_trans_4f_tab[TexCoord[i]->Size][TYPE_IDX(GL_FLOAT)];
561 }
562 }
563 }
564
565 if (ctx->Array.Flags != ctx->Array.Flag[0])
566 for (i = 0 ; i < VB_MAX ; i++)
567 ctx->Array.Flag[i] = ctx->Array.Flags;
568
569
570 if (required & VERT_NORM)
571 {
572 Normal = &ctx->Array.Normal;
573 if (fallback & VERT_NORM) {
574 Normal = &ctx->Fallback.Normal;
575 ctx->Array.NormalFunc = gl_trans_3f_tab[TYPE_IDX(GL_FLOAT)];
576 }
577 }
578
579 if ( required & VERT_EDGE )
580 {
581 if (mode == GL_TRIANGLES ||
582 mode == GL_QUADS ||
583 mode == GL_POLYGON)
584 {
585 EdgeFlag = &ctx->Array.EdgeFlag;
586 if (fallback & VERT_EDGE) {
587 EdgeFlag = &ctx->Fallback.EdgeFlag;
588 ctx->Array.EdgeFlagFunc =
589 gl_trans_1ub_tab[TYPE_IDX(GL_UNSIGNED_BYTE)];
590 }
591 }
592 else
593 required &= ~VERT_EDGE;
594 }
595
596 VB->Primitive = IM->Primitive;
597 VB->NextPrimitive = IM->NextPrimitive;
598 VB->MaterialMask = IM->MaterialMask;
599 VB->Material = IM->Material;
600 VB->BoundsPtr = 0;
601
602 while (remaining > 0) {
603 GLint vbspace = VB_MAX - VB_START;
604 GLuint count, n;
605
606 if (vbspace >= remaining) {
607 n = remaining;
608 VB->LastPrimitive = VB_START + n;
609 } else {
610 n = vbspace;
611 VB->LastPrimitive = VB_START;
612 }
613
614 VB->CullMode = 0;
615
616 ctx->Array.VertexFunc( IM->Obj + VB_START,
617 &ctx->Array.Vertex, start, n );
618
619 if (required & VERT_NORM) {
620 ctx->Array.NormalFunc( IM->Normal + VB_START,
621 Normal, start, n );
622 }
623
624 if (required & VERT_EDGE) {
625 ctx->Array.EdgeFlagFunc( IM->EdgeFlag + VB_START,
626 EdgeFlag, start, n );
627 }
628
629 if (required & VERT_RGBA) {
630 ctx->Array.ColorFunc( IM->Color + VB_START,
631 Color, start, n );
632 }
633
634 if (required & VERT_INDEX) {
635 ctx->Array.IndexFunc( IM->Index + VB_START,
636 Index, start, n );
637 }
638
639 if (required & VERT_TEX0_ANY) {
640 IM->v.TexCoord[0].size = TexCoord[0]->Size;
641 ctx->Array.TexCoordFunc[0]( IM->TexCoord[0] + VB_START,
642 TexCoord[0], start, n );
643 }
644
645 if (required & VERT_TEX1_ANY) {
646 IM->v.TexCoord[1].size = TexCoord[1]->Size;
647 ctx->Array.TexCoordFunc[1]( IM->TexCoord[1] + VB_START,
648 TexCoord[1], start, n );
649 }
650
651 VB->ObjPtr = &IM->v.Obj;
652 VB->NormalPtr = &IM->v.Normal;
653 VB->ColorPtr = &IM->v.Color;
654 VB->Color[0] = VB->Color[1] = VB->ColorPtr;
655 VB->IndexPtr = &IM->v.Index;
656 VB->EdgeFlagPtr = &IM->v.EdgeFlag;
657 VB->TexCoordPtr[0] = &IM->v.TexCoord[0];
658 VB->TexCoordPtr[1] = &IM->v.TexCoord[1];
659
660 VB->Flag = ctx->Array.Flag;
661 VB->OrFlag = ctx->Array.Flags;
662
663 VB->Start = IM->Start = VB_START;
664 count = VB->Count = IM->Count = VB_START + n;
665
666 #define RESET_VEC(v, t, s, c) (v.start = t v.data[s], v.count = c)
667
668 RESET_VEC(IM->v.Obj, (GLfloat *), VB_START, count);
669 RESET_VEC(IM->v.Normal, (GLfloat *), VB_START, count);
670 RESET_VEC(IM->v.TexCoord[0], (GLfloat *), VB_START, count);
671 RESET_VEC(IM->v.TexCoord[1], (GLfloat *), VB_START, count);
672 RESET_VEC(IM->v.Index, &, VB_START, count);
673 RESET_VEC(IM->v.Elt, &, VB_START, count);
674 RESET_VEC(IM->v.EdgeFlag, &, VB_START, count);
675 RESET_VEC(IM->v.Color, (GLubyte *), VB_START, count);
676 RESET_VEC(VB->Clip, (GLfloat *), VB_START, count);
677 RESET_VEC(VB->Eye, (GLfloat *), VB_START, count);
678 RESET_VEC(VB->Win, (GLfloat *), VB_START, count);
679 RESET_VEC(VB->BColor, (GLubyte *), VB_START, count);
680 RESET_VEC(VB->BIndex, &, VB_START, count);
681
682 VB->NextPrimitive[VB->CopyStart] = VB->Count;
683 VB->Primitive[VB->CopyStart] = mode;
684
685 /* Transform and render.
686 */
687 gl_run_pipeline( VB );
688 gl_reset_vb( VB );
689
690 ctx->Array.Flag[count] = ctx->Array.Flags;
691 ctx->Array.Flag[VB_START] = ctx->Array.Flags;
692
693 start += n;
694 remaining -= n;
695 }
696
697 gl_reset_input( ctx );
698
699 if (relock) {
700 ctx->CompileCVAFlag = relock;
701 elt->pipeline_valid = 0;
702 }
703 }
704 else if (ctx->Array.Vertex.Enabled)
705 {
706 /* The GL_COMPILE and GL_COMPILE_AND_EXECUTE cases. These
707 * could be handled by the above code, but it gets a little
708 * complex. The generated list is still of good quality
709 * this way.
710 */
711 gl_Begin( ctx, mode );
712 for (i=0;i<count;i++) {
713 gl_ArrayElement( ctx, start+i );
714 }
715 gl_End( ctx );
716 }
717 else
718 {
719 /* The degenerate case where vertices are not enabled - only
720 * need to process the very final array element, as all of the
721 * preceding ones would be overwritten anyway.
722 */
723 gl_Begin( ctx, mode );
724 gl_ArrayElement( ctx, start+count );
725 gl_End( ctx );
726 }
727 }
728
729
730
731 /* KW: Exactly fakes the effects of calling glArrayElement multiple times.
732 * Compilation is handled via. the IM->maybe_transform_vb() callback.
733 */
734 #if 1
735 #define DRAW_ELT(FUNC, TYPE) \
736 static void FUNC( GLcontext *ctx, GLenum mode, \
737 TYPE *indices, GLuint count ) \
738 { \
739 GLuint i,j; \
740 \
741 gl_Begin( ctx, mode ); \
742 \
743 for (j = 0 ; j < count ; ) { \
744 struct immediate *IM = ctx->input; \
745 GLuint start = IM->Start; \
746 GLuint nr = MIN2( VB_MAX, count - j + start ); \
747 GLuint sf = IM->Flag[start]; \
748 IM->FlushElt |= IM->ArrayEltFlush; \
749 \
750 for (i = start ; i < nr ; i++) { \
751 IM->Elt[i] = (GLuint) *indices++; \
752 IM->Flag[i] = VERT_ELT; \
753 } \
754 \
755 if (j == 0) IM->Flag[start] |= sf; \
756 \
757 IM->Count = nr; \
758 j += nr - start; \
759 \
760 if (j == count) gl_End( ctx ); \
761 IM->maybe_transform_vb( IM ); \
762 } \
763 }
764 #else
765 #define DRAW_ELT(FUNC, TYPE) \
766 static void FUNC( GLcontext *ctx, GLenum mode, \
767 TYPE *indices, GLuint count ) \
768 { \
769 int i; \
770 glBegin(mode); \
771 for (i = 0 ; i < count ; i++) \
772 glArrayElement( indices[i] ); \
773 glEnd(); \
774 }
775 #endif
776
777
778 DRAW_ELT( draw_elt_ubyte, GLubyte )
779 DRAW_ELT( draw_elt_ushort, GLushort )
780 DRAW_ELT( draw_elt_uint, GLuint )
781
782
783 static GLuint natural_stride[0x10] =
784 {
785 sizeof(GLbyte), /* 0 */
786 sizeof(GLubyte), /* 1 */
787 sizeof(GLshort), /* 2 */
788 sizeof(GLushort), /* 3 */
789 sizeof(GLint), /* 4 */
790 sizeof(GLuint), /* 5 */
791 sizeof(GLfloat), /* 6 */
792 2 * sizeof(GLbyte), /* 7 */
793 3 * sizeof(GLbyte), /* 8 */
794 4 * sizeof(GLbyte), /* 9 */
795 sizeof(GLdouble), /* a */
796 0, /* b */
797 0, /* c */
798 0, /* d */
799 0, /* e */
800 0 /* f */
801 };
802
803
804 void
805 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
806 {
807 GET_CURRENT_CONTEXT(ctx);
808 struct gl_cva *cva;
809
810 cva = &ctx->CVA;
811 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawElements");
812
813 if (count <= 0) {
814 if (count < 0)
815 gl_error( ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
816 return;
817 }
818
819 if (mode > GL_POLYGON) {
820 gl_error( ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
821 return;
822 }
823
824 if (type != GL_UNSIGNED_INT && type != GL_UNSIGNED_BYTE && type != GL_UNSIGNED_SHORT)
825 {
826 gl_error( ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
827 return;
828 }
829
830 if (ctx->NewState)
831 gl_update_state(ctx);
832
833 if (ctx->CompileCVAFlag)
834 {
835 #if defined(MESA_CVA_PROF)
836 force_init_prof();
837 #endif
838
839 /* Treat VERT_ELT like a special client array.
840 */
841 ctx->Array.NewArrayState |= VERT_ELT;
842 ctx->Array.Summary |= VERT_ELT;
843 ctx->Array.Flags |= VERT_ELT;
844
845 cva->elt_mode = mode;
846 cva->elt_count = count;
847 cva->Elt.Type = type;
848 cva->Elt.Ptr = (void *) indices;
849 cva->Elt.StrideB = natural_stride[TYPE_IDX(type)];
850 cva->EltFunc = gl_trans_1ui_tab[TYPE_IDX(type)];
851
852 if (!cva->pre.pipeline_valid)
853 gl_build_precalc_pipeline( ctx );
854 else if (MESA_VERBOSE & VERBOSE_PIPELINE)
855 fprintf(stderr, ": dont rebuild\n");
856
857 gl_cva_force_precalc( ctx );
858
859 /* Did we 'precalculate' the render op?
860 */
861 if (ctx->CVA.pre.ops & PIPE_OP_RENDER) {
862 ctx->Array.NewArrayState |= VERT_ELT;
863 ctx->Array.Summary &= ~VERT_ELT;
864 ctx->Array.Flags &= ~VERT_ELT;
865 return;
866 }
867
868 if ( (MESA_VERBOSE&VERBOSE_VARRAY) )
869 printf("using immediate\n");
870 }
871
872
873 /* Otherwise, have to use the immediate path to render.
874 */
875 switch (type) {
876 case GL_UNSIGNED_BYTE:
877 {
878 GLubyte *ub_indices = (GLubyte *) indices;
879 if (ctx->Array.Summary & VERT_OBJ_ANY) {
880 draw_elt_ubyte( ctx, mode, ub_indices, count );
881 } else {
882 gl_ArrayElement( ctx, (GLuint) ub_indices[count-1] );
883 }
884 }
885 break;
886 case GL_UNSIGNED_SHORT:
887 {
888 GLushort *us_indices = (GLushort *) indices;
889 if (ctx->Array.Summary & VERT_OBJ_ANY) {
890 draw_elt_ushort( ctx, mode, us_indices, count );
891 } else {
892 gl_ArrayElement( ctx, (GLuint) us_indices[count-1] );
893 }
894 }
895 break;
896 case GL_UNSIGNED_INT:
897 {
898 GLuint *ui_indices = (GLuint *) indices;
899 if (ctx->Array.Summary & VERT_OBJ_ANY) {
900 draw_elt_uint( ctx, mode, ui_indices, count );
901 } else {
902 gl_ArrayElement( ctx, ui_indices[count-1] );
903 }
904 }
905 break;
906 default:
907 gl_error( ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
908 break;
909 }
910
911 if (ctx->CompileCVAFlag) {
912 ctx->Array.NewArrayState |= VERT_ELT;
913 ctx->Array.Summary &= ~VERT_ELT;
914 }
915 }
916
917
918
919 void
920 _mesa_InterleavedArrays(GLenum format, GLsizei stride, const GLvoid *pointer)
921 {
922 GET_CURRENT_CONTEXT(ctx);
923 GLboolean tflag, cflag, nflag; /* enable/disable flags */
924 GLint tcomps, ccomps, vcomps; /* components per texcoord, color, vertex */
925
926 GLenum ctype; /* color type */
927 GLint coffset, noffset, voffset;/* color, normal, vertex offsets */
928 GLint defstride; /* default stride */
929 GLint c, f;
930 GLint coordUnitSave;
931
932 f = sizeof(GLfloat);
933 c = f * ((4*sizeof(GLubyte) + (f-1)) / f);
934
935 if (stride<0) {
936 gl_error( ctx, GL_INVALID_VALUE, "glInterleavedArrays(stride)" );
937 return;
938 }
939
940 switch (format) {
941 case GL_V2F:
942 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
943 tcomps = 0; ccomps = 0; vcomps = 2;
944 voffset = 0;
945 defstride = 2*f;
946 break;
947 case GL_V3F:
948 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
949 tcomps = 0; ccomps = 0; vcomps = 3;
950 voffset = 0;
951 defstride = 3*f;
952 break;
953 case GL_C4UB_V2F:
954 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
955 tcomps = 0; ccomps = 4; vcomps = 2;
956 ctype = GL_UNSIGNED_BYTE;
957 coffset = 0;
958 voffset = c;
959 defstride = c + 2*f;
960 break;
961 case GL_C4UB_V3F:
962 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
963 tcomps = 0; ccomps = 4; vcomps = 3;
964 ctype = GL_UNSIGNED_BYTE;
965 coffset = 0;
966 voffset = c;
967 defstride = c + 3*f;
968 break;
969 case GL_C3F_V3F:
970 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
971 tcomps = 0; ccomps = 3; vcomps = 3;
972 ctype = GL_FLOAT;
973 coffset = 0;
974 voffset = 3*f;
975 defstride = 6*f;
976 break;
977 case GL_N3F_V3F:
978 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_TRUE;
979 tcomps = 0; ccomps = 0; vcomps = 3;
980 noffset = 0;
981 voffset = 3*f;
982 defstride = 6*f;
983 break;
984 case GL_C4F_N3F_V3F:
985 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_TRUE;
986 tcomps = 0; ccomps = 4; vcomps = 3;
987 ctype = GL_FLOAT;
988 coffset = 0;
989 noffset = 4*f;
990 voffset = 7*f;
991 defstride = 10*f;
992 break;
993 case GL_T2F_V3F:
994 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
995 tcomps = 2; ccomps = 0; vcomps = 3;
996 voffset = 2*f;
997 defstride = 5*f;
998 break;
999 case GL_T4F_V4F:
1000 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
1001 tcomps = 4; ccomps = 0; vcomps = 4;
1002 voffset = 4*f;
1003 defstride = 8*f;
1004 break;
1005 case GL_T2F_C4UB_V3F:
1006 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1007 tcomps = 2; ccomps = 4; vcomps = 3;
1008 ctype = GL_UNSIGNED_BYTE;
1009 coffset = 2*f;
1010 voffset = c+2*f;
1011 defstride = c+5*f;
1012 break;
1013 case GL_T2F_C3F_V3F:
1014 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1015 tcomps = 2; ccomps = 3; vcomps = 3;
1016 ctype = GL_FLOAT;
1017 coffset = 2*f;
1018 voffset = 5*f;
1019 defstride = 8*f;
1020 break;
1021 case GL_T2F_N3F_V3F:
1022 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_TRUE;
1023 tcomps = 2; ccomps = 0; vcomps = 3;
1024 noffset = 2*f;
1025 voffset = 5*f;
1026 defstride = 8*f;
1027 break;
1028 case GL_T2F_C4F_N3F_V3F:
1029 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1030 tcomps = 2; ccomps = 4; vcomps = 3;
1031 ctype = GL_FLOAT;
1032 coffset = 2*f;
1033 noffset = 6*f;
1034 voffset = 9*f;
1035 defstride = 12*f;
1036 break;
1037 case GL_T4F_C4F_N3F_V4F:
1038 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1039 tcomps = 4; ccomps = 4; vcomps = 4;
1040 ctype = GL_FLOAT;
1041 coffset = 4*f;
1042 noffset = 8*f;
1043 voffset = 11*f;
1044 defstride = 15*f;
1045 break;
1046 default:
1047 gl_error( ctx, GL_INVALID_ENUM, "glInterleavedArrays(format)" );
1048 return;
1049 }
1050
1051 if (stride==0) {
1052 stride = defstride;
1053 }
1054
1055 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY );
1056 _mesa_DisableClientState( GL_INDEX_ARRAY );
1057
1058 /* Texcoords */
1059 coordUnitSave = ctx->Array.ActiveTexture;
1060 if (tflag) {
1061 GLint i;
1062 GLint factor = ctx->Array.TexCoordInterleaveFactor;
1063 for (i = 0; i < factor; i++) {
1064 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1065 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY );
1066 glTexCoordPointer( tcomps, GL_FLOAT, stride,
1067 (GLubyte *) pointer + i * coffset );
1068 }
1069 for (i = factor; i < ctx->Const.MaxTextureUnits; i++) {
1070 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1071 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
1072 }
1073 }
1074 else {
1075 GLint i;
1076 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1077 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1078 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
1079 }
1080 }
1081 /* Restore texture coordinate unit index */
1082 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + coordUnitSave) );
1083
1084
1085 /* Color */
1086 if (cflag) {
1087 _mesa_EnableClientState( GL_COLOR_ARRAY );
1088 glColorPointer( ccomps, ctype, stride,
1089 (GLubyte*) pointer + coffset );
1090 }
1091 else {
1092 _mesa_DisableClientState( GL_COLOR_ARRAY );
1093 }
1094
1095
1096 /* Normals */
1097 if (nflag) {
1098 _mesa_EnableClientState( GL_NORMAL_ARRAY );
1099 glNormalPointer( GL_FLOAT, stride,
1100 (GLubyte*) pointer + noffset );
1101 }
1102 else {
1103 _mesa_DisableClientState( GL_NORMAL_ARRAY );
1104 }
1105
1106 _mesa_EnableClientState( GL_VERTEX_ARRAY );
1107 glVertexPointer( vcomps, GL_FLOAT, stride,
1108 (GLubyte *) pointer + voffset );
1109 }
1110
1111
1112
1113 void
1114 _mesa_DrawRangeElements(GLenum mode, GLuint start,
1115 GLuint end, GLsizei count,
1116 GLenum type, const GLvoid *indices)
1117 {
1118 GET_CURRENT_CONTEXT(ctx);
1119
1120 if (end < start) {
1121 gl_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements( end < start )");
1122 return;
1123 }
1124
1125 #if 0
1126 /*
1127 * XXX something in locked arrays is broken! If start = 0,
1128 * end = 1 and count = 2 we'll take the LockArrays path and
1129 * get incorrect results. See Scott McMillan's bug of 3 Jan 2000.
1130 * For now, don't use locked arrays.
1131 */
1132 if (!ctx->Array.LockCount && 2*count > (GLint) 3*(end-start)) {
1133 glLockArraysEXT( start, end );
1134 glDrawElements( mode, count, type, indices );
1135 glUnlockArraysEXT();
1136 } else {
1137 glDrawElements( mode, count, type, indices );
1138 }
1139 #else
1140 glDrawElements( mode, count, type, indices );
1141 #endif
1142 }
1143
1144
1145
1146 void gl_update_client_state( GLcontext *ctx )
1147 {
1148 static GLuint sz_flags[5] = { 0,
1149 0,
1150 VERT_OBJ_2,
1151 VERT_OBJ_23,
1152 VERT_OBJ_234 };
1153
1154 static GLuint tc_flags[5] = { 0,
1155 VERT_TEX0_1,
1156 VERT_TEX0_12,
1157 VERT_TEX0_123,
1158 VERT_TEX0_1234 };
1159
1160 ctx->Array.Flags = 0;
1161 ctx->Array.Summary = 0;
1162 ctx->input->ArrayIncr = 0;
1163
1164 if (ctx->Array.Normal.Enabled) ctx->Array.Flags |= VERT_NORM;
1165 if (ctx->Array.Color.Enabled) ctx->Array.Flags |= VERT_RGBA;
1166 if (ctx->Array.Index.Enabled) ctx->Array.Flags |= VERT_INDEX;
1167 if (ctx->Array.EdgeFlag.Enabled) ctx->Array.Flags |= VERT_EDGE;
1168 if (ctx->Array.Vertex.Enabled) {
1169 ctx->Array.Flags |= sz_flags[ctx->Array.Vertex.Size];
1170 ctx->input->ArrayIncr = 1;
1171 }
1172 if (ctx->Array.TexCoord[0].Enabled) {
1173 ctx->Array.Flags |= tc_flags[ctx->Array.TexCoord[0].Size];
1174 }
1175 if (ctx->Array.TexCoord[1].Enabled) {
1176 ctx->Array.Flags |= (tc_flags[ctx->Array.TexCoord[1].Size] << NR_TEXSIZE_BITS);
1177 }
1178
1179 /* Not really important any more:
1180 */
1181 ctx->Array.Summary = ctx->Array.Flags & VERT_DATA;
1182 ctx->input->ArrayAndFlags = ~ctx->Array.Flags;
1183 ctx->input->ArrayEltFlush = !(ctx->CompileCVAFlag);
1184 }
1185