1 /*
2  * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Rob Clark <robclark@freedesktop.org>
25  */
26 
27 #include "util/hash_table.h"
28 #include "util/ralloc.h"
29 #define XXH_INLINE_ALL
30 #include "util/xxhash.h"
31 
32 #include "ir3_cache.h"
33 #include "ir3_gallium.h"
34 
35 static uint32_t
key_hash(const void * _key)36 key_hash(const void *_key)
37 {
38    const struct ir3_cache_key *key = _key;
39    return XXH32(key, sizeof(*key), 0);
40 }
41 
42 static bool
key_equals(const void * _a,const void * _b)43 key_equals(const void *_a, const void *_b)
44 {
45    const struct ir3_cache_key *a = _a;
46    const struct ir3_cache_key *b = _b;
47    // TODO we could optimize the key shader-variant key comparison by not
48    // ignoring has_per_samp.. not really sure if that helps..
49    return memcmp(a, b, sizeof(struct ir3_cache_key)) == 0;
50 }
51 
52 struct ir3_cache {
53    /* cache mapping gallium/etc shader state-objs + shader-key to backend
54     * specific state-object
55     */
56    struct hash_table *ht;
57 
58    const struct ir3_cache_funcs *funcs;
59    void *data;
60 };
61 
62 struct ir3_cache *
ir3_cache_create(const struct ir3_cache_funcs * funcs,void * data)63 ir3_cache_create(const struct ir3_cache_funcs *funcs, void *data)
64 {
65    struct ir3_cache *cache = rzalloc(NULL, struct ir3_cache);
66 
67    cache->ht = _mesa_hash_table_create(cache, key_hash, key_equals);
68    cache->funcs = funcs;
69    cache->data = data;
70 
71    return cache;
72 }
73 
74 void
ir3_cache_destroy(struct ir3_cache * cache)75 ir3_cache_destroy(struct ir3_cache *cache)
76 {
77    if (!cache)
78       return;
79 
80    /* _mesa_hash_table_destroy is so *almost* useful.. */
81    hash_table_foreach (cache->ht, entry) {
82       cache->funcs->destroy_state(cache->data, entry->data);
83    }
84 
85    ralloc_free(cache);
86 }
87 
88 struct ir3_program_state *
ir3_cache_lookup(struct ir3_cache * cache,const struct ir3_cache_key * key,struct pipe_debug_callback * debug)89 ir3_cache_lookup(struct ir3_cache *cache, const struct ir3_cache_key *key,
90                  struct pipe_debug_callback *debug)
91 {
92    uint32_t hash = key_hash(key);
93    struct hash_entry *entry =
94       _mesa_hash_table_search_pre_hashed(cache->ht, hash, key);
95 
96    if (entry) {
97       return entry->data;
98    }
99 
100    if (key->hs)
101       debug_assert(key->ds);
102 
103    struct ir3_shader *shaders[MESA_SHADER_STAGES] = {
104       [MESA_SHADER_VERTEX] = ir3_get_shader(key->vs),
105       [MESA_SHADER_TESS_CTRL] = ir3_get_shader(key->hs),
106       [MESA_SHADER_TESS_EVAL] = ir3_get_shader(key->ds),
107       [MESA_SHADER_GEOMETRY] = ir3_get_shader(key->gs),
108       [MESA_SHADER_FRAGMENT] = ir3_get_shader(key->fs),
109    };
110 
111    struct ir3_shader_variant *variants[MESA_SHADER_STAGES];
112    struct ir3_shader_key shader_key = key->key;
113 
114    for (gl_shader_stage stage = MESA_SHADER_VERTEX; stage < MESA_SHADER_STAGES;
115         stage++) {
116       if (shaders[stage]) {
117          variants[stage] =
118             ir3_shader_variant(shaders[stage], shader_key, false, debug);
119          if (!variants[stage])
120             return NULL;
121       } else {
122          variants[stage] = NULL;
123       }
124    }
125 
126    struct ir3_compiler *compiler = shaders[MESA_SHADER_VERTEX]->compiler;
127    uint32_t safe_constlens = ir3_trim_constlen(variants, compiler);
128    shader_key.safe_constlen = true;
129 
130    for (gl_shader_stage stage = MESA_SHADER_VERTEX; stage < MESA_SHADER_STAGES;
131         stage++) {
132       if (safe_constlens & (1 << stage)) {
133          variants[stage] =
134             ir3_shader_variant(shaders[stage], shader_key, false, debug);
135          if (!variants[stage])
136             return NULL;
137       }
138    }
139 
140    struct ir3_shader_variant *bs;
141 
142    if (ir3_has_binning_vs(&key->key)) {
143       /* starting with a6xx, the same const state is used for binning and draw
144        * passes, so the binning pass VS variant needs to match the main VS
145        */
146       shader_key.safe_constlen = (compiler->gen >= 6) &&
147             !!(safe_constlens & (1 << MESA_SHADER_VERTEX));
148       bs =
149          ir3_shader_variant(shaders[MESA_SHADER_VERTEX], shader_key, true, debug);
150       if (!bs)
151          return NULL;
152    } else {
153       bs = variants[MESA_SHADER_VERTEX];
154    }
155 
156    struct ir3_program_state *state = cache->funcs->create_state(
157       cache->data, bs, variants[MESA_SHADER_VERTEX],
158       variants[MESA_SHADER_TESS_CTRL], variants[MESA_SHADER_TESS_EVAL],
159       variants[MESA_SHADER_GEOMETRY], variants[MESA_SHADER_FRAGMENT],
160       key);
161    state->key = *key;
162 
163    /* NOTE: uses copy of key in state obj, because pointer passed by caller
164     * is probably on the stack
165     */
166    _mesa_hash_table_insert_pre_hashed(cache->ht, hash, &state->key, state);
167 
168    return state;
169 }
170 
171 /* call when an API level state object is destroyed, to invalidate
172  * cache entries which reference that state object.
173  */
174 void
ir3_cache_invalidate(struct ir3_cache * cache,void * stobj)175 ir3_cache_invalidate(struct ir3_cache *cache, void *stobj)
176 {
177    if (!cache)
178       return;
179 
180    hash_table_foreach (cache->ht, entry) {
181       const struct ir3_cache_key *key = entry->key;
182       if ((key->fs == stobj) || (key->vs == stobj) || (key->ds == stobj) ||
183           (key->hs == stobj) || (key->gs == stobj)) {
184          cache->funcs->destroy_state(cache->data, entry->data);
185          _mesa_hash_table_remove(cache->ht, entry);
186          return;
187       }
188    }
189 }
190