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