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       set_foreach(cache->set, entry) {
61          fprintf(stderr, "mesa: vertex state cache should be empty\n");
62          assert(!"vertex state cache should be empty");
63       }
64 
65       _mesa_set_destroy(cache->set, NULL);
66       simple_mtx_destroy(&cache->lock);
67    }
68 }
69 
70 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)71 util_vertex_state_cache_get(struct pipe_screen *screen,
72                             struct pipe_vertex_buffer *buffer,
73                             const struct pipe_vertex_element *elements,
74                             unsigned num_elements,
75                             struct pipe_resource *indexbuf,
76                             uint32_t full_velem_mask,
77                             struct util_vertex_state_cache *cache)
78 {
79    struct pipe_vertex_state key;
80 
81    memset(&key, 0, sizeof(key));
82    key.input.indexbuf = indexbuf;
83    key.input.vbuffer.stride = buffer->stride;
84    assert(!buffer->is_user_buffer);
85    key.input.vbuffer.buffer_offset = buffer->buffer_offset;
86    key.input.vbuffer.buffer = buffer->buffer;
87    key.input.num_elements = num_elements;
88    for (unsigned i = 0; i < num_elements; i++)
89       key.input.elements[i] = elements[i];
90    key.input.full_velem_mask = full_velem_mask;
91 
92    uint32_t hash = key_hash(&key);
93 
94    /* Find the state in the live cache. */
95    simple_mtx_lock(&cache->lock);
96    struct set_entry *entry = _mesa_set_search_pre_hashed(cache->set, hash, &key);
97    struct pipe_vertex_state *state = entry ? (void*)entry->key : NULL;
98 
99    /* Return if the state already exists. */
100    if (state) {
101       /* Increase the refcount. */
102       p_atomic_inc(&state->reference.count);
103       assert(state->reference.count >= 1);
104       simple_mtx_unlock(&cache->lock);
105       return state;
106    }
107 
108    state = cache->create(screen, buffer, elements, num_elements, indexbuf,
109                          full_velem_mask);
110    if (state) {
111       assert(key_hash(state) == hash);
112       _mesa_set_add_pre_hashed(cache->set, hash, state);
113    }
114 
115    simple_mtx_unlock(&cache->lock);
116    return state;
117 }
118 
119 void
util_vertex_state_destroy(struct pipe_screen * screen,struct util_vertex_state_cache * cache,struct pipe_vertex_state * state)120 util_vertex_state_destroy(struct pipe_screen *screen,
121                           struct util_vertex_state_cache *cache,
122                           struct pipe_vertex_state *state)
123 {
124    simple_mtx_lock(&cache->lock);
125    /* There could have been a thread race and the cache might have returned
126     * the vertex state being destroyed. Check the reference count and do
127     * nothing if it's positive.
128     */
129    if (p_atomic_read(&state->reference.count) <= 0) {
130       _mesa_set_remove_key(cache->set, state);
131       cache->destroy(screen, state);
132    }
133    simple_mtx_unlock(&cache->lock);
134 }
135