Re: [Mesa-dev] [PATCH v2 31/32] i965: Initialize disk shader cache if MESA_GLSL_CACHE_DISABLE is false

2017-10-20 Thread Matt Turner
On Wed, Oct 18, 2017 at 10:32 PM, Jordan Justen
 wrote:
> Double negative FTW!
>
> For now, the shader cache is disabled by default on i965 to allow us
> to verify its stability.
>
> In other words, to enable the shader cache on i965, set
> MESA_GLSL_CACHE_DISABLE to false or 0. If the variable is unset, then
> the shader cache will be disabled.
>
> We use the build-id of i965_dri.so for the timestamp, and the pci
> device id for the device name.
>
> v2:
>  * Simplify code by forcing link to include build id sha. (Matt)
>
> Signed-off-by: Jordan Justen 
> ---
>  src/mesa/drivers/dri/i965/brw_context.c|  2 ++
>  src/mesa/drivers/dri/i965/brw_disk_cache.c | 39 
> ++
>  src/mesa/drivers/dri/i965/brw_state.h  |  1 +
>  3 files changed, 42 insertions(+)
>
> diff --git a/src/mesa/drivers/dri/i965/brw_context.c 
> b/src/mesa/drivers/dri/i965/brw_context.c
> index 6a88d8bb48..1fdaf02022 100644
> --- a/src/mesa/drivers/dri/i965/brw_context.c
> +++ b/src/mesa/drivers/dri/i965/brw_context.c
> @@ -1018,6 +1018,8 @@ brwCreateContext(gl_api api,
>   brw->dri_config_options_sha1);
> brw->ctx.Const.dri_config_options_sha1 = brw->dri_config_options_sha1;
>
> +   brw_disk_cache_init(brw);
> +
> return true;
>  }
>
> diff --git a/src/mesa/drivers/dri/i965/brw_disk_cache.c 
> b/src/mesa/drivers/dri/i965/brw_disk_cache.c
> index 790fad6925..582c2cfbc7 100644
> --- a/src/mesa/drivers/dri/i965/brw_disk_cache.c
> +++ b/src/mesa/drivers/dri/i965/brw_disk_cache.c
> @@ -26,6 +26,8 @@
>  #include "compiler/glsl/shader_cache.h"
>  #include "compiler/nir/nir_serialize.h"
>  #include "main/mtypes.h"
> +#include "util/build_id.h"
> +#include "util/debug.h"
>  #include "util/disk_cache.h"
>  #include "util/macros.h"
>  #include "util/mesa-sha1.h"
> @@ -496,3 +498,40 @@ brw_disk_cache_write_compute_program(struct brw_context 
> *brw)
>   MESA_SHADER_COMPUTE);
> }
>  }
> +
> +void
> +brw_disk_cache_init(struct brw_context *brw)
> +{
> +#ifdef ENABLE_SHADER_CACHE
> +   if (env_var_as_boolean("MESA_GLSL_CACHE_DISABLE", true))
> +  return;
> +
> +   char *renderer = NULL;
> +   int len = asprintf(&renderer, "i965_%04x", brw->screen->deviceID);
> +   if (len < 0) {
> +  renderer = strdup("i965");
> +   }
> +   if (renderer == NULL)
> +   return;
> +
> +   const struct build_id_note *note =
> +  build_id_find_nhdr_for_addr(brw_disk_cache_init);
> +   assert(note);
> +   int id_size = build_id_length(note);
> +   char *timestamp = malloc(2 * id_size + 1);
> +
> +   const uint8_t *data = build_id_data(note);
> +   int i;
> +   for (i = 0; i < id_size; i++)
> +  snprintf(×tamp[2 * i], 3, "%02x", data[i]);

This is kind of trivial, but it bothered me. :)

Maybe just do

static const char hex[] = "0123456789ABCDEF";
for (int i = 0; i < id_size; i++) {
str[i * 2 + 0] = hex[data[i] / 16];
str[i * 2 + 1] = hex[data[i] % 16];
}
str[16] = '\0';

which doesn't call snprintf() 8 times (which overwrites the NUL byte
written by the previous call).
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


[Mesa-dev] [PATCH v2 31/32] i965: Initialize disk shader cache if MESA_GLSL_CACHE_DISABLE is false

2017-10-18 Thread Jordan Justen
Double negative FTW!

For now, the shader cache is disabled by default on i965 to allow us
to verify its stability.

In other words, to enable the shader cache on i965, set
MESA_GLSL_CACHE_DISABLE to false or 0. If the variable is unset, then
the shader cache will be disabled.

We use the build-id of i965_dri.so for the timestamp, and the pci
device id for the device name.

v2:
 * Simplify code by forcing link to include build id sha. (Matt)

Signed-off-by: Jordan Justen 
---
 src/mesa/drivers/dri/i965/brw_context.c|  2 ++
 src/mesa/drivers/dri/i965/brw_disk_cache.c | 39 ++
 src/mesa/drivers/dri/i965/brw_state.h  |  1 +
 3 files changed, 42 insertions(+)

diff --git a/src/mesa/drivers/dri/i965/brw_context.c 
b/src/mesa/drivers/dri/i965/brw_context.c
index 6a88d8bb48..1fdaf02022 100644
--- a/src/mesa/drivers/dri/i965/brw_context.c
+++ b/src/mesa/drivers/dri/i965/brw_context.c
@@ -1018,6 +1018,8 @@ brwCreateContext(gl_api api,
  brw->dri_config_options_sha1);
brw->ctx.Const.dri_config_options_sha1 = brw->dri_config_options_sha1;
 
+   brw_disk_cache_init(brw);
+
return true;
 }
 
diff --git a/src/mesa/drivers/dri/i965/brw_disk_cache.c 
b/src/mesa/drivers/dri/i965/brw_disk_cache.c
index 790fad6925..582c2cfbc7 100644
--- a/src/mesa/drivers/dri/i965/brw_disk_cache.c
+++ b/src/mesa/drivers/dri/i965/brw_disk_cache.c
@@ -26,6 +26,8 @@
 #include "compiler/glsl/shader_cache.h"
 #include "compiler/nir/nir_serialize.h"
 #include "main/mtypes.h"
+#include "util/build_id.h"
+#include "util/debug.h"
 #include "util/disk_cache.h"
 #include "util/macros.h"
 #include "util/mesa-sha1.h"
@@ -496,3 +498,40 @@ brw_disk_cache_write_compute_program(struct brw_context 
*brw)
  MESA_SHADER_COMPUTE);
}
 }
+
+void
+brw_disk_cache_init(struct brw_context *brw)
+{
+#ifdef ENABLE_SHADER_CACHE
+   if (env_var_as_boolean("MESA_GLSL_CACHE_DISABLE", true))
+  return;
+
+   char *renderer = NULL;
+   int len = asprintf(&renderer, "i965_%04x", brw->screen->deviceID);
+   if (len < 0) {
+  renderer = strdup("i965");
+   }
+   if (renderer == NULL)
+   return;
+
+   const struct build_id_note *note =
+  build_id_find_nhdr_for_addr(brw_disk_cache_init);
+   assert(note);
+   int id_size = build_id_length(note);
+   char *timestamp = malloc(2 * id_size + 1);
+
+   const uint8_t *data = build_id_data(note);
+   int i;
+   for (i = 0; i < id_size; i++)
+  snprintf(×tamp[2 * i], 3, "%02x", data[i]);
+
+   if (timestamp == NULL) {
+  free(renderer);
+  return;
+   }
+
+   brw->ctx.Cache = disk_cache_create(renderer, timestamp, 0);
+   free(renderer);
+   free(timestamp);
+#endif
+}
diff --git a/src/mesa/drivers/dri/i965/brw_state.h 
b/src/mesa/drivers/dri/i965/brw_state.h
index c98b7facd5..927e77920e 100644
--- a/src/mesa/drivers/dri/i965/brw_state.h
+++ b/src/mesa/drivers/dri/i965/brw_state.h
@@ -132,6 +132,7 @@ void gen8_write_pma_stall_bits(struct brw_context *brw,
uint32_t pma_stall_bits);
 
 /* brw_disk_cache.c */
+void brw_disk_cache_init(struct brw_context *brw);
 bool brw_disk_cache_upload_program(struct brw_context *brw,
gl_shader_stage stage);
 void brw_disk_cache_write_compute_program(struct brw_context *brw);
-- 
2.15.0.rc0

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev