radv: add pipeline creation support for geometry shaders (v2.1)
[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 "radv_private.h"
27
28 #include "ac_nir_to_llvm.h"
29
30 struct cache_entry {
31 union {
32 unsigned char sha1[20];
33 uint32_t sha1_dw[5];
34 };
35 uint32_t code_size;
36 struct ac_shader_variant_info variant_info;
37 struct ac_shader_config config;
38 uint32_t rsrc1, rsrc2;
39 struct radv_shader_variant *variant;
40 uint32_t code[0];
41 };
42
43 void
44 radv_pipeline_cache_init(struct radv_pipeline_cache *cache,
45 struct radv_device *device)
46 {
47 cache->device = device;
48 pthread_mutex_init(&cache->mutex, NULL);
49
50 cache->modified = false;
51 cache->kernel_count = 0;
52 cache->total_size = 0;
53 cache->table_size = 1024;
54 const size_t byte_size = cache->table_size * sizeof(cache->hash_table[0]);
55 cache->hash_table = malloc(byte_size);
56
57 /* We don't consider allocation failure fatal, we just start with a 0-sized
58 * cache. */
59 if (cache->hash_table == NULL ||
60 (device->debug_flags & RADV_DEBUG_NO_CACHE))
61 cache->table_size = 0;
62 else
63 memset(cache->hash_table, 0, byte_size);
64 }
65
66 void
67 radv_pipeline_cache_finish(struct radv_pipeline_cache *cache)
68 {
69 for (unsigned i = 0; i < cache->table_size; ++i)
70 if (cache->hash_table[i]) {
71 if (cache->hash_table[i]->variant)
72 radv_shader_variant_destroy(cache->device,
73 cache->hash_table[i]->variant);
74 vk_free(&cache->alloc, cache->hash_table[i]);
75 }
76 pthread_mutex_destroy(&cache->mutex);
77 free(cache->hash_table);
78 }
79
80 static uint32_t
81 entry_size(struct cache_entry *entry)
82 {
83 return sizeof(*entry) + entry->code_size;
84 }
85
86 void
87 radv_hash_shader(unsigned char *hash, struct radv_shader_module *module,
88 const char *entrypoint,
89 const VkSpecializationInfo *spec_info,
90 const struct radv_pipeline_layout *layout,
91 const union ac_shader_variant_key *key,
92 uint32_t is_geom_copy_shader)
93 {
94 struct mesa_sha1 *ctx;
95
96 ctx = _mesa_sha1_init();
97 if (key)
98 _mesa_sha1_update(ctx, key, sizeof(*key));
99 _mesa_sha1_update(ctx, module->sha1, sizeof(module->sha1));
100 _mesa_sha1_update(ctx, entrypoint, strlen(entrypoint));
101 if (layout)
102 _mesa_sha1_update(ctx, layout->sha1, sizeof(layout->sha1));
103 if (spec_info) {
104 _mesa_sha1_update(ctx, spec_info->pMapEntries,
105 spec_info->mapEntryCount * sizeof spec_info->pMapEntries[0]);
106 _mesa_sha1_update(ctx, spec_info->pData, spec_info->dataSize);
107 }
108 _mesa_sha1_update(ctx, &is_geom_copy_shader, 4);
109 _mesa_sha1_final(ctx, hash);
110 }
111
112
113 static struct cache_entry *
114 radv_pipeline_cache_search_unlocked(struct radv_pipeline_cache *cache,
115 const unsigned char *sha1)
116 {
117 const uint32_t mask = cache->table_size - 1;
118 const uint32_t start = (*(uint32_t *) sha1);
119
120 for (uint32_t i = 0; i < cache->table_size; i++) {
121 const uint32_t index = (start + i) & mask;
122 struct cache_entry *entry = cache->hash_table[index];
123
124 if (!entry)
125 return NULL;
126
127 if (memcmp(entry->sha1, sha1, sizeof(entry->sha1)) == 0) {
128 return entry;
129 }
130 }
131
132 unreachable("hash table should never be full");
133 }
134
135 static struct cache_entry *
136 radv_pipeline_cache_search(struct radv_pipeline_cache *cache,
137 const unsigned char *sha1)
138 {
139 struct cache_entry *entry;
140
141 pthread_mutex_lock(&cache->mutex);
142
143 entry = radv_pipeline_cache_search_unlocked(cache, sha1);
144
145 pthread_mutex_unlock(&cache->mutex);
146
147 return entry;
148 }
149
150 struct radv_shader_variant *
151 radv_create_shader_variant_from_pipeline_cache(struct radv_device *device,
152 struct radv_pipeline_cache *cache,
153 const unsigned char *sha1)
154 {
155 struct cache_entry *entry = radv_pipeline_cache_search(cache, sha1);
156
157 if (!entry)
158 return NULL;
159
160 if (!entry->variant) {
161 struct radv_shader_variant *variant;
162
163 variant = calloc(1, sizeof(struct radv_shader_variant));
164 if (!variant)
165 return NULL;
166
167 variant->config = entry->config;
168 variant->info = entry->variant_info;
169 variant->rsrc1 = entry->rsrc1;
170 variant->rsrc2 = entry->rsrc2;
171 variant->ref_count = 1;
172
173 variant->bo = device->ws->buffer_create(device->ws, entry->code_size, 256,
174 RADEON_DOMAIN_GTT, RADEON_FLAG_CPU_ACCESS);
175
176 void *ptr = device->ws->buffer_map(variant->bo);
177 memcpy(ptr, entry->code, entry->code_size);
178 device->ws->buffer_unmap(variant->bo);
179
180 entry->variant = variant;
181 }
182
183 __sync_fetch_and_add(&entry->variant->ref_count, 1);
184 return entry->variant;
185 }
186
187
188 static void
189 radv_pipeline_cache_set_entry(struct radv_pipeline_cache *cache,
190 struct cache_entry *entry)
191 {
192 const uint32_t mask = cache->table_size - 1;
193 const uint32_t start = entry->sha1_dw[0];
194
195 /* We'll always be able to insert when we get here. */
196 assert(cache->kernel_count < cache->table_size / 2);
197
198 for (uint32_t i = 0; i < cache->table_size; i++) {
199 const uint32_t index = (start + i) & mask;
200 if (!cache->hash_table[index]) {
201 cache->hash_table[index] = entry;
202 break;
203 }
204 }
205
206 cache->total_size += entry_size(entry);
207 cache->kernel_count++;
208 }
209
210
211 static VkResult
212 radv_pipeline_cache_grow(struct radv_pipeline_cache *cache)
213 {
214 const uint32_t table_size = cache->table_size * 2;
215 const uint32_t old_table_size = cache->table_size;
216 const size_t byte_size = table_size * sizeof(cache->hash_table[0]);
217 struct cache_entry **table;
218 struct cache_entry **old_table = cache->hash_table;
219
220 table = malloc(byte_size);
221 if (table == NULL)
222 return VK_ERROR_OUT_OF_HOST_MEMORY;
223
224 cache->hash_table = table;
225 cache->table_size = table_size;
226 cache->kernel_count = 0;
227 cache->total_size = 0;
228
229 memset(cache->hash_table, 0, byte_size);
230 for (uint32_t i = 0; i < old_table_size; i++) {
231 struct cache_entry *entry = old_table[i];
232 if (!entry)
233 continue;
234
235 radv_pipeline_cache_set_entry(cache, entry);
236 }
237
238 free(old_table);
239
240 return VK_SUCCESS;
241 }
242
243 static void
244 radv_pipeline_cache_add_entry(struct radv_pipeline_cache *cache,
245 struct cache_entry *entry)
246 {
247 if (cache->kernel_count == cache->table_size / 2)
248 radv_pipeline_cache_grow(cache);
249
250 /* Failing to grow that hash table isn't fatal, but may mean we don't
251 * have enough space to add this new kernel. Only add it if there's room.
252 */
253 if (cache->kernel_count < cache->table_size / 2)
254 radv_pipeline_cache_set_entry(cache, entry);
255 }
256
257 struct radv_shader_variant *
258 radv_pipeline_cache_insert_shader(struct radv_pipeline_cache *cache,
259 const unsigned char *sha1,
260 struct radv_shader_variant *variant,
261 const void *code, unsigned code_size)
262 {
263 pthread_mutex_lock(&cache->mutex);
264 struct cache_entry *entry = radv_pipeline_cache_search_unlocked(cache, sha1);
265 if (entry) {
266 if (entry->variant) {
267 radv_shader_variant_destroy(cache->device, variant);
268 variant = entry->variant;
269 } else {
270 entry->variant = variant;
271 }
272 __sync_fetch_and_add(&variant->ref_count, 1);
273 pthread_mutex_unlock(&cache->mutex);
274 return variant;
275 }
276
277 entry = vk_alloc(&cache->alloc, sizeof(*entry) + code_size, 8,
278 VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
279 if (!entry) {
280 pthread_mutex_unlock(&cache->mutex);
281 return variant;
282 }
283
284 memcpy(entry->sha1, sha1, 20);
285 memcpy(entry->code, code, code_size);
286 entry->config = variant->config;
287 entry->variant_info = variant->info;
288 entry->rsrc1 = variant->rsrc1;
289 entry->rsrc2 = variant->rsrc2;
290 entry->code_size = code_size;
291 entry->variant = variant;
292 __sync_fetch_and_add(&variant->ref_count, 1);
293
294 radv_pipeline_cache_add_entry(cache, entry);
295
296 cache->modified = true;
297 pthread_mutex_unlock(&cache->mutex);
298 return variant;
299 }
300
301 struct cache_header {
302 uint32_t header_size;
303 uint32_t header_version;
304 uint32_t vendor_id;
305 uint32_t device_id;
306 uint8_t uuid[VK_UUID_SIZE];
307 };
308 void
309 radv_pipeline_cache_load(struct radv_pipeline_cache *cache,
310 const void *data, size_t size)
311 {
312 struct radv_device *device = cache->device;
313 struct cache_header header;
314
315 if (size < sizeof(header))
316 return;
317 memcpy(&header, data, sizeof(header));
318 if (header.header_size < sizeof(header))
319 return;
320 if (header.header_version != VK_PIPELINE_CACHE_HEADER_VERSION_ONE)
321 return;
322 if (header.vendor_id != 0x1002)
323 return;
324 if (header.device_id != device->physical_device->rad_info.pci_id)
325 return;
326 if (memcmp(header.uuid, device->physical_device->uuid, VK_UUID_SIZE) != 0)
327 return;
328
329 char *end = (void *) data + size;
330 char *p = (void *) data + header.header_size;
331
332 while (end - p >= sizeof(struct cache_entry)) {
333 struct cache_entry *entry = (struct cache_entry*)p;
334 struct cache_entry *dest_entry;
335 if(end - p < sizeof(*entry) + entry->code_size)
336 break;
337
338 dest_entry = vk_alloc(&cache->alloc, sizeof(*entry) + entry->code_size,
339 8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
340 if (dest_entry) {
341 memcpy(dest_entry, entry, sizeof(*entry) + entry->code_size);
342 dest_entry->variant = NULL;
343 radv_pipeline_cache_add_entry(cache, dest_entry);
344 }
345 p += sizeof (*entry) + entry->code_size;
346 }
347 }
348
349 VkResult radv_CreatePipelineCache(
350 VkDevice _device,
351 const VkPipelineCacheCreateInfo* pCreateInfo,
352 const VkAllocationCallbacks* pAllocator,
353 VkPipelineCache* pPipelineCache)
354 {
355 RADV_FROM_HANDLE(radv_device, device, _device);
356 struct radv_pipeline_cache *cache;
357
358 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
359 assert(pCreateInfo->flags == 0);
360
361 cache = vk_alloc2(&device->alloc, pAllocator,
362 sizeof(*cache), 8,
363 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
364 if (cache == NULL)
365 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
366
367 if (pAllocator)
368 cache->alloc = *pAllocator;
369 else
370 cache->alloc = device->alloc;
371
372 radv_pipeline_cache_init(cache, device);
373
374 if (pCreateInfo->initialDataSize > 0) {
375 radv_pipeline_cache_load(cache,
376 pCreateInfo->pInitialData,
377 pCreateInfo->initialDataSize);
378 }
379
380 *pPipelineCache = radv_pipeline_cache_to_handle(cache);
381
382 return VK_SUCCESS;
383 }
384
385 void radv_DestroyPipelineCache(
386 VkDevice _device,
387 VkPipelineCache _cache,
388 const VkAllocationCallbacks* pAllocator)
389 {
390 RADV_FROM_HANDLE(radv_device, device, _device);
391 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
392
393 if (!cache)
394 return;
395 radv_pipeline_cache_finish(cache);
396
397 vk_free2(&device->alloc, pAllocator, cache);
398 }
399
400 VkResult radv_GetPipelineCacheData(
401 VkDevice _device,
402 VkPipelineCache _cache,
403 size_t* pDataSize,
404 void* pData)
405 {
406 RADV_FROM_HANDLE(radv_device, device, _device);
407 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
408 struct cache_header *header;
409 VkResult result = VK_SUCCESS;
410 const size_t size = sizeof(*header) + cache->total_size;
411 if (pData == NULL) {
412 *pDataSize = size;
413 return VK_SUCCESS;
414 }
415 if (*pDataSize < sizeof(*header)) {
416 *pDataSize = 0;
417 return VK_INCOMPLETE;
418 }
419 void *p = pData, *end = pData + *pDataSize;
420 header = p;
421 header->header_size = sizeof(*header);
422 header->header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE;
423 header->vendor_id = 0x1002;
424 header->device_id = device->physical_device->rad_info.pci_id;
425 memcpy(header->uuid, device->physical_device->uuid, VK_UUID_SIZE);
426 p += header->header_size;
427
428 struct cache_entry *entry;
429 for (uint32_t i = 0; i < cache->table_size; i++) {
430 if (!cache->hash_table[i])
431 continue;
432 entry = cache->hash_table[i];
433 const uint32_t size = entry_size(entry);
434 if (end < p + size) {
435 result = VK_INCOMPLETE;
436 break;
437 }
438
439 memcpy(p, entry, size);
440 ((struct cache_entry*)p)->variant = NULL;
441 p += size;
442 }
443 *pDataSize = p - pData;
444
445 return result;
446 }
447
448 static void
449 radv_pipeline_cache_merge(struct radv_pipeline_cache *dst,
450 struct radv_pipeline_cache *src)
451 {
452 for (uint32_t i = 0; i < src->table_size; i++) {
453 struct cache_entry *entry = src->hash_table[i];
454 if (!entry || radv_pipeline_cache_search(dst, entry->sha1))
455 continue;
456
457 radv_pipeline_cache_add_entry(dst, entry);
458
459 src->hash_table[i] = NULL;
460 }
461 }
462
463 VkResult radv_MergePipelineCaches(
464 VkDevice _device,
465 VkPipelineCache destCache,
466 uint32_t srcCacheCount,
467 const VkPipelineCache* pSrcCaches)
468 {
469 RADV_FROM_HANDLE(radv_pipeline_cache, dst, destCache);
470
471 for (uint32_t i = 0; i < srcCacheCount; i++) {
472 RADV_FROM_HANDLE(radv_pipeline_cache, src, pSrcCaches[i]);
473
474 radv_pipeline_cache_merge(dst, src);
475 }
476
477 return VK_SUCCESS;
478 }