1 /*
2 * Copyright © 2021 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 #ifndef VK_PIPELINE_CACHE_H
24 #define VK_PIPELINE_CACHE_H
25
26 #include "vk_object.h"
27 #include "vk_util.h"
28
29 #include "util/simple_mtx.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /* #include "util/blob.h" */
36 struct blob;
37 struct blob_reader;
38
39 /* #include "util/set.h" */
40 struct set;
41
42 /* #include "compiler/nir/nir.h" */
43 struct nir_shader;
44 struct nir_shader_compiler_options;
45
46 struct vk_pipeline_cache;
47 struct vk_pipeline_cache_object;
48
49 #define VK_PIPELINE_CACHE_BLOB_ALIGN 8
50
51 struct vk_pipeline_cache_object_ops {
52 /** Writes this cache object to the given blob
53 *
54 * Because the cache works with both raw blob data and driver object data
55 * and can't always tell the difference between the two, we have to be very
56 * careful about alignments when [de]serializing. When serialize() is
57 * called, the blob will be aligned to VK_PIPELINE_CACHE_BLOB_ALIGN. The
58 * driver must be careful to not [de]serialize any data types which require
59 * a higher alignment. When deserialize() is called, the blob_reader is
60 * also guaranteed to be aligned to VK_PIPELINE_CACHE_BLOB_ALIGN.
61 *
62 * Returns true on success
63 *
64 * This function is optional. Objects without [de]serialization support
65 * will still be cached in memory but will not be placed in the disk cache
66 * and will not exported to the client when vkGetPipelineCacheData() is
67 * called.
68 */
69 bool (*serialize)(struct vk_pipeline_cache_object *object,
70 struct blob *blob);
71
72 /** Constructs an object from cached data
73 *
74 * See serialize() for details about data alignment.
75 *
76 * returns the created object
77 *
78 * This function is optional.
79 */
80 struct vk_pipeline_cache_object *(*deserialize)(struct vk_pipeline_cache *cache,
81 const void *key_data,
82 size_t key_size,
83 struct blob_reader *blob);
84
85 /** Destroys the object
86 *
87 * Called when vk_pipeline_cache_object.ref_cnt hits 0.
88 */
89 void (*destroy)(struct vk_device *device,
90 struct vk_pipeline_cache_object *object);
91 };
92
93 /** Base struct for cached objects
94 *
95 * A vk_pipeline_cache stores any number of vk_pipeline_cache_object's, each
96 * of which has an associated key of arbitrary size. Cached objects are
97 * reference counted so that they can exist in multiple caches (for example,
98 * when vkMergePipelineCaches() is called) and so that they can persist after
99 * the pipeline cache is destroyed. Each object also has a pointer to a
100 * vk_pipeline_cache_object_ops table which the pipeline cache uses to
101 * [de]serialize the object and clean it up when the reference count hits 0.
102 *
103 * The rest of the details of any given object are entirely up to the driver.
104 * The driver may even have multiple types of objects (distinguished by their
105 * vk_pipeline_cache_object_ops table) in the cache so long as it guarantees
106 * it never has two objects of different types with the same key.
107 */
108 struct vk_pipeline_cache_object {
109 const struct vk_pipeline_cache_object_ops *ops;
110 struct vk_pipeline_cache *weak_owner;
111 uint32_t ref_cnt;
112
113 uint32_t data_size;
114 const void *key_data;
115 uint32_t key_size;
116 };
117
118 static inline void
vk_pipeline_cache_object_init(struct vk_device * device,struct vk_pipeline_cache_object * object,const struct vk_pipeline_cache_object_ops * ops,const void * key_data,uint32_t key_size)119 vk_pipeline_cache_object_init(struct vk_device *device,
120 struct vk_pipeline_cache_object *object,
121 const struct vk_pipeline_cache_object_ops *ops,
122 const void *key_data, uint32_t key_size)
123 {
124 memset(object, 0, sizeof(*object));
125 object->ops = ops;
126 p_atomic_set(&object->ref_cnt, 1);
127 object->data_size = 0; /* Unknown */
128 object->key_data = key_data;
129 object->key_size = key_size;
130 }
131
132 static inline void
vk_pipeline_cache_object_finish(struct vk_pipeline_cache_object * object)133 vk_pipeline_cache_object_finish(struct vk_pipeline_cache_object *object)
134 {
135 assert(p_atomic_read(&object->ref_cnt) <= 1);
136 }
137
138 static inline struct vk_pipeline_cache_object *
vk_pipeline_cache_object_ref(struct vk_pipeline_cache_object * object)139 vk_pipeline_cache_object_ref(struct vk_pipeline_cache_object *object)
140 {
141 assert(object && p_atomic_read(&object->ref_cnt) >= 1);
142 p_atomic_inc(&object->ref_cnt);
143 return object;
144 }
145
146 void
147 vk_pipeline_cache_object_unref(struct vk_device *device,
148 struct vk_pipeline_cache_object *object);
149
150 /** A generic implementation of VkPipelineCache */
151 struct vk_pipeline_cache {
152 struct vk_object_base base;
153
154 /* pCreateInfo::flags */
155 VkPipelineCacheCreateFlags flags;
156 bool weak_ref;
157 bool skip_disk_cache;
158 struct disk_cache *disk_cache;
159
160 struct vk_pipeline_cache_header header;
161
162 /** Protects object_cache */
163 simple_mtx_t lock;
164
165 struct set *object_cache;
166 };
167
168 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_pipeline_cache, base, VkPipelineCache,
169 VK_OBJECT_TYPE_PIPELINE_CACHE)
170
171 struct vk_pipeline_cache_create_info {
172 /* The pCreateInfo for this pipeline cache, if any.
173 *
174 * For driver-internal caches, this is allowed to be NULL.
175 */
176 const VkPipelineCacheCreateInfo *pCreateInfo;
177
178 /** If true, ignore VK_ENABLE_PIPELINE_CACHE and enable anyway */
179 bool force_enable;
180
181 /** If true, the cache operates in weak reference mode.
182 *
183 * The weak reference mode is designed for device-global caches for the
184 * purpose of de-duplicating identical shaders and pipelines. In the weak
185 * reference mode, an object's reference count is not incremented when it is
186 * added to the cache. Therefore the object will be destroyed as soon as
187 * there's no external references to it, and the runtime will perform the
188 * necessary bookkeeping to remove the dead reference from this cache's table.
189 *
190 * As the weak reference mode is designed for driver-internal use, it has
191 * several limitations:
192 * - Merging against a weak reference mode cache is not supported.
193 * - Lazy deserialization from vk_raw_data_cache_object_ops is not supported.
194 * - An object can only belong to up to one weak reference mode cache.
195 * - The cache must outlive the object, as the object will try to access its
196 * owner when it's destroyed.
197 */
198 bool weak_ref;
199
200 /** If true, do not attempt to use the disk cache */
201 bool skip_disk_cache;
202
203 /** If non-NULL, use this disk cache object instead of the default one. */
204 struct disk_cache *disk_cache;
205 };
206
207 struct vk_pipeline_cache *
208 vk_pipeline_cache_create(struct vk_device *device,
209 const struct vk_pipeline_cache_create_info *info,
210 const VkAllocationCallbacks *pAllocator);
211 void
212 vk_pipeline_cache_destroy(struct vk_pipeline_cache *cache,
213 const VkAllocationCallbacks *pAllocator);
214
215 /** Attempts to look up an object in the cache by key
216 *
217 * If an object is found in the cache matching the given key, *cache_hit is
218 * set to true and a reference to that object is returned.
219 *
220 * If the driver sets vk_device.disk_cache, we attempt to look up any missing
221 * objects in the disk cache before declaring failure. If an object is found
222 * in the disk cache but not the in-memory cache, *cache_hit is set to false.
223 *
224 * The deserialization of pipeline cache objects found in the cache data
225 * provided via VkPipelineCacheCreateInfo::pInitialData happens during
226 * vk_pipeline_cache_lookup() rather than during vkCreatePipelineCache().
227 * Prior to the first vk_pipeline_cache_lookup() of a given object, it is
228 * stored as an internal raw data object with the same hash. This allows us
229 * to avoid any complex object type tagging in the serialized cache. It does,
230 * however, mean that drivers need to be careful to ensure that objects with
231 * different types (ops) have different keys.
232 *
233 * Returns a reference to the object, if found
234 */
235 struct vk_pipeline_cache_object * MUST_CHECK
236 vk_pipeline_cache_lookup_object(struct vk_pipeline_cache *cache,
237 const void *key_data, size_t key_size,
238 const struct vk_pipeline_cache_object_ops *ops,
239 bool *cache_hit);
240
241 /** Adds an object to the pipeline cache
242 *
243 * This function adds the given object to the pipeline cache. We do not
244 * specify a key here because the key is part of the object. See also
245 * vk_pipeline_cache_object_init().
246 *
247 * This function consumes a reference to the object and returns a reference to
248 * the (possibly different) object in the cache. The intended usage pattern
249 * is as follows:
250 *
251 * key = compute_key();
252 * struct vk_pipeline_cache_object *object =
253 * vk_pipeline_cache_lookup_object(cache, &key, sizeof(key),
254 * &driver_type_ops, &cache_hit);
255 * if (object != NULL)
256 * return container_of(object, driver_type, base);
257 *
258 * object = do_compile();
259 * assert(object != NULL);
260 *
261 * object = vk_pipeline_cache_add_object(cache, object);
262 * return container_of(object, driver_type, base);
263 */
264 struct vk_pipeline_cache_object * MUST_CHECK
265 vk_pipeline_cache_add_object(struct vk_pipeline_cache *cache,
266 struct vk_pipeline_cache_object *object);
267
268 /** Creates and inserts an object into the pipeline cache
269 *
270 * This function takes serialized data and emplaces the deserialized object
271 * into the pipeline cache. It is the responsibility of the caller to
272 * specify a deserialize() function that properly initializes the object.
273 *
274 * This function can be used to avoid an extra serialize() step for
275 * disk-cache insertion. For the intended usage pattern, see
276 * vk_pipeline_cache_add_object().
277 *
278 */
279 struct vk_pipeline_cache_object *
280 vk_pipeline_cache_create_and_insert_object(struct vk_pipeline_cache *cache,
281 const void *key_data, uint32_t key_size,
282 const void *data, size_t data_size,
283 const struct vk_pipeline_cache_object_ops *ops);
284
285 struct nir_shader *
286 vk_pipeline_cache_lookup_nir(struct vk_pipeline_cache *cache,
287 const void *key_data, size_t key_size,
288 const struct nir_shader_compiler_options *nir_options,
289 bool *cache_hit, void *mem_ctx);
290 void
291 vk_pipeline_cache_add_nir(struct vk_pipeline_cache *cache,
292 const void *key_data, size_t key_size,
293 const struct nir_shader *nir);
294
295 /** Specialized type of vk_pipeline_cache_object for raw data objects.
296 *
297 * This cache object implementation, together with vk_raw_data_cache_object_ops,
298 * can be used to cache plain objects as well as already serialized data.
299 */
300 struct vk_raw_data_cache_object {
301 struct vk_pipeline_cache_object base;
302
303 const void *data;
304 size_t data_size;
305 };
306
307 struct vk_raw_data_cache_object *
308 vk_raw_data_cache_object_create(struct vk_device *device,
309 const void *key_data, size_t key_size,
310 const void *data, size_t data_size);
311
312 extern const struct vk_pipeline_cache_object_ops vk_raw_data_cache_object_ops;
313
314 #ifdef __cplusplus
315 }
316 #endif
317
318 #endif /* VK_PIPELINE_CACHE_H */
319