1 /*
2 * Copyright 2021 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "util/u_vertex_state_cache.h"
26 #include "util/u_inlines.h"
27 #include "util/hash_table.h"
28 #include "util/set.h"
29
key_hash(const void * key)30 static uint32_t key_hash(const void *key)
31 {
32 const struct pipe_vertex_state *state = key;
33
34 return _mesa_hash_data(&state->input, sizeof(state->input));
35 }
36
key_equals(const void * a,const void * b)37 static bool key_equals(const void *a, const void *b)
38 {
39 const struct pipe_vertex_state *sa = a;
40 const struct pipe_vertex_state *sb = b;
41
42 return !memcmp(&sa->input, &sb->input, sizeof(sa->input));
43 }
44
45 void
util_vertex_state_cache_init(struct util_vertex_state_cache * cache,pipe_create_vertex_state_func create,pipe_vertex_state_destroy_func destroy)46 util_vertex_state_cache_init(struct util_vertex_state_cache *cache,
47 pipe_create_vertex_state_func create,
48 pipe_vertex_state_destroy_func destroy)
49 {
50 simple_mtx_init(&cache->lock, mtx_plain);
51 cache->set = _mesa_set_create(NULL, key_hash, key_equals);
52 cache->create = create;
53 cache->destroy = destroy;
54 }
55
56 void
util_vertex_state_cache_deinit(struct util_vertex_state_cache * cache)57 util_vertex_state_cache_deinit(struct util_vertex_state_cache *cache)
58 {
59 if (cache->set) {
60 _mesa_set_destroy(cache->set, NULL);
61 simple_mtx_destroy(&cache->lock);
62 }
63 }
64
65 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)66 util_vertex_state_cache_get(struct pipe_screen *screen,
67 struct pipe_vertex_buffer *buffer,
68 const struct pipe_vertex_element *elements,
69 unsigned num_elements,
70 struct pipe_resource *indexbuf,
71 uint32_t full_velem_mask,
72 struct util_vertex_state_cache *cache)
73 {
74 struct pipe_vertex_state key;
75
76 memset(&key, 0, sizeof(key));
77 key.input.indexbuf = indexbuf;
78 key.input.vbuffer.stride = buffer->stride;
79 assert(!buffer->is_user_buffer);
80 key.input.vbuffer.buffer_offset = buffer->buffer_offset;
81 key.input.vbuffer.buffer = buffer->buffer;
82 key.input.num_elements = num_elements;
83 for (unsigned i = 0; i < num_elements; i++)
84 key.input.elements[i] = elements[i];
85 key.input.full_velem_mask = full_velem_mask;
86
87 uint32_t hash = key_hash(&key);
88
89 /* Find the state in the live cache. */
90 simple_mtx_lock(&cache->lock);
91 struct set_entry *entry = _mesa_set_search_pre_hashed(cache->set, hash, &key);
92 struct pipe_vertex_state *state = entry ? (void*)entry->key : NULL;
93
94 /* Return if the state already exists. */
95 if (state) {
96 /* Increase the refcount. */
97 p_atomic_inc(&state->reference.count);
98 assert(state->reference.count >= 1);
99 simple_mtx_unlock(&cache->lock);
100 return state;
101 }
102
103 state = cache->create(screen, buffer, elements, num_elements, indexbuf,
104 full_velem_mask);
105 if (state) {
106 assert(key_hash(state) == hash);
107 _mesa_set_add_pre_hashed(cache->set, hash, state);
108 }
109
110 simple_mtx_unlock(&cache->lock);
111 return state;
112 }
113
114 void
util_vertex_state_destroy(struct pipe_screen * screen,struct util_vertex_state_cache * cache,struct pipe_vertex_state * state)115 util_vertex_state_destroy(struct pipe_screen *screen,
116 struct util_vertex_state_cache *cache,
117 struct pipe_vertex_state *state)
118 {
119 simple_mtx_lock(&cache->lock);
120 /* There could have been a thread race and the cache might have returned
121 * the vertex state being destroyed. Check the reference count and do
122 * nothing if it's positive.
123 */
124 if (p_atomic_read(&state->reference.count) <= 0) {
125 _mesa_set_remove_key(cache->set, state);
126 cache->destroy(screen, state);
127 }
128 simple_mtx_unlock(&cache->lock);
129 }
130