Add EXT_vertex_cull support to mesa
authorKeith Whitwell <keith@tungstengraphics.com>
Mon, 10 May 2004 18:16:03 +0000 (18:16 +0000)
committerKeith Whitwell <keith@tungstengraphics.com>
Mon, 10 May 2004 18:16:03 +0000 (18:16 +0000)
14 files changed:
src/mesa/main/clip.c
src/mesa/main/clip.h
src/mesa/main/enable.c
src/mesa/main/extensions.c
src/mesa/main/matrix.c
src/mesa/main/mtypes.h
src/mesa/main/state.c
src/mesa/math/m_xform.h
src/mesa/tnl/t_context.h
src/mesa/tnl/t_pipeline.h
src/mesa/tnl/t_vb_cull.c [new file with mode: 0644]
src/mesa/tnl/t_vb_render.c
src/mesa/tnl/t_vb_vertex.c
src/mesa/tnl_dd/t_dd_dmatmp.h

index 31e6af91bd1a99f8361d8478590d0de1788723a0..a1eda32ebf73230e949b4318def61d7bfe2efd37 100644 (file)
@@ -117,3 +117,45 @@ _mesa_GetClipPlane( GLenum plane, GLdouble *equation )
    equation[3] = (GLdouble) ctx->Transform.EyeUserPlane[p][3];
 }
 
+void GLAPIENTRY 
+_mesa_CullParameterfvEXT (GLenum cap, GLfloat *v)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   switch (cap) {
+   case GL_CULL_VERTEX_EYE_POSITION_EXT:
+      FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
+      COPY_4FV(ctx->Transform.CullEyePos, v);
+      
+      _mesa_transform_vector( ctx->Transform.CullObjPos, 
+                             ctx->Transform.CullEyePos,
+                             ctx->ModelviewMatrixStack.Top->inv );
+      break;
+
+   case GL_CULL_VERTEX_OBJECT_POSITION_EXT:
+      FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
+      COPY_4FV(ctx->Transform.CullObjPos, v);
+
+      _mesa_transform_vector( ctx->Transform.CullEyePos, 
+                             ctx->Transform.CullObjPos,
+                             ctx->ModelviewMatrixStack.Top->m );
+     break;
+   default:
+      _mesa_error( ctx, GL_INVALID_ENUM, "glCullParameterfvEXT" );
+   }
+}
+
+void GLAPIENTRY 
+_mesa_CullParameterdvEXT (GLenum cap, GLdouble *v)
+{
+   GLfloat f[4];
+   
+   f[0] = (GLfloat)v[0];
+   f[1] = (GLfloat)v[1];
+   f[2] = (GLfloat)v[2];
+   f[3] = (GLfloat)v[3];
+
+   _mesa_CullParameterfvEXT(cap, f);
+}
+
index 9442e8643953507753c039746298a521fe472f19..d53afb45bdf6f9f1c3c2c8520e56f5d7999177f0 100644 (file)
@@ -39,4 +39,11 @@ _mesa_ClipPlane( GLenum plane, const GLdouble *equation );
 extern void GLAPIENTRY 
 _mesa_GetClipPlane( GLenum plane, GLdouble *equation );
 
+extern void GLAPIENTRY 
+_mesa_CullParameterfvEXT (GLenum cap, GLfloat *v);
+
+extern void GLAPIENTRY 
+_mesa_CullParameterdvEXT (GLenum cap, GLdouble *v);
+
+
 #endif
index 64cc4d17ca7538dbe5a1d78acf1b2e6da838a65a..25bb7938c01747a303be1c39b3022720a06f244b 100644 (file)
@@ -290,6 +290,15 @@ void _mesa_set_enable( GLcontext *ctx, GLenum cap, GLboolean state )
          FLUSH_VERTICES(ctx, _NEW_POLYGON);
          ctx->Polygon.CullFlag = state;
          break;
+
+      case GL_CULL_VERTEX_EXT:
+         CHECK_EXTENSION(EXT_cull_vertex, cap);
+         if (ctx->Transform.CullVertexFlag == state)
+            return;
+         FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
+         ctx->Transform.CullVertexFlag = state;
+         break;
+
       case GL_DEPTH_TEST:
          if (state && ctx->Visual.depthBits==0) {
             _mesa_warning(ctx,"glEnable(GL_DEPTH_TEST) but no depth buffer");
index f840b3fe086654753d59877a63a666291d445c13..172843a4a4e6003fcff963a808097ade6a4d9561 100644 (file)
@@ -75,6 +75,7 @@ static const struct {
    { OFF, "GL_EXT_blend_minmax",               F(EXT_blend_minmax) },
    { OFF, "GL_EXT_blend_subtract",             F(EXT_blend_subtract) },
    { ON,  "GL_EXT_clip_volume_hint",           F(EXT_clip_volume_hint) },
+   { OFF, "GL_EXT_cull_vertex",                F(EXT_cull_vertex) },
    { ON,  "GL_EXT_compiled_vertex_array",      F(EXT_compiled_vertex_array) },
    { OFF, "GL_EXT_convolution",                F(EXT_convolution) },
    { ON,  "GL_EXT_copy_texture",               F(EXT_copy_texture) },
index 19e8dc3a5a04bc091bb20a81e3d91c2a320daf01..1e6b9dd39473b711704d5b515b2efd58464821e5 100644 (file)
@@ -745,8 +745,16 @@ calculate_model_project_matrix( GLcontext *ctx )
  */
 void _mesa_update_modelview_project( GLcontext *ctx, GLuint new_state )
 {
-   if (new_state & _NEW_MODELVIEW)
+   if (new_state & _NEW_MODELVIEW) {
       _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
+    
+      /* Bring cull position uptodate.
+       */
+      TRANSFORM_POINT3( ctx->Transform.CullObjPos, 
+                       ctx->ModelviewMatrixStack.Top->inv,
+                       ctx->Transform.CullEyePos );
+   }
+
 
    if (new_state & _NEW_PROJECTION)
       update_projection( ctx );
@@ -898,6 +906,9 @@ void _mesa_init_transform( GLcontext *ctx )
       ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
    }
    ctx->Transform.ClipPlanesEnabled = 0;
+
+   ASSIGN_4V( ctx->Transform.CullObjPos, 0.0, 0.0, 1.0, 0.0 );
+   ASSIGN_4V( ctx->Transform.CullEyePos, 0.0, 0.0, 1.0, 0.0 );
 }
 
 
index e045c1f35b8a00e0a94f03195c75c06876da0eaa..1458f16a537e377ca26809b51ac9bcafc788a353 100644 (file)
@@ -1323,6 +1323,10 @@ struct gl_transform_attrib {
    GLboolean Normalize;                                /**< Normalize all normals? */
    GLboolean RescaleNormals;                   /**< GL_EXT_rescale_normal */
    GLboolean RasterPositionUnclipped;           /**< GL_IBM_rasterpos_clip */
+
+   GLboolean CullVertexFlag;   /**< True if GL_CULL_VERTEX_EXT is enabled */
+   GLfloat CullEyePos[4];
+   GLfloat CullObjPos[4];
 };
 
 
@@ -1885,6 +1889,7 @@ struct gl_extensions
    GLboolean EXT_blend_minmax;
    GLboolean EXT_blend_subtract;
    GLboolean EXT_clip_volume_hint;
+   GLboolean EXT_cull_vertex;
    GLboolean EXT_convolution;
    GLboolean EXT_compiled_vertex_array;
    GLboolean EXT_copy_texture;
index c894241a739b1786942db58c301d2a2fcbf7ddb6..0f3750e61caf71e192999820d40da143a12bf577 100644 (file)
@@ -211,6 +211,8 @@ _mesa_init_exec_table(struct _glapi_table *exec, GLuint tableSize)
    exec->ClipPlane = _mesa_ClipPlane;
    exec->ColorMaterial = _mesa_ColorMaterial;
    exec->CopyPixels = _mesa_CopyPixels;
+   exec->CullParameterfvEXT = _mesa_CullParameterfvEXT;
+   exec->CullParameterdvEXT = _mesa_CullParameterdvEXT;
    exec->DeleteLists = _mesa_DeleteLists;
    exec->DepthFunc = _mesa_DepthFunc;
    exec->DepthMask = _mesa_DepthMask;
index 5c96cea3d5a77015f7cb9013ebb23d042d858638..63f1062fc85a69e95e383d1d27812eaf208a5878 100644 (file)
@@ -98,6 +98,7 @@ _math_init_transformation(void);
 #define CLIP_NEAR_BIT    0x10
 #define CLIP_FAR_BIT     0x20
 #define CLIP_USER_BIT    0x40
+#define CLIP_CULL_BIT    0x80
 #define CLIP_ALL_BITS    0x3f
 
 
index b9fc7b8ea0db1b8abab2e16eac8de1dfc4266477..3d099b205a07d40448166070ca994b3f600d1476 100644 (file)
@@ -432,6 +432,7 @@ struct vertex_buffer
    GLvector4f  *ClipPtr;                       /* _TNL_BIT_POS */
    GLvector4f  *NdcPtr;                         /* _TNL_BIT_POS */
    GLubyte     ClipOrMask;                     /* _TNL_BIT_POS */
+   GLubyte     ClipAndMask;                    /* _TNL_BIT_POS */
    GLubyte     *ClipMask;                      /* _TNL_BIT_POS */
    GLvector4f  *NormalPtr;                     /* _TNL_BIT_NORMAL */
    GLfloat     *NormalLengthPtr;               /* _TNL_BIT_NORMAL */
index 02128ea8fd35d5844eab8a37055ae3337cd45efb..d356ee4532cf7a69f85d7890de3a243adc1102e8 100644 (file)
@@ -47,6 +47,7 @@ extern void _tnl_install_pipeline( GLcontext *ctx,
 /* These are implemented in the t_vb_*.c files:
  */
 extern const struct tnl_pipeline_stage _tnl_vertex_transform_stage;
+extern const struct tnl_pipeline_stage _tnl_vertex_cull_stage;
 extern const struct tnl_pipeline_stage _tnl_normal_transform_stage;
 extern const struct tnl_pipeline_stage _tnl_lighting_stage;
 extern const struct tnl_pipeline_stage _tnl_fog_coordinate_stage;
diff --git a/src/mesa/tnl/t_vb_cull.c b/src/mesa/tnl/t_vb_cull.c
new file mode 100644 (file)
index 0000000..9527ead
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * Mesa 3-D graphics library
+ * Version:  6.1
+ *
+ * Copyright (C) 1999-2004  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"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Keith Whitwell <keith@tungstengraphics.com>
+ */
+
+
+#include "glheader.h"
+#include "colormac.h"
+#include "context.h"
+#include "macros.h"
+#include "imports.h"
+#include "mtypes.h"
+
+#include "math/m_xform.h"
+
+#include "t_context.h"
+#include "t_pipeline.h"
+
+
+
+/* EXT_vertex_cull.  Not really a big win, but probably depends on
+ * your application.  This stage not included in the default pipeline.
+ */
+static GLboolean run_cull_stage( GLcontext *ctx,
+                                struct tnl_pipeline_stage *stage )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   struct vertex_buffer *VB = &tnl->vb;
+
+   const GLfloat a = ctx->Transform.CullObjPos[0];
+   const GLfloat b = ctx->Transform.CullObjPos[1];
+   const GLfloat c = ctx->Transform.CullObjPos[2];
+   GLfloat *norm = (GLfloat *)VB->NormalPtr->data;
+   GLuint stride = VB->NormalPtr->stride;
+   GLuint count = VB->Count;
+   GLuint i;
+
+   VB->ClipOrMask &= ~CLIP_CULL_BIT;
+   VB->ClipAndMask |= CLIP_CULL_BIT;
+
+   for (i = 0 ; i < count ; i++) {
+      GLfloat dp = (norm[0] * a + 
+                   norm[1] * b +
+                   norm[2] * c);
+
+      if (dp < 0) {
+        VB->ClipMask[i] |= CLIP_CULL_BIT;
+        VB->ClipOrMask |= CLIP_CULL_BIT;
+      }
+      else {
+        VB->ClipMask[i] &= ~CLIP_CULL_BIT;
+        VB->ClipAndMask &= ~CLIP_CULL_BIT;
+      }
+
+      STRIDE_F(norm, stride);
+   }
+
+   return !(VB->ClipAndMask & CLIP_CULL_BIT);
+}
+
+
+static void check_cull( GLcontext *ctx, struct tnl_pipeline_stage *stage )
+{
+   stage->active = (!ctx->VertexProgram._Enabled &&
+                   ctx->Transform.CullVertexFlag);
+}
+
+
+static void dtr( struct tnl_pipeline_stage *stage )
+{
+}
+
+
+const struct tnl_pipeline_stage _tnl_vertex_cull_stage =
+{
+   "EXT_cull_vertex",
+   _NEW_PROGRAM | 
+   _NEW_TRANSFORM,  
+   _NEW_TRANSFORM,
+   GL_TRUE,                    /* active */
+   _TNL_BIT_NORMAL | 
+   _TNL_BIT_POS,               /* inputs */
+   _TNL_BIT_POS,               /* outputs */
+   0,                          /* changed_inputs */
+   NULL,                       /* private data */
+   dtr,                                /* destructor */
+   check_cull,         /* check */
+   run_cull_stage              /* run -- initially set to init */
+};
index 1c5016896833c50ceaec8780027a1c4b53d41430..f8e41f5d9be6949bcd56a152349048a1c5224036 100644 (file)
@@ -77,6 +77,9 @@
 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
 
 
+#define CLIPMASK (CLIP_ALL_BITS|CLIP_CULL_BIT)
+
+
 /* Vertices, with the possibility of clipping.
  */
 #define RENDER_POINTS( start, count ) \
@@ -88,7 +91,7 @@ do {                                          \
    GLubyte ormask = c1|c2;                     \
    if (!ormask)                                        \
       LineFunc( ctx, v1, v2 );                 \
-   else if (!(c1 & c2 & 0x3f))                 \
+   else if (!(c1 & c2 & CLIPMASK))                     \
       clip_line_4( ctx, v1, v2, ormask );      \
 } while (0)
 
@@ -98,7 +101,7 @@ do {                                                 \
    GLubyte ormask = c1|c2|c3;                          \
    if (!ormask)                                                \
       TriangleFunc( ctx, v1, v2, v3 );                 \
-   else if (!(c1 & c2 & c3 & 0x3f))                    \
+   else if (!(c1 & c2 & c3 & CLIPMASK))                        \
       clip_tri_4( ctx, v1, v2, v3, ormask );           \
 } while (0)
 
@@ -109,7 +112,7 @@ do {                                                        \
    GLubyte ormask = c1|c2|c3|c4;                       \
    if (!ormask)                                                \
       QuadFunc( ctx, v1, v2, v3, v4 );                 \
-   else if (!(c1 & c2 & c3 & c4 & 0x3f))               \
+   else if (!(c1 & c2 & c3 & c4 & CLIPMASK))           \
       clip_quad_4( ctx, v1, v2, v3, v4, ormask );      \
 } while (0)
 
@@ -170,7 +173,7 @@ static void clip_elt_triangles( GLcontext *ctx,
       if (ormask) {
         if (start < j)
            render_tris( ctx, start, j, 0 );
-        if (!(c1&c2&c3&0x3f))
+        if (!(c1&c2&c3&CLIPMASK))
            clip_tri_4( ctx, elt[j], elt[j+1], elt[j+2], ormask );
         start = j+3;
       }
index b483a9522d1f5793db32c2ee0087bc34b74b90c1..3d6428188215abcc43d8d6e614a361bb39b64a72 100644 (file)
@@ -208,6 +208,7 @@ static GLboolean run_vertex_stage( GLcontext *ctx,
            return GL_FALSE;
       }
 
+      VB->ClipAndMask = store->andmask;
       VB->ClipOrMask = store->ormask;
       VB->ClipMask = store->clipmask;
 
@@ -222,6 +223,7 @@ static GLboolean run_vertex_stage( GLcontext *ctx,
       VB->ClipPtr = store->save_clipptr;
       VB->NdcPtr = store->save_ndcptr;
       VB->ClipMask = store->clipmask;
+      VB->ClipAndMask = store->andmask;
       VB->ClipOrMask = store->ormask;
       if (store->andmask)
         return GL_FALSE;
index e8681dcd034170977cdb95513aa48056d748285f..43d50308566dc7a277e7a49c33850d9e2d1cd33f 100644 (file)
@@ -1152,7 +1152,7 @@ static GLboolean TAG(validate_render)( GLcontext *ctx,
 {
    GLint i;
 
-   if (VB->ClipOrMask)
+   if (VB->ClipOrMask & ~CLIP_CULL_BIT)
       return GL_FALSE;
 
    if (VB->Elts && !HAVE_ELTS)