<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">I made one minor comment below.</div><div class="gmail_quote"><br></div><div class="gmail_quote">Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">On Sun, Oct 22, 2017 at 1:01 PM, Jordan Justen <span dir="ltr"><<a href="mailto:jordan.l.justen@intel.com" target="_blank">jordan.l.justen@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Timothy Arceri <<a href="mailto:timothy.arceri@collabora.com">timothy.arceri@collabora.com</a>><br>
<br>
This uses the recently-added disk_cache.c to write out the final<br>
linked binary for vertex and fragment shader programs.<br>
<br>
This is based off the initial implementation done by Carl Worth.<br>
<br>
v2:<br>
 * Squash 'i965: add image param shader cache support'<br>
 * Squash 'i965: add shader cache support for pull param pointers'<br>
 * Sustantially simplified by a rework on top of Jason's 2975e4c56a7a.<br>
 * Rename load_program_data to read_program_data. (Jason)<br>
<br>
v3:<br>
 * Simplify and align program read/write. (Jason)<br>
<br>
[<a href="mailto:jordan.l.justen@intel.com">jordan.l.justen@intel.com</a>: *_cached_program => brw_disk_cache_*_program]<br>
[<a href="mailto:jordan.l.justen@intel.com">jordan.l.justen@intel.com</a>: brw_shader_cache.c => brw_disk_cache.c]<br>
[<a href="mailto:jordan.l.justen@intel.com">jordan.l.justen@intel.com</a>: don't map to write program when LLC is present]<br>
[<a href="mailto:jordan.l.justen@intel.com">jordan.l.justen@intel.com</a>: set program_written_to_cache on read from cache]<br>
[<a href="mailto:jordan.l.justen@intel.com">jordan.l.justen@intel.com</a>: only try cache when status is linking_skipped]<br>
[<a href="mailto:jordan.l.justen@intel.com">jordan.l.justen@intel.com</a>: rework based on uniforms rework 2975e4c56a7a]<br>
[<a href="mailto:jordan.l.justen@intel.com">jordan.l.justen@intel.com</a>: Simplify and align program read/write]<br>
Signed-off-by: Jordan Justen <<a href="mailto:jordan.l.justen@intel.com">jordan.l.justen@intel.com</a>><br>
---<br>
 src/mesa/drivers/dri/i965/<wbr>Makefile.sources |   1 +<br>
 src/mesa/drivers/dri/i965/brw_<wbr>disk_cache.c | 329 +++++++++++++++++++++++++++++<br>
 src/mesa/drivers/dri/i965/brw_<wbr>state.h      |   5 +<br>
 src/mesa/drivers/dri/i965/<wbr>meson.build      |   1 +<br>
 4 files changed, 336 insertions(+)<br>
 create mode 100644 src/mesa/drivers/dri/i965/brw_<wbr>disk_cache.c<br>
<br>
diff --git a/src/mesa/drivers/dri/i965/<wbr>Makefile.sources b/src/mesa/drivers/dri/i965/<wbr>Makefile.sources<br>
index 053d89b81ec..2980cdb3c54 100644<br>
--- a/src/mesa/drivers/dri/i965/<wbr>Makefile.sources<br>
+++ b/src/mesa/drivers/dri/i965/<wbr>Makefile.sources<br>
@@ -14,6 +14,7 @@ i965_FILES = \<br>
        brw_cs.h \<br>
        brw_curbe.c \<br>
        brw_defines.h \<br>
+       brw_disk_cache.c \<br>
        brw_draw.c \<br>
        brw_draw.h \<br>
        brw_draw_upload.c \<br>
diff --git a/src/mesa/drivers/dri/i965/<wbr>brw_disk_cache.c b/src/mesa/drivers/dri/i965/<wbr>brw_disk_cache.c<br>
new file mode 100644<br>
index 00000000000..186cbe83706<br>
--- /dev/null<br>
+++ b/src/mesa/drivers/dri/i965/<wbr>brw_disk_cache.c<br>
@@ -0,0 +1,329 @@<br>
+/*<br>
+ * Copyright © 2014 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include "compiler/blob.h"<br>
+#include "compiler/glsl/ir_uniform.h"<br>
+#include "compiler/glsl/shader_cache.h"<br>
+#include "main/mtypes.h"<br>
+#include "util/disk_cache.h"<br>
+#include "util/macros.h"<br>
+#include "util/mesa-sha1.h"<br>
+<br>
+#include "brw_context.h"<br>
+#include "brw_state.h"<br>
+#include "brw_vs.h"<br>
+#include "brw_wm.h"<br>
+<br>
+static void<br>
+gen_shader_sha1(struct brw_context *brw, struct gl_program *prog,<br>
+                gl_shader_stage stage, void *key, unsigned char *out_sha1)<br>
+{<br>
+   char sha1_buf[41];<br>
+   unsigned char sha1[20];<br>
+   char manifest[256];<br>
+   int offset = 0;<br>
+<br>
+   _mesa_sha1_format(sha1_buf, prog->sh.data->sha1);<br>
+   offset += snprintf(manifest, sizeof(manifest), "program: %s\n", sha1_buf);<br>
+<br>
+   _mesa_sha1_compute(key, brw_prog_key_size(stage), sha1);<br>
+   _mesa_sha1_format(sha1_buf, sha1);<br>
+   offset += snprintf(manifest + offset, sizeof(manifest) - offset,<br>
+                      "%s_key: %s\n", _mesa_shader_stage_to_abbrev(<wbr>stage),<br>
+                      sha1_buf);<br>
+<br>
+   _mesa_sha1_compute(manifest, strlen(manifest), out_sha1);<br>
+}<br>
+<br>
+static void<br>
+write_blob_program_data(<wbr>struct blob *binary, const void *program,<br>
+                        size_t program_size,<br>
+                        struct brw_stage_prog_data *prog_data,<br>
+                        size_t prog_data_size)<br>
+{<br>
+   /* Write program to blob. */<br>
+   blob_write_uint32(binary, program_size);<br>
+   blob_write_bytes(binary, program, program_size);<br>
+<br>
+   /* Write program_data to blob. */<br>
+   blob_write_uint32(binary, prog_data_size);<br>
+   blob_write_bytes(binary, prog_data, prog_data_size);<br>
+<br>
+   /* Write push params */<br>
+   blob_write_bytes(binary, prog_data->param,<br>
+                    sizeof(uint32_t) * prog_data->nr_params);<br>
+<br>
+   /* Write pull params */<br>
+   blob_write_bytes(binary, prog_data->pull_param,<br>
+                    sizeof(uint32_t) * prog_data->nr_pull_params);<br>
+}<br>
+<br>
+static bool<br>
+read_blob_program_data(struct blob_reader *binary,<br>
+                       struct gl_program *prog, gl_shader_stage stage,<br>
+                       const uint8_t **program, size_t *program_size,<br>
+                       struct brw_stage_prog_data *prog_data)<br>
+{<br>
+   /* Read shader program from blob. */<br>
+   *program_size = blob_read_uint32(binary);<br>
+   *program = blob_read_bytes(binary, *program_size);<br>
+<br>
+   /* Read shader prog_data from blob. */<br>
+   size_t prog_data_size = blob_read_uint32(binary);<br>
+   if (binary->overrun || prog_data_size != brw_prog_data_size(stage))<br>
+      return false;<br>
+   blob_copy_bytes(binary, prog_data, prog_data_size);<br>
+   if (binary->overrun)<br>
+      return false;<br>
+<br>
+   /* Read push params */<br>
+   prog_data->param = rzalloc_array(NULL, uint32_t, prog_data->nr_params);<br>
+   blob_copy_bytes(binary, prog_data->param,<br>
+                   sizeof(uint32_t) * prog_data->nr_params);<br>
+<br>
+   /* Read pull params */<br>
+   prog_data->pull_param = rzalloc_array(NULL, uint32_t,<br>
+                                         prog_data->nr_pull_params);<br>
+   blob_copy_bytes(binary, prog_data->pull_param,<br>
+                   sizeof(uint32_t) * prog_data->nr_pull_params);<br>
+<br>
+   return (binary->current == binary->end && !binary->overrun);<br>
+}<br>
+<br>
+static bool<br>
+read_and_upload(struct brw_context *brw, struct disk_cache *cache,<br>
+                struct gl_program *prog, gl_shader_stage stage)<br>
+{<br>
+   unsigned char binary_sha1[20];<br>
+<br>
+   brw_any_prog_key prog_key;<br>
+<br>
+   switch (stage) {<br>
+   case MESA_SHADER_VERTEX:<br>
+      brw_vs_populate_key(brw, &prog_key.vs);<br>
+      /* We don't care what instance of the program it is for the disk cache<br>
+       * hash lookup, so set the id to 0 for the sha1 hashing.<br>
+       * program_string_id will be set by the SET_UPLOAD_PARAMS macro below.<br>
+       */<br>
+      prog_key.vs.program_string_id = 0;<br>
+      break;<br>
+   case MESA_SHADER_FRAGMENT:<br>
+      brw_wm_populate_key(brw, &prog_key.wm);<br>
+      prog_key.wm.program_string_id = 0;<br>
+      break;<br>
+   default:<br>
+      unreachable("Unsupported stage!");<br>
+   }<br>
+<br>
+   gen_shader_sha1(brw, prog, stage, &prog_key, binary_sha1);<br>
+<br>
+   size_t buffer_size;<br>
+   uint8_t *buffer = disk_cache_get(cache, binary_sha1, &buffer_size);<br>
+   if (buffer == NULL) {<br>
+      if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {<br>
+         char sha1_buf[41];<br>
+         _mesa_sha1_format(sha1_buf, binary_sha1);<br>
+         fprintf(stderr, "No cached %s binary found for: %s\n",<br>
+                 _mesa_shader_stage_to_abbrev(<wbr>stage), sha1_buf);<br>
+      }<br>
+      return false;<br>
+   }<br>
+<br>
+   if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {<br>
+      char sha1_buf[41];<br>
+      _mesa_sha1_format(sha1_buf, binary_sha1);<br>
+      fprintf(stderr, "attempting to populate bo cache with binary: %s\n",<br>
+              sha1_buf);<br>
+   }<br>
+<br>
+   struct blob_reader binary;<br>
+   blob_reader_init(&binary, buffer, buffer_size);<br>
+<br>
+   size_t program_size;<br>
+   const uint8_t *program;<br>
+   struct brw_stage_prog_data *prog_data =<br>
+      ralloc_size(NULL, sizeof(brw_any_prog_data));<br>
+   if (!read_blob_program_data(&<wbr>binary, prog, stage, &program, &program_size,<br>
+                               prog_data)) {<br>
+      /* Something very bad has gone wrong discard the item from the cache and<br>
+       * rebuild from source.<br>
+       */<br>
+      assert(!"Invalid i965 shader disk cache item!");<br>
+<br>
+      if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {<br>
+         fprintf(stderr, "Error reading program from cache (invalid i965 "<br>
+                 "cache item)\n"); <br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      }<br>
+<br>
+      disk_cache_remove(cache, binary_sha1);<br>
+      free(buffer);<br>
+      return false;<br>
+   }<br>
+<br>
+   const struct gen_device_info *devinfo = &brw->screen->devinfo;<br>
+   enum brw_cache_id cache_id;<br>
+   unsigned max_threads;<br>
+   struct brw_stage_state *stage_state;<br>
+<br>
+   #define SET_UPLOAD_PARAMS(sh, sh_caps, prog)                      \<br>
+      do {                                                           \<br>
+         prog_key.sh.program_string_id = prog->id;                   \<br>
+         cache_id = BRW_CACHE_##sh_caps##_PROG;                      \<br>
+         max_threads = devinfo->max_##sh##_threads;                  \<br>
+         stage_state = &brw->sh.base;                                \<br>
+      } while(0)<br>
+<br>
+   switch (stage) {<br>
+   case MESA_SHADER_VERTEX: {<br>
+      struct brw_program *vp = (struct brw_program *) prog;<br></blockquote><div><br></div><div>I think you can move the cast inside the macro.  It's all brw_program as far as I can see.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      SET_UPLOAD_PARAMS(vs, VS, vp);<br>
+      break;<br>
+   }<br>
+   case MESA_SHADER_FRAGMENT: {<br>
+      struct brw_program *wp = (struct brw_program *) prog;<br>
+      SET_UPLOAD_PARAMS(wm, FS, wp);<br>
+      break;<br>
+   }<br>
+   default:<br>
+      unreachable("Unsupported stage!");<br>
+   }<br>
+<br>
+   brw_alloc_stage_scratch(brw, stage_state, prog_data->total_scratch,<br>
+                           max_threads);<br>
+<br>
+   brw_upload_cache(&brw->cache, cache_id, &prog_key, brw_prog_key_size(stage),<br>
+                    program, program_size, prog_data, brw_prog_data_size(stage),<br>
+                    &stage_state->prog_offset, &stage_state->prog_data);<br>
+<br>
+   prog->program_written_to_cache = true;<br>
+<br>
+   free(buffer);<br>
+<br>
+   return true;<br>
+}<br>
+<br>
+bool<br>
+brw_disk_cache_upload_<wbr>program(struct brw_context *brw, gl_shader_stage stage)<br>
+{<br>
+   struct disk_cache *cache = brw->ctx.Cache;<br>
+   if (cache == NULL)<br>
+      return false;<br>
+<br>
+   struct gl_program *prog = brw->ctx._Shader-><wbr>CurrentProgram[stage];<br>
+   if (prog == NULL)<br>
+      return false;<br>
+<br>
+   if (prog->sh.data->LinkStatus != linking_skipped)<br>
+      goto FAIL;<br>
+<br>
+   if (!read_and_upload(brw, cache, prog, stage))<br>
+      goto FAIL;<br>
+<br>
+   if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {<br>
+      fprintf(stderr, "read gen program from cache\n");<br>
+   }<br>
+<br>
+   return true;<br>
+<br>
+FAIL:<br>
+   /*FIXME: Fall back and compile from source here. */<br>
+   return false;<br>
+}<br>
+<br>
+static void<br>
+write_program_data(struct brw_context *brw, struct gl_program *prog,<br>
+                   void *key, struct brw_stage_prog_data *prog_data,<br>
+                   size_t program_size, size_t prog_data_size,<br>
+                   uint32_t prog_offset, struct disk_cache *cache,<br>
+                   gl_shader_stage stage)<br>
+{<br>
+   struct blob binary;<br>
+   blob_init(&binary);<br>
+<br>
+   const void *program_map;<br>
+   if (brw->screen->devinfo.has_llc) {<br>
+      program_map = brw->cache.map + prog_offset;<br>
+   } else {<br>
+      program_map = brw_bo_map(brw, brw-><a href="http://cache.bo" rel="noreferrer" target="_blank">cache.bo</a>, MAP_READ);<br>
+      if (unlikely(!program_map)) {<br>
+         _mesa_error_no_memory(__func__<wbr>);<br>
+         return;<br>
+      }<br>
+      program_map += prog_offset;<br>
+   }<br>
+<br>
+   write_blob_program_data(&<wbr>binary, program_map, program_size, prog_data,<br>
+                           prog_data_size);<br>
+<br>
+   if (!brw->screen->devinfo.has_<wbr>llc) {<br>
+      brw_bo_unmap(brw-><a href="http://cache.bo" rel="noreferrer" target="_blank">cache.bo</a>);<br>
+   }<br>
+<br>
+   unsigned char sha1[20];<br>
+   char buf[41];<br>
+   gen_shader_sha1(brw, prog, stage, key, sha1);<br>
+   _mesa_sha1_format(buf, sha1);<br>
+   if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {<br>
+      fprintf(stderr, "putting binary in cache: %s\n", buf);<br>
+   }<br>
+<br>
+   disk_cache_put(cache, sha1, binary.data, binary.size, NULL);<br>
+<br>
+   prog->program_written_to_cache = true;<br>
+   blob_finish(&binary);<br>
+}<br>
+<br>
+void<br>
+brw_disk_cache_write_program(<wbr>struct brw_context *brw)<br>
+{<br>
+   struct disk_cache *cache = brw->ctx.Cache;<br>
+   if (cache == NULL)<br>
+      return;<br>
+<br>
+   struct gl_program *prog =<br>
+      brw->ctx._Shader-><wbr>CurrentProgram[MESA_SHADER_<wbr>VERTEX];<br>
+   if (prog && !prog->program_written_to_<wbr>cache) {<br>
+      struct brw_vs_prog_key vs_key;<br>
+      brw_vs_populate_key(brw, &vs_key);<br>
+      vs_key.program_string_id = 0;<br>
+<br>
+      write_program_data(brw, prog, &vs_key, brw->vs.base.prog_data,<br>
+                         brw->vs.base.prog_data-><wbr>program_size,<br>
+                         sizeof(struct brw_vs_prog_data),<br>
+                         brw->vs.base.prog_offset, cache,<br>
+                         MESA_SHADER_VERTEX);<br>
+   }<br>
+<br>
+   prog = brw->ctx._Shader-><wbr>CurrentProgram[MESA_SHADER_<wbr>FRAGMENT];<br>
+   if (prog && !prog->program_written_to_<wbr>cache) {<br>
+      struct brw_wm_prog_key wm_key;<br>
+      brw_wm_populate_key(brw, &wm_key);<br>
+      wm_key.program_string_id = 0;<br>
+<br>
+      write_program_data(brw, prog, &wm_key, brw->wm.base.prog_data,<br>
+                         brw->wm.base.prog_data-><wbr>program_size,<br>
+                         sizeof(struct brw_wm_prog_data),<br>
+                         brw->wm.base.prog_offset, cache,<br>
+                         MESA_SHADER_FRAGMENT);<br>
+   }<br>
+}<br>
diff --git a/src/mesa/drivers/dri/i965/<wbr>brw_state.h b/src/mesa/drivers/dri/i965/<wbr>brw_state.h<br>
index 8db354cf232..6f2e0501b4b 100644<br>
--- a/src/mesa/drivers/dri/i965/<wbr>brw_state.h<br>
+++ b/src/mesa/drivers/dri/i965/<wbr>brw_state.h<br>
@@ -131,6 +131,11 @@ void brw_upload_state_base_address(<wbr>struct brw_context *brw);<br>
 void gen8_write_pma_stall_bits(<wbr>struct brw_context *brw,<br>
                                uint32_t pma_stall_bits);<br>
<br>
+/* brw_disk_cache.c */<br>
+bool brw_disk_cache_upload_program(<wbr>struct brw_context *brw,<br>
+                                   gl_shader_stage stage);<br>
+void brw_disk_cache_write_program(<wbr>struct brw_context *brw);<br>
+<br>
 /*****************************<wbr>******************************<wbr>************<br>
  * brw_state.c<br>
  */<br>
diff --git a/src/mesa/drivers/dri/i965/<wbr>meson.build b/src/mesa/drivers/dri/i965/<wbr>meson.build<br>
index 144a254bd64..09e1179adc4 100644<br>
--- a/src/mesa/drivers/dri/i965/<wbr>meson.build<br>
+++ b/src/mesa/drivers/dri/i965/<wbr>meson.build<br>
@@ -34,6 +34,7 @@ files_i965 = files(<br>
   'brw_cs.h',<br>
   'brw_curbe.c',<br>
   'brw_defines.h',<br>
+  'brw_disk_cache.c',<br>
   'brw_draw.c',<br>
   'brw_draw.h',<br>
   'brw_draw_upload.c',<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.15.0.rc0<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>