added dispatch override mechanism, used by trace extension
[mesa.git] / src / mesa / swrast / s_triangle.c
index 267a0f2c8e50e20901c527898c615dfe7b70a210..b61d4401e1bf07b58a99b5d294a1d2d8a4ea31ac 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: s_triangle.c,v 1.2 2000/11/05 18:24:41 keithw Exp $ */
+/* $Id: s_triangle.c,v 1.8 2001/01/05 02:26:49 keithw Exp $ */
 
 /*
  * Mesa 3-D graphics library
@@ -48,9 +48,9 @@
 #include "s_triangle.h"
  
 GLboolean gl_cull_triangle( GLcontext *ctx,
-                           SWvertex *v0, 
-                           SWvertex *v1, 
-                           SWvertex *v2 )
+                           const SWvertex *v0, 
+                           const SWvertex *v1, 
+                           const SWvertex *v2 )
 {
    GLfloat ex = v1->win[0] - v0->win[0];
    GLfloat ey = v1->win[1] - v0->win[1];
@@ -58,7 +58,7 @@ GLboolean gl_cull_triangle( GLcontext *ctx,
    GLfloat fy = v2->win[1] - v0->win[1];
    GLfloat c = ex*fy-ey*fx;
 
-   if (c * ctx->_backface_sign > 0)
+   if (c * SWRAST_CONTEXT(ctx)->_backface_sign > 0)
       return 0;
    
    return 1;
@@ -69,17 +69,11 @@ GLboolean gl_cull_triangle( GLcontext *ctx,
  * Render a flat-shaded color index triangle.
  */
 static void flat_ci_triangle( GLcontext *ctx,
-                             SWvertex *v0, 
-                             SWvertex *v1, 
-                             SWvertex *v2 )
+                             const SWvertex *v0, 
+                             const SWvertex *v1, 
+                             const SWvertex *v2 )
 {
 #define INTERP_Z 1
-#define SETUP_CODE                             \
-   GLuint index = v0->index;   \
-   if (1) {                                    \
-      /* set the color index */                        \
-      (*ctx->Driver.Index)( ctx, index );      \
-   }
 
 #define INNER_LOOP( LEFT, RIGHT, Y )                           \
        {                                                       \
@@ -95,7 +89,7 @@ static void flat_ci_triangle( GLcontext *ctx,
                 fffog += fdfogdx;                              \
              }                                                 \
              gl_write_monoindex_span( ctx, n, LEFT, Y, zspan,  \
-                                fogspan, index, GL_POLYGON );  \
+                         fogspan, v0->index, GL_POLYGON );     \
           }                                                    \
        }
 
@@ -108,9 +102,9 @@ static void flat_ci_triangle( GLcontext *ctx,
  * Render a smooth-shaded color index triangle.
  */
 static void smooth_ci_triangle( GLcontext *ctx,
-                               SWvertex *v0, 
-                               SWvertex *v1, 
-                               SWvertex *v2 )
+                               const SWvertex *v0, 
+                               const SWvertex *v1, 
+                               const SWvertex *v2 )
 {
 #define INTERP_Z 1
 #define INTERP_INDEX 1
@@ -145,23 +139,13 @@ static void smooth_ci_triangle( GLcontext *ctx,
  * Render a flat-shaded RGBA triangle.
  */
 static void flat_rgba_triangle( GLcontext *ctx,
-                               SWvertex *v0,
-                               SWvertex *v1,
-                               SWvertex *v2 )
+                               const SWvertex *v0,
+                               const SWvertex *v1,
+                               const SWvertex *v2 )
 {
 #define INTERP_Z 1
 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
 
-#define SETUP_CODE                             \
-   if (1) {                                    \
-      /* set the color */                      \
-      GLchan r = v0->color[0]; \
-      GLchan g = v0->color[1]; \
-      GLchan b = v0->color[2]; \
-      GLchan a = v0->color[3]; \
-      (*ctx->Driver.Color)( ctx, r, g, b, a ); \
-   }
-
 #define INNER_LOOP( LEFT, RIGHT, Y )                           \
        {                                                       \
           const GLint n = RIGHT-LEFT;                          \
@@ -176,8 +160,7 @@ static void flat_rgba_triangle( GLcontext *ctx,
                 fffog += fdfogdx;                              \
              }                                                 \
               gl_write_monocolor_span( ctx, n, LEFT, Y, zspan, \
-                                       fogspan, \
-                                       v0->color,      \
+                                       fogspan, v2->color,     \
                                       GL_POLYGON );            \
           }                                                    \
        }
@@ -194,9 +177,9 @@ static void flat_rgba_triangle( GLcontext *ctx,
  * Render a smooth-shaded RGBA triangle.
  */
 static void smooth_rgba_triangle( GLcontext *ctx,
-                                 SWvertex *v0,
-                                 SWvertex *v1,
-                                 SWvertex *v2 )
+                                 const SWvertex *v0,
+                                 const SWvertex *v1,
+                                 const SWvertex *v2 )
 {
 
 #define INTERP_Z 1
@@ -247,15 +230,15 @@ static void smooth_rgba_triangle( GLcontext *ctx,
  * No fog.
  */
 static void simple_textured_triangle( GLcontext *ctx,
-                                     SWvertex *v0,
-                                     SWvertex *v1,
-                                     SWvertex *v2 )
+                                     const SWvertex *v0,
+                                     const SWvertex *v1,
+                                     const SWvertex *v2 )
 {
 #define INTERP_INT_TEX 1
 #define S_SCALE twidth
 #define T_SCALE theight
 #define SETUP_CODE                                                     \
-   struct gl_texture_object *obj = ctx->Texture.Unit[0].CurrentD[2];   \
+   struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D;     \
    GLint b = obj->BaseLevel;                                           \
    GLfloat twidth = (GLfloat) obj->Image[b]->Width;                    \
    GLfloat theight = (GLfloat) obj->Image[b]->Height;                  \
@@ -306,9 +289,9 @@ static void simple_textured_triangle( GLcontext *ctx,
  * No fog.
  */
 static void simple_z_textured_triangle( GLcontext *ctx,
-                                       SWvertex *v0,
-                                       SWvertex *v1,
-                                       SWvertex *v2 )
+                                       const SWvertex *v0,
+                                       const SWvertex *v1,
+                                       const SWvertex *v2 )
 {
 #define INTERP_Z 1
 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
@@ -316,7 +299,7 @@ static void simple_z_textured_triangle( GLcontext *ctx,
 #define S_SCALE twidth
 #define T_SCALE theight
 #define SETUP_CODE                                                     \
-   struct gl_texture_object *obj = ctx->Texture.Unit[0].CurrentD[2];   \
+   struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D;     \
    GLint b = obj->BaseLevel;                                           \
    GLfloat twidth = (GLfloat) obj->Image[b]->Width;                    \
    GLfloat theight = (GLfloat) obj->Image[b]->Height;                  \
@@ -375,9 +358,9 @@ static void simple_z_textured_triangle( GLcontext *ctx,
  * Render an RGB/RGBA textured triangle without perspective correction.
  */
 static void affine_textured_triangle( GLcontext *ctx,
-                                     SWvertex *v0,
-                                     SWvertex *v1,
-                                     SWvertex *v2 )
+                                     const SWvertex *v0,
+                                     const SWvertex *v1,
+                                     const SWvertex *v2 )
 {
 #define INTERP_Z 1
 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
@@ -388,7 +371,7 @@ static void affine_textured_triangle( GLcontext *ctx,
 #define T_SCALE theight
 #define SETUP_CODE                                                     \
    struct gl_texture_unit *unit = ctx->Texture.Unit+0;                 \
-   struct gl_texture_object *obj = unit->CurrentD[2];                  \
+   struct gl_texture_object *obj = unit->Current2D;                    \
    GLint b = obj->BaseLevel;                                           \
    GLfloat twidth = (GLfloat) obj->Image[b]->Width;                    \
    GLfloat theight = (GLfloat) obj->Image[b]->Height;                  \
@@ -696,10 +679,11 @@ static void affine_textured_triangle( GLcontext *ctx,
  * This function written by Klaus Niederkrueger <klaus@math.leidenuniv.nl>
  * Send all questions and bug reports to him.
  */
+#if 0 /* XXX disabled because of texcoord interpolation errors */
 static void near_persp_textured_triangle(GLcontext *ctx,
-                                        SWvertex *v0,
-                                        SWvertex *v1,
-                                        SWvertex *v2 )
+                                        const SWvertex *v0,
+                                        const SWvertex *v1,
+                                        const SWvertex *v2 )
 {
 /* The BIAS value is used to shift negative values into positive values.
  * Without this, negative texture values don't GL_REPEAT correctly at just
@@ -716,7 +700,7 @@ static void near_persp_textured_triangle(GLcontext *ctx,
 #define INTERP_TEX 1
 #define SETUP_CODE                                                     \
    struct gl_texture_unit *unit = ctx->Texture.Unit+0;                 \
-   struct gl_texture_object *obj = unit->CurrentD[2];                  \
+   struct gl_texture_object *obj = unit->Current2D;                    \
    const GLint b = obj->BaseLevel;                                     \
    const GLfloat twidth = (GLfloat) obj->Image[b]->Width;              \
    const GLfloat theight = (GLfloat) obj->Image[b]->Height;            \
@@ -1425,7 +1409,7 @@ static void near_persp_textured_triangle(GLcontext *ctx,
 #undef DRAW_LINE
 #undef BIAS
 }
-
+#endif
 
 
 /*
@@ -1436,10 +1420,11 @@ static void near_persp_textured_triangle(GLcontext *ctx,
  * This function written by Klaus Niederkrueger <klaus@math.leidenuniv.nl>
  * Send all questions and bug reports to him.
  */
+#if 0 /* XXX disabled because of texcoord interpolation errors */
 static void lin_persp_textured_triangle( GLcontext *ctx,
-                                        SWvertex *v0,
-                                        SWvertex *v1,
-                                        SWvertex *v2 )
+                                        const SWvertex *v0,
+                                        const SWvertex *v1,
+                                        const SWvertex *v2 )
 {
 #define INTERP_Z 1
 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
@@ -1448,7 +1433,7 @@ static void lin_persp_textured_triangle( GLcontext *ctx,
 #define INTERP_TEX 1
 #define SETUP_CODE                                                     \
    struct gl_texture_unit *unit = ctx->Texture.Unit+0;                 \
-   struct gl_texture_object *obj = unit->CurrentD[2];                  \
+   struct gl_texture_object *obj = unit->Current2D;                    \
    const GLint b = obj->BaseLevel;                                     \
    const GLfloat twidth = (GLfloat) obj->Image[b]->Width;              \
    const GLfloat theight = (GLfloat) obj->Image[b]->Height;            \
@@ -1602,11 +1587,10 @@ static void lin_persp_textured_triangle( GLcontext *ctx,
       }                                                        \
    }
 
-
 #include "s_tritemp.h"
 #undef SPAN
 }
-
+#endif
 
 
 /*
@@ -1616,9 +1600,9 @@ static void lin_persp_textured_triangle( GLcontext *ctx,
  * R is already used for red.
  */
 static void general_textured_triangle( GLcontext *ctx,
-                                      SWvertex *v0,
-                                      SWvertex *v1,
-                                      SWvertex *v2 )
+                                      const SWvertex *v0,
+                                      const SWvertex *v1,
+                                      const SWvertex *v2 )
 {
 #define INTERP_Z 1
 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
@@ -1629,10 +1613,10 @@ static void general_textured_triangle( GLcontext *ctx,
    GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT);    \
    GLint r, g, b, a;                                           \
    if (flat_shade) {                                           \
-      r = v0->color[0];                                \
-      g = v0->color[1];                                \
-      b = v0->color[2];                                \
-      a = v0->color[3];                                \
+      r = v2->color[0];                                                \
+      g = v2->color[1];                                                \
+      b = v2->color[2];                                                \
+      a = v2->color[3];                                                \
    }
 #define INNER_LOOP( LEFT, RIGHT, Y )                           \
        {                                                       \
@@ -1706,9 +1690,9 @@ static void general_textured_triangle( GLcontext *ctx,
  * R is already used for red.
  */
 static void general_textured_spec_triangle1( GLcontext *ctx,
-                                            SWvertex *v0,
-                                            SWvertex *v1,
-                                            SWvertex *v2,
+                                            const SWvertex *v0,
+                                            const SWvertex *v1,
+                                            const SWvertex *v2,
                                              GLdepth zspan[MAX_WIDTH],
                                              GLfixed fogspan[MAX_WIDTH],
                                              GLchan rgba[MAX_WIDTH][4],
@@ -1724,13 +1708,13 @@ static void general_textured_spec_triangle1( GLcontext *ctx,
    GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT);    \
    GLint r, g, b, a, sr, sg, sb;                               \
    if (flat_shade) {                                           \
-      r = v0->color[0];                                \
-      g = v0->color[1];                                \
-      b = v0->color[2];                                \
-      a = v0->color[3];                                \
-      sr = v0->specular[0];                    \
-      sg = v0->specular[1];                    \
-      sb = v0->specular[2];                    \
+      r = v2->color[0];                                                \
+      g = v2->color[1];                                                \
+      b = v2->color[2];                                                \
+      a = v2->color[3];                                                \
+      sr = v2->specular[0];                                    \
+      sg = v2->specular[1];                                    \
+      sb = v2->specular[2];                                    \
    }
 #define INNER_LOOP( LEFT, RIGHT, Y )                           \
        {                                                       \
@@ -1831,9 +1815,9 @@ compute_lambda( GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
  * mipmaps, lambda is also used to select the texture level of detail.
  */
 static void lambda_textured_triangle1( GLcontext *ctx,
-                                      SWvertex *v0,
-                                      SWvertex *v1,
-                                      SWvertex *v2, 
+                                      const SWvertex *v0,
+                                      const SWvertex *v1,
+                                      const SWvertex *v2, 
                                        GLfloat s[MAX_WIDTH],
                                        GLfloat t[MAX_WIDTH],
                                        GLfloat u[MAX_WIDTH] )
@@ -1853,10 +1837,10 @@ static void lambda_textured_triangle1( GLcontext *ctx,
    const GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT);      \
    GLint r, g, b, a;                                                   \
    if (flat_shade) {                                                   \
-      r = v0->color[0];                                        \
-      g = v0->color[1];                                        \
-      b = v0->color[2];                                        \
-      a = v0->color[3];                                        \
+      r = v2->color[0];                                                        \
+      g = v2->color[1];                                                        \
+      b = v2->color[2];                                                        \
+      a = v2->color[3];                                                        \
    }
 
 #define INNER_LOOP( LEFT, RIGHT, Y )                                   \
@@ -1935,9 +1919,9 @@ static void lambda_textured_triangle1( GLcontext *ctx,
  * mipmaps, lambda is also used to select the texture level of detail.
  */
 static void lambda_textured_spec_triangle1( GLcontext *ctx,
-                                           SWvertex *v0,
-                                           SWvertex *v1,
-                                           SWvertex *v2,
+                                           const SWvertex *v0,
+                                           const SWvertex *v1,
+                                           const SWvertex *v2,
                                             GLfloat s[MAX_WIDTH],
                                             GLfloat t[MAX_WIDTH],
                                             GLfloat u[MAX_WIDTH] )
@@ -1958,13 +1942,13 @@ static void lambda_textured_spec_triangle1( GLcontext *ctx,
    const GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT);      \
    GLint r, g, b, a, sr, sg, sb;                                       \
    if (flat_shade) {                                                   \
-      r = v0->color[0];                                        \
-      g = v0->color[1];                                        \
-      b = v0->color[2];                                        \
-      a = v0->color[3];                                        \
-      sr = v0->specular[0];                            \
-      sg = v0->specular[1];                            \
-      sb = v0->specular[2];                            \
+      r = v2->color[0];                                                        \
+      g = v2->color[1];                                                        \
+      b = v2->color[2];                                                        \
+      a = v2->color[3];                                                        \
+      sr = v2->specular[0];                                            \
+      sg = v2->specular[1];                                            \
+      sb = v2->specular[2];                                            \
    }
 
 #define INNER_LOOP( LEFT, RIGHT, Y )                                   \
@@ -2052,9 +2036,9 @@ static void lambda_textured_spec_triangle1( GLcontext *ctx,
  */
 static void 
 lambda_multitextured_triangle1( GLcontext *ctx,
-                               SWvertex *v0,
-                               SWvertex *v1,
-                               SWvertex *v2,
+                               const SWvertex *v0,
+                               const SWvertex *v1,
+                               const SWvertex *v2,
                                GLfloat s[MAX_TEXTURE_UNITS][MAX_WIDTH],
                                GLfloat t[MAX_TEXTURE_UNITS][MAX_WIDTH],
                                GLfloat u[MAX_TEXTURE_UNITS][MAX_WIDTH])
@@ -2071,10 +2055,10 @@ lambda_multitextured_triangle1( GLcontext *ctx,
    GLfloat twidth[MAX_TEXTURE_UNITS], theight[MAX_TEXTURE_UNITS];              \
    GLint r, g, b, a;                                                           \
    if (flat_shade) {                                                           \
-      r = v0->color[0];                                                \
-      g = v0->color[1];                                                \
-      b = v0->color[2];                                                \
-      a = v0->color[3];                                                \
+      r = v2->color[0];                                                                \
+      g = v2->color[1];                                                                \
+      b = v2->color[2];                                                                \
+      a = v2->color[3];                                                                \
    }                                                                           \
    {                                                                           \
       GLuint unit;                                                             \
@@ -2181,9 +2165,9 @@ lambda_multitextured_triangle1( GLcontext *ctx,
  */
 
 static void general_textured_spec_triangle(GLcontext *ctx,
-                                          SWvertex *v0,
-                                          SWvertex *v1,
-                                          SWvertex *v2 )
+                                          const SWvertex *v0,
+                                          const SWvertex *v1,
+                                          const SWvertex *v2 )
 {
    GLdepth zspan[MAX_WIDTH];
    GLfixed fogspan[MAX_WIDTH];                    
@@ -2192,18 +2176,18 @@ static void general_textured_spec_triangle(GLcontext *ctx,
 }
 
 static void lambda_textured_triangle( GLcontext *ctx,
-                                     SWvertex *v0,
-                                     SWvertex *v1,
-                                     SWvertex *v2 )
+                                     const SWvertex *v0,
+                                     const SWvertex *v1,
+                                     const SWvertex *v2 )
 {
    GLfloat s[MAX_WIDTH], t[MAX_WIDTH], u[MAX_WIDTH];
    lambda_textured_triangle1(ctx,v0,v1,v2,s,t,u);
 }
 
 static void lambda_textured_spec_triangle( GLcontext *ctx,
-                                          SWvertex *v0,
-                                          SWvertex *v1,
-                                          SWvertex *v2 )
+                                          const SWvertex *v0,
+                                          const SWvertex *v1,
+                                          const SWvertex *v2 )
 {
    GLfloat s[MAX_WIDTH];
    GLfloat t[MAX_WIDTH];
@@ -2213,9 +2197,9 @@ static void lambda_textured_spec_triangle( GLcontext *ctx,
 
 
 static void lambda_multitextured_triangle( GLcontext *ctx,
-                                          SWvertex *v0,
-                                          SWvertex *v1,
-                                          SWvertex *v2 )
+                                          const SWvertex *v0,
+                                          const SWvertex *v1,
+                                          const SWvertex *v2 )
 {
 
    GLfloat s[MAX_TEXTURE_UNITS][MAX_WIDTH];
@@ -2231,9 +2215,9 @@ static void lambda_multitextured_triangle( GLcontext *ctx,
 
 
 static void occlusion_zless_triangle( GLcontext *ctx,
-                                     SWvertex *v0,
-                                     SWvertex *v1,
-                                     SWvertex *v2 )
+                                     const SWvertex *v0,
+                                     const SWvertex *v1,
+                                     const SWvertex *v2 )
 {
    if (ctx->OcclusionResult) {
       return;
@@ -2313,7 +2297,7 @@ _swrast_choose_triangle( GLcontext *ctx )
       if (ctx->Texture._ReallyEnabled) {
          /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
         GLint format, filter;
-        const struct gl_texture_object *current2Dtex = ctx->Texture.Unit[0].CurrentD[2];
+        const struct gl_texture_object *current2Dtex = ctx->Texture.Unit[0].Current2D;
          const struct gl_texture_image *image;
          /* First see if we can used an optimized 2-D texture function */
          if (ctx->Texture._ReallyEnabled==TEXTURE0_2D
@@ -2359,6 +2343,7 @@ _swrast_choose_triangle( GLcontext *ctx )
               }
            }
            else {
+#if 00 /* XXX these function have problems with texture coord interpolation */
               if (filter==GL_NEAREST) {
                 swrast->Triangle = near_persp_textured_triangle;
                 dputs("near_persp_textured_triangle");
@@ -2367,6 +2352,8 @@ _swrast_choose_triangle( GLcontext *ctx )
                 swrast->Triangle = lin_persp_textured_triangle;
                 dputs("lin_persp_textured_triangle");
               }
+#endif
+               swrast->Triangle = general_textured_triangle;
            }
         }
          else {
@@ -2378,7 +2365,7 @@ _swrast_choose_triangle( GLcontext *ctx )
                needLambda = GL_TRUE;
             else
                needLambda = GL_FALSE;
-            if (ctx->Texture._MultiTextureEnabled) {
+            if (swrast->_MultiTextureEnabled) {
                swrast->Triangle = lambda_multitextured_triangle;
               dputs("lambda_multitextured_triangle");
             }