mesa/st: Don't set alpha if ALPHA_TEST is lowered
[mesa.git] / src / mesa / main / state.c
index cce1b464f0cae2ccb56ff1d1f02717b776539188..2f89cecd8abd3d04f682c7f85a13daea89e24ed1 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.3
  *
  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
@@ -33,6 +33,7 @@
 
 #include "glheader.h"
 #include "mtypes.h"
+#include "arrayobj.h"
 #include "context.h"
 #include "debug.h"
 #include "macros.h"
 #include "pixel.h"
 #include "program/program.h"
 #include "program/prog_parameter.h"
+#include "shaderobj.h"
 #include "state.h"
 #include "stencil.h"
 #include "texenvprogram.h"
 #include "texobj.h"
 #include "texstate.h"
+#include "varray.h"
+#include "vbo/vbo.h"
+#include "viewport.h"
+#include "blend.h"
 
 
-static void
-update_separate_specular(struct gl_context *ctx)
-{
-   if (NEED_SECONDARY_COLOR(ctx))
-      ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
-   else
-      ctx->_TriangleCaps &= ~DD_SEPARATE_SPECULAR;
-}
-
-
-/**
- * Compute the index of the last array element that can be safely accessed
- * in a vertex array.  We can really only do this when the array lives in
- * a VBO.
- * The array->_MaxElement field will be updated.
- * Later in glDrawArrays/Elements/etc we can do some bounds checking.
- */
-static void
-compute_max_element(struct gl_client_array *array)
-{
-   assert(array->Enabled);
-   if (array->BufferObj->Name) {
-      GLsizeiptrARB offset = (GLsizeiptrARB) array->Ptr;
-      GLsizeiptrARB obj_size = (GLsizeiptrARB) array->BufferObj->Size;
-
-      if (offset < obj_size) {
-        array->_MaxElement = (obj_size - offset +
-                              array->StrideB -
-                              array->_ElementSize) / array->StrideB;
-      } else {
-        array->_MaxElement = 0;
-      }
-   }
-   else {
-      /* user-space array, no idea how big it is */
-      array->_MaxElement = 2 * 1000 * 1000 * 1000; /* just a big number */
-   }
-}
-
-
-/**
- * Helper for update_arrays().
- * \return  min(current min, array->_MaxElement).
- */
-static GLuint
-update_min(GLuint min, struct gl_client_array *array)
-{
-   compute_max_element(array);
-   return MIN2(min, array->_MaxElement);
-}
-
-
-/**
- * Update ctx->Array._MaxElement (the max legal index into all enabled arrays).
- * Need to do this upon new array state or new buffer object state.
- */
-static void
-update_arrays( struct gl_context *ctx )
-{
-   struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
-   GLuint i, min = ~0;
-
-   /* find min of _MaxElement values for all enabled arrays */
-
-   /* 0 */
-   if (ctx->VertexProgram._Current
-       && arrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled) {
-      min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_POS]);
-   }
-   else if (arrayObj->Vertex.Enabled) {
-      min = update_min(min, &arrayObj->Vertex);
-   }
-
-   /* 1 */
-   if (ctx->VertexProgram._Enabled
-       && arrayObj->VertexAttrib[VERT_ATTRIB_WEIGHT].Enabled) {
-      min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_WEIGHT]);
-   }
-   /* no conventional vertex weight array */
-
-   /* 2 */
-   if (ctx->VertexProgram._Enabled
-       && arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL].Enabled) {
-      min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL]);
-   }
-   else if (arrayObj->Normal.Enabled) {
-      min = update_min(min, &arrayObj->Normal);
-   }
-
-   /* 3 */
-   if (ctx->VertexProgram._Enabled
-       && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0].Enabled) {
-      min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0]);
-   }
-   else if (arrayObj->Color.Enabled) {
-      min = update_min(min, &arrayObj->Color);
-   }
-
-   /* 4 */
-   if (ctx->VertexProgram._Enabled
-       && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR1].Enabled) {
-      min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR1]);
-   }
-   else if (arrayObj->SecondaryColor.Enabled) {
-      min = update_min(min, &arrayObj->SecondaryColor);
-   }
-
-   /* 5 */
-   if (ctx->VertexProgram._Enabled
-       && arrayObj->VertexAttrib[VERT_ATTRIB_FOG].Enabled) {
-      min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_FOG]);
-   }
-   else if (arrayObj->FogCoord.Enabled) {
-      min = update_min(min, &arrayObj->FogCoord);
-   }
-
-   /* 6 */
-   if (ctx->VertexProgram._Enabled
-       && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR_INDEX].Enabled) {
-      min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR_INDEX]);
-   }
-   else if (arrayObj->Index.Enabled) {
-      min = update_min(min, &arrayObj->Index);
-   }
-
-   /* 7 */
-   if (ctx->VertexProgram._Enabled
-       && arrayObj->VertexAttrib[VERT_ATTRIB_EDGEFLAG].Enabled) {
-      min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_EDGEFLAG]);
-   }
-
-   /* 8..15 */
-   for (i = VERT_ATTRIB_TEX0; i <= VERT_ATTRIB_TEX7; i++) {
-      if (ctx->VertexProgram._Enabled
-          && arrayObj->VertexAttrib[i].Enabled) {
-         min = update_min(min, &arrayObj->VertexAttrib[i]);
-      }
-      else if (i - VERT_ATTRIB_TEX0 < ctx->Const.MaxTextureCoordUnits
-               && arrayObj->TexCoord[i - VERT_ATTRIB_TEX0].Enabled) {
-         min = update_min(min, &arrayObj->TexCoord[i - VERT_ATTRIB_TEX0]);
-      }
-   }
-
-   /* 16..31 */
-   if (ctx->VertexProgram._Current) {
-      for (i = 0; i < Elements(arrayObj->VertexAttrib); i++) {
-         if (arrayObj->VertexAttrib[i].Enabled) {
-            min = update_min(min, &arrayObj->VertexAttrib[i]);
-         }
-      }
-   }
-
-   if (arrayObj->EdgeFlag.Enabled) {
-      min = update_min(min, &arrayObj->EdgeFlag);
-   }
-
-   /* _MaxElement is one past the last legal array element */
-   arrayObj->_MaxElement = min;
-}
-
-
-/**
- * Update the following fields:
- *   ctx->VertexProgram._Enabled
- *   ctx->FragmentProgram._Enabled
- *   ctx->ATIFragmentShader._Enabled
- * This needs to be done before texture state validation.
- */
-static void
-update_program_enables(struct gl_context *ctx)
+void
+_mesa_update_allow_draw_out_of_order(struct gl_context *ctx)
 {
-   /* These _Enabled flags indicate if the program is enabled AND valid. */
-   ctx->VertexProgram._Enabled = ctx->VertexProgram.Enabled
-      && ctx->VertexProgram.Current->Base.Instructions;
-   ctx->FragmentProgram._Enabled = ctx->FragmentProgram.Enabled
-      && ctx->FragmentProgram.Current->Base.Instructions;
-   ctx->ATIFragmentShader._Enabled = ctx->ATIFragmentShader.Enabled
-      && ctx->ATIFragmentShader.Current->Instructions[0];
+   /* Out-of-order drawing is useful when vertex array draws and immediate
+    * mode are interleaved.
+    *
+    * Example with 3 draws:
+    *   glBegin();
+    *      glVertex();
+    *   glEnd();
+    *   glDrawElements();
+    *   glBegin();
+    *      glVertex();
+    *   glEnd();
+    *
+    * Out-of-order drawing changes the execution order like this:
+    *   glDrawElements();
+    *   glBegin();
+    *      glVertex();
+    *      glVertex();
+    *   glEnd();
+    *
+    * If out-of-order draws are enabled, immediate mode vertices are not
+    * flushed before glDrawElements, resulting in fewer draws and lower CPU
+    * overhead. This helps workstation applications.
+    *
+    * This is a simplified version of out-of-order determination to catch
+    * common cases.
+    *
+    * RadeonSI has a complete and more complicated out-of-order determination
+    * for driver-internal reasons.
+    */
+   /* Only the compatibility profile with immediate mode needs this. */
+   if (ctx->API != API_OPENGL_COMPAT || !ctx->Const.AllowDrawOutOfOrder)
+      return;
+
+   /* If all of these are NULL, GLSL is disabled. */
+   struct gl_program *vs =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
+   struct gl_program *tcs =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
+   struct gl_program *tes =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
+   struct gl_program *gs =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
+   struct gl_program *fs =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
+   GLenum16 depth_func = ctx->Depth.Func;
+
+   /* Z fighting and any primitives with equal Z shouldn't be reordered
+    * with LESS/LEQUAL/GREATER/GEQUAL functions.
+    *
+    * When drawing 2 primitive with equal Z:
+    * - with LEQUAL/GEQUAL, the last primitive wins the Z test.
+    * - with LESS/GREATER, the first primitive wins the Z test.
+    *
+    * Here we ignore that on the basis that such cases don't occur in real
+    * apps, and we they do occur, they occur with blending where out-of-order
+    * drawing is always disabled.
+    */
+   bool previous_state = ctx->_AllowDrawOutOfOrder;
+   ctx->_AllowDrawOutOfOrder =
+         ctx->DrawBuffer &&
+         ctx->DrawBuffer->Visual.depthBits &&
+         ctx->Depth.Test &&
+         ctx->Depth.Mask &&
+         (depth_func == GL_NEVER ||
+          depth_func == GL_LESS ||
+          depth_func == GL_LEQUAL ||
+          depth_func == GL_GREATER ||
+          depth_func == GL_GEQUAL) &&
+         (!ctx->DrawBuffer->Visual.stencilBits ||
+          !ctx->Stencil.Enabled) &&
+         (!ctx->Color.ColorMask ||
+          (!ctx->Color.BlendEnabled &&
+           (!ctx->Color.ColorLogicOpEnabled ||
+            ctx->Color._LogicOp == COLOR_LOGICOP_COPY))) &&
+         (!vs || !vs->info.writes_memory) &&
+         (!tes || !tes->info.writes_memory) &&
+         (!tcs || !tcs->info.writes_memory) &&
+         (!gs || !gs->info.writes_memory) &&
+         (!fs || !fs->info.writes_memory || !fs->info.fs.early_fragment_tests);
+
+   /* If we are disabling out-of-order drawing, we need to flush queued
+    * vertices.
+    */
+   if (previous_state && !ctx->_AllowDrawOutOfOrder)
+      FLUSH_VERTICES(ctx, 0);
 }
 
 
 /**
- * Update vertex/fragment program state.  In particular, update these fields:
- *   ctx->VertexProgram._Current
- *   ctx->VertexProgram._TnlProgram,
- * These point to the highest priority enabled vertex/fragment program or are
- * NULL if fixed-function processing is to be done.
+ * Update the ctx->*Program._Current pointers to point to the
+ * current/active programs.
+ *
+ * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
+ * programs or programs derived from fixed-function state.
  *
  * This function needs to be called after texture state validation in case
  * we're generating a fragment program from fixed-function texture state.
@@ -247,13 +163,24 @@ update_program_enables(struct gl_context *ctx)
 static GLbitfield
 update_program(struct gl_context *ctx)
 {
-   const struct gl_shader_program *vsProg = ctx->Shader.CurrentVertexProgram;
-   const struct gl_shader_program *gsProg = ctx->Shader.CurrentGeometryProgram;
-   const struct gl_shader_program *fsProg = ctx->Shader.CurrentFragmentProgram;
-   const struct gl_vertex_program *prevVP = ctx->VertexProgram._Current;
-   const struct gl_fragment_program *prevFP = ctx->FragmentProgram._Current;
-   const struct gl_geometry_program *prevGP = ctx->GeometryProgram._Current;
-   GLbitfield new_state = 0x0;
+   struct gl_program *vsProg =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
+   struct gl_program *tcsProg =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
+   struct gl_program *tesProg =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
+   struct gl_program *gsProg =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
+   struct gl_program *fsProg =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
+   struct gl_program *csProg =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
+   const struct gl_program *prevVP = ctx->VertexProgram._Current;
+   const struct gl_program *prevFP = ctx->FragmentProgram._Current;
+   const struct gl_program *prevGP = ctx->GeometryProgram._Current;
+   const struct gl_program *prevTCP = ctx->TessCtrlProgram._Current;
+   const struct gl_program *prevTEP = ctx->TessEvalProgram._Current;
+   const struct gl_program *prevCP = ctx->ComputeProgram._Current;
 
    /*
     * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
@@ -264,133 +191,171 @@ update_program(struct gl_context *ctx)
     * follows:
     *   1. OpenGL 2.0/ARB vertex/fragment shaders
     *   2. ARB/NV vertex/fragment programs
-    *   3. Programs derived from fixed-function state.
+    *   3. ATI fragment shader
+    *   4. Programs derived from fixed-function state.
     *
     * Note: it's possible for a vertex shader to get used with a fragment
     * program (and vice versa) here, but in practice that shouldn't ever
     * come up, or matter.
     */
 
-   if (fsProg && fsProg->LinkStatus && fsProg->FragmentProgram) {
-      /* Use shader programs */
-      _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
-                               fsProg->FragmentProgram);
-   }
-   else if (ctx->FragmentProgram._Enabled) {
-      /* use user-defined vertex program */
-      _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
-                               ctx->FragmentProgram.Current);
+   if (fsProg) {
+      /* Use GLSL fragment shader */
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, fsProg);
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
+                              NULL);
+   }
+   else if (_mesa_arb_fragment_program_enabled(ctx)) {
+      /* Use user-defined fragment program */
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
+                              ctx->FragmentProgram.Current);
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
+                             NULL);
+   }
+   else if (_mesa_ati_fragment_shader_enabled(ctx) &&
+            ctx->ATIFragmentShader.Current->Program) {
+       /* Use the enabled ATI fragment shader's associated program */
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
+                              ctx->ATIFragmentShader.Current->Program);
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
+                              NULL);
    }
    else if (ctx->FragmentProgram._MaintainTexEnvProgram) {
-      /* Use fragment program generated from fixed-function state.
-       */
-      _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
-                               _mesa_get_fixed_func_fragment_program(ctx));
-      _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
-                               ctx->FragmentProgram._Current);
+      /* Use fragment program generated from fixed-function state */
+      struct gl_shader_program *f = _mesa_get_fixed_func_fragment_program(ctx);
+
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
+                             f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program);
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
+                             f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program);
    }
    else {
-      /* no fragment program */
-      _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
+      /* No fragment program */
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, NULL);
+      _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
+                             NULL);
    }
 
-   if (gsProg && gsProg->LinkStatus && gsProg->GeometryProgram) {
-      /* Use shader programs */
-      _mesa_reference_geomprog(ctx, &ctx->GeometryProgram._Current,
-                               gsProg->GeometryProgram);
+   if (gsProg) {
+      /* Use GLSL geometry shader */
+      _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, gsProg);
    } else {
-      /* no fragment program */
-      _mesa_reference_geomprog(ctx, &ctx->GeometryProgram._Current, NULL);
+      /* No geometry program */
+      _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, NULL);
+   }
+
+   if (tesProg) {
+      /* Use GLSL tessellation evaluation shader */
+      _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, tesProg);
+   }
+   else {
+      /* No tessellation evaluation program */
+      _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, NULL);
+   }
+
+   if (tcsProg) {
+      /* Use GLSL tessellation control shader */
+      _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, tcsProg);
+   }
+   else {
+      /* No tessellation control program */
+      _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, NULL);
    }
 
    /* Examine vertex program after fragment program as
     * _mesa_get_fixed_func_vertex_program() needs to know active
     * fragprog inputs.
     */
-   if (vsProg && vsProg->LinkStatus && vsProg->VertexProgram) {
-      /* Use shader programs */
-      _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
-                            vsProg->VertexProgram);
+   if (vsProg) {
+      /* Use GLSL vertex shader */
+      assert(VP_MODE_SHADER == ctx->VertexProgram._VPMode);
+      _mesa_reference_program(ctx, &ctx->VertexProgram._Current, vsProg);
    }
-   else if (ctx->VertexProgram._Enabled) {
-      /* use user-defined vertex program */
-      _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
-                               ctx->VertexProgram.Current);
+   else if (_mesa_arb_vertex_program_enabled(ctx)) {
+      /* Use user-defined vertex program */
+      assert(VP_MODE_SHADER == ctx->VertexProgram._VPMode);
+      _mesa_reference_program(ctx, &ctx->VertexProgram._Current,
+                              ctx->VertexProgram.Current);
    }
    else if (ctx->VertexProgram._MaintainTnlProgram) {
-      /* Use vertex program generated from fixed-function state.
-       */
-      _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
-                               _mesa_get_fixed_func_vertex_program(ctx));
-      _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram,
-                               ctx->VertexProgram._Current);
+      /* Use vertex program generated from fixed-function state */
+      assert(VP_MODE_FF == ctx->VertexProgram._VPMode);
+      _mesa_reference_program(ctx, &ctx->VertexProgram._Current,
+                              _mesa_get_fixed_func_vertex_program(ctx));
+      _mesa_reference_program(ctx, &ctx->VertexProgram._TnlProgram,
+                              ctx->VertexProgram._Current);
    }
    else {
       /* no vertex program */
-      _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
+      assert(VP_MODE_FF == ctx->VertexProgram._VPMode);
+      _mesa_reference_program(ctx, &ctx->VertexProgram._Current, NULL);
+   }
+
+   if (csProg) {
+      /* Use GLSL compute shader */
+      _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, csProg);
+   } else {
+      /* no compute program */
+      _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, NULL);
    }
 
    /* Let the driver know what's happening:
     */
-   if (ctx->FragmentProgram._Current != prevFP) {
-      new_state |= _NEW_PROGRAM;
-      if (ctx->Driver.BindProgram) {
-         ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
-                          (struct gl_program *) ctx->FragmentProgram._Current);
-      }
-   }
+   if (ctx->FragmentProgram._Current != prevFP ||
+       ctx->VertexProgram._Current != prevVP ||
+       ctx->GeometryProgram._Current != prevGP ||
+       ctx->TessEvalProgram._Current != prevTEP ||
+       ctx->TessCtrlProgram._Current != prevTCP ||
+       ctx->ComputeProgram._Current != prevCP)
+      return _NEW_PROGRAM;
+
+   return 0;
+}
 
-   if (ctx->GeometryProgram._Current != prevGP) {
-      new_state |= _NEW_PROGRAM;
-      if (ctx->Driver.BindProgram) {
-         ctx->Driver.BindProgram(ctx, MESA_GEOMETRY_PROGRAM,
-                            (struct gl_program *) ctx->GeometryProgram._Current);
-      }
-   }
 
-   if (ctx->VertexProgram._Current != prevVP) {
-      new_state |= _NEW_PROGRAM;
-      if (ctx->Driver.BindProgram) {
-         ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
-                            (struct gl_program *) ctx->VertexProgram._Current);
+static GLbitfield
+update_single_program_constants(struct gl_context *ctx,
+                                struct gl_program *prog,
+                                gl_shader_stage stage)
+{
+   if (prog) {
+      const struct gl_program_parameter_list *params = prog->Parameters;
+      if (params && params->StateFlags & ctx->NewState) {
+         if (ctx->DriverFlags.NewShaderConstants[stage])
+            ctx->NewDriverState |= ctx->DriverFlags.NewShaderConstants[stage];
+         else
+            return _NEW_PROGRAM_CONSTANTS;
       }
    }
-
-   return new_state;
+   return 0;
 }
 
 
 /**
+ * This updates fixed-func state constants such as gl_ModelViewMatrix.
  * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
  */
 static GLbitfield
 update_program_constants(struct gl_context *ctx)
 {
-   GLbitfield new_state = 0x0;
-
-   if (ctx->FragmentProgram._Current) {
-      const struct gl_program_parameter_list *params =
-         ctx->FragmentProgram._Current->Base.Parameters;
-      if (params && params->StateFlags & ctx->NewState) {
-         new_state |= _NEW_PROGRAM_CONSTANTS;
-      }
-   }
-
-   if (ctx->GeometryProgram._Current) {
-      const struct gl_program_parameter_list *params =
-         ctx->GeometryProgram._Current->Base.Parameters;
-      /*FIXME: StateFlags is always 0 because we have unnamed constant
-       *       not state changes */
-      if (params /*&& params->StateFlags & ctx->NewState*/) {
-         new_state |= _NEW_PROGRAM_CONSTANTS;
-      }
-   }
-
-   if (ctx->VertexProgram._Current) {
-      const struct gl_program_parameter_list *params =
-         ctx->VertexProgram._Current->Base.Parameters;
-      if (params && params->StateFlags & ctx->NewState) {
-         new_state |= _NEW_PROGRAM_CONSTANTS;
+   GLbitfield new_state =
+      update_single_program_constants(ctx, ctx->VertexProgram._Current,
+                                      MESA_SHADER_VERTEX) |
+      update_single_program_constants(ctx, ctx->FragmentProgram._Current,
+                                      MESA_SHADER_FRAGMENT);
+
+   if (ctx->API == API_OPENGL_COMPAT &&
+       ctx->Const.GLSLVersionCompat >= 150) {
+      new_state |=
+         update_single_program_constants(ctx, ctx->GeometryProgram._Current,
+                                         MESA_SHADER_GEOMETRY);
+
+      if (_mesa_has_ARB_tessellation_shader(ctx)) {
+         new_state |=
+            update_single_program_constants(ctx, ctx->TessCtrlProgram._Current,
+                                            MESA_SHADER_TESS_CTRL) |
+            update_single_program_constants(ctx, ctx->TessEvalProgram._Current,
+                                            MESA_SHADER_TESS_EVAL);
       }
    }
 
@@ -398,144 +363,23 @@ update_program_constants(struct gl_context *ctx)
 }
 
 
-
-
-static void
-update_viewport_matrix(struct gl_context *ctx)
-{
-   const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
-
-   ASSERT(depthMax > 0);
-
-   /* Compute scale and bias values. This is really driver-specific
-    * and should be maintained elsewhere if at all.
-    * NOTE: RasterPos uses this.
-    */
-   _math_matrix_viewport(&ctx->Viewport._WindowMap,
-                         ctx->Viewport.X, ctx->Viewport.Y,
-                         ctx->Viewport.Width, ctx->Viewport.Height,
-                         ctx->Viewport.Near, ctx->Viewport.Far,
-                         depthMax);
-}
-
-
-/**
- * Update derived multisample state.
- */
 static void
-update_multisample(struct gl_context *ctx)
+update_fixed_func_program_usage(struct gl_context *ctx)
 {
-   ctx->Multisample._Enabled = GL_FALSE;
-   if (ctx->Multisample.Enabled &&
-       ctx->DrawBuffer &&
-       ctx->DrawBuffer->Visual.sampleBuffers)
-      ctx->Multisample._Enabled = GL_TRUE;
+   ctx->FragmentProgram._UsesTexEnvProgram =
+      ctx->FragmentProgram._MaintainTexEnvProgram &&
+      !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] && /* GLSL*/
+      !_mesa_arb_fragment_program_enabled(ctx) &&
+      !(_mesa_ati_fragment_shader_enabled(ctx) &&
+        ctx->ATIFragmentShader.Current->Program);
+
+   ctx->VertexProgram._UsesTnlProgram =
+      ctx->VertexProgram._MaintainTnlProgram &&
+      !ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] && /* GLSL */
+      !_mesa_arb_vertex_program_enabled(ctx);
 }
 
 
-/**
- * Update derived color/blend/logicop state.
- */
-static void
-update_color(struct gl_context *ctx)
-{
-   /* This is needed to support 1.1's RGB logic ops AND
-    * 1.0's blending logicops.
-    */
-   ctx->Color._LogicOpEnabled = RGBA_LOGICOP_ENABLED(ctx);
-}
-
-
-/*
- * Check polygon state and set DD_TRI_CULL_FRONT_BACK and/or DD_TRI_OFFSET
- * in ctx->_TriangleCaps if needed.
- */
-static void
-update_polygon(struct gl_context *ctx)
-{
-   ctx->_TriangleCaps &= ~(DD_TRI_CULL_FRONT_BACK | DD_TRI_OFFSET);
-
-   if (ctx->Polygon.CullFlag && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
-      ctx->_TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
-
-   if (   ctx->Polygon.OffsetPoint
-       || ctx->Polygon.OffsetLine
-       || ctx->Polygon.OffsetFill)
-      ctx->_TriangleCaps |= DD_TRI_OFFSET;
-}
-
-
-/**
- * Update the ctx->_TriangleCaps bitfield.
- * XXX that bitfield should really go away someday!
- * This function must be called after other update_*() functions since
- * there are dependencies on some other derived values.
- */
-#if 0
-static void
-update_tricaps(struct gl_context *ctx, GLbitfield new_state)
-{
-   ctx->_TriangleCaps = 0;
-
-   /*
-    * Points
-    */
-   if (1/*new_state & _NEW_POINT*/) {
-      if (ctx->Point.SmoothFlag)
-         ctx->_TriangleCaps |= DD_POINT_SMOOTH;
-      if (ctx->Point._Attenuated)
-         ctx->_TriangleCaps |= DD_POINT_ATTEN;
-   }
-
-   /*
-    * Lines
-    */
-   if (1/*new_state & _NEW_LINE*/) {
-      if (ctx->Line.SmoothFlag)
-         ctx->_TriangleCaps |= DD_LINE_SMOOTH;
-      if (ctx->Line.StippleFlag)
-         ctx->_TriangleCaps |= DD_LINE_STIPPLE;
-   }
-
-   /*
-    * Polygons
-    */
-   if (1/*new_state & _NEW_POLYGON*/) {
-      if (ctx->Polygon.SmoothFlag)
-         ctx->_TriangleCaps |= DD_TRI_SMOOTH;
-      if (ctx->Polygon.StippleFlag)
-         ctx->_TriangleCaps |= DD_TRI_STIPPLE;
-      if (ctx->Polygon.FrontMode != GL_FILL
-          || ctx->Polygon.BackMode != GL_FILL)
-         ctx->_TriangleCaps |= DD_TRI_UNFILLED;
-      if (ctx->Polygon.CullFlag
-          && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
-         ctx->_TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
-      if (ctx->Polygon.OffsetPoint ||
-          ctx->Polygon.OffsetLine ||
-          ctx->Polygon.OffsetFill)
-         ctx->_TriangleCaps |= DD_TRI_OFFSET;
-   }
-
-   /*
-    * Lighting and shading
-    */
-   if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
-      ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
-   if (ctx->Light.ShadeModel == GL_FLAT)
-      ctx->_TriangleCaps |= DD_FLATSHADE;
-   if (NEED_SECONDARY_COLOR(ctx))
-      ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
-
-   /*
-    * Stencil
-    */
-   if (ctx->Stencil._TestTwoSide)
-      ctx->_TriangleCaps |= DD_TRI_TWOSTENCIL;
-}
-#endif
-
-
 /**
  * Compute derived GL state.
  * If __struct gl_contextRec::NewState is non-zero then this function \b must
@@ -552,116 +396,102 @@ void
 _mesa_update_state_locked( struct gl_context *ctx )
 {
    GLbitfield new_state = ctx->NewState;
-   GLbitfield prog_flags = _NEW_PROGRAM;
    GLbitfield new_prog_state = 0x0;
+   const GLbitfield computed_states = ~(_NEW_CURRENT_ATTRIB | _NEW_LINE);
 
-   if (new_state == _NEW_CURRENT_ATTRIB) 
+   /* we can skip a bunch of state validation checks if the dirty
+    * state matches one or more bits in 'computed_states'.
+    */
+   if ((new_state & computed_states) == 0)
       goto out;
 
    if (MESA_VERBOSE & VERBOSE_STATE)
       _mesa_print_state("_mesa_update_state", new_state);
 
-   /* Determine which state flags effect vertex/fragment program state */
-   if (ctx->FragmentProgram._MaintainTexEnvProgram) {
-      prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE | _NEW_FOG |
-                    _NEW_ARRAY | _NEW_LIGHT | _NEW_POINT | _NEW_RENDERMODE |
-                    _NEW_PROGRAM);
-   }
-   if (ctx->VertexProgram._MaintainTnlProgram) {
-      prog_flags |= (_NEW_ARRAY | _NEW_TEXTURE | _NEW_TEXTURE_MATRIX |
-                     _NEW_TRANSFORM | _NEW_POINT |
-                     _NEW_FOG | _NEW_LIGHT |
-                     _MESA_NEW_NEED_EYE_COORDS);
-   }
-
-   /*
-    * Now update derived state info
-    */
-
-   if (new_state & prog_flags)
-      update_program_enables( ctx );
-
-   if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
-      _mesa_update_modelview_project( ctx, new_state );
-
-   if (new_state & (_NEW_PROGRAM|_NEW_TEXTURE|_NEW_TEXTURE_MATRIX))
-      _mesa_update_texture( ctx, new_state );
-
    if (new_state & _NEW_BUFFERS)
-      _mesa_update_framebuffer(ctx);
-
-   if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
-      _mesa_update_draw_buffer_bounds( ctx );
+      _mesa_update_framebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer);
 
-   if (new_state & _NEW_POLYGON)
-      update_polygon( ctx );
+   /* Handle Core and Compatibility contexts separately. */
+   if (ctx->API == API_OPENGL_COMPAT ||
+       ctx->API == API_OPENGLES) {
+      GLbitfield prog_flags = _NEW_PROGRAM;
 
-   if (new_state & _NEW_LIGHT)
-      _mesa_update_lighting( ctx );
+      if (new_state & _NEW_PROGRAM)
+         update_fixed_func_program_usage(ctx);
 
-   if (new_state & (_NEW_STENCIL | _NEW_BUFFERS))
-      _mesa_update_stencil( ctx );
-
-   if (new_state & _MESA_NEW_TRANSFER_STATE)
-      _mesa_update_pixel( ctx, new_state );
+      /* Determine which states affect fixed-func vertex/fragment program. */
+      if (ctx->FragmentProgram._UsesTexEnvProgram) {
+         prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE_OBJECT | _NEW_FOG |
+                        _NEW_VARYING_VP_INPUTS | _NEW_LIGHT | _NEW_POINT |
+                        _NEW_RENDERMODE | _NEW_COLOR | _NEW_TEXTURE_STATE);
+      }
 
-   if (new_state & _DD_NEW_SEPARATE_SPECULAR)
-      update_separate_specular( ctx );
+      if (ctx->VertexProgram._UsesTnlProgram) {
+         prog_flags |= (_NEW_VARYING_VP_INPUTS | _NEW_TEXTURE_OBJECT |
+                        _NEW_TEXTURE_MATRIX | _NEW_TRANSFORM | _NEW_POINT |
+                        _NEW_FOG | _NEW_LIGHT | _NEW_TEXTURE_STATE |
+                        _MESA_NEW_NEED_EYE_COORDS);
+      }
 
-   if (new_state & (_NEW_BUFFERS | _NEW_VIEWPORT))
-      update_viewport_matrix(ctx);
+      /*
+       * Now update derived state info
+       */
+      if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
+         _mesa_update_modelview_project( ctx, new_state );
 
-   if (new_state & _NEW_MULTISAMPLE)
-      update_multisample( ctx );
+      if (new_state & _NEW_TEXTURE_MATRIX)
+         _mesa_update_texture_matrices(ctx);
 
-   if (new_state & _NEW_COLOR)
-      update_color( ctx );
+      if (new_state & (_NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE | _NEW_PROGRAM))
+         _mesa_update_texture_state(ctx);
 
-#if 0
-   if (new_state & (_NEW_POINT | _NEW_LINE | _NEW_POLYGON | _NEW_LIGHT
-                    | _NEW_STENCIL | _DD_NEW_SEPARATE_SPECULAR))
-      update_tricaps( ctx, new_state );
-#endif
+      if (new_state & _NEW_LIGHT)
+         _mesa_update_lighting(ctx);
 
-   /* ctx->_NeedEyeCoords is now up to date.
-    *
-    * If the truth value of this variable has changed, update for the
-    * new lighting space and recompute the positions of lights and the
-    * normal transform.
-    *
-    * If the lighting space hasn't changed, may still need to recompute
-    * light positions & normal transforms for other reasons.
-    */
-   if (new_state & _MESA_NEW_NEED_EYE_COORDS) 
-      _mesa_update_tnl_spaces( ctx, new_state );
+      if (new_state & _NEW_PIXEL)
+         _mesa_update_pixel( ctx );
 
-   if (new_state & prog_flags) {
-      /* When we generate programs from fixed-function vertex/fragment state
-       * this call may generate/bind a new program.  If so, we need to
-       * propogate the _NEW_PROGRAM flag to the driver.
+      /* ctx->_NeedEyeCoords is now up to date.
+       *
+       * If the truth value of this variable has changed, update for the
+       * new lighting space and recompute the positions of lights and the
+       * normal transform.
+       *
+       * If the lighting space hasn't changed, may still need to recompute
+       * light positions & normal transforms for other reasons.
        */
-      new_prog_state |= update_program( ctx );
-   }
+      if (new_state & _MESA_NEW_NEED_EYE_COORDS)
+         _mesa_update_tnl_spaces( ctx, new_state );
+
+      if (new_state & prog_flags) {
+         /* When we generate programs from fixed-function vertex/fragment state
+          * this call may generate/bind a new program.  If so, we need to
+          * propogate the _NEW_PROGRAM flag to the driver.
+          */
+         new_prog_state |= update_program(ctx);
+      }
+   } else {
+      /* GL Core and GLES 2/3 contexts */
+      if (new_state & (_NEW_TEXTURE_OBJECT | _NEW_PROGRAM))
+         _mesa_update_texture_state(ctx);
 
-   if (new_state & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT))
-      update_arrays( ctx );
+      if (new_state & _NEW_PROGRAM)
+         update_program(ctx);
+   }
 
  out:
    new_prog_state |= update_program_constants(ctx);
 
+   ctx->NewState |= new_prog_state;
+
    /*
     * Give the driver a chance to act upon the new_state flags.
     * The driver might plug in different span functions, for example.
     * Also, this is where the driver can invalidate the state of any
     * active modules (such as swrast_setup, swrast, tnl, etc).
-    *
-    * Set ctx->NewState to zero to avoid recursion if
-    * Driver.UpdateState() has to call FLUSH_VERTICES().  (fixed?)
     */
-   new_state = ctx->NewState | new_prog_state;
+   ctx->Driver.UpdateState(ctx);
    ctx->NewState = 0;
-   ctx->Driver.UpdateState(ctx, new_state);
-   ctx->Array.NewState = 0;
 }
 
 
@@ -700,14 +530,24 @@ _mesa_update_state( struct gl_context *ctx )
  * may vary or otherwise differ from the ctx->Current values.
  * Otherwise, the fp should track them as state values instead.
  */
-void
-_mesa_set_varying_vp_inputs( struct gl_context *ctx,
-                             GLbitfield varying_inputs )
+static void
+set_varying_vp_inputs(struct gl_context *ctx, GLbitfield varying_inputs)
 {
+   /*
+    * The gl_context::varying_vp_inputs value is only used when in
+    * VP_MODE_FF mode.
+    */
+   if (VP_MODE_FF != ctx->VertexProgram._VPMode)
+      return;
+
+   /* Only fixed-func generated programs ever uses varying_vp_inputs. */
+   if (!ctx->VertexProgram._MaintainTnlProgram &&
+       !ctx->FragmentProgram._MaintainTexEnvProgram)
+      return;
+
    if (ctx->varying_vp_inputs != varying_inputs) {
       ctx->varying_vp_inputs = varying_inputs;
-      ctx->NewState |= _NEW_ARRAY;
-      /*printf("%s %x\n", __FUNCTION__, varying_inputs);*/
+      ctx->NewState |= _NEW_VARYING_VP_INPUTS;
    }
 }
 
@@ -730,3 +570,82 @@ _mesa_set_vp_override(struct gl_context *ctx, GLboolean flag)
       ctx->NewState |= _NEW_PROGRAM;
    }
 }
+
+
+static void
+set_vertex_processing_mode(struct gl_context *ctx, gl_vertex_processing_mode m)
+{
+   if (ctx->VertexProgram._VPMode == m)
+      return;
+
+   /* On change we may get new maps into the current values */
+   ctx->NewDriverState |= ctx->DriverFlags.NewArray;
+
+   /* Finally memorize the value */
+   ctx->VertexProgram._VPMode = m;
+
+   /* Since we only track the varying inputs while being in fixed function
+    * vertex processing mode, we may need to recheck for the
+    * _NEW_VARYING_VP_INPUTS bit.
+    */
+   set_varying_vp_inputs(ctx, ctx->Array._DrawVAOEnabledAttribs);
+}
+
+
+/**
+ * Update ctx->VertexProgram._VPMode.
+ * This is to distinguish whether we're running
+ *   a vertex program/shader,
+ *   a fixed-function TNL program or
+ *   a fixed function vertex transformation without any program.
+ */
+void
+_mesa_update_vertex_processing_mode(struct gl_context *ctx)
+{
+   if (ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX])
+      set_vertex_processing_mode(ctx, VP_MODE_SHADER);
+   else if (_mesa_arb_vertex_program_enabled(ctx))
+      set_vertex_processing_mode(ctx, VP_MODE_SHADER);
+   else
+      set_vertex_processing_mode(ctx, VP_MODE_FF);
+}
+
+
+/**
+ * Set the _DrawVAO and the net enabled arrays.
+ * The vao->_Enabled bitmask is transformed due to position/generic0
+ * as stored in vao->_AttributeMapMode. Then the filter bitmask is applied
+ * to filter out arrays unwanted for the currently executed draw operation.
+ * For example, the generic attributes are masked out form the _DrawVAO's
+ * enabled arrays when a fixed function array draw is executed.
+ */
+void
+_mesa_set_draw_vao(struct gl_context *ctx, struct gl_vertex_array_object *vao,
+                   GLbitfield filter)
+{
+   struct gl_vertex_array_object **ptr = &ctx->Array._DrawVAO;
+   bool new_array = false;
+   if (*ptr != vao) {
+      _mesa_reference_vao_(ctx, ptr, vao);
+
+      new_array = true;
+   }
+
+   if (vao->NewArrays) {
+      _mesa_update_vao_derived_arrays(ctx, vao);
+      vao->NewArrays = 0;
+
+      new_array = true;
+   }
+
+   /* May shuffle the position and generic0 bits around, filter out unwanted */
+   const GLbitfield enabled = filter & _mesa_get_vao_vp_inputs(vao);
+   if (ctx->Array._DrawVAOEnabledAttribs != enabled)
+      new_array = true;
+
+   if (new_array)
+      ctx->NewDriverState |= ctx->DriverFlags.NewArray;
+
+   ctx->Array._DrawVAOEnabledAttribs = enabled;
+   set_varying_vp_inputs(ctx, enabled);
+}