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
21 * DEALINGS IN THE SOFTWARE.
24 #include "tu_private.h"
26 #include "util/debug.h"
27 #include "util/disk_cache.h"
28 #include "util/mesa-sha1.h"
29 #include "util/u_atomic.h"
31 struct cache_entry_variant_info
38 unsigned char sha1
[20];
41 uint32_t code_sizes
[MESA_SHADER_STAGES
];
42 struct tu_shader_variant
*variants
[MESA_SHADER_STAGES
];
47 tu_pipeline_cache_init(struct tu_pipeline_cache
*cache
,
48 struct tu_device
*device
)
50 cache
->device
= device
;
51 pthread_mutex_init(&cache
->mutex
, NULL
);
53 cache
->modified
= false;
54 cache
->kernel_count
= 0;
55 cache
->total_size
= 0;
56 cache
->table_size
= 1024;
57 const size_t byte_size
= cache
->table_size
* sizeof(cache
->hash_table
[0]);
58 cache
->hash_table
= malloc(byte_size
);
60 /* We don't consider allocation failure fatal, we just start with a 0-sized
61 * cache. Disable caching when we want to keep shader debug info, since
62 * we don't get the debug info on cached shaders. */
63 if (cache
->hash_table
== NULL
)
64 cache
->table_size
= 0;
66 memset(cache
->hash_table
, 0, byte_size
);
70 tu_pipeline_cache_finish(struct tu_pipeline_cache
*cache
)
72 for (unsigned i
= 0; i
< cache
->table_size
; ++i
)
73 if (cache
->hash_table
[i
]) {
74 vk_free(&cache
->alloc
, cache
->hash_table
[i
]);
76 pthread_mutex_destroy(&cache
->mutex
);
77 free(cache
->hash_table
);
81 entry_size(struct cache_entry
*entry
)
83 size_t ret
= sizeof(*entry
);
84 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
85 if (entry
->code_sizes
[i
])
87 sizeof(struct cache_entry_variant_info
) + entry
->code_sizes
[i
];
91 static struct cache_entry
*
92 tu_pipeline_cache_search_unlocked(struct tu_pipeline_cache
*cache
,
93 const unsigned char *sha1
)
95 const uint32_t mask
= cache
->table_size
- 1;
96 const uint32_t start
= (*(uint32_t *) sha1
);
98 if (cache
->table_size
== 0)
101 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
102 const uint32_t index
= (start
+ i
) & mask
;
103 struct cache_entry
*entry
= cache
->hash_table
[index
];
108 if (memcmp(entry
->sha1
, sha1
, sizeof(entry
->sha1
)) == 0) {
113 unreachable("hash table should never be full");
116 static struct cache_entry
*
117 tu_pipeline_cache_search(struct tu_pipeline_cache
*cache
,
118 const unsigned char *sha1
)
120 struct cache_entry
*entry
;
122 pthread_mutex_lock(&cache
->mutex
);
124 entry
= tu_pipeline_cache_search_unlocked(cache
, sha1
);
126 pthread_mutex_unlock(&cache
->mutex
);
132 tu_pipeline_cache_set_entry(struct tu_pipeline_cache
*cache
,
133 struct cache_entry
*entry
)
135 const uint32_t mask
= cache
->table_size
- 1;
136 const uint32_t start
= entry
->sha1_dw
[0];
138 /* We'll always be able to insert when we get here. */
139 assert(cache
->kernel_count
< cache
->table_size
/ 2);
141 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
142 const uint32_t index
= (start
+ i
) & mask
;
143 if (!cache
->hash_table
[index
]) {
144 cache
->hash_table
[index
] = entry
;
149 cache
->total_size
+= entry_size(entry
);
150 cache
->kernel_count
++;
154 tu_pipeline_cache_grow(struct tu_pipeline_cache
*cache
)
156 const uint32_t table_size
= cache
->table_size
* 2;
157 const uint32_t old_table_size
= cache
->table_size
;
158 const size_t byte_size
= table_size
* sizeof(cache
->hash_table
[0]);
159 struct cache_entry
**table
;
160 struct cache_entry
**old_table
= cache
->hash_table
;
162 table
= malloc(byte_size
);
164 return vk_error(cache
->device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
166 cache
->hash_table
= table
;
167 cache
->table_size
= table_size
;
168 cache
->kernel_count
= 0;
169 cache
->total_size
= 0;
171 memset(cache
->hash_table
, 0, byte_size
);
172 for (uint32_t i
= 0; i
< old_table_size
; i
++) {
173 struct cache_entry
*entry
= old_table
[i
];
177 tu_pipeline_cache_set_entry(cache
, entry
);
186 tu_pipeline_cache_add_entry(struct tu_pipeline_cache
*cache
,
187 struct cache_entry
*entry
)
189 if (cache
->kernel_count
== cache
->table_size
/ 2)
190 tu_pipeline_cache_grow(cache
);
192 /* Failing to grow that hash table isn't fatal, but may mean we don't
193 * have enough space to add this new kernel. Only add it if there's room.
195 if (cache
->kernel_count
< cache
->table_size
/ 2)
196 tu_pipeline_cache_set_entry(cache
, entry
);
201 uint32_t header_size
;
202 uint32_t header_version
;
205 uint8_t uuid
[VK_UUID_SIZE
];
209 tu_pipeline_cache_load(struct tu_pipeline_cache
*cache
,
213 struct tu_device
*device
= cache
->device
;
214 struct cache_header header
;
216 if (size
< sizeof(header
))
218 memcpy(&header
, data
, sizeof(header
));
219 if (header
.header_size
< sizeof(header
))
221 if (header
.header_version
!= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
)
223 if (header
.vendor_id
!= 0 /* TODO */)
225 if (header
.device_id
!= 0 /* TODO */)
227 if (memcmp(header
.uuid
, device
->physical_device
->cache_uuid
,
231 char *end
= (void *) data
+ size
;
232 char *p
= (void *) data
+ header
.header_size
;
234 while (end
- p
>= sizeof(struct cache_entry
)) {
235 struct cache_entry
*entry
= (struct cache_entry
*) p
;
236 struct cache_entry
*dest_entry
;
237 size_t size
= entry_size(entry
);
242 vk_alloc(&cache
->alloc
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE
);
244 memcpy(dest_entry
, entry
, size
);
245 for (int i
= 0; i
< MESA_SHADER_STAGES
; ++i
)
246 dest_entry
->variants
[i
] = NULL
;
247 tu_pipeline_cache_add_entry(cache
, dest_entry
);
254 tu_CreatePipelineCache(VkDevice _device
,
255 const VkPipelineCacheCreateInfo
*pCreateInfo
,
256 const VkAllocationCallbacks
*pAllocator
,
257 VkPipelineCache
*pPipelineCache
)
259 TU_FROM_HANDLE(tu_device
, device
, _device
);
260 struct tu_pipeline_cache
*cache
;
262 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
);
263 assert(pCreateInfo
->flags
== 0);
265 cache
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*cache
), 8,
266 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
268 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
271 cache
->alloc
= *pAllocator
;
273 cache
->alloc
= device
->alloc
;
275 tu_pipeline_cache_init(cache
, device
);
277 if (pCreateInfo
->initialDataSize
> 0) {
278 tu_pipeline_cache_load(cache
, pCreateInfo
->pInitialData
,
279 pCreateInfo
->initialDataSize
);
282 *pPipelineCache
= tu_pipeline_cache_to_handle(cache
);
288 tu_DestroyPipelineCache(VkDevice _device
,
289 VkPipelineCache _cache
,
290 const VkAllocationCallbacks
*pAllocator
)
292 TU_FROM_HANDLE(tu_device
, device
, _device
);
293 TU_FROM_HANDLE(tu_pipeline_cache
, cache
, _cache
);
297 tu_pipeline_cache_finish(cache
);
299 vk_free2(&device
->alloc
, pAllocator
, cache
);
303 tu_GetPipelineCacheData(VkDevice _device
,
304 VkPipelineCache _cache
,
308 TU_FROM_HANDLE(tu_device
, device
, _device
);
309 TU_FROM_HANDLE(tu_pipeline_cache
, cache
, _cache
);
310 struct cache_header
*header
;
311 VkResult result
= VK_SUCCESS
;
313 pthread_mutex_lock(&cache
->mutex
);
315 const size_t size
= sizeof(*header
) + cache
->total_size
;
317 pthread_mutex_unlock(&cache
->mutex
);
321 if (*pDataSize
< sizeof(*header
)) {
322 pthread_mutex_unlock(&cache
->mutex
);
324 return VK_INCOMPLETE
;
326 void *p
= pData
, *end
= pData
+ *pDataSize
;
328 header
->header_size
= sizeof(*header
);
329 header
->header_version
= VK_PIPELINE_CACHE_HEADER_VERSION_ONE
;
330 header
->vendor_id
= 0 /* TODO */;
331 header
->device_id
= 0 /* TODO */;
332 memcpy(header
->uuid
, device
->physical_device
->cache_uuid
, VK_UUID_SIZE
);
333 p
+= header
->header_size
;
335 struct cache_entry
*entry
;
336 for (uint32_t i
= 0; i
< cache
->table_size
; i
++) {
337 if (!cache
->hash_table
[i
])
339 entry
= cache
->hash_table
[i
];
340 const uint32_t size
= entry_size(entry
);
341 if (end
< p
+ size
) {
342 result
= VK_INCOMPLETE
;
346 memcpy(p
, entry
, size
);
347 for (int j
= 0; j
< MESA_SHADER_STAGES
; ++j
)
348 ((struct cache_entry
*) p
)->variants
[j
] = NULL
;
351 *pDataSize
= p
- pData
;
353 pthread_mutex_unlock(&cache
->mutex
);
358 tu_pipeline_cache_merge(struct tu_pipeline_cache
*dst
,
359 struct tu_pipeline_cache
*src
)
361 for (uint32_t i
= 0; i
< src
->table_size
; i
++) {
362 struct cache_entry
*entry
= src
->hash_table
[i
];
363 if (!entry
|| tu_pipeline_cache_search(dst
, entry
->sha1
))
366 tu_pipeline_cache_add_entry(dst
, entry
);
368 src
->hash_table
[i
] = NULL
;
373 tu_MergePipelineCaches(VkDevice _device
,
374 VkPipelineCache destCache
,
375 uint32_t srcCacheCount
,
376 const VkPipelineCache
*pSrcCaches
)
378 TU_FROM_HANDLE(tu_pipeline_cache
, dst
, destCache
);
380 for (uint32_t i
= 0; i
< srcCacheCount
; i
++) {
381 TU_FROM_HANDLE(tu_pipeline_cache
, src
, pSrcCaches
[i
]);
383 tu_pipeline_cache_merge(dst
, src
);