i965: remove outdated comment about TCS passthrough
[mesa.git] / src / util / disk_cache.h
1 /*
2 * Copyright © 2014 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef DISK_CACHE_H
25 #define DISK_CACHE_H
26
27 #ifdef HAVE_DLFCN_H
28 #include <dlfcn.h>
29 #endif
30 #include <assert.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <sys/stat.h>
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /* Size of cache keys in bytes. */
40 #define CACHE_KEY_SIZE 20
41
42 #define CACHE_DIR_NAME "mesa_shader_cache"
43
44 typedef uint8_t cache_key[CACHE_KEY_SIZE];
45
46 /* WARNING: 3rd party applications might be reading the cache item metadata.
47 * Do not change these values without making the change widely known.
48 * Please contact Valve developers and make them aware of this change.
49 */
50 #define CACHE_ITEM_TYPE_UNKNOWN 0x0
51 #define CACHE_ITEM_TYPE_GLSL 0x1
52
53 typedef void
54 (*disk_cache_put_cb) (const void *key, signed long keySize,
55 const void *value, signed long valueSize);
56
57 typedef signed long
58 (*disk_cache_get_cb) (const void *key, signed long keySize,
59 void *value, signed long valueSize);
60
61 struct cache_item_metadata {
62 /**
63 * The cache item type. This could be used to identify a GLSL cache item,
64 * a certain type of IR (tgsi, nir, etc), or signal that it is the final
65 * binary form of the shader.
66 */
67 uint32_t type;
68
69 /** GLSL cache item metadata */
70 cache_key *keys; /* sha1 list of shaders that make up the cache item */
71 uint32_t num_keys;
72 };
73
74 struct disk_cache;
75
76 static inline char *
77 disk_cache_format_hex_id(char *buf, const uint8_t *hex_id, unsigned size)
78 {
79 static const char hex_digits[] = "0123456789abcdef";
80 unsigned i;
81
82 for (i = 0; i < size; i += 2) {
83 buf[i] = hex_digits[hex_id[i >> 1] >> 4];
84 buf[i + 1] = hex_digits[hex_id[i >> 1] & 0x0f];
85 }
86 buf[i] = '\0';
87
88 return buf;
89 }
90
91 #ifdef HAVE_DLFCN_H
92 static inline bool
93 disk_cache_get_function_timestamp(void *ptr, uint32_t* timestamp)
94 {
95 Dl_info info;
96 struct stat st;
97 if (!dladdr(ptr, &info) || !info.dli_fname) {
98 return false;
99 }
100 if (stat(info.dli_fname, &st)) {
101 return false;
102 }
103 *timestamp = st.st_mtime;
104 return true;
105 }
106 #endif
107
108 /* Provide inlined stub functions if the shader cache is disabled. */
109
110 #ifdef ENABLE_SHADER_CACHE
111
112 /**
113 * Create a new cache object.
114 *
115 * This function creates the handle necessary for all subsequent cache_*
116 * functions.
117 *
118 * This cache provides two distinct operations:
119 *
120 * o Storage and retrieval of arbitrary objects by cryptographic
121 * name (or "key"). This is provided via disk_cache_put() and
122 * disk_cache_get().
123 *
124 * o The ability to store a key alone and check later whether the
125 * key was previously stored. This is provided via disk_cache_put_key()
126 * and disk_cache_has_key().
127 *
128 * The put_key()/has_key() operations are conceptually identical to
129 * put()/get() with no data, but are provided separately to allow for
130 * a more efficient implementation.
131 *
132 * In all cases, the keys are sequences of 20 bytes. It is anticipated
133 * that callers will compute appropriate SHA-1 signatures for keys,
134 * (though nothing in this implementation directly relies on how the
135 * names are computed). See mesa-sha1.h and _mesa_sha1_compute for
136 * assistance in computing SHA-1 signatures.
137 */
138 struct disk_cache *
139 disk_cache_create(const char *gpu_name, const char *timestamp,
140 uint64_t driver_flags);
141
142 /**
143 * Destroy a cache object, (freeing all associated resources).
144 */
145 void
146 disk_cache_destroy(struct disk_cache *cache);
147
148 /**
149 * Remove the item in the cache under the name \key.
150 */
151 void
152 disk_cache_remove(struct disk_cache *cache, const cache_key key);
153
154 /**
155 * Store an item in the cache under the name \key.
156 *
157 * The item can be retrieved later with disk_cache_get(), (unless the item has
158 * been evicted in the interim).
159 *
160 * Any call to disk_cache_put() may cause an existing, random item to be
161 * evicted from the cache.
162 */
163 void
164 disk_cache_put(struct disk_cache *cache, const cache_key key,
165 const void *data, size_t size,
166 struct cache_item_metadata *cache_item_metadata);
167
168 /**
169 * Retrieve an item previously stored in the cache with the name <key>.
170 *
171 * The item must have been previously stored with a call to disk_cache_put().
172 *
173 * If \size is non-NULL, then, on successful return, it will be set to the
174 * size of the object.
175 *
176 * \return A pointer to the stored object if found. NULL if the object
177 * is not found, or if any error occurs, (memory allocation failure,
178 * filesystem error, etc.). The returned data is malloc'ed so the
179 * caller should call free() it when finished.
180 */
181 void *
182 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size);
183
184 /**
185 * Store the name \key within the cache, (without any associated data).
186 *
187 * Later this key can be checked with disk_cache_has_key(), (unless the key
188 * has been evicted in the interim).
189 *
190 * Any call to disk_cache_put_key() may cause an existing, random key to be
191 * evicted from the cache.
192 */
193 void
194 disk_cache_put_key(struct disk_cache *cache, const cache_key key);
195
196 /**
197 * Test whether the name \key was previously recorded in the cache.
198 *
199 * Return value: True if disk_cache_put_key() was previously called with
200 * \key, (and the key was not evicted in the interim).
201 *
202 * Note: disk_cache_has_key() will only return true for keys passed to
203 * disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause
204 * disk_cache_has_key() to return true for the same key.
205 */
206 bool
207 disk_cache_has_key(struct disk_cache *cache, const cache_key key);
208
209 /**
210 * Compute the name \key from \data of given \size.
211 */
212 void
213 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
214 cache_key key);
215
216 void
217 disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
218 disk_cache_get_cb get);
219
220 #else
221
222 static inline struct disk_cache *
223 disk_cache_create(const char *gpu_name, const char *timestamp,
224 uint64_t driver_flags)
225 {
226 return NULL;
227 }
228
229 static inline void
230 disk_cache_destroy(struct disk_cache *cache) {
231 return;
232 }
233
234 static inline void
235 disk_cache_put(struct disk_cache *cache, const cache_key key,
236 const void *data, size_t size,
237 struct cache_item_metadata *cache_item_metadata)
238 {
239 return;
240 }
241
242 static inline void
243 disk_cache_remove(struct disk_cache *cache, const cache_key key)
244 {
245 return;
246 }
247
248 static inline uint8_t *
249 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
250 {
251 return NULL;
252 }
253
254 static inline void
255 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
256 {
257 return;
258 }
259
260 static inline bool
261 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
262 {
263 return false;
264 }
265
266 static inline void
267 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
268 const cache_key key)
269 {
270 return;
271 }
272
273 static inline void
274 disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
275 disk_cache_get_cb get)
276 {
277 return;
278 }
279
280 #endif /* ENABLE_SHADER_CACHE */
281
282 #ifdef __cplusplus
283 }
284 #endif
285
286 #endif /* CACHE_H */