i965: Make a helper for finding an existing shader variant.

We had five copies of the same "walk the cache and look for an
existing shader variant for this program" code.  Now we have one
helper function that returns the key.

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eduardo Lima Mitev <elima@igalia.com>
diff --git a/src/mesa/drivers/dri/i965/brw_gs.c b/src/mesa/drivers/dri/i965/brw_gs.c
index b7fb9f9..2996203 100644
--- a/src/mesa/drivers/dri/i965/brw_gs.c
+++ b/src/mesa/drivers/dri/i965/brw_gs.c
@@ -40,26 +40,14 @@
 brw_gs_debug_recompile(struct brw_context *brw, struct gl_program *prog,
                        const struct brw_gs_prog_key *key)
 {
-   struct brw_cache_item *c = NULL;
-   const struct brw_gs_prog_key *old_key = NULL;
-   bool found = false;
-
    perf_debug("Recompiling geometry shader for program %d\n", prog->Id);
 
-   for (unsigned int i = 0; i < brw->cache.size; i++) {
-      for (c = brw->cache.items[i]; c; c = c->next) {
-         if (c->cache_id == BRW_CACHE_GS_PROG) {
-            old_key = c->key;
+   bool found = false;
+   const struct brw_gs_prog_key *old_key =
+      brw_find_previous_compile(&brw->cache, BRW_CACHE_GS_PROG,
+                                key->program_string_id);
 
-            if (old_key->program_string_id == key->program_string_id)
-               break;
-         }
-      }
-      if (c)
-         break;
-   }
-
-   if (!c) {
+   if (!old_key) {
       perf_debug("  Didn't find previous compile in the shader cache for "
                  "debug\n");
       return;
diff --git a/src/mesa/drivers/dri/i965/brw_program_cache.c b/src/mesa/drivers/dri/i965/brw_program_cache.c
index 3947904a..3d95372 100644
--- a/src/mesa/drivers/dri/i965/brw_program_cache.c
+++ b/src/mesa/drivers/dri/i965/brw_program_cache.c
@@ -55,6 +55,27 @@
 
 #define FILE_DEBUG_FLAG DEBUG_STATE
 
+static unsigned
+get_program_string_id(enum brw_cache_id cache_id, const void *key)
+{
+   switch (cache_id) {
+   case BRW_CACHE_VS_PROG:
+      return ((struct brw_vs_prog_key *) key)->program_string_id;
+   case BRW_CACHE_TCS_PROG:
+      return ((struct brw_tcs_prog_key *) key)->program_string_id;
+   case BRW_CACHE_TES_PROG:
+      return ((struct brw_tes_prog_key *) key)->program_string_id;
+   case BRW_CACHE_GS_PROG:
+      return ((struct brw_gs_prog_key *) key)->program_string_id;
+   case BRW_CACHE_CS_PROG:
+      return ((struct brw_cs_prog_key *) key)->program_string_id;
+   case BRW_CACHE_FS_PROG:
+      return ((struct brw_wm_prog_key *) key)->program_string_id;
+   default:
+      unreachable("no program string id for this kind of program");
+   }
+}
+
 static GLuint
 hash_key(struct brw_cache_item *item)
 {
@@ -268,6 +289,23 @@
    return offset;
 }
 
+const void *
+brw_find_previous_compile(struct brw_cache *cache,
+                          enum brw_cache_id cache_id,
+                          unsigned program_string_id)
+{
+   for (unsigned i = 0; i < cache->size; i++) {
+      for (struct brw_cache_item *c = cache->items[i]; c; c = c->next) {
+         if (c->cache_id == cache_id &&
+             get_program_string_id(cache_id, c->key) == program_string_id) {
+            return c->key;
+         }
+      }
+   }
+
+   return NULL;
+}
+
 void
 brw_upload_cache(struct brw_cache *cache,
                  enum brw_cache_id cache_id,
diff --git a/src/mesa/drivers/dri/i965/brw_state.h b/src/mesa/drivers/dri/i965/brw_state.h
index 176557b..bd82212 100644
--- a/src/mesa/drivers/dri/i965/brw_state.h
+++ b/src/mesa/drivers/dri/i965/brw_state.h
@@ -235,6 +235,11 @@
                       const void *key,
                       GLuint key_size,
                       uint32_t *inout_offset, void *inout_aux);
+
+const void *brw_find_previous_compile(struct brw_cache *cache,
+                                      enum brw_cache_id cache_id,
+                                      unsigned program_string_id);
+
 void brw_program_cache_check_size(struct brw_context *brw);
 
 void brw_init_caches( struct brw_context *brw );
diff --git a/src/mesa/drivers/dri/i965/brw_tcs.c b/src/mesa/drivers/dri/i965/brw_tcs.c
index 78ad257..bbba749 100644
--- a/src/mesa/drivers/dri/i965/brw_tcs.c
+++ b/src/mesa/drivers/dri/i965/brw_tcs.c
@@ -120,27 +120,15 @@
 brw_tcs_debug_recompile(struct brw_context *brw, struct gl_program *prog,
                        const struct brw_tcs_prog_key *key)
 {
-   struct brw_cache_item *c = NULL;
-   const struct brw_tcs_prog_key *old_key = NULL;
-   bool found = false;
-
    perf_debug("Recompiling tessellation control shader for program %d\n",
               prog->Id);
 
-   for (unsigned int i = 0; i < brw->cache.size; i++) {
-      for (c = brw->cache.items[i]; c; c = c->next) {
-         if (c->cache_id == BRW_CACHE_TCS_PROG) {
-            old_key = c->key;
+   bool found = false;
+   const struct brw_tcs_prog_key *old_key =
+      brw_find_previous_compile(&brw->cache, BRW_CACHE_TCS_PROG,
+                                key->program_string_id);
 
-            if (old_key->program_string_id == key->program_string_id)
-               break;
-         }
-      }
-      if (c)
-         break;
-   }
-
-   if (!c) {
+   if (!old_key) {
       perf_debug("  Didn't find previous compile in the shader cache for "
                  "debug\n");
       return;
diff --git a/src/mesa/drivers/dri/i965/brw_tes.c b/src/mesa/drivers/dri/i965/brw_tes.c
index 57dcda7..cb12b9c 100644
--- a/src/mesa/drivers/dri/i965/brw_tes.c
+++ b/src/mesa/drivers/dri/i965/brw_tes.c
@@ -38,27 +38,15 @@
 brw_tes_debug_recompile(struct brw_context *brw, struct gl_program *prog,
                        const struct brw_tes_prog_key *key)
 {
-   struct brw_cache_item *c = NULL;
-   const struct brw_tes_prog_key *old_key = NULL;
-   bool found = false;
-
    perf_debug("Recompiling tessellation evaluation shader for program %d\n",
               prog->Id);
 
-   for (unsigned int i = 0; i < brw->cache.size; i++) {
-      for (c = brw->cache.items[i]; c; c = c->next) {
-         if (c->cache_id == BRW_CACHE_TES_PROG) {
-            old_key = c->key;
+   bool found = false;
+   const struct brw_tes_prog_key *old_key =
+      brw_find_previous_compile(&brw->cache, BRW_CACHE_TES_PROG,
+                                key->program_string_id);
 
-            if (old_key->program_string_id == key->program_string_id)
-               break;
-         }
-      }
-      if (c)
-         break;
-   }
-
-   if (!c) {
+   if (!old_key) {
       perf_debug("  Didn't find previous compile in the shader cache for "
                  "debug\n");
       return;
diff --git a/src/mesa/drivers/dri/i965/brw_vs.c b/src/mesa/drivers/dri/i965/brw_vs.c
index afb1f20..5d199393 100644
--- a/src/mesa/drivers/dri/i965/brw_vs.c
+++ b/src/mesa/drivers/dri/i965/brw_vs.c
@@ -88,26 +88,14 @@
 brw_vs_debug_recompile(struct brw_context *brw, struct gl_program *prog,
                        const struct brw_vs_prog_key *key)
 {
-   struct brw_cache_item *c = NULL;
-   const struct brw_vs_prog_key *old_key = NULL;
-   bool found = false;
-
    perf_debug("Recompiling vertex shader for program %d\n", prog->Id);
 
-   for (unsigned int i = 0; i < brw->cache.size; i++) {
-      for (c = brw->cache.items[i]; c; c = c->next) {
-         if (c->cache_id == BRW_CACHE_VS_PROG) {
-            old_key = c->key;
+   bool found = false;
+   const struct brw_vs_prog_key *old_key =
+      brw_find_previous_compile(&brw->cache, BRW_CACHE_VS_PROG,
+                                key->program_string_id);
 
-            if (old_key->program_string_id == key->program_string_id)
-               break;
-         }
-      }
-      if (c)
-         break;
-   }
-
-   if (!c) {
+   if (!old_key) {
       perf_debug("  Didn't find previous compile in the shader cache for "
                  "debug\n");
       return;
diff --git a/src/mesa/drivers/dri/i965/brw_wm.c b/src/mesa/drivers/dri/i965/brw_wm.c
index bd2b24a..a774720 100644
--- a/src/mesa/drivers/dri/i965/brw_wm.c
+++ b/src/mesa/drivers/dri/i965/brw_wm.c
@@ -70,26 +70,14 @@
 brw_wm_debug_recompile(struct brw_context *brw, struct gl_program *prog,
                        const struct brw_wm_prog_key *key)
 {
-   struct brw_cache_item *c = NULL;
-   const struct brw_wm_prog_key *old_key = NULL;
-   bool found = false;
-
    perf_debug("Recompiling fragment shader for program %d\n", prog->Id);
 
-   for (unsigned int i = 0; i < brw->cache.size; i++) {
-      for (c = brw->cache.items[i]; c; c = c->next) {
-         if (c->cache_id == BRW_CACHE_FS_PROG) {
-            old_key = c->key;
+   bool found = false;
+   const struct brw_wm_prog_key *old_key =
+      brw_find_previous_compile(&brw->cache, BRW_CACHE_FS_PROG,
+                                key->program_string_id);
 
-            if (old_key->program_string_id == key->program_string_id)
-               break;
-         }
-      }
-      if (c)
-         break;
-   }
-
-   if (!c) {
+   if (!old_key) {
       perf_debug("  Didn't find previous compile in the shader cache for debug\n");
       return;
    }