Re: [Mesa-dev] [PATCH 1/2] [swr] Add fetch shader cache

2017-02-21 Thread Cherniak, Bruce
Reviewed-by: Bruce Cherniak 

> On Feb 17, 2017, at 2:30 PM, George Kyriazis  
> wrote:
> 
> For now, the cache key is all of FETCH_COMPILE_STATE.
> 
> Use new/delete for swr_vertex_element_state, since we have to call the
> constructors/destructors of the struct elements.
> ---
> src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h |  2 +-
> src/gallium/drivers/swr/swr_draw.cpp  | 19 +++
> src/gallium/drivers/swr/swr_shader.cpp| 14 ++
> src/gallium/drivers/swr/swr_shader.h  | 15 +++
> src/gallium/drivers/swr/swr_state.cpp |  6 --
> src/gallium/drivers/swr/swr_state.h   |  9 +
> 6 files changed, 50 insertions(+), 15 deletions(-)
> 
> diff --git a/src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h 
> b/src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h
> index 1547453..622608a 100644
> --- a/src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h
> +++ b/src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h
> @@ -94,7 +94,7 @@ enum ComponentControl
> //
> struct FETCH_COMPILE_STATE
> {
> -uint32_t numAttribs;
> +uint32_t numAttribs {0};
> INPUT_ELEMENT_DESC layout[KNOB_NUM_ATTRIBUTES];
> SWR_FORMAT indexType;
> uint32_t cutIndex{ 0x };
> diff --git a/src/gallium/drivers/swr/swr_draw.cpp 
> b/src/gallium/drivers/swr/swr_draw.cpp
> index c4d5e5c..4bdd3bb 100644
> --- a/src/gallium/drivers/swr/swr_draw.cpp
> +++ b/src/gallium/drivers/swr/swr_draw.cpp
> @@ -141,19 +141,22 @@ swr_draw_vbo(struct pipe_context *pipe, const struct 
> pipe_draw_info *info)
>}
> 
>struct swr_vertex_element_state *velems = ctx->velems;
> -   if (!velems->fsFunc
> -   || (velems->fsState.cutIndex != info->restart_index)
> -   || (velems->fsState.bEnableCutIndex != info->primitive_restart)) {
> -
> -  velems->fsState.cutIndex = info->restart_index;
> -  velems->fsState.bEnableCutIndex = info->primitive_restart;
> -
> -  /* Create Fetch Shader */
> +   velems->fsState.cutIndex = info->restart_index;
> +   velems->fsState.bEnableCutIndex = info->primitive_restart;
> +
> +   swr_jit_fetch_key key;
> +   swr_generate_fetch_key(key, velems);
> +   auto search = velems->map.find(key);
> +   if (search != velems->map.end()) {
> +  velems->fsFunc = search->second;
> +   } else {
>   HANDLE hJitMgr = swr_screen(ctx->pipe.screen)->hJitMgr;
>   velems->fsFunc = JitCompileFetch(hJitMgr, velems->fsState);
> 
>   debug_printf("fetch shader %p\n", velems->fsFunc);
>   assert(velems->fsFunc && "Error: FetchShader = NULL");
> +
> +  velems->map.insert(std::make_pair(key, velems->fsFunc));
>}
> 
>SwrSetFetchFunc(ctx->swrContext, velems->fsFunc);
> diff --git a/src/gallium/drivers/swr/swr_shader.cpp 
> b/src/gallium/drivers/swr/swr_shader.cpp
> index 979a28b..676938c 100644
> --- a/src/gallium/drivers/swr/swr_shader.cpp
> +++ b/src/gallium/drivers/swr/swr_shader.cpp
> @@ -61,6 +61,11 @@ bool operator==(const swr_jit_vs_key , const 
> swr_jit_vs_key )
>return !memcmp(, , sizeof(lhs));
> }
> 
> +bool operator==(const swr_jit_fetch_key , const swr_jit_fetch_key )
> +{
> +   return !memcmp(, , sizeof(lhs));
> +}
> +
> static void
> swr_generate_sampler_key(const struct lp_tgsi_info ,
>  struct swr_context *ctx,
> @@ -157,6 +162,15 @@ swr_generate_vs_key(struct swr_jit_vs_key ,
>swr_generate_sampler_key(swr_vs->info, ctx, PIPE_SHADER_VERTEX, key);
> }
> 
> +void
> +swr_generate_fetch_key(struct swr_jit_fetch_key ,
> +   struct swr_vertex_element_state *velems)
> +{
> +   memset(, 0, sizeof(key));
> +
> +   key.fsState = velems->fsState;
> +}
> +
> struct BuilderSWR : public Builder {
>BuilderSWR(JitManager *pJitMgr, const char *pName)
>   : Builder(pJitMgr)
> diff --git a/src/gallium/drivers/swr/swr_shader.h 
> b/src/gallium/drivers/swr/swr_shader.h
> index 7e3399c..266573f 100644
> --- a/src/gallium/drivers/swr/swr_shader.h
> +++ b/src/gallium/drivers/swr/swr_shader.h
> @@ -42,6 +42,9 @@ void swr_generate_vs_key(struct swr_jit_vs_key ,
>  struct swr_context *ctx,
>  swr_vertex_shader *swr_vs);
> 
> +void swr_generate_fetch_key(struct swr_jit_fetch_key ,
> +struct swr_vertex_element_state *velems);
> +
> struct swr_jit_sampler_key {
>unsigned nr_samplers;
>unsigned nr_sampler_views;
> @@ -60,6 +63,10 @@ struct swr_jit_vs_key : swr_jit_sampler_key {
>unsigned clip_plane_mask; // from rasterizer state & vs_info
> };
> 
> +struct swr_jit_fetch_key {
> +   FETCH_COMPILE_STATE fsState;
> +};
> +
> namespace std
> {
> template <> struct hash {
> @@ -75,7 +82,15 @@ template <> struct hash {
>   return util_hash_crc32(, sizeof(k));
>}
> };
> +
> +template <> struct hash 

[Mesa-dev] [PATCH 1/2] [swr] Add fetch shader cache

2017-02-17 Thread George Kyriazis
For now, the cache key is all of FETCH_COMPILE_STATE.

Use new/delete for swr_vertex_element_state, since we have to call the
constructors/destructors of the struct elements.
---
 src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h |  2 +-
 src/gallium/drivers/swr/swr_draw.cpp  | 19 +++
 src/gallium/drivers/swr/swr_shader.cpp| 14 ++
 src/gallium/drivers/swr/swr_shader.h  | 15 +++
 src/gallium/drivers/swr/swr_state.cpp |  6 --
 src/gallium/drivers/swr/swr_state.h   |  9 +
 6 files changed, 50 insertions(+), 15 deletions(-)

diff --git a/src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h 
b/src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h
index 1547453..622608a 100644
--- a/src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h
+++ b/src/gallium/drivers/swr/rasterizer/jitter/fetch_jit.h
@@ -94,7 +94,7 @@ enum ComponentControl
 //
 struct FETCH_COMPILE_STATE
 {
-uint32_t numAttribs;
+uint32_t numAttribs {0};
 INPUT_ELEMENT_DESC layout[KNOB_NUM_ATTRIBUTES];
 SWR_FORMAT indexType;
 uint32_t cutIndex{ 0x };
diff --git a/src/gallium/drivers/swr/swr_draw.cpp 
b/src/gallium/drivers/swr/swr_draw.cpp
index c4d5e5c..4bdd3bb 100644
--- a/src/gallium/drivers/swr/swr_draw.cpp
+++ b/src/gallium/drivers/swr/swr_draw.cpp
@@ -141,19 +141,22 @@ swr_draw_vbo(struct pipe_context *pipe, const struct 
pipe_draw_info *info)
}
 
struct swr_vertex_element_state *velems = ctx->velems;
-   if (!velems->fsFunc
-   || (velems->fsState.cutIndex != info->restart_index)
-   || (velems->fsState.bEnableCutIndex != info->primitive_restart)) {
-
-  velems->fsState.cutIndex = info->restart_index;
-  velems->fsState.bEnableCutIndex = info->primitive_restart;
-
-  /* Create Fetch Shader */
+   velems->fsState.cutIndex = info->restart_index;
+   velems->fsState.bEnableCutIndex = info->primitive_restart;
+
+   swr_jit_fetch_key key;
+   swr_generate_fetch_key(key, velems);
+   auto search = velems->map.find(key);
+   if (search != velems->map.end()) {
+  velems->fsFunc = search->second;
+   } else {
   HANDLE hJitMgr = swr_screen(ctx->pipe.screen)->hJitMgr;
   velems->fsFunc = JitCompileFetch(hJitMgr, velems->fsState);
 
   debug_printf("fetch shader %p\n", velems->fsFunc);
   assert(velems->fsFunc && "Error: FetchShader = NULL");
+
+  velems->map.insert(std::make_pair(key, velems->fsFunc));
}
 
SwrSetFetchFunc(ctx->swrContext, velems->fsFunc);
diff --git a/src/gallium/drivers/swr/swr_shader.cpp 
b/src/gallium/drivers/swr/swr_shader.cpp
index 979a28b..676938c 100644
--- a/src/gallium/drivers/swr/swr_shader.cpp
+++ b/src/gallium/drivers/swr/swr_shader.cpp
@@ -61,6 +61,11 @@ bool operator==(const swr_jit_vs_key , const 
swr_jit_vs_key )
return !memcmp(, , sizeof(lhs));
 }
 
+bool operator==(const swr_jit_fetch_key , const swr_jit_fetch_key )
+{
+   return !memcmp(, , sizeof(lhs));
+}
+
 static void
 swr_generate_sampler_key(const struct lp_tgsi_info ,
  struct swr_context *ctx,
@@ -157,6 +162,15 @@ swr_generate_vs_key(struct swr_jit_vs_key ,
swr_generate_sampler_key(swr_vs->info, ctx, PIPE_SHADER_VERTEX, key);
 }
 
+void
+swr_generate_fetch_key(struct swr_jit_fetch_key ,
+   struct swr_vertex_element_state *velems)
+{
+   memset(, 0, sizeof(key));
+
+   key.fsState = velems->fsState;
+}
+
 struct BuilderSWR : public Builder {
BuilderSWR(JitManager *pJitMgr, const char *pName)
   : Builder(pJitMgr)
diff --git a/src/gallium/drivers/swr/swr_shader.h 
b/src/gallium/drivers/swr/swr_shader.h
index 7e3399c..266573f 100644
--- a/src/gallium/drivers/swr/swr_shader.h
+++ b/src/gallium/drivers/swr/swr_shader.h
@@ -42,6 +42,9 @@ void swr_generate_vs_key(struct swr_jit_vs_key ,
  struct swr_context *ctx,
  swr_vertex_shader *swr_vs);
 
+void swr_generate_fetch_key(struct swr_jit_fetch_key ,
+struct swr_vertex_element_state *velems);
+
 struct swr_jit_sampler_key {
unsigned nr_samplers;
unsigned nr_sampler_views;
@@ -60,6 +63,10 @@ struct swr_jit_vs_key : swr_jit_sampler_key {
unsigned clip_plane_mask; // from rasterizer state & vs_info
 };
 
+struct swr_jit_fetch_key {
+   FETCH_COMPILE_STATE fsState;
+};
+
 namespace std
 {
 template <> struct hash {
@@ -75,7 +82,15 @@ template <> struct hash {
   return util_hash_crc32(, sizeof(k));
}
 };
+
+template <> struct hash {
+   std::size_t operator()(const swr_jit_fetch_key ) const
+   {
+  return util_hash_crc32(, sizeof(k));
+   }
+};
 };
 
 bool operator==(const swr_jit_fs_key , const swr_jit_fs_key );
 bool operator==(const swr_jit_vs_key , const swr_jit_vs_key );
+bool operator==(const swr_jit_fetch_key , const swr_jit_fetch_key );
diff --git