Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / mesa / main / enable.c
index 248df1badca3b43a4768dd24051c4982e27f9579..72ed50808c58b3381618a11feb2cbf5071dd5d7e 100644 (file)
@@ -201,6 +201,26 @@ _mesa_DisableClientState( GLenum cap )
    }
 
 
+
+/**
+ * Return pointer to current texture unit for setting/getting coordinate
+ * state.
+ * Note that we'll set GL_INVALID_OPERATION if the active texture unit is
+ * higher than the number of supported coordinate units.  And we'll return NULL.
+ */
+static struct gl_texture_unit *
+get_texcoord_unit(GLcontext *ctx)
+{
+   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glEnable/Disable(texcoord unit)");
+      return NULL;
+   }
+   else {
+      return &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+   }
+}
+
+
 /**
  * Helper function to enable or disable a texture target.
  */
@@ -612,54 +632,62 @@ _mesa_set_enable(GLcontext *ctx, GLenum cap, GLboolean state)
             return;
          }
          break;
-      case GL_TEXTURE_GEN_Q: {
-         GLuint unit = ctx->Texture.CurrentUnit;
-         struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-         GLuint newenabled = texUnit->TexGenEnabled & ~Q_BIT;
-         if (state)
-            newenabled |= Q_BIT;
-         if (texUnit->TexGenEnabled == newenabled)
-            return;
-         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-         texUnit->TexGenEnabled = newenabled;
+      case GL_TEXTURE_GEN_Q:
+         {
+            struct gl_texture_unit *texUnit = get_texcoord_unit(ctx);
+            if (texUnit) {
+               GLuint newenabled = texUnit->TexGenEnabled & ~Q_BIT;
+               if (state)
+                  newenabled |= Q_BIT;
+               if (texUnit->TexGenEnabled == newenabled)
+                  return;
+               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
+               texUnit->TexGenEnabled = newenabled;
+            }
+         }
          break;
-      }
-      case GL_TEXTURE_GEN_R: {
-         GLuint unit = ctx->Texture.CurrentUnit;
-         struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-         GLuint newenabled = texUnit->TexGenEnabled & ~R_BIT;
-         if (state)
-            newenabled |= R_BIT;
-         if (texUnit->TexGenEnabled == newenabled)
-            return;
-         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-         texUnit->TexGenEnabled = newenabled;
+      case GL_TEXTURE_GEN_R:
+         {
+            struct gl_texture_unit *texUnit = get_texcoord_unit(ctx);
+            if (texUnit) {
+               GLuint newenabled = texUnit->TexGenEnabled & ~R_BIT;
+               if (state)
+                  newenabled |= R_BIT;
+               if (texUnit->TexGenEnabled == newenabled)
+                  return;
+               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
+               texUnit->TexGenEnabled = newenabled;
+            }
+         }
          break;
-      }
-      case GL_TEXTURE_GEN_S: {
-         GLuint unit = ctx->Texture.CurrentUnit;
-         struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-         GLuint newenabled = texUnit->TexGenEnabled & ~S_BIT;
-         if (state)
-            newenabled |= S_BIT;
-         if (texUnit->TexGenEnabled == newenabled)
-            return;
-         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-         texUnit->TexGenEnabled = newenabled;
+      case GL_TEXTURE_GEN_S:
+         {
+            struct gl_texture_unit *texUnit = get_texcoord_unit(ctx);
+            if (texUnit) {
+               GLuint newenabled = texUnit->TexGenEnabled & ~S_BIT;
+               if (state)
+                  newenabled |= S_BIT;
+               if (texUnit->TexGenEnabled == newenabled)
+                  return;
+               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
+               texUnit->TexGenEnabled = newenabled;
+            }
+         }
          break;
-      }
-      case GL_TEXTURE_GEN_T: {
-         GLuint unit = ctx->Texture.CurrentUnit;
-         struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-         GLuint newenabled = texUnit->TexGenEnabled & ~T_BIT;
-         if (state)
-            newenabled |= T_BIT;
-         if (texUnit->TexGenEnabled == newenabled)
-            return;
-         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-         texUnit->TexGenEnabled = newenabled;
+      case GL_TEXTURE_GEN_T:
+         {
+            struct gl_texture_unit *texUnit = get_texcoord_unit(ctx);
+            if (texUnit) {
+               GLuint newenabled = texUnit->TexGenEnabled & ~T_BIT;
+               if (state)
+                  newenabled |= T_BIT;
+               if (texUnit->TexGenEnabled == newenabled)
+                  return;
+               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
+               texUnit->TexGenEnabled = newenabled;
+            }
+         }
          break;
-      }
 
       /*
        * CLIENT STATE!!!
@@ -1155,28 +1183,36 @@ _mesa_IsEnabled( GLenum cap )
          return is_texture_enabled(ctx, TEXTURE_3D_BIT);
       case GL_TEXTURE_GEN_Q:
          {
-            const struct gl_texture_unit *texUnit;
-            texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-            return (texUnit->TexGenEnabled & Q_BIT) ? GL_TRUE : GL_FALSE;
+            const struct gl_texture_unit *texUnit = get_texcoord_unit(ctx);
+            if (texUnit) {
+               return (texUnit->TexGenEnabled & Q_BIT) ? GL_TRUE : GL_FALSE;
+            }
          }
+         return GL_FALSE;
       case GL_TEXTURE_GEN_R:
          {
-            const struct gl_texture_unit *texUnit;
-            texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-            return (texUnit->TexGenEnabled & R_BIT) ? GL_TRUE : GL_FALSE;
+            const struct gl_texture_unit *texUnit = get_texcoord_unit(ctx);
+            if (texUnit) {
+               return (texUnit->TexGenEnabled & R_BIT) ? GL_TRUE : GL_FALSE;
+            }
          }
+         return GL_FALSE;
       case GL_TEXTURE_GEN_S:
          {
-            const struct gl_texture_unit *texUnit;
-            texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-            return (texUnit->TexGenEnabled & S_BIT) ? GL_TRUE : GL_FALSE;
+            const struct gl_texture_unit *texUnit = get_texcoord_unit(ctx);
+            if (texUnit) {
+               return (texUnit->TexGenEnabled & S_BIT) ? GL_TRUE : GL_FALSE;
+            }
          }
+         return GL_FALSE;
       case GL_TEXTURE_GEN_T:
          {
-            const struct gl_texture_unit *texUnit;
-            texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-            return (texUnit->TexGenEnabled & T_BIT) ? GL_TRUE : GL_FALSE;
+            const struct gl_texture_unit *texUnit = get_texcoord_unit(ctx);
+            if (texUnit) {
+               return (texUnit->TexGenEnabled & T_BIT) ? GL_TRUE : GL_FALSE;
+            }
          }
+         return GL_FALSE;
 
       /*
        * CLIENT STATE!!!