1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
* Copyright © 2021 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef VK_PIPELINE_CACHE_H
#define VK_PIPELINE_CACHE_H
#include "vk_object.h"
#include "vk_util.h"
#include "util/simple_mtx.h"
#ifdef __cplusplus
extern "C" {
#endif
/* #include "util/blob.h" */
struct blob;
struct blob_reader;
/* #include "util/set.h" */
struct set;
/* #include "compiler/nir/nir.h" */
struct nir_shader;
struct nir_shader_compiler_options;
struct vk_pipeline_cache;
struct vk_pipeline_cache_object;
#define VK_PIPELINE_CACHE_BLOB_ALIGN 8
struct vk_pipeline_cache_object_ops {
/** Writes this cache object to the given blob
*
* Because the cache works with both raw blob data and driver object data
* and can't always tell the difference between the two, we have to be very
* careful about alignments when [de]serializing. When serialize() is
* called, the blob will be aligned to VK_PIPELINE_CACHE_BLOB_ALIGN. The
* driver must be careful to not [de]serialize any data types which require
* a higher alignment. When deserialize() is called, the blob_reader is
* also guaranteed to be aligned to VK_PIPELINE_CACHE_BLOB_ALIGN.
*
* Returns true on success
*
* This function is optional. Objects without [de]serialization support
* will still be cached in memory but will not be placed in the disk cache
* and will not exported to the client when vkGetPipelineCacheData() is
* called.
*/
bool (*serialize)(struct vk_pipeline_cache_object *object,
struct blob *blob);
/** Constructs an object from cached data
*
* See serialize() for details about data alignment.
*
* returns the created object
*
* This function is optional.
*/
struct vk_pipeline_cache_object *(*deserialize)(struct vk_pipeline_cache *cache,
const void *key_data,
size_t key_size,
struct blob_reader *blob);
/** Destroys the object
*
* Called when vk_pipeline_cache_object.ref_cnt hits 0.
*/
void (*destroy)(struct vk_device *device,
struct vk_pipeline_cache_object *object);
};
/** Base struct for cached objects
*
* A vk_pipeline_cache stores any number of vk_pipeline_cache_object's, each
* of which has an associated key of arbitrary size. Cached objects are
* reference counted so that they can exist in multiple caches (for example,
* when vkMergePipelineCaches() is called) and so that they can persist after
* the pipeline cache is destroyed. Each object also has a pointer to a
* vk_pipeline_cache_object_ops table which the pipeline cache uses to
* [de]serialize the object and clean it up when the reference count hits 0.
*
* The rest of the details of any given object are entirely up to the driver.
* The driver may even have multiple types of objects (distinguished by their
* vk_pipeline_cache_object_ops table) in the cache so long as it guarantees
* it never has two objects of different types with the same key.
*/
struct vk_pipeline_cache_object {
const struct vk_pipeline_cache_object_ops *ops;
uint32_t ref_cnt;
uint32_t data_size;
const void *key_data;
uint32_t key_size;
};
static inline void
vk_pipeline_cache_object_init(struct vk_device *device,
struct vk_pipeline_cache_object *object,
const struct vk_pipeline_cache_object_ops *ops,
const void *key_data, uint32_t key_size)
{
memset(object, 0, sizeof(*object));
object->ops = ops;
p_atomic_set(&object->ref_cnt, 1);
object->data_size = 0; /* Unknown */
object->key_data = key_data;
object->key_size = key_size;
}
static inline void
vk_pipeline_cache_object_finish(struct vk_pipeline_cache_object *object)
{
assert(p_atomic_read(&object->ref_cnt) <= 1);
}
static inline struct vk_pipeline_cache_object *
vk_pipeline_cache_object_ref(struct vk_pipeline_cache_object *object)
{
assert(object && p_atomic_read(&object->ref_cnt) >= 1);
p_atomic_inc(&object->ref_cnt);
return object;
}
static inline void
vk_pipeline_cache_object_unref(struct vk_device *device,
struct vk_pipeline_cache_object *object)
{
assert(object && p_atomic_read(&object->ref_cnt) >= 1);
if (p_atomic_dec_zero(&object->ref_cnt))
object->ops->destroy(device, object);
}
/** A generic implementation of VkPipelineCache */
struct vk_pipeline_cache {
struct vk_object_base base;
/* pCreateInfo::flags */
VkPipelineCacheCreateFlags flags;
struct vk_pipeline_cache_header header;
/** Protects object_cache */
simple_mtx_t lock;
struct set *object_cache;
};
VK_DEFINE_NONDISP_HANDLE_CASTS(vk_pipeline_cache, base, VkPipelineCache,
VK_OBJECT_TYPE_PIPELINE_CACHE)
struct vk_pipeline_cache_create_info {
/* The pCreateInfo for this pipeline cache, if any.
*
* For driver-internal caches, this is allowed to be NULL.
*/
const VkPipelineCacheCreateInfo *pCreateInfo;
/** If true, ignore VK_ENABLE_PIPELINE_CACHE and enable anyway */
bool force_enable;
};
struct vk_pipeline_cache *
vk_pipeline_cache_create(struct vk_device *device,
const struct vk_pipeline_cache_create_info *info,
const VkAllocationCallbacks *pAllocator);
void
vk_pipeline_cache_destroy(struct vk_pipeline_cache *cache,
const VkAllocationCallbacks *pAllocator);
/** Attempts to look up an object in the cache by key
*
* If an object is found in the cache matching the given key, *cache_hit is
* set to true and a reference to that object is returned.
*
* If the driver sets vk_device.disk_cache, we attempt to look up any missing
* objects in the disk cache before declaring failure. If an object is found
* in the disk cache but not the in-memory cache, *cache_hit is set to false.
*
* The deserialization of pipeline cache objects found in the cache data
* provided via VkPipelineCacheCreateInfo::pInitialData happens during
* vk_pipeline_cache_lookup() rather than during vkCreatePipelineCache().
* Prior to the first vk_pipeline_cache_lookup() of a given object, it is
* stored as an internal raw data object with the same hash. This allows us
* to avoid any complex object type tagging in the serialized cache. It does,
* however, mean that drivers need to be careful to ensure that objects with
* different types (ops) have different keys.
*
* Returns a reference to the object, if found
*/
struct vk_pipeline_cache_object * MUST_CHECK
vk_pipeline_cache_lookup_object(struct vk_pipeline_cache *cache,
const void *key_data, size_t key_size,
const struct vk_pipeline_cache_object_ops *ops,
bool *cache_hit);
/** Adds an object to the pipeline cache
*
* This function adds the given object to the pipeline cache. We do not
* specify a key here because the key is part of the object. See also
* vk_pipeline_cache_object_init().
*
* This function consumes a reference to the object and returns a reference to
* the (possibly different) object in the cache. The intended usage pattern
* is as follows:
*
* key = compute_key();
* struct vk_pipeline_cache_object *object =
* vk_pipeline_cache_lookup_object(cache, &key, sizeof(key),
* &driver_type_ops, &cache_hit);
* if (object != NULL)
* return container_of(object, driver_type, base);
*
* object = do_compile();
* assert(object != NULL);
*
* object = vk_pipeline_cache_add_object(cache, object);
* return container_of(object, driver_type, base);
*/
struct vk_pipeline_cache_object * MUST_CHECK
vk_pipeline_cache_add_object(struct vk_pipeline_cache *cache,
struct vk_pipeline_cache_object *object);
/** Creates and inserts an object into the pipeline cache
*
* This function takes serialized data and emplaces the deserialized object
* into the pipeline cache. It is the responsibility of the caller to
* specify a deserialize() function that properly initializes the object.
*
* This function can be used to avoid an extra serialize() step for
* disk-cache insertion. For the intended usage pattern, see
* vk_pipeline_cache_add_object().
*
*/
struct vk_pipeline_cache_object *
vk_pipeline_cache_create_and_insert_object(struct vk_pipeline_cache *cache,
const void *key_data, uint32_t key_size,
const void *data, size_t data_size,
const struct vk_pipeline_cache_object_ops *ops);
struct nir_shader *
vk_pipeline_cache_lookup_nir(struct vk_pipeline_cache *cache,
const void *key_data, size_t key_size,
const struct nir_shader_compiler_options *nir_options,
bool *cache_hit, void *mem_ctx);
void
vk_pipeline_cache_add_nir(struct vk_pipeline_cache *cache,
const void *key_data, size_t key_size,
const struct nir_shader *nir);
/** Specialized type of vk_pipeline_cache_object for raw data objects.
*
* This cache object implementation, together with vk_raw_data_cache_object_ops,
* can be used to cache plain objects as well as already serialized data.
*/
struct vk_raw_data_cache_object {
struct vk_pipeline_cache_object base;
const void *data;
size_t data_size;
};
struct vk_raw_data_cache_object *
vk_raw_data_cache_object_create(struct vk_device *device,
const void *key_data, size_t key_size,
const void *data, size_t data_size);
extern const struct vk_pipeline_cache_object_ops vk_raw_data_cache_object_ops;
#ifdef __cplusplus
}
#endif
#endif /* VK_PIPELINE_CACHE_H */
|