tnl: Use gl_array_attribute::_ElementSize.
[mesa.git] / src / mesa / tnl / t_vb_vertex.c
index 2a61ff117795794e7107506e5f86a1c09c021fd2..4fb3659a3583d8757f7fd063e15208d9ed02afce 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
  *
  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  *
  * 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.
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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>
+ *    Keith Whitwell <keithw@vmware.com>
  */
 
 
 #include "main/glheader.h"
-#include "main/colormac.h"
-#include "main/context.h"
 #include "main/macros.h"
 #include "main/imports.h"
 #include "main/mtypes.h"
 
 #include "math/m_xform.h"
 
+#include "util/bitscan.h"
+
 #include "t_context.h"
 #include "t_pipeline.h"
 
@@ -44,7 +44,6 @@ struct vertex_stage_data {
    GLvector4f eye;
    GLvector4f clip;
    GLvector4f proj;
-   GLfloat *clipdistance[MAX_CLIP_PLANES];
    GLubyte *clipmask;
    GLubyte ormask;
    GLubyte andmask;
@@ -57,52 +56,48 @@ struct vertex_stage_data {
 
 /* This function implements cliptesting for user-defined clip planes.
  * The clipping of primitives to these planes is implemented in
- * t_vp_cliptmp.h.
+ * t_render_clip.h.
  */
 #define USER_CLIPTEST(NAME, SZ)                                        \
-static void NAME( GLcontext *ctx,                              \
+static void NAME( struct gl_context *ctx,                              \
                  GLvector4f *clip,                             \
-                 GLfloat *clipdistances[MAX_CLIP_PLANES],      \
                  GLubyte *clipmask,                            \
                  GLubyte *clipormask,                          \
                  GLubyte *clipandmask )                        \
 {                                                              \
-   GLuint p;                                                   \
-                                                               \
-   for (p = 0; p < ctx->Const.MaxClipPlanes; p++)              \
-      if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {       \
-        GLuint nr, i;                                          \
-        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]; \
-         GLfloat *coord = (GLfloat *)clip->data;               \
-         GLuint stride = clip->stride;                         \
-         GLuint count = clip->count;                           \
+   GLbitfield mask = ctx->Transform.ClipPlanesEnabled;          \
+   while (mask) {                                               \
+      const int p = u_bit_scan(&mask);                          \
+      GLuint nr, i;                                            \
+      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];   \
+      GLfloat *coord = (GLfloat *)clip->data;                   \
+      GLuint stride = clip->stride;                            \
+      GLuint count = clip->count;                              \
                                                                \
-        for (nr = 0, i = 0 ; i < count ; i++) {                \
-           GLfloat dp = coord[0] * a + coord[1] * b;           \
-           if (SZ > 2) dp += coord[2] * c;                     \
-           if (SZ > 3) dp += coord[3] * d; else dp += d;       \
+      for (nr = 0, i = 0 ; i < count ; i++) {                   \
+         GLfloat dp = coord[0] * a + coord[1] * b;             \
+         if (SZ > 2) dp += coord[2] * c;                       \
+         if (SZ > 3) dp += coord[3] * d; else dp += d;          \
                                                                \
-           if (dp < 0) {                                       \
-              nr++;                                            \
-              clipmask[i] |= CLIP_USER_BIT;                    \
-           }                                                   \
+         if (dp < 0) {                                          \
+            nr++;                                              \
+            clipmask[i] |= CLIP_USER_BIT;                      \
+         }                                                     \
                                                                \
-           clipdistances[p][i] = dp;                           \
+         STRIDE_F(coord, stride);                              \
+      }                                                         \
                                                                \
-           STRIDE_F(coord, stride);                            \
-        }                                                      \
-                                                               \
-        if (nr > 0) {                                          \
-           *clipormask |= CLIP_USER_BIT;                       \
-           if (nr == count) {                                  \
-              *clipandmask |= CLIP_USER_BIT;                   \
-              return;                                          \
-           }                                                   \
-        }                                                      \
+      if (nr > 0) {                                            \
+         *clipormask |= CLIP_USER_BIT;                          \
+         if (nr == count) {                                    \
+            *clipandmask |= CLIP_USER_BIT;                     \
+            return;                                            \
+         }                                                     \
       }                                                                \
+   }                                                           \
 }
 
 
@@ -110,10 +105,9 @@ USER_CLIPTEST(userclip2, 2)
 USER_CLIPTEST(userclip3, 3)
 USER_CLIPTEST(userclip4, 4)
 
-static void (*(usercliptab[5]))( GLcontext *,
-                                GLvector4f *,
-                                GLfloat *[MAX_CLIP_PLANES],
-                                GLubyte *, GLubyte *, GLubyte * ) =
+static void (*(usercliptab[5]))( struct gl_context *,
+                                GLvector4f *, GLubyte *,
+                                GLubyte *, GLubyte * ) =
 {
    NULL,
    NULL,
@@ -124,12 +118,12 @@ static void (*(usercliptab[5]))( GLcontext *,
 
 
 void
-tnl_clip_prepare(GLcontext *ctx)
+tnl_clip_prepare(struct gl_context *ctx)
 {
    /* Neither the x86 nor sparc asm cliptest functions have been updated
     * for ARB_depth_clamp, so force the C paths.
     */
-   if (ctx->Transform.DepthClamp) {
+   if (ctx->Transform.DepthClampNear && ctx->Transform.DepthClampFar) {
       static GLboolean c_funcs_installed = GL_FALSE;
       if (!c_funcs_installed) {
          init_c_cliptest();
@@ -140,7 +134,7 @@ tnl_clip_prepare(GLcontext *ctx)
 
 
 
-static GLboolean run_vertex_stage( GLcontext *ctx,
+static GLboolean run_vertex_stage( struct gl_context *ctx,
                                   struct tnl_pipeline_stage *stage )
 {
    struct vertex_stage_data *store = (struct vertex_stage_data *)stage->privatePtr;
@@ -157,16 +151,16 @@ static GLboolean run_vertex_stage( GLcontext *ctx,
        * Use combined ModelProject to avoid some depth artifacts
        */
       if (ctx->ModelviewMatrixStack.Top->type == MATRIX_IDENTITY)
-        VB->EyePtr = VB->ObjPtr;
+        VB->EyePtr = VB->AttribPtr[_TNL_ATTRIB_POS];
       else
         VB->EyePtr = TransformRaw( &store->eye,
                                    ctx->ModelviewMatrixStack.Top,
-                                   VB->ObjPtr);
+                                   VB->AttribPtr[_TNL_ATTRIB_POS]);
    }
 
    VB->ClipPtr = TransformRaw( &store->clip,
                               &ctx->_ModelProjectMatrix,
-                              VB->ObjPtr );
+                              VB->AttribPtr[_TNL_ATTRIB_POS] );
 
    /* Drivers expect this to be clean to element 4...
     */
@@ -197,7 +191,8 @@ static GLboolean run_vertex_stage( GLcontext *ctx,
                                            store->clipmask,
                                            &store->ormask,
                                            &store->andmask,
-                                           !ctx->Transform.DepthClamp );
+                                           !(ctx->Transform.DepthClampNear &&
+                                              ctx->Transform.DepthClampFar) );
    }
    else {
       VB->NdcPtr = NULL;
@@ -206,7 +201,8 @@ static GLboolean run_vertex_stage( GLcontext *ctx,
                                            store->clipmask,
                                            &store->ormask,
                                            &store->andmask,
-                                           !ctx->Transform.DepthClamp );
+                                           !(ctx->Transform.DepthClampNear &&
+                                              ctx->Transform.DepthClampFar) );
    }
 
    if (store->andmask)
@@ -219,16 +215,12 @@ static GLboolean run_vertex_stage( GLcontext *ctx,
    if (ctx->Transform.ClipPlanesEnabled) {
       usercliptab[VB->ClipPtr->size]( ctx,
                                      VB->ClipPtr,
-                                     store->clipdistance,
                                      store->clipmask,
                                      &store->ormask,
                                      &store->andmask );
 
       if (store->andmask)
         return GL_FALSE;
-
-      memcpy(VB->ClipDistancePtr, store->clipdistance,
-            sizeof(store->clipdistance));
    }
 
    VB->ClipAndMask = store->andmask;
@@ -239,15 +231,14 @@ static GLboolean run_vertex_stage( GLcontext *ctx,
 }
 
 
-static GLboolean init_vertex_stage( GLcontext *ctx,
+static GLboolean init_vertex_stage( struct gl_context *ctx,
                                    struct tnl_pipeline_stage *stage )
 {
    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
    struct vertex_stage_data *store;
    GLuint size = VB->Size;
-   unsigned i;
 
-   stage->privatePtr = CALLOC(sizeof(*store));
+   stage->privatePtr = calloc(1, sizeof(*store));
    store = VERTEX_STAGE_DATA(stage);
    if (!store)
       return GL_FALSE;
@@ -256,18 +247,9 @@ static GLboolean init_vertex_stage( GLcontext *ctx,
    _mesa_vector4f_alloc( &store->clip, 0, size, 32 );
    _mesa_vector4f_alloc( &store->proj, 0, size, 32 );
 
-   store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
-   for (i = 0; i < MAX_CLIP_PLANES; i++)
-      store->clipdistance[i] =
-        (GLfloat *) ALIGN_MALLOC(sizeof(GLfloat) * size, 32);
+   store->clipmask = _mesa_align_malloc(sizeof(GLubyte)*size, 32 );
 
    if (!store->clipmask ||
-       !store->clipdistance[0] ||
-       !store->clipdistance[1] ||
-       !store->clipdistance[2] ||
-       !store->clipdistance[3] ||
-       !store->clipdistance[4] ||
-       !store->clipdistance[5] ||
        !store->eye.data ||
        !store->clip.data ||
        !store->proj.data)
@@ -281,17 +263,11 @@ static void dtr( struct tnl_pipeline_stage *stage )
    struct vertex_stage_data *store = VERTEX_STAGE_DATA(stage);
 
    if (store) {
-      unsigned i;
-
       _mesa_vector4f_free( &store->eye );
       _mesa_vector4f_free( &store->clip );
       _mesa_vector4f_free( &store->proj );
-      ALIGN_FREE( store->clipmask );
-
-      for (i = 0; i < MAX_CLIP_PLANES; i++)
-        ALIGN_FREE(store->clipdistance[i]);
-
-      FREE(store);
+      _mesa_align_free( store->clipmask );
+      free(store);
       stage->privatePtr = NULL;
       stage->run = init_vertex_stage;
    }