Merge remote branch 'origin/mesa_7_6_branch'
[mesa.git] / src / mesa / tnl / t_vb_cliptmp.h
index 90b9b82f53a7cab34e34e6fb76581dc0e26b44b3..618b8b31304f0e2351d0fa03636d941a1db925f7 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: t_vb_cliptmp.h,v 1.12 2001/05/09 12:25:40 keithw Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
+ * Version:  6.5.2
  *
- * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2006  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"),
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  *
  * Authors:
- *    Keith Whitwell <keithw@valinux.com>
+ *    Keith Whitwell <keith@tungstengraphics.com>
  */
 
 
 #define CLIP_DOTPROD(K, A, B, C, D) X(K)*A + Y(K)*B + Z(K)*C + W(K)*D
 
-#define POLY_CLIP( PLANE, A, B, C, D )                                 \
+#define POLY_CLIP( PLANE_BIT, A, B, C, D )                             \
 do {                                                                   \
-   if (mask & PLANE) {                                                 \
+   if (mask & PLANE_BIT) {                                             \
       GLuint idxPrev = inlist[0];                                      \
       GLfloat dpPrev = CLIP_DOTPROD(idxPrev, A, B, C, D );             \
       GLuint outcount = 0;                                             \
@@ -43,17 +41,12 @@ do {                                                                        \
         GLuint idx = inlist[i];                                        \
         GLfloat dp = CLIP_DOTPROD(idx, A, B, C, D );                   \
                                                                        \
-         clipmask[idxPrev] |= PLANE;                                   \
-        if (!NEGATIVE(dpPrev)) {                                       \
+        if (!IS_NEGATIVE(dpPrev)) {                                    \
            outlist[outcount++] = idxPrev;                              \
-           clipmask[idxPrev] &= ~PLANE;                                \
         }                                                              \
                                                                        \
         if (DIFFERENT_SIGNS(dp, dpPrev)) {                             \
-            GLuint newvert = VB->LastClipped++;                                \
-            VB->ClipMask[newvert] = 0;                                 \
-            outlist[outcount++] = newvert;                             \
-           if (NEGATIVE(dp)) {                                         \
+           if (IS_NEGATIVE(dp)) {                                      \
               /* Going out of bounds.  Avoid division by zero as we    \
                * know dp != dpPrev from DIFFERENT_SIGNS, above.        \
                */                                                      \
@@ -67,6 +60,7 @@ do {                                                                  \
                INTERP_4F( t, coord[newvert], coord[idxPrev], coord[idx]); \
               interp( ctx, t, newvert, idxPrev, idx, GL_FALSE );       \
            }                                                           \
+            outlist[outcount++] = newvert++;                           \
         }                                                              \
                                                                        \
         idxPrev = idx;                                                 \
@@ -86,51 +80,54 @@ do {                                                                        \
 } while (0)
 
 
-#define LINE_CLIP(PLANE, A, B, C, D )                                  \
+#define LINE_CLIP(PLANE_BIT, A, B, C, D )                              \
 do {                                                                   \
-   if (mask & PLANE) {                                                 \
-      GLfloat dpI = CLIP_DOTPROD( ii, A, B, C, D );                    \
-      GLfloat dpJ = CLIP_DOTPROD( jj, A, B, C, D );                    \
+   if (mask & PLANE_BIT) {                                             \
+      const GLfloat dp0 = CLIP_DOTPROD( v0, A, B, C, D );              \
+      const GLfloat dp1 = CLIP_DOTPROD( v1, A, B, C, D );              \
+      const GLboolean neg_dp0 = IS_NEGATIVE(dp0);                      \
+      const GLboolean neg_dp1 = IS_NEGATIVE(dp1);                      \
+                                                                       \
+      /* For regular clipping, we know from the clipmask that one      \
+       * (or both) of these must be negative (otherwise we wouldn't    \
+       * be here).                                                     \
+       * For userclip, there is only a single bit for all active       \
+       * planes, so we can end up here when there is nothing to do,    \
+       * hence the second IS_NEGATIVE() test:                          \
+       */                                                              \
+      if (neg_dp0 && neg_dp1)                                          \
+         return; /* both vertices outside clip plane: discard */       \
                                                                        \
-      if (DIFFERENT_SIGNS(dpI, dpJ)) {                                 \
-         GLuint newvert = VB->LastClipped++;                           \
-         VB->ClipMask[newvert] = 0;                                    \
-        if (NEGATIVE(dpJ)) {                                           \
-           GLfloat t = dpI / (dpI - dpJ);                              \
-            VB->ClipMask[jj] |= PLANE;                                 \
-            INTERP_4F( t, coord[newvert], coord[ii], coord[jj] );      \
-           interp( ctx, t, newvert, ii, jj, GL_FALSE );                \
-            jj = newvert;                                              \
-        } else {                                                       \
-           GLfloat t = dpJ / (dpJ - dpI);                              \
-            VB->ClipMask[ii] |= PLANE;                                 \
-            INTERP_4F( t, coord[newvert], coord[jj], coord[ii] );      \
-           interp( ctx, t, newvert, jj, ii, GL_FALSE );                \
-            ii = newvert;                                              \
-        }                                                              \
+      if (neg_dp1) {                                                   \
+        GLfloat t = dp1 / (dp1 - dp0);                                 \
+        if (t > t1) t1 = t;                                            \
+      } else if (neg_dp0) {                                            \
+        GLfloat t = dp0 / (dp0 - dp1);                                 \
+        if (t > t0) t0 = t;                                            \
       }                                                                        \
-      else if (NEGATIVE(dpI))                                          \
-        return;                                                        \
-                                                                     \
+      if (t0 + t1 >= 1.0)                                              \
+        return; /* discard */                                          \
+   }                                                                   \
 } while (0)
 
 
 
 /* Clip a line against the viewport and user clip planes.
  */
-static __inline void TAG(clip_line)( GLcontext *ctx,
-                                    GLuint i, GLuint j,
-                                    GLubyte mask )
+static INLINE void
+TAG(clip_line)( GLcontext *ctx, GLuint v0, GLuint v1, GLubyte mask )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    struct vertex_buffer *VB = &tnl->vb;
-   interp_func interp = tnl->Driver.RenderInterp;
+   tnl_interp_func interp = tnl->Driver.Render.Interp;
    GLfloat (*coord)[4] = VB->ClipPtr->data;
-   GLuint ii = i, jj = j, p;
-
-   VB->LastClipped = VB->FirstClipped;
+   GLuint newvert = VB->Count;
+   GLfloat t0 = 0;
+   GLfloat t1 = 0;
+   GLuint p;
+   const GLuint v0_orig = v0;
 
-   if (mask & 0x3f) {
+   if (mask & CLIP_FRUSTUM_BITS) {
       LINE_CLIP( CLIP_RIGHT_BIT,  -1,  0,  0, 1 );
       LINE_CLIP( CLIP_LEFT_BIT,    1,  0,  0, 1 );
       LINE_CLIP( CLIP_TOP_BIT,     0, -1,  0, 1 );
@@ -140,46 +137,86 @@ static __inline void TAG(clip_line)( GLcontext *ctx,
    }
 
    if (mask & CLIP_USER_BIT) {
-      for (p=0;p<MAX_CLIP_PLANES;p++) {
-        if (ctx->Transform.ClipEnabled[p]) {
-           GLfloat a = ctx->Transform._ClipUserPlane[p][0];
-           GLfloat b = ctx->Transform._ClipUserPlane[p][1];
-           GLfloat c = ctx->Transform._ClipUserPlane[p][2];
-           GLfloat d = ctx->Transform._ClipUserPlane[p][3];
+      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
+        if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
+            const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
+            const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
+            const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
+            const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
            LINE_CLIP( CLIP_USER_BIT, a, b, c, d );
         }
       }
    }
 
-   if ((ctx->_TriangleCaps & DD_FLATSHADE) && j != jj)
-      tnl->Driver.RenderCopyPV( ctx, jj, j );
+   if (VB->ClipMask[v0]) {
+      INTERP_4F( t0, coord[newvert], coord[v0], coord[v1] );
+      interp( ctx, t0, newvert, v0, v1, GL_FALSE );
+      v0 = newvert;
+      newvert++;
+   }
+   else {
+      ASSERT(t0 == 0.0);
+   }
+
+   /* Note: we need to use vertex v0_orig when computing the new
+    * interpolated/clipped vertex position, not the current v0 which
+    * may have got set when we clipped the other end of the line!
+    */
+   if (VB->ClipMask[v1]) {
+      INTERP_4F( t1, coord[newvert], coord[v1], coord[v0_orig] );
+      interp( ctx, t1, newvert, v1, v0_orig, GL_FALSE );
 
-   tnl->Driver.RenderClippedLine( ctx, ii, jj );
+      if (ctx->Light.ShadeModel == GL_FLAT)
+        tnl->Driver.Render.CopyPV( ctx, newvert, v1 );
+
+      v1 = newvert;
+
+      newvert++;
+   }
+   else {
+      ASSERT(t1 == 0.0);
+   }
+
+   tnl->Driver.Render.ClippedLine( ctx, v0, v1 );
 }
 
 
 /* Clip a triangle against the viewport and user clip planes.
  */
-static __inline void TAG(clip_tri)( GLcontext *ctx,
-                                   GLuint v0, GLuint v1, GLuint v2,
-                                   GLubyte mask )
+static INLINE void
+TAG(clip_tri)( GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLubyte mask )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    struct vertex_buffer *VB = &tnl->vb;
-   interp_func interp = tnl->Driver.RenderInterp;
+   tnl_interp_func interp = tnl->Driver.Render.Interp;
+   GLuint newvert = VB->Count;
    GLfloat (*coord)[4] = VB->ClipPtr->data;
    GLuint pv = v2;
    GLuint vlist[2][MAX_CLIPPED_VERTICES];
    GLuint *inlist = vlist[0], *outlist = vlist[1];
    GLuint p;
-   GLubyte *clipmask = VB->ClipMask;
    GLuint n = 3;
 
    ASSIGN_3V(inlist, v2, v0, v1 ); /* pv rotated to slot zero */
 
-   VB->LastClipped = VB->FirstClipped;
+   if (0) {
+      /* print pre-clip vertex coords */
+      GLuint i, j;
+      _mesa_printf("pre clip:\n");
+      for (i = 0; i < n; i++) {
+         j = inlist[i];
+         _mesa_printf("  %u: %u: %f, %f, %f, %f\n",
+                      i, j,
+                      coord[j][0], coord[j][1], coord[j][2], coord[j][3]);
+         assert(!IS_INF_OR_NAN(coord[j][0]));
+         assert(!IS_INF_OR_NAN(coord[j][1]));
+         assert(!IS_INF_OR_NAN(coord[j][2]));
+         assert(!IS_INF_OR_NAN(coord[j][3]));
+      }
+   }
+
 
-   if (mask & 0x3f) {
+   if (mask & CLIP_FRUSTUM_BITS) {
       POLY_CLIP( CLIP_RIGHT_BIT,  -1,  0,  0, 1 );
       POLY_CLIP( CLIP_LEFT_BIT,    1,  0,  0, 1 );
       POLY_CLIP( CLIP_TOP_BIT,     0, -1,  0, 1 );
@@ -189,50 +226,60 @@ static __inline void TAG(clip_tri)( GLcontext *ctx,
    }
 
    if (mask & CLIP_USER_BIT) {
-      for (p=0;p<MAX_CLIP_PLANES;p++) {
-        if (ctx->Transform.ClipEnabled[p]) {
-           GLfloat a = ctx->Transform._ClipUserPlane[p][0];
-           GLfloat b = ctx->Transform._ClipUserPlane[p][1];
-           GLfloat c = ctx->Transform._ClipUserPlane[p][2];
-           GLfloat d = ctx->Transform._ClipUserPlane[p][3];
-           POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
-        }
+      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
+         if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
+            const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
+            const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
+            const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
+            const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
+            POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
+         }
       }
    }
 
-   if (ctx->_TriangleCaps & DD_FLATSHADE) {
+   if (ctx->Light.ShadeModel == GL_FLAT) {
       if (pv != inlist[0]) {
-        ASSERT( inlist[0] >= VB->FirstClipped );
-        tnl->Driver.RenderCopyPV( ctx, inlist[0], pv );
+        ASSERT( inlist[0] >= VB->Count );
+        tnl->Driver.Render.CopyPV( ctx, inlist[0], pv );
       }
    }
 
-   tnl->Driver.RenderClippedPolygon( ctx, inlist, n );
+   if (0) {
+      /* print post-clip vertex coords */
+      GLuint i, j;
+      _mesa_printf("post clip:\n");
+      for (i = 0; i < n; i++) {
+         j = inlist[i];
+         _mesa_printf("  %u: %u: %f, %f, %f, %f\n",
+                      i, j,
+                      coord[j][0], coord[j][1], coord[j][2], coord[j][3]);
+      }
+   }
+
+   tnl->Driver.Render.ClippedPolygon( ctx, inlist, n );
 }
 
 
 /* Clip a quad against the viewport and user clip planes.
  */
-static __inline void TAG(clip_quad)( GLcontext *ctx,
-                                    GLuint v0, GLuint v1, GLuint v2, GLuint v3,
-                                    GLubyte mask )
+static INLINE void
+TAG(clip_quad)( GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3,
+                GLubyte mask )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    struct vertex_buffer *VB = &tnl->vb;
-   interp_func interp = tnl->Driver.RenderInterp;
+   tnl_interp_func interp = tnl->Driver.Render.Interp;
+   GLuint newvert = VB->Count;
    GLfloat (*coord)[4] = VB->ClipPtr->data;
    GLuint pv = v3;
    GLuint vlist[2][MAX_CLIPPED_VERTICES];
    GLuint *inlist = vlist[0], *outlist = vlist[1];
    GLuint p;
-   GLubyte *clipmask = VB->ClipMask;
    GLuint n = 4;
 
    ASSIGN_4V(inlist, v3, v0, v1, v2 ); /* pv rotated to slot zero */
 
-   VB->LastClipped = VB->FirstClipped;
-
-   if (mask & 0x3f) {
+   if (mask & CLIP_FRUSTUM_BITS) {
       POLY_CLIP( CLIP_RIGHT_BIT,  -1,  0,  0, 1 );
       POLY_CLIP( CLIP_LEFT_BIT,    1,  0,  0, 1 );
       POLY_CLIP( CLIP_TOP_BIT,     0, -1,  0, 1 );
@@ -242,25 +289,25 @@ static __inline void TAG(clip_quad)( GLcontext *ctx,
    }
 
    if (mask & CLIP_USER_BIT) {
-      for (p=0;p<MAX_CLIP_PLANES;p++) {
-        if (ctx->Transform.ClipEnabled[p]) {
-           GLfloat a = ctx->Transform._ClipUserPlane[p][0];
-           GLfloat b = ctx->Transform._ClipUserPlane[p][1];
-           GLfloat c = ctx->Transform._ClipUserPlane[p][2];
-           GLfloat d = ctx->Transform._ClipUserPlane[p][3];
+      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
+        if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
+            const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
+            const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
+            const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
+            const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
            POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
         }
       }
    }
 
-   if (ctx->_TriangleCaps & DD_FLATSHADE) {
+   if (ctx->Light.ShadeModel == GL_FLAT) {
       if (pv != inlist[0]) {
-        ASSERT( inlist[0] >= VB->FirstClipped );
-        tnl->Driver.RenderCopyPV( ctx, inlist[0], pv );
+        ASSERT( inlist[0] >= VB->Count );
+        tnl->Driver.Render.CopyPV( ctx, inlist[0], pv );
       }
    }
 
-   tnl->Driver.RenderClippedPolygon( ctx, inlist, n );
+   tnl->Driver.Render.ClippedPolygon( ctx, inlist, n );
 }
 
 #undef W