2 * Copyright © 2015 Intel Corporation
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:
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
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
24 #include "util/blob.h"
25 #include "util/hash_table.h"
26 #include "util/debug.h"
27 #include "util/disk_cache.h"
28 #include "util/mesa-sha1.h"
29 #include "nir/nir_serialize.h"
30 #include "anv_private.h"
31 #include "nir/nir_xfb_info.h"
33 struct anv_shader_bin
*
34 anv_shader_bin_create(struct anv_device
*device
,
35 gl_shader_stage stage
,
36 const void *key_data
, uint32_t key_size
,
37 const void *kernel_data
, uint32_t kernel_size
,
38 const void *constant_data
, uint32_t constant_data_size
,
39 const struct brw_stage_prog_data
*prog_data_in
,
40 uint32_t prog_data_size
,
41 const struct brw_compile_stats
*stats
, uint32_t num_stats
,
42 const nir_xfb_info
*xfb_info_in
,
43 const struct anv_pipeline_bind_map
*bind_map
)
45 struct anv_shader_bin
*shader
;
46 struct anv_shader_bin_key
*key
;
47 struct brw_stage_prog_data
*prog_data
;
48 uint32_t *prog_data_param
;
49 nir_xfb_info
*xfb_info
;
50 struct anv_pipeline_binding
*surface_to_descriptor
, *sampler_to_descriptor
;
53 anv_multialloc_add(&ma
, &shader
, 1);
54 anv_multialloc_add_size(&ma
, &key
, sizeof(*key
) + key_size
);
55 anv_multialloc_add_size(&ma
, &prog_data
, prog_data_size
);
56 anv_multialloc_add(&ma
, &prog_data_param
, prog_data_in
->nr_params
);
58 uint32_t xfb_info_size
= nir_xfb_info_size(xfb_info_in
->output_count
);
59 anv_multialloc_add_size(&ma
, &xfb_info
, xfb_info_size
);
61 anv_multialloc_add(&ma
, &surface_to_descriptor
,
62 bind_map
->surface_count
);
63 anv_multialloc_add(&ma
, &sampler_to_descriptor
,
64 bind_map
->sampler_count
);
66 if (!anv_multialloc_alloc(&ma
, &device
->vk
.alloc
,
67 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
))
72 shader
->stage
= stage
;
75 memcpy(key
->data
, key_data
, key_size
);
79 anv_state_pool_alloc(&device
->instruction_state_pool
, kernel_size
, 64);
80 memcpy(shader
->kernel
.map
, kernel_data
, kernel_size
);
81 shader
->kernel_size
= kernel_size
;
83 if (constant_data_size
) {
84 shader
->constant_data
=
85 anv_state_pool_alloc(&device
->dynamic_state_pool
,
86 constant_data_size
, 32);
87 memcpy(shader
->constant_data
.map
, constant_data
, constant_data_size
);
89 shader
->constant_data
= ANV_STATE_NULL
;
91 shader
->constant_data_size
= constant_data_size
;
93 memcpy(prog_data
, prog_data_in
, prog_data_size
);
94 memset(prog_data_param
, 0,
95 prog_data
->nr_params
* sizeof(*prog_data_param
));
96 prog_data
->param
= prog_data_param
;
97 shader
->prog_data
= prog_data
;
98 shader
->prog_data_size
= prog_data_size
;
100 assert(num_stats
<= ARRAY_SIZE(shader
->stats
));
101 typed_memcpy(shader
->stats
, stats
, num_stats
);
102 shader
->num_stats
= num_stats
;
105 *xfb_info
= *xfb_info_in
;
106 typed_memcpy(xfb_info
->outputs
, xfb_info_in
->outputs
,
107 xfb_info_in
->output_count
);
108 shader
->xfb_info
= xfb_info
;
110 shader
->xfb_info
= NULL
;
113 shader
->bind_map
= *bind_map
;
114 typed_memcpy(surface_to_descriptor
, bind_map
->surface_to_descriptor
,
115 bind_map
->surface_count
);
116 shader
->bind_map
.surface_to_descriptor
= surface_to_descriptor
;
117 typed_memcpy(sampler_to_descriptor
, bind_map
->sampler_to_descriptor
,
118 bind_map
->sampler_count
);
119 shader
->bind_map
.sampler_to_descriptor
= sampler_to_descriptor
;
125 anv_shader_bin_destroy(struct anv_device
*device
,
126 struct anv_shader_bin
*shader
)
128 assert(shader
->ref_cnt
== 0);
129 anv_state_pool_free(&device
->instruction_state_pool
, shader
->kernel
);
130 anv_state_pool_free(&device
->dynamic_state_pool
, shader
->constant_data
);
131 vk_free(&device
->vk
.alloc
, shader
);
135 anv_shader_bin_write_to_blob(const struct anv_shader_bin
*shader
,
138 blob_write_uint32(blob
, shader
->stage
);
140 blob_write_uint32(blob
, shader
->key
->size
);
141 blob_write_bytes(blob
, shader
->key
->data
, shader
->key
->size
);
143 blob_write_uint32(blob
, shader
->kernel_size
);
144 blob_write_bytes(blob
, shader
->kernel
.map
, shader
->kernel_size
);
146 blob_write_uint32(blob
, shader
->constant_data_size
);
147 blob_write_bytes(blob
, shader
->constant_data
.map
,
148 shader
->constant_data_size
);
150 blob_write_uint32(blob
, shader
->prog_data_size
);
151 blob_write_bytes(blob
, shader
->prog_data
, shader
->prog_data_size
);
153 blob_write_uint32(blob
, shader
->num_stats
);
154 blob_write_bytes(blob
, shader
->stats
,
155 shader
->num_stats
* sizeof(shader
->stats
[0]));
157 if (shader
->xfb_info
) {
158 uint32_t xfb_info_size
=
159 nir_xfb_info_size(shader
->xfb_info
->output_count
);
160 blob_write_uint32(blob
, xfb_info_size
);
161 blob_write_bytes(blob
, shader
->xfb_info
, xfb_info_size
);
163 blob_write_uint32(blob
, 0);
166 blob_write_bytes(blob
, shader
->bind_map
.surface_sha1
,
167 sizeof(shader
->bind_map
.surface_sha1
));
168 blob_write_bytes(blob
, shader
->bind_map
.sampler_sha1
,
169 sizeof(shader
->bind_map
.sampler_sha1
));
170 blob_write_bytes(blob
, shader
->bind_map
.push_sha1
,
171 sizeof(shader
->bind_map
.push_sha1
));
172 blob_write_uint32(blob
, shader
->bind_map
.surface_count
);
173 blob_write_uint32(blob
, shader
->bind_map
.sampler_count
);
174 blob_write_bytes(blob
, shader
->bind_map
.surface_to_descriptor
,
175 shader
->bind_map
.surface_count
*
176 sizeof(*shader
->bind_map
.surface_to_descriptor
));
177 blob_write_bytes(blob
, shader
->bind_map
.sampler_to_descriptor
,
178 shader
->bind_map
.sampler_count
*
179 sizeof(*shader
->bind_map
.sampler_to_descriptor
));
180 blob_write_bytes(blob
, shader
->bind_map
.push_ranges
,
181 sizeof(shader
->bind_map
.push_ranges
));
183 return !blob
->out_of_memory
;
186 static struct anv_shader_bin
*
187 anv_shader_bin_create_from_blob(struct anv_device
*device
,
188 struct blob_reader
*blob
)
190 gl_shader_stage stage
= blob_read_uint32(blob
);
192 uint32_t key_size
= blob_read_uint32(blob
);
193 const void *key_data
= blob_read_bytes(blob
, key_size
);
195 uint32_t kernel_size
= blob_read_uint32(blob
);
196 const void *kernel_data
= blob_read_bytes(blob
, kernel_size
);
198 uint32_t constant_data_size
= blob_read_uint32(blob
);
199 const void *constant_data
= blob_read_bytes(blob
, constant_data_size
);
201 uint32_t prog_data_size
= blob_read_uint32(blob
);
202 const struct brw_stage_prog_data
*prog_data
=
203 blob_read_bytes(blob
, prog_data_size
);
207 uint32_t num_stats
= blob_read_uint32(blob
);
208 const struct brw_compile_stats
*stats
=
209 blob_read_bytes(blob
, num_stats
* sizeof(stats
[0]));
211 const nir_xfb_info
*xfb_info
= NULL
;
212 uint32_t xfb_size
= blob_read_uint32(blob
);
214 xfb_info
= blob_read_bytes(blob
, xfb_size
);
216 struct anv_pipeline_bind_map bind_map
;
217 blob_copy_bytes(blob
, bind_map
.surface_sha1
, sizeof(bind_map
.surface_sha1
));
218 blob_copy_bytes(blob
, bind_map
.sampler_sha1
, sizeof(bind_map
.sampler_sha1
));
219 blob_copy_bytes(blob
, bind_map
.push_sha1
, sizeof(bind_map
.push_sha1
));
220 bind_map
.surface_count
= blob_read_uint32(blob
);
221 bind_map
.sampler_count
= blob_read_uint32(blob
);
222 bind_map
.surface_to_descriptor
= (void *)
223 blob_read_bytes(blob
, bind_map
.surface_count
*
224 sizeof(*bind_map
.surface_to_descriptor
));
225 bind_map
.sampler_to_descriptor
= (void *)
226 blob_read_bytes(blob
, bind_map
.sampler_count
*
227 sizeof(*bind_map
.sampler_to_descriptor
));
228 blob_copy_bytes(blob
, bind_map
.push_ranges
, sizeof(bind_map
.push_ranges
));
233 return anv_shader_bin_create(device
, stage
,
235 kernel_data
, kernel_size
,
236 constant_data
, constant_data_size
,
237 prog_data
, prog_data_size
,
238 stats
, num_stats
, xfb_info
, &bind_map
);
243 * - Compact binding table layout so it's tight and not dependent on
244 * descriptor set layout.
246 * - Review prog_data struct for size and cacheability: struct
247 * brw_stage_prog_data has binding_table which uses a lot of uint32_t for 8
248 * bit quantities etc; use bit fields for all bools, eg dual_src_blend.
252 shader_bin_key_hash_func(const void *void_key
)
254 const struct anv_shader_bin_key
*key
= void_key
;
255 return _mesa_hash_data(key
->data
, key
->size
);
259 shader_bin_key_compare_func(const void *void_a
, const void *void_b
)
261 const struct anv_shader_bin_key
*a
= void_a
, *b
= void_b
;
262 if (a
->size
!= b
->size
)
265 return memcmp(a
->data
, b
->data
, a
->size
) == 0;
269 sha1_hash_func(const void *sha1
)
271 return _mesa_hash_data(sha1
, 20);
275 sha1_compare_func(const void *sha1_a
, const void *sha1_b
)
277 return memcmp(sha1_a
, sha1_b
, 20) == 0;
281 anv_pipeline_cache_init(struct anv_pipeline_cache
*cache
,
282 struct anv_device
*device
,
286 vk_object_base_init(&device
->vk
, &cache
->base
,
287 VK_OBJECT_TYPE_PIPELINE_CACHE
);
288 cache
->device
= device
;
289 cache
->external_sync
= external_sync
;
290 pthread_mutex_init(&cache
->mutex
, NULL
);
293 cache
->cache
= _mesa_hash_table_create(NULL
, shader_bin_key_hash_func
,
294 shader_bin_key_compare_func
);
295 cache
->nir_cache
= _mesa_hash_table_create(NULL
, sha1_hash_func
,
299 cache
->nir_cache
= NULL
;
304 anv_pipeline_cache_finish(struct anv_pipeline_cache
*cache
)
306 pthread_mutex_destroy(&cache
->mutex
);
309 /* This is a bit unfortunate. In order to keep things from randomly
310 * going away, the shader cache has to hold a reference to all shader
311 * binaries it contains. We unref them when we destroy the cache.
313 hash_table_foreach(cache
->cache
, entry
)
314 anv_shader_bin_unref(cache
->device
, entry
->data
);
316 _mesa_hash_table_destroy(cache
->cache
, NULL
);
319 if (cache
->nir_cache
) {
320 hash_table_foreach(cache
->nir_cache
, entry
)
321 ralloc_free(entry
->data
);
323 _mesa_hash_table_destroy(cache
->nir_cache
, NULL
);
326 vk_object_base_finish(&cache
->base
);
329 static struct anv_shader_bin
*
330 anv_pipeline_cache_search_locked(struct anv_pipeline_cache
*cache
,
331 const void *key_data
, uint32_t key_size
)
333 uint32_t vla
[1 + DIV_ROUND_UP(key_size
, sizeof(uint32_t))];
334 struct anv_shader_bin_key
*key
= (void *)vla
;
335 key
->size
= key_size
;
336 memcpy(key
->data
, key_data
, key_size
);
338 struct hash_entry
*entry
= _mesa_hash_table_search(cache
->cache
, key
);
346 anv_cache_lock(struct anv_pipeline_cache
*cache
)
348 if (!cache
->external_sync
)
349 pthread_mutex_lock(&cache
->mutex
);
353 anv_cache_unlock(struct anv_pipeline_cache
*cache
)
355 if (!cache
->external_sync
)
356 pthread_mutex_unlock(&cache
->mutex
);
359 struct anv_shader_bin
*
360 anv_pipeline_cache_search(struct anv_pipeline_cache
*cache
,
361 const void *key_data
, uint32_t key_size
)
366 anv_cache_lock(cache
);
368 struct anv_shader_bin
*shader
=
369 anv_pipeline_cache_search_locked(cache
, key_data
, key_size
);
371 anv_cache_unlock(cache
);
373 /* We increment refcount before handing it to the caller */
375 anv_shader_bin_ref(shader
);
381 anv_pipeline_cache_add_shader_bin(struct anv_pipeline_cache
*cache
,
382 struct anv_shader_bin
*bin
)
387 anv_cache_lock(cache
);
389 struct hash_entry
*entry
= _mesa_hash_table_search(cache
->cache
, bin
->key
);
391 /* Take a reference for the cache */
392 anv_shader_bin_ref(bin
);
393 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
396 anv_cache_unlock(cache
);
399 static struct anv_shader_bin
*
400 anv_pipeline_cache_add_shader_locked(struct anv_pipeline_cache
*cache
,
401 gl_shader_stage stage
,
402 const void *key_data
, uint32_t key_size
,
403 const void *kernel_data
,
404 uint32_t kernel_size
,
405 const void *constant_data
,
406 uint32_t constant_data_size
,
407 const struct brw_stage_prog_data
*prog_data
,
408 uint32_t prog_data_size
,
409 const struct brw_compile_stats
*stats
,
411 const nir_xfb_info
*xfb_info
,
412 const struct anv_pipeline_bind_map
*bind_map
)
414 struct anv_shader_bin
*shader
=
415 anv_pipeline_cache_search_locked(cache
, key_data
, key_size
);
419 struct anv_shader_bin
*bin
=
420 anv_shader_bin_create(cache
->device
, stage
,
422 kernel_data
, kernel_size
,
423 constant_data
, constant_data_size
,
424 prog_data
, prog_data_size
,
425 stats
, num_stats
, xfb_info
, bind_map
);
429 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
434 struct anv_shader_bin
*
435 anv_pipeline_cache_upload_kernel(struct anv_pipeline_cache
*cache
,
436 gl_shader_stage stage
,
437 const void *key_data
, uint32_t key_size
,
438 const void *kernel_data
, uint32_t kernel_size
,
439 const void *constant_data
,
440 uint32_t constant_data_size
,
441 const struct brw_stage_prog_data
*prog_data
,
442 uint32_t prog_data_size
,
443 const struct brw_compile_stats
*stats
,
445 const nir_xfb_info
*xfb_info
,
446 const struct anv_pipeline_bind_map
*bind_map
)
449 anv_cache_lock(cache
);
451 struct anv_shader_bin
*bin
=
452 anv_pipeline_cache_add_shader_locked(cache
, stage
, key_data
, key_size
,
453 kernel_data
, kernel_size
,
454 constant_data
, constant_data_size
,
455 prog_data
, prog_data_size
,
459 anv_cache_unlock(cache
);
461 /* We increment refcount before handing it to the caller */
463 anv_shader_bin_ref(bin
);
467 /* In this case, we're not caching it so the caller owns it entirely */
468 return anv_shader_bin_create(cache
->device
, stage
,
470 kernel_data
, kernel_size
,
471 constant_data
, constant_data_size
,
472 prog_data
, prog_data_size
,
478 struct cache_header
{
479 uint32_t header_size
;
480 uint32_t header_version
;
483 uint8_t uuid
[VK_UUID_SIZE
];
487 anv_pipeline_cache_load(struct anv_pipeline_cache
*cache
,
488 const void *data
, size_t size
)
490 struct anv_device
*device
= cache
->device
;
491 struct anv_physical_device
*pdevice
= device
->physical
;
493 if (cache
->cache
== NULL
)
496 struct blob_reader blob
;
497 blob_reader_init(&blob
, data
, size
);
499 struct cache_header header
;
500 blob_copy_bytes(&blob
, &header
, sizeof(header
));
501 uint32_t count
= blob_read_uint32(&blob
);
505 if (header
.header_size
< sizeof(header
))
507 if (header
.header_version
!= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
)
509 if (header
.vendor_id
!= 0x8086)
511 if (header
.device_id
!= device
->info
.chipset_id
)
513 if (memcmp(header
.uuid
, pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
) != 0)
516 for (uint32_t i
= 0; i
< count
; i
++) {
517 struct anv_shader_bin
*bin
=
518 anv_shader_bin_create_from_blob(device
, &blob
);
521 _mesa_hash_table_insert(cache
->cache
, bin
->key
, bin
);
525 VkResult
anv_CreatePipelineCache(
527 const VkPipelineCacheCreateInfo
* pCreateInfo
,
528 const VkAllocationCallbacks
* pAllocator
,
529 VkPipelineCache
* pPipelineCache
)
531 ANV_FROM_HANDLE(anv_device
, device
, _device
);
532 struct anv_pipeline_cache
*cache
;
534 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
);
535 assert(pCreateInfo
->flags
== 0);
537 cache
= vk_alloc2(&device
->vk
.alloc
, pAllocator
,
539 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
541 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
543 anv_pipeline_cache_init(cache
, device
,
544 device
->physical
->instance
->pipeline_cache_enabled
,
545 pCreateInfo
->flags
& VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT_EXT
);
547 if (pCreateInfo
->initialDataSize
> 0)
548 anv_pipeline_cache_load(cache
,
549 pCreateInfo
->pInitialData
,
550 pCreateInfo
->initialDataSize
);
552 *pPipelineCache
= anv_pipeline_cache_to_handle(cache
);
557 void anv_DestroyPipelineCache(
559 VkPipelineCache _cache
,
560 const VkAllocationCallbacks
* pAllocator
)
562 ANV_FROM_HANDLE(anv_device
, device
, _device
);
563 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
568 anv_pipeline_cache_finish(cache
);
570 vk_free2(&device
->vk
.alloc
, pAllocator
, cache
);
573 VkResult
anv_GetPipelineCacheData(
575 VkPipelineCache _cache
,
579 ANV_FROM_HANDLE(anv_device
, device
, _device
);
580 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
584 blob_init_fixed(&blob
, pData
, *pDataSize
);
586 blob_init_fixed(&blob
, NULL
, SIZE_MAX
);
589 struct cache_header header
= {
590 .header_size
= sizeof(struct cache_header
),
591 .header_version
= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
,
593 .device_id
= device
->info
.chipset_id
,
595 memcpy(header
.uuid
, device
->physical
->pipeline_cache_uuid
, VK_UUID_SIZE
);
596 blob_write_bytes(&blob
, &header
, sizeof(header
));
599 intptr_t count_offset
= blob_reserve_uint32(&blob
);
600 if (count_offset
< 0) {
603 return VK_INCOMPLETE
;
606 VkResult result
= VK_SUCCESS
;
608 hash_table_foreach(cache
->cache
, entry
) {
609 struct anv_shader_bin
*shader
= entry
->data
;
611 size_t save_size
= blob
.size
;
612 if (!anv_shader_bin_write_to_blob(shader
, &blob
)) {
613 /* If it fails reset to the previous size and bail */
614 blob
.size
= save_size
;
615 result
= VK_INCOMPLETE
;
623 blob_overwrite_uint32(&blob
, count_offset
, count
);
625 *pDataSize
= blob
.size
;
632 VkResult
anv_MergePipelineCaches(
634 VkPipelineCache destCache
,
635 uint32_t srcCacheCount
,
636 const VkPipelineCache
* pSrcCaches
)
638 ANV_FROM_HANDLE(anv_pipeline_cache
, dst
, destCache
);
643 for (uint32_t i
= 0; i
< srcCacheCount
; i
++) {
644 ANV_FROM_HANDLE(anv_pipeline_cache
, src
, pSrcCaches
[i
]);
648 hash_table_foreach(src
->cache
, entry
) {
649 struct anv_shader_bin
*bin
= entry
->data
;
652 if (_mesa_hash_table_search(dst
->cache
, bin
->key
))
655 anv_shader_bin_ref(bin
);
656 _mesa_hash_table_insert(dst
->cache
, bin
->key
, bin
);
663 struct anv_shader_bin
*
664 anv_device_search_for_kernel(struct anv_device
*device
,
665 struct anv_pipeline_cache
*cache
,
666 const void *key_data
, uint32_t key_size
,
667 bool *user_cache_hit
)
669 struct anv_shader_bin
*bin
;
671 *user_cache_hit
= false;
674 bin
= anv_pipeline_cache_search(cache
, key_data
, key_size
);
676 *user_cache_hit
= cache
!= &device
->default_pipeline_cache
;
681 #ifdef ENABLE_SHADER_CACHE
682 struct disk_cache
*disk_cache
= device
->physical
->disk_cache
;
683 if (disk_cache
&& device
->physical
->instance
->pipeline_cache_enabled
) {
685 disk_cache_compute_key(disk_cache
, key_data
, key_size
, cache_key
);
688 uint8_t *buffer
= disk_cache_get(disk_cache
, cache_key
, &buffer_size
);
690 struct blob_reader blob
;
691 blob_reader_init(&blob
, buffer
, buffer_size
);
692 bin
= anv_shader_bin_create_from_blob(device
, &blob
);
697 anv_pipeline_cache_add_shader_bin(cache
, bin
);
707 struct anv_shader_bin
*
708 anv_device_upload_kernel(struct anv_device
*device
,
709 struct anv_pipeline_cache
*cache
,
710 gl_shader_stage stage
,
711 const void *key_data
, uint32_t key_size
,
712 const void *kernel_data
, uint32_t kernel_size
,
713 const void *constant_data
,
714 uint32_t constant_data_size
,
715 const struct brw_stage_prog_data
*prog_data
,
716 uint32_t prog_data_size
,
717 const struct brw_compile_stats
*stats
,
719 const nir_xfb_info
*xfb_info
,
720 const struct anv_pipeline_bind_map
*bind_map
)
722 struct anv_shader_bin
*bin
;
724 bin
= anv_pipeline_cache_upload_kernel(cache
, stage
, key_data
, key_size
,
725 kernel_data
, kernel_size
,
726 constant_data
, constant_data_size
,
727 prog_data
, prog_data_size
,
731 bin
= anv_shader_bin_create(device
, stage
, key_data
, key_size
,
732 kernel_data
, kernel_size
,
733 constant_data
, constant_data_size
,
734 prog_data
, prog_data_size
,
742 #ifdef ENABLE_SHADER_CACHE
743 struct disk_cache
*disk_cache
= device
->physical
->disk_cache
;
747 if (anv_shader_bin_write_to_blob(bin
, &binary
)) {
749 disk_cache_compute_key(disk_cache
, key_data
, key_size
, cache_key
);
751 disk_cache_put(disk_cache
, cache_key
, binary
.data
, binary
.size
, NULL
);
754 blob_finish(&binary
);
761 struct serialized_nir
{
762 unsigned char sha1_key
[20];
768 anv_device_search_for_nir(struct anv_device
*device
,
769 struct anv_pipeline_cache
*cache
,
770 const nir_shader_compiler_options
*nir_options
,
771 unsigned char sha1_key
[20],
774 if (cache
&& cache
->nir_cache
) {
775 const struct serialized_nir
*snir
= NULL
;
777 anv_cache_lock(cache
);
778 struct hash_entry
*entry
=
779 _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
782 anv_cache_unlock(cache
);
785 struct blob_reader blob
;
786 blob_reader_init(&blob
, snir
->data
, snir
->size
);
788 nir_shader
*nir
= nir_deserialize(mem_ctx
, nir_options
, &blob
);
801 anv_device_upload_nir(struct anv_device
*device
,
802 struct anv_pipeline_cache
*cache
,
803 const struct nir_shader
*nir
,
804 unsigned char sha1_key
[20])
806 if (cache
&& cache
->nir_cache
) {
807 anv_cache_lock(cache
);
808 struct hash_entry
*entry
=
809 _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
810 anv_cache_unlock(cache
);
817 nir_serialize(&blob
, nir
, false);
818 if (blob
.out_of_memory
) {
823 anv_cache_lock(cache
);
824 /* Because ralloc isn't thread-safe, we have to do all this inside the
825 * lock. We could unlock for the big memcpy but it's probably not worth
828 entry
= _mesa_hash_table_search(cache
->nir_cache
, sha1_key
);
831 anv_cache_unlock(cache
);
835 struct serialized_nir
*snir
=
836 ralloc_size(cache
->nir_cache
, sizeof(*snir
) + blob
.size
);
837 memcpy(snir
->sha1_key
, sha1_key
, 20);
838 snir
->size
= blob
.size
;
839 memcpy(snir
->data
, blob
.data
, blob
.size
);
843 _mesa_hash_table_insert(cache
->nir_cache
, snir
->sha1_key
, snir
);
845 anv_cache_unlock(cache
);