From d4630b703d23821ac57e52e9b9b58307029aa404 Mon Sep 17 00:00:00 2001 From: Boris Brezillon Date: Tue, 21 Jun 2022 17:12:17 +0200 Subject: [PATCH] dzn: Add a generic cached blob wrapper Basically what vk_pipeline_cache's raw_data_object abstraction provides, but I'm not sure it makes sense to make it generic so I copied it here. Might be more appropriate to make raw_data_object public. Reviewed-by: Jesse Natalie Part-of: --- src/microsoft/vulkan/dzn_device.c | 7 +++ src/microsoft/vulkan/dzn_pipeline.c | 83 +++++++++++++++++++++++++++++ src/microsoft/vulkan/dzn_private.h | 2 + 3 files changed, 92 insertions(+) diff --git a/src/microsoft/vulkan/dzn_device.c b/src/microsoft/vulkan/dzn_device.c index dd03d853c0d..8503643ddc4 100644 --- a/src/microsoft/vulkan/dzn_device.c +++ b/src/microsoft/vulkan/dzn_device.c @@ -281,6 +281,11 @@ dzn_physical_device_init_uuids(struct dzn_physical_device *pdev) memcpy(pdev->device_uuid, sha1, VK_UUID_SIZE); } +const struct vk_pipeline_cache_object_ops *const dzn_pipeline_cache_import_ops[] = { + &dzn_cached_blob_ops, + NULL, +}; + static VkResult dzn_physical_device_create(struct dzn_instance *instance, IDXGIAdapter1 *adapter, @@ -326,6 +331,8 @@ dzn_physical_device_create(struct dzn_instance *instance, assert(num_sync_types <= MAX_SYNC_TYPES); pdev->vk.supported_sync_types = pdev->sync_types; + pdev->vk.pipeline_cache_import_ops = dzn_pipeline_cache_import_ops; + /* TODO: something something queue families */ result = dzn_wsi_init(pdev); diff --git a/src/microsoft/vulkan/dzn_pipeline.c b/src/microsoft/vulkan/dzn_pipeline.c index a04842a320c..1fab857c3ad 100644 --- a/src/microsoft/vulkan/dzn_pipeline.c +++ b/src/microsoft/vulkan/dzn_pipeline.c @@ -36,6 +36,7 @@ #include "vk_util.h" #include "vk_format.h" #include "vk_pipeline.h" +#include "vk_pipeline_cache.h" #include "util/u_debug.h" @@ -73,6 +74,88 @@ gfx_pipeline_variant_key_hash(const void *key) return _mesa_hash_data(key, sizeof(struct dzn_graphics_pipeline_variant_key)); } +struct dzn_cached_blob { + struct vk_pipeline_cache_object base; + uint8_t hash[SHA1_DIGEST_LENGTH]; + const void *data; + size_t size; +}; + +static bool +dzn_cached_blob_serialize(struct vk_pipeline_cache_object *object, + struct blob *blob) +{ + struct dzn_cached_blob *cached_blob = + container_of(object, struct dzn_cached_blob, base); + + blob_write_bytes(blob, cached_blob->data, cached_blob->size); + return true; +} + +static void +dzn_cached_blob_destroy(struct vk_pipeline_cache_object *object) +{ + struct dzn_cached_blob *shader = + container_of(object, struct dzn_cached_blob, base); + + vk_free(&shader->base.device->alloc, shader); +} + +static struct vk_pipeline_cache_object * +dzn_cached_blob_create(struct vk_device *device, + const void *hash, + const void *data, + size_t data_size); + +static struct vk_pipeline_cache_object * +dzn_cached_blob_deserialize(struct vk_device *device, + const void *key_data, + size_t key_size, + struct blob_reader *blob) +{ + size_t data_size = blob->end - blob->current; + assert(key_size == SHA1_DIGEST_LENGTH); + + return dzn_cached_blob_create(device, key_data, + blob_read_bytes(blob, data_size), + data_size); +} + +const struct vk_pipeline_cache_object_ops dzn_cached_blob_ops = { + .serialize = dzn_cached_blob_serialize, + .deserialize = dzn_cached_blob_deserialize, + .destroy = dzn_cached_blob_destroy, +}; + + +static struct vk_pipeline_cache_object * +dzn_cached_blob_create(struct vk_device *device, + const void *hash, + const void *data, + size_t data_size) +{ + VK_MULTIALLOC(ma); + VK_MULTIALLOC_DECL(&ma, struct dzn_cached_blob, blob, 1); + VK_MULTIALLOC_DECL(&ma, uint8_t, copy, data_size); + + if (!vk_multialloc_alloc(&ma, &device->alloc, + VK_SYSTEM_ALLOCATION_SCOPE_DEVICE)) + return NULL; + + memcpy(blob->hash, hash, sizeof(blob->hash)); + + vk_pipeline_cache_object_init(device, &blob->base, + &dzn_cached_blob_ops, + blob->hash, sizeof(blob->hash)); + + if (data) + memcpy(copy, data, data_size); + blob->data = copy; + blob->size = data_size; + + return &blob->base; +} + static VkResult dzn_graphics_pipeline_prepare_for_variants(struct dzn_device *device, struct dzn_graphics_pipeline *pipeline) diff --git a/src/microsoft/vulkan/dzn_private.h b/src/microsoft/vulkan/dzn_private.h index 147a2fef699..79536db92e2 100644 --- a/src/microsoft/vulkan/dzn_private.h +++ b/src/microsoft/vulkan/dzn_private.h @@ -756,6 +756,8 @@ struct dzn_pipeline { ID3D12PipelineState *state; }; +extern const struct vk_pipeline_cache_object_ops dzn_cached_blob_ops; + enum dzn_indirect_draw_cmd_sig_type { DZN_INDIRECT_DRAW_CMD_SIG, DZN_INDIRECT_INDEXED_DRAW_CMD_SIG,