blob: 4a701d190fb1153a228d05605c80a061d3a2090b [file] [log] [blame]
From 5b6f9e4821a48a1abedf6082ab7a7cb06e7b7905 Mon Sep 17 00:00:00 2001
From: John Bates <jbates@chromium.org>
Date: Tue, 13 Oct 2020 14:17:53 -0700
Subject: [PATCH] BACKPORT: disk_cache: build option for disabled-by-default
On some systems it is problematic to have the shader cache enabled
by default. This adds a build option to support the disk cache but
keep it disabled unless the environment variable
MESA_GLSL_CACHE_DISABLE=false.
For example, on Chrome OS, Chrome already has it's own shader
disk cache implementation so it disables the mesa feature. Tests
do not want the shader disk cache enabled because it can cause
inconsistent performance results and the default 1GB for the
disk cache could lead to problems that require more effort to
work around. The Mesa shader disk cache is useful for VMs though,
where it is easy to configure the feature with environment
variables. With the current version of Mesa, Chrome OS would need
to have a system-wide environment variable to disable the disk
cache everywhere except where needed. More elegant to just build
Mesa with the cache feature disabled by default.
Reviewed-by: Rob Clark <robdclark@chromium.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
---
docs/envvars.rst | 4 +++-
meson.build | 5 ++++-
meson_options.txt | 4 ++--
src/compiler/glsl/tests/cache_test.c | 21 +++++++++++++++++++++
src/util/disk_cache.c | 8 +++++++-
5 files changed, 37 insertions(+), 5 deletions(-)
diff --git a/docs/envvars.rst b/docs/envvars.rst
index 76397eec041..872463e7ccb 100644
--- a/docs/envvars.rst
+++ b/docs/envvars.rst
@@ -141,7 +141,9 @@ Core Mesa environment variables
features of the given language version if it's higher than what's
normally reported. (for developers only)
``MESA_GLSL_CACHE_DISABLE``
- if set to ``true``, disables the GLSL shader cache
+ if set to ``true``, disables the GLSL shader cache. If set to
+ ``false``, enables the GLSL shader cache when it is disabled by
+ default.
``MESA_GLSL_CACHE_MAX_SIZE``
if set, determines the maximum size of the on-disk cache of compiled
GLSL programs. Should be set to a number optionally followed by
diff --git a/meson.build b/meson.build
index e8b641c6b1d..46844e8a1e3 100644
--- a/meson.build
+++ b/meson.build
@@ -901,11 +901,14 @@ elif _shader_cache == 'false'
endif
if _shader_cache != 'disabled'
if host_machine.system() == 'windows'
- if _shader_cache == 'enabled'
+ if ['enabled', 'default-disabled'].contains(_shader_cache)
error('Shader Cache does not currently work on Windows')
endif
else
pre_args += '-DENABLE_SHADER_CACHE'
+ if _shader_cache == 'default-disabled'
+ pre_args += '-DSHADER_CACHE_DISABLE_BY_DEFAULT'
+ endif
with_shader_cache = true
endif
endif
diff --git a/meson_options.txt b/meson_options.txt
index 2d39d13b6ad..ed93d545cc0 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -173,8 +173,8 @@ option(
'shader-cache',
type : 'combo',
value : 'auto',
- choices : ['auto', 'true', 'false', 'enabled', 'disabled'],
- description : 'Build with on-disk shader cache support'
+ choices : ['auto', 'true', 'false', 'enabled', 'disabled', 'default-disabled'],
+ description : 'Build with on-disk shader cache support. If set to default-disabled, the feature is only activated when environment variable MESA_GLSL_CACHE_DISABLE is set to false'
)
option(
'vulkan-icd-dir',
diff --git a/src/compiler/glsl/tests/cache_test.c b/src/compiler/glsl/tests/cache_test.c
index a1db67a5845..926b5e458cc 100644
--- a/src/compiler/glsl/tests/cache_test.c
+++ b/src/compiler/glsl/tests/cache_test.c
@@ -193,6 +193,19 @@ test_disk_cache_create(void)
unsetenv("MESA_GLSL_CACHE_DISABLE");
+#ifdef SHADER_CACHE_DISABLE_BY_DEFAULT
+ /* With SHADER_CACHE_DISABLE_BY_DEFAULT, ensure that with
+ * MESA_GLSL_CACHE_DISABLE set to nothing, disk_cache_create returns NULL.
+ */
+ unsetenv("MESA_GLSL_CACHE_DISABLE");
+ cache = disk_cache_create("test", "make_check", 0);
+ expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DISABLE unset "
+ " and SHADER_CACHE_DISABLE_BY_DEFAULT build option");
+
+ /* For remaining tests, ensure that the cache is enabled. */
+ setenv("MESA_GLSL_CACHE_DISABLE", "false", 1);
+#endif /* SHADER_CACHE_DISABLE_BY_DEFAULT */
+
/* For the first real disk_cache_create() clear these environment
* variables to test creation of cache in home directory.
*/
@@ -266,6 +279,10 @@ test_put_and_get(void)
uint8_t one_KB_key[20], one_MB_key[20];
int count;
+#ifdef SHADER_CACHE_DISABLE_BY_DEFAULT
+ setenv("MESA_GLSL_CACHE_DISABLE", "false", 1);
+#endif /* SHADER_CACHE_DISABLE_BY_DEFAULT */
+
cache = disk_cache_create("test", "make_check", 0);
disk_cache_compute_key(cache, blob, sizeof(blob), blob_key);
@@ -437,6 +454,10 @@ test_put_key_and_get_key(void)
{ 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
+#ifdef SHADER_CACHE_DISABLE_BY_DEFAULT
+ setenv("MESA_GLSL_CACHE_DISABLE", "false", 1);
+#endif /* SHADER_CACHE_DISABLE_BY_DEFAULT */
+
cache = disk_cache_create("test", "make_check", 0);
/* First test that disk_cache_has_key returns false before disk_cache_put_key */
diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c
index a92d621927a..90310b85463 100644
--- a/src/util/disk_cache.c
+++ b/src/util/disk_cache.c
@@ -210,6 +210,12 @@ disk_cache_create(const char *gpu_name, const char *driver_id,
uint8_t cache_version = CACHE_VERSION;
size_t cv_size = sizeof(cache_version);
+#ifdef SHADER_CACHE_DISABLE_BY_DEFAULT
+ bool disable_by_default = true;
+#else
+ bool disable_by_default = false;
+#endif
+
/* If running as a users other than the real user disable cache */
if (geteuid() != getuid())
return NULL;
@@ -220,7 +226,7 @@ disk_cache_create(const char *gpu_name, const char *driver_id,
goto fail;
/* At user request, disable shader cache entirely. */
- if (env_var_as_boolean("MESA_GLSL_CACHE_DISABLE", false))
+ if (env_var_as_boolean("MESA_GLSL_CACHE_DISABLE", disable_by_default))
goto fail;
cache = rzalloc(NULL, struct disk_cache);
--
2.28.0.1011.ga647a8990f-goog