fragment program execution
[mesa.git] / src / mesa / tnl / t_imm_api.c
index 48a8081c16a57fb25e6a91d0d0ed0e4fb5e190e6..462d8dbaf6e38f44b284c5d06ef7bacc7d05a51c 100644 (file)
@@ -1,10 +1,10 @@
-/* $Id: t_imm_api.c,v 1.23 2002/01/05 20:51:13 brianp Exp $ */
+/* $Id: t_imm_api.c,v 1.39 2003/01/14 04:55:47 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  4.1
+ * Version:  5.1
  *
- * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -24,7 +24,7 @@
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  *
  * Authors:
- *    Keith Whitwell <keithw@valinux.com>
+ *    Keith Whitwell <keith@tungstengraphics.com>
  */
 
 
@@ -34,7 +34,7 @@
 #include "dlist.h"
 #include "enums.h"
 #include "light.h"
-#include "mem.h"
+#include "imports.h"
 #include "state.h"
 #include "colormac.h"
 #include "macros.h"
 
 /* A cassette is full or flushed on a statechange.
  */
-void _tnl_flush_immediate( struct immediate *IM )
+void _tnl_flush_immediate( GLcontext *ctx, struct immediate *IM )
 {
-   GLcontext *ctx = IM->backref;
+   if (!ctx) {
+      /* We were called by glVertex, glEvalCoord, glArrayElement, etc.
+       * The current context is corresponds to the IM structure.
+       */
+      GET_CURRENT_CONTEXT(context);
+      ctx = context;
+   }
+
+   if (MESA_VERBOSE & VERBOSE_IMMEDIATE)
+      _mesa_debug(ctx, "_tnl_flush_immediate IM: %d compiling: %d\n",
+                  IM->id, ctx->CompileFlag);
 
    if (IM->FlushElt == FLUSH_ELT_EAGER) {
       _tnl_translate_array_elts( ctx, IM, IM->LastPrimitive, IM->Count );
@@ -69,45 +79,63 @@ void _tnl_flush_immediate( struct immediate *IM )
 }
 
 
+/* Hook for ctx->Driver.FlushVertices:
+ */
 void _tnl_flush_vertices( GLcontext *ctx, GLuint flags )
 {
    struct immediate *IM = TNL_CURRENT_IM(ctx);
 
-   if (IM->Flag[IM->Start])
-      if ((flags & FLUSH_UPDATE_CURRENT) || IM->Count > IM->Start)
-        _tnl_flush_immediate( IM );
+   if (MESA_VERBOSE & VERBOSE_IMMEDIATE)
+      _mesa_debug(ctx,
+                  "_tnl_flush_vertices flags %x IM(%d) %d..%d Flag[%d]: %x\n", 
+                  flags, IM->id, IM->Start, IM->Count, IM->Start,
+                  IM->Flag[IM->Start]);
+
+   if (IM->Flag[IM->Start]) {
+      if ((flags & FLUSH_UPDATE_CURRENT) || 
+         IM->Count > IM->Start ||
+         (IM->Flag[IM->Start] & (VERT_BIT_BEGIN | VERT_BIT_END))) {
+        _tnl_flush_immediate( ctx, IM );
+      }
+   }
 }
 
 
-
-
-/* Note the ctx argument.  This function called only by _tnl_Begin,
- * _tnl_save_Begin and _tnl_hard_begin() in this file.  
- */
-static void
-_tnl_begin( GLcontext *ctx, GLenum p )
+void
+_tnl_save_Begin( GLenum mode )
 {
+   GET_CURRENT_CONTEXT(ctx);
    struct immediate *IM = TNL_CURRENT_IM(ctx);
    GLuint inflags, state;
 
-   if (MESA_VERBOSE&VERBOSE_API)
-      fprintf(stderr, "glBegin(IM %d) %s\n", IM->id, 
-             _mesa_lookup_enum_by_nr(p));
+/*     _mesa_debug(ctx, "%s: before: %x\n", __FUNCTION__, IM->BeginState); */
+
+   if (mode > GL_POLYGON) {
+      _mesa_compile_error( ctx, GL_INVALID_ENUM, "_tnl_Begin" );
+      return;
+   }
 
    if (ctx->NewState)
       _mesa_update_state(ctx);
 
+#if 000
    /* if only a very few slots left, might as well flush now
     */
    if (IM->Count > IMM_MAXDATA-8) {
-      _tnl_flush_immediate( IM );
+      _tnl_flush_immediate( ctx, IM );
+      IM = TNL_CURRENT_IM(ctx);
+   }
+#endif
+
+   if (IM->Count > IMM_MAXDATA-8) {
+      _tnl_flush_immediate( ctx, IM );
       IM = TNL_CURRENT_IM(ctx);
    }
 
    /* Check for and flush buffered vertices from internal operations.
     */
    if (IM->SavedBeginState) {
-      _tnl_flush_immediate( IM );
+      _tnl_flush_immediate( ctx, IM );
       IM = TNL_CURRENT_IM(ctx);
       IM->BeginState = IM->SavedBeginState;
       IM->SavedBeginState = 0;
@@ -123,8 +151,8 @@ _tnl_begin( GLcontext *ctx, GLenum p )
       GLuint last = IM->LastPrimitive;
 
       state |= (VERT_BEGIN_0|VERT_BEGIN_1);
-      IM->Flag[count] |= VERT_BEGIN;
-      IM->Primitive[count] = p | PRIM_BEGIN;
+      IM->Flag[count] |= VERT_BIT_BEGIN;
+      IM->Primitive[count] = mode | PRIM_BEGIN;
       IM->PrimitiveLength[IM->LastPrimitive] = count - IM->LastPrimitive;
       IM->LastPrimitive = count;
 
@@ -139,22 +167,10 @@ _tnl_begin( GLcontext *ctx, GLenum p )
 
    ctx->Driver.NeedFlush |= FLUSH_STORED_VERTICES;
    IM->BeginState = state;
-}
-
 
-void
-_tnl_save_Begin( GLenum mode )
-{
-   GET_CURRENT_CONTEXT(ctx);
-
-   if (mode > GL_POLYGON) {
-      _mesa_compile_error( ctx, GL_INVALID_ENUM, "glBegin" );
-      return;
-   }
-
-   _tnl_begin( ctx, mode );
-
-   /* Update save_primitive now.
+   /* Update save_primitive now.  Don't touch ExecPrimitive as this is
+    * updated in the replay of this cassette if we are in
+    * COMPILE_AND_EXECUTE mode.
     */
    if (ctx->Driver.CurrentSavePrimitive == PRIM_UNKNOWN)
       ctx->Driver.CurrentSavePrimitive = PRIM_INSIDE_UNKNOWN_PRIM;
@@ -167,18 +183,62 @@ void
 _tnl_Begin( GLenum mode )
 {
    GET_CURRENT_CONTEXT(ctx);
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   ASSERT (!ctx->CompileFlag);
 
    if (mode > GL_POLYGON) {
-      _mesa_compile_error( ctx, GL_INVALID_ENUM, "glBegin" );
+      _mesa_error( ctx, GL_INVALID_ENUM, "_tnl_Begin(0x%x)", mode );
       return;
    }
 
-   _tnl_begin(ctx, mode);
+   if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END) {
+      _mesa_error( ctx, GL_INVALID_OPERATION, "_tnl_Begin" );
+      return;
+   }
 
-   /* Update exec_primitive now.
-    */
-   ASSERT (!ctx->CompileFlag);
-   if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
+   if (ctx->NewState)
+      _mesa_update_state(ctx);
+
+   {
+      struct immediate *IM = TNL_CURRENT_IM(ctx);
+      if (IM->Count > IMM_MAXDATA-8) {
+        _tnl_flush_immediate( ctx, IM );
+        IM = TNL_CURRENT_IM(ctx);
+      }
+   }
+
+
+   {
+      struct immediate *IM = TNL_CURRENT_IM(ctx);
+      GLuint count = IM->Count;
+      GLuint last = IM->LastPrimitive;
+
+      if (IM->Start == IM->Count &&
+         tnl->Driver.NotifyBegin &&
+         tnl->Driver.NotifyBegin( ctx, mode )) {
+        return;
+      }
+
+      assert( (IM->SavedBeginState & (VERT_BEGIN_0|VERT_BEGIN_1)) == 0 );
+      assert( (IM->BeginState & (VERT_BEGIN_0|VERT_BEGIN_1)) == 0 );
+
+      /* Not quite right.  Need to use the fallback '_aa_ArrayElement'
+       * when not known to be inside begin/end and arrays are
+       * unlocked.  
+       */
+      if (IM->FlushElt == FLUSH_ELT_EAGER) {
+        _tnl_translate_array_elts( ctx, IM, last, count );
+      }
+
+      IM->Flag[count] |= VERT_BIT_BEGIN;
+      IM->Primitive[count] = mode | PRIM_BEGIN;
+      IM->PrimitiveLength[last] = count - last;
+      IM->LastPrimitive = count;
+      IM->BeginState = (VERT_BEGIN_0|VERT_BEGIN_1);
+
+/*        _mesa_debug(ctx, "%s: %x\n", __FUNCTION__, IM->BeginState);  */
+
+      ctx->Driver.NeedFlush |= FLUSH_STORED_VERTICES;
       ctx->Driver.CurrentExecPrimitive = mode;
    }
 }
@@ -191,12 +251,13 @@ _tnl_Begin( GLenum mode )
 GLboolean
 _tnl_hard_begin( GLcontext *ctx, GLenum p )
 {
+/*     _mesa_debug(ctx, "%s\n", __FUNCTION__); */
+
    if (!ctx->CompileFlag) {
       /* If not compiling, treat as a normal begin().
        */
-      _tnl_begin( ctx, p );
-      ASSERT(ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END);
-      ctx->Driver.CurrentExecPrimitive = p;
+/*        _mesa_debug(ctx, "%s: treating as glBegin\n", __FUNCTION__); */
+      glBegin( p );
       return GL_TRUE;
    }
    else {
@@ -210,7 +271,7 @@ _tnl_hard_begin( GLcontext *ctx, GLenum p )
         _mesa_update_state(ctx);
 
       if (IM->Count > IMM_MAXDATA-8) {
-        _tnl_flush_immediate( IM );
+        _tnl_flush_immediate( ctx, IM );
         IM = TNL_CURRENT_IM(ctx);
       }
 
@@ -249,7 +310,7 @@ _tnl_hard_begin( GLcontext *ctx, GLenum p )
         ASSERT (IM->FlushElt != FLUSH_ELT_EAGER);
 
         IM->BeginState |= VERT_BEGIN_0|VERT_BEGIN_1;
-        IM->Flag[IM->Count] |= VERT_BEGIN;
+        IM->Flag[IM->Count] |= VERT_BIT_BEGIN;
         IM->Primitive[IM->Count] = p | PRIM_BEGIN;
         IM->PrimitiveLength[IM->LastPrimitive] = IM->Count - IM->LastPrimitive;
         IM->LastPrimitive = IM->Count;
@@ -262,7 +323,7 @@ _tnl_hard_begin( GLcontext *ctx, GLenum p )
         return GL_TRUE;
 
       default:
-        ASSERT (0);
+        assert (0);
         return GL_TRUE;
       }
    }
@@ -285,6 +346,11 @@ _tnl_end( GLcontext *ctx )
    GLuint state = IM->BeginState;
    GLuint inflags = (~state) & (VERT_BEGIN_0|VERT_BEGIN_1);
 
+   /* Not the case if vertices emitted without calling glBegin first:
+    */
+/*   assert( ctx->Driver.NeedFlush & FLUSH_STORED_VERTICES ); */
+
+
    state |= inflags << 2;      /* errors */
 
    if (inflags != (VERT_BEGIN_0|VERT_BEGIN_1))
@@ -293,7 +359,7 @@ _tnl_end( GLcontext *ctx )
       GLuint last = IM->LastPrimitive;
 
       state &= ~(VERT_BEGIN_0|VERT_BEGIN_1); /* update state */
-      IM->Flag[count] |= VERT_END;
+      IM->Flag[count] |= VERT_BIT_END;
       IM->Primitive[last] |= PRIM_END;
       IM->PrimitiveLength[last] = count - last;
       IM->Primitive[count] = PRIM_OUTSIDE_BEGIN_END; /* removes PRIM_BEGIN 
@@ -309,20 +375,24 @@ _tnl_end( GLcontext *ctx )
    IM->BeginState = state;
 
    if (!ctx->CompileFlag) {
-      ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
+      if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) 
+        _mesa_error( ctx, GL_INVALID_OPERATION, "_tnl_End" );
+      else
+        ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
    }
 
    /* You can set this flag to get the old 'flush_vb on glEnd()'
     * behaviour.
     */
-   if (1 /*(MESA_DEBUG_FLAGS&DEBUG_ALWAYS_FLUSH)*/ )
-      _tnl_flush_immediate( IM );
+   if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH)
+      _tnl_flush_immediate( ctx, IM );
 }
 
 void
 _tnl_End(void)
 {
    GET_CURRENT_CONTEXT(ctx);
+
    _tnl_end( ctx );
 
    /* Need to keep save primitive uptodate in COMPILE and
@@ -339,7 +409,7 @@ _tnl_End(void)
    GET_IMMEDIATE;                                              \
    GLuint count = IM->Count;                                   \
    GLfloat *color = IM->Attrib[VERT_ATTRIB_COLOR0][count];     \
-   IM->Flag[count] |= VERT_COLOR0_BIT;                         \
+   IM->Flag[count] |= VERT_BIT_COLOR0;                         \
    color[0] = r;                                               \
    color[1] = g;                                               \
    color[2] = b;                                               \
@@ -414,7 +484,7 @@ _tnl_Color4ubv( const GLubyte *v)
    GLuint count;                                       \
    GET_IMMEDIATE;                                      \
    count = IM->Count;                                  \
-   IM->Flag[count] |= VERT_COLOR1_BIT;                 \
+   IM->Flag[count] |= VERT_BIT_COLOR1;                 \
    IM->Attrib[VERT_ATTRIB_COLOR1][count][0] = r;       \
    IM->Attrib[VERT_ATTRIB_COLOR1][count][1] = g;       \
    IM->Attrib[VERT_ATTRIB_COLOR1][count][2] = b;       \
@@ -456,7 +526,7 @@ _tnl_EdgeFlag( GLboolean flag )
    GET_IMMEDIATE;
    count = IM->Count;
    IM->EdgeFlag[count] = flag;
-   IM->Flag[count] |= VERT_EDGEFLAG_BIT;
+   IM->Flag[count] |= VERT_BIT_EDGEFLAG;
 }
 
 
@@ -467,7 +537,7 @@ _tnl_EdgeFlagv( const GLboolean *flag )
    GET_IMMEDIATE;
    count = IM->Count;
    IM->EdgeFlag[count] = *flag;
-   IM->Flag[count] |= VERT_EDGEFLAG_BIT;
+   IM->Flag[count] |= VERT_BIT_EDGEFLAG;
 }
 
 
@@ -478,7 +548,7 @@ _tnl_FogCoordfEXT( GLfloat f )
    GET_IMMEDIATE;
    count = IM->Count;
    IM->Attrib[VERT_ATTRIB_FOG][count][0] = f; /*FogCoord[count] = f;*/
-   IM->Flag[count] |= VERT_FOG_BIT;
+   IM->Flag[count] |= VERT_BIT_FOG;
 }
 
 static void
@@ -488,7 +558,7 @@ _tnl_FogCoordfvEXT( const GLfloat *v )
    GET_IMMEDIATE;
    count = IM->Count;
    IM->Attrib[VERT_ATTRIB_FOG][count][0] = v[0]; /*FogCoord[count] = v[0];*/
-   IM->Flag[count] |= VERT_FOG_BIT;
+   IM->Flag[count] |= VERT_BIT_FOG;
 }
 
 
@@ -499,7 +569,7 @@ _tnl_Indexi( GLint c )
    GET_IMMEDIATE;
    count = IM->Count;
    IM->Index[count] = c;
-   IM->Flag[count] |= VERT_INDEX_BIT;
+   IM->Flag[count] |= VERT_BIT_INDEX;
 }
 
 
@@ -510,7 +580,7 @@ _tnl_Indexiv( const GLint *c )
    GET_IMMEDIATE;
    count = IM->Count;
    IM->Index[count] = *c;
-   IM->Flag[count] |= VERT_INDEX_BIT;
+   IM->Flag[count] |= VERT_BIT_INDEX;
 }
 
 
@@ -520,7 +590,7 @@ _tnl_Indexiv( const GLint *c )
    GLfloat *normal;                                    \
    GET_IMMEDIATE;                                      \
    count = IM->Count;                                  \
-   IM->Flag[count] |= VERT_NORMAL_BIT;                 \
+   IM->Flag[count] |= VERT_BIT_NORMAL;                 \
    normal = IM->Attrib[VERT_ATTRIB_NORMAL][count];     \
    ASSIGN_3V(normal, x,y,z);                           \
 }
@@ -532,7 +602,7 @@ _tnl_Indexiv( const GLint *c )
    fi_type *normal;                                            \
    GET_IMMEDIATE;                                              \
    count = IM->Count;                                          \
-   IM->Flag[count] |= VERT_NORMAL_BIT;                         \
+   IM->Flag[count] |= VERT_BIT_NORMAL;                         \
    normal = (fi_type *)IM->Attrib[VERT_ATTRIB_NORMAL][count];  \
    normal[0].i = ((fi_type *)&(x))->i;                         \
    normal[1].i = ((fi_type *)&(y))->i;                         \
@@ -553,6 +623,8 @@ static void
 _tnl_Normal3fv( const GLfloat *v )
 {
    NORMALF( v[0], v[1], v[2] );
+/*     struct immediate *IM = (struct immediate *)(((GLcontext *) _glapi_Context)->swtnl_im); */
+/*     IM->Flag[IM->Count] = VERT_NORM; */
 }
 
 
@@ -563,7 +635,7 @@ _tnl_Normal3fv( const GLfloat *v )
    GLfloat *tc;                                        \
    GET_IMMEDIATE;                              \
    count = IM->Count;                          \
-   IM->Flag[count] |= VERT_TEX0_BIT;           \
+   IM->Flag[count] |= VERT_BIT_TEX0;           \
    tc = IM->Attrib[VERT_ATTRIB_TEX0][count];   \
    ASSIGN_4V(tc,s,0,0,1);                      \
 }
@@ -574,7 +646,7 @@ _tnl_Normal3fv( const GLfloat *v )
    GLfloat *tc;                                        \
    GET_IMMEDIATE;                              \
    count = IM->Count;                          \
-   IM->Flag[count] |= VERT_TEX0_BIT;           \
+   IM->Flag[count] |= VERT_BIT_TEX0;           \
    tc = IM->Attrib[VERT_ATTRIB_TEX0][count];   \
    ASSIGN_4V(tc, s, t, 0, 1);                  \
 }
@@ -585,7 +657,7 @@ _tnl_Normal3fv( const GLfloat *v )
    GLfloat *tc;                                        \
    GET_IMMEDIATE;                              \
    count = IM->Count;                          \
-   IM->Flag[count] |= VERT_TEX0_BIT;           \
+   IM->Flag[count] |= VERT_BIT_TEX0;           \
    IM->TexSize |= TEX_0_SIZE_3;                        \
    tc = IM->Attrib[VERT_ATTRIB_TEX0][count];   \
    ASSIGN_4V(tc, s, t, u, 1);                  \
@@ -597,7 +669,7 @@ _tnl_Normal3fv( const GLfloat *v )
    GLfloat *tc;                                        \
    GET_IMMEDIATE;                              \
    count = IM->Count;                          \
-   IM->Flag[count] |= VERT_TEX0_BIT;           \
+   IM->Flag[count] |= VERT_BIT_TEX0;           \
    IM->TexSize |= TEX_0_SIZE_4;                        \
    tc = IM->Attrib[VERT_ATTRIB_TEX0][count];   \
    ASSIGN_4V(tc, s, t, u, v);                  \
@@ -610,7 +682,7 @@ _tnl_Normal3fv( const GLfloat *v )
    fi_type *tc;                                                \
    GET_IMMEDIATE;                                      \
    count = IM->Count;                                  \
-   IM->Flag[count] |= VERT_TEX0_BIT;                   \
+   IM->Flag[count] |= VERT_BIT_TEX0;                   \
    tc = (fi_type *)IM->Attrib[VERT_ATTRIB_TEX0][count];        \
    tc[0].i = ((fi_type *)&(s))->i;                     \
    tc[1].i = ((fi_type *)&(t))->i;                     \
@@ -680,32 +752,32 @@ _tnl_TexCoord4fv( const GLfloat *v )
 {                                                      \
    GLuint count = IM->Count++;                         \
    GLfloat *dest = IM->Attrib[VERT_ATTRIB_POS][count]; \
-   IM->Flag[count] |= VERT_OBJ_BIT;                    \
+   IM->Flag[count] |= VERT_BIT_POS;                    \
    ASSIGN_4V(dest, x, y, 0, 1);                                \
 /*     ASSERT(IM->Flag[IM->Count]==0);          */     \
    if (count == IMM_MAXDATA - 1)                       \
-      _tnl_flush_immediate( IM );                      \
+      _tnl_flush_immediate( NULL, IM );                        \
 }
 
 #define VERTEX3(IM,x,y,z)                              \
 {                                                      \
    GLuint count = IM->Count++;                         \
    GLfloat *dest = IM->Attrib[VERT_ATTRIB_POS][count]; \
-   IM->Flag[count] |= VERT_OBJ_23;                     \
+   IM->Flag[count] |= VERT_BITS_OBJ_23;                        \
    ASSIGN_4V(dest, x, y, z, 1);                                \
 /*     ASSERT(IM->Flag[IM->Count]==0); */              \
    if (count == IMM_MAXDATA - 1)                       \
-      _tnl_flush_immediate( IM );                      \
+      _tnl_flush_immediate( NULL, IM );                        \
 }
 
 #define VERTEX4(IM, x,y,z,w)                           \
 {                                                      \
    GLuint count = IM->Count++;                         \
    GLfloat *dest = IM->Attrib[VERT_ATTRIB_POS][count]; \
-   IM->Flag[count] |= VERT_OBJ_234;                    \
+   IM->Flag[count] |= VERT_BITS_OBJ_234;               \
    ASSIGN_4V(dest, x, y, z, w);                                \
    if (count == IMM_MAXDATA - 1)                       \
-      _tnl_flush_immediate( IM );                      \
+      _tnl_flush_immediate( NULL, IM );                        \
 }
 
 #if defined(USE_IEEE)
@@ -713,14 +785,14 @@ _tnl_TexCoord4fv( const GLfloat *v )
 {                                                                      \
    GLuint count = IM->Count++;                                         \
    fi_type *dest = (fi_type *)IM->Attrib[VERT_ATTRIB_POS][count];      \
-   IM->Flag[count] |= VERT_OBJ_BIT;                                    \
+   IM->Flag[count] |= VERT_BIT_POS;                                    \
    dest[0].i = ((fi_type *)&(x))->i;                                   \
    dest[1].i = ((fi_type *)&(y))->i;                                   \
    dest[2].i = 0;                                                      \
    dest[3].i = IEEE_ONE;                                               \
 /*     ASSERT(IM->Flag[IM->Count]==0); */                              \
    if (count == IMM_MAXDATA - 1)                                       \
-      _tnl_flush_immediate( IM );                                      \
+      _tnl_flush_immediate( NULL, IM );                                        \
 }
 #else
 #define VERTEX2F VERTEX2
@@ -731,14 +803,14 @@ _tnl_TexCoord4fv( const GLfloat *v )
 {                                                                      \
    GLuint count = IM->Count++;                                         \
    fi_type *dest = (fi_type *)IM->Attrib[VERT_ATTRIB_POS][count];      \
-   IM->Flag[count] |= VERT_OBJ_23;                                     \
+   IM->Flag[count] |= VERT_BITS_OBJ_23;                                        \
    dest[0].i = ((fi_type *)&(x))->i;                                   \
    dest[1].i = ((fi_type *)&(y))->i;                                   \
    dest[2].i = ((fi_type *)&(z))->i;                                   \
    dest[3].i = IEEE_ONE;                                               \
 /*     ASSERT(IM->Flag[IM->Count]==0);  */                             \
    if (count == IMM_MAXDATA - 1)                                       \
-      _tnl_flush_immediate( IM );                                      \
+      _tnl_flush_immediate( NULL, IM );                                        \
 }
 #else
 #define VERTEX3F VERTEX3
@@ -749,13 +821,13 @@ _tnl_TexCoord4fv( const GLfloat *v )
 {                                                                      \
    GLuint count = IM->Count++;                                         \
    fi_type *dest = (fi_type *)IM->Attrib[VERT_ATTRIB_POS][count];      \
-   IM->Flag[count] |= VERT_OBJ_234;                                    \
+   IM->Flag[count] |= VERT_BITS_OBJ_234;                               \
    dest[0].i = ((fi_type *)&(x))->i;                                   \
    dest[1].i = ((fi_type *)&(y))->i;                                   \
    dest[2].i = ((fi_type *)&(z))->i;                                   \
    dest[3].i = ((fi_type *)&(w))->i;                                   \
    if (count == IMM_MAXDATA - 1)                                       \
-      _tnl_flush_immediate( IM );                                      \
+      _tnl_flush_immediate( NULL, IM );                                        \
 }
 #else
 #define VERTEX4F VERTEX4
@@ -815,7 +887,7 @@ _tnl_Vertex4fv( const GLfloat *v )
  * don't crash.  We no-op on invalid targets.
  */
 
-#define MAX_TARGET (GL_TEXTURE0_ARB + MAX_TEXTURE_UNITS)
+#define MAX_TARGET (GL_TEXTURE0_ARB + MAX_TEXTURE_COORD_UNITS)
 
 #define MULTI_TEXCOORD1(target, s)                     \
 {                                                      \
@@ -825,7 +897,7 @@ _tnl_Vertex4fv( const GLfloat *v )
       GLuint count = IM->Count;                                \
       GLfloat *tc = IM->Attrib[VERT_ATTRIB_TEX0 + texunit][count];     \
       ASSIGN_4V(tc, s, 0.0F, 0.0F, 1.0F);              \
-      IM->Flag[count] |= VERT_TEX(texunit);            \
+      IM->Flag[count] |= VERT_BIT_TEX(texunit);                \
    }                                                   \
 }
 
@@ -837,7 +909,7 @@ _tnl_Vertex4fv( const GLfloat *v )
       GLuint count = IM->Count;                                \
       GLfloat *tc = IM->Attrib[VERT_ATTRIB_TEX0 + texunit][count];     \
       ASSIGN_4V(tc, s, t, 0.0F, 1.0F);                 \
-      IM->Flag[count] |= VERT_TEX(texunit);            \
+      IM->Flag[count] |= VERT_BIT_TEX(texunit);                \
    }                                                   \
 }
 
@@ -849,7 +921,7 @@ _tnl_Vertex4fv( const GLfloat *v )
       GLuint count = IM->Count;                                \
       GLfloat *tc = IM->Attrib[VERT_ATTRIB_TEX0 + texunit][count];     \
       ASSIGN_4V(tc, s, t, u, 1.0F);                    \
-      IM->Flag[count] |= VERT_TEX(texunit);            \
+      IM->Flag[count] |= VERT_BIT_TEX(texunit);                \
       IM->TexSize |= TEX_SIZE_3(texunit);              \
    }                                                   \
 }
@@ -862,7 +934,7 @@ _tnl_Vertex4fv( const GLfloat *v )
       GLuint count = IM->Count;                                \
       GLfloat *tc = IM->Attrib[VERT_ATTRIB_TEX0 + texunit][count];     \
       ASSIGN_4V(tc, s, t, u, v);                       \
-      IM->Flag[count] |= VERT_TEX(texunit);            \
+      IM->Flag[count] |= VERT_BIT_TEX(texunit);                \
       IM->TexSize |= TEX_SIZE_4(texunit);              \
    }                                                   \
 }
@@ -875,7 +947,7 @@ _tnl_Vertex4fv( const GLfloat *v )
    if (texunit < IM->MaxTextureUnits) {                                \
       GLuint count = IM->Count;                                        \
       fi_type *tc = (fi_type *)IM->Attrib[VERT_ATTRIB_TEX0 + texunit][count];\
-      IM->Flag[count] |= VERT_TEX(texunit);                    \
+      IM->Flag[count] |= VERT_BIT_TEX(texunit);                        \
       tc[0].i = ((fi_type *)&(s))->i;                          \
       tc[1].i = ((fi_type *)&(t))->i;                          \
       tc[2].i = 0;                                             \
@@ -946,40 +1018,40 @@ _tnl_MultiTexCoord4fvARB(GLenum target, const GLfloat *v)
 {                                                      \
    GLuint count = IM->Count++;                         \
    GLfloat *dest = IM->Attrib[VERT_ATTRIB_POS][count]; \
-   IM->Flag[count] |= VERT_EVAL_C1;                    \
+   IM->Flag[count] |= VERT_BIT_EVAL_C1;                        \
    ASSIGN_4V(dest, x, 0, 0, 1);                                \
    if (count == IMM_MAXDATA-1)                         \
-      _tnl_flush_immediate( IM );                      \
+      _tnl_flush_immediate( NULL, IM );                        \
 }
 
 #define EVALCOORD2(IM, x, y)                           \
 {                                                      \
    GLuint count = IM->Count++;                         \
    GLfloat *dest = IM->Attrib[VERT_ATTRIB_POS][count]; \
-   IM->Flag[count] |= VERT_EVAL_C2;                    \
+   IM->Flag[count] |= VERT_BIT_EVAL_C2;                        \
    ASSIGN_4V(dest, x, y, 0, 1);                                \
    if (count == IMM_MAXDATA-1)                         \
-      _tnl_flush_immediate( IM );                      \
+      _tnl_flush_immediate( NULL, IM );                        \
 }
 
 #define EVALPOINT1(IM, x)                              \
 {                                                      \
    GLuint count = IM->Count++;                         \
    GLfloat *dest = IM->Attrib[VERT_ATTRIB_POS][count]; \
-   IM->Flag[count] |= VERT_EVAL_P1;                    \
+   IM->Flag[count] |= VERT_BIT_EVAL_P1;                        \
    ASSIGN_4V(dest, x, 0, 0, 1);                                \
    if (count == IMM_MAXDATA-1)                         \
-      _tnl_flush_immediate( IM );                      \
+      _tnl_flush_immediate( NULL, IM );                        \
 }
 
 #define EVALPOINT2(IM, x, y)                           \
 {                                                      \
    GLuint count = IM->Count++;                         \
    GLfloat *dest = IM->Attrib[VERT_ATTRIB_POS][count]; \
-   IM->Flag[count] |= VERT_EVAL_P2;                    \
+   IM->Flag[count] |= VERT_BIT_EVAL_P2;                        \
    ASSIGN_4V(dest, x, y, 0, 1);                                \
    if (count == IMM_MAXDATA-1)                         \
-      _tnl_flush_immediate( IM );                      \
+      _tnl_flush_immediate( NULL, IM );                        \
 }
 
 static void
@@ -1035,11 +1107,11 @@ _tnl_EvalPoint2( GLint i, GLint j )
    GLuint count = IM->Count;                   \
    IM->Elt[count] = i;                         \
    IM->Flag[count] &= IM->ArrayEltFlags;       \
-   IM->Flag[count] |= VERT_ELT;                        \
+   IM->Flag[count] |= VERT_BIT_ELT;            \
    IM->FlushElt = IM->ArrayEltFlush;           \
    IM->Count += IM->ArrayEltIncr;              \
    if (IM->Count == IMM_MAXDATA)               \
-      _tnl_flush_immediate( IM );              \
+      _tnl_flush_immediate( NULL, IM );                \
 }
 
 
@@ -1074,19 +1146,6 @@ _tnl_eval_coord2f( GLcontext *CC, GLfloat u, GLfloat v )
    EVALCOORD2( i, u, v );
 }
 
-void
-_tnl_array_element( GLcontext *CC, GLint i )
-{
-   struct immediate *im = TNL_CURRENT_IM(CC);
-   ARRAY_ELT( im, i );
-}
-
-void
-_tnl_vertex2f( GLcontext *ctx, GLfloat x, GLfloat y )
-{
-   struct immediate *im = TNL_CURRENT_IM(ctx);
-   VERTEX2( im, x, y );
-}
 
 
 
@@ -1106,9 +1165,13 @@ _tnl_VertexAttrib4fNV( GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w
       if (index == 0) {
          IM->Count++;
          if (count == IMM_MAXDATA - 1)
-            _tnl_flush_immediate( IM );
+            _tnl_flush_immediate( NULL, IM );
       }
    }
+   else {
+      GET_CURRENT_CONTEXT(ctx);
+      _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribNV(index > 15)");
+   }
 }   
 
 static void
@@ -1123,9 +1186,13 @@ _tnl_VertexAttrib4fvNV( GLuint index, const GLfloat *v )
       if (index == 0) {
          IM->Count++;
          if (count == IMM_MAXDATA - 1)
-            _tnl_flush_immediate( IM );
+            _tnl_flush_immediate( NULL, IM );
       }
    }
+   else {
+      GET_CURRENT_CONTEXT(ctx);
+      _mesa_error(ctx, GL_INVALID_VALUE, "glVertexAttribNV(index > 15)");
+   }
 }   
 
 
@@ -1139,14 +1206,12 @@ _tnl_Rectf( GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2 )
 {
    GET_CURRENT_CONTEXT(ctx);
 
-/*     fprintf(stderr, "%s\n", __FUNCTION__); */
-
    if (_tnl_hard_begin( ctx, GL_QUADS )) {
-      _tnl_vertex2f( ctx, x1, y1 );
-      _tnl_vertex2f( ctx, x2, y1 );
-      _tnl_vertex2f( ctx, x2, y2 );
-      _tnl_vertex2f( ctx, x1, y2 );
-      _tnl_end( ctx );
+      glVertex2f( x1, y1 );
+      glVertex2f( x2, y1 );
+      glVertex2f( x2, y2 );
+      glVertex2f( x1, y2 );
+      glEnd();
    }
 }
 
@@ -1154,6 +1219,7 @@ static void
 _tnl_Materialfv( GLenum face, GLenum pname, const GLfloat *params )
 {
    GET_CURRENT_CONTEXT(ctx);
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
    struct immediate *IM = TNL_CURRENT_IM(ctx);
    GLuint count = IM->Count;
    struct gl_material *mat;
@@ -1161,11 +1227,22 @@ _tnl_Materialfv( GLenum face, GLenum pname, const GLfloat *params )
 
    if (bitmask == 0)
       return;
+   
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx, "_tnl_Materialfv\n");
 
-   if (!(IM->Flag[count] & VERT_MATERIAL)) {
+   if (tnl->IsolateMaterials &&
+       !(IM->BeginState & VERT_BEGIN_1)) /* heuristic */
+   {
+      _tnl_flush_immediate( ctx, IM );      
+      IM = TNL_CURRENT_IM(ctx);
+      count = IM->Count;
+   }
+
+   if (!(IM->Flag[count] & VERT_BIT_MATERIAL)) {
       if (!IM->Material) {
-        IM->Material = (GLmaterial (*)[2]) MALLOC( sizeof(GLmaterial) *
-                                                   IMM_SIZE * 2 );
+        IM->Material = (struct gl_material (*)[2])
+            MALLOC( sizeof(struct gl_material) * IMM_SIZE * 2 );
         IM->MaterialMask = (GLuint *) MALLOC( sizeof(GLuint) * IMM_SIZE );
         IM->MaterialMask[IM->LastMaterial] = 0;
       }
@@ -1175,7 +1252,7 @@ _tnl_Materialfv( GLenum face, GLenum pname, const GLfloat *params )
                                    IM->MaterialOrMask & ~bitmask );
       }
 
-      IM->Flag[count] |= VERT_MATERIAL;
+      IM->Flag[count] |= VERT_BIT_MATERIAL;
       IM->MaterialMask[count] = 0;
       IM->MaterialAndMask &= IM->MaterialMask[IM->LastMaterial];
       IM->LastMaterial = count;
@@ -1227,6 +1304,12 @@ _tnl_Materialfv( GLenum face, GLenum pname, const GLfloat *params )
       mat[1].DiffuseIndex = params[1];
       mat[1].SpecularIndex = params[2];
    }
+
+   if (tnl->IsolateMaterials && 
+       !(IM->BeginState & VERT_BEGIN_1)) /* heuristic */
+   {
+      _tnl_flush_immediate( ctx, IM );
+   }
 }
 
 void _tnl_imm_vtxfmt_init( GLcontext *ctx )