mesa: Allow extensions in MESA_EXTENSION_OVERRIDE to be prefixed with '+'
[mesa.git] / src / mesa / main / arbprogram.c
index 54df8183b5c1a675d6f3d8d39f927bab1070bc78..26d781954eda19db45b53717eff0ab87ea3c2463 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
+ * Version:  7.0
  *
- * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
+ * 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"),
 
 /**
  * \file arbprogram.c
- * \brief ARB_vertex/fragment_program state management functions.
+ * ARB_vertex/fragment_program state management functions.
  * \author Brian Paul
  */
 
 
-#include "glheader.h"
-#include "context.h"
-#include "hash.h"
-#include "imports.h"
-#include "macros.h"
-#include "mtypes.h"
-#include "arbprogram.h"
+#include "main/glheader.h"
+#include "main/context.h"
+#include "main/hash.h"
+#include "main/imports.h"
+#include "main/macros.h"
+#include "main/mtypes.h"
+#include "main/arbprogram.h"
+#include "program/arbprogparse.h"
+#include "program/nvfragparse.h"
+#include "program/nvvertparse.h"
+#include "program/program.h"
 
 
 
-void
-_mesa_VertexAttrib1sARB(GLuint index, GLshort x)
-{
-}
-
-void
-_mesa_VertexAttrib1fARB(GLuint index, GLfloat x)
-{
-}
-
-void
-_mesa_VertexAttrib1dARB(GLuint index, GLdouble x)
-{
-}
-
-void
-_mesa_VertexAttrib2sARB(GLuint index, GLshort x, GLshort y)
+/**
+ * Mixing ARB and NV vertex/fragment programs can be tricky.
+ * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
+ *  but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
+ * The two different fragment program targets are supposed to be compatible
+ * to some extent (see GL_ARB_fragment_program spec).
+ * This function does the compatibility check.
+ */
+static GLboolean
+compatible_program_targets(GLenum t1, GLenum t2)
 {
+   if (t1 == t2)
+      return GL_TRUE;
+   if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
+      return GL_TRUE;
+   if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
+      return GL_TRUE;
+   return GL_FALSE;
 }
 
-void
-_mesa_VertexAttrib2fARB(GLuint index, GLfloat x, GLfloat y)
-{
-}
 
-void
-_mesa_VertexAttrib2dARB(GLuint index, GLdouble x, GLdouble y)
+/**
+ * Bind a program (make it current)
+ * \note Called from the GL API dispatcher by both glBindProgramNV
+ * and glBindProgramARB.
+ */
+void GLAPIENTRY
+_mesa_BindProgram(GLenum target, GLuint id)
 {
-}
+   struct gl_program *curProg, *newProg;
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-void
-_mesa_VertexAttrib3sARB(GLuint index, GLshort x, GLshort y, GLshort z)
-{
-}
+   /* Error-check target and get curProg */
+   if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
+        (ctx->Extensions.NV_vertex_program ||
+         ctx->Extensions.ARB_vertex_program)) {
+      curProg = &ctx->VertexProgram.Current->Base;
+   }
+   else if ((target == GL_FRAGMENT_PROGRAM_NV
+             && ctx->Extensions.NV_fragment_program) ||
+            (target == GL_FRAGMENT_PROGRAM_ARB
+             && ctx->Extensions.ARB_fragment_program)) {
+      curProg = &ctx->FragmentProgram.Current->Base;
+   }
+   else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
+      return;
+   }
 
-void
-_mesa_VertexAttrib3fARB(GLuint index, GLfloat x, GLfloat y, GLfloat z)
-{
-}
+   /*
+    * Get pointer to new program to bind.
+    * NOTE: binding to a non-existant program is not an error.
+    * That's supposed to be caught in glBegin.
+    */
+   if (id == 0) {
+      /* Bind a default program */
+      newProg = NULL;
+      if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
+         newProg = &ctx->Shared->DefaultVertexProgram->Base;
+      else
+         newProg = &ctx->Shared->DefaultFragmentProgram->Base;
+   }
+   else {
+      /* Bind a user program */
+      newProg = _mesa_lookup_program(ctx, id);
+      if (!newProg || newProg == &_mesa_DummyProgram) {
+         /* allocate a new program now */
+         newProg = ctx->Driver.NewProgram(ctx, target, id);
+         if (!newProg) {
+            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
+            return;
+         }
+         _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
+      }
+      else if (!compatible_program_targets(newProg->Target, target)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBindProgramNV/ARB(target mismatch)");
+         return;
+      }
+   }
 
-void
-_mesa_VertexAttrib3dARB(GLuint index, GLdouble x, GLdouble y, GLdouble z)
-{
-}
+   /** All error checking is complete now **/
 
-void
-_mesa_VertexAttrib4sARB(GLuint index, GLshort x, GLshort y, GLshort z, GLshort w)
-{
-}
+   if (curProg->Id == id) {
+      /* binding same program - no change */
+      return;
+   }
 
-void
-_mesa_VertexAttrib4fARB(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-}
+   /* signal new program (and its new constants) */
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
 
-void
-_mesa_VertexAttrib4dARB(GLuint index, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-}
+   /* bind newProg */
+   if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
+      _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
+                               (struct gl_vertex_program *) newProg);
+   }
+   else if (target == GL_FRAGMENT_PROGRAM_NV ||
+            target == GL_FRAGMENT_PROGRAM_ARB) {
+      _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
+                               (struct gl_fragment_program *) newProg);
+   }
 
-void
-_mesa_VertexAttrib4NubARB(GLuint index, GLubyte x, GLubyte y, GLubyte z, GLubyte w)
-{
-}
+   /* Never null pointers */
+   ASSERT(ctx->VertexProgram.Current);
+   ASSERT(ctx->FragmentProgram.Current);
 
-void
-_mesa_VertexAttrib1svARB(GLuint index, const GLshort *v)
-{
+   if (ctx->Driver.BindProgram)
+      ctx->Driver.BindProgram(ctx, target, newProg);
 }
 
-void
-_mesa_VertexAttrib1fvARB(GLuint index, const GLfloat *v)
-{
-}
 
-void
-_mesa_VertexAttrib1dvARB(GLuint index, const GLdouble *v)
+/**
+ * Delete a list of programs.
+ * \note Not compiled into display lists.
+ * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
+ */
+void GLAPIENTRY 
+_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
 {
-}
+   GLint i;
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-void
-_mesa_VertexAttrib2svARB(GLuint index, const GLshort *v)
-{
-}
+   if (n < 0) {
+      _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
+      return;
+   }
 
-void
-_mesa_VertexAttrib2fvARB(GLuint index, const GLfloat *v)
-{
+   for (i = 0; i < n; i++) {
+      if (ids[i] != 0) {
+         struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
+         if (prog == &_mesa_DummyProgram) {
+            _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
+         }
+         else if (prog) {
+            /* Unbind program if necessary */
+            switch (prog->Target) {
+            case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
+            case GL_VERTEX_STATE_PROGRAM_NV:
+               if (ctx->VertexProgram.Current &&
+                   ctx->VertexProgram.Current->Base.Id == ids[i]) {
+                  /* unbind this currently bound program */
+                  _mesa_BindProgram(prog->Target, 0);
+               }
+               break;
+            case GL_FRAGMENT_PROGRAM_NV:
+            case GL_FRAGMENT_PROGRAM_ARB:
+               if (ctx->FragmentProgram.Current &&
+                   ctx->FragmentProgram.Current->Base.Id == ids[i]) {
+                  /* unbind this currently bound program */
+                  _mesa_BindProgram(prog->Target, 0);
+               }
+               break;
+            default:
+               _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
+               return;
+            }
+            /* The ID is immediately available for re-use now */
+            _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
+            _mesa_reference_program(ctx, &prog, NULL);
+         }
+      }
+   }
 }
 
-void
-_mesa_VertexAttrib2dvARB(GLuint index, const GLdouble *v)
-{
-}
 
-void
-_mesa_VertexAttrib3svARB(GLuint index, const GLshort *v)
+/**
+ * Generate a list of new program identifiers.
+ * \note Not compiled into display lists.
+ * \note Called by both glGenProgramsNV and glGenProgramsARB.
+ */
+void GLAPIENTRY
+_mesa_GenPrograms(GLsizei n, GLuint *ids)
 {
-}
+   GLuint first;
+   GLuint i;
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-void
-_mesa_VertexAttrib3fvARB(GLuint index, const GLfloat *v)
-{
-}
+   if (n < 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
+      return;
+   }
 
-void
-_mesa_VertexAttrib3dvARB(GLuint index, const GLdouble *v)
-{
-}
+   if (!ids)
+      return;
 
-void
-_mesa_VertexAttrib4bvARB(GLuint index, const GLbyte *v)
-{
-}
+   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
 
-void
-_mesa_VertexAttrib4svARB(GLuint index, const GLshort *v)
-{
-}
+   /* Insert pointer to dummy program as placeholder */
+   for (i = 0; i < (GLuint) n; i++) {
+      _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
+   }
 
-void
-_mesa_VertexAttrib4ivARB(GLuint index, const GLint *v)
-{
+   /* Return the program names */
+   for (i = 0; i < (GLuint) n; i++) {
+      ids[i] = first + i;
+   }
 }
 
-void
-_mesa_VertexAttrib4ubvARB(GLuint index, const GLubyte *v)
-{
-}
 
-void
-_mesa_VertexAttrib4usvARB(GLuint index, const GLushort *v)
+/**
+ * Determine if id names a vertex or fragment program.
+ * \note Not compiled into display lists.
+ * \note Called from both glIsProgramNV and glIsProgramARB.
+ * \param id is the program identifier
+ * \return GL_TRUE if id is a program, else GL_FALSE.
+ */
+GLboolean GLAPIENTRY
+_mesa_IsProgramARB(GLuint id)
 {
-}
+   struct gl_program *prog = NULL; 
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
 
-void
-_mesa_VertexAttrib4uivARB(GLuint index, const GLuint *v)
-{
-}
+   if (id == 0)
+      return GL_FALSE;
 
-void
-_mesa_VertexAttrib4fvARB(GLuint index, const GLfloat *v)
-{
+   prog = _mesa_lookup_program(ctx, id);
+   if (prog && (prog != &_mesa_DummyProgram))
+      return GL_TRUE;
+   else
+      return GL_FALSE;
 }
 
-void
-_mesa_VertexAttrib4dvARB(GLuint index, const GLdouble *v)
-{
-}
 
-void
-_mesa_VertexAttrib4NbvARB(GLuint index, const GLbyte *v)
+void GLAPIENTRY
+_mesa_ProgramStringARB(GLenum target, GLenum format, GLsizei len,
+                       const GLvoid *string)
 {
-}
+   struct gl_program *base;
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-void
-_mesa_VertexAttrib4NsvARB(GLuint index, const GLshort *v)
-{
-}
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM);
 
-void
-_mesa_VertexAttrib4NivARB(GLuint index, const GLint *v)
-{
-}
+   if (!ctx->Extensions.ARB_vertex_program
+       && !ctx->Extensions.ARB_fragment_program) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramStringARB()");
+      return;
+   }
 
-void
-_mesa_VertexAttrib4NubvARB(GLuint index, const GLubyte *v)
-{
-}
+   if (format != GL_PROGRAM_FORMAT_ASCII_ARB) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glProgramStringARB(format)");
+      return;
+   }
 
-void
-_mesa_VertexAttrib4NusvARB(GLuint index, const GLushort *v)
-{
-}
+   /* The first couple cases are complicated.  The same enum value is used for
+    * ARB and NV vertex programs.  If the target is a vertex program, parse it
+    * using the ARB grammar if the string starts with "!!ARB" or if
+    * NV_vertex_program is not supported.
+    */
+   if (target == GL_VERTEX_PROGRAM_ARB
+       && ctx->Extensions.ARB_vertex_program
+       && ((strncmp(string, "!!ARB", 5) == 0)
+          || !ctx->Extensions.NV_vertex_program)) {
+      struct gl_vertex_program *prog = ctx->VertexProgram.Current;
+      _mesa_parse_arb_vertex_program(ctx, target, string, len, prog);
 
-void
-_mesa_VertexAttrib4NuivARB(GLuint index, const GLuint *v)
-{
-}
+      base = & prog->Base;
+   }
+   else if ((target == GL_VERTEX_PROGRAM_ARB
+            || target == GL_VERTEX_STATE_PROGRAM_NV)
+           && ctx->Extensions.NV_vertex_program) {
+      struct gl_vertex_program *prog = ctx->VertexProgram.Current;
+      _mesa_parse_nv_vertex_program(ctx, target, string, len, prog);
 
+      base = & prog->Base;
+   }
+   else if (target == GL_FRAGMENT_PROGRAM_ARB
+            && ctx->Extensions.ARB_fragment_program) {
+      struct gl_fragment_program *prog = ctx->FragmentProgram.Current;
+      _mesa_parse_arb_fragment_program(ctx, target, string, len, prog);
 
-void
-_mesa_VertexAttribPointerARB(GLuint index, GLint size, GLenum type,
-                             GLboolean normalized, GLsizei stride,
-                             const GLvoid *pointer)
-{
-}
+      base = & prog->Base;
+   }
+   else if (target == GL_FRAGMENT_PROGRAM_NV
+            && ctx->Extensions.NV_fragment_program) {
+      struct gl_fragment_program *prog = ctx->FragmentProgram.Current;
+      _mesa_parse_nv_fragment_program(ctx, target, string, len, prog);
 
+      base = & prog->Base;
+   }
+   else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glProgramStringARB(target)");
+      return;
+   }
 
-void
-_mesa_EnableVertexAttribArrayARB(GLuint index)
-{
+   if (ctx->Program.ErrorPos == -1) {
+      /* finally, give the program to the driver for translation/checking */
+      if (!ctx->Driver.ProgramStringNotify(ctx, target, base)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glProgramStringARB(rejected by driver");
+      }
+   }
 }
 
 
-void
-_mesa_DisableVertexAttribArrayARB(GLuint index)
+/**
+ * Set a program env parameter register.
+ * \note Called from the GL API dispatcher.
+ * Note, this function is also used by the GL_NV_vertex_program extension
+ * (alias to ProgramParameterdNV)
+ */
+void GLAPIENTRY
+_mesa_ProgramEnvParameter4dARB(GLenum target, GLuint index,
+                               GLdouble x, GLdouble y, GLdouble z, GLdouble w)
 {
+   _mesa_ProgramEnvParameter4fARB(target, index, (GLfloat) x, (GLfloat) y, 
+                                 (GLfloat) z, (GLfloat) w);
 }
 
 
-void
-_mesa_GetVertexAttribdvARB(GLuint index, GLenum pname, GLdouble *params)
+/**
+ * Set a program env parameter register.
+ * \note Called from the GL API dispatcher.
+ * Note, this function is also used by the GL_NV_vertex_program extension
+ * (alias to ProgramParameterdvNV)
+ */
+void GLAPIENTRY
+_mesa_ProgramEnvParameter4dvARB(GLenum target, GLuint index,
+                                const GLdouble *params)
 {
+   _mesa_ProgramEnvParameter4fARB(target, index, (GLfloat) params[0], 
+                                 (GLfloat) params[1], (GLfloat) params[2], 
+                                 (GLfloat) params[3]);
 }
 
 
-void
-_mesa_GetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
+/**
+ * Set a program env parameter register.
+ * \note Called from the GL API dispatcher.
+ * Note, this function is also used by the GL_NV_vertex_program extension
+ * (alias to ProgramParameterfNV)
+ */
+void GLAPIENTRY
+_mesa_ProgramEnvParameter4fARB(GLenum target, GLuint index,
+                               GLfloat x, GLfloat y, GLfloat z, GLfloat w)
 {
-}
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
 
-void
-_mesa_GetVertexAttribivARB(GLuint index, GLenum pname, GLint *params)
-{
+   if (target == GL_FRAGMENT_PROGRAM_ARB
+       && ctx->Extensions.ARB_fragment_program) {
+      if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter(index)");
+         return;
+      }
+      ASSIGN_4V(ctx->FragmentProgram.Parameters[index], x, y, z, w);
+   }
+   else if (target == GL_VERTEX_PROGRAM_ARB /* == GL_VERTEX_PROGRAM_NV */
+       && (ctx->Extensions.ARB_vertex_program || ctx->Extensions.NV_vertex_program)) {
+      if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter(index)");
+         return;
+      }
+      ASSIGN_4V(ctx->VertexProgram.Parameters[index], x, y, z, w);
+   }
+   else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameter(target)");
+      return;
+   }
 }
 
 
-void
-_mesa_GetVertexAttribPointervARB(GLuint index, GLenum pname, GLvoid **pointer)
-{
-}
 
-
-void
-_mesa_ProgramStringARB(GLenum target, GLenum format, GLsizei len,
-                       const GLvoid *string)
+/**
+ * Set a program env parameter register.
+ * \note Called from the GL API dispatcher.
+ * Note, this function is also used by the GL_NV_vertex_program extension
+ * (alias to ProgramParameterfvNV)
+ */
+void GLAPIENTRY
+_mesa_ProgramEnvParameter4fvARB(GLenum target, GLuint index,
+                                const GLfloat *params)
 {
-}
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
 
-void
-_mesa_BindProgramARB(GLenum target, GLuint program)
-{
+   if (target == GL_FRAGMENT_PROGRAM_ARB
+       && ctx->Extensions.ARB_fragment_program) {
+      if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter4fv(index)");
+         return;
+      }
+      memcpy(ctx->FragmentProgram.Parameters[index], params,
+             4 * sizeof(GLfloat));
+   }
+   else if (target == GL_VERTEX_PROGRAM_ARB /* == GL_VERTEX_PROGRAM_NV */
+       && (ctx->Extensions.ARB_vertex_program || ctx->Extensions.NV_vertex_program)) {
+      if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter4fv(index)");
+         return;
+      }
+      memcpy(ctx->VertexProgram.Parameters[index], params,
+             4 * sizeof(GLfloat));
+   }
+   else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameter4fv(target)");
+      return;
+   }
 }
 
 
-void
-_mesa_DeleteProgramsARB(GLsizei n, const GLuint *programs)
+void GLAPIENTRY
+_mesa_ProgramEnvParameters4fvEXT(GLenum target, GLuint index, GLsizei count,
+                                const GLfloat *params)
 {
-}
+   GET_CURRENT_CONTEXT(ctx);
+   GLfloat * dest;
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
 
-void
-_mesa_GenProgramsARB(GLsizei n, GLuint *programs)
-{
-}
+   if (count <= 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(count)");
+   }
 
+   if (target == GL_FRAGMENT_PROGRAM_ARB
+       && ctx->Extensions.ARB_fragment_program) {
+      if ((index + count) > ctx->Const.FragmentProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(index + count)");
+         return;
+      }
+      dest = ctx->FragmentProgram.Parameters[index];
+   }
+   else if (target == GL_VERTEX_PROGRAM_ARB
+       && ctx->Extensions.ARB_vertex_program) {
+      if ((index + count) > ctx->Const.VertexProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(index + count)");
+         return;
+      }
+      dest = ctx->VertexProgram.Parameters[index];
+   }
+   else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameters4fv(target)");
+      return;
+   }
 
-void
-_mesa_ProgramEnvParameter4dARB(GLenum target, GLuint index,
-                               GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
+   memcpy(dest, params, count * 4 * sizeof(GLfloat));
 }
 
 
-void
-_mesa_ProgramEnvParameter4dvARB(GLenum target, GLuint index,
-                                   const GLdouble *params)
+void GLAPIENTRY
+_mesa_GetProgramEnvParameterdvARB(GLenum target, GLuint index,
+                                  GLdouble *params)
 {
+   GET_CURRENT_CONTEXT(ctx);
+   GLfloat fparams[4];
+
+   _mesa_GetProgramEnvParameterfvARB(target, index, fparams);
+   if (ctx->ErrorValue == GL_NO_ERROR) {
+      params[0] = fparams[0];
+      params[1] = fparams[1];
+      params[2] = fparams[2];
+      params[3] = fparams[3];
+   }
 }
 
 
-void
-_mesa_ProgramEnvParameter4fARB(GLenum target, GLuint index,
-                               GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+void GLAPIENTRY
+_mesa_GetProgramEnvParameterfvARB(GLenum target, GLuint index, 
+                                  GLfloat *params)
 {
-}
+   GET_CURRENT_CONTEXT(ctx);
 
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-void
-_mesa_ProgramEnvParameter4fvARB(GLenum target, GLuint index,
-                                   const GLfloat *params)
-{
+   if (target == GL_FRAGMENT_PROGRAM_ARB
+       && ctx->Extensions.ARB_fragment_program) {
+      if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramEnvParameter(index)");
+         return;
+      }
+      COPY_4V(params, ctx->FragmentProgram.Parameters[index]);
+   }
+   else if (target == GL_VERTEX_PROGRAM_ARB
+       && ctx->Extensions.ARB_vertex_program) {
+      if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramEnvParameter(index)");
+         return;
+      }
+      COPY_4V(params, ctx->VertexProgram.Parameters[index]);
+   }
+   else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramEnvParameter(target)");
+      return;
+   }
 }
 
 
-#if 111
-
-void
+/**
+ * Note, this function is also used by the GL_NV_fragment_program extension.
+ */
+void GLAPIENTRY
 _mesa_ProgramLocalParameter4fARB(GLenum target, GLuint index,
                                  GLfloat x, GLfloat y, GLfloat z, GLfloat w)
 {
    GET_CURRENT_CONTEXT(ctx);
+   struct gl_program *prog;
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (target == GL_FRAGMENT_PROGRAM_NV) {
-      struct fragment_program *fprog = ctx->FragmentProgram.Current;
-      if (!fprog) {
-         _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameterARB");
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
+
+   if ((target == GL_FRAGMENT_PROGRAM_NV
+        && ctx->Extensions.NV_fragment_program) ||
+       (target == GL_FRAGMENT_PROGRAM_ARB
+        && ctx->Extensions.ARB_fragment_program)) {
+      if (index >= ctx->Const.FragmentProgram.MaxLocalParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameterARB");
          return;
       }
-      if (index >= MAX_NV_FRAGMENT_PROGRAM_PARAMS) {
+      prog = &(ctx->FragmentProgram.Current->Base);
+   }
+   else if (target == GL_VERTEX_PROGRAM_ARB
+            && ctx->Extensions.ARB_vertex_program) {
+      if (index >= ctx->Const.VertexProgram.MaxLocalParams) {
          _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameterARB");
          return;
       }
-      fprog->Base.LocalParams[index][0] = x;
-      fprog->Base.LocalParams[index][1] = y;
-      fprog->Base.LocalParams[index][2] = z;
-      fprog->Base.LocalParams[index][3] = w;
+      prog = &(ctx->VertexProgram.Current->Base);
    }
    else {
       _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameterARB");
       return;
    }
+
+   ASSERT(index < MAX_PROGRAM_LOCAL_PARAMS);
+   prog->LocalParams[index][0] = x;
+   prog->LocalParams[index][1] = y;
+   prog->LocalParams[index][2] = z;
+   prog->LocalParams[index][3] = w;
 }
 
 
-void
+/**
+ * Note, this function is also used by the GL_NV_fragment_program extension.
+ */
+void GLAPIENTRY
 _mesa_ProgramLocalParameter4fvARB(GLenum target, GLuint index,
                                   const GLfloat *params)
 {
-   glProgramLocalParameter4fARB(target, index, params[0], params[1],
-                                params[2], params[3]);
+   _mesa_ProgramLocalParameter4fARB(target, index, params[0], params[1],
+                                    params[2], params[3]);
 }
 
 
-void
+void GLAPIENTRY
+_mesa_ProgramLocalParameters4fvEXT(GLenum target, GLuint index, GLsizei count,
+                                  const GLfloat *params)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   GLfloat *dest;
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
+
+   if (count <= 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fv(count)");
+   }
+
+   if (target == GL_FRAGMENT_PROGRAM_ARB
+       && ctx->Extensions.ARB_fragment_program) {
+      if ((index + count) > ctx->Const.FragmentProgram.MaxLocalParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
+         return;
+      }
+      dest = ctx->FragmentProgram.Current->Base.LocalParams[index];
+   }
+   else if (target == GL_VERTEX_PROGRAM_ARB
+            && ctx->Extensions.ARB_vertex_program) {
+      if ((index + count) > ctx->Const.VertexProgram.MaxLocalParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
+         return;
+      }
+      dest = ctx->VertexProgram.Current->Base.LocalParams[index];
+   }
+   else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameters4fvEXT(target)");
+      return;
+   }
+
+   memcpy(dest, params, count * 4 * sizeof(GLfloat));
+}
+
+
+/**
+ * Note, this function is also used by the GL_NV_fragment_program extension.
+ */
+void GLAPIENTRY
 _mesa_ProgramLocalParameter4dARB(GLenum target, GLuint index,
                                  GLdouble x, GLdouble y,
                                  GLdouble z, GLdouble w)
 {
-   glProgramLocalParameter4fARB(target, index, (GLfloat)x, (GLfloat)y, 
-                                              (GLfloat)z, (GLfloat)w);
+   _mesa_ProgramLocalParameter4fARB(target, index, (GLfloat) x, (GLfloat) y, 
+                                    (GLfloat) z, (GLfloat) w);
 }
 
 
-void
+/**
+ * Note, this function is also used by the GL_NV_fragment_program extension.
+ */
+void GLAPIENTRY
 _mesa_ProgramLocalParameter4dvARB(GLenum target, GLuint index,
                                   const GLdouble *params)
 {
-   glProgramLocalParameter4fARB(target, index, (GLfloat)params[0], 
-                               (GLfloat)params[1], (GLfloat)params[2],
-                               (GLfloat)params[3]);
+   _mesa_ProgramLocalParameter4fARB(target, index,
+                                    (GLfloat) params[0], (GLfloat) params[1],
+                                    (GLfloat) params[2], (GLfloat) params[3]);
 }
 
 
-void
+/**
+ * Note, this function is also used by the GL_NV_fragment_program extension.
+ */
+void GLAPIENTRY
 _mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index,
                                     GLfloat *params)
 {
-   struct program *prog;
+   const struct gl_program *prog;
    GLuint maxParams;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -389,14 +675,17 @@ _mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index,
    if (target == GL_VERTEX_PROGRAM_ARB
        && ctx->Extensions.ARB_vertex_program) {
       prog = &(ctx->VertexProgram.Current->Base);
-      maxParams = ctx->Const.MaxVertexProgramParams;
+      maxParams = ctx->Const.VertexProgram.MaxLocalParams;
+   }
+   else if (target == GL_FRAGMENT_PROGRAM_ARB
+            && ctx->Extensions.ARB_fragment_program) {
+      prog = &(ctx->FragmentProgram.Current->Base);
+      maxParams = ctx->Const.FragmentProgram.MaxLocalParams;
    }
-   else if ((target == GL_FRAGMENT_PROGRAM_ARB
-             && ctx->Extensions.ARB_fragment_program) ||
-            (target == GL_FRAGMENT_PROGRAM_NV
-             && ctx->Extensions.NV_fragment_program)) {
+   else if (target == GL_FRAGMENT_PROGRAM_NV
+            && ctx->Extensions.NV_fragment_program) {
       prog = &(ctx->FragmentProgram.Current->Base);
-      maxParams = ctx->Const.MaxFragmentProgramParams;
+      maxParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
    }
    else {
       _mesa_error(ctx, GL_INVALID_ENUM,
@@ -411,98 +700,46 @@ _mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index,
    }
 
    ASSERT(prog);
-   params[0] = prog->LocalParams[index][0];
-   params[1] = prog->LocalParams[index][1];
-   params[2] = prog->LocalParams[index][2];
-   params[3] = prog->LocalParams[index][3];
+   ASSERT(index < MAX_PROGRAM_LOCAL_PARAMS);
+   COPY_4V(params, prog->LocalParams[index]);
 }
 
 
-void
+/**
+ * Note, this function is also used by the GL_NV_fragment_program extension.
+ */
+void GLAPIENTRY
 _mesa_GetProgramLocalParameterdvARB(GLenum target, GLuint index,
                                     GLdouble *params)
 {
+   GET_CURRENT_CONTEXT(ctx);
    GLfloat floatParams[4];
-   glGetProgramLocalParameterfvARB(target, index, floatParams);
-   COPY_4V(params, floatParams);
-}
-
-#else
-
-
-void
-_mesa_ProgramLocalParameter4dARB(GLenum target, GLuint index,
-                                 GLdouble x, GLdouble y,
-                                 GLdouble z, GLdouble w)
-{
-}
-
-
-void
-_mesa_ProgramLocalParameter4dvARB(GLenum target, GLuint index,
-                                  const GLdouble *params)
-{
-}
-
-
-void
-_mesa_ProgramLocalParameter4fARB(GLenum target, GLuint index,
-                                 GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-}
-
-
-void
-_mesa_ProgramLocalParameter4fvARB(GLenum target, GLuint index,
-                                  const GLfloat *params)
-{
-}
-
-#endif
-
-void
-_mesa_GetProgramEnvParameterdvARB(GLenum target, GLuint index,
-                                  GLdouble *params)
-{
-}
-
-
-void
-_mesa_GetProgramEnvParameterfvARB(GLenum target, GLuint index, 
-                                  GLfloat *params)
-{
-}
-
-
-#if 000
-void
-_mesa_GetProgramLocalParameterdvARB(GLenum target, GLuint index,
-                                    GLdouble *params)
-{
-}
-
-
-void
-_mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index, 
-                                    GLfloat *params)
-{
+   ASSIGN_4V(floatParams, 0.0F, 0.0F, 0.0F, 0.0F);
+   _mesa_GetProgramLocalParameterfvARB(target, index, floatParams);
+   if (ctx->ErrorValue == GL_NO_ERROR) {
+      COPY_4V(params, floatParams);
+   }
 }
-#endif
-
 
 
-void
+void GLAPIENTRY
 _mesa_GetProgramivARB(GLenum target, GLenum pname, GLint *params)
 {
-   struct program *prog;
+   const struct gl_program_constants *limits;
+   struct gl_program *prog;
    GET_CURRENT_CONTEXT(ctx);
+
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (target == GL_VERTEX_PROGRAM_ARB) {
+   if (target == GL_VERTEX_PROGRAM_ARB
+       && ctx->Extensions.ARB_vertex_program) {
       prog = &(ctx->VertexProgram.Current->Base);
+      limits = &ctx->Const.VertexProgram;
    }
-   else if (target == GL_FRAGMENT_PROGRAM_ARB) {
+   else if (target == GL_FRAGMENT_PROGRAM_ARB
+            && ctx->Extensions.ARB_fragment_program) {
       prog = &(ctx->FragmentProgram.Current->Base);
+      limits = &ctx->Const.FragmentProgram;
    }
    else {
       _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(target)");
@@ -510,55 +747,172 @@ _mesa_GetProgramivARB(GLenum target, GLenum pname, GLint *params)
    }
 
    ASSERT(prog);
+   ASSERT(limits);
 
+   /* Queries supported for both vertex and fragment programs */
    switch (pname) {
       case GL_PROGRAM_LENGTH_ARB:
-         *params = prog->String ? _mesa_strlen((char *) prog->String) : 0;
-         break;
+         *params
+            = prog->String ? (GLint) strlen((char *) prog->String) : 0;
+         return;
       case GL_PROGRAM_FORMAT_ARB:
          *params = prog->Format;
-         break;
+         return;
       case GL_PROGRAM_BINDING_ARB:
          *params = prog->Id;
-         break;
+         return;
       case GL_PROGRAM_INSTRUCTIONS_ARB:
          *params = prog->NumInstructions;
-         break;
+         return;
       case GL_MAX_PROGRAM_INSTRUCTIONS_ARB:
+         *params = limits->MaxInstructions;
+         return;
       case GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB:
+         *params = prog->NumNativeInstructions;
+         return;
       case GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB:
+         *params = limits->MaxNativeInstructions;
+         return;
       case GL_PROGRAM_TEMPORARIES_ARB:
+         *params = prog->NumTemporaries;
+         return;
       case GL_MAX_PROGRAM_TEMPORARIES_ARB:
+         *params = limits->MaxTemps;
+         return;
       case GL_PROGRAM_NATIVE_TEMPORARIES_ARB:
+         *params = prog->NumNativeTemporaries;
+         return;
       case GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB:
+         *params = limits->MaxNativeTemps;
+         return;
       case GL_PROGRAM_PARAMETERS_ARB:
+         *params = prog->NumParameters;
+         return;
       case GL_MAX_PROGRAM_PARAMETERS_ARB:
+         *params = limits->MaxParameters;
+         return;
       case GL_PROGRAM_NATIVE_PARAMETERS_ARB:
+         *params = prog->NumNativeParameters;
+         return;
       case GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB:
+         *params = limits->MaxNativeParameters;
+         return;
       case GL_PROGRAM_ATTRIBS_ARB:
+         *params = prog->NumAttributes;
+         return;
       case GL_MAX_PROGRAM_ATTRIBS_ARB:
+         *params = limits->MaxAttribs;
+         return;
       case GL_PROGRAM_NATIVE_ATTRIBS_ARB:
+         *params = prog->NumNativeAttributes;
+         return;
       case GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB:
+         *params = limits->MaxNativeAttribs;
+         return;
       case GL_PROGRAM_ADDRESS_REGISTERS_ARB:
+         *params = prog->NumAddressRegs;
+         return;
       case GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB:
+         *params = limits->MaxAddressRegs;
+         return;
       case GL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB:
+         *params = prog->NumNativeAddressRegs;
+         return;
       case GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB:
+         *params = limits->MaxNativeAddressRegs;
+         return;
       case GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB:
+         *params = limits->MaxLocalParams;
+         return;
       case GL_MAX_PROGRAM_ENV_PARAMETERS_ARB:
+         *params = limits->MaxEnvParams;
+         return;
       case GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB:
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
+         /*
+          * XXX we may not really need a driver callback here.
+          * If the number of native instructions, registers, etc. used
+          * are all below the maximums, we could return true.
+          * The spec says that even if this query returns true, there's
+          * no guarantee that the program will run in hardware.
+          */
+         if (prog->Id == 0) {
+            /* default/null program */
+            *params = GL_FALSE;
+         }
+        else if (ctx->Driver.IsProgramNative) {
+            /* ask the driver */
+           *params = ctx->Driver.IsProgramNative( ctx, target, prog );
+         }
+        else {
+            /* probably running in software */
+           *params = GL_TRUE;
+         }
          return;
+      default:
+         /* continue with fragment-program only queries below */
+         break;
+   }
+
+   /*
+    * The following apply to fragment programs only (at this time)
+    */
+   if (target == GL_FRAGMENT_PROGRAM_ARB) {
+      const struct gl_fragment_program *fp = ctx->FragmentProgram.Current;
+      switch (pname) {
+         case GL_PROGRAM_ALU_INSTRUCTIONS_ARB:
+            *params = fp->Base.NumNativeAluInstructions;
+            return;
+         case GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
+            *params = fp->Base.NumAluInstructions;
+            return;
+         case GL_PROGRAM_TEX_INSTRUCTIONS_ARB:
+            *params = fp->Base.NumTexInstructions;
+            return;
+         case GL_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
+            *params = fp->Base.NumNativeTexInstructions;
+            return;
+         case GL_PROGRAM_TEX_INDIRECTIONS_ARB:
+            *params = fp->Base.NumTexIndirections;
+            return;
+         case GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
+            *params = fp->Base.NumNativeTexIndirections;
+            return;
+         case GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB:
+            *params = limits->MaxAluInstructions;
+            return;
+         case GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
+            *params = limits->MaxNativeAluInstructions;
+            return;
+         case GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB:
+            *params = limits->MaxTexInstructions;
+            return;
+         case GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
+            *params = limits->MaxNativeTexInstructions;
+            return;
+         case GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB:
+            *params = limits->MaxTexIndirections;
+            return;
+         case GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
+            *params = limits->MaxNativeTexIndirections;
+            return;
+         default:
+            _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
+            return;
+      }
+   } else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
+      return;
    }
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetProgramStringARB(GLenum target, GLenum pname, GLvoid *string)
 {
-   struct program *prog;
+   const struct gl_program *prog;
+   char *dst = (char *) string;
    GET_CURRENT_CONTEXT(ctx);
+
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (target == GL_VERTEX_PROGRAM_ARB) {
@@ -579,23 +933,8 @@ _mesa_GetProgramStringARB(GLenum target, GLenum pname, GLvoid *string)
       return;
    }
 
-   MEMCPY(string, prog->String, _mesa_strlen((char *) prog->String));
-}
-
-
-GLboolean
-_mesa_IsProgramARB(GLuint program)
-{
-   struct program *prog;
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
-
-   if (program == 0)
-      return GL_FALSE;
-
-   prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, program);
-   if (prog && prog->Target)
-      return GL_TRUE;
+   if (prog->String)
+      memcpy(dst, prog->String, strlen((char *) prog->String));
    else
-      return GL_FALSE;
+      *dst = '\0';
 }