mesa: Add PrimitiveRestartFixedIndex to gl_constants
[mesa.git] / src / mesa / main / extensions.c
index 7483b8f24967c9ea7a2651638c24137dfd831a11..6ded1bb426983fc1262405809f6f2c07d31d8ff1 100644 (file)
@@ -31,7 +31,7 @@
 
 
 #include "glheader.h"
-#include "imports.h"
+
 #include "context.h"
 #include "extensions.h"
 #include "macros.h"
 
 struct gl_extensions _mesa_extension_override_enables;
 struct gl_extensions _mesa_extension_override_disables;
-static char *extra_extensions = NULL;
 
+#define MAX_UNRECOGNIZED_EXTENSIONS 16
+static struct {
+   char *env;
+   const char *names[MAX_UNRECOGNIZED_EXTENSIONS];
+} unrecognized_extensions;
 
 /**
  * Given a member \c x of struct gl_extensions, return offset of
@@ -48,6 +52,13 @@ static char *extra_extensions = NULL;
  */
 #define o(x) offsetof(struct gl_extensions, x)
 
+static int
+extension_name_compare(const void *name, const void *elem)
+{
+   const struct mesa_extension *entry = elem;
+   return strcmp(name, entry->name);
+}
+
 /**
  * Given an extension name, lookup up the corresponding member of struct
  * gl_extensions and return that member's index.  If the name is
@@ -59,15 +70,18 @@ static char *extra_extensions = NULL;
 static int
 name_to_index(const char* name)
 {
-   unsigned i;
+   const struct mesa_extension *entry;
 
-   if (name == 0)
+   if (!name)
       return -1;
 
-   for (i = 0; i < MESA_EXTENSION_COUNT; ++i) {
-      if (strcmp(name, _mesa_extension_table[i].name) == 0)
-        return i;
-   }
+   entry = bsearch(name,
+                   _mesa_extension_table, MESA_EXTENSION_COUNT,
+                   sizeof(_mesa_extension_table[0]),
+                   extension_name_compare);
+
+   if (entry)
+      return entry - _mesa_extension_table;
 
    return -1;
 }
@@ -129,9 +143,7 @@ _mesa_enable_sw_extensions(struct gl_context *ctx)
    ctx->Extensions.ARB_texture_env_crossbar = GL_TRUE;
    ctx->Extensions.ARB_texture_env_dot3 = GL_TRUE;
    ctx->Extensions.ARB_texture_filter_anisotropic = GL_TRUE;
-#ifdef TEXTURE_FLOAT_ENABLED
    ctx->Extensions.ARB_texture_float = GL_TRUE;
-#endif
    ctx->Extensions.ARB_texture_mirror_clamp_to_edge = GL_TRUE;
    ctx->Extensions.ARB_texture_non_power_of_two = GL_TRUE;
    ctx->Extensions.ARB_texture_rg = GL_TRUE;
@@ -144,7 +156,6 @@ _mesa_enable_sw_extensions(struct gl_context *ctx)
    ctx->Extensions.ATI_texture_compression_3dc = GL_TRUE;
    ctx->Extensions.ATI_texture_env_combine3 = GL_TRUE;
    ctx->Extensions.ATI_texture_mirror_once = GL_TRUE;
-   ctx->Extensions.ATI_separate_stencil = GL_TRUE;
    ctx->Extensions.EXT_blend_color = GL_TRUE;
    ctx->Extensions.EXT_blend_equation_separate = GL_TRUE;
    ctx->Extensions.EXT_blend_func_separate = GL_TRUE;
@@ -196,31 +207,19 @@ set_extension(struct gl_extensions *ext, int i, GLboolean state)
    return offset;
 }
 
-/**
- * The unrecognized extensions from \c MESA_EXTENSION_OVERRIDE.
- * Returns \c NULL if empty.
- */
-static const char *
-get_extension_override( struct gl_context *ctx )
-{
-   if (extra_extensions)
-      _mesa_problem(ctx, "Trying to enable unknown extensions: %s",
-                    extra_extensions);
-
-   return extra_extensions;
-}
-
 
 /**
- * \brief Free extra_extensions string
+ * \brief Free string pointed by unrecognized_extensions
  *
- * These strings are allocated early during the first context creation by
+ * This string is allocated early during the first context creation by
  * _mesa_one_time_init_extension_overrides.
  */
 static void
 free_unknown_extensions_strings(void)
 {
-   free(extra_extensions);
+   free(unrecognized_extensions.env);
+   for (int i = 0; i < MAX_UNRECOGNIZED_EXTENSIONS; ++i)
+      unrecognized_extensions.names[i] = NULL;
 }
 
 
@@ -242,10 +241,8 @@ _mesa_one_time_init_extension_overrides(void)
    const char *env_const = getenv("MESA_EXTENSION_OVERRIDE");
    char *env;
    char *ext;
-   int len;
    size_t offset;
-
-   atexit(free_unknown_extensions_strings);
+   unsigned unknown_ext = 0;
 
    memset(&_mesa_extension_override_enables, 0, sizeof(struct gl_extensions));
    memset(&_mesa_extension_override_disables, 0, sizeof(struct gl_extensions));
@@ -254,18 +251,11 @@ _mesa_one_time_init_extension_overrides(void)
       return;
    }
 
-   /* extra_exts: List of unrecognized extensions. */
-   extra_extensions = calloc(ALIGN(strlen(env_const) + 2, 4), sizeof(char));
-
    /* Copy env_const because strtok() is destructive. */
    env = strdup(env_const);
 
-   if (env == NULL ||
-       extra_extensions == NULL) {
-      free(env);
-      free(extra_extensions);
+   if (env == NULL)
       return;
-   }
 
    for (ext = strtok(env, " "); ext != NULL; ext = strtok(NULL, " ")) {
       int enable;
@@ -294,20 +284,28 @@ _mesa_one_time_init_extension_overrides(void)
          recognized = false;
 
       if (!recognized && enable) {
-         strcat(extra_extensions, ext);
-         strcat(extra_extensions, " ");
+         if (unknown_ext >= MAX_UNRECOGNIZED_EXTENSIONS) {
+            static bool warned;
+
+            if (!warned) {
+               warned = true;
+               _mesa_problem(NULL, "Trying to enable too many unknown extension. "
+                                   "Only the first %d will be honoured",
+                                   MAX_UNRECOGNIZED_EXTENSIONS);
+            }
+         } else {
+            unrecognized_extensions.names[unknown_ext] = ext;
+            unknown_ext++;
+            _mesa_problem(NULL, "Trying to enable unknown extension: %s", ext);
+         }
       }
    }
 
-   free(env);
-
-   /* Remove trailing space, and free if unused. */
-   len = strlen(extra_extensions);
-   if (len == 0) {
-      free(extra_extensions);
-      extra_extensions = NULL;
-   } else if (extra_extensions[len - 1] == ' ') {
-      extra_extensions[len - 1] = '\0';
+   if (!unknown_ext) {
+      free(env);
+   } else {
+      unrecognized_extensions.env = env;
+      atexit(free_unknown_extensions_strings);
    }
 }
 
@@ -352,6 +350,30 @@ _mesa_extension_supported(const struct gl_context *ctx, extension_index i)
    return (ctx->Version >= ext->version[ctx->API]) && base[ext->offset];
 }
 
+/**
+ * Compare two entries of the extensions table.  Sorts first by year,
+ * then by name.
+ *
+ * Arguments are indices into _mesa_extension_table.
+ */
+static int
+extension_compare(const void *p1, const void *p2)
+{
+   extension_index i1 = * (const extension_index *) p1;
+   extension_index i2 = * (const extension_index *) p2;
+   const struct mesa_extension *e1 = &_mesa_extension_table[i1];
+   const struct mesa_extension *e2 = &_mesa_extension_table[i2];
+   int res;
+
+   res = (int)e1->year - (int)e2->year;
+
+   if (res == 0) {
+      res = strcmp(e1->name, e2->name);
+   }
+
+   return res;
+}
+
 
 /**
  * Construct the GL_EXTENSIONS string.  Called the first time that
@@ -368,8 +390,6 @@ _mesa_make_extension_string(struct gl_context *ctx)
    unsigned count;
    /* Indices of the extensions sorted by year */
    extension_index extension_indices[MESA_EXTENSION_COUNT];
-   /* String of extra extensions. */
-   const char *extra_extensions = get_extension_override(ctx);
    unsigned k;
    unsigned j;
    unsigned maxYear = ~0;
@@ -391,18 +411,37 @@ _mesa_make_extension_string(struct gl_context *ctx)
 
       if (i->year <= maxYear &&
           _mesa_extension_supported(ctx, k)) {
-         length += strlen(i->name) + 1; /* +1 for space */
-         extension_indices[count++] = k;
+        length += strlen(i->name) + 1; /* +1 for space */
+        ++count;
       }
    }
-   if (extra_extensions != NULL)
-      length += 1 + strlen(extra_extensions); /* +1 for space */
+   for (k = 0; k < MAX_UNRECOGNIZED_EXTENSIONS; k++)
+      if (unrecognized_extensions.names[k])
+         length += 1 + strlen(unrecognized_extensions.names[k]); /* +1 for space */
 
    exts = calloc(ALIGN(length + 1, 4), sizeof(char));
    if (exts == NULL) {
       return NULL;
    }
 
+   /* Sort extensions in chronological order because idTech 2/3 games
+    * (e.g., Quake3 demo) store the extension list in a fixed size buffer.
+    * Some cases truncate, while others overflow the buffer. Resulting in
+    * misrendering and crashes, respectively.
+    * Address the former here, while the latter will be addressed by setting
+    * the MESA_EXTENSION_MAX_YEAR environment variable.
+    */
+   j = 0;
+   for (k = 0; k < MESA_EXTENSION_COUNT; ++k) {
+      if (_mesa_extension_table[k].year <= maxYear &&
+         _mesa_extension_supported(ctx, k)) {
+         extension_indices[j++] = k;
+      }
+   }
+   assert(j == count);
+   qsort(extension_indices, count,
+         sizeof *extension_indices, extension_compare);
+
    /* Build the extension string.*/
    for (j = 0; j < count; ++j) {
       const struct mesa_extension *i = &_mesa_extension_table[extension_indices[j]];
@@ -410,8 +449,11 @@ _mesa_make_extension_string(struct gl_context *ctx)
       strcat(exts, i->name);
       strcat(exts, " ");
    }
-   if (extra_extensions != 0) {
-      strcat(exts, extra_extensions);
+   for (j = 0; j < MAX_UNRECOGNIZED_EXTENSIONS; j++) {
+      if (unrecognized_extensions.names[j]) {
+         strcat(exts, unrecognized_extensions.names[j]);
+         strcat(exts, " ");
+      }
    }
 
    return (GLubyte *) exts;
@@ -433,6 +475,11 @@ _mesa_get_extension_count(struct gl_context *ctx)
       if (_mesa_extension_supported(ctx, k))
         ctx->Extensions.Count++;
    }
+
+   for (k = 0; k < MAX_UNRECOGNIZED_EXTENSIONS; ++k) {
+      if (unrecognized_extensions.names[k])
+         ctx->Extensions.Count++;
+   }
    return ctx->Extensions.Count;
 }
 
@@ -454,5 +501,13 @@ _mesa_get_enabled_extension(struct gl_context *ctx, GLuint index)
       }
    }
 
+   for (i = 0; i < MAX_UNRECOGNIZED_EXTENSIONS; ++i) {
+      if (unrecognized_extensions.names[i]) {
+         if (n == index)
+            return (const GLubyte*) unrecognized_extensions.names[i];
+         else
+            ++n;
+      }
+   }
    return NULL;
 }