radv: Add logic for multisample format descriptions.
[mesa.git] / src / amd / vulkan / radv_pipeline_cache.c
1 /*
2 * Copyright © 2015 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 "util/mesa-sha1.h"
25 #include "util/debug.h"
26 #include "util/disk_cache.h"
27 #include "util/u_atomic.h"
28 #include "radv_debug.h"
29 #include "radv_private.h"
30 #include "radv_shader.h"
31
32 #include "ac_nir_to_llvm.h"
33
34 struct cache_entry_variant_info {
35 struct radv_shader_variant_info variant_info;
36 struct ac_shader_config config;
37 uint32_t rsrc1, rsrc2;
38 };
39
40 struct cache_entry {
41 union {
42 unsigned char sha1[20];
43 uint32_t sha1_dw[5];
44 };
45 uint32_t code_sizes[MESA_SHADER_STAGES];
46 struct radv_shader_variant *variants[MESA_SHADER_STAGES];
47 char code[0];
48 };
49
50 void
51 radv_pipeline_cache_init(struct radv_pipeline_cache *cache,
52 struct radv_device *device)
53 {
54 cache->device = device;
55 pthread_mutex_init(&cache->mutex, NULL);
56
57 cache->modified = false;
58 cache->kernel_count = 0;
59 cache->total_size = 0;
60 cache->table_size = 1024;
61 const size_t byte_size = cache->table_size * sizeof(cache->hash_table[0]);
62 cache->hash_table = malloc(byte_size);
63
64 /* We don't consider allocation failure fatal, we just start with a 0-sized
65 * cache. Disable caching when we want to keep shader debug info, since
66 * we don't get the debug info on cached shaders. */
67 if (cache->hash_table == NULL ||
68 (device->instance->debug_flags & RADV_DEBUG_NO_CACHE) ||
69 device->keep_shader_info)
70 cache->table_size = 0;
71 else
72 memset(cache->hash_table, 0, byte_size);
73 }
74
75 void
76 radv_pipeline_cache_finish(struct radv_pipeline_cache *cache)
77 {
78 for (unsigned i = 0; i < cache->table_size; ++i)
79 if (cache->hash_table[i]) {
80 for(int j = 0; j < MESA_SHADER_STAGES; ++j) {
81 if (cache->hash_table[i]->variants[j])
82 radv_shader_variant_destroy(cache->device,
83 cache->hash_table[i]->variants[j]);
84 }
85 vk_free(&cache->alloc, cache->hash_table[i]);
86 }
87 pthread_mutex_destroy(&cache->mutex);
88 free(cache->hash_table);
89 }
90
91 static uint32_t
92 entry_size(struct cache_entry *entry)
93 {
94 size_t ret = sizeof(*entry);
95 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
96 if (entry->code_sizes[i])
97 ret += sizeof(struct cache_entry_variant_info) + entry->code_sizes[i];
98 return ret;
99 }
100
101 void
102 radv_hash_shaders(unsigned char *hash,
103 const VkPipelineShaderStageCreateInfo **stages,
104 const struct radv_pipeline_layout *layout,
105 const struct radv_pipeline_key *key,
106 uint32_t flags)
107 {
108 struct mesa_sha1 ctx;
109
110 _mesa_sha1_init(&ctx);
111 if (key)
112 _mesa_sha1_update(&ctx, key, sizeof(*key));
113 if (layout)
114 _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
115
116 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
117 if (stages[i]) {
118 RADV_FROM_HANDLE(radv_shader_module, module, stages[i]->module);
119 const VkSpecializationInfo *spec_info = stages[i]->pSpecializationInfo;
120
121 _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
122 _mesa_sha1_update(&ctx, stages[i]->pName, strlen(stages[i]->pName));
123 if (spec_info) {
124 _mesa_sha1_update(&ctx, spec_info->pMapEntries,
125 spec_info->mapEntryCount * sizeof spec_info->pMapEntries[0]);
126 _mesa_sha1_update(&ctx, spec_info->pData, spec_info->dataSize);
127 }
128 }
129 }
130 _mesa_sha1_update(&ctx, &flags, 4);
131 _mesa_sha1_final(&ctx, hash);
132 }
133
134
135 static struct cache_entry *
136 radv_pipeline_cache_search_unlocked(struct radv_pipeline_cache *cache,
137 const unsigned char *sha1)
138 {
139 const uint32_t mask = cache->table_size - 1;
140 const uint32_t start = (*(uint32_t *) sha1);
141
142 if (cache->table_size == 0)
143 return NULL;
144
145 for (uint32_t i = 0; i < cache->table_size; i++) {
146 const uint32_t index = (start + i) & mask;
147 struct cache_entry *entry = cache->hash_table[index];
148
149 if (!entry)
150 return NULL;
151
152 if (memcmp(entry->sha1, sha1, sizeof(entry->sha1)) == 0) {
153 return entry;
154 }
155 }
156
157 unreachable("hash table should never be full");
158 }
159
160 static struct cache_entry *
161 radv_pipeline_cache_search(struct radv_pipeline_cache *cache,
162 const unsigned char *sha1)
163 {
164 struct cache_entry *entry;
165
166 pthread_mutex_lock(&cache->mutex);
167
168 entry = radv_pipeline_cache_search_unlocked(cache, sha1);
169
170 pthread_mutex_unlock(&cache->mutex);
171
172 return entry;
173 }
174
175 static void
176 radv_pipeline_cache_set_entry(struct radv_pipeline_cache *cache,
177 struct cache_entry *entry)
178 {
179 const uint32_t mask = cache->table_size - 1;
180 const uint32_t start = entry->sha1_dw[0];
181
182 /* We'll always be able to insert when we get here. */
183 assert(cache->kernel_count < cache->table_size / 2);
184
185 for (uint32_t i = 0; i < cache->table_size; i++) {
186 const uint32_t index = (start + i) & mask;
187 if (!cache->hash_table[index]) {
188 cache->hash_table[index] = entry;
189 break;
190 }
191 }
192
193 cache->total_size += entry_size(entry);
194 cache->kernel_count++;
195 }
196
197
198 static VkResult
199 radv_pipeline_cache_grow(struct radv_pipeline_cache *cache)
200 {
201 const uint32_t table_size = cache->table_size * 2;
202 const uint32_t old_table_size = cache->table_size;
203 const size_t byte_size = table_size * sizeof(cache->hash_table[0]);
204 struct cache_entry **table;
205 struct cache_entry **old_table = cache->hash_table;
206
207 table = malloc(byte_size);
208 if (table == NULL)
209 return vk_error(cache->device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
210
211 cache->hash_table = table;
212 cache->table_size = table_size;
213 cache->kernel_count = 0;
214 cache->total_size = 0;
215
216 memset(cache->hash_table, 0, byte_size);
217 for (uint32_t i = 0; i < old_table_size; i++) {
218 struct cache_entry *entry = old_table[i];
219 if (!entry)
220 continue;
221
222 radv_pipeline_cache_set_entry(cache, entry);
223 }
224
225 free(old_table);
226
227 return VK_SUCCESS;
228 }
229
230 static void
231 radv_pipeline_cache_add_entry(struct radv_pipeline_cache *cache,
232 struct cache_entry *entry)
233 {
234 if (cache->kernel_count == cache->table_size / 2)
235 radv_pipeline_cache_grow(cache);
236
237 /* Failing to grow that hash table isn't fatal, but may mean we don't
238 * have enough space to add this new kernel. Only add it if there's room.
239 */
240 if (cache->kernel_count < cache->table_size / 2)
241 radv_pipeline_cache_set_entry(cache, entry);
242 }
243
244 static bool
245 radv_is_cache_disabled(struct radv_device *device)
246 {
247 /* Pipeline caches can be disabled with RADV_DEBUG=nocache, with
248 * MESA_GLSL_CACHE_DISABLE=1, and when VK_AMD_shader_info is requested.
249 */
250 return (device->instance->debug_flags & RADV_DEBUG_NO_CACHE) ||
251 device->keep_shader_info;
252 }
253
254 bool
255 radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
256 struct radv_pipeline_cache *cache,
257 const unsigned char *sha1,
258 struct radv_shader_variant **variants,
259 bool *found_in_application_cache)
260 {
261 struct cache_entry *entry;
262
263 if (!cache) {
264 cache = device->mem_cache;
265 *found_in_application_cache = false;
266 }
267
268 pthread_mutex_lock(&cache->mutex);
269
270 entry = radv_pipeline_cache_search_unlocked(cache, sha1);
271
272 if (!entry) {
273 *found_in_application_cache = false;
274
275 /* Don't cache when we want debug info, since this isn't
276 * present in the cache.
277 */
278 if (radv_is_cache_disabled(device) || !device->physical_device->disk_cache) {
279 pthread_mutex_unlock(&cache->mutex);
280 return false;
281 }
282
283 uint8_t disk_sha1[20];
284 disk_cache_compute_key(device->physical_device->disk_cache,
285 sha1, 20, disk_sha1);
286 entry = (struct cache_entry *)
287 disk_cache_get(device->physical_device->disk_cache,
288 disk_sha1, NULL);
289 if (!entry) {
290 pthread_mutex_unlock(&cache->mutex);
291 return false;
292 } else {
293 size_t size = entry_size(entry);
294 struct cache_entry *new_entry = vk_alloc(&cache->alloc, size, 8,
295 VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
296 if (!new_entry) {
297 free(entry);
298 pthread_mutex_unlock(&cache->mutex);
299 return false;
300 }
301
302 memcpy(new_entry, entry, entry_size(entry));
303 free(entry);
304 entry = new_entry;
305
306 radv_pipeline_cache_add_entry(cache, new_entry);
307 }
308 }
309
310 char *p = entry->code;
311 for(int i = 0; i < MESA_SHADER_STAGES; ++i) {
312 if (!entry->variants[i] && entry->code_sizes[i]) {
313 struct radv_shader_variant *variant;
314 struct cache_entry_variant_info info;
315
316 variant = calloc(1, sizeof(struct radv_shader_variant));
317 if (!variant) {
318 pthread_mutex_unlock(&cache->mutex);
319 return false;
320 }
321
322 memcpy(&info, p, sizeof(struct cache_entry_variant_info));
323 p += sizeof(struct cache_entry_variant_info);
324
325 variant->config = info.config;
326 variant->info = info.variant_info;
327 variant->rsrc1 = info.rsrc1;
328 variant->rsrc2 = info.rsrc2;
329 variant->code_size = entry->code_sizes[i];
330 variant->ref_count = 1;
331
332 void *ptr = radv_alloc_shader_memory(device, variant);
333 memcpy(ptr, p, entry->code_sizes[i]);
334 p += entry->code_sizes[i];
335
336 entry->variants[i] = variant;
337 } else if (entry->code_sizes[i]) {
338 p += sizeof(struct cache_entry_variant_info) + entry->code_sizes[i];
339 }
340
341 }
342
343 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
344 if (entry->variants[i])
345 p_atomic_inc(&entry->variants[i]->ref_count);
346
347 memcpy(variants, entry->variants, sizeof(entry->variants));
348 pthread_mutex_unlock(&cache->mutex);
349 return true;
350 }
351
352 void
353 radv_pipeline_cache_insert_shaders(struct radv_device *device,
354 struct radv_pipeline_cache *cache,
355 const unsigned char *sha1,
356 struct radv_shader_variant **variants,
357 const void *const *codes,
358 const unsigned *code_sizes)
359 {
360 if (!cache)
361 cache = device->mem_cache;
362
363 pthread_mutex_lock(&cache->mutex);
364 struct cache_entry *entry = radv_pipeline_cache_search_unlocked(cache, sha1);
365 if (entry) {
366 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
367 if (entry->variants[i]) {
368 radv_shader_variant_destroy(cache->device, variants[i]);
369 variants[i] = entry->variants[i];
370 } else {
371 entry->variants[i] = variants[i];
372 }
373 if (variants[i])
374 p_atomic_inc(&variants[i]->ref_count);
375 }
376 pthread_mutex_unlock(&cache->mutex);
377 return;
378 }
379
380 /* Don't cache when we want debug info, since this isn't
381 * present in the cache.
382 */
383 if (radv_is_cache_disabled(device)) {
384 pthread_mutex_unlock(&cache->mutex);
385 return;
386 }
387
388 size_t size = sizeof(*entry);
389 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
390 if (variants[i])
391 size += sizeof(struct cache_entry_variant_info) + code_sizes[i];
392
393
394 entry = vk_alloc(&cache->alloc, size, 8,
395 VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
396 if (!entry) {
397 pthread_mutex_unlock(&cache->mutex);
398 return;
399 }
400
401 memset(entry, 0, sizeof(*entry));
402 memcpy(entry->sha1, sha1, 20);
403
404 char* p = entry->code;
405 struct cache_entry_variant_info info;
406 memset(&info, 0, sizeof(info));
407
408 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
409 if (!variants[i])
410 continue;
411
412 entry->code_sizes[i] = code_sizes[i];
413
414 info.config = variants[i]->config;
415 info.variant_info = variants[i]->info;
416 info.rsrc1 = variants[i]->rsrc1;
417 info.rsrc2 = variants[i]->rsrc2;
418 memcpy(p, &info, sizeof(struct cache_entry_variant_info));
419 p += sizeof(struct cache_entry_variant_info);
420
421 memcpy(p, codes[i], code_sizes[i]);
422 p += code_sizes[i];
423 }
424
425 /* Always add cache items to disk. This will allow collection of
426 * compiled shaders by third parties such as steam, even if the app
427 * implements its own pipeline cache.
428 */
429 if (device->physical_device->disk_cache) {
430 uint8_t disk_sha1[20];
431 disk_cache_compute_key(device->physical_device->disk_cache, sha1, 20,
432 disk_sha1);
433 disk_cache_put(device->physical_device->disk_cache,
434 disk_sha1, entry, entry_size(entry), NULL);
435 }
436
437 /* We delay setting the variant so we have reproducible disk cache
438 * items.
439 */
440 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
441 if (!variants[i])
442 continue;
443
444 entry->variants[i] = variants[i];
445 p_atomic_inc(&variants[i]->ref_count);
446 }
447
448 radv_pipeline_cache_add_entry(cache, entry);
449
450 cache->modified = true;
451 pthread_mutex_unlock(&cache->mutex);
452 return;
453 }
454
455 struct cache_header {
456 uint32_t header_size;
457 uint32_t header_version;
458 uint32_t vendor_id;
459 uint32_t device_id;
460 uint8_t uuid[VK_UUID_SIZE];
461 };
462
463 bool
464 radv_pipeline_cache_load(struct radv_pipeline_cache *cache,
465 const void *data, size_t size)
466 {
467 struct radv_device *device = cache->device;
468 struct cache_header header;
469
470 if (size < sizeof(header))
471 return false;
472 memcpy(&header, data, sizeof(header));
473 if (header.header_size < sizeof(header))
474 return false;
475 if (header.header_version != VK_PIPELINE_CACHE_HEADER_VERSION_ONE)
476 return false;
477 if (header.vendor_id != ATI_VENDOR_ID)
478 return false;
479 if (header.device_id != device->physical_device->rad_info.pci_id)
480 return false;
481 if (memcmp(header.uuid, device->physical_device->cache_uuid, VK_UUID_SIZE) != 0)
482 return false;
483
484 char *end = (void *) data + size;
485 char *p = (void *) data + header.header_size;
486
487 while (end - p >= sizeof(struct cache_entry)) {
488 struct cache_entry *entry = (struct cache_entry*)p;
489 struct cache_entry *dest_entry;
490 size_t size = entry_size(entry);
491 if(end - p < size)
492 break;
493
494 dest_entry = vk_alloc(&cache->alloc, size,
495 8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
496 if (dest_entry) {
497 memcpy(dest_entry, entry, size);
498 for (int i = 0; i < MESA_SHADER_STAGES; ++i)
499 dest_entry->variants[i] = NULL;
500 radv_pipeline_cache_add_entry(cache, dest_entry);
501 }
502 p += size;
503 }
504
505 return true;
506 }
507
508 VkResult radv_CreatePipelineCache(
509 VkDevice _device,
510 const VkPipelineCacheCreateInfo* pCreateInfo,
511 const VkAllocationCallbacks* pAllocator,
512 VkPipelineCache* pPipelineCache)
513 {
514 RADV_FROM_HANDLE(radv_device, device, _device);
515 struct radv_pipeline_cache *cache;
516
517 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
518 assert(pCreateInfo->flags == 0);
519
520 cache = vk_alloc2(&device->alloc, pAllocator,
521 sizeof(*cache), 8,
522 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
523 if (cache == NULL)
524 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
525
526 if (pAllocator)
527 cache->alloc = *pAllocator;
528 else
529 cache->alloc = device->alloc;
530
531 radv_pipeline_cache_init(cache, device);
532
533 if (pCreateInfo->initialDataSize > 0) {
534 radv_pipeline_cache_load(cache,
535 pCreateInfo->pInitialData,
536 pCreateInfo->initialDataSize);
537 }
538
539 *pPipelineCache = radv_pipeline_cache_to_handle(cache);
540
541 return VK_SUCCESS;
542 }
543
544 void radv_DestroyPipelineCache(
545 VkDevice _device,
546 VkPipelineCache _cache,
547 const VkAllocationCallbacks* pAllocator)
548 {
549 RADV_FROM_HANDLE(radv_device, device, _device);
550 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
551
552 if (!cache)
553 return;
554 radv_pipeline_cache_finish(cache);
555
556 vk_free2(&device->alloc, pAllocator, cache);
557 }
558
559 VkResult radv_GetPipelineCacheData(
560 VkDevice _device,
561 VkPipelineCache _cache,
562 size_t* pDataSize,
563 void* pData)
564 {
565 RADV_FROM_HANDLE(radv_device, device, _device);
566 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
567 struct cache_header *header;
568 VkResult result = VK_SUCCESS;
569
570 pthread_mutex_lock(&cache->mutex);
571
572 const size_t size = sizeof(*header) + cache->total_size;
573 if (pData == NULL) {
574 pthread_mutex_unlock(&cache->mutex);
575 *pDataSize = size;
576 return VK_SUCCESS;
577 }
578 if (*pDataSize < sizeof(*header)) {
579 pthread_mutex_unlock(&cache->mutex);
580 *pDataSize = 0;
581 return VK_INCOMPLETE;
582 }
583 void *p = pData, *end = pData + *pDataSize;
584 header = p;
585 header->header_size = sizeof(*header);
586 header->header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE;
587 header->vendor_id = ATI_VENDOR_ID;
588 header->device_id = device->physical_device->rad_info.pci_id;
589 memcpy(header->uuid, device->physical_device->cache_uuid, VK_UUID_SIZE);
590 p += header->header_size;
591
592 struct cache_entry *entry;
593 for (uint32_t i = 0; i < cache->table_size; i++) {
594 if (!cache->hash_table[i])
595 continue;
596 entry = cache->hash_table[i];
597 const uint32_t size = entry_size(entry);
598 if (end < p + size) {
599 result = VK_INCOMPLETE;
600 break;
601 }
602
603 memcpy(p, entry, size);
604 for(int j = 0; j < MESA_SHADER_STAGES; ++j)
605 ((struct cache_entry*)p)->variants[j] = NULL;
606 p += size;
607 }
608 *pDataSize = p - pData;
609
610 pthread_mutex_unlock(&cache->mutex);
611 return result;
612 }
613
614 static void
615 radv_pipeline_cache_merge(struct radv_pipeline_cache *dst,
616 struct radv_pipeline_cache *src)
617 {
618 for (uint32_t i = 0; i < src->table_size; i++) {
619 struct cache_entry *entry = src->hash_table[i];
620 if (!entry || radv_pipeline_cache_search(dst, entry->sha1))
621 continue;
622
623 radv_pipeline_cache_add_entry(dst, entry);
624
625 src->hash_table[i] = NULL;
626 }
627 }
628
629 VkResult radv_MergePipelineCaches(
630 VkDevice _device,
631 VkPipelineCache destCache,
632 uint32_t srcCacheCount,
633 const VkPipelineCache* pSrcCaches)
634 {
635 RADV_FROM_HANDLE(radv_pipeline_cache, dst, destCache);
636
637 for (uint32_t i = 0; i < srcCacheCount; i++) {
638 RADV_FROM_HANDLE(radv_pipeline_cache, src, pSrcCaches[i]);
639
640 radv_pipeline_cache_merge(dst, src);
641 }
642
643 return VK_SUCCESS;
644 }