summaryrefslogtreecommitdiff
path: root/src/vulkan/runtime/vk_pipeline_cache.c
blob: b16d5a08fd69c5a6903bb58b609385ea481a9fd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*
 * Copyright © 2021 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "vk_pipeline_cache.h"

#include "vk_alloc.h"
#include "vk_common_entrypoints.h"
#include "vk_device.h"
#include "vk_log.h"
#include "vk_physical_device.h"

#include "compiler/nir/nir_serialize.h"

#include "util/blob.h"
#include "util/u_debug.h"
#include "util/disk_cache.h"
#include "util/hash_table.h"
#include "util/set.h"

#define vk_pipeline_cache_log(cache, ...)                                      \
   if (cache->base.client_visible)                                             \
      vk_logw(VK_LOG_OBJS(cache), __VA_ARGS__)

static bool
vk_raw_data_cache_object_serialize(struct vk_pipeline_cache_object *object,
                                   struct blob *blob)
{
   struct vk_raw_data_cache_object *data_obj =
      container_of(object, struct vk_raw_data_cache_object, base);

   blob_write_bytes(blob, data_obj->data, data_obj->data_size);

   return true;
}

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)
{
   /* We consume the entire blob_reader.  Each call to ops->deserialize()
    * happens with a brand new blob reader for error checking anyway so we
    * can assume the blob consumes the entire reader and we don't need to
    * serialize the data size separately.
    */
   assert(blob->current < blob->end);
   size_t data_size = blob->end - blob->current;
   const void *data = blob_read_bytes(blob, data_size);

   struct vk_raw_data_cache_object *data_obj =
      vk_raw_data_cache_object_create(cache->base.device, key_data, key_size,
                                      data, data_size);

   return data_obj ? &data_obj->base : NULL;
}

static void
vk_raw_data_cache_object_destroy(struct vk_device *device,
                                 struct vk_pipeline_cache_object *object)
{
   struct vk_raw_data_cache_object *data_obj =
      container_of(object, struct vk_raw_data_cache_object, base);

   vk_free(&device->alloc, data_obj);
}

const struct vk_pipeline_cache_object_ops vk_raw_data_cache_object_ops = {
   .serialize = vk_raw_data_cache_object_serialize,
   .deserialize = vk_raw_data_cache_object_deserialize,
   .destroy = vk_raw_data_cache_object_destroy,
};

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)
{
   VK_MULTIALLOC(ma);
   VK_MULTIALLOC_DECL(&ma, struct vk_raw_data_cache_object, data_obj, 1);
   VK_MULTIALLOC_DECL_SIZE(&ma, char, obj_key_data, key_size);
   VK_MULTIALLOC_DECL_SIZE(&ma, char, obj_data, data_size);

   if (!vk_multialloc_alloc(&ma, &device->alloc,
                            VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
      return NULL;

   vk_pipeline_cache_object_init(device, &data_obj->base,
                                 &vk_raw_data_cache_object_ops,
                                 obj_key_data, key_size);
   data_obj->data = obj_data;
   data_obj->data_size = data_size;

   memcpy(obj_key_data, key_data, key_size);
   memcpy(obj_data, data, data_size);

   return data_obj;
}

static bool
object_keys_equal(const void *void_a, const void *void_b)
{
   const struct vk_pipeline_cache_object *a = void_a, *b = void_b;
   if (a->key_size != b->key_size)
      return false;

   return memcmp(a->key_data, b->key_data, a->key_size) == 0;
}

static uint32_t
object_key_hash(const void *void_object)
{
   const struct vk_pipeline_cache_object *object = void_object;
   return _mesa_hash_data(object->key_data, object->key_size);
}

static void
vk_pipeline_cache_lock(struct vk_pipeline_cache *cache)
{

   if (!(cache->flags & VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT))
      simple_mtx_lock(&cache->lock);
}

static void
vk_pipeline_cache_unlock(struct vk_pipeline_cache *cache)
{
   if (!(cache->flags & VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT))
      simple_mtx_unlock(&cache->lock);
}

static void
vk_pipeline_cache_remove_object(struct vk_pipeline_cache *cache,
                                uint32_t hash,
                                struct vk_pipeline_cache_object *object)
{
   vk_pipeline_cache_lock(cache);
   struct set_entry *entry =
      _mesa_set_search_pre_hashed(cache->object_cache, hash, object);
   if (entry && entry->key == (const void *)object) {
      /* Drop the reference owned by the cache */
      vk_pipeline_cache_object_unref(cache->base.device, object);

      _mesa_set_remove(cache->object_cache, entry);
   }
   vk_pipeline_cache_unlock(cache);

   /* Drop our reference */
   vk_pipeline_cache_object_unref(cache->base.device, object);
}

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)
{
   if (object->ops->serialize == NULL)
      return false;

   assert(blob->size == align64(blob->size, VK_PIPELINE_CACHE_BLOB_ALIGN));
   size_t start = blob->size;

   /* Special case for if we're writing to a NULL blob (just to get the size)
    * and we already know the data size of the allocation.  This should make
    * the first GetPipelineCacheData() call to get the data size faster in the
    * common case where a bunch of our objects were loaded from a previous
    * cache or where we've already serialized the cache once.
    */
   if (blob->data == NULL && blob->fixed_allocation) {
      *data_size = p_atomic_read(&object->data_size);
      if (*data_size > 0) {
         blob_write_bytes(blob, NULL, *data_size);
         return true;
      }
   }

   if (!object->ops->serialize(object, blob)) {
      vk_pipeline_cache_log(cache, "Failed to serialize pipeline cache object");
      return false;
   }

   size_t size = blob->size - start;
   if (size > UINT32_MAX) {
      vk_pipeline_cache_log(cache, "Skipping giant (4 GiB or larger) object");
      return false;
   }

   if (blob->out_of_memory) {
      vk_pipeline_cache_log(cache,
                            "Insufficient memory for pipeline cache data");
      return false;
   }

   *data_size = (uint32_t)size;
   p_atomic_set(&object->data_size, *data_size);

   return true;
}

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)
{
   if (ops == NULL)
      ops = &vk_raw_data_cache_object_ops;

   if (unlikely(ops->deserialize == NULL)) {
      vk_pipeline_cache_log(cache,
                            "Pipeline cache object cannot be deserialized");
      return NULL;
   }

   struct blob_reader reader;
   blob_reader_init(&reader, data, data_size);

   struct vk_pipeline_cache_object *object =
      ops->deserialize(cache, key_data, key_size, &reader);

   if (object == NULL)
      return NULL;

   assert(reader.current == reader.end && !reader.overrun);
   assert(object->ops == ops);
   assert(object->ref_cnt == 1);
   assert(object->key_size == key_size);
   assert(memcmp(object->key_data, key_data, key_size) == 0);

   return object;
}

static struct vk_pipeline_cache_object *
vk_pipeline_cache_insert_object(struct vk_pipeline_cache *cache,
                                struct vk_pipeline_cache_object *object)
{
   assert(object->ops != NULL);

   if (cache->object_cache == NULL)
      return object;

   uint32_t hash = object_key_hash(object);

   vk_pipeline_cache_lock(cache);
   bool found = false;
   struct set_entry *entry = _mesa_set_search_or_add_pre_hashed(
       cache->object_cache, hash, object, &found);

   struct vk_pipeline_cache_object *result = NULL;
   /* add reference to either the found or inserted object */
   if (found) {
       struct vk_pipeline_cache_object *found_object = (void *)entry->key;
       if (found_object->ops != object->ops) {
          /* The found object in the cache isn't fully formed. Replace it. */
          assert(found_object->ops == &vk_raw_data_cache_object_ops);
          assert(found_object->ref_cnt == 1 && object->ref_cnt == 1);
          entry->key = object;
          object = found_object;
       }

      result = vk_pipeline_cache_object_ref((void *)entry->key);
   } else {
      result = vk_pipeline_cache_object_ref(object);
   }
   vk_pipeline_cache_unlock(cache);

   if (found) {
      vk_pipeline_cache_object_unref(cache->base.device, object);
   }
   return result;
}

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)
{
   assert(key_size <= UINT32_MAX);
   assert(ops != NULL);

   if (cache_hit != NULL)
      *cache_hit = false;

   struct vk_pipeline_cache_object key = {
      .key_data = key_data,
      .key_size = key_size,
   };
   uint32_t hash = object_key_hash(&key);

   struct vk_pipeline_cache_object *object = NULL;

   if (cache != NULL && cache->object_cache != NULL) {
      vk_pipeline_cache_lock(cache);
      struct set_entry *entry =
         _mesa_set_search_pre_hashed(cache->object_cache, hash, &key);
      if (entry) {
         object = vk_pipeline_cache_object_ref((void *)entry->key);
         if (cache_hit != NULL)
            *cache_hit = true;
      }
      vk_pipeline_cache_unlock(cache);
   }

   if (object == NULL) {
#ifdef ENABLE_SHADER_CACHE
      struct disk_cache *disk_cache = cache->base.device->physical->disk_cache;
      if (disk_cache != NULL && cache->object_cache != NULL) {
         cache_key cache_key;
         disk_cache_compute_key(disk_cache, key_data, key_size, cache_key);

         size_t data_size;
         uint8_t *data = disk_cache_get(disk_cache, cache_key, &data_size);
         if (data) {
            object = vk_pipeline_cache_object_deserialize(cache,
                                                          key_data, key_size,
                                                          data, data_size,
                                                          ops);
            free(data);
            if (object != NULL) {
               return vk_pipeline_cache_insert_object(cache, object);
            }
         }
      }
#endif

      /* No disk cache or not found in the disk cache */
      return NULL;
   }

   if (object->ops == &vk_raw_data_cache_object_ops &&
       ops != &vk_raw_data_cache_object_ops) {
      /* The object isn't fully formed yet and we need to deserialize it into
       * a real object before it can be used.
       */
      struct vk_raw_data_cache_object *data_obj =
         container_of(object, struct vk_raw_data_cache_object, base);

      struct vk_pipeline_cache_object *real_object =
         vk_pipeline_cache_object_deserialize(cache,
                                              data_obj->base.key_data,
                                              data_obj->base.key_size,
                                              data_obj->data,
                                              data_obj->data_size, ops);
      if (real_object == NULL) {
         vk_pipeline_cache_log(cache,
                               "Deserializing pipeline cache object failed");

         vk_pipeline_cache_remove_object(cache, hash, object);
         return NULL;
      }

      vk_pipeline_cache_object_unref(cache->base.device, object);
      object = vk_pipeline_cache_insert_object(cache, real_object);
   }

   assert(object->ops == ops);

   return object;
}

struct vk_pipeline_cache_object *
vk_pipeline_cache_add_object(struct vk_pipeline_cache *cache,
                             struct vk_pipeline_cache_object *object)
{
   struct vk_pipeline_cache_object *inserted =
       vk_pipeline_cache_insert_object(cache, object);

#ifdef ENABLE_SHADER_CACHE
   if (object == inserted) {
      /* If it wasn't in the object cache, it might not be in the disk cache
       * either.  Better try and add it.
       */

      struct disk_cache *disk_cache = cache->base.device->physical->disk_cache;
      if (object->ops->serialize != NULL && disk_cache) {
         struct blob blob;
         blob_init(&blob);

         if (object->ops->serialize(object, &blob) && !blob.out_of_memory) {
            cache_key cache_key;
            disk_cache_compute_key(disk_cache, object->key_data,
                                   object->key_size, cache_key);

            disk_cache_put(disk_cache, cache_key, blob.data, blob.size, NULL);
         }

         blob_finish(&blob);
      }
   }
#endif

   return inserted;
}

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)
{
#ifdef ENABLE_SHADER_CACHE
   struct disk_cache *disk_cache = cache->base.device->physical->disk_cache;
   if (disk_cache) {
      cache_key cache_key;
      disk_cache_compute_key(disk_cache, key_data, key_size, cache_key);
      disk_cache_put(disk_cache, cache_key, data, data_size, NULL);
   }
#endif

   struct vk_pipeline_cache_object *object =
       vk_pipeline_cache_object_deserialize(cache, key_data, key_size, data,
                                            data_size, ops);

   if (object)
      object = vk_pipeline_cache_insert_object(cache, object);

   return object;
}

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)
{
   struct vk_pipeline_cache_object *object =
      vk_pipeline_cache_lookup_object(cache, key_data, key_size,
                                      &vk_raw_data_cache_object_ops,
                                      cache_hit);
   if (object == NULL)
      return NULL;

   struct vk_raw_data_cache_object *data_obj =
      container_of(object, struct vk_raw_data_cache_object, base);

   struct blob_reader blob;
   blob_reader_init(&blob, data_obj->data, data_obj->data_size);

   nir_shader *nir = nir_deserialize(mem_ctx, nir_options, &blob);
   vk_pipeline_cache_object_unref(cache->base.device, object);

   if (blob.overrun) {
      ralloc_free(nir);
      return NULL;
   }

   return nir;
}

void
vk_pipeline_cache_add_nir(struct vk_pipeline_cache *cache,
                          const void *key_data, size_t key_size,
                          const nir_shader *nir)
{
   struct blob blob;
   blob_init(&blob);

   nir_serialize(&blob, nir, false);
   if (blob.out_of_memory) {
      vk_pipeline_cache_log(cache, "Ran out of memory serializing NIR shader");
      blob_finish(&blob);
      return;
   }

   struct vk_raw_data_cache_object *data_obj =
      vk_raw_data_cache_object_create(cache->base.device,
                                      key_data, key_size,
                                      blob.data, blob.size);
   blob_finish(&blob);

   struct vk_pipeline_cache_object *cached =
      vk_pipeline_cache_add_object(cache, &data_obj->base);
   vk_pipeline_cache_object_unref(cache->base.device, cached);
}

static int32_t
find_type_for_ops(const struct vk_physical_device *pdevice,
                  const struct vk_pipeline_cache_object_ops *ops)
{
   const struct vk_pipeline_cache_object_ops *const *import_ops =
      pdevice->pipeline_cache_import_ops;

   if (import_ops == NULL)
      return -1;

   for (int32_t i = 0; import_ops[i]; i++) {
      if (import_ops[i] == ops)
         return i;
   }

   return -1;
}

static const struct vk_pipeline_cache_object_ops *
find_ops_for_type(const struct vk_physical_device *pdevice,
                  int32_t type)
{
   const struct vk_pipeline_cache_object_ops *const *import_ops =
      pdevice->pipeline_cache_import_ops;

   if (import_ops == NULL || type < 0)
      return NULL;

   return import_ops[type];
}

static void
vk_pipeline_cache_load(struct vk_pipeline_cache *cache,
                       const void *data, size_t size)
{
   struct blob_reader blob;
   blob_reader_init(&blob, data, size);

   struct vk_pipeline_cache_header header;
   blob_copy_bytes(&blob, &header, sizeof(header));
   uint32_t count = blob_read_uint32(&blob);
   if (blob.overrun)
      return;

   if (memcmp(&header, &cache->header, sizeof(header)) != 0)
      return;

   for (uint32_t i = 0; i < count; i++) {
      int32_t type = blob_read_uint32(&blob);
      uint32_t key_size = blob_read_uint32(&blob);
      uint32_t data_size = blob_read_uint32(&blob);
      const void *key_data = blob_read_bytes(&blob, key_size);
      blob_reader_align(&blob, VK_PIPELINE_CACHE_BLOB_ALIGN);
      const void *data = blob_read_bytes(&blob, data_size);
      if (blob.overrun)
         break;

      const struct vk_pipeline_cache_object_ops *ops =
         find_ops_for_type(cache->base.device->physical, type);

      struct vk_pipeline_cache_object *object =
         vk_pipeline_cache_create_and_insert_object(cache, key_data, key_size,
                                                    data, data_size, ops);

      if (object == NULL) {
         vk_pipeline_cache_log(cache, "Failed to load pipeline cache object");
         continue;
      }

      vk_pipeline_cache_object_unref(cache->base.device, object);
   }
}

struct vk_pipeline_cache *
vk_pipeline_cache_create(struct vk_device *device,
                         const struct vk_pipeline_cache_create_info *info,
                         const VkAllocationCallbacks *pAllocator)
{
   static const struct VkPipelineCacheCreateInfo default_create_info = {
      .sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO,
   };
   struct vk_pipeline_cache *cache;

   const struct VkPipelineCacheCreateInfo *pCreateInfo =
      info->pCreateInfo != NULL ? info->pCreateInfo : &default_create_info;

   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);

   cache = vk_object_zalloc(device, pAllocator, sizeof(*cache),
                            VK_OBJECT_TYPE_PIPELINE_CACHE);
   if (cache == NULL)
      return NULL;

   cache->flags = pCreateInfo->flags;

   struct VkPhysicalDeviceProperties pdevice_props;
   device->physical->dispatch_table.GetPhysicalDeviceProperties(
      vk_physical_device_to_handle(device->physical), &pdevice_props);

   cache->header = (struct vk_pipeline_cache_header) {
      .header_size = sizeof(struct vk_pipeline_cache_header),
      .header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE,
      .vendor_id = pdevice_props.vendorID,
      .device_id = pdevice_props.deviceID,
   };
   memcpy(cache->header.uuid, pdevice_props.pipelineCacheUUID, VK_UUID_SIZE);

   simple_mtx_init(&cache->lock, mtx_plain);

   if (info->force_enable ||
       debug_get_bool_option("VK_ENABLE_PIPELINE_CACHE", true)) {
      cache->object_cache = _mesa_set_create(NULL, object_key_hash,
                                             object_keys_equal);
   }

   if (cache->object_cache && pCreateInfo->initialDataSize > 0) {
      vk_pipeline_cache_load(cache, pCreateInfo->pInitialData,
                             pCreateInfo->initialDataSize);
   }

   return cache;
}

void
vk_pipeline_cache_destroy(struct vk_pipeline_cache *cache,
                          const VkAllocationCallbacks *pAllocator)
{
   if (cache->object_cache) {
      set_foreach_remove(cache->object_cache, entry) {
         vk_pipeline_cache_object_unref(cache->base.device,
                                        (void *)entry->key);
      }
      _mesa_set_destroy(cache->object_cache, NULL);
   }
   simple_mtx_destroy(&cache->lock);
   vk_object_free(cache->base.device, pAllocator, cache);
}

VKAPI_ATTR VkResult VKAPI_CALL
vk_common_CreatePipelineCache(VkDevice _device,
                              const VkPipelineCacheCreateInfo *pCreateInfo,
                              const VkAllocationCallbacks *pAllocator,
                              VkPipelineCache *pPipelineCache)
{
   VK_FROM_HANDLE(vk_device, device, _device);
   struct vk_pipeline_cache *cache;

   struct vk_pipeline_cache_create_info info = {
      .pCreateInfo = pCreateInfo,
   };
   cache = vk_pipeline_cache_create(device, &info, pAllocator);
   if (cache == NULL)
      return VK_ERROR_OUT_OF_HOST_MEMORY;

   *pPipelineCache = vk_pipeline_cache_to_handle(cache);

   return VK_SUCCESS;
}

VKAPI_ATTR void VKAPI_CALL
vk_common_DestroyPipelineCache(VkDevice device,
                               VkPipelineCache pipelineCache,
                               const VkAllocationCallbacks *pAllocator)
{
   VK_FROM_HANDLE(vk_pipeline_cache, cache, pipelineCache);

   if (cache == NULL)
      return;

   assert(cache->base.device == vk_device_from_handle(device));
   vk_pipeline_cache_destroy(cache, pAllocator);
}

VKAPI_ATTR VkResult VKAPI_CALL
vk_common_GetPipelineCacheData(VkDevice _device,
                               VkPipelineCache pipelineCache,
                               size_t *pDataSize,
                               void *pData)
{
   VK_FROM_HANDLE(vk_device, device, _device);
   VK_FROM_HANDLE(vk_pipeline_cache, cache, pipelineCache);

   struct blob blob;
   if (pData) {
      blob_init_fixed(&blob, pData, *pDataSize);
   } else {
      blob_init_fixed(&blob, NULL, SIZE_MAX);
   }

   blob_write_bytes(&blob, &cache->header, sizeof(cache->header));

   uint32_t count = 0;
   intptr_t count_offset = blob_reserve_uint32(&blob);
   if (count_offset < 0) {
      *pDataSize = 0;
      blob_finish(&blob);
      return VK_INCOMPLETE;
   }

   vk_pipeline_cache_lock(cache);

   VkResult result = VK_SUCCESS;
   if (cache->object_cache != NULL) {
      set_foreach(cache->object_cache, entry) {
         struct vk_pipeline_cache_object *object = (void *)entry->key;

         if (object->ops->serialize == NULL)
            continue;

         size_t blob_size_save = blob.size;

         int32_t type = find_type_for_ops(device->physical, object->ops);
         blob_write_uint32(&blob, type);
         blob_write_uint32(&blob, object->key_size);
         intptr_t data_size_resv = blob_reserve_uint32(&blob);
         blob_write_bytes(&blob, object->key_data, object->key_size);

         if (!blob_align(&blob, VK_PIPELINE_CACHE_BLOB_ALIGN)) {
            result = VK_INCOMPLETE;
            break;
         }

         uint32_t data_size;
         if (!vk_pipeline_cache_object_serialize(cache, object,
                                                 &blob, &data_size)) {
            blob.size = blob_size_save;
            if (blob.out_of_memory) {
               result = VK_INCOMPLETE;
               break;
            }

            /* Failed for some other reason; keep going */
            continue;
         }

         /* vk_pipeline_cache_object_serialize should have failed */
         assert(!blob.out_of_memory);

         assert(data_size_resv >= 0);
         blob_overwrite_uint32(&blob, data_size_resv, data_size);

         count++;
      }
   }

   vk_pipeline_cache_unlock(cache);

   blob_overwrite_uint32(&blob, count_offset, count);

   *pDataSize = blob.size;

   blob_finish(&blob);

   return result;
}

VKAPI_ATTR VkResult VKAPI_CALL
vk_common_MergePipelineCaches(VkDevice _device,
                              VkPipelineCache dstCache,
                              uint32_t srcCacheCount,
                              const VkPipelineCache *pSrcCaches)
{
   VK_FROM_HANDLE(vk_pipeline_cache, dst, dstCache);
   VK_FROM_HANDLE(vk_device, device, _device);
   assert(dst->base.device == device);

   if (!dst->object_cache)
      return VK_SUCCESS;

   vk_pipeline_cache_lock(dst);

   for (uint32_t i = 0; i < srcCacheCount; i++) {
      VK_FROM_HANDLE(vk_pipeline_cache, src, pSrcCaches[i]);
      assert(src->base.device == device);

      if (!src->object_cache)
         continue;

      assert(src != dst);
      if (src == dst)
         continue;

      vk_pipeline_cache_lock(src);

      set_foreach(src->object_cache, src_entry) {
         struct vk_pipeline_cache_object *src_object = (void *)src_entry->key;

         bool found_in_dst = false;
         struct set_entry *dst_entry =
            _mesa_set_search_or_add_pre_hashed(dst->object_cache,
                                               src_entry->hash,
                                               src_object, &found_in_dst);
         if (found_in_dst) {
            struct vk_pipeline_cache_object *dst_object = (void *)dst_entry->key;
            if (dst_object->ops == &vk_raw_data_cache_object_ops &&
                src_object->ops != &vk_raw_data_cache_object_ops) {
               /* Even though dst has the object, it only has the blob version
                * which isn't as useful.  Replace it with the real object.
                */
               vk_pipeline_cache_object_unref(device, dst_object);
               dst_entry->key = vk_pipeline_cache_object_ref(src_object);
            }
         } else {
            /* We inserted src_object in dst so it needs a reference */
            assert(dst_entry->key == (const void *)src_object);
            vk_pipeline_cache_object_ref(src_object);
         }
      }

      vk_pipeline_cache_unlock(src);
   }

   vk_pipeline_cache_unlock(dst);

   return VK_SUCCESS;
}