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/ralloc.h"
28 #include "util/hash_table.h"
29 #define XXH_INLINE_ALL
30 #include "util/xxhash.h"
31 
32 #include "ir3_cache.h"
33 #include "ir3_gallium.h"
34 
35 
36 static uint32_t
key_hash(const void * _key)37 key_hash(const void *_key)
38 {
39 	const struct ir3_cache_key *key = _key;
40 	return  XXH32(key, sizeof(*key), 0);
41 }
42 
43 static bool
key_equals(const void * _a,const void * _b)44 key_equals(const void *_a, const void *_b)
45 {
46 	const struct ir3_cache_key *a = _a;
47 	const struct ir3_cache_key *b = _b;
48 	// TODO we could optimize the key shader-variant key comparison by not
49 	// ignoring has_per_samp.. not really sure if that helps..
50 	return memcmp(a, b, sizeof(struct ir3_cache_key)) == 0;
51 }
52 
53 struct ir3_cache {
54 	/* cache mapping gallium/etc shader state-objs + shader-key to backend
55 	 * specific state-object
56 	 */
57 	struct hash_table *ht;
58 
59 	const struct ir3_cache_funcs *funcs;
60 	void *data;
61 };
62 
ir3_cache_create(const struct ir3_cache_funcs * funcs,void * data)63 struct ir3_cache * 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 
ir3_cache_destroy(struct ir3_cache * cache)74 void ir3_cache_destroy(struct ir3_cache *cache)
75 {
76 	/* _mesa_hash_table_destroy is so *almost* useful.. */
77 	hash_table_foreach(cache->ht, entry) {
78 		cache->funcs->destroy_state(cache->data, entry->data);
79 	}
80 
81 	ralloc_free(cache);
82 }
83 
84 struct ir3_program_state *
ir3_cache_lookup(struct ir3_cache * cache,const struct ir3_cache_key * key,struct pipe_debug_callback * debug)85 ir3_cache_lookup(struct ir3_cache *cache, const struct ir3_cache_key *key,
86 		struct pipe_debug_callback *debug)
87 {
88 	uint32_t hash = key_hash(key);
89 	struct hash_entry *entry =
90 		_mesa_hash_table_search_pre_hashed(cache->ht, hash, key);
91 
92 	if (entry) {
93 		return entry->data;
94 	}
95 
96 	if (key->hs)
97 		debug_assert(key->ds);
98 
99 	struct ir3_shader *shaders[MESA_SHADER_STAGES] = {
100 		[MESA_SHADER_VERTEX] = key->vs,
101 		[MESA_SHADER_TESS_CTRL] = key->hs,
102 		[MESA_SHADER_TESS_EVAL] = key->ds,
103 		[MESA_SHADER_GEOMETRY] = key->gs,
104 		[MESA_SHADER_FRAGMENT] = key->fs,
105 	};
106 
107 	struct ir3_shader_variant *variants[MESA_SHADER_STAGES];
108 	struct ir3_shader_key shader_key = key->key;
109 
110 	for (gl_shader_stage stage = MESA_SHADER_VERTEX;
111 		 stage < MESA_SHADER_STAGES; stage++) {
112 		if (shaders[stage]) {
113 			variants[stage] =
114 				ir3_shader_variant(shaders[stage], shader_key, false, debug);
115 			if (!variants[stage])
116 				return NULL;
117 		} else {
118 			variants[stage] = NULL;
119 		}
120 	}
121 
122 	uint32_t safe_constlens = ir3_trim_constlen(variants, key->vs->compiler);
123 	shader_key.safe_constlen = true;
124 
125 	for (gl_shader_stage stage = MESA_SHADER_VERTEX;
126 		 stage < MESA_SHADER_STAGES; stage++) {
127 		if (safe_constlens & (1 << stage)) {
128 			variants[stage] =
129 				ir3_shader_variant(shaders[stage], shader_key, false, debug);
130 			if (!variants[stage])
131 				return NULL;
132 		}
133 	}
134 
135 	struct ir3_shader_variant *bs;
136 
137 	if (ir3_has_binning_vs(&key->key)) {
138 		shader_key.safe_constlen = !!(safe_constlens & (1 << MESA_SHADER_VERTEX));
139 		bs = ir3_shader_variant(key->vs, key->key, true, debug);
140 		if (!bs)
141 			return NULL;
142 	} else {
143 		bs = variants[MESA_SHADER_VERTEX];
144 	}
145 
146 	struct ir3_program_state *state =
147 		cache->funcs->create_state(cache->data, bs,
148 								   variants[MESA_SHADER_VERTEX],
149 								   variants[MESA_SHADER_TESS_CTRL],
150 								   variants[MESA_SHADER_TESS_EVAL],
151 								   variants[MESA_SHADER_GEOMETRY],
152 								   variants[MESA_SHADER_FRAGMENT],
153 								   &key->key);
154 	state->key = *key;
155 
156 	/* NOTE: uses copy of key in state obj, because pointer passed by caller
157 	 * is probably on the stack
158 	 */
159 	_mesa_hash_table_insert_pre_hashed(cache->ht, hash, &state->key, state);
160 
161 	return state;
162 }
163 
164 /* call when an API level state object is destroyed, to invalidate
165  * cache entries which reference that state object.
166  */
ir3_cache_invalidate(struct ir3_cache * cache,void * stobj)167 void ir3_cache_invalidate(struct ir3_cache *cache, void *stobj)
168 {
169 	hash_table_foreach(cache->ht, entry) {
170 		const struct ir3_cache_key *key = entry->key;
171 		if ((key->fs == stobj) || (key->vs == stobj)) {
172 			cache->funcs->destroy_state(cache->data, entry->data);
173 			_mesa_hash_table_remove(cache->ht, entry);
174 			return;
175 		}
176 	}
177 }
178