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