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 
522    struct vk_pipeline_cache_object *cached =
523       vk_pipeline_cache_add_object(cache, &data_obj->base);
524    vk_pipeline_cache_object_unref(cache->base.device, cached);
525 }
526 
527 static int32_t
find_type_for_ops(const struct vk_physical_device * pdevice,const struct vk_pipeline_cache_object_ops * ops)528 find_type_for_ops(const struct vk_physical_device *pdevice,
529                   const struct vk_pipeline_cache_object_ops *ops)
530 {
531    const struct vk_pipeline_cache_object_ops *const *import_ops =
532       pdevice->pipeline_cache_import_ops;
533 
534    if (import_ops == NULL)
535       return -1;
536 
537    for (int32_t i = 0; import_ops[i]; i++) {
538       if (import_ops[i] == ops)
539          return i;
540    }
541 
542    return -1;
543 }
544 
545 static const struct vk_pipeline_cache_object_ops *
find_ops_for_type(const struct vk_physical_device * pdevice,int32_t type)546 find_ops_for_type(const struct vk_physical_device *pdevice,
547                   int32_t type)
548 {
549    const struct vk_pipeline_cache_object_ops *const *import_ops =
550       pdevice->pipeline_cache_import_ops;
551 
552    if (import_ops == NULL || type < 0)
553       return NULL;
554 
555    return import_ops[type];
556 }
557 
558 static void
vk_pipeline_cache_load(struct vk_pipeline_cache * cache,const void * data,size_t size)559 vk_pipeline_cache_load(struct vk_pipeline_cache *cache,
560                        const void *data, size_t size)
561 {
562    struct blob_reader blob;
563    blob_reader_init(&blob, data, size);
564 
565    struct vk_pipeline_cache_header header;
566    blob_copy_bytes(&blob, &header, sizeof(header));
567    uint32_t count = blob_read_uint32(&blob);
568    if (blob.overrun)
569       return;
570 
571    if (memcmp(&header, &cache->header, sizeof(header)) != 0)
572       return;
573 
574    for (uint32_t i = 0; i < count; i++) {
575       int32_t type = blob_read_uint32(&blob);
576       uint32_t key_size = blob_read_uint32(&blob);
577       uint32_t data_size = blob_read_uint32(&blob);
578       const void *key_data = blob_read_bytes(&blob, key_size);
579       blob_reader_align(&blob, VK_PIPELINE_CACHE_BLOB_ALIGN);
580       const void *data = blob_read_bytes(&blob, data_size);
581       if (blob.overrun)
582          break;
583 
584       const struct vk_pipeline_cache_object_ops *ops =
585          find_ops_for_type(cache->base.device->physical, type);
586 
587       struct vk_pipeline_cache_object *object =
588          vk_pipeline_cache_create_and_insert_object(cache, key_data, key_size,
589                                                     data, data_size, ops);
590 
591       if (object == NULL) {
592          vk_pipeline_cache_log(cache, "Failed to load pipeline cache object");
593          continue;
594       }
595 
596       vk_pipeline_cache_object_unref(cache->base.device, object);
597    }
598 }
599 
600 struct vk_pipeline_cache *
vk_pipeline_cache_create(struct vk_device * device,const struct vk_pipeline_cache_create_info * info,const VkAllocationCallbacks * pAllocator)601 vk_pipeline_cache_create(struct vk_device *device,
602                          const struct vk_pipeline_cache_create_info *info,
603                          const VkAllocationCallbacks *pAllocator)
604 {
605    static const struct VkPipelineCacheCreateInfo default_create_info = {
606       .sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO,
607    };
608    struct vk_pipeline_cache *cache;
609 
610    const struct VkPipelineCacheCreateInfo *pCreateInfo =
611       info->pCreateInfo != NULL ? info->pCreateInfo : &default_create_info;
612 
613    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
614 
615    cache = vk_object_zalloc(device, pAllocator, sizeof(*cache),
616                             VK_OBJECT_TYPE_PIPELINE_CACHE);
617    if (cache == NULL)
618       return NULL;
619 
620    cache->flags = pCreateInfo->flags;
621    cache->weak_ref = info->weak_ref;
622 #ifndef ENABLE_SHADER_CACHE
623    cache->skip_disk_cache = true;
624 #else
625    cache->skip_disk_cache = info->skip_disk_cache;
626 #endif
627 
628    struct VkPhysicalDeviceProperties pdevice_props;
629    device->physical->dispatch_table.GetPhysicalDeviceProperties(
630       vk_physical_device_to_handle(device->physical), &pdevice_props);
631 
632    cache->header = (struct vk_pipeline_cache_header) {
633       .header_size = sizeof(struct vk_pipeline_cache_header),
634       .header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE,
635       .vendor_id = pdevice_props.vendorID,
636       .device_id = pdevice_props.deviceID,
637    };
638    memcpy(cache->header.uuid, pdevice_props.pipelineCacheUUID, VK_UUID_SIZE);
639 
640    simple_mtx_init(&cache->lock, mtx_plain);
641 
642    if (info->force_enable ||
643        debug_get_bool_option("VK_ENABLE_PIPELINE_CACHE", true)) {
644       cache->object_cache = _mesa_set_create(NULL, object_key_hash,
645                                              object_keys_equal);
646    }
647 
648    if (cache->object_cache && pCreateInfo->initialDataSize > 0) {
649       vk_pipeline_cache_load(cache, pCreateInfo->pInitialData,
650                              pCreateInfo->initialDataSize);
651    }
652 
653    return cache;
654 }
655 
656 void
vk_pipeline_cache_destroy(struct vk_pipeline_cache * cache,const VkAllocationCallbacks * pAllocator)657 vk_pipeline_cache_destroy(struct vk_pipeline_cache *cache,
658                           const VkAllocationCallbacks *pAllocator)
659 {
660    if (cache->object_cache) {
661       if (!cache->weak_ref) {
662          set_foreach_remove(cache->object_cache, entry) {
663             vk_pipeline_cache_object_unref(cache->base.device, (void *)entry->key);
664          }
665       } else {
666          assert(cache->object_cache->entries == 0);
667       }
668       _mesa_set_destroy(cache->object_cache, NULL);
669    }
670    simple_mtx_destroy(&cache->lock);
671    vk_object_free(cache->base.device, pAllocator, cache);
672 }
673 
674 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_CreatePipelineCache(VkDevice _device,const VkPipelineCacheCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkPipelineCache * pPipelineCache)675 vk_common_CreatePipelineCache(VkDevice _device,
676                               const VkPipelineCacheCreateInfo *pCreateInfo,
677                               const VkAllocationCallbacks *pAllocator,
678                               VkPipelineCache *pPipelineCache)
679 {
680    VK_FROM_HANDLE(vk_device, device, _device);
681    struct vk_pipeline_cache *cache;
682 
683    struct vk_pipeline_cache_create_info info = {
684       .pCreateInfo = pCreateInfo,
685    };
686    cache = vk_pipeline_cache_create(device, &info, pAllocator);
687    if (cache == NULL)
688       return VK_ERROR_OUT_OF_HOST_MEMORY;
689 
690    *pPipelineCache = vk_pipeline_cache_to_handle(cache);
691 
692    return VK_SUCCESS;
693 }
694 
695 VKAPI_ATTR void VKAPI_CALL
vk_common_DestroyPipelineCache(VkDevice device,VkPipelineCache pipelineCache,const VkAllocationCallbacks * pAllocator)696 vk_common_DestroyPipelineCache(VkDevice device,
697                                VkPipelineCache pipelineCache,
698                                const VkAllocationCallbacks *pAllocator)
699 {
700    VK_FROM_HANDLE(vk_pipeline_cache, cache, pipelineCache);
701 
702    if (cache == NULL)
703       return;
704 
705    assert(cache->base.device == vk_device_from_handle(device));
706    vk_pipeline_cache_destroy(cache, pAllocator);
707 }
708 
709 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_GetPipelineCacheData(VkDevice _device,VkPipelineCache pipelineCache,size_t * pDataSize,void * pData)710 vk_common_GetPipelineCacheData(VkDevice _device,
711                                VkPipelineCache pipelineCache,
712                                size_t *pDataSize,
713                                void *pData)
714 {
715    VK_FROM_HANDLE(vk_device, device, _device);
716    VK_FROM_HANDLE(vk_pipeline_cache, cache, pipelineCache);
717 
718    struct blob blob;
719    if (pData) {
720       blob_init_fixed(&blob, pData, *pDataSize);
721    } else {
722       blob_init_fixed(&blob, NULL, SIZE_MAX);
723    }
724 
725    blob_write_bytes(&blob, &cache->header, sizeof(cache->header));
726 
727    uint32_t count = 0;
728    intptr_t count_offset = blob_reserve_uint32(&blob);
729    if (count_offset < 0) {
730       *pDataSize = 0;
731       blob_finish(&blob);
732       return VK_INCOMPLETE;
733    }
734 
735    vk_pipeline_cache_lock(cache);
736 
737    VkResult result = VK_SUCCESS;
738    if (cache->object_cache != NULL) {
739       set_foreach(cache->object_cache, entry) {
740          struct vk_pipeline_cache_object *object = (void *)entry->key;
741 
742          if (object->ops->serialize == NULL)
743             continue;
744 
745          size_t blob_size_save = blob.size;
746 
747          int32_t type = find_type_for_ops(device->physical, object->ops);
748          blob_write_uint32(&blob, type);
749          blob_write_uint32(&blob, object->key_size);
750          intptr_t data_size_resv = blob_reserve_uint32(&blob);
751          blob_write_bytes(&blob, object->key_data, object->key_size);
752 
753          if (!blob_align(&blob, VK_PIPELINE_CACHE_BLOB_ALIGN)) {
754             result = VK_INCOMPLETE;
755             break;
756          }
757 
758          uint32_t data_size;
759          if (!vk_pipeline_cache_object_serialize(cache, object,
760                                                  &blob, &data_size)) {
761             blob.size = blob_size_save;
762             if (blob.out_of_memory) {
763                result = VK_INCOMPLETE;
764                break;
765             }
766 
767             /* Failed for some other reason; keep going */
768             continue;
769          }
770 
771          /* vk_pipeline_cache_object_serialize should have failed */
772          assert(!blob.out_of_memory);
773 
774          assert(data_size_resv >= 0);
775          blob_overwrite_uint32(&blob, data_size_resv, data_size);
776 
777          count++;
778       }
779    }
780 
781    vk_pipeline_cache_unlock(cache);
782 
783    blob_overwrite_uint32(&blob, count_offset, count);
784 
785    *pDataSize = blob.size;
786 
787    blob_finish(&blob);
788 
789    return result;
790 }
791 
792 VKAPI_ATTR VkResult VKAPI_CALL
vk_common_MergePipelineCaches(VkDevice _device,VkPipelineCache dstCache,uint32_t srcCacheCount,const VkPipelineCache * pSrcCaches)793 vk_common_MergePipelineCaches(VkDevice _device,
794                               VkPipelineCache dstCache,
795                               uint32_t srcCacheCount,
796                               const VkPipelineCache *pSrcCaches)
797 {
798    VK_FROM_HANDLE(vk_pipeline_cache, dst, dstCache);
799    VK_FROM_HANDLE(vk_device, device, _device);
800    assert(dst->base.device == device);
801    assert(!dst->weak_ref);
802 
803    if (!dst->object_cache)
804       return VK_SUCCESS;
805 
806    vk_pipeline_cache_lock(dst);
807 
808    for (uint32_t i = 0; i < srcCacheCount; i++) {
809       VK_FROM_HANDLE(vk_pipeline_cache, src, pSrcCaches[i]);
810       assert(src->base.device == device);
811 
812       if (!src->object_cache)
813          continue;
814 
815       assert(src != dst);
816       if (src == dst)
817          continue;
818 
819       vk_pipeline_cache_lock(src);
820 
821       set_foreach(src->object_cache, src_entry) {
822          struct vk_pipeline_cache_object *src_object = (void *)src_entry->key;
823 
824          bool found_in_dst = false;
825          struct set_entry *dst_entry =
826             _mesa_set_search_or_add_pre_hashed(dst->object_cache,
827                                                src_entry->hash,
828                                                src_object, &found_in_dst);
829          if (found_in_dst) {
830             struct vk_pipeline_cache_object *dst_object = (void *)dst_entry->key;
831             if (dst_object->ops == &vk_raw_data_cache_object_ops &&
832                 src_object->ops != &vk_raw_data_cache_object_ops) {
833                /* Even though dst has the object, it only has the blob version
834                 * which isn't as useful.  Replace it with the real object.
835                 */
836                vk_pipeline_cache_object_unref(device, dst_object);
837                dst_entry->key = vk_pipeline_cache_object_ref(src_object);
838             }
839          } else {
840             /* We inserted src_object in dst so it needs a reference */
841             assert(dst_entry->key == (const void *)src_object);
842             vk_pipeline_cache_object_ref(src_object);
843          }
844       }
845 
846       vk_pipeline_cache_unlock(src);
847    }
848 
849    vk_pipeline_cache_unlock(dst);
850 
851    return VK_SUCCESS;
852 }
853