[Mesa-dev] [PATCH 01/77] i965: move program id generation

Timothy Arceri timothy.arceri at collabora.com
Mon Oct 3 06:04:20 UTC 2016


This generates the program ids at cache upload time rather than at
program creation time.

Moving the id generation here will be useful for on-disk shader
cache support because it means we don't generate ids if there was
a cache miss and we had to fall back to compiling from source.
This increases the likelyhood of finding a match.
---
 src/intel/blorp/blorp.h                     |  2 +-
 src/intel/blorp/blorp_blit.c                |  2 +-
 src/mesa/drivers/dri/i965/brw_blorp.c       | 14 +++---
 src/mesa/drivers/dri/i965/brw_clip.c        |  2 +-
 src/mesa/drivers/dri/i965/brw_cs.c          |  2 +-
 src/mesa/drivers/dri/i965/brw_ff_gs.c       |  2 +-
 src/mesa/drivers/dri/i965/brw_gs.c          |  2 +-
 src/mesa/drivers/dri/i965/brw_program.c     | 31 +-----------
 src/mesa/drivers/dri/i965/brw_sf.c          |  2 +-
 src/mesa/drivers/dri/i965/brw_state.h       |  4 +-
 src/mesa/drivers/dri/i965/brw_state_cache.c | 75 ++++++++++++++++++++++++++++-
 src/mesa/drivers/dri/i965/brw_tcs.c         |  2 +-
 src/mesa/drivers/dri/i965/brw_tes.c         |  2 +-
 src/mesa/drivers/dri/i965/brw_vs.c          |  3 +-
 src/mesa/drivers/dri/i965/brw_wm.c          |  2 +-
 15 files changed, 96 insertions(+), 51 deletions(-)

diff --git a/src/intel/blorp/blorp.h b/src/intel/blorp/blorp.h
index 480f1bb..39ebe09 100644
--- a/src/intel/blorp/blorp.h
+++ b/src/intel/blorp/blorp.h
@@ -55,7 +55,7 @@ struct blorp_context {
                          const void *key, uint32_t key_size,
                          uint32_t *kernel_out, void *prog_data_out);
    void (*upload_shader)(struct blorp_context *blorp,
-                         const void *key, uint32_t key_size,
+                         void *key, uint32_t key_size,
                          const void *kernel, uint32_t kernel_size,
                          const void *prog_data, uint32_t prog_data_size,
                          uint32_t *kernel_out, void *prog_data_out);
diff --git a/src/intel/blorp/blorp_blit.c b/src/intel/blorp/blorp_blit.c
index af46389..7a17cb4 100644
--- a/src/intel/blorp/blorp_blit.c
+++ b/src/intel/blorp/blorp_blit.c
@@ -1227,7 +1227,7 @@ brw_blorp_build_nir_shader(struct blorp_context *blorp,
 static void
 brw_blorp_get_blit_kernel(struct blorp_context *blorp,
                           struct blorp_params *params,
-                          const struct brw_blorp_blit_prog_key *prog_key)
+                          struct brw_blorp_blit_prog_key *prog_key)
 {
    if (blorp->lookup_shader(blorp, prog_key, sizeof(*prog_key),
                             &params->wm_prog_kernel, &params->wm_prog_data))
diff --git a/src/mesa/drivers/dri/i965/brw_blorp.c b/src/mesa/drivers/dri/i965/brw_blorp.c
index 23fe3aa..75d81c1 100644
--- a/src/mesa/drivers/dri/i965/brw_blorp.c
+++ b/src/mesa/drivers/dri/i965/brw_blorp.c
@@ -49,16 +49,16 @@ brw_blorp_lookup_shader(struct blorp_context *blorp,
 }
 
 static void
-brw_blorp_upload_shader(struct blorp_context *blorp,
-                        const void *key, uint32_t key_size,
-                        const void *kernel, uint32_t kernel_size,
-                        const void *prog_data, uint32_t prog_data_size,
-                        uint32_t *kernel_out, void *prog_data_out)
+brw_blorp_upload_shader(struct blorp_context *blorp, void *key,
+                        uint32_t key_size, const void *kernel,
+                        uint32_t kernel_size, const void *prog_data,
+                        uint32_t prog_data_size, uint32_t *kernel_out,
+                        void *prog_data_out)
 {
    struct brw_context *brw = blorp->driver_ctx;
-   brw_upload_cache(&brw->cache, BRW_CACHE_BLORP_PROG, key, key_size,
+   brw_upload_cache(&brw->cache, BRW_CACHE_BLORP_PROG, (void *) key, key_size,
                     kernel, kernel_size, prog_data, prog_data_size,
-                    kernel_out, prog_data_out);
+                    kernel_out, prog_data_out, NULL);
 }
 
 void
diff --git a/src/mesa/drivers/dri/i965/brw_clip.c b/src/mesa/drivers/dri/i965/brw_clip.c
index 1134fa4..450dfb6 100644
--- a/src/mesa/drivers/dri/i965/brw_clip.c
+++ b/src/mesa/drivers/dri/i965/brw_clip.c
@@ -126,7 +126,7 @@ static void compile_clip_prog( struct brw_context *brw,
 		    &c.key, sizeof(c.key),
 		    program, program_size,
 		    &c.prog_data, sizeof(c.prog_data),
-		    &brw->clip.prog_offset, &brw->clip.prog_data);
+                    &brw->clip.prog_offset, &brw->clip.prog_data, NULL);
    ralloc_free(mem_ctx);
 }
 
diff --git a/src/mesa/drivers/dri/i965/brw_cs.c b/src/mesa/drivers/dri/i965/brw_cs.c
index 4e746fe..5d7ad92 100644
--- a/src/mesa/drivers/dri/i965/brw_cs.c
+++ b/src/mesa/drivers/dri/i965/brw_cs.c
@@ -180,7 +180,7 @@ brw_codegen_cs_prog(struct brw_context *brw,
                     key, sizeof(*key),
                     program, program_size,
                     &prog_data, sizeof(prog_data),
-                    &brw->cs.base.prog_offset, &brw->cs.prog_data);
+                    &brw->cs.base.prog_offset, &brw->cs.prog_data, cp);
    ralloc_free(mem_ctx);
 
    return true;
diff --git a/src/mesa/drivers/dri/i965/brw_ff_gs.c b/src/mesa/drivers/dri/i965/brw_ff_gs.c
index d8eb146..7dbdbe9 100644
--- a/src/mesa/drivers/dri/i965/brw_ff_gs.c
+++ b/src/mesa/drivers/dri/i965/brw_ff_gs.c
@@ -144,7 +144,7 @@ brw_codegen_ff_gs_prog(struct brw_context *brw,
 		    &c.key, sizeof(c.key),
 		    program, program_size,
 		    &c.prog_data, sizeof(c.prog_data),
-		    &brw->ff_gs.prog_offset, &brw->ff_gs.prog_data);
+		    &brw->ff_gs.prog_offset, &brw->ff_gs.prog_data, NULL);
    ralloc_free(mem_ctx);
 }
 
diff --git a/src/mesa/drivers/dri/i965/brw_gs.c b/src/mesa/drivers/dri/i965/brw_gs.c
index 9c37d96..7b668ba 100644
--- a/src/mesa/drivers/dri/i965/brw_gs.c
+++ b/src/mesa/drivers/dri/i965/brw_gs.c
@@ -191,7 +191,7 @@ brw_codegen_gs_prog(struct brw_context *brw,
                     key, sizeof(*key),
                     program, program_size,
                     &prog_data, sizeof(prog_data),
-                    &stage_state->prog_offset, &brw->gs.prog_data);
+                    &stage_state->prog_offset, &brw->gs.prog_data, gp);
    ralloc_free(mem_ctx);
 
    return true;
diff --git a/src/mesa/drivers/dri/i965/brw_program.c b/src/mesa/drivers/dri/i965/brw_program.c
index a41f36e..31d99e2 100644
--- a/src/mesa/drivers/dri/i965/brw_program.c
+++ b/src/mesa/drivers/dri/i965/brw_program.c
@@ -29,7 +29,6 @@
   *   Keith Whitwell <keithw at vmware.com>
   */
 
-#include <pthread.h>
 #include "main/imports.h"
 #include "program/prog_parameter.h"
 #include "program/prog_print.h"
@@ -113,29 +112,15 @@ brw_create_nir(struct brw_context *brw,
    return nir;
 }
 
-static unsigned
-get_new_program_id(struct intel_screen *screen)
-{
-   static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
-   pthread_mutex_lock(&m);
-   unsigned id = screen->program_id++;
-   pthread_mutex_unlock(&m);
-   return id;
-}
-
 static struct gl_program *brwNewProgram( struct gl_context *ctx,
 				      GLenum target,
 				      GLuint id )
 {
-   struct brw_context *brw = brw_context(ctx);
-
    switch (target) {
    case GL_VERTEX_PROGRAM_ARB: {
       struct brw_vertex_program *prog = CALLOC_STRUCT(brw_vertex_program);
       if (prog) {
-	 prog->id = get_new_program_id(brw->screen);
-
-	 return _mesa_init_gl_program(&prog->program.Base, target, id);
+         return _mesa_init_gl_program(&prog->program.Base, target, id);
       }
       else
 	 return NULL;
@@ -144,9 +129,7 @@ static struct gl_program *brwNewProgram( struct gl_context *ctx,
    case GL_FRAGMENT_PROGRAM_ARB: {
       struct brw_fragment_program *prog = CALLOC_STRUCT(brw_fragment_program);
       if (prog) {
-	 prog->id = get_new_program_id(brw->screen);
-
-	 return _mesa_init_gl_program(&prog->program.Base, target, id);
+         return _mesa_init_gl_program(&prog->program.Base, target, id);
       }
       else
 	 return NULL;
@@ -155,8 +138,6 @@ static struct gl_program *brwNewProgram( struct gl_context *ctx,
    case GL_GEOMETRY_PROGRAM_NV: {
       struct brw_geometry_program *prog = CALLOC_STRUCT(brw_geometry_program);
       if (prog) {
-         prog->id = get_new_program_id(brw->screen);
-
          return _mesa_init_gl_program(&prog->program.Base, target, id);
       } else {
          return NULL;
@@ -166,8 +147,6 @@ static struct gl_program *brwNewProgram( struct gl_context *ctx,
    case GL_TESS_CONTROL_PROGRAM_NV: {
       struct brw_tess_ctrl_program *prog = CALLOC_STRUCT(brw_tess_ctrl_program);
       if (prog) {
-         prog->id = get_new_program_id(brw->screen);
-
          return _mesa_init_gl_program(&prog->program.Base, target, id);
       } else {
          return NULL;
@@ -177,8 +156,6 @@ static struct gl_program *brwNewProgram( struct gl_context *ctx,
    case GL_TESS_EVALUATION_PROGRAM_NV: {
       struct brw_tess_eval_program *prog = CALLOC_STRUCT(brw_tess_eval_program);
       if (prog) {
-         prog->id = get_new_program_id(brw->screen);
-
          return _mesa_init_gl_program(&prog->program.Base, target, id);
       } else {
          return NULL;
@@ -188,8 +165,6 @@ static struct gl_program *brwNewProgram( struct gl_context *ctx,
    case GL_COMPUTE_PROGRAM_NV: {
       struct brw_compute_program *prog = CALLOC_STRUCT(brw_compute_program);
       if (prog) {
-         prog->id = get_new_program_id(brw->screen);
-
          return _mesa_init_gl_program(&prog->program.Base, target, id);
       } else {
          return NULL;
@@ -225,7 +200,6 @@ brwProgramStringNotify(struct gl_context *ctx,
 
       if (newFP == curFP)
 	 brw->ctx.NewDriverState |= BRW_NEW_FRAGMENT_PROGRAM;
-      newFP->id = get_new_program_id(brw->screen);
 
       brw_add_texrect_params(prog);
 
@@ -245,7 +219,6 @@ brwProgramStringNotify(struct gl_context *ctx,
       if (newVP->program.IsPositionInvariant) {
 	 _mesa_insert_mvp_code(ctx, &newVP->program);
       }
-      newVP->id = get_new_program_id(brw->screen);
 
       /* Also tell tnl about it:
        */
diff --git a/src/mesa/drivers/dri/i965/brw_sf.c b/src/mesa/drivers/dri/i965/brw_sf.c
index 6d8cd74..aaaccd1 100644
--- a/src/mesa/drivers/dri/i965/brw_sf.c
+++ b/src/mesa/drivers/dri/i965/brw_sf.c
@@ -127,7 +127,7 @@ static void compile_sf_prog( struct brw_context *brw,
 		    &c.key, sizeof(c.key),
 		    program, program_size,
 		    &c.prog_data, sizeof(c.prog_data),
-		    &brw->sf.prog_offset, &brw->sf.prog_data);
+                    &brw->sf.prog_offset, &brw->sf.prog_data, NULL);
    ralloc_free(mem_ctx);
 }
 
diff --git a/src/mesa/drivers/dri/i965/brw_state.h b/src/mesa/drivers/dri/i965/brw_state.h
index b42b9af..aba9508 100644
--- a/src/mesa/drivers/dri/i965/brw_state.h
+++ b/src/mesa/drivers/dri/i965/brw_state.h
@@ -224,13 +224,13 @@ brw_select_pipeline(struct brw_context *brw, enum brw_pipeline pipeline)
 
 void brw_upload_cache(struct brw_cache *cache,
 		      enum brw_cache_id cache_id,
-		      const void *key,
+                      void *key,
 		      GLuint key_sz,
 		      const void *data,
 		      GLuint data_sz,
 		      const void *aux,
 		      GLuint aux_sz,
-		      uint32_t *out_offset, void *out_aux);
+                      uint32_t *out_offset, void *out_aux, void *prog);
 
 bool brw_search_cache(struct brw_cache *cache,
 		      enum brw_cache_id cache_id,
diff --git a/src/mesa/drivers/dri/i965/brw_state_cache.c b/src/mesa/drivers/dri/i965/brw_state_cache.c
index 0e98e65..691587d 100644
--- a/src/mesa/drivers/dri/i965/brw_state_cache.c
+++ b/src/mesa/drivers/dri/i965/brw_state_cache.c
@@ -44,6 +44,7 @@
  * big we throw out all of the cache data and let it get regenerated.
  */
 
+#include <pthread.h>
 #include "main/imports.h"
 #include "intel_batchbuffer.h"
 #include "brw_state.h"
@@ -55,6 +56,16 @@
 
 #define FILE_DEBUG_FLAG DEBUG_STATE
 
+static unsigned
+get_new_program_id(struct intel_screen *screen)
+{
+   static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
+   pthread_mutex_lock(&m);
+   unsigned id = screen->program_id++;
+   pthread_mutex_unlock(&m);
+   return id;
+}
+
 static GLuint
 hash_key(struct brw_cache_item *item)
 {
@@ -271,14 +282,15 @@ brw_alloc_item_data(struct brw_cache *cache, uint32_t size)
 void
 brw_upload_cache(struct brw_cache *cache,
 		 enum brw_cache_id cache_id,
-		 const void *key,
+                 void *key,
 		 GLuint key_size,
 		 const void *data,
 		 GLuint data_size,
 		 const void *aux,
 		 GLuint aux_size,
 		 uint32_t *out_offset,
-		 void *out_aux)
+                 void *out_aux,
+                 void *prog)
 {
    struct brw_context *brw = cache->brw;
    struct brw_cache_item *item = CALLOC_STRUCT(brw_cache_item);
@@ -287,6 +299,65 @@ brw_upload_cache(struct brw_cache *cache,
    GLuint hash;
    void *tmp;
 
+   switch (cache_id) {
+   case BRW_CACHE_VS_PROG: {
+      struct brw_vertex_program *p = (struct brw_vertex_program *) prog;
+      if (p->id == 0) {
+         struct brw_vs_prog_key *k = (struct brw_vs_prog_key *) key;
+         p->id = get_new_program_id(brw->screen);
+         k->program_string_id = p->id;
+      }
+      break;
+   }
+   case BRW_CACHE_TCS_PROG: {
+      struct brw_tess_ctrl_program *p = (struct brw_tess_ctrl_program *) prog;
+      if (p && p->id == 0) {
+         struct brw_tcs_prog_key *k = (struct brw_tcs_prog_key *) key;
+         p->id = get_new_program_id(brw->screen);
+         k->program_string_id = p->id;
+      }
+      break;
+   }
+   case BRW_CACHE_TES_PROG: {
+      struct brw_tess_eval_program *p = (struct brw_tess_eval_program *) prog;
+      if (p->id == 0) {
+         struct brw_tes_prog_key *k = (struct brw_tes_prog_key *) key;
+         p->id = get_new_program_id(brw->screen);
+         k->program_string_id = p->id;
+      }
+      break;
+   }
+   case BRW_CACHE_GS_PROG: {
+      struct brw_geometry_program *p = (struct brw_geometry_program *) prog;
+      if (p->id == 0) {
+         struct brw_gs_prog_key *k = (struct brw_gs_prog_key *) key;
+         p->id = get_new_program_id(brw->screen);
+         k->program_string_id = p->id;
+      }
+      break;
+   }
+   case BRW_CACHE_FS_PROG: {
+      struct brw_fragment_program *p = (struct brw_fragment_program *) prog;
+      if (p->id == 0) {
+         struct brw_wm_prog_key *k = (struct brw_wm_prog_key *) key;
+         p->id = get_new_program_id(brw->screen);
+         k->program_string_id = p->id;
+      }
+      break;
+   }
+   case BRW_CACHE_CS_PROG: {
+      struct brw_compute_program *p = (struct brw_compute_program *) prog;
+      if (p->id == 0) {
+         struct brw_cs_prog_key *k = (struct brw_cs_prog_key *) key;
+         p->id = get_new_program_id(brw->screen);
+         k->program_string_id = p->id;
+      }
+      break;
+   }
+   default:
+      ; /* Do nothing */
+   }
+
    item->cache_id = cache_id;
    item->size = data_size;
    item->key = key;
diff --git a/src/mesa/drivers/dri/i965/brw_tcs.c b/src/mesa/drivers/dri/i965/brw_tcs.c
index 1f1b1a5..da8ef12 100644
--- a/src/mesa/drivers/dri/i965/brw_tcs.c
+++ b/src/mesa/drivers/dri/i965/brw_tcs.c
@@ -304,7 +304,7 @@ brw_codegen_tcs_prog(struct brw_context *brw,
                     key, sizeof(*key),
                     program, program_size,
                     &prog_data, sizeof(prog_data),
-                    &stage_state->prog_offset, &brw->tcs.prog_data);
+                    &stage_state->prog_offset, &brw->tcs.prog_data, tcp);
    ralloc_free(mem_ctx);
    if (!tcs)
       ralloc_free(nir);
diff --git a/src/mesa/drivers/dri/i965/brw_tes.c b/src/mesa/drivers/dri/i965/brw_tes.c
index ad0eb2e..634d685 100644
--- a/src/mesa/drivers/dri/i965/brw_tes.c
+++ b/src/mesa/drivers/dri/i965/brw_tes.c
@@ -223,7 +223,7 @@ brw_codegen_tes_prog(struct brw_context *brw,
                     key, sizeof(*key),
                     program, program_size,
                     &prog_data, sizeof(prog_data),
-                    &stage_state->prog_offset, &brw->tes.prog_data);
+                    &stage_state->prog_offset, &brw->tes.prog_data, tep);
    ralloc_free(mem_ctx);
 
    return true;
diff --git a/src/mesa/drivers/dri/i965/brw_vs.c b/src/mesa/drivers/dri/i965/brw_vs.c
index 86cdbe0..a2f82d3 100644
--- a/src/mesa/drivers/dri/i965/brw_vs.c
+++ b/src/mesa/drivers/dri/i965/brw_vs.c
@@ -229,7 +229,8 @@ brw_codegen_vs_prog(struct brw_context *brw,
 		    key, sizeof(struct brw_vs_prog_key),
 		    program, program_size,
 		    &prog_data, sizeof(prog_data),
-		    &brw->vs.base.prog_offset, &brw->vs.prog_data);
+		    &brw->vs.base.prog_offset, &brw->vs.prog_data, vp);
+
    ralloc_free(mem_ctx);
 
    return true;
diff --git a/src/mesa/drivers/dri/i965/brw_wm.c b/src/mesa/drivers/dri/i965/brw_wm.c
index 64cfd98..488106e 100644
--- a/src/mesa/drivers/dri/i965/brw_wm.c
+++ b/src/mesa/drivers/dri/i965/brw_wm.c
@@ -182,7 +182,7 @@ brw_codegen_wm_prog(struct brw_context *brw,
 		    key, sizeof(struct brw_wm_prog_key),
 		    program, program_size,
 		    &prog_data, sizeof(prog_data),
-		    &brw->wm.base.prog_offset, &brw->wm.prog_data);
+		    &brw->wm.base.prog_offset, &brw->wm.prog_data, fp);
 
    ralloc_free(mem_ctx);
 
-- 
2.7.4



More information about the mesa-dev mailing list