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
24 #include "vk_pipeline_cache.h"
25
26 #include "vk_alloc.h"
27 #include "vk_common_entrypoints.h"
28 #include "vk_device.h"
29 #include "vk_log.h"
30 #include "vk_physical_device.h"
31
32 #include "compiler/nir/nir_serialize.h"
33
34 #include "util/blob.h"
35 #include "util/u_debug.h"
36 #include "util/disk_cache.h"
37 #include "util/hash_table.h"
38 #include "util/set.h"
39
40 #define vk_pipeline_cache_log(cache, ...) \
41 if (cache->base.client_visible) \
42 vk_logw(VK_LOG_OBJS(cache), __VA_ARGS__)
43
44 static bool
vk_raw_data_cache_object_serialize(struct vk_pipeline_cache_object * object,struct blob * blob)45 vk_raw_data_cache_object_serialize(struct vk_pipeline_cache_object *object,
46 struct blob *blob)
47 {
48 struct vk_raw_data_cache_object *data_obj =
49 container_of(object, struct vk_raw_data_cache_object, base);
50
51 blob_write_bytes(blob, data_obj->data, data_obj->data_size);
52
53 return true;
54 }
55
56 static struct vk_pipeline_cache_object *
vk_raw_data_cache_object_deserialize(struct vk_pipeline_cache * cache,const void * key_data,size_t key_size,struct blob_reader * blob)57 vk_raw_data_cache_object_deserialize(struct vk_pipeline_cache *cache,
58 const void *key_data,
59 size_t key_size,
60 struct blob_reader *blob)
61 {
62 /* We consume the entire blob_reader. Each call to ops->deserialize()
63 * happens with a brand new blob reader for error checking anyway so we
64 * can assume the blob consumes the entire reader and we don't need to
65 * serialize the data size separately.
66 */
67 assert(blob->current < blob->end);
68 size_t data_size = blob->end - blob->current;
69 const void *data = blob_read_bytes(blob, data_size);
70
71 struct vk_raw_data_cache_object *data_obj =
72 vk_raw_data_cache_object_create(cache->base.device, key_data, key_size,
73 data, data_size);
74
75 return data_obj ? &data_obj->base : NULL;
76 }
77
78 static void
vk_raw_data_cache_object_destroy(struct vk_device * device,struct vk_pipeline_cache_object * object)79 vk_raw_data_cache_object_destroy(struct vk_device *device,
80 struct vk_pipeline_cache_object *object)
81 {
82 struct vk_raw_data_cache_object *data_obj =
83 container_of(object, struct vk_raw_data_cache_object, base);
84
85 vk_free(&device->alloc, data_obj);
86 }
87
88 const struct vk_pipeline_cache_object_ops vk_raw_data_cache_object_ops = {
89 .serialize = vk_raw_data_cache_object_serialize,
90 .deserialize = vk_raw_data_cache_object_deserialize,
91 .destroy = vk_raw_data_cache_object_destroy,
92 };
93
94 struct vk_raw_data_cache_object *
vk_raw_data_cache_object_create(struct vk_device * device,const void * key_data,size_t key_size,const void * data,size_t data_size)95 vk_raw_data_cache_object_create(struct vk_device *device,
96 const void *key_data, size_t key_size,
97 const void *data, size_t data_size)
98 {
99 VK_MULTIALLOC(ma);
100 VK_MULTIALLOC_DECL(&ma, struct vk_raw_data_cache_object, data_obj, 1);
101 VK_MULTIALLOC_DECL_SIZE(&ma, char, obj_key_data, key_size);
102 VK_MULTIALLOC_DECL_SIZE(&ma, char, obj_data, data_size);
103
104 if (!vk_multialloc_alloc(&ma, &device->alloc,
105 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
106 return NULL;
107
108 vk_pipeline_cache_object_init(device, &data_obj->base,
109 &vk_raw_data_cache_object_ops,
110 obj_key_data, key_size);
111 data_obj->data = obj_data;
112 data_obj->data_size = data_size;
113
114 memcpy(obj_key_data, key_data, key_size);
115 memcpy(obj_data, data, data_size);
116
117 return data_obj;
118 }
119
120 static bool
object_keys_equal(const void * void_a,const void * void_b)121 object_keys_equal(const void *void_a, const void *void_b)
122 {
123 const struct vk_pipeline_cache_object *a = void_a, *b = void_b;
124 if (a->key_size != b->key_size)
125 return false;
126
127 return memcmp(a->key_data, b->key_data, a->key_size) == 0;
128 }
129
130 static uint32_t
object_key_hash(const void * void_object)131 object_key_hash(const void *void_object)
132 {
133 const struct vk_pipeline_cache_object *object = void_object;
134 return _mesa_hash_data(object->key_data, object->key_size);
135 }
136
137 static void
vk_pipeline_cache_lock(struct vk_pipeline_cache * cache)138 vk_pipeline_cache_lock(struct vk_pipeline_cache *cache)
139 {
140
141 if (!(cache->flags & VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT))
142 simple_mtx_lock(&cache->lock);
143 }
144
145 static void
vk_pipeline_cache_unlock(struct vk_pipeline_cache * cache)146 vk_pipeline_cache_unlock(struct vk_pipeline_cache *cache)
147 {
148 if (!(cache->flags & VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT))
149 simple_mtx_unlock(&cache->lock);
150 }
151
152 /* cache->lock must be held when calling */
153 static void
vk_pipeline_cache_remove_object(struct vk_pipeline_cache * cache,uint32_t hash,struct vk_pipeline_cache_object * object)154 vk_pipeline_cache_remove_object(struct vk_pipeline_cache *cache,
155 uint32_t hash,
156 struct vk_pipeline_cache_object *object)
157 {
158 struct set_entry *entry =
159 _mesa_set_search_pre_hashed(cache->object_cache, hash, object);
160 if (entry && entry->key == (const void *)object) {
161 /* Drop the reference owned by the cache */
162 if (!cache->weak_ref)
163 vk_pipeline_cache_object_unref(cache->base.device, object);
164
165 _mesa_set_remove(cache->object_cache, entry);
166 }
167 }
168
169 static inline struct vk_pipeline_cache_object *
vk_pipeline_cache_object_weak_ref(struct vk_pipeline_cache * cache,struct vk_pipeline_cache_object * object)170 vk_pipeline_cache_object_weak_ref(struct vk_pipeline_cache *cache,
171 struct vk_pipeline_cache_object *object)
172 {
173 assert(!object->weak_owner);
174 p_atomic_set(&object->weak_owner, cache);
175 return object;
176 }
177
178 void
vk_pipeline_cache_object_unref(struct vk_device * device,struct vk_pipeline_cache_object * object)179 vk_pipeline_cache_object_unref(struct vk_device *device, struct vk_pipeline_cache_object *object)
180 {
181 assert(object && p_atomic_read(&object->ref_cnt) >= 1);
182
183 struct vk_pipeline_cache *weak_owner = p_atomic_read(&object->weak_owner);
184 if (!weak_owner) {
185 if (p_atomic_dec_zero(&object->ref_cnt))
186 object->ops->destroy(device, object);
187 } else {
188 vk_pipeline_cache_lock(weak_owner);
189 bool destroy = p_atomic_dec_zero(&object->ref_cnt);
190 if (destroy) {
191 uint32_t hash = object_key_hash(object);
192 vk_pipeline_cache_remove_object(weak_owner, hash, object);
193 }
194 vk_pipeline_cache_unlock(weak_owner);
195 if (destroy)
196 object->ops->destroy(device, object);
197 }
198 }
199
200 static bool
vk_pipeline_cache_object_serialize(struct vk_pipeline_cache * cache,struct vk_pipeline_cache_object * object,struct blob * blob,uint32_t * data_size)201 vk_pipeline_cache_object_serialize(struct vk_pipeline_cache *cache,
202 struct vk_pipeline_cache_object *object,
203 struct blob *blob, uint32_t *data_size)
204 {
205 if (object->ops->serialize == NULL)
206 return false;
207
208 assert(blob->size == align64(blob->size, VK_PIPELINE_CACHE_BLOB_ALIGN));
209 size_t start = blob->size;
210
211 /* Special case for if we're writing to a NULL blob (just to get the size)
212 * and we already know the data size of the allocation. This should make
213 * the first GetPipelineCacheData() call to get the data size faster in the
214 * common case where a bunch of our objects were loaded from a previous
215 * cache or where we've already serialized the cache once.
216 */
217 if (blob->data == NULL && blob->fixed_allocation) {
218 *data_size = p_atomic_read(&object->data_size);
219 if (*data_size > 0) {
220 blob_write_bytes(blob, NULL, *data_size);
221 return true;
222 }
223 }
224
225 if (!object->ops->serialize(object, blob)) {
226 vk_pipeline_cache_log(cache, "Failed to serialize pipeline cache object");
227 return false;
228 }
229
230 size_t size = blob->size - start;
231 if (size > UINT32_MAX) {
232 vk_pipeline_cache_log(cache, "Skipping giant (4 GiB or larger) object");
233 return false;
234 }
235
236 if (blob->out_of_memory) {
237 vk_pipeline_cache_log(cache,
238 "Insufficient memory for pipeline cache data");
239 return false;
240 }
241
242 *data_size = (uint32_t)size;
243 p_atomic_set(&object->data_size, *data_size);
244
245 return true;
246 }
247
248 static struct vk_pipeline_cache_object *
vk_pipeline_cache_object_deserialize(struct vk_pipeline_cache * cache,const void * key_data,uint32_t key_size,const void * data,size_t data_size,const struct vk_pipeline_cache_object_ops * ops)249 vk_pipeline_cache_object_deserialize(struct vk_pipeline_cache *cache,
250 const void *key_data, uint32_t key_size,
251 const void *data, size_t data_size,
252 const struct vk_pipeline_cache_object_ops *ops)
253 {
254 if (ops == NULL)
255 ops = &vk_raw_data_cache_object_ops;
256
257 if (unlikely(ops->deserialize == NULL)) {
258 vk_pipeline_cache_log(cache,
259 "Pipeline cache object cannot be deserialized");
260 return NULL;
261 }
262
263 struct blob_reader reader;
264 blob_reader_init(&reader, data, data_size);
265
266 struct vk_pipeline_cache_object *object =
267 ops->deserialize(cache, key_data, key_size, &reader);
268
269 if (object == NULL)
270 return NULL;
271
272 assert(reader.current == reader.end && !reader.overrun);
273 assert(object->ops == ops);
274 assert(object->ref_cnt == 1);
275 assert(object->key_size == key_size);
276 assert(memcmp(object->key_data, key_data, key_size) == 0);
277
278 return object;
279 }
280
281 static struct vk_pipeline_cache_object *
vk_pipeline_cache_insert_object(struct vk_pipeline_cache * cache,struct vk_pipeline_cache_object * object)282 vk_pipeline_cache_insert_object(struct vk_pipeline_cache *cache,
283 struct vk_pipeline_cache_object *object)
284 {
285 assert(object->ops != NULL);
286
287 if (cache->object_cache == NULL)
288 return object;
289
290 uint32_t hash = object_key_hash(object);
291
292 vk_pipeline_cache_lock(cache);
293 bool found = false;
294 struct set_entry *entry = _mesa_set_search_or_add_pre_hashed(
295 cache->object_cache, hash, object, &found);
296
297 struct vk_pipeline_cache_object *result = NULL;
298 /* add reference to either the found or inserted object */
299 if (found) {
300 struct vk_pipeline_cache_object *found_object = (void *)entry->key;
301 if (found_object->ops != object->ops) {
302 /* The found object in the cache isn't fully formed. Replace it. */
303 assert(!cache->weak_ref);
304 assert(found_object->ops == &vk_raw_data_cache_object_ops);
305 assert(object->ref_cnt == 1);
306 entry->key = object;
307 object = found_object;
308 }
309
310 result = vk_pipeline_cache_object_ref((void *)entry->key);
311 } else {
312 result = object;
313 if (!cache->weak_ref)
314 vk_pipeline_cache_object_ref(result);
315 else
316 vk_pipeline_cache_object_weak_ref(cache, result);
317 }
318 vk_pipeline_cache_unlock(cache);
319
320 if (found) {
321 vk_pipeline_cache_object_unref(cache->base.device, object);
322 }
323 return result;
324 }
325
326 struct vk_pipeline_cache_object *
vk_pipeline_cache_lookup_object(struct vk_pipeline_cache * cache,const void * key_data,size_t key_size,const struct vk_pipeline_cache_object_ops * ops,bool * cache_hit)327 vk_pipeline_cache_lookup_object(struct vk_pipeline_cache *cache,
328 const void *key_data, size_t key_size,
329 const struct vk_pipeline_cache_object_ops *ops,
330 bool *cache_hit)
331 {
332 assert(key_size <= UINT32_MAX);
333 assert(ops != NULL);
334
335 if (cache_hit != NULL)
336 *cache_hit = false;
337
338 struct vk_pipeline_cache_object key = {
339 .key_data = key_data,
340 .key_size = key_size,
341 };
342 uint32_t hash = object_key_hash(&key);
343
344 struct vk_pipeline_cache_object *object = NULL;
345
346 if (cache != NULL && cache->object_cache != NULL) {
347 vk_pipeline_cache_lock(cache);
348 struct set_entry *entry =
349 _mesa_set_search_pre_hashed(cache->object_cache, hash, &key);
350 if (entry) {
351 object = vk_pipeline_cache_object_ref((void *)entry->key);
352 if (cache_hit != NULL)
353 *cache_hit = true;
354 }
355 vk_pipeline_cache_unlock(cache);
356 }
357
358 if (object == NULL) {
359 struct disk_cache *disk_cache = cache->base.device->physical->disk_cache;
360 if (!cache->skip_disk_cache && disk_cache && cache->object_cache) {
361 cache_key cache_key;
362 disk_cache_compute_key(disk_cache, key_data, key_size, cache_key);
363
364 size_t data_size;
365 uint8_t *data = disk_cache_get(disk_cache, cache_key, &data_size);
366 if (data) {
367 object = vk_pipeline_cache_object_deserialize(cache,
368 key_data, key_size,
369 data, data_size,
370 ops);
371 free(data);
372 if (object != NULL) {
373 return vk_pipeline_cache_insert_object(cache, object);
374 }
375 }
376 }
377
378 /* No disk cache or not found in the disk cache */
379 return NULL;
380 }
381
382 if (object->ops == &vk_raw_data_cache_object_ops &&
383 ops != &vk_raw_data_cache_object_ops) {
384 /* The object isn't fully formed yet and we need to deserialize it into
385 * a real object before it can be used.
386 */
387 struct vk_raw_data_cache_object *data_obj =
388 container_of(object, struct vk_raw_data_cache_object, base);
389
390 struct vk_pipeline_cache_object *real_object =
391 vk_pipeline_cache_object_deserialize(cache,
392 data_obj->base.key_data,
393 data_obj->base.key_size,
394 data_obj->data,
395 data_obj->data_size, ops);
396 if (real_object == NULL) {
397 vk_pipeline_cache_log(cache,
398 "Deserializing pipeline cache object failed");
399
400 vk_pipeline_cache_lock(cache);
401 vk_pipeline_cache_remove_object(cache, hash, object);
402 vk_pipeline_cache_unlock(cache);
403 vk_pipeline_cache_object_unref(cache->base.device, object);
404 return NULL;
405 }
406
407 vk_pipeline_cache_object_unref(cache->base.device, object);
408 object = vk_pipeline_cache_insert_object(cache, real_object);
409 }
410
411 assert(object->ops == ops);
412
413 return object;
414 }
415
416 struct vk_pipeline_cache_object *
vk_pipeline_cache_add_object(struct vk_pipeline_cache * cache,struct vk_pipeline_cache_object * object)417 vk_pipeline_cache_add_object(struct vk_pipeline_cache *cache,
418 struct vk_pipeline_cache_object *object)
419 {
420 struct vk_pipeline_cache_object *inserted =
421 vk_pipeline_cache_insert_object(cache, object);
422
423 if (object == inserted) {
424 /* If it wasn't in the object cache, it might not be in the disk cache
425 * either. Better try and add it.
426 */
427
428 struct disk_cache *disk_cache = cache->base.device->physical->disk_cache;
429 if (!cache->skip_disk_cache && object->ops->serialize && disk_cache) {
430 struct blob blob;
431 blob_init(&blob);
432
433 if (object->ops->serialize(object, &blob) && !blob.out_of_memory) {
434 cache_key cache_key;
435 disk_cache_compute_key(disk_cache, object->key_data,
436 object->key_size, cache_key);
437
438 disk_cache_put(disk_cache, cache_key, blob.data, blob.size, NULL);
439 }
440
441 blob_finish(&blob);
442 }
443 }
444
445 return inserted;
446 }
447
448 struct vk_pipeline_cache_object *
vk_pipeline_cache_create_and_insert_object(struct vk_pipeline_cache * cache,const void * key_data,uint32_t key_size,const void * data,size_t data_size,const struct vk_pipeline_cache_object_ops * ops)449 vk_pipeline_cache_create_and_insert_object(struct vk_pipeline_cache *cache,
450 const void *key_data, uint32_t key_size,
451 const void *data, size_t data_size,
452 const struct vk_pipeline_cache_object_ops *ops)
453 {
454 struct disk_cache *disk_cache = cache->base.device->physical->disk_cache;
455 if (!cache->skip_disk_cache && disk_cache) {
456 cache_key cache_key;
457 disk_cache_compute_key(disk_cache, key_data, key_size, cache_key);
458 disk_cache_put(disk_cache, cache_key, data, data_size, NULL);
459 }
460
461 struct vk_pipeline_cache_object *object =
462 vk_pipeline_cache_object_deserialize(cache, key_data, key_size, data,
463 data_size, ops);
464
465 if (object)
466 object = vk_pipeline_cache_insert_object(cache, object);
467
468 return object;
469 }
470
471 nir_shader *
vk_pipeline_cache_lookup_nir(struct vk_pipeline_cache * cache,const void * key_data,size_t key_size,const struct nir_shader_compiler_options * nir_options,bool * cache_hit,void * mem_ctx)472 vk_pipeline_cache_lookup_nir(struct vk_pipeline_cache *cache,
473 const void *key_data, size_t key_size,
474 const struct nir_shader_compiler_options *nir_options,
475 bool *cache_hit, void *mem_ctx)
476 {
477 struct vk_pipeline_cache_object *object =
478 vk_pipeline_cache_lookup_object(cache, key_data, key_size,
479 &vk_raw_data_cache_object_ops,
480 cache_hit);
481 if (object == NULL)
482 return NULL;
483
484 struct vk_raw_data_cache_object *data_obj =
485 container_of(object, struct vk_raw_data_cache_object, base);
486
487 struct blob_reader blob;
488 blob_reader_init(&blob, data_obj->data, data_obj->data_size);
489
490 nir_shader *nir = nir_deserialize(mem_ctx, nir_options, &blob);
491 vk_pipeline_cache_object_unref(cache->base.device, object);
492
493 if (blob.overrun) {
494 ralloc_free(nir);
495 return NULL;
496 }
497
498 return nir;
499 }
500
501 void
vk_pipeline_cache_add_nir(struct vk_pipeline_cache * cache,const void * key_data,size_t key_size,const nir_shader * nir)502 vk_pipeline_cache_add_nir(struct vk_pipeline_cache *cache,
503 const void *key_data, size_t key_size,
504 const nir_shader *nir)
505 {
506 struct blob blob;
507 blob_init(&blob);
508
509 nir_serialize(&blob, nir, false);
510 if (blob.out_of_memory) {
511 vk_pipeline_cache_log(cache, "Ran out of memory serializing NIR shader");
512 blob_finish(&blob);
513 return;
514 }
515
516 struct vk_raw_data_cache_object *data_obj =
517 vk_raw_data_cache_object_create(cache->base.device,
518 key_data, key_size,
519 blob.data, blob.size);
520 blob_finish(&blob);
521 if (data_obj == NULL) {
522 vk_pipeline_cache_log(cache, "Ran out of memory creating NIR shader");
523 return;
524 }
525
526 struct vk_pipeline_cache_object *cached =
527 vk_pipeline_cache_add_object(cache, &data_obj->base);
528 vk_pipeline_cache_object_unref(cache->base.device, cached);
529 }
530
531 static int32_t
find_type_for_ops(const struct vk_physical_device * pdevice,const struct vk_pipeline_cache_object_ops * ops)532 find_type_for_ops(const struct vk_physical_device *pdevice,
533 const struct vk_pipeline_cache_object_ops *ops)
534 {
535 const struct vk_pipeline_cache_object_ops *const *import_ops =
536 pdevice->pipeline_cache_import_ops;
537
538 if (import_ops == NULL)
539 return -1;
540
541 for (int32_t i = 0; import_ops[i]; i++) {
542 if (import_ops[i] == ops)
543 return i;
544 }
545
546 return -1;
547 }
548
549 static const struct vk_pipeline_cache_object_ops *
find_ops_for_type(const struct vk_physical_device * pdevice,int32_t type)550 find_ops_for_type(const struct vk_physical_device *pdevice,
551 int32_t type)
552 {
553 const struct vk_pipeline_cache_object_ops *const *import_ops =
554 pdevice->pipeline_cache_import_ops;
555
556 if (import_ops == NULL || type < 0)
557 return NULL;
558
559 return import_ops[type];
560 }
561
562 static void
vk_pipeline_cache_load(struct vk_pipeline_cache * cache,const void * data,size_t size)563 vk_pipeline_cache_load(struct vk_pipeline_cache *cache,
564 const void *data, size_t size)
565 {
566 struct blob_reader blob;
567 blob_reader_init(&blob, data, size);
568
569 struct vk_pipeline_cache_header header;
570 blob_copy_bytes(&blob, &header, sizeof(header));
571 uint32_t count = blob_read_uint32(&blob);
572 if (blob.overrun)
573 return;
574
575 if (memcmp(&header, &cache->header, sizeof(header)) != 0)
576 return;
577
578 for (uint32_t i = 0; i < count; i++) {
579 int32_t type = blob_read_uint32(&blob);
580 uint32_t key_size = blob_read_uint32(&blob);
581 uint32_t data_size = blob_read_uint32(&blob);
582 const void *key_data = blob_read_bytes(&blob, key_size);
583 blob_reader_align(&blob, VK_PIPELINE_CACHE_BLOB_ALIGN);
584 const void *data = blob_read_bytes(&blob, data_size);
585 if (blob.overrun)
586 break;
587
588 const struct vk_pipeline_cache_object_ops *ops =
589 find_ops_for_type(cache->base.device->physical, type);
590
591 struct vk_pipeline_cache_object *object =
592 vk_pipeline_cache_create_and_insert_object(cache, key_data, key_size,
593 data, data_size, ops);
594
595 if (object == NULL) {
596 vk_pipeline_cache_log(cache, "Failed to load pipeline cache object");
597 continue;
598 }
599
600 vk_pipeline_cache_object_unref(cache->base.device, object);
601 }
602 }
603
604 struct vk_pipeline_cache *
vk_pipeline_cache_create(struct vk_device * device,const struct vk_pipeline_cache_create_info * info,const VkAllocationCallbacks * pAllocator)605 vk_pipeline_cache_create(struct vk_device *device,
606 const struct vk_pipeline_cache_create_info *info,
607 const VkAllocationCallbacks *pAllocator)
608 {
609 static const struct VkPipelineCacheCreateInfo default_create_info = {
610 .sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO,
611 };
612 struct vk_pipeline_cache *cache;
613
614 const struct VkPipelineCacheCreateInfo *pCreateInfo =
615 info->pCreateInfo != NULL ? info->pCreateInfo : &default_create_info;
616
617 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
618
619 cache = vk_object_zalloc(device, pAllocator, sizeof(*cache),
620 VK_OBJECT_TYPE_PIPELINE_CACHE);
621 if (cache == NULL)
622 return NULL;
623
624 cache->flags = pCreateInfo->flags;
625 cache->weak_ref = info->weak_ref;
626 #ifndef ENABLE_SHADER_CACHE
627 cache->skip_disk_cache = true;
628 #else
629 cache->skip_disk_cache = info->skip_disk_cache;
630 #endif
631
632 struct VkPhysicalDeviceProperties pdevice_props;
633 device->physical->dispatch_table.GetPhysicalDeviceProperties(
634 vk_physical_device_to_handle(device->physical), &pdevice_props);
635
636 cache->header = (struct vk_pipeline_cache_header) {
637 .header_size = sizeof(struct vk_pipeline_cache_header),
638 .header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE,
639 .vendor_id = pdevice_props.vendorID,
640 .device_id = pdevice_props.deviceID,
641 };
642 memcpy(cache->header.uuid, pdevice_props.pipelineCacheUUID, VK_UUID_SIZE);
643
644 simple_mtx_init(&cache->lock, mtx_plain);
645
646 if (info->force_enable ||
647 debug_get_bool_option("VK_ENABLE_PIPELINE_CACHE", true)) {
648 cache->object_cache = _mesa_set_create(NULL, object_key_hash,
649 object_keys_equal);
650 }
651
652 if (cache->object_cache && pCreateInfo->initialDataSize > 0) {
653 vk_pipeline_cache_load(cache, pCreateInfo->pInitialData,
654 pCreateInfo->initialDataSize);
655 }
656
657 return cache;
658 }
659
660 void
vk_pipeline_cache_destroy(struct vk_pipeline_cache * cache,const VkAllocationCallbacks * pAllocator)661 vk_pipeline_cache_destroy(struct vk_pipeline_cache *cache,
662 const VkAllocationCallbacks *pAllocator)
663 {
664 if (cache->object_cache) {
665 if (!cache->weak_ref) {
666 set_foreach(cache->object_cache, entry) {
667 vk_pipeline_cache_object_unref(cache->base.device, (void *)entry->key);
668 }
669 } else {
670 assert(cache->object_cache->entries == 0);
671 }
672 _mesa_set_destroy(cache->object_cache, NULL);
673 }
674 simple_mtx_destroy(&cache->lock);
675 vk_object_free(cache->base.device, pAllocator, cache);
676 }
677
678 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_CreatePipelineCache(VkDevice _device,const VkPipelineCacheCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkPipelineCache * pPipelineCache)679 vk_common_CreatePipelineCache(VkDevice _device,
680 const VkPipelineCacheCreateInfo *pCreateInfo,
681 const VkAllocationCallbacks *pAllocator,
682 VkPipelineCache *pPipelineCache)
683 {
684 VK_FROM_HANDLE(vk_device, device, _device);
685 struct vk_pipeline_cache *cache;
686
687 struct vk_pipeline_cache_create_info info = {
688 .pCreateInfo = pCreateInfo,
689 .skip_disk_cache = device->disable_internal_cache,
690 };
691 cache = vk_pipeline_cache_create(device, &info, pAllocator);
692 if (cache == NULL)
693 return VK_ERROR_OUT_OF_HOST_MEMORY;
694
695 *pPipelineCache = vk_pipeline_cache_to_handle(cache);
696
697 return VK_SUCCESS;
698 }
699
700 VKAPI_ATTR void VKAPI_CALL
vk_common_DestroyPipelineCache(VkDevice device,VkPipelineCache pipelineCache,const VkAllocationCallbacks * pAllocator)701 vk_common_DestroyPipelineCache(VkDevice device,
702 VkPipelineCache pipelineCache,
703 const VkAllocationCallbacks *pAllocator)
704 {
705 VK_FROM_HANDLE(vk_pipeline_cache, cache, pipelineCache);
706
707 if (cache == NULL)
708 return;
709
710 assert(cache->base.device == vk_device_from_handle(device));
711 vk_pipeline_cache_destroy(cache, pAllocator);
712 }
713
714 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_GetPipelineCacheData(VkDevice _device,VkPipelineCache pipelineCache,size_t * pDataSize,void * pData)715 vk_common_GetPipelineCacheData(VkDevice _device,
716 VkPipelineCache pipelineCache,
717 size_t *pDataSize,
718 void *pData)
719 {
720 VK_FROM_HANDLE(vk_device, device, _device);
721 VK_FROM_HANDLE(vk_pipeline_cache, cache, pipelineCache);
722
723 struct blob blob;
724 if (pData) {
725 blob_init_fixed(&blob, pData, *pDataSize);
726 } else {
727 blob_init_fixed(&blob, NULL, SIZE_MAX);
728 }
729
730 blob_write_bytes(&blob, &cache->header, sizeof(cache->header));
731
732 uint32_t count = 0;
733 intptr_t count_offset = blob_reserve_uint32(&blob);
734 if (count_offset < 0) {
735 *pDataSize = 0;
736 blob_finish(&blob);
737 return VK_INCOMPLETE;
738 }
739
740 vk_pipeline_cache_lock(cache);
741
742 VkResult result = VK_SUCCESS;
743 if (cache->object_cache != NULL) {
744 set_foreach(cache->object_cache, entry) {
745 struct vk_pipeline_cache_object *object = (void *)entry->key;
746
747 if (object->ops->serialize == NULL)
748 continue;
749
750 size_t blob_size_save = blob.size;
751
752 int32_t type = find_type_for_ops(device->physical, object->ops);
753 blob_write_uint32(&blob, type);
754 blob_write_uint32(&blob, object->key_size);
755 intptr_t data_size_resv = blob_reserve_uint32(&blob);
756 blob_write_bytes(&blob, object->key_data, object->key_size);
757
758 if (!blob_align(&blob, VK_PIPELINE_CACHE_BLOB_ALIGN)) {
759 result = VK_INCOMPLETE;
760 break;
761 }
762
763 uint32_t data_size;
764 if (!vk_pipeline_cache_object_serialize(cache, object,
765 &blob, &data_size)) {
766 blob.size = blob_size_save;
767 if (blob.out_of_memory) {
768 result = VK_INCOMPLETE;
769 break;
770 }
771
772 /* Failed for some other reason; keep going */
773 continue;
774 }
775
776 /* vk_pipeline_cache_object_serialize should have failed */
777 assert(!blob.out_of_memory);
778
779 assert(data_size_resv >= 0);
780 blob_overwrite_uint32(&blob, data_size_resv, data_size);
781
782 count++;
783 }
784 }
785
786 vk_pipeline_cache_unlock(cache);
787
788 blob_overwrite_uint32(&blob, count_offset, count);
789
790 *pDataSize = blob.size;
791
792 blob_finish(&blob);
793
794 return result;
795 }
796
797 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_MergePipelineCaches(VkDevice _device,VkPipelineCache dstCache,uint32_t srcCacheCount,const VkPipelineCache * pSrcCaches)798 vk_common_MergePipelineCaches(VkDevice _device,
799 VkPipelineCache dstCache,
800 uint32_t srcCacheCount,
801 const VkPipelineCache *pSrcCaches)
802 {
803 VK_FROM_HANDLE(vk_pipeline_cache, dst, dstCache);
804 VK_FROM_HANDLE(vk_device, device, _device);
805 assert(dst->base.device == device);
806 assert(!dst->weak_ref);
807
808 if (!dst->object_cache)
809 return VK_SUCCESS;
810
811 vk_pipeline_cache_lock(dst);
812
813 for (uint32_t i = 0; i < srcCacheCount; i++) {
814 VK_FROM_HANDLE(vk_pipeline_cache, src, pSrcCaches[i]);
815 assert(src->base.device == device);
816
817 if (!src->object_cache)
818 continue;
819
820 assert(src != dst);
821 if (src == dst)
822 continue;
823
824 vk_pipeline_cache_lock(src);
825
826 set_foreach(src->object_cache, src_entry) {
827 struct vk_pipeline_cache_object *src_object = (void *)src_entry->key;
828
829 bool found_in_dst = false;
830 struct set_entry *dst_entry =
831 _mesa_set_search_or_add_pre_hashed(dst->object_cache,
832 src_entry->hash,
833 src_object, &found_in_dst);
834 if (found_in_dst) {
835 struct vk_pipeline_cache_object *dst_object = (void *)dst_entry->key;
836 if (dst_object->ops == &vk_raw_data_cache_object_ops &&
837 src_object->ops != &vk_raw_data_cache_object_ops) {
838 /* Even though dst has the object, it only has the blob version
839 * which isn't as useful. Replace it with the real object.
840 */
841 vk_pipeline_cache_object_unref(device, dst_object);
842 dst_entry->key = vk_pipeline_cache_object_ref(src_object);
843 }
844 } else {
845 /* We inserted src_object in dst so it needs a reference */
846 assert(dst_entry->key == (const void *)src_object);
847 vk_pipeline_cache_object_ref(src_object);
848 }
849 }
850
851 vk_pipeline_cache_unlock(src);
852 }
853
854 vk_pipeline_cache_unlock(dst);
855
856 return VK_SUCCESS;
857 }
858