driconf: add option to reuse GL names

Fix apps expecting name recycling.
https://gitlab.freedesktop.org/mesa/mesa/-/issues/3144 is an example
of such issue, SPECviewperf13 has this problem too.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6600>
diff --git a/src/gallium/auxiliary/pipe-loader/driinfo_gallium.h b/src/gallium/auxiliary/pipe-loader/driinfo_gallium.h
index 9246f24..d7872b1 100644
--- a/src/gallium/auxiliary/pipe-loader/driinfo_gallium.h
+++ b/src/gallium/auxiliary/pipe-loader/driinfo_gallium.h
@@ -34,6 +34,7 @@
    DRI_CONF_ALLOW_GLSL_LAYOUT_QUALIFIER_ON_FUNCTION_PARAMETERS("false")
    DRI_CONF_ALLOW_DRAW_OUT_OF_ORDER("false")
    DRI_CONF_FORCE_COMPAT_PROFILE("false")
+   DRI_CONF_FORCE_GL_NAMES_REUSE("false")
    DRI_CONF_FORCE_GL_VENDOR()
    DRI_CONF_OVERRIDE_VRAM_SIZE()
 DRI_CONF_SECTION_END
diff --git a/src/gallium/frontends/dri/dri_screen.c b/src/gallium/frontends/dri/dri_screen.c
index e3bda00..bcdd96b 100644
--- a/src/gallium/frontends/dri/dri_screen.c
+++ b/src/gallium/frontends/dri/dri_screen.c
@@ -98,6 +98,8 @@
       driQueryOptionb(optionCache, "allow_glsl_layout_qualifier_on_function_parameters");
    options->allow_draw_out_of_order =
       driQueryOptionb(optionCache, "allow_draw_out_of_order");
+   options->force_gl_names_reuse =
+      driQueryOptionb(optionCache, "force_gl_names_reuse");
 
    char *vendor_str = driQueryOptionstr(optionCache, "force_gl_vendor");
    /* not an empty string */
diff --git a/src/gallium/include/frontend/api.h b/src/gallium/include/frontend/api.h
index effc2cd..9dcabe0 100644
--- a/src/gallium/include/frontend/api.h
+++ b/src/gallium/include/frontend/api.h
@@ -233,6 +233,7 @@
    bool allow_glsl_layout_qualifier_on_function_parameters;
    bool allow_draw_out_of_order;
    bool force_integer_tex_nearest;
+   bool force_gl_names_reuse;
    char *force_gl_vendor;
    unsigned char config_options_sha1[20];
 };
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index 5fb526f..80dff85 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -3891,6 +3891,11 @@
    GLchar GLSLZeroInit;
 
    /**
+    * Force GL names reuse. Needed by SPECviewperf13.
+    */
+   GLboolean ForceGLNamesReuse;
+
+   /**
     * Treat integer textures using GL_LINEAR filters as GL_NEAREST.
     */
    GLboolean ForceIntegerTexNearest;
diff --git a/src/mesa/state_tracker/st_context.c b/src/mesa/state_tracker/st_context.c
index aaaa615..271c18c 100644
--- a/src/mesa/state_tracker/st_context.c
+++ b/src/mesa/state_tracker/st_context.c
@@ -815,6 +815,17 @@
 
    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);
+   }
+
    _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 f5443b0..1278ef1 100644
--- a/src/mesa/state_tracker/st_extensions.c
+++ b/src/mesa/state_tracker/st_extensions.c
@@ -1221,6 +1221,8 @@
       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 c569903..ce0a633 100644
--- a/src/util/00-mesa-defaults.conf
+++ b/src/util/00-mesa-defaults.conf
@@ -286,6 +286,7 @@
         <application name="SPECviewperf13" executable="viewperf">
             <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" />
         </application>
 
         <!-- The GL thread allowlist is below, workarounds are above.
diff --git a/src/util/driconf.h b/src/util/driconf.h
index 7559aa7..12bfadb 100644
--- a/src/util/driconf.h
+++ b/src/util/driconf.h
@@ -227,6 +227,11 @@
         DRI_CONF_DESC("Override the VRAM size advertised to the application in MiB (-1 = default)") \
 DRI_CONF_OPT_END
 
+#define DRI_CONF_FORCE_GL_NAMES_REUSE(def) \
+DRI_CONF_OPT_BEGIN_B(force_gl_names_reuse, def) \
+        DRI_CONF_DESC("Force GL names reuse") \
+DRI_CONF_OPT_END
+
 /**
  * \brief Image quality-related options
  */