Committing in .
[mesa.git] / src / mesa / main / varray.c
1 /* $Id: varray.c,v 1.22 2000/06/12 15:30:51 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
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
374 void
375 _mesa_VertexPointerEXT(GLint size, GLenum type, GLsizei stride,
376 GLsizei count, const GLvoid *ptr)
377 {
378 (void) count;
379 _mesa_VertexPointer(size, type, stride, ptr);
380 }
381
382
383 void
384 _mesa_NormalPointerEXT(GLenum type, GLsizei stride, GLsizei count,
385 const GLvoid *ptr)
386 {
387 (void) count;
388 _mesa_NormalPointer(type, stride, ptr);
389 }
390
391
392 void
393 _mesa_ColorPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count,
394 const GLvoid *ptr)
395 {
396 (void) count;
397 _mesa_ColorPointer(size, type, stride, ptr);
398 }
399
400
401 void
402 _mesa_IndexPointerEXT(GLenum type, GLsizei stride, GLsizei count,
403 const GLvoid *ptr)
404 {
405 (void) count;
406 _mesa_IndexPointer(type, stride, ptr);
407 }
408
409
410 void
411 _mesa_TexCoordPointerEXT(GLint size, GLenum type, GLsizei stride,
412 GLsizei count, const GLvoid *ptr)
413 {
414 (void) count;
415 _mesa_TexCoordPointer(size, type, stride, ptr);
416 }
417
418
419 void
420 _mesa_EdgeFlagPointerEXT(GLsizei stride, GLsizei count, const GLboolean *ptr)
421 {
422 (void) count;
423 _mesa_EdgeFlagPointer(stride, ptr);
424 }
425
426
427
428
429
430 /* KW: Batch function to exec all the array elements in the input
431 * buffer prior to transform. Done only the first time a vertex
432 * buffer is executed or compiled.
433 *
434 * KW: Have to do this after each glEnd if cva isn't active. (also
435 * have to do it after each full buffer)
436 */
437 void gl_exec_array_elements( GLcontext *ctx, struct immediate *IM,
438 GLuint start,
439 GLuint count)
440 {
441 GLuint *flags = IM->Flag;
442 GLuint *elts = IM->Elt;
443 GLuint translate = ctx->Array.Flags;
444 GLuint i;
445
446 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
447 fprintf(stderr, "exec_array_elements %d .. %d\n", start, count);
448
449 if (translate & VERT_OBJ_ANY)
450 (ctx->Array.VertexEltFunc)( IM->Obj,
451 &ctx->Array.Vertex,
452 flags, elts, (VERT_ELT|VERT_OBJ_ANY),
453 start, count);
454
455 if (translate & VERT_NORM)
456 (ctx->Array.NormalEltFunc)( IM->Normal,
457 &ctx->Array.Normal,
458 flags, elts, (VERT_ELT|VERT_NORM),
459 start, count);
460
461 if (translate & VERT_EDGE)
462 (ctx->Array.EdgeFlagEltFunc)( IM->EdgeFlag,
463 &ctx->Array.EdgeFlag,
464 flags, elts, (VERT_ELT|VERT_EDGE),
465 start, count);
466
467 if (translate & VERT_RGBA)
468 (ctx->Array.ColorEltFunc)( IM->Color,
469 &ctx->Array.Color,
470 flags, elts, (VERT_ELT|VERT_RGBA),
471 start, count);
472
473 if (translate & VERT_INDEX)
474 (ctx->Array.IndexEltFunc)( IM->Index,
475 &ctx->Array.Index,
476 flags, elts, (VERT_ELT|VERT_INDEX),
477 start, count);
478
479 if (translate & VERT_TEX0_ANY)
480 (ctx->Array.TexCoordEltFunc[0])( IM->TexCoord[0],
481 &ctx->Array.TexCoord[0],
482 flags, elts, (VERT_ELT|VERT_TEX0_ANY),
483 start, count);
484
485 if (translate & VERT_TEX1_ANY)
486 (ctx->Array.TexCoordEltFunc[1])( IM->TexCoord[1],
487 &ctx->Array.TexCoord[1],
488 flags, elts, (VERT_ELT|VERT_TEX1_ANY),
489 start, count);
490
491
492 for (i = start ; i < count ; i++)
493 if (flags[i] & VERT_ELT)
494 flags[i] |= translate;
495
496 }
497
498
499
500 /* Enough funny business going on in here it might be quicker to use a
501 * function pointer.
502 */
503 #define ARRAY_ELT( IM, i ) \
504 { \
505 GLuint count = IM->Count; \
506 IM->Elt[count] = i; \
507 IM->Flag[count] = ((IM->Flag[count] & IM->ArrayAndFlags) | \
508 VERT_ELT); \
509 IM->FlushElt |= IM->ArrayEltFlush; \
510 IM->Count = count += IM->ArrayIncr; \
511 if (count == VB_MAX) \
512 IM->maybe_transform_vb( IM ); \
513 }
514
515
516 void
517 _mesa_ArrayElement( GLint i )
518 {
519 GET_IMMEDIATE;
520 ARRAY_ELT( IM, i );
521 }
522
523
524 static void
525 gl_ArrayElement( GLcontext *CC, GLint i )
526 {
527 struct immediate *im = CC->input;
528 ARRAY_ELT( im, i );
529 }
530
531
532
533 void
534 _mesa_DrawArrays(GLenum mode, GLint start, GLsizei count)
535 {
536 GET_CURRENT_CONTEXT(ctx);
537 struct vertex_buffer *VB = ctx->VB;
538 GLint i;
539
540 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawArrays");
541
542 if (count<0) {
543 gl_error( ctx, GL_INVALID_VALUE, "glDrawArrays(count)" );
544 return;
545 }
546
547 if (!ctx->CompileFlag && ctx->Array.Vertex.Enabled)
548 {
549 GLint remaining = count;
550 GLint i;
551 struct gl_client_array *Normal;
552 struct gl_client_array *Color;
553 struct gl_client_array *Index;
554 struct gl_client_array *TexCoord[MAX_TEXTURE_UNITS];
555 struct gl_client_array *EdgeFlag;
556 struct immediate *IM = VB->IM;
557 struct gl_pipeline *elt = &ctx->CVA.elt;
558 GLboolean relock;
559 GLuint fallback, required;
560
561 if (ctx->NewState)
562 gl_update_state( ctx );
563
564 /* Just turn off cva on this path. Could be useful for multipass
565 * rendering to keep it turned on.
566 */
567 relock = ctx->CompileCVAFlag;
568
569 if (relock) {
570 ctx->CompileCVAFlag = 0;
571 elt->pipeline_valid = 0;
572 }
573
574 if (!elt->pipeline_valid)
575 gl_build_immediate_pipeline( ctx );
576
577 required = elt->inputs;
578 fallback = (elt->inputs & ~ctx->Array.Summary);
579
580 /* The translate function doesn't do anything about size. It
581 * just ensures that type and stride come out right.
582 */
583 IM->v.Obj.size = ctx->Array.Vertex.Size;
584
585 if (required & VERT_RGBA)
586 {
587 Color = &ctx->Array.Color;
588 if (fallback & VERT_RGBA) {
589 Color = &ctx->Fallback.Color;
590 ctx->Array.ColorFunc =
591 gl_trans_4ub_tab[4][TYPE_IDX(GL_UNSIGNED_BYTE)];
592 }
593 }
594
595 if (required & VERT_INDEX)
596 {
597 Index = &ctx->Array.Index;
598 if (fallback & VERT_INDEX) {
599 Index = &ctx->Fallback.Index;
600 ctx->Array.IndexFunc = gl_trans_1ui_tab[TYPE_IDX(GL_UNSIGNED_INT)];
601 }
602 }
603
604 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++)
605 {
606 GLuint flag = VERT_TEX_ANY(i);
607
608 if (required & flag) {
609 TexCoord[i] = &ctx->Array.TexCoord[i];
610
611 if (fallback & flag)
612 {
613 TexCoord[i] = &ctx->Fallback.TexCoord[i];
614 TexCoord[i]->Size = gl_texcoord_size( ctx->Current.Flag, i );
615
616 ctx->Array.TexCoordFunc[i] =
617 gl_trans_4f_tab[TexCoord[i]->Size][TYPE_IDX(GL_FLOAT)];
618 }
619 }
620 }
621
622 if (ctx->Array.Flags != ctx->Array.Flag[0])
623 for (i = 0 ; i < VB_MAX ; i++)
624 ctx->Array.Flag[i] = ctx->Array.Flags;
625
626
627 if (required & VERT_NORM)
628 {
629 Normal = &ctx->Array.Normal;
630 if (fallback & VERT_NORM) {
631 Normal = &ctx->Fallback.Normal;
632 ctx->Array.NormalFunc = gl_trans_3f_tab[TYPE_IDX(GL_FLOAT)];
633 }
634 }
635
636 if ( required & VERT_EDGE )
637 {
638 if (mode == GL_TRIANGLES ||
639 mode == GL_QUADS ||
640 mode == GL_POLYGON)
641 {
642 EdgeFlag = &ctx->Array.EdgeFlag;
643 if (fallback & VERT_EDGE) {
644 EdgeFlag = &ctx->Fallback.EdgeFlag;
645 ctx->Array.EdgeFlagFunc =
646 gl_trans_1ub_tab[TYPE_IDX(GL_UNSIGNED_BYTE)];
647 }
648 }
649 else
650 required &= ~VERT_EDGE;
651 }
652
653 VB->Primitive = IM->Primitive;
654 VB->NextPrimitive = IM->NextPrimitive;
655 VB->MaterialMask = IM->MaterialMask;
656 VB->Material = IM->Material;
657 VB->BoundsPtr = 0;
658
659 while (remaining > 0) {
660 GLint vbspace = VB_MAX - VB_START;
661 GLuint count, n;
662
663 if (vbspace >= remaining) {
664 n = remaining;
665 VB->LastPrimitive = VB_START + n;
666 } else {
667 n = vbspace;
668 VB->LastPrimitive = VB_START;
669 }
670
671 VB->CullMode = 0;
672
673 ctx->Array.VertexFunc( IM->Obj + VB_START,
674 &ctx->Array.Vertex, start, n );
675
676 if (required & VERT_NORM) {
677 ctx->Array.NormalFunc( IM->Normal + VB_START,
678 Normal, start, n );
679 }
680
681 if (required & VERT_EDGE) {
682 ctx->Array.EdgeFlagFunc( IM->EdgeFlag + VB_START,
683 EdgeFlag, start, n );
684 }
685
686 if (required & VERT_RGBA) {
687 ctx->Array.ColorFunc( IM->Color + VB_START,
688 Color, start, n );
689 }
690
691 if (required & VERT_INDEX) {
692 ctx->Array.IndexFunc( IM->Index + VB_START,
693 Index, start, n );
694 }
695
696 if (required & VERT_TEX0_ANY) {
697 IM->v.TexCoord[0].size = TexCoord[0]->Size;
698 ctx->Array.TexCoordFunc[0]( IM->TexCoord[0] + VB_START,
699 TexCoord[0], start, n );
700 }
701
702 if (required & VERT_TEX1_ANY) {
703 IM->v.TexCoord[1].size = TexCoord[1]->Size;
704 ctx->Array.TexCoordFunc[1]( IM->TexCoord[1] + VB_START,
705 TexCoord[1], start, n );
706 }
707
708 VB->ObjPtr = &IM->v.Obj;
709 VB->NormalPtr = &IM->v.Normal;
710 VB->ColorPtr = &IM->v.Color;
711 VB->Color[0] = VB->Color[1] = VB->ColorPtr;
712 VB->IndexPtr = &IM->v.Index;
713 VB->EdgeFlagPtr = &IM->v.EdgeFlag;
714 VB->TexCoordPtr[0] = &IM->v.TexCoord[0];
715 VB->TexCoordPtr[1] = &IM->v.TexCoord[1];
716
717 VB->Flag = ctx->Array.Flag;
718 VB->OrFlag = ctx->Array.Flags;
719
720 VB->Start = IM->Start = VB_START;
721 count = VB->Count = IM->Count = VB_START + n;
722
723 #define RESET_VEC(v, t, s, c) (v.start = t v.data[s], v.count = c)
724
725 RESET_VEC(IM->v.Obj, (GLfloat *), VB_START, count);
726 RESET_VEC(IM->v.Normal, (GLfloat *), VB_START, count);
727 RESET_VEC(IM->v.TexCoord[0], (GLfloat *), VB_START, count);
728 RESET_VEC(IM->v.TexCoord[1], (GLfloat *), VB_START, count);
729 RESET_VEC(IM->v.Index, &, VB_START, count);
730 RESET_VEC(IM->v.Elt, &, VB_START, count);
731 RESET_VEC(IM->v.EdgeFlag, &, VB_START, count);
732 RESET_VEC(IM->v.Color, (GLubyte *), VB_START, count);
733 RESET_VEC(VB->Clip, (GLfloat *), VB_START, count);
734 RESET_VEC(VB->Eye, (GLfloat *), VB_START, count);
735 RESET_VEC(VB->Win, (GLfloat *), VB_START, count);
736 RESET_VEC(VB->BColor, (GLubyte *), VB_START, count);
737 RESET_VEC(VB->BIndex, &, VB_START, count);
738
739 VB->NextPrimitive[VB->CopyStart] = VB->Count;
740 VB->Primitive[VB->CopyStart] = mode;
741 ctx->Array.Flag[count] |= VERT_END_VB;
742
743 /* Transform and render.
744 */
745 gl_run_pipeline( VB );
746 gl_reset_vb( VB );
747
748 ctx->Array.Flag[count] = ctx->Array.Flags;
749 ctx->Array.Flag[VB_START] = ctx->Array.Flags;
750
751 start += n;
752 remaining -= n;
753 }
754
755 gl_reset_input( ctx );
756
757 if (relock) {
758 ctx->CompileCVAFlag = relock;
759 elt->pipeline_valid = 0;
760 }
761 }
762 else if (ctx->Array.Vertex.Enabled)
763 {
764 /* The GL_COMPILE and GL_COMPILE_AND_EXECUTE cases. These
765 * could be handled by the above code, but it gets a little
766 * complex. The generated list is still of good quality
767 * this way.
768 */
769 gl_Begin( ctx, mode );
770 for (i=0;i<count;i++) {
771 gl_ArrayElement( ctx, start+i );
772 }
773 gl_End( ctx );
774 }
775 else
776 {
777 /* The degenerate case where vertices are not enabled - only
778 * need to process the very final array element, as all of the
779 * preceding ones would be overwritten anyway.
780 */
781 gl_Begin( ctx, mode );
782 gl_ArrayElement( ctx, start+count );
783 gl_End( ctx );
784 }
785 }
786
787
788
789 /* KW: Exactly fakes the effects of calling glArrayElement multiple times.
790 * Compilation is handled via. the IM->maybe_transform_vb() callback.
791 */
792 #if 1
793 #define DRAW_ELT(FUNC, TYPE) \
794 static void FUNC( GLcontext *ctx, GLenum mode, \
795 TYPE *indices, GLuint count ) \
796 { \
797 GLuint i,j; \
798 \
799 gl_Begin( ctx, mode ); \
800 \
801 for (j = 0 ; j < count ; ) { \
802 struct immediate *IM = ctx->input; \
803 GLuint start = IM->Start; \
804 GLuint nr = MIN2( VB_MAX, count - j + start ); \
805 GLuint sf = IM->Flag[start]; \
806 IM->FlushElt |= IM->ArrayEltFlush; \
807 \
808 for (i = start ; i < nr ; i++) { \
809 IM->Elt[i] = (GLuint) *indices++; \
810 IM->Flag[i] = VERT_ELT; \
811 } \
812 \
813 if (j == 0) IM->Flag[start] |= sf; \
814 \
815 IM->Count = nr; \
816 j += nr - start; \
817 \
818 if (j == count) gl_End( ctx ); \
819 IM->maybe_transform_vb( IM ); \
820 } \
821 }
822 #else
823 #define DRAW_ELT(FUNC, TYPE) \
824 static void FUNC( GLcontext *ctx, GLenum mode, \
825 TYPE *indices, GLuint count ) \
826 { \
827 int i; \
828 glBegin(mode); \
829 for (i = 0 ; i < count ; i++) \
830 glArrayElement( indices[i] ); \
831 glEnd(); \
832 }
833 #endif
834
835
836 DRAW_ELT( draw_elt_ubyte, GLubyte )
837 DRAW_ELT( draw_elt_ushort, GLushort )
838 DRAW_ELT( draw_elt_uint, GLuint )
839
840
841 static GLuint natural_stride[0x10] =
842 {
843 sizeof(GLbyte), /* 0 */
844 sizeof(GLubyte), /* 1 */
845 sizeof(GLshort), /* 2 */
846 sizeof(GLushort), /* 3 */
847 sizeof(GLint), /* 4 */
848 sizeof(GLuint), /* 5 */
849 sizeof(GLfloat), /* 6 */
850 2 * sizeof(GLbyte), /* 7 */
851 3 * sizeof(GLbyte), /* 8 */
852 4 * sizeof(GLbyte), /* 9 */
853 sizeof(GLdouble), /* a */
854 0, /* b */
855 0, /* c */
856 0, /* d */
857 0, /* e */
858 0 /* f */
859 };
860
861
862 void
863 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
864 {
865 GET_CURRENT_CONTEXT(ctx);
866 struct gl_cva *cva;
867
868 cva = &ctx->CVA;
869 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawElements");
870
871 if (count <= 0) {
872 if (count < 0)
873 gl_error( ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
874 return;
875 }
876
877 if (mode < 0 || mode > GL_POLYGON) {
878 gl_error( ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
879 return;
880 }
881
882 if (type != GL_UNSIGNED_INT && type != GL_UNSIGNED_BYTE && type != GL_UNSIGNED_SHORT)
883 {
884 gl_error( ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
885 return;
886 }
887
888 if (ctx->NewState)
889 gl_update_state(ctx);
890
891 if (ctx->CompileCVAFlag)
892 {
893 #if defined(MESA_CVA_PROF)
894 force_init_prof();
895 #endif
896
897 /* Treat VERT_ELT like a special client array.
898 */
899 ctx->Array.NewArrayState |= VERT_ELT;
900 ctx->Array.Summary |= VERT_ELT;
901 ctx->Array.Flags |= VERT_ELT;
902
903 cva->elt_mode = mode;
904 cva->elt_count = count;
905 cva->Elt.Type = type;
906 cva->Elt.Ptr = (void *) indices;
907 cva->Elt.StrideB = natural_stride[TYPE_IDX(type)];
908 cva->EltFunc = gl_trans_1ui_tab[TYPE_IDX(type)];
909
910 if (!cva->pre.pipeline_valid)
911 gl_build_precalc_pipeline( ctx );
912 else if (MESA_VERBOSE & VERBOSE_PIPELINE)
913 fprintf(stderr, ": dont rebuild\n");
914
915 gl_cva_force_precalc( ctx );
916
917 /* Did we 'precalculate' the render op?
918 */
919 if (ctx->CVA.pre.ops & PIPE_OP_RENDER) {
920 ctx->Array.NewArrayState |= VERT_ELT;
921 ctx->Array.Summary &= ~VERT_ELT;
922 ctx->Array.Flags &= ~VERT_ELT;
923 return;
924 }
925
926 if ( (MESA_VERBOSE&VERBOSE_VARRAY) )
927 printf("using immediate\n");
928 }
929
930
931 /* Otherwise, have to use the immediate path to render.
932 */
933 switch (type) {
934 case GL_UNSIGNED_BYTE:
935 {
936 GLubyte *ub_indices = (GLubyte *) indices;
937 if (ctx->Array.Summary & VERT_OBJ_ANY) {
938 draw_elt_ubyte( ctx, mode, ub_indices, count );
939 } else {
940 gl_ArrayElement( ctx, (GLuint) ub_indices[count-1] );
941 }
942 }
943 break;
944 case GL_UNSIGNED_SHORT:
945 {
946 GLushort *us_indices = (GLushort *) indices;
947 if (ctx->Array.Summary & VERT_OBJ_ANY) {
948 draw_elt_ushort( ctx, mode, us_indices, count );
949 } else {
950 gl_ArrayElement( ctx, (GLuint) us_indices[count-1] );
951 }
952 }
953 break;
954 case GL_UNSIGNED_INT:
955 {
956 GLuint *ui_indices = (GLuint *) indices;
957 if (ctx->Array.Summary & VERT_OBJ_ANY) {
958 draw_elt_uint( ctx, mode, ui_indices, count );
959 } else {
960 gl_ArrayElement( ctx, ui_indices[count-1] );
961 }
962 }
963 break;
964 default:
965 gl_error( ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
966 break;
967 }
968
969 if (ctx->CompileCVAFlag) {
970 ctx->Array.NewArrayState |= VERT_ELT;
971 ctx->Array.Summary &= ~VERT_ELT;
972 }
973 }
974
975
976
977 void
978 _mesa_InterleavedArrays(GLenum format, GLsizei stride, const GLvoid *pointer)
979 {
980 GET_CURRENT_CONTEXT(ctx);
981 GLboolean tflag, cflag, nflag; /* enable/disable flags */
982 GLint tcomps, ccomps, vcomps; /* components per texcoord, color, vertex */
983
984 GLenum ctype; /* color type */
985 GLint coffset, noffset, voffset;/* color, normal, vertex offsets */
986 GLint defstride; /* default stride */
987 GLint c, f;
988 GLint coordUnitSave;
989
990 f = sizeof(GLfloat);
991 c = f * ((4*sizeof(GLubyte) + (f-1)) / f);
992
993 if (stride<0) {
994 gl_error( ctx, GL_INVALID_VALUE, "glInterleavedArrays(stride)" );
995 return;
996 }
997
998 switch (format) {
999 case GL_V2F:
1000 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
1001 tcomps = 0; ccomps = 0; vcomps = 2;
1002 voffset = 0;
1003 defstride = 2*f;
1004 break;
1005 case GL_V3F:
1006 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
1007 tcomps = 0; ccomps = 0; vcomps = 3;
1008 voffset = 0;
1009 defstride = 3*f;
1010 break;
1011 case GL_C4UB_V2F:
1012 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1013 tcomps = 0; ccomps = 4; vcomps = 2;
1014 ctype = GL_UNSIGNED_BYTE;
1015 coffset = 0;
1016 voffset = c;
1017 defstride = c + 2*f;
1018 break;
1019 case GL_C4UB_V3F:
1020 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1021 tcomps = 0; ccomps = 4; vcomps = 3;
1022 ctype = GL_UNSIGNED_BYTE;
1023 coffset = 0;
1024 voffset = c;
1025 defstride = c + 3*f;
1026 break;
1027 case GL_C3F_V3F:
1028 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
1029 tcomps = 0; ccomps = 3; vcomps = 3;
1030 ctype = GL_FLOAT;
1031 coffset = 0;
1032 voffset = 3*f;
1033 defstride = 6*f;
1034 break;
1035 case GL_N3F_V3F:
1036 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_TRUE;
1037 tcomps = 0; ccomps = 0; vcomps = 3;
1038 noffset = 0;
1039 voffset = 3*f;
1040 defstride = 6*f;
1041 break;
1042 case GL_C4F_N3F_V3F:
1043 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_TRUE;
1044 tcomps = 0; ccomps = 4; vcomps = 3;
1045 ctype = GL_FLOAT;
1046 coffset = 0;
1047 noffset = 4*f;
1048 voffset = 7*f;
1049 defstride = 10*f;
1050 break;
1051 case GL_T2F_V3F:
1052 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
1053 tcomps = 2; ccomps = 0; vcomps = 3;
1054 voffset = 2*f;
1055 defstride = 5*f;
1056 break;
1057 case GL_T4F_V4F:
1058 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
1059 tcomps = 4; ccomps = 0; vcomps = 4;
1060 voffset = 4*f;
1061 defstride = 8*f;
1062 break;
1063 case GL_T2F_C4UB_V3F:
1064 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1065 tcomps = 2; ccomps = 4; vcomps = 3;
1066 ctype = GL_UNSIGNED_BYTE;
1067 coffset = 2*f;
1068 voffset = c+2*f;
1069 defstride = c+5*f;
1070 break;
1071 case GL_T2F_C3F_V3F:
1072 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1073 tcomps = 2; ccomps = 3; vcomps = 3;
1074 ctype = GL_FLOAT;
1075 coffset = 2*f;
1076 voffset = 5*f;
1077 defstride = 8*f;
1078 break;
1079 case GL_T2F_N3F_V3F:
1080 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_TRUE;
1081 tcomps = 2; ccomps = 0; vcomps = 3;
1082 noffset = 2*f;
1083 voffset = 5*f;
1084 defstride = 8*f;
1085 break;
1086 case GL_T2F_C4F_N3F_V3F:
1087 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1088 tcomps = 2; ccomps = 4; vcomps = 3;
1089 ctype = GL_FLOAT;
1090 coffset = 2*f;
1091 noffset = 6*f;
1092 voffset = 9*f;
1093 defstride = 12*f;
1094 break;
1095 case GL_T4F_C4F_N3F_V4F:
1096 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1097 tcomps = 4; ccomps = 4; vcomps = 4;
1098 ctype = GL_FLOAT;
1099 coffset = 4*f;
1100 noffset = 8*f;
1101 voffset = 11*f;
1102 defstride = 15*f;
1103 break;
1104 default:
1105 gl_error( ctx, GL_INVALID_ENUM, "glInterleavedArrays(format)" );
1106 return;
1107 }
1108
1109 if (stride==0) {
1110 stride = defstride;
1111 }
1112
1113 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY );
1114 _mesa_DisableClientState( GL_INDEX_ARRAY );
1115
1116 /* Texcoords */
1117 coordUnitSave = ctx->Array.ActiveTexture;
1118 if (tflag) {
1119 GLint i;
1120 GLint factor = ctx->Array.TexCoordInterleaveFactor;
1121 for (i = 0; i < factor; i++) {
1122 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1123 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY );
1124 glTexCoordPointer( tcomps, GL_FLOAT, stride,
1125 (GLubyte *) pointer + i * coffset );
1126 }
1127 for (i = factor; i < ctx->Const.MaxTextureUnits; i++) {
1128 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1129 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
1130 }
1131 }
1132 else {
1133 GLint i;
1134 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1135 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1136 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
1137 }
1138 }
1139 /* Restore texture coordinate unit index */
1140 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + coordUnitSave) );
1141
1142
1143 /* Color */
1144 if (cflag) {
1145 _mesa_EnableClientState( GL_COLOR_ARRAY );
1146 glColorPointer( ccomps, ctype, stride,
1147 (GLubyte*) pointer + coffset );
1148 }
1149 else {
1150 _mesa_DisableClientState( GL_COLOR_ARRAY );
1151 }
1152
1153
1154 /* Normals */
1155 if (nflag) {
1156 _mesa_EnableClientState( GL_NORMAL_ARRAY );
1157 glNormalPointer( GL_FLOAT, stride,
1158 (GLubyte*) pointer + noffset );
1159 }
1160 else {
1161 _mesa_DisableClientState( GL_NORMAL_ARRAY );
1162 }
1163
1164 _mesa_EnableClientState( GL_VERTEX_ARRAY );
1165 glVertexPointer( vcomps, GL_FLOAT, stride,
1166 (GLubyte *) pointer + voffset );
1167 }
1168
1169
1170
1171 void
1172 _mesa_DrawRangeElements(GLenum mode, GLuint start,
1173 GLuint end, GLsizei count,
1174 GLenum type, const GLvoid *indices)
1175 {
1176 GET_CURRENT_CONTEXT(ctx);
1177
1178 if (end < start) {
1179 gl_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements( end < start )");
1180 return;
1181 }
1182
1183 #if 0
1184 /*
1185 * XXX something in locked arrays is broken! If start = 0,
1186 * end = 1 and count = 2 we'll take the LockArrays path and
1187 * get incorrect results. See Scott McMillan's bug of 3 Jan 2000.
1188 * For now, don't use locked arrays.
1189 */
1190 if (!ctx->Array.LockCount && 2*count > (GLint) 3*(end-start)) {
1191 glLockArraysEXT( start, end );
1192 glDrawElements( mode, count, type, indices );
1193 glUnlockArraysEXT();
1194 } else {
1195 glDrawElements( mode, count, type, indices );
1196 }
1197 #else
1198 glDrawElements( mode, count, type, indices );
1199 #endif
1200 }
1201
1202
1203
1204 void gl_update_client_state( GLcontext *ctx )
1205 {
1206 static GLuint sz_flags[5] = { 0,
1207 0,
1208 VERT_OBJ_2,
1209 VERT_OBJ_23,
1210 VERT_OBJ_234 };
1211
1212 static GLuint tc_flags[5] = { 0,
1213 VERT_TEX0_1,
1214 VERT_TEX0_12,
1215 VERT_TEX0_123,
1216 VERT_TEX0_1234 };
1217
1218 ctx->Array.Flags = 0;
1219 ctx->Array.Summary = 0;
1220 ctx->input->ArrayIncr = 0;
1221
1222 if (ctx->Array.Normal.Enabled) ctx->Array.Flags |= VERT_NORM;
1223 if (ctx->Array.Color.Enabled) ctx->Array.Flags |= VERT_RGBA;
1224 if (ctx->Array.Index.Enabled) ctx->Array.Flags |= VERT_INDEX;
1225 if (ctx->Array.EdgeFlag.Enabled) ctx->Array.Flags |= VERT_EDGE;
1226 if (ctx->Array.Vertex.Enabled) {
1227 ctx->Array.Flags |= sz_flags[ctx->Array.Vertex.Size];
1228 ctx->input->ArrayIncr = 1;
1229 }
1230 if (ctx->Array.TexCoord[0].Enabled) {
1231 ctx->Array.Flags |= tc_flags[ctx->Array.TexCoord[0].Size];
1232 }
1233 if (ctx->Array.TexCoord[1].Enabled) {
1234 ctx->Array.Flags |= (tc_flags[ctx->Array.TexCoord[1].Size] << NR_TEXSIZE_BITS);
1235 }
1236
1237 /* Not really important any more:
1238 */
1239 ctx->Array.Summary = ctx->Array.Flags & VERT_DATA;
1240 ctx->input->ArrayAndFlags = ~ctx->Array.Flags;
1241 ctx->input->ArrayEltFlush = !(ctx->CompileCVAFlag);
1242 }
1243