glsl: pass shader source keys to the disk cache
[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 ENABLE_SHADER_CACHE
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 struct cache_item_metadata {
54 /**
55 * The cache item type. This could be used to identify a GLSL cache item,
56 * a certain type of IR (tgsi, nir, etc), or signal that it is the final
57 * binary form of the shader.
58 */
59 uint32_t type;
60
61 /** GLSL cache item metadata */
62 cache_key *keys; /* sha1 list of shaders that make up the cache item */
63 uint32_t num_keys;
64 };
65
66 struct disk_cache;
67
68 static inline bool
69 disk_cache_get_function_timestamp(void *ptr, uint32_t* timestamp)
70 {
71 #ifdef ENABLE_SHADER_CACHE
72 Dl_info info;
73 struct stat st;
74 if (!dladdr(ptr, &info) || !info.dli_fname) {
75 return false;
76 }
77 if (stat(info.dli_fname, &st)) {
78 return false;
79 }
80 *timestamp = st.st_mtime;
81 return true;
82 #else
83 return false;
84 #endif
85 }
86
87 /* Provide inlined stub functions if the shader cache is disabled. */
88
89 #ifdef ENABLE_SHADER_CACHE
90
91 /**
92 * Create a new cache object.
93 *
94 * This function creates the handle necessary for all subsequent cache_*
95 * functions.
96 *
97 * This cache provides two distinct operations:
98 *
99 * o Storage and retrieval of arbitrary objects by cryptographic
100 * name (or "key"). This is provided via disk_cache_put() and
101 * disk_cache_get().
102 *
103 * o The ability to store a key alone and check later whether the
104 * key was previously stored. This is provided via disk_cache_put_key()
105 * and disk_cache_has_key().
106 *
107 * The put_key()/has_key() operations are conceptually identical to
108 * put()/get() with no data, but are provided separately to allow for
109 * a more efficient implementation.
110 *
111 * In all cases, the keys are sequences of 20 bytes. It is anticipated
112 * that callers will compute appropriate SHA-1 signatures for keys,
113 * (though nothing in this implementation directly relies on how the
114 * names are computed). See mesa-sha1.h and _mesa_sha1_compute for
115 * assistance in computing SHA-1 signatures.
116 */
117 struct disk_cache *
118 disk_cache_create(const char *gpu_name, const char *timestamp,
119 uint64_t driver_flags);
120
121 /**
122 * Destroy a cache object, (freeing all associated resources).
123 */
124 void
125 disk_cache_destroy(struct disk_cache *cache);
126
127 /**
128 * Remove the item in the cache under the name \key.
129 */
130 void
131 disk_cache_remove(struct disk_cache *cache, const cache_key key);
132
133 /**
134 * Store an item in the cache under the name \key.
135 *
136 * The item can be retrieved later with disk_cache_get(), (unless the item has
137 * been evicted in the interim).
138 *
139 * Any call to disk_cache_put() may cause an existing, random item to be
140 * evicted from the cache.
141 */
142 void
143 disk_cache_put(struct disk_cache *cache, const cache_key key,
144 const void *data, size_t size,
145 struct cache_item_metadata *cache_item_metadata);
146
147 /**
148 * Retrieve an item previously stored in the cache with the name <key>.
149 *
150 * The item must have been previously stored with a call to disk_cache_put().
151 *
152 * If \size is non-NULL, then, on successful return, it will be set to the
153 * size of the object.
154 *
155 * \return A pointer to the stored object if found. NULL if the object
156 * is not found, or if any error occurs, (memory allocation failure,
157 * filesystem error, etc.). The returned data is malloc'ed so the
158 * caller should call free() it when finished.
159 */
160 void *
161 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size);
162
163 /**
164 * Store the name \key within the cache, (without any associated data).
165 *
166 * Later this key can be checked with disk_cache_has_key(), (unless the key
167 * has been evicted in the interim).
168 *
169 * Any call to disk_cache_put_key() may cause an existing, random key to be
170 * evicted from the cache.
171 */
172 void
173 disk_cache_put_key(struct disk_cache *cache, const cache_key key);
174
175 /**
176 * Test whether the name \key was previously recorded in the cache.
177 *
178 * Return value: True if disk_cache_put_key() was previously called with
179 * \key, (and the key was not evicted in the interim).
180 *
181 * Note: disk_cache_has_key() will only return true for keys passed to
182 * disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause
183 * disk_cache_has_key() to return true for the same key.
184 */
185 bool
186 disk_cache_has_key(struct disk_cache *cache, const cache_key key);
187
188 /**
189 * Compute the name \key from \data of given \size.
190 */
191 void
192 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
193 cache_key key);
194
195 #else
196
197 static inline struct disk_cache *
198 disk_cache_create(const char *gpu_name, const char *timestamp,
199 uint64_t driver_flags)
200 {
201 return NULL;
202 }
203
204 static inline void
205 disk_cache_destroy(struct disk_cache *cache) {
206 return;
207 }
208
209 static inline void
210 disk_cache_put(struct disk_cache *cache, const cache_key key,
211 const void *data, size_t size,
212 struct cache_item_metadata *cache_item_metadata)
213 {
214 return;
215 }
216
217 static inline void
218 disk_cache_remove(struct disk_cache *cache, const cache_key key)
219 {
220 return;
221 }
222
223 static inline uint8_t *
224 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
225 {
226 return NULL;
227 }
228
229 static inline void
230 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
231 {
232 return;
233 }
234
235 static inline bool
236 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
237 {
238 return false;
239 }
240
241 static inline void
242 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
243 const cache_key key)
244 {
245 return;
246 }
247
248 #endif /* ENABLE_SHADER_CACHE */
249
250 #ifdef __cplusplus
251 }
252 #endif
253
254 #endif /* CACHE_H */