Fog coordinate stage which drivers may use to replace standard fogging
[mesa.git] / src / mesa / main / varray.c
1 /* $Id: varray.c,v 1.21 2000/02/25 03:55:40 keithw 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 ctx->Array.Flag[count] |= VERT_END_VB;
685
686 /* Transform and render.
687 */
688 gl_run_pipeline( VB );
689 gl_reset_vb( VB );
690
691 ctx->Array.Flag[count] = ctx->Array.Flags;
692 ctx->Array.Flag[VB_START] = ctx->Array.Flags;
693
694 start += n;
695 remaining -= n;
696 }
697
698 gl_reset_input( ctx );
699
700 if (relock) {
701 ctx->CompileCVAFlag = relock;
702 elt->pipeline_valid = 0;
703 }
704 }
705 else if (ctx->Array.Vertex.Enabled)
706 {
707 /* The GL_COMPILE and GL_COMPILE_AND_EXECUTE cases. These
708 * could be handled by the above code, but it gets a little
709 * complex. The generated list is still of good quality
710 * this way.
711 */
712 gl_Begin( ctx, mode );
713 for (i=0;i<count;i++) {
714 gl_ArrayElement( ctx, start+i );
715 }
716 gl_End( ctx );
717 }
718 else
719 {
720 /* The degenerate case where vertices are not enabled - only
721 * need to process the very final array element, as all of the
722 * preceding ones would be overwritten anyway.
723 */
724 gl_Begin( ctx, mode );
725 gl_ArrayElement( ctx, start+count );
726 gl_End( ctx );
727 }
728 }
729
730
731
732 /* KW: Exactly fakes the effects of calling glArrayElement multiple times.
733 * Compilation is handled via. the IM->maybe_transform_vb() callback.
734 */
735 #if 1
736 #define DRAW_ELT(FUNC, TYPE) \
737 static void FUNC( GLcontext *ctx, GLenum mode, \
738 TYPE *indices, GLuint count ) \
739 { \
740 GLuint i,j; \
741 \
742 gl_Begin( ctx, mode ); \
743 \
744 for (j = 0 ; j < count ; ) { \
745 struct immediate *IM = ctx->input; \
746 GLuint start = IM->Start; \
747 GLuint nr = MIN2( VB_MAX, count - j + start ); \
748 GLuint sf = IM->Flag[start]; \
749 IM->FlushElt |= IM->ArrayEltFlush; \
750 \
751 for (i = start ; i < nr ; i++) { \
752 IM->Elt[i] = (GLuint) *indices++; \
753 IM->Flag[i] = VERT_ELT; \
754 } \
755 \
756 if (j == 0) IM->Flag[start] |= sf; \
757 \
758 IM->Count = nr; \
759 j += nr - start; \
760 \
761 if (j == count) gl_End( ctx ); \
762 IM->maybe_transform_vb( IM ); \
763 } \
764 }
765 #else
766 #define DRAW_ELT(FUNC, TYPE) \
767 static void FUNC( GLcontext *ctx, GLenum mode, \
768 TYPE *indices, GLuint count ) \
769 { \
770 int i; \
771 glBegin(mode); \
772 for (i = 0 ; i < count ; i++) \
773 glArrayElement( indices[i] ); \
774 glEnd(); \
775 }
776 #endif
777
778
779 DRAW_ELT( draw_elt_ubyte, GLubyte )
780 DRAW_ELT( draw_elt_ushort, GLushort )
781 DRAW_ELT( draw_elt_uint, GLuint )
782
783
784 static GLuint natural_stride[0x10] =
785 {
786 sizeof(GLbyte), /* 0 */
787 sizeof(GLubyte), /* 1 */
788 sizeof(GLshort), /* 2 */
789 sizeof(GLushort), /* 3 */
790 sizeof(GLint), /* 4 */
791 sizeof(GLuint), /* 5 */
792 sizeof(GLfloat), /* 6 */
793 2 * sizeof(GLbyte), /* 7 */
794 3 * sizeof(GLbyte), /* 8 */
795 4 * sizeof(GLbyte), /* 9 */
796 sizeof(GLdouble), /* a */
797 0, /* b */
798 0, /* c */
799 0, /* d */
800 0, /* e */
801 0 /* f */
802 };
803
804
805 void
806 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
807 {
808 GET_CURRENT_CONTEXT(ctx);
809 struct gl_cva *cva;
810
811 cva = &ctx->CVA;
812 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawElements");
813
814 if (count <= 0) {
815 if (count < 0)
816 gl_error( ctx, GL_INVALID_VALUE, "glDrawElements(count)" );
817 return;
818 }
819
820 if (mode < 0 || mode > GL_POLYGON) {
821 gl_error( ctx, GL_INVALID_ENUM, "glDrawArrays(mode)" );
822 return;
823 }
824
825 if (type != GL_UNSIGNED_INT && type != GL_UNSIGNED_BYTE && type != GL_UNSIGNED_SHORT)
826 {
827 gl_error( ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
828 return;
829 }
830
831 if (ctx->NewState)
832 gl_update_state(ctx);
833
834 if (ctx->CompileCVAFlag)
835 {
836 #if defined(MESA_CVA_PROF)
837 force_init_prof();
838 #endif
839
840 /* Treat VERT_ELT like a special client array.
841 */
842 ctx->Array.NewArrayState |= VERT_ELT;
843 ctx->Array.Summary |= VERT_ELT;
844 ctx->Array.Flags |= VERT_ELT;
845
846 cva->elt_mode = mode;
847 cva->elt_count = count;
848 cva->Elt.Type = type;
849 cva->Elt.Ptr = (void *) indices;
850 cva->Elt.StrideB = natural_stride[TYPE_IDX(type)];
851 cva->EltFunc = gl_trans_1ui_tab[TYPE_IDX(type)];
852
853 if (!cva->pre.pipeline_valid)
854 gl_build_precalc_pipeline( ctx );
855 else if (MESA_VERBOSE & VERBOSE_PIPELINE)
856 fprintf(stderr, ": dont rebuild\n");
857
858 gl_cva_force_precalc( ctx );
859
860 /* Did we 'precalculate' the render op?
861 */
862 if (ctx->CVA.pre.ops & PIPE_OP_RENDER) {
863 ctx->Array.NewArrayState |= VERT_ELT;
864 ctx->Array.Summary &= ~VERT_ELT;
865 ctx->Array.Flags &= ~VERT_ELT;
866 return;
867 }
868
869 if ( (MESA_VERBOSE&VERBOSE_VARRAY) )
870 printf("using immediate\n");
871 }
872
873
874 /* Otherwise, have to use the immediate path to render.
875 */
876 switch (type) {
877 case GL_UNSIGNED_BYTE:
878 {
879 GLubyte *ub_indices = (GLubyte *) indices;
880 if (ctx->Array.Summary & VERT_OBJ_ANY) {
881 draw_elt_ubyte( ctx, mode, ub_indices, count );
882 } else {
883 gl_ArrayElement( ctx, (GLuint) ub_indices[count-1] );
884 }
885 }
886 break;
887 case GL_UNSIGNED_SHORT:
888 {
889 GLushort *us_indices = (GLushort *) indices;
890 if (ctx->Array.Summary & VERT_OBJ_ANY) {
891 draw_elt_ushort( ctx, mode, us_indices, count );
892 } else {
893 gl_ArrayElement( ctx, (GLuint) us_indices[count-1] );
894 }
895 }
896 break;
897 case GL_UNSIGNED_INT:
898 {
899 GLuint *ui_indices = (GLuint *) indices;
900 if (ctx->Array.Summary & VERT_OBJ_ANY) {
901 draw_elt_uint( ctx, mode, ui_indices, count );
902 } else {
903 gl_ArrayElement( ctx, ui_indices[count-1] );
904 }
905 }
906 break;
907 default:
908 gl_error( ctx, GL_INVALID_ENUM, "glDrawElements(type)" );
909 break;
910 }
911
912 if (ctx->CompileCVAFlag) {
913 ctx->Array.NewArrayState |= VERT_ELT;
914 ctx->Array.Summary &= ~VERT_ELT;
915 }
916 }
917
918
919
920 void
921 _mesa_InterleavedArrays(GLenum format, GLsizei stride, const GLvoid *pointer)
922 {
923 GET_CURRENT_CONTEXT(ctx);
924 GLboolean tflag, cflag, nflag; /* enable/disable flags */
925 GLint tcomps, ccomps, vcomps; /* components per texcoord, color, vertex */
926
927 GLenum ctype; /* color type */
928 GLint coffset, noffset, voffset;/* color, normal, vertex offsets */
929 GLint defstride; /* default stride */
930 GLint c, f;
931 GLint coordUnitSave;
932
933 f = sizeof(GLfloat);
934 c = f * ((4*sizeof(GLubyte) + (f-1)) / f);
935
936 if (stride<0) {
937 gl_error( ctx, GL_INVALID_VALUE, "glInterleavedArrays(stride)" );
938 return;
939 }
940
941 switch (format) {
942 case GL_V2F:
943 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
944 tcomps = 0; ccomps = 0; vcomps = 2;
945 voffset = 0;
946 defstride = 2*f;
947 break;
948 case GL_V3F:
949 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_FALSE;
950 tcomps = 0; ccomps = 0; vcomps = 3;
951 voffset = 0;
952 defstride = 3*f;
953 break;
954 case GL_C4UB_V2F:
955 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
956 tcomps = 0; ccomps = 4; vcomps = 2;
957 ctype = GL_UNSIGNED_BYTE;
958 coffset = 0;
959 voffset = c;
960 defstride = c + 2*f;
961 break;
962 case GL_C4UB_V3F:
963 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
964 tcomps = 0; ccomps = 4; vcomps = 3;
965 ctype = GL_UNSIGNED_BYTE;
966 coffset = 0;
967 voffset = c;
968 defstride = c + 3*f;
969 break;
970 case GL_C3F_V3F:
971 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_FALSE;
972 tcomps = 0; ccomps = 3; vcomps = 3;
973 ctype = GL_FLOAT;
974 coffset = 0;
975 voffset = 3*f;
976 defstride = 6*f;
977 break;
978 case GL_N3F_V3F:
979 tflag = GL_FALSE; cflag = GL_FALSE; nflag = GL_TRUE;
980 tcomps = 0; ccomps = 0; vcomps = 3;
981 noffset = 0;
982 voffset = 3*f;
983 defstride = 6*f;
984 break;
985 case GL_C4F_N3F_V3F:
986 tflag = GL_FALSE; cflag = GL_TRUE; nflag = GL_TRUE;
987 tcomps = 0; ccomps = 4; vcomps = 3;
988 ctype = GL_FLOAT;
989 coffset = 0;
990 noffset = 4*f;
991 voffset = 7*f;
992 defstride = 10*f;
993 break;
994 case GL_T2F_V3F:
995 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
996 tcomps = 2; ccomps = 0; vcomps = 3;
997 voffset = 2*f;
998 defstride = 5*f;
999 break;
1000 case GL_T4F_V4F:
1001 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_FALSE;
1002 tcomps = 4; ccomps = 0; vcomps = 4;
1003 voffset = 4*f;
1004 defstride = 8*f;
1005 break;
1006 case GL_T2F_C4UB_V3F:
1007 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1008 tcomps = 2; ccomps = 4; vcomps = 3;
1009 ctype = GL_UNSIGNED_BYTE;
1010 coffset = 2*f;
1011 voffset = c+2*f;
1012 defstride = c+5*f;
1013 break;
1014 case GL_T2F_C3F_V3F:
1015 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_FALSE;
1016 tcomps = 2; ccomps = 3; vcomps = 3;
1017 ctype = GL_FLOAT;
1018 coffset = 2*f;
1019 voffset = 5*f;
1020 defstride = 8*f;
1021 break;
1022 case GL_T2F_N3F_V3F:
1023 tflag = GL_TRUE; cflag = GL_FALSE; nflag = GL_TRUE;
1024 tcomps = 2; ccomps = 0; vcomps = 3;
1025 noffset = 2*f;
1026 voffset = 5*f;
1027 defstride = 8*f;
1028 break;
1029 case GL_T2F_C4F_N3F_V3F:
1030 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1031 tcomps = 2; ccomps = 4; vcomps = 3;
1032 ctype = GL_FLOAT;
1033 coffset = 2*f;
1034 noffset = 6*f;
1035 voffset = 9*f;
1036 defstride = 12*f;
1037 break;
1038 case GL_T4F_C4F_N3F_V4F:
1039 tflag = GL_TRUE; cflag = GL_TRUE; nflag = GL_TRUE;
1040 tcomps = 4; ccomps = 4; vcomps = 4;
1041 ctype = GL_FLOAT;
1042 coffset = 4*f;
1043 noffset = 8*f;
1044 voffset = 11*f;
1045 defstride = 15*f;
1046 break;
1047 default:
1048 gl_error( ctx, GL_INVALID_ENUM, "glInterleavedArrays(format)" );
1049 return;
1050 }
1051
1052 if (stride==0) {
1053 stride = defstride;
1054 }
1055
1056 _mesa_DisableClientState( GL_EDGE_FLAG_ARRAY );
1057 _mesa_DisableClientState( GL_INDEX_ARRAY );
1058
1059 /* Texcoords */
1060 coordUnitSave = ctx->Array.ActiveTexture;
1061 if (tflag) {
1062 GLint i;
1063 GLint factor = ctx->Array.TexCoordInterleaveFactor;
1064 for (i = 0; i < factor; i++) {
1065 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1066 _mesa_EnableClientState( GL_TEXTURE_COORD_ARRAY );
1067 glTexCoordPointer( tcomps, GL_FLOAT, stride,
1068 (GLubyte *) pointer + i * coffset );
1069 }
1070 for (i = factor; i < ctx->Const.MaxTextureUnits; i++) {
1071 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1072 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
1073 }
1074 }
1075 else {
1076 GLint i;
1077 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1078 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + i) );
1079 _mesa_DisableClientState( GL_TEXTURE_COORD_ARRAY );
1080 }
1081 }
1082 /* Restore texture coordinate unit index */
1083 _mesa_ClientActiveTextureARB( (GLenum) (GL_TEXTURE0_ARB + coordUnitSave) );
1084
1085
1086 /* Color */
1087 if (cflag) {
1088 _mesa_EnableClientState( GL_COLOR_ARRAY );
1089 glColorPointer( ccomps, ctype, stride,
1090 (GLubyte*) pointer + coffset );
1091 }
1092 else {
1093 _mesa_DisableClientState( GL_COLOR_ARRAY );
1094 }
1095
1096
1097 /* Normals */
1098 if (nflag) {
1099 _mesa_EnableClientState( GL_NORMAL_ARRAY );
1100 glNormalPointer( GL_FLOAT, stride,
1101 (GLubyte*) pointer + noffset );
1102 }
1103 else {
1104 _mesa_DisableClientState( GL_NORMAL_ARRAY );
1105 }
1106
1107 _mesa_EnableClientState( GL_VERTEX_ARRAY );
1108 glVertexPointer( vcomps, GL_FLOAT, stride,
1109 (GLubyte *) pointer + voffset );
1110 }
1111
1112
1113
1114 void
1115 _mesa_DrawRangeElements(GLenum mode, GLuint start,
1116 GLuint end, GLsizei count,
1117 GLenum type, const GLvoid *indices)
1118 {
1119 GET_CURRENT_CONTEXT(ctx);
1120
1121 if (end < start) {
1122 gl_error(ctx, GL_INVALID_VALUE, "glDrawRangeElements( end < start )");
1123 return;
1124 }
1125
1126 #if 0
1127 /*
1128 * XXX something in locked arrays is broken! If start = 0,
1129 * end = 1 and count = 2 we'll take the LockArrays path and
1130 * get incorrect results. See Scott McMillan's bug of 3 Jan 2000.
1131 * For now, don't use locked arrays.
1132 */
1133 if (!ctx->Array.LockCount && 2*count > (GLint) 3*(end-start)) {
1134 glLockArraysEXT( start, end );
1135 glDrawElements( mode, count, type, indices );
1136 glUnlockArraysEXT();
1137 } else {
1138 glDrawElements( mode, count, type, indices );
1139 }
1140 #else
1141 glDrawElements( mode, count, type, indices );
1142 #endif
1143 }
1144
1145
1146
1147 void gl_update_client_state( GLcontext *ctx )
1148 {
1149 static GLuint sz_flags[5] = { 0,
1150 0,
1151 VERT_OBJ_2,
1152 VERT_OBJ_23,
1153 VERT_OBJ_234 };
1154
1155 static GLuint tc_flags[5] = { 0,
1156 VERT_TEX0_1,
1157 VERT_TEX0_12,
1158 VERT_TEX0_123,
1159 VERT_TEX0_1234 };
1160
1161 ctx->Array.Flags = 0;
1162 ctx->Array.Summary = 0;
1163 ctx->input->ArrayIncr = 0;
1164
1165 if (ctx->Array.Normal.Enabled) ctx->Array.Flags |= VERT_NORM;
1166 if (ctx->Array.Color.Enabled) ctx->Array.Flags |= VERT_RGBA;
1167 if (ctx->Array.Index.Enabled) ctx->Array.Flags |= VERT_INDEX;
1168 if (ctx->Array.EdgeFlag.Enabled) ctx->Array.Flags |= VERT_EDGE;
1169 if (ctx->Array.Vertex.Enabled) {
1170 ctx->Array.Flags |= sz_flags[ctx->Array.Vertex.Size];
1171 ctx->input->ArrayIncr = 1;
1172 }
1173 if (ctx->Array.TexCoord[0].Enabled) {
1174 ctx->Array.Flags |= tc_flags[ctx->Array.TexCoord[0].Size];
1175 }
1176 if (ctx->Array.TexCoord[1].Enabled) {
1177 ctx->Array.Flags |= (tc_flags[ctx->Array.TexCoord[1].Size] << NR_TEXSIZE_BITS);
1178 }
1179
1180 /* Not really important any more:
1181 */
1182 ctx->Array.Summary = ctx->Array.Flags & VERT_DATA;
1183 ctx->input->ArrayAndFlags = ~ctx->Array.Flags;
1184 ctx->input->ArrayEltFlush = !(ctx->CompileCVAFlag);
1185 }
1186