2 * Copyright © 2014 Intel Corporation
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:
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
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
27 #ifdef ENABLE_SHADER_CACHE
39 /* Size of cache keys in bytes. */
40 #define CACHE_KEY_SIZE 20
42 #define CACHE_DIR_NAME "mesa_shader_cache"
44 typedef uint8_t cache_key
[CACHE_KEY_SIZE
];
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.
50 #define CACHE_ITEM_TYPE_UNKNOWN 0x0
51 #define CACHE_ITEM_TYPE_GLSL 0x1
53 struct cache_item_metadata
{
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.
61 /** GLSL cache item metadata */
62 cache_key
*keys
; /* sha1 list of shaders that make up the cache item */
69 disk_cache_format_hex_id(char *buf
, const uint8_t *hex_id
, unsigned size
)
71 static const char hex_digits
[] = "0123456789abcdef";
74 for (i
= 0; i
< size
; i
+= 2) {
75 buf
[i
] = hex_digits
[hex_id
[i
>> 1] >> 4];
76 buf
[i
+ 1] = hex_digits
[hex_id
[i
>> 1] & 0x0f];
84 disk_cache_get_function_timestamp(void *ptr
, uint32_t* timestamp
)
86 #ifdef ENABLE_SHADER_CACHE
89 if (!dladdr(ptr
, &info
) || !info
.dli_fname
) {
92 if (stat(info
.dli_fname
, &st
)) {
95 *timestamp
= st
.st_mtime
;
102 /* Provide inlined stub functions if the shader cache is disabled. */
104 #ifdef ENABLE_SHADER_CACHE
107 * Create a new cache object.
109 * This function creates the handle necessary for all subsequent cache_*
112 * This cache provides two distinct operations:
114 * o Storage and retrieval of arbitrary objects by cryptographic
115 * name (or "key"). This is provided via disk_cache_put() and
118 * o The ability to store a key alone and check later whether the
119 * key was previously stored. This is provided via disk_cache_put_key()
120 * and disk_cache_has_key().
122 * The put_key()/has_key() operations are conceptually identical to
123 * put()/get() with no data, but are provided separately to allow for
124 * a more efficient implementation.
126 * In all cases, the keys are sequences of 20 bytes. It is anticipated
127 * that callers will compute appropriate SHA-1 signatures for keys,
128 * (though nothing in this implementation directly relies on how the
129 * names are computed). See mesa-sha1.h and _mesa_sha1_compute for
130 * assistance in computing SHA-1 signatures.
133 disk_cache_create(const char *gpu_name
, const char *timestamp
,
134 uint64_t driver_flags
);
137 * Destroy a cache object, (freeing all associated resources).
140 disk_cache_destroy(struct disk_cache
*cache
);
143 * Remove the item in the cache under the name \key.
146 disk_cache_remove(struct disk_cache
*cache
, const cache_key key
);
149 * Store an item in the cache under the name \key.
151 * The item can be retrieved later with disk_cache_get(), (unless the item has
152 * been evicted in the interim).
154 * Any call to disk_cache_put() may cause an existing, random item to be
155 * evicted from the cache.
158 disk_cache_put(struct disk_cache
*cache
, const cache_key key
,
159 const void *data
, size_t size
,
160 struct cache_item_metadata
*cache_item_metadata
);
163 * Retrieve an item previously stored in the cache with the name <key>.
165 * The item must have been previously stored with a call to disk_cache_put().
167 * If \size is non-NULL, then, on successful return, it will be set to the
168 * size of the object.
170 * \return A pointer to the stored object if found. NULL if the object
171 * is not found, or if any error occurs, (memory allocation failure,
172 * filesystem error, etc.). The returned data is malloc'ed so the
173 * caller should call free() it when finished.
176 disk_cache_get(struct disk_cache
*cache
, const cache_key key
, size_t *size
);
179 * Store the name \key within the cache, (without any associated data).
181 * Later this key can be checked with disk_cache_has_key(), (unless the key
182 * has been evicted in the interim).
184 * Any call to disk_cache_put_key() may cause an existing, random key to be
185 * evicted from the cache.
188 disk_cache_put_key(struct disk_cache
*cache
, const cache_key key
);
191 * Test whether the name \key was previously recorded in the cache.
193 * Return value: True if disk_cache_put_key() was previously called with
194 * \key, (and the key was not evicted in the interim).
196 * Note: disk_cache_has_key() will only return true for keys passed to
197 * disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause
198 * disk_cache_has_key() to return true for the same key.
201 disk_cache_has_key(struct disk_cache
*cache
, const cache_key key
);
204 * Compute the name \key from \data of given \size.
207 disk_cache_compute_key(struct disk_cache
*cache
, const void *data
, size_t size
,
212 static inline struct disk_cache
*
213 disk_cache_create(const char *gpu_name
, const char *timestamp
,
214 uint64_t driver_flags
)
220 disk_cache_destroy(struct disk_cache
*cache
) {
225 disk_cache_put(struct disk_cache
*cache
, const cache_key key
,
226 const void *data
, size_t size
,
227 struct cache_item_metadata
*cache_item_metadata
)
233 disk_cache_remove(struct disk_cache
*cache
, const cache_key key
)
238 static inline uint8_t *
239 disk_cache_get(struct disk_cache
*cache
, const cache_key key
, size_t *size
)
245 disk_cache_put_key(struct disk_cache
*cache
, const cache_key key
)
251 disk_cache_has_key(struct disk_cache
*cache
, const cache_key key
)
257 disk_cache_compute_key(struct disk_cache
*cache
, const void *data
, size_t size
,
263 #endif /* ENABLE_SHADER_CACHE */