Mesa (master): cso: remove context and delete_state pointers from all CSOs

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Dec 22 12:18:26 UTC 2020


Module: Mesa
Branch: master
Commit: 0d7aae7d9c571debad7331207a207d7a148f4678
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=0d7aae7d9c571debad7331207a207d7a148f4678

Author: Marek Olšák <marek.olsak at amd.com>
Date:   Thu Dec  3 15:52:20 2020 -0500

cso: remove context and delete_state pointers from all CSOs

We just need them per context, not per CSO. The new delete callback
replaces the per-CSO callbacks.

Reviewed-by: Eric Anholt <eric at anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7940>

---

 src/gallium/auxiliary/cso_cache/cso_cache.c   |  92 ++++++++--------------
 src/gallium/auxiliary/cso_cache/cso_cache.h   |  25 +++---
 src/gallium/auxiliary/cso_cache/cso_context.c | 106 +++++---------------------
 src/gallium/auxiliary/util/u_vbuf.c           |  19 +++--
 4 files changed, 76 insertions(+), 166 deletions(-)

diff --git a/src/gallium/auxiliary/cso_cache/cso_cache.c b/src/gallium/auxiliary/cso_cache/cso_cache.c
index b879a08caae..87c7493c8bf 100644
--- a/src/gallium/auxiliary/cso_cache/cso_cache.c
+++ b/src/gallium/auxiliary/cso_cache/cso_cache.c
@@ -77,68 +77,32 @@ static inline struct cso_hash *_cso_hash_for_type(struct cso_cache *sc, enum cso
    return &sc->hashes[type];
 }
 
-static void delete_blend_state(void *state, UNUSED void *data)
-{
-   struct cso_blend *cso = (struct cso_blend *)state;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-}
-
-static void delete_depth_stencil_state(void *state, UNUSED void *data)
-{
-   struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-}
-
-static void delete_sampler_state(void *state, UNUSED void *data)
-{
-   struct cso_sampler *cso = (struct cso_sampler *)state;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-}
-
-static void delete_rasterizer_state(void *state, UNUSED void *data)
-{
-   struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-}
-
-static void delete_velements(void *state, UNUSED void *data)
-{
-   struct cso_velements *cso = (struct cso_velements *)state;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-}
-
-static inline void delete_cso(void *state, enum cso_cache_type type)
+/* Default delete callback. It can also be used by custom callbacks. */
+void cso_delete_state(struct pipe_context *pipe, void *state,
+                      enum cso_cache_type type)
 {
    switch (type) {
    case CSO_BLEND:
-      delete_blend_state(state, 0);
+      pipe->delete_blend_state(pipe, ((struct cso_blend*)state)->data);
       break;
    case CSO_SAMPLER:
-      delete_sampler_state(state, 0);
+      pipe->delete_sampler_state(pipe, ((struct cso_sampler*)state)->data);
       break;
    case CSO_DEPTH_STENCIL_ALPHA:
-      delete_depth_stencil_state(state, 0);
+      pipe->delete_depth_stencil_alpha_state(pipe,
+                           ((struct cso_depth_stencil_alpha*)state)->data);
       break;
    case CSO_RASTERIZER:
-      delete_rasterizer_state(state, 0);
+      pipe->delete_rasterizer_state(pipe, ((struct cso_rasterizer*)state)->data);
       break;
    case CSO_VELEMENTS:
-      delete_velements(state, 0);
+      pipe->delete_vertex_elements_state(pipe, ((struct cso_velements*)state)->data);
       break;
    default:
       assert(0);
-      FREE(state);
    }
+
+   FREE(state);
 }
 
 
@@ -153,8 +117,10 @@ static inline void sanitize_hash(struct cso_cache *sc,
 
 
 static inline void sanitize_cb(struct cso_hash *hash, enum cso_cache_type type,
-			       int max_size, UNUSED void *user_data)
+			       int max_size, void *user_data)
 {
+   struct cso_cache *cache = (struct cso_cache *)user_data;
+
    /* if we're approach the maximum size, remove fourth of the entries
     * otherwise every subsequent call will go through the same */
    int hash_size = cso_hash_size(hash);
@@ -167,7 +133,7 @@ static inline void sanitize_cb(struct cso_hash *hash, enum cso_cache_type type,
       /*fixme: currently we pick the nodes to remove at random*/
       struct cso_hash_iter iter = cso_hash_first_node(hash);
       void  *cso = cso_hash_take(hash, cso_hash_iter_key(iter));
-      delete_cso(cso, type);
+      cache->delete_cso(cache->delete_cso_ctx, cso, type);
       --to_remove;
    }
 }
@@ -226,7 +192,7 @@ struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
    return iter;
 }
 
-void cso_cache_init(struct cso_cache *sc)
+void cso_cache_init(struct cso_cache *sc, struct pipe_context *pipe)
 {
    memset(sc, 0, sizeof(*sc));
 
@@ -235,11 +201,12 @@ void cso_cache_init(struct cso_cache *sc)
       cso_hash_init(&sc->hashes[i]);
 
    sc->sanitize_cb        = sanitize_cb;
-   sc->sanitize_data      = 0;
+   sc->sanitize_data      = sc;
+   sc->delete_cso = (cso_delete_cso_callback)cso_delete_state;
+   sc->delete_cso_ctx = pipe;
 }
 
-void cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type,
-                        cso_state_callback func, void *user_data)
+static void cso_delete_all(struct cso_cache *sc, enum cso_cache_type type)
 {
    struct cso_hash *hash = _cso_hash_for_type(sc, type);
    struct cso_hash_iter iter;
@@ -249,7 +216,7 @@ void cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type,
       void *state = cso_hash_iter_data(iter);
       iter = cso_hash_iter_next(iter);
       if (state) {
-         func(state, user_data);
+         sc->delete_cso(sc->delete_cso_ctx, state, type);
       }
    }
 }
@@ -259,11 +226,11 @@ void cso_cache_delete(struct cso_cache *sc)
    int i;
 
    /* delete driver data */
-   cso_for_each_state(sc, CSO_BLEND, delete_blend_state, 0);
-   cso_for_each_state(sc, CSO_DEPTH_STENCIL_ALPHA, delete_depth_stencil_state, 0);
-   cso_for_each_state(sc, CSO_RASTERIZER, delete_rasterizer_state, 0);
-   cso_for_each_state(sc, CSO_SAMPLER, delete_sampler_state, 0);
-   cso_for_each_state(sc, CSO_VELEMENTS, delete_velements, 0);
+   cso_delete_all(sc, CSO_BLEND);
+   cso_delete_all(sc, CSO_DEPTH_STENCIL_ALPHA);
+   cso_delete_all(sc, CSO_RASTERIZER);
+   cso_delete_all(sc, CSO_SAMPLER);
+   cso_delete_all(sc, CSO_VELEMENTS);
 
    for (i = 0; i < CSO_CACHE_MAX; i++)
       cso_hash_deinit(&sc->hashes[i]);
@@ -287,3 +254,10 @@ void cso_cache_set_sanitize_callback(struct cso_cache *sc,
    sc->sanitize_data = user_data;
 }
 
+void cso_cache_set_delete_cso_callback(struct cso_cache *sc,
+                                       cso_delete_cso_callback delete_cso,
+                                       void *ctx)
+{
+   sc->delete_cso = delete_cso;
+   sc->delete_cso_ctx = ctx;
+}
diff --git a/src/gallium/auxiliary/cso_cache/cso_cache.h b/src/gallium/auxiliary/cso_cache/cso_cache.h
index 358904534bc..8a64f05a327 100644
--- a/src/gallium/auxiliary/cso_cache/cso_cache.h
+++ b/src/gallium/auxiliary/cso_cache/cso_cache.h
@@ -94,6 +94,9 @@ enum cso_cache_type {
    CSO_CACHE_MAX,
 };
 
+typedef void (*cso_delete_cso_callback)(void *ctx, void *state,
+                                        enum cso_cache_type type);
+
 typedef void (*cso_state_callback)(void *ctx, void *obj);
 
 typedef void (*cso_sanitize_callback)(struct cso_hash *hash,
@@ -107,34 +110,29 @@ struct cso_cache {
 
    cso_sanitize_callback sanitize_cb;
    void                 *sanitize_data;
+
+   cso_delete_cso_callback delete_cso;
+   void                    *delete_cso_ctx;
 };
 
 struct cso_blend {
    struct pipe_blend_state state;
    void *data;
-   cso_state_callback delete_state;
-   struct pipe_context *context;
 };
 
 struct cso_depth_stencil_alpha {
    struct pipe_depth_stencil_alpha_state state;
    void *data;
-   cso_state_callback delete_state;
-   struct pipe_context *context;
 };
 
 struct cso_rasterizer {
    struct pipe_rasterizer_state state;
    void *data;
-   cso_state_callback delete_state;
-   struct pipe_context *context;
 };
 
 struct cso_sampler {
    struct pipe_sampler_state state;
    void *data;
-   cso_state_callback delete_state;
-   struct pipe_context *context;
    unsigned hash_key;
 };
 
@@ -146,18 +144,19 @@ struct cso_velems_state {
 struct cso_velements {
    struct cso_velems_state state;
    void *data;
-   cso_state_callback delete_state;
-   struct pipe_context *context;
 };
 
 unsigned cso_construct_key(void *item, int item_size);
 
-void cso_cache_init(struct cso_cache *sc);
+void cso_cache_init(struct cso_cache *sc, struct pipe_context *pipe);
 void cso_cache_delete(struct cso_cache *sc);
 
 void cso_cache_set_sanitize_callback(struct cso_cache *sc,
                                      cso_sanitize_callback cb,
                                      void *user_data);
+void cso_cache_set_delete_cso_callback(struct cso_cache *sc,
+                                       cso_delete_cso_callback delete_cso,
+                                       void *ctx);
 
 struct cso_hash_iter cso_insert_state(struct cso_cache *sc,
                                       unsigned hash_key, enum cso_cache_type type,
@@ -167,9 +166,9 @@ struct cso_hash_iter cso_find_state(struct cso_cache *sc,
 struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
                                              unsigned hash_key, enum cso_cache_type type,
                                              void *templ, unsigned size);
-void cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type,
-                        cso_state_callback func, void *user_data);
 void cso_set_maximum_cache_size(struct cso_cache *sc, int number);
+void cso_delete_state(struct pipe_context *pipe, void *state,
+                      enum cso_cache_type type);
 
 #ifdef	__cplusplus
 }
diff --git a/src/gallium/auxiliary/cso_cache/cso_context.c b/src/gallium/auxiliary/cso_cache/cso_context.c
index a2324c8952e..b651cd4b28a 100644
--- a/src/gallium/auxiliary/cso_cache/cso_context.c
+++ b/src/gallium/auxiliary/cso_cache/cso_context.c
@@ -136,89 +136,35 @@ struct pipe_context *cso_get_pipe_context(struct cso_context *cso)
    return cso->pipe;
 }
 
-static boolean delete_blend_state(struct cso_context *ctx, void *state)
-{
-   struct cso_blend *cso = (struct cso_blend *)state;
-
-   if (ctx->blend == cso->data)
-      return FALSE;
-
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-   return TRUE;
-}
-
-static boolean delete_depth_stencil_state(struct cso_context *ctx, void *state)
-{
-   struct cso_depth_stencil_alpha *cso =
-      (struct cso_depth_stencil_alpha *)state;
-
-   if (ctx->depth_stencil == cso->data)
-      return FALSE;
-
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-
-   return TRUE;
-}
-
-static boolean delete_sampler_state(UNUSED struct cso_context *ctx, void *state)
-{
-   struct cso_sampler *cso = (struct cso_sampler *)state;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-   return TRUE;
-}
-
-static boolean delete_rasterizer_state(struct cso_context *ctx, void *state)
-{
-   struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
-
-   if (ctx->rasterizer == cso->data)
-      return FALSE;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-   return TRUE;
-}
-
-static boolean delete_vertex_elements(struct cso_context *ctx,
-                                      void *state)
-{
-   struct cso_velements *cso = (struct cso_velements *)state;
-
-   if (ctx->velements == cso->data)
-      return FALSE;
-
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-   return TRUE;
-}
-
-
 static inline boolean delete_cso(struct cso_context *ctx,
                                  void *state, enum cso_cache_type type)
 {
    switch (type) {
    case CSO_BLEND:
-      return delete_blend_state(ctx, state);
-   case CSO_SAMPLER:
-      return delete_sampler_state(ctx, state);
+      if (ctx->blend == ((struct cso_blend*)state)->data)
+         return false;
+      break;
    case CSO_DEPTH_STENCIL_ALPHA:
-      return delete_depth_stencil_state(ctx, state);
+      if (ctx->depth_stencil == ((struct cso_depth_stencil_alpha*)state)->data)
+         return false;
+      break;
    case CSO_RASTERIZER:
-      return delete_rasterizer_state(ctx, state);
+      if (ctx->rasterizer == ((struct cso_rasterizer*)state)->data)
+         return false;
+      break;
    case CSO_VELEMENTS:
-      return delete_vertex_elements(ctx, state);
+      if (ctx->velements == ((struct cso_velements*)state)->data)
+         return false;
+      break;
+   case CSO_SAMPLER:
+      /* nothing to do for samplers */
+      break;
    default:
       assert(0);
-      FREE(state);
    }
-   return FALSE;
+
+   cso_delete_state(ctx->pipe, state, type);
+   return true;
 }
 
 static inline void
@@ -313,7 +259,7 @@ cso_create_context(struct pipe_context *pipe, unsigned flags)
    if (!ctx)
       return NULL;
 
-   cso_cache_init(&ctx->cache);
+   cso_cache_init(&ctx->cache, pipe);
    cso_cache_set_sanitize_callback(&ctx->cache, sanitize_hash, ctx);
 
    ctx->pipe = pipe;
@@ -501,8 +447,6 @@ enum pipe_error cso_set_blend(struct cso_context *ctx,
       memset(&cso->state, 0, sizeof cso->state);
       memcpy(&cso->state, templ, key_size);
       cso->data = ctx->pipe->create_blend_state(ctx->pipe, &cso->state);
-      cso->delete_state = (cso_state_callback)ctx->pipe->delete_blend_state;
-      cso->context = ctx->pipe;
 
       iter = cso_insert_state(&ctx->cache, hash_key, CSO_BLEND, cso);
       if (cso_hash_iter_is_null(iter)) {
@@ -563,9 +507,6 @@ cso_set_depth_stencil_alpha(struct cso_context *ctx,
       memcpy(&cso->state, templ, sizeof(*templ));
       cso->data = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe,
                                                               &cso->state);
-      cso->delete_state =
-         (cso_state_callback)ctx->pipe->delete_depth_stencil_alpha_state;
-      cso->context = ctx->pipe;
 
       iter = cso_insert_state(&ctx->cache, hash_key,
                               CSO_DEPTH_STENCIL_ALPHA, cso);
@@ -631,9 +572,6 @@ enum pipe_error cso_set_rasterizer(struct cso_context *ctx,
 
       memcpy(&cso->state, templ, sizeof(*templ));
       cso->data = ctx->pipe->create_rasterizer_state(ctx->pipe, &cso->state);
-      cso->delete_state =
-         (cso_state_callback)ctx->pipe->delete_rasterizer_state;
-      cso->context = ctx->pipe;
 
       iter = cso_insert_state(&ctx->cache, hash_key, CSO_RASTERIZER, cso);
       if (cso_hash_iter_is_null(iter)) {
@@ -1039,9 +977,6 @@ cso_set_vertex_elements_direct(struct cso_context *ctx,
       cso->data = ctx->pipe->create_vertex_elements_state(ctx->pipe,
                                                           velems->count,
                                                       &cso->state.velems[0]);
-      cso->delete_state =
-         (cso_state_callback) ctx->pipe->delete_vertex_elements_state;
-      cso->context = ctx->pipe;
 
       iter = cso_insert_state(&ctx->cache, hash_key, CSO_VELEMENTS, cso);
       if (cso_hash_iter_is_null(iter)) {
@@ -1257,9 +1192,6 @@ cso_single_sampler(struct cso_context *ctx, enum pipe_shader_type shader_stage,
 
          memcpy(&cso->state, templ, sizeof(*templ));
          cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state);
-         cso->delete_state =
-            (cso_state_callback) ctx->pipe->delete_sampler_state;
-         cso->context = ctx->pipe;
          cso->hash_key = hash_key;
 
          iter = cso_insert_state(&ctx->cache, hash_key, CSO_SAMPLER, cso);
diff --git a/src/gallium/auxiliary/util/u_vbuf.c b/src/gallium/auxiliary/util/u_vbuf.c
index 4bdd8f4c8ec..d5b40dd0d15 100644
--- a/src/gallium/auxiliary/util/u_vbuf.c
+++ b/src/gallium/auxiliary/util/u_vbuf.c
@@ -192,7 +192,8 @@ struct u_vbuf {
 static void *
 u_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count,
                               const struct pipe_vertex_element *attribs);
-static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso);
+static void u_vbuf_delete_vertex_elements(void *ctx, void *state,
+                                          enum cso_cache_type type);
 
 static const struct {
    enum pipe_format from, to;
@@ -322,7 +323,6 @@ u_vbuf_create(struct pipe_context *pipe, struct u_vbuf_caps *caps)
 
    mgr->caps = *caps;
    mgr->pipe = pipe;
-   cso_cache_init(&mgr->cso_cache);
    mgr->translate_cache = translate_cache_create();
    memset(mgr->fallback_vbs, ~0, sizeof(mgr->fallback_vbs));
    mgr->allowed_vb_mask = u_bit_consecutive(0, mgr->caps.max_vertex_buffers);
@@ -331,6 +331,10 @@ u_vbuf_create(struct pipe_context *pipe, struct u_vbuf_caps *caps)
       pipe->screen->get_param(pipe->screen,
                               PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET);
 
+   cso_cache_init(&mgr->cso_cache, pipe);
+   cso_cache_set_delete_cso_callback(&mgr->cso_cache,
+                                     u_vbuf_delete_vertex_elements, pipe);
+
    return mgr;
 }
 
@@ -357,8 +361,6 @@ u_vbuf_set_vertex_elements_internal(struct u_vbuf *mgr,
       memcpy(&cso->state, velems, key_size);
       cso->data = u_vbuf_create_vertex_elements(mgr, velems->count,
                                                 velems->velems);
-      cso->delete_state = (cso_state_callback)u_vbuf_delete_vertex_elements;
-      cso->context = (void*)mgr;
 
       iter = cso_insert_state(&mgr->cso_cache, hash_key, CSO_VELEMENTS, cso);
       ve = cso->data;
@@ -865,14 +867,17 @@ u_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count,
    return ve;
 }
 
-static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso)
+static void u_vbuf_delete_vertex_elements(void *ctx, void *state,
+                                          enum cso_cache_type type)
 {
-   struct pipe_context *pipe = mgr->pipe;
-   struct u_vbuf_elements *ve = cso;
+   struct pipe_context *pipe = (struct pipe_context*)ctx;
+   struct cso_velements *cso = (struct cso_velements*)state;
+   struct u_vbuf_elements *ve = (struct u_vbuf_elements*)cso->data;
 
    if (ve->driver_cso)
       pipe->delete_vertex_elements_state(pipe, ve->driver_cso);
    FREE(ve);
+   FREE(cso);
 }
 
 void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr,



More information about the mesa-commit mailing list