vulkan/pipeline_cache: Add helpers for storing NIR in the cache

Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13184>
This commit is contained in:
Jason Ekstrand
2021-10-04 15:14:29 -05:00
committed by Marge Bot
parent 591da98779
commit 93d0c66b27
3 changed files with 73 additions and 4 deletions

View File

@@ -79,7 +79,7 @@ vulkan_runtime_files = files(
vulkan_runtime_deps = [
vulkan_wsi_deps,
idep_mesautil,
idep_nir_headers,
idep_nir,
idep_vulkan_util,
]

View File

@@ -29,6 +29,8 @@
#include "vk_log.h"
#include "vk_physical_device.h"
#include "compiler/nir/nir_serialize.h"
#include "util/blob.h"
#include "util/debug.h"
#include "util/disk_cache.h"
@@ -327,15 +329,13 @@ vk_pipeline_cache_lookup_object(struct vk_pipeline_cache *cache,
return NULL;
}
if (object->ops == &raw_data_object_ops) {
if (object->ops == &raw_data_object_ops && ops != &raw_data_object_ops) {
/* The object isn't fully formed yet and we need to deserialize it into
* a real object before it can be used.
*/
struct raw_data_object *data_obj =
container_of(object, struct raw_data_object, base);
assert(ops != &raw_data_object_ops);
struct vk_pipeline_cache_object *real_object =
vk_pipeline_cache_object_deserialize(cache,
data_obj->base.key_data,
@@ -412,6 +412,61 @@ vk_pipeline_cache_add_object(struct vk_pipeline_cache *cache,
}
}
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)
{
struct vk_pipeline_cache_object *object =
vk_pipeline_cache_lookup_object(cache, key_data, key_size,
&raw_data_object_ops, cache_hit);
if (object == NULL)
return NULL;
struct raw_data_object *data_obj =
container_of(object, struct raw_data_object, base);
struct blob_reader blob;
blob_reader_init(&blob, data_obj->data, data_obj->data_size);
nir_shader *nir = nir_deserialize(mem_ctx, nir_options, &blob);
vk_pipeline_cache_object_unref(object);
if (blob.overrun) {
ralloc_free(nir);
return NULL;
}
return nir;
}
void
vk_pipeline_cache_add_nir(struct vk_pipeline_cache *cache,
const void *key_data, size_t key_size,
const nir_shader *nir)
{
struct blob blob;
blob_init(&blob);
nir_serialize(&blob, nir, false);
if (blob.out_of_memory) {
vk_logw(VK_LOG_OBJS(cache), "Ran out of memory serializing NIR shader");
blob_finish(&blob);
return;
}
struct raw_data_object *data_obj =
raw_data_object_create(cache->base.device,
key_data, key_size,
blob.data, blob.size);
blob_finish(&blob);
struct vk_pipeline_cache_object *cached =
vk_pipeline_cache_add_object(cache, &data_obj->base);
vk_pipeline_cache_object_unref(cached);
}
static int32_t
find_type_for_ops(const struct vk_physical_device *pdevice,
const struct vk_pipeline_cache_object_ops *ops)

View File

@@ -39,6 +39,10 @@ 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;
@@ -236,6 +240,16 @@ struct vk_pipeline_cache_object * MUST_CHECK
vk_pipeline_cache_add_object(struct vk_pipeline_cache *cache,
struct vk_pipeline_cache_object *object);
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);
#ifdef __cplusplus
}
#endif