Mesa (main): gallium/util: add util_vertex_state_cache for deduplicating the states

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Oct 1 15:45:22 UTC 2021


Module: Mesa
Branch: main
Commit: 7a2458f4bde0d9fe590692f3ec63aa088b552763
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=7a2458f4bde0d9fe590692f3ec63aa088b552763

Author: Marek Olšák <marek.olsak at amd.com>
Date:   Wed Sep 15 22:51:49 2021 -0400

gallium/util: add util_vertex_state_cache for deduplicating the states

Reviewed-By: Mike Blumenkrantz <michael.blumenkrantz at gmail.com>
Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13050>

---

 src/gallium/auxiliary/meson.build                 |   2 +
 src/gallium/auxiliary/util/u_vertex_state_cache.c | 134 ++++++++++++++++++++++
 src/gallium/auxiliary/util/u_vertex_state_cache.h |  67 +++++++++++
 3 files changed, 203 insertions(+)

diff --git a/src/gallium/auxiliary/meson.build b/src/gallium/auxiliary/meson.build
index 81ff5672f2c..7682357edf0 100644
--- a/src/gallium/auxiliary/meson.build
+++ b/src/gallium/auxiliary/meson.build
@@ -321,6 +321,8 @@ files_libgallium = files(
   'util/u_upload_mgr.h',
   'util/u_vbuf.c',
   'util/u_vbuf.h',
+  'util/u_vertex_state_cache.c',
+  'util/u_vertex_state_cache.h',
   'util/u_video.h',
   'util/u_viewport.h',
   'nir/tgsi_to_nir.c',
diff --git a/src/gallium/auxiliary/util/u_vertex_state_cache.c b/src/gallium/auxiliary/util/u_vertex_state_cache.c
new file mode 100644
index 00000000000..f98a1071af4
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_vertex_state_cache.c
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "util/u_vertex_state_cache.h"
+#include "util/u_inlines.h"
+#include "util/hash_table.h"
+#include "util/set.h"
+
+static uint32_t key_hash(const void *key)
+{
+   const struct pipe_vertex_state *state = key;
+
+   return _mesa_hash_data(&state->input, sizeof(state->input));
+}
+
+static bool key_equals(const void *a, const void *b)
+{
+   const struct pipe_vertex_state *sa = a;
+   const struct pipe_vertex_state *sb = b;
+
+   return !memcmp(&sa->input, &sb->input, sizeof(sa->input));
+}
+
+void
+util_vertex_state_cache_init(struct util_vertex_state_cache *cache,
+                             pipe_create_vertex_state_func create,
+                             pipe_vertex_state_destroy_func destroy)
+{
+   simple_mtx_init(&cache->lock, mtx_plain);
+   cache->set = _mesa_set_create(NULL, key_hash, key_equals);
+   cache->create = create;
+   cache->destroy = destroy;
+}
+
+void
+util_vertex_state_cache_deinit(struct util_vertex_state_cache *cache)
+{
+   if (cache->set) {
+      set_foreach(cache->set, entry) {
+         fprintf(stderr, "mesa: vertex state cache should be empty\n");
+         assert(!"vertex state cache should be empty");
+      }
+
+      _mesa_set_destroy(cache->set, NULL);
+      simple_mtx_destroy(&cache->lock);
+   }
+}
+
+struct pipe_vertex_state *
+util_vertex_state_cache_get(struct pipe_screen *screen,
+                            struct pipe_vertex_buffer *buffer,
+                            const struct pipe_vertex_element *elements,
+                            unsigned num_elements,
+                            struct pipe_resource *indexbuf,
+                            uint32_t full_velem_mask,
+                            struct util_vertex_state_cache *cache)
+{
+   struct pipe_vertex_state key;
+
+   memset(&key, 0, sizeof(key));
+   key.input.indexbuf = indexbuf;
+   key.input.vbuffer.stride = buffer->stride;
+   assert(!buffer->is_user_buffer);
+   key.input.vbuffer.buffer_offset = buffer->buffer_offset;
+   key.input.vbuffer.buffer = buffer->buffer;
+   key.input.num_elements = num_elements;
+   for (unsigned i = 0; i < num_elements; i++)
+      key.input.elements[i] = elements[i];
+   key.input.full_velem_mask = full_velem_mask;
+
+   uint32_t hash = key_hash(&key);
+
+   /* Find the state in the live cache. */
+   simple_mtx_lock(&cache->lock);
+   struct set_entry *entry = _mesa_set_search_pre_hashed(cache->set, hash, &key);
+   struct pipe_vertex_state *state = entry ? (void*)entry->key : NULL;
+
+   /* Return if the state already exists. */
+   if (state) {
+      /* Increase the refcount. */
+      p_atomic_inc(&state->reference.count);
+      assert(state->reference.count >= 1);
+      simple_mtx_unlock(&cache->lock);
+      return state;
+   }
+
+   state = cache->create(screen, buffer, elements, num_elements, indexbuf,
+                         full_velem_mask);
+   if (state) {
+      assert(key_hash(state) == hash);
+      _mesa_set_add_pre_hashed(cache->set, hash, state);
+   }
+
+   simple_mtx_unlock(&cache->lock);
+   return state;
+}
+
+void
+util_vertex_state_destroy(struct pipe_screen *screen,
+                          struct util_vertex_state_cache *cache,
+                          struct pipe_vertex_state *state)
+{
+   simple_mtx_lock(&cache->lock);
+   /* There could have been a thread race and the cache might have returned
+    * the vertex state being destroyed. Check the reference count and do
+    * nothing if it's positive.
+    */
+   if (p_atomic_read(&state->reference.count) <= 0) {
+      _mesa_set_remove_key(cache->set, state);
+      cache->destroy(screen, state);
+   }
+   simple_mtx_unlock(&cache->lock);
+}
diff --git a/src/gallium/auxiliary/util/u_vertex_state_cache.h b/src/gallium/auxiliary/util/u_vertex_state_cache.h
new file mode 100644
index 00000000000..902e91e43a3
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_vertex_state_cache.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/* This deduplicates pipe_vertex_state CSOs to enable draw merging in
+ * u_threaded_context because the draw merging is possible only if different
+ * display lists use the same pipe_vertex_state CSO.
+ */
+
+#ifndef U_VERTEX_STATE_CACHE_H
+#define U_VERTEX_STATE_CACHE_H
+
+#include "util/simple_mtx.h"
+#include "pipe/p_screen.h"
+#include "pipe/p_state.h"
+
+struct util_vertex_state_cache {
+   simple_mtx_t lock;
+   struct set *set;
+
+   pipe_create_vertex_state_func create;
+   pipe_vertex_state_destroy_func destroy;
+};
+
+void
+util_vertex_state_cache_init(struct util_vertex_state_cache *cache,
+                             pipe_create_vertex_state_func create,
+                             pipe_vertex_state_destroy_func destroy);
+
+void
+util_vertex_state_cache_deinit(struct util_vertex_state_cache *cache);
+
+struct pipe_vertex_state *
+util_vertex_state_cache_get(struct pipe_screen *screen,
+                            struct pipe_vertex_buffer *buffer,
+                            const struct pipe_vertex_element *elements,
+                            unsigned num_elements,
+                            struct pipe_resource *indexbuf,
+                            uint32_t full_velem_mask,
+                            struct util_vertex_state_cache *cache);
+
+void
+util_vertex_state_destroy(struct pipe_screen *screen,
+                          struct util_vertex_state_cache *cache,
+                          struct pipe_vertex_state *state);
+
+#endif



More information about the mesa-commit mailing list