Minor tweaks to help out at a driver level.
[mesa.git] / src / mesa / main / arbprogram.c
index 99118cf60d569d05d1544b41f7d74fe3cc396eb3..ab27e086770ab781334941d7d9d81af2056a31bd 100644 (file)
 
 /**
  * \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 "arbprogram.h"
+#include "arbfragparse.h"
+#include "arbvertparse.h"
 #include "context.h"
-#include "hash.h"
 #include "imports.h"
 #include "macros.h"
 #include "mtypes.h"
 #include "nvprogram.h"
+#include "nvfragparse.h"
 #include "nvfragprog.h"
+#include "nvvertparse.h"
 #include "nvvertprog.h"
 
 
-/* XXX temporary */
-static void
-_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
-                               const GLubyte *string, GLsizei len,
-                               struct vertex_program *prog)
-{
-}
-
-
-static void
-_mesa_parse_arb_fragment_program(GLcontext *ctx, GLenum target,
-                                 const GLubyte *string, GLsizei len,
-                                 struct fragment_program *prog)
-{
-}
-
-
-
-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)
-{
-}
-
-void
-_mesa_VertexAttrib2fARB(GLuint index, GLfloat x, GLfloat y)
-{
-}
-
-void
-_mesa_VertexAttrib2dARB(GLuint index, GLdouble x, GLdouble y)
-{
-}
-
-void
-_mesa_VertexAttrib3sARB(GLuint index, GLshort x, GLshort y, GLshort z)
-{
-}
-
-void
-_mesa_VertexAttrib3fARB(GLuint index, GLfloat x, GLfloat y, GLfloat z)
-{
-}
-
-void
-_mesa_VertexAttrib3dARB(GLuint index, GLdouble x, GLdouble y, GLdouble z)
-{
-}
-
-void
-_mesa_VertexAttrib4sARB(GLuint index, GLshort x, GLshort y, GLshort z, GLshort w)
-{
-}
-
-void
-_mesa_VertexAttrib4fARB(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-}
-
-void
-_mesa_VertexAttrib4dARB(GLuint index, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-}
-
-void
-_mesa_VertexAttrib4NubARB(GLuint index, GLubyte x, GLubyte y, GLubyte z, GLubyte w)
-{
-}
-
-void
-_mesa_VertexAttrib1svARB(GLuint index, const GLshort *v)
-{
-}
-
-void
-_mesa_VertexAttrib1fvARB(GLuint index, const GLfloat *v)
-{
-}
-
-void
-_mesa_VertexAttrib1dvARB(GLuint index, const GLdouble *v)
-{
-}
-
-void
-_mesa_VertexAttrib2svARB(GLuint index, const GLshort *v)
-{
-}
-
-void
-_mesa_VertexAttrib2fvARB(GLuint index, const GLfloat *v)
-{
-}
-
-void
-_mesa_VertexAttrib2dvARB(GLuint index, const GLdouble *v)
-{
-}
-
-void
-_mesa_VertexAttrib3svARB(GLuint index, const GLshort *v)
-{
-}
-
-void
-_mesa_VertexAttrib3fvARB(GLuint index, const GLfloat *v)
-{
-}
-
-void
-_mesa_VertexAttrib3dvARB(GLuint index, const GLdouble *v)
-{
-}
-
-void
-_mesa_VertexAttrib4bvARB(GLuint index, const GLbyte *v)
-{
-}
-
-void
-_mesa_VertexAttrib4svARB(GLuint index, const GLshort *v)
-{
-}
-
-void
-_mesa_VertexAttrib4ivARB(GLuint index, const GLint *v)
-{
-}
-
-void
-_mesa_VertexAttrib4ubvARB(GLuint index, const GLubyte *v)
-{
-}
-
-void
-_mesa_VertexAttrib4usvARB(GLuint index, const GLushort *v)
-{
-}
-
-void
-_mesa_VertexAttrib4uivARB(GLuint index, const GLuint *v)
-{
-}
-
-void
-_mesa_VertexAttrib4fvARB(GLuint index, const GLfloat *v)
-{
-}
-
-void
-_mesa_VertexAttrib4dvARB(GLuint index, const GLdouble *v)
-{
-}
-
-void
-_mesa_VertexAttrib4NbvARB(GLuint index, const GLbyte *v)
-{
-}
-
-void
-_mesa_VertexAttrib4NsvARB(GLuint index, const GLshort *v)
-{
-}
-
-void
-_mesa_VertexAttrib4NivARB(GLuint index, const GLint *v)
-{
-}
-
-void
-_mesa_VertexAttrib4NubvARB(GLuint index, const GLubyte *v)
-{
-}
-
-void
-_mesa_VertexAttrib4NusvARB(GLuint index, const GLushort *v)
-{
-}
-
-void
-_mesa_VertexAttrib4NuivARB(GLuint index, const GLuint *v)
-{
-}
-
-
-void
-_mesa_VertexAttribPointerARB(GLuint index, GLint size, GLenum type,
-                             GLboolean normalized, GLsizei stride,
-                             const GLvoid *pointer)
-{
-}
-
-
-void
+void GLAPIENTRY
 _mesa_EnableVertexAttribArrayARB(GLuint index)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -260,13 +56,14 @@ _mesa_EnableVertexAttribArrayARB(GLuint index)
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_ARRAY);
    ctx->Array.VertexAttrib[index].Enabled = GL_TRUE;
    ctx->Array._Enabled |= _NEW_ARRAY_ATTRIB(index);
    ctx->Array.NewState |= _NEW_ARRAY_ATTRIB(index);
 }
 
 
-void
+void GLAPIENTRY
 _mesa_DisableVertexAttribArrayARB(GLuint index)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -278,13 +75,14 @@ _mesa_DisableVertexAttribArrayARB(GLuint index)
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_ARRAY);
    ctx->Array.VertexAttrib[index].Enabled = GL_FALSE;
    ctx->Array._Enabled &= ~_NEW_ARRAY_ATTRIB(index);
-   ctx->Array.NewState &= ~_NEW_ARRAY_ATTRIB(index);
+   ctx->Array.NewState |= _NEW_ARRAY_ATTRIB(index);
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetVertexAttribdvARB(GLuint index, GLenum pname, GLdouble *params)
 {
    GLfloat fparams[4];
@@ -303,7 +101,7 @@ _mesa_GetVertexAttribdvARB(GLuint index, GLenum pname, GLdouble *params)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -316,23 +114,30 @@ _mesa_GetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
 
    switch (pname) {
       case GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB:
-         params[0] = ctx->Array.VertexAttrib[index].Enabled;
+         params[0] = (GLfloat) ctx->Array.VertexAttrib[index].Enabled;
          break;
       case GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB:
-         params[0] = ctx->Array.VertexAttrib[index].Size;
+         params[0] = (GLfloat) ctx->Array.VertexAttrib[index].Size;
          break;
       case GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB:
-         params[0] = ctx->Array.VertexAttrib[index].Stride;
+         params[0] = (GLfloat) ctx->Array.VertexAttrib[index].Stride;
          break;
       case GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB:
-         params[0] = ctx->Array.VertexAttrib[index].Type;
+         params[0] = (GLfloat) ctx->Array.VertexAttrib[index].Type;
          break;
       case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB:
          params[0] = ctx->Array.VertexAttrib[index].Normalized;
          break;
       case GL_CURRENT_VERTEX_ATTRIB_ARB:
+        FLUSH_CURRENT(ctx, 0);
          COPY_4V(params, ctx->Current.Attrib[index]);
          break;
+      case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB:
+         if (!ctx->Extensions.ARB_vertex_buffer_object) {
+            _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexAttribfvARB(pname)");
+            return;
+         }
+         params[0] = (GLfloat) ctx->Array.VertexAttrib[index].BufferObj->Name;
       default:
          _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexAttribfvARB(pname)");
          return;
@@ -340,7 +145,7 @@ _mesa_GetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetVertexAttribivARB(GLuint index, GLenum pname, GLint *params)
 {
    GLfloat fparams[4];
@@ -350,16 +155,16 @@ _mesa_GetVertexAttribivARB(GLuint index, GLenum pname, GLint *params)
    _mesa_GetVertexAttribfvARB(index, pname, fparams);
    if (ctx->ErrorValue == GL_NO_ERROR) {
       if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
-         COPY_4V(params, fparams);  /* float to int */
+         COPY_4V_CAST(params, fparams, GLint);  /* float to int */
       }
       else {
-         params[0] = fparams[0];
+         params[0] = (GLint) fparams[0];
       }
    }
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetVertexAttribPointervARB(GLuint index, GLenum pname, GLvoid **pointer)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -375,17 +180,19 @@ _mesa_GetVertexAttribPointervARB(GLuint index, GLenum pname, GLvoid **pointer)
       return;
    }
 
-   *pointer = ctx->Array.VertexAttrib[index].Ptr;;
+   *pointer = (GLvoid *) ctx->Array.VertexAttrib[index].Ptr;;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ProgramStringARB(GLenum target, GLenum format, GLsizei len,
                        const GLvoid *string)
 {
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM);
+
    if (target == GL_VERTEX_PROGRAM_ARB
        && ctx->Extensions.ARB_vertex_program) {
       struct vertex_program *prog = ctx->VertexProgram.Current;
@@ -393,7 +200,8 @@ _mesa_ProgramStringARB(GLenum target, GLenum format, GLsizei len,
          _mesa_error(ctx, GL_INVALID_ENUM, "glProgramStringARB(format)");
          return;
       }
-      _mesa_parse_arb_vertex_program(ctx, target, string, len, prog);
+      _mesa_parse_arb_vertex_program(ctx, target, (const GLubyte *) string,
+                                     len, prog);
    }
    else if (target == GL_FRAGMENT_PROGRAM_ARB
             && ctx->Extensions.ARB_fragment_program) {
@@ -402,7 +210,8 @@ _mesa_ProgramStringARB(GLenum target, GLenum format, GLsizei len,
          _mesa_error(ctx, GL_INVALID_ENUM, "glProgramStringARB(format)");
          return;
       }
-      _mesa_parse_arb_fragment_program(ctx, target, string, len, prog);
+      _mesa_parse_arb_fragment_program(ctx, target, (const GLubyte *) string,
+                                       len, prog);
    }
    else {
       _mesa_error(ctx, GL_INVALID_ENUM, "glProgramStringARB(target)");
@@ -410,133 +219,41 @@ _mesa_ProgramStringARB(GLenum target, GLenum format, GLsizei len,
 }
 
 
-void
-_mesa_BindProgramARB(GLenum target, GLuint program)
-{
-   struct program *prog;
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
-
-   if (target == GL_VERTEX_PROGRAM_ARB
-       && ctx->Extensions.ARB_vertex_program) {
-      if (ctx->VertexProgram.Current &&
-          ctx->VertexProgram.Current->Base.Id == program)
-         return;
-      /* decrement refcount on previously bound vertex program */
-      if (ctx->VertexProgram.Current) {
-         ctx->VertexProgram.Current->Base.RefCount--;
-         /* and delete if refcount goes below one */
-         if (ctx->VertexProgram.Current->Base.RefCount <= 0) {
-            _mesa_delete_program(ctx, &(ctx->VertexProgram.Current->Base));
-            _mesa_HashRemove(ctx->Shared->Programs, program);
-         }
-      }
-   }
-   else if (target == GL_FRAGMENT_PROGRAM_ARB
-            && ctx->Extensions.ARB_fragment_program) {
-      if (ctx->FragmentProgram.Current &&
-          ctx->FragmentProgram.Current->Base.Id == program)
-         return;
-      /* decrement refcount on previously bound fragment program */
-      if (ctx->FragmentProgram.Current) {
-         ctx->FragmentProgram.Current->Base.RefCount--;
-         /* and delete if refcount goes below one */
-         if (ctx->FragmentProgram.Current->Base.RefCount <= 0) {
-            _mesa_delete_program(ctx, &(ctx->FragmentProgram.Current->Base));
-            _mesa_HashRemove(ctx->Shared->Programs, program);
-         }
-      }
-   }
-   else {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramARB(target)");
-      return;
-   }
-
-   /* NOTE: binding to a non-existant program is not an error.
-    * That's supposed to be caught in glBegin.
-    * NOTE: program number 0 is legal here.
-    */
-   if (program == 0) {
-      /* default program */
-      if (target == GL_VERTEX_PROGRAM_ARB)
-         prog = ctx->Shared->DefaultVertexProgram;
-      else
-         prog = ctx->Shared->DefaultFragmentProgram;
-   }
-   else {
-      prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, program);
-      if (prog) {
-         if (prog->Target == 0) {
-            /* prog was allocated with glGenProgramsARB */
-            prog->Target = target;
-         }
-         else if (prog->Target != target) {
-            _mesa_error(ctx, GL_INVALID_OPERATION,
-                        "glBindProgramARB(target mismatch)");
-            return;
-         }
-      }
-      else {
-         /* allocate a new program now */
-         prog = _mesa_alloc_program(ctx, target, program);
-         if (!prog) {
-            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramARB");
-            return;
-         }
-         prog->Id = program;
-         prog->Target = target;
-         prog->Resident = GL_TRUE;
-         prog->RefCount = 1;
-         _mesa_HashInsert(ctx->Shared->Programs, program, prog);
-      }
-   }
-
-   /* bind now */
-   if (target == GL_VERTEX_PROGRAM_ARB) {
-      ctx->VertexProgram.Current = (struct vertex_program *) prog;
-   }
-   else {
-      ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
-      ctx->FragmentProgram.Current = (struct fragment_program *) prog;
-   }
-
-   if (prog)
-      prog->RefCount++;
-}
-
-
-void
+void GLAPIENTRY
 _mesa_ProgramEnvParameter4dARB(GLenum target, GLuint index,
                                GLdouble x, GLdouble y, GLdouble z, GLdouble w)
 {
-   _mesa_ProgramEnvParameter4fARB(target, index, x, y, z, w);
+   _mesa_ProgramEnvParameter4fARB(target, index, (GLfloat) x, (GLfloat) y, 
+                                 (GLfloat) z, (GLfloat) w);
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ProgramEnvParameter4dvARB(GLenum target, GLuint index,
                                 const GLdouble *params)
 {
-   _mesa_ProgramEnvParameter4fARB(target, index, params[0], params[1],
-                                  params[2], params[3]);
+   _mesa_ProgramEnvParameter4fARB(target, index, (GLfloat) params[0], 
+                                 (GLfloat) params[1], (GLfloat) params[2], 
+                                 (GLfloat) params[3]);
 }
 
 
-void
+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);
+
    if (target == GL_FRAGMENT_PROGRAM_ARB
        && ctx->Extensions.ARB_fragment_program) {
       if (index >= ctx->Const.MaxFragmentProgramEnvParams) {
          _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter(index)");
          return;
       }
-      index += FP_PROG_REG_START;
-      ASSIGN_4V(ctx->FragmentProgram.Machine.Registers[index], x, y, z, w);
+      ASSIGN_4V(ctx->FragmentProgram.Parameters[index], x, y, z, w);
    }
    if (target == GL_VERTEX_PROGRAM_ARB
        && ctx->Extensions.ARB_vertex_program) {
@@ -544,8 +261,7 @@ _mesa_ProgramEnvParameter4fARB(GLenum target, GLuint index,
          _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter(index)");
          return;
       }
-      index += VP_PROG_REG_START;
-      ASSIGN_4V(ctx->VertexProgram.Machine.Registers[index], x, y, z, w);
+      ASSIGN_4V(ctx->VertexProgram.Parameters[index], x, y, z, w);
    }
    else {
       _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameter(target)");
@@ -554,7 +270,7 @@ _mesa_ProgramEnvParameter4fARB(GLenum target, GLuint index,
 }
 
 
-void
+void GLAPIENTRY
 _mesa_ProgramEnvParameter4fvARB(GLenum target, GLuint index,
                                    const GLfloat *params)
 {
@@ -563,7 +279,7 @@ _mesa_ProgramEnvParameter4fvARB(GLenum target, GLuint index,
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetProgramEnvParameterdvARB(GLenum target, GLuint index,
                                   GLdouble *params)
 {
@@ -580,12 +296,16 @@ _mesa_GetProgramEnvParameterdvARB(GLenum target, GLuint index,
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetProgramEnvParameterfvARB(GLenum target, GLuint index, 
                                   GLfloat *params)
 {
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM);
+
+   if (!ctx->_CurrentProgram)
+      ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (target == GL_FRAGMENT_PROGRAM_ARB
        && ctx->Extensions.ARB_fragment_program) {
@@ -593,8 +313,7 @@ _mesa_GetProgramEnvParameterfvARB(GLenum target, GLuint index,
          _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramEnvParameter(index)");
          return;
       }
-      index += FP_PROG_REG_START;
-      COPY_4V(params, ctx->FragmentProgram.Machine.Registers[index]);
+      COPY_4V(params, ctx->FragmentProgram.Parameters[index]);
    }
    if (target == GL_VERTEX_PROGRAM_ARB
        && ctx->Extensions.ARB_vertex_program) {
@@ -602,8 +321,7 @@ _mesa_GetProgramEnvParameterfvARB(GLenum target, GLuint index,
          _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramEnvParameter(index)");
          return;
       }
-      index += VP_PROG_REG_START;
-      COPY_4V(params, ctx->VertexProgram.Machine.Registers[index]);
+      COPY_4V(params, ctx->VertexProgram.Parameters[index]);
    }
    else {
       _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramEnvParameter(target)");
@@ -615,7 +333,7 @@ _mesa_GetProgramEnvParameterfvARB(GLenum target, GLuint index,
 /**
  * Note, this function is also used by the GL_NV_fragment_program extension.
  */
-void
+void GLAPIENTRY
 _mesa_ProgramLocalParameter4fARB(GLenum target, GLuint index,
                                  GLfloat x, GLfloat y, GLfloat z, GLfloat w)
 {
@@ -623,6 +341,8 @@ _mesa_ProgramLocalParameter4fARB(GLenum target, GLuint index,
    struct program *prog;
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM);
+
    if ((target == GL_FRAGMENT_PROGRAM_NV
         && ctx->Extensions.NV_fragment_program) ||
        (target == GL_FRAGMENT_PROGRAM_ARB
@@ -657,7 +377,7 @@ _mesa_ProgramLocalParameter4fARB(GLenum target, GLuint index,
 /**
  * Note, this function is also used by the GL_NV_fragment_program extension.
  */
-void
+void GLAPIENTRY
 _mesa_ProgramLocalParameter4fvARB(GLenum target, GLuint index,
                                   const GLfloat *params)
 {
@@ -669,7 +389,7 @@ _mesa_ProgramLocalParameter4fvARB(GLenum target, GLuint index,
 /**
  * Note, this function is also used by the GL_NV_fragment_program extension.
  */
-void
+void GLAPIENTRY
 _mesa_ProgramLocalParameter4dARB(GLenum target, GLuint index,
                                  GLdouble x, GLdouble y,
                                  GLdouble z, GLdouble w)
@@ -682,7 +402,7 @@ _mesa_ProgramLocalParameter4dARB(GLenum target, GLuint index,
 /**
  * Note, this function is also used by the GL_NV_fragment_program extension.
  */
-void
+void GLAPIENTRY
 _mesa_ProgramLocalParameter4dvARB(GLenum target, GLuint index,
                                   const GLdouble *params)
 {
@@ -695,7 +415,7 @@ _mesa_ProgramLocalParameter4dvARB(GLenum target, GLuint index,
 /**
  * Note, this function is also used by the GL_NV_fragment_program extension.
  */
-void
+void GLAPIENTRY
 _mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index,
                                     GLfloat *params)
 {
@@ -740,7 +460,7 @@ _mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index,
 /**
  * Note, this function is also used by the GL_NV_fragment_program extension.
  */
-void
+void GLAPIENTRY
 _mesa_GetProgramLocalParameterdvARB(GLenum target, GLuint index,
                                     GLdouble *params)
 {
@@ -753,19 +473,21 @@ _mesa_GetProgramLocalParameterdvARB(GLenum target, GLuint index,
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetProgramivARB(GLenum target, GLenum pname, GLint *params)
 {
    struct program *prog;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   if (!ctx->_CurrentProgram)
+      ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (target == GL_VERTEX_PROGRAM_ARB
        && ctx->Extensions.ARB_vertex_program) {
       prog = &(ctx->VertexProgram.Current->Base);
    }
    else if (target == GL_FRAGMENT_PROGRAM_ARB
-            && ctx->Extensions.ARB_vertex_program) {
+            && ctx->Extensions.ARB_fragment_program) {
       prog = &(ctx->FragmentProgram.Current->Base);
    }
    else {
@@ -958,12 +680,14 @@ _mesa_GetProgramivARB(GLenum target, GLenum pname, GLint *params)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetProgramStringARB(GLenum target, GLenum pname, GLvoid *string)
 {
    struct program *prog;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   if (!ctx->_CurrentProgram)
+      ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (target == GL_VERTEX_PROGRAM_ARB) {
       prog = &(ctx->VertexProgram.Current->Base);
@@ -985,3 +709,4 @@ _mesa_GetProgramStringARB(GLenum target, GLenum pname, GLvoid *string)
 
    MEMCPY(string, prog->String, _mesa_strlen((char *) prog->String));
 }
+