From cdde6fcb7947514753c1a3feaee71c2212cffea6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marek=20Ol=C5=A1=C3=A1k?= <marek.olsak@amd.com>
Date: Tue, 6 Feb 2024 03:10:58 -0500
Subject: [PATCH] mesa: enable GL names reuse for _mesa_HashTable, remove the
 alternative

This matches the behavior of closed source drivers and driconf workarounds
are no longer needed.

Reviewed-by: Adam Jackson <ajax@redhat.com>
---
 .../auxiliary/pipe-loader/driinfo_gallium.h   |  1 -
 src/gallium/auxiliary/util/u_driconf.c        |  1 -
 src/gallium/include/frontend/api.h            |  1 -
 src/mesa/main/consts_exts.h                   |  5 --
 src/mesa/main/hash.c                          | 60 +------------------
 src/mesa/main/hash.h                          |  8 +--
 src/mesa/state_tracker/st_context.c           | 18 ------
 src/mesa/state_tracker/st_extensions.c        |  2 -
 src/util/00-mesa-defaults.conf                |  9 ---
 src/util/driconf.h                            |  3 -
 10 files changed, 4 insertions(+), 104 deletions(-)

diff --git a/src/gallium/auxiliary/pipe-loader/driinfo_gallium.h b/src/gallium/auxiliary/pipe-loader/driinfo_gallium.h
index f764916b6303a..6975859f466e6 100644
--- a/src/gallium/auxiliary/pipe-loader/driinfo_gallium.h
+++ b/src/gallium/auxiliary/pipe-loader/driinfo_gallium.h
@@ -41,7 +41,6 @@ DRI_CONF_SECTION_DEBUG
    DRI_CONF_GLTHREAD_NOP_CHECK_FRAMEBUFFER_STATUS(false)
    DRI_CONF_FORCE_COMPAT_PROFILE(false)
    DRI_CONF_FORCE_COMPAT_SHADERS(false)
-   DRI_CONF_FORCE_GL_NAMES_REUSE(false)
    DRI_CONF_FORCE_GL_MAP_BUFFER_SYNCHRONIZED(false)
    DRI_CONF_TRANSCODE_ETC(false)
    DRI_CONF_TRANSCODE_ASTC(false)
diff --git a/src/gallium/auxiliary/util/u_driconf.c b/src/gallium/auxiliary/util/u_driconf.c
index 165de18b131c3..497b8ac98de2f 100644
--- a/src/gallium/auxiliary/util/u_driconf.c
+++ b/src/gallium/auxiliary/util/u_driconf.c
@@ -67,7 +67,6 @@ u_driconf_fill_st_options(struct st_config_options *options,
    query_bool_option(glthread_nop_check_framebuffer_status);
    query_bool_option(ignore_map_unsynchronized);
    query_bool_option(ignore_discard_framebuffer);
-   query_bool_option(force_gl_names_reuse);
    query_bool_option(force_gl_map_buffer_synchronized);
    query_bool_option(transcode_etc);
    query_bool_option(transcode_astc);
diff --git a/src/gallium/include/frontend/api.h b/src/gallium/include/frontend/api.h
index 712cbe9e47e97..abf5d59c9c2eb 100644
--- a/src/gallium/include/frontend/api.h
+++ b/src/gallium/include/frontend/api.h
@@ -196,7 +196,6 @@ struct st_config_options
    bool ignore_map_unsynchronized;
    bool ignore_discard_framebuffer;
    bool force_integer_tex_nearest;
-   bool force_gl_names_reuse;
    bool force_gl_map_buffer_synchronized;
    bool transcode_etc;
    bool transcode_astc;
diff --git a/src/mesa/main/consts_exts.h b/src/mesa/main/consts_exts.h
index e0a5ed0c930be..94694055f28b4 100644
--- a/src/mesa/main/consts_exts.h
+++ b/src/mesa/main/consts_exts.h
@@ -677,11 +677,6 @@ struct gl_constants
     */
    GLchar GLSLZeroInit;
 
-   /**
-    * Force GL names reuse. Needed by SPECviewperf13.
-    */
-   GLboolean ForceGLNamesReuse;
-
    /**
     * Treat integer textures using GL_LINEAR filters as GL_NEAREST.
     */
diff --git a/src/mesa/main/hash.c b/src/mesa/main/hash.c
index ef6df6eb63c3f..96e9d651f27c6 100644
--- a/src/mesa/main/hash.c
+++ b/src/mesa/main/hash.c
@@ -86,14 +86,6 @@ _mesa_DeinitHashTable(struct _mesa_HashTable *table,
    simple_mtx_destroy(&table->Mutex);
 }
 
-void
-_mesa_HashEnableNameReuse(struct _mesa_HashTable *table)
-{
-   _mesa_HashLockMutex(table);
-   table->alloc_via_idalloc = true;
-   _mesa_HashUnlockMutex(table);
-}
-
 /**
  * Insert a key/pointer pair into the hash table without locking the mutex.
  * If an entry with this key already exists we'll replace the existing entry.
@@ -109,10 +101,6 @@ void
 _mesa_HashInsertLocked(struct _mesa_HashTable *table, GLuint key, void *data)
 {
    assert(key);
-
-   if (key > table->MaxKey)
-      table->MaxKey = key;
-
    *(void**)util_sparse_array_get(&table->array, key) = data;
 
    util_idalloc_sparse_reserve(&table->id_alloc, key);
@@ -195,61 +183,19 @@ _mesa_HashWalk(struct _mesa_HashTable *table,
  * \param table the hash table.
  * \param numKeys number of keys needed.
  * 
- * \return Starting key of free block or 0 if failure.
- *
- * If there are enough free keys between the maximum key existing in the table
- * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
- * the adjacent key. Otherwise do a full search for a free key block in the
- * allowable key range.
+ * \return Starting key of a free block
  */
 GLuint
 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
 {
-   const GLuint maxKey = ~((GLuint) 0) - 1;
-   if (table->alloc_via_idalloc) {
-      return util_idalloc_sparse_alloc_range(&table->id_alloc, numKeys);
-   } else if (maxKey - numKeys > table->MaxKey) {
-      /* the quick solution */
-      return table->MaxKey + 1;
-   }
-   else {
-      /* the slow solution */
-      GLuint freeCount = 0;
-      GLuint freeStart = 1;
-      GLuint key;
-      for (key = 1; key != maxKey; key++) {
-	 if (_mesa_HashLookupLocked(table, key)) {
-	    /* darn, this key is already in use */
-	    freeCount = 0;
-	    freeStart = key+1;
-	 }
-	 else {
-	    /* this key not in use, check if we've found enough */
-	    freeCount++;
-	    if (freeCount == numKeys) {
-	       return freeStart;
-	    }
-	 }
-      }
-      /* cannot allocate a block of numKeys consecutive keys */
-      return 0;
-   }
+   return util_idalloc_sparse_alloc_range(&table->id_alloc, numKeys);
 }
 
 bool
 _mesa_HashFindFreeKeys(struct _mesa_HashTable *table, GLuint* keys, GLuint numKeys)
 {
-   if (!table->alloc_via_idalloc) {
-      GLuint first = _mesa_HashFindFreeKeyBlock(table, numKeys);
-      for (int i = 0; i < numKeys; i++) {
-         keys[i] = first + i;
-      }
-      return first != 0;
-   }
-
-   for (int i = 0; i < numKeys; i++) {
+   for (int i = 0; i < numKeys; i++)
       keys[i] = util_idalloc_sparse_alloc(&table->id_alloc);
-   }
 
    return true;
 }
diff --git a/src/mesa/main/hash.h b/src/mesa/main/hash.h
index f9f0afeeb3f97..3c4d852dcaca7 100644
--- a/src/mesa/main/hash.h
+++ b/src/mesa/main/hash.h
@@ -46,11 +46,8 @@
  */
 struct _mesa_HashTable {
    struct util_sparse_array array;
-   simple_mtx_t Mutex;
-   GLuint MaxKey;                        /**< highest key inserted so far */
-   bool alloc_via_idalloc;
-   /* Used when name reuse is enabled */
    struct util_idalloc_sparse id_alloc;
+   simple_mtx_t Mutex;
 };
 
 void
@@ -90,9 +87,6 @@ bool
 _mesa_HashFindFreeKeys(struct _mesa_HashTable *table, GLuint* keys,
                        GLuint numKeys);
 
-void
-_mesa_HashEnableNameReuse(struct _mesa_HashTable *table);
-
 /* Inline functions. */
 
 /**
diff --git a/src/mesa/state_tracker/st_context.c b/src/mesa/state_tracker/st_context.c
index a13657b25d6e9..394679636a9cc 100644
--- a/src/mesa/state_tracker/st_context.c
+++ b/src/mesa/state_tracker/st_context.c
@@ -723,24 +723,6 @@ st_create_context_priv(struct gl_context *ctx, struct pipe_context *pipe,
 
    st->bitmap.cache.empty = true;
 
-   if (ctx->Const.ForceGLNamesReuse && ctx->Shared->RefCount == 1) {
-      _mesa_HashEnableNameReuse(&ctx->Shared->TexObjects);
-      _mesa_HashEnableNameReuse(&ctx->Shared->ShaderObjects);
-      _mesa_HashEnableNameReuse(&ctx->Shared->BufferObjects);
-      _mesa_HashEnableNameReuse(&ctx->Shared->SamplerObjects);
-      _mesa_HashEnableNameReuse(&ctx->Shared->FrameBuffers);
-      _mesa_HashEnableNameReuse(&ctx->Shared->RenderBuffers);
-      _mesa_HashEnableNameReuse(&ctx->Shared->MemoryObjects);
-      _mesa_HashEnableNameReuse(&ctx->Shared->SemaphoreObjects);
-   }
-   /* SPECviewperf13/sw-04 crashes since a56849ddda6 if Mesa is build with
-    * -O3 on gcc 7.5, which doesn't happen with ForceGLNamesReuse, which is
-    * the default setting for SPECviewperf because it simulates glGen behavior
-    * of closed source drivers.
-    */
-   if (ctx->Const.ForceGLNamesReuse)
-      _mesa_HashEnableNameReuse(&ctx->Query.QueryObjects);
-
    _mesa_override_extensions(ctx);
    _mesa_compute_version(ctx);
 
diff --git a/src/mesa/state_tracker/st_extensions.c b/src/mesa/state_tracker/st_extensions.c
index 650c831f9925e..03829a2e603e1 100644
--- a/src/mesa/state_tracker/st_extensions.c
+++ b/src/mesa/state_tracker/st_extensions.c
@@ -1326,8 +1326,6 @@ void st_init_extensions(struct pipe_screen *screen,
       consts->GLSLZeroInit = screen->get_param(screen, PIPE_CAP_GLSL_ZERO_INIT);
    }
 
-   consts->ForceGLNamesReuse = options->force_gl_names_reuse;
-
    consts->ForceIntegerTexNearest = options->force_integer_tex_nearest;
 
    consts->VendorOverride = options->force_gl_vendor;
diff --git a/src/util/00-mesa-defaults.conf b/src/util/00-mesa-defaults.conf
index 3de1fb510087c..3733382db7678 100644
--- a/src/util/00-mesa-defaults.conf
+++ b/src/util/00-mesa-defaults.conf
@@ -384,7 +384,6 @@ TODO: document the other workarounds.
             <option name="vblank_mode" value="0" />
             <option name="allow_glsl_extension_directive_midshader" value="true" />
             <option name="allow_glsl_120_subset_in_110" value="true" />
-            <option name="force_gl_names_reuse" value="true" />
             <!-- This makes it use GL_NVX_gpu_memory_info to query memory info
                  instead of non-existent GLX_AMD_gpu_association. Yes, it checks
                  the vendor string, not the extension list. -->
@@ -884,14 +883,6 @@ TODO: document the other workarounds.
             <option name="glsl_correct_derivatives_after_discard" value="true" />
         </application>
 
-        <!-- Workaround for STAR WARS: Knights of the Old Republic relying on
-             non-specified OpenGL behavior (expecting texture names to be
-             small enough to fit in the static array).
-          -->
-        <application name="SWKOTOR (wine)" executable="swkotor.exe">
-            <option name="force_gl_names_reuse" value="true"/>
-        </application>
-
         <application name="Assault Android Cactus (32-bit)" executable="cactus_demo.x86">
             <option name="vs_position_always_precise" value="true" />
         </application>
diff --git a/src/util/driconf.h b/src/util/driconf.h
index 37107a11aef98..7317f81d2ad6d 100644
--- a/src/util/driconf.h
+++ b/src/util/driconf.h
@@ -269,9 +269,6 @@
    DRI_CONF_OPT_I(override_vram_size, -1, -1, 2147483647, \
                   "Override the VRAM size advertised to the application in MiB (-1 = default)")
 
-#define DRI_CONF_FORCE_GL_NAMES_REUSE(def) \
-   DRI_CONF_OPT_B(force_gl_names_reuse, def, "Force GL names reuse")
-
 #define DRI_CONF_FORCE_GL_MAP_BUFFER_SYNCHRONIZED(def) \
    DRI_CONF_OPT_B(force_gl_map_buffer_synchronized, def, "Override GL_MAP_UNSYNCHRONIZED_BIT.")
 
-- 
GitLab