mesa: don't include m_xform.h where not needed
[mesa.git] / src / mesa / main / matrix.c
index c6459947fe5846ebaca96abefa6a144cb0dfb3cb..90d142278d183f85c1c006a23ea3b09b87ac0615 100644 (file)
@@ -1,21 +1,19 @@
-/* $Id: matrix.c,v 1.1 1999/08/19 00:55:41 jtg Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.1
- * 
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
- * 
+ * Version:  6.5.3
+ *
+ * Copyright (C) 1999-2007  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
  */
 
 
-
-
-
-/*
- * Matrix operations
- *
- *
- * NOTES:
- * 1. 4x4 transformation matrices are stored in memory in column major order.
- * 2. Points/vertices are to be thought of as column vectors.
- * 3. Transformation of a point p by a matrix M is: p' = M * p
+/**
+ * \file matrix.c
+ * Matrix operations.
  *
+ * \note
+ * -# 4x4 transformation matrices are stored in memory in column major order.
+ * -# Points/vertices are to be thought of as column vectors.
+ * -# Transformation of a point p by a matrix M is: p' = M * p
  */
 
 
-#ifdef PC_HEADER
-#include "all.h"
-#else
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "glheader.h"
+#include "imports.h"
 #include "context.h"
 #include "enums.h"
 #include "macros.h"
 #include "matrix.h"
-#include "mmath.h"
-#include "types.h"
-#ifdef XFree86Server
-#include "GL/xf86glx.h"
-#endif
-#endif
-
-
-static const char *types[] = {
-   "MATRIX_GENERAL",
-   "MATRIX_IDENTITY",
-   "MATRIX_3D_NO_ROT",
-   "MATRIX_PERSPECTIVE",
-   "MATRIX_2D",
-   "MATRIX_2D_NO_ROT",
-   "MATRIX_3D"
-};
-static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b );
+#include "mtypes.h"
+#include "math/m_matrix.h"
 
 
-static GLfloat Identity[16] = {
-   1.0, 0.0, 0.0, 0.0,
-   0.0, 1.0, 0.0, 0.0,
-   0.0, 0.0, 1.0, 0.0,
-   0.0, 0.0, 0.0, 1.0
-};
-
-
-static void print_matrix_floats( const GLfloat m[16] )
+/**
+ * Apply a perspective projection matrix.
+ *
+ * \param left left clipping plane coordinate.
+ * \param right right clipping plane coordinate.
+ * \param bottom bottom clipping plane coordinate.
+ * \param top top clipping plane coordinate.
+ * \param nearval distance to the near clipping plane.
+ * \param farval distance to the far clipping plane.
+ *
+ * \sa glFrustum().
+ *
+ * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with
+ * the top matrix of the current matrix stack and sets
+ * __GLcontextRec::NewState.
+ */
+void GLAPIENTRY
+_mesa_Frustum( GLdouble left, GLdouble right,
+               GLdouble bottom, GLdouble top,
+               GLdouble nearval, GLdouble farval )
 {
-   int i;
-   for (i=0;i<4;i++) {
-      fprintf(stderr,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+
+   if (nearval <= 0.0 ||
+       farval <= 0.0 ||
+       nearval == farval ||
+       left == right ||
+       top == bottom)
+   {
+      _mesa_error( ctx,  GL_INVALID_VALUE, "glFrustum" );
+      return;
    }
-}
 
-void gl_print_matrix( const GLmatrix *m )
-{
-   fprintf(stderr, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
-   print_matrix_floats(m->m);
-#if 1
-   fprintf(stderr, "Inverse: \n");
-   if (m->inv) {
-      GLfloat prod[16];
-      print_matrix_floats(m->inv);
-      matmul4(prod, m->m, m->inv);
-      fprintf(stderr, "Mat * Inverse:\n");
-      print_matrix_floats(prod);
-  } else 
-      fprintf(stderr, "  - not available\n");
-#endif
+   _math_matrix_frustum( ctx->CurrentStack->Top,
+                         (GLfloat) left, (GLfloat) right, 
+                        (GLfloat) bottom, (GLfloat) top, 
+                        (GLfloat) nearval, (GLfloat) farval );
+   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
 }
 
 
-
-/*
- * This matmul was contributed by Thomas Malik 
+/**
+ * Apply an orthographic projection matrix.
+ *
+ * \param left left clipping plane coordinate.
+ * \param right right clipping plane coordinate.
+ * \param bottom bottom clipping plane coordinate.
+ * \param top top clipping plane coordinate.
+ * \param nearval distance to the near clipping plane.
+ * \param farval distance to the far clipping plane.
  *
- * Perform a 4x4 matrix multiplication  (product = a x b).
- * Input:  a, b - matrices to multiply
- * Output:  product - product of a and b
- * WARNING: (product != b) assumed
- * NOTE:    (product == a) allowed    
+ * \sa glOrtho().
  *
- * KW: 4*16 = 64 muls
+ * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
+ * the top matrix of the current matrix stack and sets
+ * __GLcontextRec::NewState.
  */
-#define A(row,col)  a[(col<<2)+row]
-#define B(row,col)  b[(col<<2)+row]
-#define P(row,col)  product[(col<<2)+row]
-
-static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
+void GLAPIENTRY
+_mesa_Ortho( GLdouble left, GLdouble right,
+             GLdouble bottom, GLdouble top,
+             GLdouble nearval, GLdouble farval )
 {
-   GLint i;
-   for (i = 0; i < 4; i++) {
-      GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);
-      P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
-      P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
-      P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
-      P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
-   }
-}
-
-
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx, "glOrtho(%f, %f, %f, %f, %f, %f)\n",
+                  left, right, bottom, top, nearval, farval);
 
-/* Multiply two matrices known to occupy only the top three rows,
- * such as typical modelling matrices, and ortho matrices.
- *
- * KW: 3*9 = 27 muls
- */
-static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
-{
-   GLint i;
-   for (i = 0; i < 3; i++) {
-      GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);
-      P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
-      P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
-      P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
-      P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
+   if (left == right ||
+       bottom == top ||
+       nearval == farval)
+   {
+      _mesa_error( ctx,  GL_INVALID_VALUE, "glOrtho" );
+      return;
    }
-   P(3,0) = 0;
-   P(3,1) = 0;
-   P(3,2) = 0;
-   P(3,3) = 1;
-}
 
-static void matmul4fd( GLfloat *product, const GLfloat *a, const GLdouble *b )
-{
-   GLint i;
-   for (i = 0; i < 4; i++) {
-      GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);
-      P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
-      P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
-      P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
-      P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
-   }
+   _math_matrix_ortho( ctx->CurrentStack->Top,
+                       (GLfloat) left, (GLfloat) right, 
+                      (GLfloat) bottom, (GLfloat) top, 
+                      (GLfloat) nearval, (GLfloat) farval );
+   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
 }
 
-#undef A
-#undef B
-#undef P
-
-
-
-#define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
-#define MAT(m,r,c) (m)[(c)*4+(r)]
 
-/*
- * Compute inverse of 4x4 transformation matrix.
- * Code contributed by Jacques Leroy jle@star.be
- * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
+/**
+ * Set the current matrix stack.
+ *
+ * \param mode matrix stack.
+ *
+ * \sa glMatrixMode().
+ *
+ * Flushes the vertices, validates the parameter and updates
+ * __GLcontextRec::CurrentStack and gl_transform_attrib::MatrixMode with the
+ * specified matrix stack.
  */
-static GLboolean invert_matrix_general( GLmatrix *mat )
+void GLAPIENTRY
+_mesa_MatrixMode( GLenum mode )
 {
-   const GLfloat *m = mat->m;
-   GLfloat *out = mat->inv;
-   GLfloat wtmp[4][8];
-   GLfloat m0, m1, m2, m3, s;
-   GLfloat *r0, *r1, *r2, *r3;
-  
-   r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
-  
-   r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
-   r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
-   r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
-  
-   r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
-   r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
-   r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
-  
-   r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
-   r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
-   r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
-  
-   r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
-   r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
-   r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
-  
-   /* choose pivot - or die */
-   if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
-   if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
-   if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
-   if (0.0 == r0[0])  return GL_FALSE;
-  
-   /* eliminate first variable     */
-   m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
-   s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
-   s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
-   s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
-   s = r0[4];
-   if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
-   s = r0[5];
-   if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
-   s = r0[6];
-   if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
-   s = r0[7];
-   if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
-  
-   /* choose pivot - or die */
-   if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
-   if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
-   if (0.0 == r1[1])  return GL_FALSE;
-  
-   /* eliminate second variable */
-   m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
-   r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
-   r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
-   s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
-   s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
-   s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
-   s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
-  
-   /* choose pivot - or die */
-   if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
-   if (0.0 == r2[2])  return GL_FALSE;
-  
-   /* eliminate third variable */
-   m3 = r3[2]/r2[2];
-   r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
-   r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
-   r3[7] -= m3 * r2[7];
-  
-   /* last check */
-   if (0.0 == r3[3]) return GL_FALSE;
-  
-   s = 1.0/r3[3];              /* now back substitute row 3 */
-   r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
-  
-   m2 = r2[3];                 /* now back substitute row 2 */
-   s  = 1.0/r2[2];
-   r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
-   r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
-   m1 = r1[3];
-   r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
-   r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
-   m0 = r0[3];
-   r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
-   r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
-  
-   m1 = r1[2];                 /* now back substitute row 1 */
-   s  = 1.0/r1[1];
-   r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
-   r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
-   m0 = r0[2];
-   r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
-   r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
-  
-   m0 = r0[1];                 /* now back substitute row 0 */
-   s  = 1.0/r0[0];
-   r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
-   r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
-  
-   MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
-   MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
-   MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
-   MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
-   MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
-   MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
-   MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
-   MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; 
-  
-   return GL_TRUE;
-}
-#undef SWAP_ROWS
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-/* Adapted from graphics gems II.
- */  
-GLboolean invert_matrix_3d_general( GLmatrix *mat )
-{
-   const GLfloat *in = mat->m;
-   GLfloat *out = mat->inv;
-   GLfloat pos, neg, t;
-   GLfloat det;
+   if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
+      return;
+   FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
 
-   /* Calculate the determinant of upper left 3x3 submatrix and
-    * determine if the matrix is singular. 
-    */
-   pos = neg = 0.0;
-   t =  MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
-   if (t >= 0.0) pos += t; else neg += t;
-
-   t =  MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
-   if (t >= 0.0) pos += t; else neg += t;
-
-   t =  MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
-   if (t >= 0.0) pos += t; else neg += t;
-
-   t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
-   if (t >= 0.0) pos += t; else neg += t;
-
-   t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
-   if (t >= 0.0) pos += t; else neg += t;
-
-   t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
-   if (t >= 0.0) pos += t; else neg += t;
-
-   det = pos + neg;
-
-   if (det*det < 1e-25) 
-      return GL_FALSE;
-   
-   det = 1.0 / det;
-   MAT(out,0,0) = (  (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
-   MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
-   MAT(out,0,2) = (  (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
-   MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
-   MAT(out,1,1) = (  (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
-   MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
-   MAT(out,2,0) = (  (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
-   MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
-   MAT(out,2,2) = (  (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
-
-   /* Do the translation part */
-   MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
-                    MAT(in,1,3) * MAT(out,0,1) +
-                    MAT(in,2,3) * MAT(out,0,2) );
-   MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
-                    MAT(in,1,3) * MAT(out,1,1) +
-                    MAT(in,2,3) * MAT(out,1,2) );
-   MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
-                    MAT(in,1,3) * MAT(out,2,1) +
-                    MAT(in,2,3) * MAT(out,2,2) );
-    
-   return GL_TRUE;
+   switch (mode) {
+   case GL_MODELVIEW:
+      ctx->CurrentStack = &ctx->ModelviewMatrixStack;
+      break;
+   case GL_PROJECTION:
+      ctx->CurrentStack = &ctx->ProjectionMatrixStack;
+      break;
+   case GL_TEXTURE:
+      if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
+         _mesa_error(ctx, GL_INVALID_OPERATION, "glMatrixMode(texcoord unit)");
+         return;
+      }
+      ctx->CurrentStack = &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
+      break;
+   case GL_COLOR:
+      ctx->CurrentStack = &ctx->ColorMatrixStack;
+      break;
+   case GL_MATRIX0_NV:
+   case GL_MATRIX1_NV:
+   case GL_MATRIX2_NV:
+   case GL_MATRIX3_NV:
+   case GL_MATRIX4_NV:
+   case GL_MATRIX5_NV:
+   case GL_MATRIX6_NV:
+   case GL_MATRIX7_NV:
+      if (ctx->Extensions.NV_vertex_program) {
+         ctx->CurrentStack = &ctx->ProgramMatrixStack[mode - GL_MATRIX0_NV];
+      }
+      else {
+         _mesa_error( ctx,  GL_INVALID_ENUM, "glMatrixMode(mode)" );
+         return;
+      }
+      break;
+   case GL_MATRIX0_ARB:
+   case GL_MATRIX1_ARB:
+   case GL_MATRIX2_ARB:
+   case GL_MATRIX3_ARB:
+   case GL_MATRIX4_ARB:
+   case GL_MATRIX5_ARB:
+   case GL_MATRIX6_ARB:
+   case GL_MATRIX7_ARB:
+      if (ctx->Extensions.ARB_vertex_program ||
+          ctx->Extensions.ARB_fragment_program) {
+         const GLuint m = mode - GL_MATRIX0_ARB;
+         if (m > ctx->Const.MaxProgramMatrices) {
+            _mesa_error(ctx, GL_INVALID_ENUM,
+                        "glMatrixMode(GL_MATRIX%d_ARB)", m);
+            return;
+         }
+         ctx->CurrentStack = &ctx->ProgramMatrixStack[m];
+      }
+      else {
+         _mesa_error( ctx,  GL_INVALID_ENUM, "glMatrixMode(mode)" );
+         return;
+      }
+      break;
+   default:
+      _mesa_error( ctx,  GL_INVALID_ENUM, "glMatrixMode(mode)" );
+      return;
+   }
+
+   ctx->Transform.MatrixMode = mode;
 }
 
 
-static GLboolean invert_matrix_3d( GLmatrix *mat )
+/**
+ * Push the current matrix stack.
+ *
+ * \sa glPushMatrix().
+ * 
+ * Verifies the current matrix stack is not full, and duplicates the top-most
+ * matrix in the stack. Marks __GLcontextRec::NewState with the stack dirty
+ * flag.
+ */
+void GLAPIENTRY
+_mesa_PushMatrix( void )
 {
-   const GLfloat *in = mat->m;
-   GLfloat *out = mat->inv;
+   GET_CURRENT_CONTEXT(ctx);
+   struct gl_matrix_stack *stack = ctx->CurrentStack;
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING))
-   {
-      return invert_matrix_3d_general( mat );
-   }
-   
-   if (mat->flags & MAT_FLAG_UNIFORM_SCALE)
-   {
-      GLfloat  scale = (MAT(in,0,0) * MAT(in,0,0) +
-                       MAT(in,0,1) * MAT(in,0,1) +
-                       MAT(in,0,2) * MAT(in,0,2));
-
-      if (scale == 0.0) 
-         return GL_FALSE;
-
-      scale = 1.0 / scale;
-
-      /* Transpose and scale the 3 by 3 upper-left submatrix. */
-      MAT(out,0,0) = scale * MAT(in,0,0);
-      MAT(out,1,0) = scale * MAT(in,0,1);
-      MAT(out,2,0) = scale * MAT(in,0,2);
-      MAT(out,0,1) = scale * MAT(in,1,0);
-      MAT(out,1,1) = scale * MAT(in,1,1);
-      MAT(out,2,1) = scale * MAT(in,1,2);
-      MAT(out,0,2) = scale * MAT(in,2,0);
-      MAT(out,1,2) = scale * MAT(in,2,1);
-      MAT(out,2,2) = scale * MAT(in,2,2);
-   }
-   else if (mat->flags & MAT_FLAG_ROTATION)
-   {
-      /* Transpose the 3 by 3 upper-left submatrix. */
-      MAT(out,0,0) = MAT(in,0,0);
-      MAT(out,1,0) = MAT(in,0,1);
-      MAT(out,2,0) = MAT(in,0,2);
-      MAT(out,0,1) = MAT(in,1,0);
-      MAT(out,1,1) = MAT(in,1,1);
-      MAT(out,2,1) = MAT(in,1,2);
-      MAT(out,0,2) = MAT(in,2,0);
-      MAT(out,1,2) = MAT(in,2,1);
-      MAT(out,2,2) = MAT(in,2,2);
-   }
-   else /* pure translation */
-   {
-      MEMCPY( out, Identity, sizeof(Identity) );
-      MAT(out,0,3) = - MAT(in,0,3);
-      MAT(out,1,3) = - MAT(in,1,3);
-      MAT(out,2,3) = - MAT(in,2,3);
-      return GL_TRUE;
-   }
-    
-   if (mat->flags & MAT_FLAG_TRANSLATION)
-   {
-      /* Do the translation part */
-      MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
-                       MAT(in,1,3) * MAT(out,0,1) +
-                       MAT(in,2,3) * MAT(out,0,2) );
-      MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
-                       MAT(in,1,3) * MAT(out,1,1) +
-                       MAT(in,2,3) * MAT(out,1,2) );
-      MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
-                       MAT(in,1,3) * MAT(out,2,1) +
-                       MAT(in,2,3) * MAT(out,2,2) );
-   }
-   else 
-   {
-      MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
+   if (MESA_VERBOSE&VERBOSE_API)
+      _mesa_debug(ctx, "glPushMatrix %s\n",
+                  _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
+
+   if (stack->Depth + 1 >= stack->MaxDepth) {
+      if (ctx->Transform.MatrixMode == GL_TEXTURE) {
+         _mesa_error(ctx,  GL_STACK_OVERFLOW,
+                     "glPushMatrix(mode=GL_TEXTURE, unit=%d)",
+                      ctx->Texture.CurrentUnit);
+      }
+      else {
+         _mesa_error(ctx,  GL_STACK_OVERFLOW, "glPushMatrix(mode=%s)",
+                     _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
+      }
+      return;
    }
-    
-   return GL_TRUE;
+   _math_matrix_copy( &stack->Stack[stack->Depth + 1],
+                      &stack->Stack[stack->Depth] );
+   stack->Depth++;
+   stack->Top = &(stack->Stack[stack->Depth]);
+   ctx->NewState |= stack->DirtyFlag;
 }
 
-  
 
-static GLboolean invert_matrix_identity( GLmatrix *mat )
+/**
+ * Pop the current matrix stack.
+ *
+ * \sa glPopMatrix().
+ * 
+ * Flushes the vertices, verifies the current matrix stack is not empty, and
+ * moves the stack head down. Marks __GLcontextRec::NewState with the dirty
+ * stack flag.
+ */
+void GLAPIENTRY
+_mesa_PopMatrix( void )
 {
-   MEMCPY( mat->inv, Identity, sizeof(Identity) );
-   return GL_TRUE;
-}
-
+   GET_CURRENT_CONTEXT(ctx);
+   struct gl_matrix_stack *stack = ctx->CurrentStack;
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
-{
-   const GLfloat *in = mat->m;
-   GLfloat *out = mat->inv;
-
-   if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )       
-      return GL_FALSE;
-  
-   MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
-   MAT(out,0,0) = 1.0 / MAT(in,0,0);
-   MAT(out,1,1) = 1.0 / MAT(in,1,1);
-   MAT(out,2,2) = 1.0 / MAT(in,2,2);
-
-   if (mat->flags & MAT_FLAG_TRANSLATION)
-   {
-      MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
-      MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
-      MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
+   if (MESA_VERBOSE&VERBOSE_API)
+      _mesa_debug(ctx, "glPopMatrix %s\n",
+                  _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
+
+   if (stack->Depth == 0) {
+      if (ctx->Transform.MatrixMode == GL_TEXTURE) {
+         _mesa_error(ctx,  GL_STACK_UNDERFLOW,
+                     "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
+                      ctx->Texture.CurrentUnit);
+      }
+      else {
+         _mesa_error(ctx,  GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)",
+                     _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
+      }
+      return;
    }
-
-   return GL_TRUE;
+   stack->Depth--;
+   stack->Top = &(stack->Stack[stack->Depth]);
+   ctx->NewState |= stack->DirtyFlag;
 }
 
 
-static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
+/**
+ * Replace the current matrix with the identity matrix.
+ *
+ * \sa glLoadIdentity().
+ *
+ * Flushes the vertices and calls _math_matrix_set_identity() with the top-most
+ * matrix in the current stack. Marks __GLcontextRec::NewState with the stack
+ * dirty flag.
+ */
+void GLAPIENTRY
+_mesa_LoadIdentity( void )
 {
-   const GLfloat *in = mat->m;
-   GLfloat *out = mat->inv;
-
-   if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)       
-      return GL_FALSE;
-  
-   MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
-   MAT(out,0,0) = 1.0 / MAT(in,0,0);
-   MAT(out,1,1) = 1.0 / MAT(in,1,1);
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   if (mat->flags & MAT_FLAG_TRANSLATION)
-   {
-      MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
-      MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
-   }
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx, "glLoadIdentity()");
 
-   return GL_TRUE;
+   _math_matrix_set_identity( ctx->CurrentStack->Top );
+   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
 }
 
 
-static GLboolean invert_matrix_perspective( GLmatrix *mat )
+/**
+ * Replace the current matrix with a given matrix.
+ *
+ * \param m matrix.
+ *
+ * \sa glLoadMatrixf().
+ *
+ * Flushes the vertices and calls _math_matrix_loadf() with the top-most matrix
+ * in the current stack and the given matrix. Marks __GLcontextRec::NewState
+ * with the dirty stack flag.
+ */
+void GLAPIENTRY
+_mesa_LoadMatrixf( const GLfloat *m )
 {
-   const GLfloat *in = mat->m;
-   GLfloat *out = mat->inv;
-
-   if (MAT(in,2,3) == 0)
-      return GL_FALSE;
-
-   MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
-
-   MAT(out,0,0) = 1.0 / MAT(in,0,0);
-   MAT(out,1,1) = 1.0 / MAT(in,1,1);
-
-   MAT(out,0,3) = MAT(in,0,2);
-   MAT(out,1,3) = MAT(in,1,2);
-
-   MAT(out,2,2) = 0;
-   MAT(out,2,3) = -1;
-
-   MAT(out,3,2) = 1.0 / MAT(in,2,3);
-   MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
+   GET_CURRENT_CONTEXT(ctx);
+   if (!m) return;
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx,
+          "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
+          m[0], m[4], m[8], m[12],
+          m[1], m[5], m[9], m[13],
+          m[2], m[6], m[10], m[14],
+          m[3], m[7], m[11], m[15]);
 
-   return GL_TRUE;
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   _math_matrix_loadf( ctx->CurrentStack->Top, m );
+   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
 }
 
 
-typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
-
-static inv_mat_func inv_mat_tab[7] = {
-   invert_matrix_general,
-   invert_matrix_identity,
-   invert_matrix_3d_no_rot,
-   invert_matrix_perspective,
-   invert_matrix_3d,           /* lazy! */
-   invert_matrix_2d_no_rot,
-   invert_matrix_3d
-};
-
-
-GLboolean gl_matrix_invert( GLmatrix *mat )
+/**
+ * Multiply the current matrix with a given matrix.
+ *
+ * \param m matrix.
+ *
+ * \sa glMultMatrixf().
+ *
+ * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
+ * matrix in the current stack and the given matrix. Marks
+ * __GLcontextRec::NewState with the dirty stack flag.
+ */
+void GLAPIENTRY
+_mesa_MultMatrixf( const GLfloat *m )
 {
-   if (inv_mat_tab[mat->type](mat)) {
-#if 0
-      GLmatrix m; m.inv = 0; m.type = 0; m.flags = 0;
-      matmul4( m.m, mat->m, mat->inv );
-      printf("inverted matrix of type %s:\n", types[mat->type]);
-      gl_print_matrix( mat );
-      gl_print_matrix( &m );
-#endif
-      return GL_TRUE;
-   } else {
-      MEMCPY( mat->inv, Identity, sizeof(Identity) );
-      return GL_FALSE;
-   }  
+   GET_CURRENT_CONTEXT(ctx);
+   if (!m) return;
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx,
+          "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
+          m[0], m[4], m[8], m[12],
+          m[1], m[5], m[9], m[13],
+          m[2], m[6], m[10], m[14],
+          m[3], m[7], m[11], m[15]);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
+   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
 }
 
 
-
-/*
- * Generate a 4x4 transformation matrix from glRotate parameters.
+/**
+ * Multiply the current matrix with a rotation matrix.
+ *
+ * \param angle angle of rotation, in degrees.
+ * \param x rotation vector x coordinate.
+ * \param y rotation vector y coordinate.
+ * \param z rotation vector z coordinate.
+ *
+ * \sa glRotatef().
+ *
+ * Flushes the vertices and calls _math_matrix_rotate() with the top-most
+ * matrix in the current stack and the given parameters. Marks
+ * __GLcontextRec::NewState with the dirty stack flag.
  */
-void gl_rotation_matrix( GLfloat angle, GLfloat x, GLfloat y, GLfloat z,
-                         GLfloat m[] )
+void GLAPIENTRY
+_mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
 {
-   /* This function contributed by Erich Boleyn (erich@uruk.org) */
-   GLfloat mag, s, c;
-   GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
-
-   s = sin( angle * DEG2RAD );
-   c = cos( angle * DEG2RAD );
-
-   mag = GL_SQRT( x*x + y*y + z*z );
-
-   if (mag == 0.0) {
-      /* generate an identity matrix and return */
-      MEMCPY(m, Identity, sizeof(GLfloat)*16);
-      return;
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   if (angle != 0.0F) {
+      _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
+      ctx->NewState |= ctx->CurrentStack->DirtyFlag;
    }
-
-   x /= mag;
-   y /= mag;
-   z /= mag;
-
-#define M(row,col)  m[col*4+row]
-
-   /*
-    *     Arbitrary axis rotation matrix.
-    *
-    *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
-    *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
-    *  (which is about the X-axis), and the two composite transforms
-    *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
-    *  from the arbitrary axis to the X-axis then back.  They are
-    *  all elementary rotations.
-    *
-    *  Rz' is a rotation about the Z-axis, to bring the axis vector
-    *  into the x-z plane.  Then Ry' is applied, rotating about the
-    *  Y-axis to bring the axis vector parallel with the X-axis.  The
-    *  rotation about the X-axis is then performed.  Ry and Rz are
-    *  simply the respective inverse transforms to bring the arbitrary
-    *  axis back to it's original orientation.  The first transforms
-    *  Rz' and Ry' are considered inverses, since the data from the
-    *  arbitrary axis gives you info on how to get to it, not how
-    *  to get away from it, and an inverse must be applied.
-    *
-    *  The basic calculation used is to recognize that the arbitrary
-    *  axis vector (x, y, z), since it is of unit length, actually
-    *  represents the sines and cosines of the angles to rotate the
-    *  X-axis to the same orientation, with theta being the angle about
-    *  Z and phi the angle about Y (in the order described above)
-    *  as follows:
-    *
-    *  cos ( theta ) = x / sqrt ( 1 - z^2 )
-    *  sin ( theta ) = y / sqrt ( 1 - z^2 )
-    *
-    *  cos ( phi ) = sqrt ( 1 - z^2 )
-    *  sin ( phi ) = z
-    *
-    *  Note that cos ( phi ) can further be inserted to the above
-    *  formulas:
-    *
-    *  cos ( theta ) = x / cos ( phi )
-    *  sin ( theta ) = y / sin ( phi )
-    *
-    *  ...etc.  Because of those relations and the standard trigonometric
-    *  relations, it is pssible to reduce the transforms down to what
-    *  is used below.  It may be that any primary axis chosen will give the
-    *  same results (modulo a sign convention) using thie method.
-    *
-    *  Particularly nice is to notice that all divisions that might
-    *  have caused trouble when parallel to certain planes or
-    *  axis go away with care paid to reducing the expressions.
-    *  After checking, it does perform correctly under all cases, since
-    *  in all the cases of division where the denominator would have
-    *  been zero, the numerator would have been zero as well, giving
-    *  the expected result.
-    */
-
-   xx = x * x;
-   yy = y * y;
-   zz = z * z;
-   xy = x * y;
-   yz = y * z;
-   zx = z * x;
-   xs = x * s;
-   ys = y * s;
-   zs = z * s;
-   one_c = 1.0F - c;
-
-   M(0,0) = (one_c * xx) + c;
-   M(0,1) = (one_c * xy) - zs;
-   M(0,2) = (one_c * zx) + ys;
-   M(0,3) = 0.0F;
-
-   M(1,0) = (one_c * xy) + zs;
-   M(1,1) = (one_c * yy) + c;
-   M(1,2) = (one_c * yz) - xs;
-   M(1,3) = 0.0F;
-
-   M(2,0) = (one_c * zx) - ys;
-   M(2,1) = (one_c * yz) + xs;
-   M(2,2) = (one_c * zz) + c;
-   M(2,3) = 0.0F;
-
-   M(3,0) = 0.0F;
-   M(3,1) = 0.0F;
-   M(3,2) = 0.0F;
-   M(3,3) = 1.0F;
-
-#undef M
 }
 
-#define ZERO(x) (1<<x)
-#define ONE(x)  (1<<(x+16))
-
-#define MASK_NO_TRX      (ZERO(12) | ZERO(13) | ZERO(14))
-#define MASK_NO_2D_SCALE ( ONE(0)  | ONE(5))
-
-#define MASK_IDENTITY    ( ONE(0)  | ZERO(4)  | ZERO(8)  | ZERO(12) |\
-                         ZERO(1)  |  ONE(5)  | ZERO(9)  | ZERO(13) |\
-                         ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\
-                         ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
-
-#define MASK_2D_NO_ROT   (           ZERO(4)  | ZERO(8)  |           \
-                         ZERO(1)  |            ZERO(9)  |           \
-                         ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\
-                         ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
-
-#define MASK_2D          (                      ZERO(8)  |           \
-                                               ZERO(9)  |           \
-                         ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\
-                         ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
-
-
-#define MASK_3D_NO_ROT   (           ZERO(4)  | ZERO(8)  |           \
-                         ZERO(1)  |            ZERO(9)  |           \
-                         ZERO(2)  | ZERO(6)  |                      \
-                         ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
-
-#define MASK_3D          (                                           \
-                                                                    \
-                                                                    \
-                         ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
-
-
-#define MASK_PERSPECTIVE (           ZERO(4)  |            ZERO(12) |\
-                         ZERO(1)  |                       ZERO(13) |\
-                         ZERO(2)  | ZERO(6)  |                      \
-                         ZERO(3)  | ZERO(7)  |            ZERO(15) )
 
-#define SQ(x) ((x)*(x))
-  
-/* Determine type and flags from scratch.  This is expensive enough to
- * only want to do it once.
+/**
+ * Multiply the current matrix with a general scaling matrix.
+ *
+ * \param x x axis scale factor.
+ * \param y y axis scale factor.
+ * \param z z axis scale factor.
+ *
+ * \sa glScalef().
+ *
+ * Flushes the vertices and calls _math_matrix_scale() with the top-most
+ * matrix in the current stack and the given parameters. Marks
+ * __GLcontextRec::NewState with the dirty stack flag.
  */
-static void analyze_from_scratch( GLmatrix *mat )
+void GLAPIENTRY
+_mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
 {
-   const GLfloat *m = mat->m;
-   GLuint mask = 0;
-   GLuint i;
-
-   for (i = 0 ; i < 16 ; i++) 
-   {
-      if (m[i] == 0.0) mask |= (1<<i);
-   }
-   
-   if (m[0] == 1.0F) mask |= (1<<16);
-   if (m[5] == 1.0F) mask |= (1<<21);
-   if (m[10] == 1.0F) mask |= (1<<26);
-   if (m[15] == 1.0F) mask |= (1<<31);
-
-   mat->flags &= ~MAT_FLAGS_GEOMETRY;
-
-   /* Check for translation - no-one really cares 
-    */
-   if ((mask & MASK_NO_TRX) != MASK_NO_TRX) 
-      mat->flags |= MAT_FLAG_TRANSLATION;      
-
-   /* Do the real work
-    */
-   if (mask == MASK_IDENTITY) {
-      mat->type = MATRIX_IDENTITY;
-   }
-   else if ((mask & MASK_2D_NO_ROT) == MASK_2D_NO_ROT)  
-   {
-      mat->type = MATRIX_2D_NO_ROT;
-      
-      if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
-        mat->flags = MAT_FLAG_GENERAL_SCALE;
-   }
-   else if ((mask & MASK_2D) == MASK_2D)  
-   {
-      GLfloat mm = DOT2(m, m);
-      GLfloat m4m4 = DOT2(m+4,m+4);
-      GLfloat mm4 = DOT2(m,m+4);
-
-      mat->type = MATRIX_2D;
-
-      /* Check for scale */
-      if (SQ(mm-1) > SQ(1e-6) ||
-         SQ(m4m4-1) > SQ(1e-6)) 
-        mat->flags |= MAT_FLAG_GENERAL_SCALE;
-
-      /* Check for rotation */
-      if (SQ(mm4) > SQ(1e-6))
-        mat->flags |= MAT_FLAG_GENERAL_3D;
-      else
-        mat->flags |= MAT_FLAG_ROTATION;
-
-   }
-   else if ((mask & MASK_3D_NO_ROT) == MASK_3D_NO_ROT)
-   {
-      mat->type = MATRIX_3D_NO_ROT;
-
-      /* Check for scale */
-      if (SQ(m[0]-m[5]) < SQ(1e-6) && 
-         SQ(m[0]-m[10]) < SQ(1e-6)) {
-        if (SQ(m[0]-1.0) > SQ(1e-6))
-           mat->flags |= MAT_FLAG_UNIFORM_SCALE;
-      } else
-        mat->flags |= MAT_FLAG_GENERAL_SCALE;
-   }
-   else if ((mask & MASK_3D) == MASK_3D)
-   {
-      GLfloat c1 = DOT3(m,m);
-      GLfloat c2 = DOT3(m+4,m+4);
-      GLfloat c3 = DOT3(m+8,m+8);
-      GLfloat d1 = DOT3(m, m+4);
-      GLfloat cp[3];
-
-      mat->type = MATRIX_3D;
-
-      /* Check for scale */
-      if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) {
-        if (SQ(c1-1.0) > SQ(1e-6))
-           mat->flags |= MAT_FLAG_UNIFORM_SCALE;
-        /* else no scale at all */
-      } else 
-        mat->flags |= MAT_FLAG_GENERAL_SCALE;
-
-      /* Check for rotation */
-      if (SQ(d1) < SQ(1e-6)) {
-        CROSS3( cp, m, m+4 );
-        SUB_3V( cp, cp, (m+8) );
-        if (LEN_SQUARED_3FV(cp) < SQ(1e-6)) 
-           mat->flags |= MAT_FLAG_ROTATION;
-        else
-           mat->flags |= MAT_FLAG_GENERAL_3D;
-      }
-      else
-        mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
-   }
-   else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) 
-   {
-      mat->type = MATRIX_PERSPECTIVE;
-      mat->flags |= MAT_FLAG_GENERAL;
-   }
-   else {
-      mat->type = MATRIX_GENERAL;
-      mat->flags |= MAT_FLAG_GENERAL;
-   }
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
+   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
 }
 
 
-/* Analyse a matrix given that its flags are accurate - this is the
- * more common operation, hopefully. 
+/**
+ * Multiply the current matrix with a translation matrix.
+ *
+ * \param x translation vector x coordinate.
+ * \param y translation vector y coordinate.
+ * \param z translation vector z coordinate.
+ *
+ * \sa glTranslatef().
+ *
+ * Flushes the vertices and calls _math_matrix_translate() with the top-most
+ * matrix in the current stack and the given parameters. Marks
+ * __GLcontextRec::NewState with the dirty stack flag.
  */
-static void analyze_from_flags( GLmatrix *mat )
+void GLAPIENTRY
+_mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
 {
-   const GLfloat *m = mat->m;
-
-   if (TEST_MAT_FLAGS(mat, 0)) {
-      mat->type = MATRIX_IDENTITY;
-   }
-   else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
-                                MAT_FLAG_UNIFORM_SCALE |
-                                MAT_FLAG_GENERAL_SCALE)))
-   {
-      if ( m[10]==1.0F && m[14]==0.0F ) {
-        mat->type = MATRIX_2D_NO_ROT;
-      }
-      else {
-        mat->type = MATRIX_3D_NO_ROT;
-      }
-   }
-   else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
-      if (                                 m[ 8]==0.0F               
-            &&                             m[ 9]==0.0F
-            && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F)
-      {
-        mat->type = MATRIX_2D;
-      }
-      else 
-      {
-        mat->type = MATRIX_3D;
-      }
-   }
-   else if (                 m[4]==0.0F                 && m[12]==0.0F
-            && m[1]==0.0F                               && m[13]==0.0F
-            && m[2]==0.0F && m[6]==0.0F
-            && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) 
-   {
-      mat->type = MATRIX_PERSPECTIVE;
-   }
-   else {
-      mat->type = MATRIX_GENERAL;
-   }
-
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
+   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
 }
 
-
-void gl_matrix_analyze( GLmatrix *mat ) 
+#if _HAVE_FULL_GL
+void GLAPIENTRY
+_mesa_LoadMatrixd( const GLdouble *m )
 {
-   if (mat->flags & MAT_DIRTY_TYPE) {
-      if (mat->flags & MAT_DIRTY_FLAGS) 
-        analyze_from_scratch( mat );
-      else
-        analyze_from_flags( mat );
-   }
-
-   if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
-      gl_matrix_invert( mat );
-   }
+   GLint i;
+   GLfloat f[16];
+   if (!m) return;
+   for (i = 0; i < 16; i++)
+      f[i] = (GLfloat) m[i];
+   _mesa_LoadMatrixf(f);
+}
 
-   mat->flags &= ~(MAT_DIRTY_FLAGS|
-                  MAT_DIRTY_TYPE|
-                  MAT_DIRTY_INVERSE);
+void GLAPIENTRY
+_mesa_MultMatrixd( const GLdouble *m )
+{
+   GLint i;
+   GLfloat f[16];
+   if (!m) return;
+   for (i = 0; i < 16; i++)
+      f[i] = (GLfloat) m[i];
+   _mesa_MultMatrixf( f );
 }
 
 
-#define GET_ACTIVE_MATRIX(ctx, mat, flags, where)                      \
-do {                                                                   \
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where);                      \
-   if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where);        \
-   switch (ctx->Transform.MatrixMode) {                                        \
-      case GL_MODELVIEW:                                               \
-        mat = &ctx->ModelView;                                         \
-        flags |= NEW_MODELVIEW;                                        \
-        break;                                                         \
-      case GL_PROJECTION:                                              \
-        mat = &ctx->ProjectionMatrix;                                  \
-        flags |= NEW_PROJECTION;                                       \
-        break;                                                         \
-      case GL_TEXTURE:                                                 \
-        mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit];  \
-        flags |= NEW_TEXTURE_MATRIX;                                   \
-        break;                                                         \
-      default:                                                         \
-         gl_problem(ctx, where);                                       \
-   }                                                                   \
-} while (0)
-
-
-void gl_Frustum( GLcontext *ctx,
-                 GLdouble left, GLdouble right,
-                GLdouble bottom, GLdouble top,
-                GLdouble nearval, GLdouble farval )
+void GLAPIENTRY
+_mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
 {
-   GLfloat x, y, a, b, c, d;
-   GLfloat m[16];
-   GLmatrix *mat = 0;
+   _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
+}
 
-   GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glFrustrum" );
 
-   if (nearval<=0.0 || farval<=0.0) {
-      gl_error( ctx,  GL_INVALID_VALUE, "glFrustum(near or far)" );
-   }
+void GLAPIENTRY
+_mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
+{
+   _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
+}
 
-   x = (2.0*nearval) / (right-left);
-   y = (2.0*nearval) / (top-bottom);
-   a = (right+left) / (right-left);
-   b = (top+bottom) / (top-bottom);
-   c = -(farval+nearval) / ( farval-nearval);
-   d = -(2.0*farval*nearval) / (farval-nearval);  /* error? */
 
-#define M(row,col)  m[col*4+row]
-   M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = a;      M(0,3) = 0.0F;
-   M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = b;      M(1,3) = 0.0F;
-   M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = c;      M(2,3) = d;
-   M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = -1.0F;  M(3,3) = 0.0F;
-#undef M
+void GLAPIENTRY
+_mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
+{
+   _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
+}
+#endif
 
 
-   gl_mat_mul_floats( mat, m, MAT_FLAG_PERSPECTIVE );
+#if _HAVE_FULL_GL
+void GLAPIENTRY
+_mesa_LoadTransposeMatrixfARB( const GLfloat *m )
+{
+   GLfloat tm[16];
+   if (!m) return;
+   _math_transposef(tm, m);
+   _mesa_LoadMatrixf(tm);
+}
 
 
-   if (ctx->Transform.MatrixMode == GL_PROJECTION)
-   {
-      /* Need to keep a stack of near/far values in case the user push/pops
-       * the projection matrix stack so that we can call Driver.NearFar()
-       * after a pop.
-       */
-      ctx->NearFarStack[ctx->ProjectionStackDepth][0] = nearval;
-      ctx->NearFarStack[ctx->ProjectionStackDepth][1] = farval;
-      
-      if (ctx->Driver.NearFar) {
-        (*ctx->Driver.NearFar)( ctx, nearval, farval );
-      }
-   }
+void GLAPIENTRY
+_mesa_LoadTransposeMatrixdARB( const GLdouble *m )
+{
+   GLfloat tm[16];
+   if (!m) return;
+   _math_transposefd(tm, m);
+   _mesa_LoadMatrixf(tm);
 }
 
 
-void gl_Ortho( GLcontext *ctx,
-               GLdouble left, GLdouble right,
-               GLdouble bottom, GLdouble top,
-               GLdouble nearval, GLdouble farval )
+void GLAPIENTRY
+_mesa_MultTransposeMatrixfARB( const GLfloat *m )
 {
-   GLfloat x, y, z;
-   GLfloat tx, ty, tz;
-   GLfloat m[16];
-   GLmatrix *mat = 0;
-
-   GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glOrtho" );
-
-   x = 2.0 / (right-left);
-   y = 2.0 / (top-bottom);
-   z = -2.0 / (farval-nearval);
-   tx = -(right+left) / (right-left);
-   ty = -(top+bottom) / (top-bottom);
-   tz = -(farval+nearval) / (farval-nearval);
-
-#define M(row,col)  m[col*4+row]
-   M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = 0.0F;  M(0,3) = tx;
-   M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = 0.0F;  M(1,3) = ty;
-   M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = z;     M(2,3) = tz;
-   M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = 0.0F;  M(3,3) = 1.0F;
-#undef M
-
-   gl_mat_mul_floats( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
-
-   if (ctx->Driver.NearFar) {
-      (*ctx->Driver.NearFar)( ctx, nearval, farval );
-   }
+   GLfloat tm[16];
+   if (!m) return;
+   _math_transposef(tm, m);
+   _mesa_MultMatrixf(tm);
 }
 
 
-void gl_MatrixMode( GLcontext *ctx, GLenum mode )
+void GLAPIENTRY
+_mesa_MultTransposeMatrixdARB( const GLdouble *m )
 {
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glMatrixMode");
-   switch (mode) {
-      case GL_MODELVIEW:
-      case GL_PROJECTION:
-      case GL_TEXTURE:
-         ctx->Transform.MatrixMode = mode;
-         break;
-      default:
-         gl_error( ctx,  GL_INVALID_ENUM, "glMatrixMode" );
-   }
+   GLfloat tm[16];
+   if (!m) return;
+   _math_transposefd(tm, m);
+   _mesa_MultMatrixf(tm);
 }
+#endif
 
+/**
+ * Set the viewport.
+ * 
+ * \param x, y coordinates of the lower-left corner of the viewport rectangle.
+ * \param width width of the viewport rectangle.
+ * \param height height of the viewport rectangle.
+ *
+ * \sa Called via glViewport() or display list execution.
+ *
+ * Flushes the vertices and calls _mesa_set_viewport() with the given
+ * parameters.
+ */
+void GLAPIENTRY
+_mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
+{
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
+   _mesa_set_viewport(ctx, x, y, width, height);
+}
 
 
-void gl_PushMatrix( GLcontext *ctx )
+/**
+ * Set new viewport parameters and update derived state (the _WindowMap
+ * matrix).  Usually called from _mesa_Viewport().
+ * 
+ * \param ctx GL context.
+ * \param x, y coordinates of the lower left corner of the viewport rectangle.
+ * \param width width of the viewport rectangle.
+ * \param height height of the viewport rectangle.
+ */
+void
+_mesa_set_viewport( GLcontext *ctx, GLint x, GLint y,
+                    GLsizei width, GLsizei height )
 {
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushMatrix");
-
-   if (MESA_VERBOSE&VERBOSE_API)
-      fprintf(stderr, "glPushMatrix %s\n", 
-             gl_lookup_enum_by_nr(ctx->Transform.MatrixMode));
+   if (MESA_VERBOSE & VERBOSE_API)
+      _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
 
-   switch (ctx->Transform.MatrixMode) {
-      case GL_MODELVIEW:
-         if (ctx->ModelViewStackDepth>=MAX_MODELVIEW_STACK_DEPTH-1) {
-            gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix");
-            return;
-         }
-         gl_matrix_copy( &ctx->ModelViewStack[ctx->ModelViewStackDepth++],
-                        &ctx->ModelView );
-         break;
-      case GL_PROJECTION:
-         if (ctx->ProjectionStackDepth>=MAX_PROJECTION_STACK_DEPTH) {
-            gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix");
-            return;
-         }
-         gl_matrix_copy( &ctx->ProjectionStack[ctx->ProjectionStackDepth++],
-                        &ctx->ProjectionMatrix );
-
-         /* Save near and far projection values */
-         ctx->NearFarStack[ctx->ProjectionStackDepth][0]
-            = ctx->NearFarStack[ctx->ProjectionStackDepth-1][0];
-         ctx->NearFarStack[ctx->ProjectionStackDepth][1]
-            = ctx->NearFarStack[ctx->ProjectionStackDepth-1][1];
-         break;
-      case GL_TEXTURE:
-         {
-            GLuint t = ctx->Texture.CurrentTransformUnit;
-            if (ctx->TextureStackDepth[t] >= MAX_TEXTURE_STACK_DEPTH) {
-               gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix");
-               return;
-            }
-           gl_matrix_copy( &ctx->TextureStack[t][ctx->TextureStackDepth[t]++],
-                           &ctx->TextureMatrix[t] );
-         }
-         break;
-      default:
-         gl_problem(ctx, "Bad matrix mode in gl_PushMatrix");
+   if (width < 0 || height < 0) {
+      _mesa_error( ctx,  GL_INVALID_VALUE,
+                   "glViewport(%d, %d, %d, %d)", x, y, width, height );
+      return;
    }
-}
-
 
+   /* clamp width and height to the implementation dependent range */
+   width  = CLAMP(width,  1, (GLsizei) ctx->Const.MaxViewportWidth);
+   height = CLAMP(height, 1, (GLsizei) ctx->Const.MaxViewportHeight);
 
-void gl_PopMatrix( GLcontext *ctx )
-{
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopMatrix");
-
-   if (MESA_VERBOSE&VERBOSE_API)
-      fprintf(stderr, "glPopMatrix %s\n", 
-             gl_lookup_enum_by_nr(ctx->Transform.MatrixMode));
+   ctx->Viewport.X = x;
+   ctx->Viewport.Width = width;
+   ctx->Viewport.Y = y;
+   ctx->Viewport.Height = height;
+   ctx->NewState |= _NEW_VIEWPORT;
 
-   switch (ctx->Transform.MatrixMode) {
-      case GL_MODELVIEW:
-         if (ctx->ModelViewStackDepth==0) {
-            gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix");
-            return;
-         }
-         gl_matrix_copy( &ctx->ModelView,
-                        &ctx->ModelViewStack[--ctx->ModelViewStackDepth] );
-        ctx->NewState |= NEW_MODELVIEW;
-         break;
-      case GL_PROJECTION:
-         if (ctx->ProjectionStackDepth==0) {
-            gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix");
-            return;
-         }
+#if 1
+   /* XXX remove this someday.  Currently the DRI drivers rely on
+    * the WindowMap matrix being up to date in the driver's Viewport
+    * and DepthRange functions.
+    */
+   _math_matrix_viewport(&ctx->Viewport._WindowMap,
+                         ctx->Viewport.X, ctx->Viewport.Y,
+                         ctx->Viewport.Width, ctx->Viewport.Height,
+                         ctx->Viewport.Near, ctx->Viewport.Far,
+                         ctx->DrawBuffer->_DepthMaxF);
+#endif
 
-         gl_matrix_copy( &ctx->ProjectionMatrix,
-                        &ctx->ProjectionStack[--ctx->ProjectionStackDepth] );
-        ctx->NewState |= NEW_PROJECTION;
-
-         /* Device driver near/far values */
-         {
-            GLfloat nearVal = ctx->NearFarStack[ctx->ProjectionStackDepth][0];
-            GLfloat farVal  = ctx->NearFarStack[ctx->ProjectionStackDepth][1];
-            if (ctx->Driver.NearFar) {
-               (*ctx->Driver.NearFar)( ctx, nearVal, farVal );
-            }
-         }
-         break;
-      case GL_TEXTURE:
-         {
-            GLuint t = ctx->Texture.CurrentTransformUnit;
-            if (ctx->TextureStackDepth[t]==0) {
-               gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix");
-               return;
-            }
-           gl_matrix_copy(&ctx->TextureMatrix[t],
-                          &ctx->TextureStack[t][--ctx->TextureStackDepth[t]]);
-         }
-         break;
-      default:
-         gl_problem(ctx, "Bad matrix mode in gl_PopMatrix");
+   if (ctx->Driver.Viewport) {
+      /* Many drivers will use this call to check for window size changes
+       * and reallocate the z/stencil/accum/etc buffers if needed.
+       */
+      (*ctx->Driver.Viewport)( ctx, x, y, width, height );
    }
 }
 
 
-
-void gl_LoadIdentity( GLcontext *ctx )
+#if _HAVE_FULL_GL
+/**
+ * Called by glDepthRange
+ *
+ * \param nearval  specifies the Z buffer value which should correspond to
+ *                 the near clip plane
+ * \param farval  specifies the Z buffer value which should correspond to
+ *                the far clip plane
+ */
+void GLAPIENTRY
+_mesa_DepthRange( GLclampd nearval, GLclampd farval )
 {
-   GLmatrix *mat = 0;
-   GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadIdentity");
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
+   if (MESA_VERBOSE&VERBOSE_API)
+      _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
 
-   if (mat->inv)
-      MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
+   ctx->Viewport.Near = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
+   ctx->Viewport.Far = (GLfloat) CLAMP( farval, 0.0, 1.0 );
+   ctx->NewState |= _NEW_VIEWPORT;
 
-   mat->type = MATRIX_IDENTITY;
-   
-   /* Have to set this to dirty to make sure we recalculate the
-    * combined matrix later.  The update_matrix in this case is a
-    * shortcircuit anyway...
+#if 1
+   /* XXX remove this someday.  Currently the DRI drivers rely on
+    * the WindowMap matrix being up to date in the driver's Viewport
+    * and DepthRange functions.
     */
-   mat->flags = MAT_DIRTY_DEPENDENTS;  
-}
+   _math_matrix_viewport(&ctx->Viewport._WindowMap,
+                         ctx->Viewport.X, ctx->Viewport.Y,
+                         ctx->Viewport.Width, ctx->Viewport.Height,
+                         ctx->Viewport.Near, ctx->Viewport.Far,
+                         ctx->DrawBuffer->_DepthMaxF);
+#endif
 
+   if (ctx->Driver.DepthRange) {
+      (*ctx->Driver.DepthRange)( ctx, nearval, farval );
+   }
+}
+#endif
 
-void gl_LoadMatrixf( GLcontext *ctx, const GLfloat *m )
-{
-   GLmatrix *mat = 0;
-   GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadMatrix");
 
-   MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
-   mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
 
-   if (ctx->Transform.MatrixMode == GL_PROJECTION) {
+/**********************************************************************/
+/** \name State management */
+/*@{*/
 
-#define M(row,col)  m[col*4+row]
-      GLfloat c = M(2,2);
-      GLfloat d = M(2,3);
-#undef M
-      GLfloat n = (c ==  1.0 ? 0.0 : d / (c - 1.0));
-      GLfloat f = (c == -1.0 ? 1.0 : d / (c + 1.0));
 
-      /* Need to keep a stack of near/far values in case the user
-       * push/pops the projection matrix stack so that we can call
-       * Driver.NearFar() after a pop.
-       */
-      ctx->NearFarStack[ctx->ProjectionStackDepth][0] = n;
-      ctx->NearFarStack[ctx->ProjectionStackDepth][1] = f;
+/**
+ * Update the projection matrix stack.
+ *
+ * \param ctx GL context.
+ *
+ * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
+ * stack, and recomputes user clip positions if necessary.
+ * 
+ * \note This routine references __GLcontextRec::Tranform attribute values to
+ * compute userclip positions in clip space, but is only called on
+ * _NEW_PROJECTION.  The _mesa_ClipPlane() function keeps these values up to
+ * date across changes to the __GLcontextRec::Transform attributes.
+ */
+static void
+update_projection( GLcontext *ctx )
+{
+   _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
 
-      if (ctx->Driver.NearFar) {
-        (*ctx->Driver.NearFar)( ctx, n, f );
+#if FEATURE_userclip
+   /* Recompute clip plane positions in clipspace.  This is also done
+    * in _mesa_ClipPlane().
+    */
+   if (ctx->Transform.ClipPlanesEnabled) {
+      GLuint p;
+      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
+        if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
+           _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
+                                ctx->Transform.EyeUserPlane[p],
+                                ctx->ProjectionMatrixStack.Top->inv );
+        }
       }
    }
+#endif
 }
 
 
-
-/*
- * Multiply the active matrix by an arbitary matrix.
+/**
+ * Calculate the combined modelview-projection matrix.
+ *
+ * \param ctx GL context.
+ *
+ * Multiplies the top matrices of the projection and model view stacks into
+ * __GLcontextRec::_ModelProjectMatrix via _math_matrix_mul_matrix() and
+ * analyzes the resulting matrix via _math_matrix_analyse().
  */
-void gl_MultMatrixf( GLcontext *ctx, const GLfloat *m )
+static void
+calculate_model_project_matrix( GLcontext *ctx )
 {
-   GLmatrix *mat = 0;
-   GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glMultMatrix" );
-   matmul4( mat->m, mat->m, m );
-   mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
-}
-
+   _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
+                            ctx->ProjectionMatrixStack.Top,
+                            ctx->ModelviewMatrixStack.Top );
 
-/*
- * Multiply the active matrix by an arbitary matrix.  
- */
-void gl_MultMatrixd( GLcontext *ctx, const GLdouble *m )
-{
-   GLmatrix *mat = 0;
-   GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glMultMatrix" );
-   matmul4fd( mat->m, mat->m, m );
-   mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
+   _math_matrix_analyse( &ctx->_ModelProjectMatrix );
 }
 
 
-
-
-/*
- * Multiply a matrix by an array of floats with known properties.
+/**
+ * Updates the combined modelview-projection matrix.
+ *
+ * \param ctx GL context.
+ * \param new_state new state bit mask.
+ *
+ * If there is a new model view matrix then analyzes it. If there is a new
+ * projection matrix, updates it. Finally calls
+ * calculate_model_project_matrix() to recalculate the modelview-projection
+ * matrix.
  */
-void gl_mat_mul_floats( GLmatrix *mat, const GLfloat *m, GLuint flags )
+void _mesa_update_modelview_project( GLcontext *ctx, GLuint new_state )
 {
-   mat->flags |= (flags |
-                 MAT_DIRTY_TYPE | 
-                 MAT_DIRTY_INVERSE | 
-                 MAT_DIRTY_DEPENDENTS);
+   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 (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
-      matmul34( mat->m, mat->m, m );
-   else 
-      matmul4( mat->m, mat->m, m );      
 
-}
+   if (new_state & _NEW_PROJECTION)
+      update_projection( ctx );
 
-/*
- * Multiply a matrix by an array of floats with known properties.
- */
-void gl_mat_mul_mat( GLmatrix *mat, const GLmatrix *m )
-{
-   mat->flags |= (m->flags |
-                 MAT_DIRTY_TYPE | 
-                 MAT_DIRTY_INVERSE | 
-                 MAT_DIRTY_DEPENDENTS);
-
-   if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
-      matmul34( mat->m, mat->m, m->m );
-   else 
-      matmul4( mat->m, mat->m, m->m );      
+   /* Keep ModelviewProject uptodate always to allow tnl
+    * implementations that go model->clip even when eye is required.
+    */
+   calculate_model_project_matrix(ctx);
 }
 
+/*@}*/
 
 
-/*
- * Execute a glRotate call
- */
-void gl_Rotatef( GLcontext *ctx,
-                 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
-{
-   GLfloat m[16];
-   if (angle != 0.0F) {
-      GLmatrix *mat = 0;
-      GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glRotate" );
+/**********************************************************************/
+/** Matrix stack initialization */
+/*@{*/
 
-      gl_rotation_matrix( angle, x, y, z, m );
-      gl_mat_mul_floats( mat, m, MAT_FLAG_ROTATION );
-   }
-}
 
-/*
- * Execute a glScale call
+/**
+ * Initialize a matrix stack.
+ *
+ * \param stack matrix stack.
+ * \param maxDepth maximum stack depth.
+ * \param dirtyFlag dirty flag.
+ * 
+ * Allocates an array of \p maxDepth elements for the matrix stack and calls
+ * _math_matrix_ctr() and _math_matrix_alloc_inv() for each element to
+ * initialize it.
  */
-void gl_Scalef( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z )
+static void
+init_matrix_stack( struct gl_matrix_stack *stack,
+                   GLuint maxDepth, GLuint dirtyFlag )
 {
-   GLmatrix *mat = 0;
-   GLfloat *m;
-   GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glScale");
-
-   m = mat->m;
-   m[0] *= x;   m[4] *= y;   m[8]  *= z;
-   m[1] *= x;   m[5] *= y;   m[9]  *= z;
-   m[2] *= x;   m[6] *= y;   m[10] *= z;
-   m[3] *= x;   m[7] *= y;   m[11] *= z;
-
-   if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
-      mat->flags |= MAT_FLAG_UNIFORM_SCALE;
-   else
-      mat->flags |= MAT_FLAG_GENERAL_SCALE;
-
-   mat->flags |= (MAT_DIRTY_TYPE | 
-                 MAT_DIRTY_INVERSE | 
-                 MAT_DIRTY_DEPENDENTS);
-}
+   GLuint i;
 
-/*
- * Execute a glTranslate call
- */
-void gl_Translatef( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z )
-{
-   GLmatrix *mat = 0;
-   GLfloat *m;
-   GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glTranslate");
-   m = mat->m;
-   m[12] = m[0] * x + m[4] * y + m[8]  * z + m[12];
-   m[13] = m[1] * x + m[5] * y + m[9]  * z + m[13];
-   m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
-   m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
-
-   mat->flags |= (MAT_FLAG_TRANSLATION | 
-                 MAT_DIRTY_TYPE | 
-                 MAT_DIRTY_INVERSE | 
-                 MAT_DIRTY_DEPENDENTS);
+   stack->Depth = 0;
+   stack->MaxDepth = maxDepth;
+   stack->DirtyFlag = dirtyFlag;
+   /* The stack */
+   stack->Stack = (GLmatrix *) CALLOC(maxDepth * sizeof(GLmatrix));
+   for (i = 0; i < maxDepth; i++) {
+      _math_matrix_ctr(&stack->Stack[i]);
+      _math_matrix_alloc_inv(&stack->Stack[i]);
+   }
+   stack->Top = stack->Stack;
 }
 
-
-/*
- * Define a new viewport and reallocate auxillary buffers if the size of
- * the window (color buffer) has changed.
+/**
+ * Free matrix stack.
+ * 
+ * \param stack matrix stack.
+ * 
+ * Calls _math_matrix_dtr() for each element of the matrix stack and
+ * frees the array.
  */
-void gl_Viewport( GLcontext *ctx,
-                  GLint x, GLint y, GLsizei width, GLsizei height )
+static void
+free_matrix_stack( struct gl_matrix_stack *stack )
 {
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glViewport");
-
-   if (width<0 || height<0) {
-      gl_error( ctx,  GL_INVALID_VALUE, "glViewport" );
-      return;
+   GLuint i;
+   for (i = 0; i < stack->MaxDepth; i++) {
+      _math_matrix_dtr(&stack->Stack[i]);
    }
+   FREE(stack->Stack);
+   stack->Stack = stack->Top = NULL;
+}
 
-   if (MESA_VERBOSE & VERBOSE_API)
-      fprintf(stderr, "glViewport %d %d %d %d\n", x, y, width, height);
-   
-   /* clamp width, and height to implementation dependent range */
-   width  = CLAMP( width,  1, MAX_WIDTH );
-   height = CLAMP( height, 1, MAX_HEIGHT );
-
-   /* Save viewport */
-   ctx->Viewport.X = x;
-   ctx->Viewport.Width = width;
-   ctx->Viewport.Y = y;
-   ctx->Viewport.Height = height;
-
-   /* compute scale and bias values */
-   ctx->Viewport.WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
-   ctx->Viewport.WindowMap.m[MAT_TX] = ctx->Viewport.WindowMap.m[MAT_SX] + x;
-   ctx->Viewport.WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
-   ctx->Viewport.WindowMap.m[MAT_TY] = ctx->Viewport.WindowMap.m[MAT_SY] + y;
-
-   ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
-   ctx->NewState |= NEW_VIEWPORT;
+/*@}*/
 
-   /* Check if window/buffer has been resized and if so, reallocate the
-    * ancillary buffers.
-    */
-   gl_ResizeBuffersMESA(ctx);
 
+/**********************************************************************/
+/** \name Initialization */
+/*@{*/
 
-   ctx->RasterMask &= WINCLIP_BIT;
 
-   if (   ctx->Viewport.X<0
-       || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
-       || ctx->Viewport.Y<0
-       || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
-      ctx->RasterMask |= WINCLIP_BIT;
-   }
+/**
+ * Initialize the context matrix data.
+ *
+ * \param ctx GL context.
+ *
+ * Initializes each of the matrix stacks and the combined modelview-projection
+ * matrix.
+ */
+void _mesa_init_matrix( GLcontext * ctx )
+{
+   GLint i;
 
+   /* Initialize matrix stacks */
+   init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
+                     _NEW_MODELVIEW);
+   init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
+                     _NEW_PROJECTION);
+   init_matrix_stack(&ctx->ColorMatrixStack, MAX_COLOR_STACK_DEPTH,
+                     _NEW_COLOR_MATRIX);
+   for (i = 0; i < MAX_TEXTURE_UNITS; i++)
+      init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
+                        _NEW_TEXTURE_MATRIX);
+   for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
+      init_matrix_stack(&ctx->ProgramMatrixStack[i], 
+                       MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
+   ctx->CurrentStack = &ctx->ModelviewMatrixStack;
 
-   if (ctx->Driver.Viewport) {
-      (*ctx->Driver.Viewport)( ctx, x, y, width, height );
-   }
+   /* Init combined Modelview*Projection matrix */
+   _math_matrix_ctr( &ctx->_ModelProjectMatrix );
 }
 
 
-
-void gl_DepthRange( GLcontext *ctx, GLclampd nearval, GLclampd farval )
+/**
+ * Free the context matrix data.
+ * 
+ * \param ctx GL context.
+ *
+ * Frees each of the matrix stacks and the combined modelview-projection
+ * matrix.
+ */
+void _mesa_free_matrix_data( GLcontext *ctx )
 {
-   /*
-    * nearval - specifies mapping of the near clipping plane to window
-    *   coordinates, default is 0
-    * farval - specifies mapping of the far clipping plane to window
-    *   coordinates, default is 1
-    *
-    * After clipping and div by w, z coords are in -1.0 to 1.0,
-    * corresponding to near and far clipping planes.  glDepthRange
-    * specifies a linear mapping of the normalized z coords in
-    * this range to window z coords.
-    */
-   GLfloat n, f;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthRange");
-
-   if (MESA_VERBOSE&VERBOSE_API)
-      fprintf(stderr, "glDepthRange %f %f\n", nearval, farval); 
-
-   n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
-   f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
-
-   ctx->Viewport.Near = n;
-   ctx->Viewport.Far = f;
-   ctx->Viewport.WindowMap.m[MAT_SZ] = DEPTH_SCALE * ((f - n) / 2.0);
-   ctx->Viewport.WindowMap.m[MAT_TZ] = DEPTH_SCALE * ((f - n) / 2.0 + n);
+   GLint i;
 
-   ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
+   free_matrix_stack(&ctx->ModelviewMatrixStack);
+   free_matrix_stack(&ctx->ProjectionMatrixStack);
+   free_matrix_stack(&ctx->ColorMatrixStack);
+   for (i = 0; i < MAX_TEXTURE_UNITS; i++)
+      free_matrix_stack(&ctx->TextureMatrixStack[i]);
+   for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
+      free_matrix_stack(&ctx->ProgramMatrixStack[i]);
+   /* combined Modelview*Projection matrix */
+   _math_matrix_dtr( &ctx->_ModelProjectMatrix );
 
-   if (ctx->Driver.DepthRange) {
-      (*ctx->Driver.DepthRange)( ctx, nearval, farval );
-   }
 }
 
 
-void gl_calculate_model_project_matrix( GLcontext *ctx )
+/** 
+ * Initialize the context transform attribute group.
+ *
+ * \param ctx GL context.
+ *
+ * \todo Move this to a new file with other 'transform' routines.
+ */
+void _mesa_init_transform( GLcontext *ctx )
 {
-   gl_matrix_mul( &ctx->ModelProjectMatrix,
-                 &ctx->ProjectionMatrix,
-                 &ctx->ModelView );
+   GLint i;
+
+   /* Transformation group */
+   ctx->Transform.MatrixMode = GL_MODELVIEW;
+   ctx->Transform.Normalize = GL_FALSE;
+   ctx->Transform.RescaleNormals = GL_FALSE;
+   ctx->Transform.RasterPositionUnclipped = GL_FALSE;
+   for (i=0;i<MAX_CLIP_PLANES;i++) {
+      ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
+   }
+   ctx->Transform.ClipPlanesEnabled = 0;
 
-   gl_matrix_analyze( &ctx->ModelProjectMatrix );
+   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 );
 }
 
 
-void gl_matrix_ctr( GLmatrix *m )
+/** 
+ * Initialize the context viewport attribute group.
+ *
+ * \param ctx GL context.
+ * 
+ * \todo Move this to a new file with other 'viewport' routines.
+ */
+void _mesa_init_viewport( GLcontext *ctx )
 {
-   m->inv = 0;
-   MEMCPY( m->m, Identity, sizeof(Identity));
-   m->type = MATRIX_IDENTITY;
-   m->flags = MAT_DIRTY_DEPENDENTS;
-}
+   GLfloat depthMax = 65535.0F; /* sorf of arbitrary */
 
-void gl_matrix_dtr( GLmatrix *m )
-{
-   if (m->inv != 0) {
-      free(m->inv);
-      m->inv = 0;
-   }
-}
+   /* Viewport group */
+   ctx->Viewport.X = 0;
+   ctx->Viewport.Y = 0;
+   ctx->Viewport.Width = 0;
+   ctx->Viewport.Height = 0;
+   ctx->Viewport.Near = 0.0;
+   ctx->Viewport.Far = 1.0;
+   _math_matrix_ctr(&ctx->Viewport._WindowMap);
 
-void gl_matrix_alloc_inv( GLmatrix *m )
-{
-   if (m->inv == 0) {
-      m->inv = (GLfloat *)malloc(16*sizeof(GLfloat));
-      MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
-   }
+   _math_matrix_viewport(&ctx->Viewport._WindowMap, 0, 0, 0, 0,
+                         0.0F, 1.0F, depthMax);
 }
 
-void gl_matrix_copy( GLmatrix *to, const GLmatrix *from )
-{
-   MEMCPY( to->m, from->m, sizeof(Identity));
-   to->flags = from->flags | MAT_DIRTY_DEPENDENTS;
-   to->type = from->type;
-
-   if (to->inv != 0) {
-      if (from->inv == 0) {
-        gl_matrix_invert( to );
-      } else {
-        MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
-      }
-   }
-}
 
-void gl_matrix_mul( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
+/** 
+ * Free the context viewport attribute group data.
+ *
+ * \param ctx GL context.
+ * 
+ * \todo Move this to a new file with other 'viewport' routines.
+ */
+void _mesa_free_viewport_data( GLcontext *ctx )
 {
-   dest->flags = (a->flags |
-                 b->flags |
-                 MAT_DIRTY_TYPE | 
-                 MAT_DIRTY_INVERSE | 
-                 MAT_DIRTY_DEPENDENTS);
-
-   if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
-      matmul34( dest->m, a->m, b->m );
-   else 
-      matmul4( dest->m, a->m, b->m );
+   _math_matrix_dtr(&ctx->Viewport._WindowMap);
 }
+
+/*@}*/