From 78e637f857d0a58a7e35371cf3f7b8c2d17fbf1a Mon Sep 17 00:00:00 2001 From: Dawn Han Date: Mon, 27 Jun 2022 19:23:22 +0000 Subject: [PATCH] Refactor the descriptor enums to be extensible Signed-off-by: Dawn Han Part-of: --- src/virtio/vulkan/vn_descriptor_set.c | 50 +++++++++++++++++++++++---- src/virtio/vulkan/vn_descriptor_set.h | 26 +++++++++++--- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/virtio/vulkan/vn_descriptor_set.c b/src/virtio/vulkan/vn_descriptor_set.c index 0c428b57ce6..9da279f87d0 100644 --- a/src/virtio/vulkan/vn_descriptor_set.c +++ b/src/virtio/vulkan/vn_descriptor_set.c @@ -62,6 +62,40 @@ vn_descriptor_set_destroy(struct vn_device *dev, vk_free(alloc, set); } +/* Mapping VkDescriptorType to array index */ +static enum vn_descriptor_type +vn_descriptor_type_index(VkDescriptorType type) +{ + switch (type) { + case VK_DESCRIPTOR_TYPE_SAMPLER: + return VN_DESCRIPTOR_TYPE_SAMPLER; + case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: + return VN_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: + return VN_DESCRIPTOR_TYPE_SAMPLED_IMAGE; + case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: + return VN_DESCRIPTOR_TYPE_STORAGE_IMAGE; + case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: + return VN_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; + case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: + return VN_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; + case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: + return VN_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: + return VN_DESCRIPTOR_TYPE_STORAGE_BUFFER; + case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: + return VN_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC; + case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: + return VN_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC; + case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: + return VN_DESCRIPTOR_TYPE_INPUT_ATTACHMENT; + default: + break; + } + + unreachable("bad VkDescriptorType"); +} + /* descriptor set layout commands */ void @@ -268,11 +302,11 @@ vn_CreateDescriptorPool(VkDevice device, for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) { const VkDescriptorPoolSize *pool_size = &pCreateInfo->pPoolSizes[i]; + const uint32_t type_index = vn_descriptor_type_index(pool_size->type); - assert(pool_size->type < VN_NUM_DESCRIPTOR_TYPES); + assert(type_index < VN_NUM_DESCRIPTOR_TYPES); - pool->max.descriptor_counts[pool_size->type] += - pool_size->descriptorCount; + pool->max.descriptor_counts[type_index] += pool_size->descriptorCount; } list_inithead(&pool->descriptor_sets); @@ -341,10 +375,11 @@ vn_descriptor_pool_alloc_descriptors( ? last_binding_descriptor_count : layout->bindings[i].count; - pool->used.descriptor_counts[type] += count; + const uint32_t type_index = vn_descriptor_type_index(type); + pool->used.descriptor_counts[type_index] += count; - if (pool->used.descriptor_counts[type] > - pool->max.descriptor_counts[type]) { + if (pool->used.descriptor_counts[type_index] > + pool->max.descriptor_counts[type_index]) { /* restore pool state before this allocation */ pool->used = recovery; return false; @@ -368,7 +403,8 @@ vn_descriptor_pool_free_descriptors( ? last_binding_descriptor_count : layout->bindings[i].count; - pool->used.descriptor_counts[layout->bindings[i].type] -= count; + pool->used.descriptor_counts[vn_descriptor_type_index( + layout->bindings[i].type)] -= count; } --pool->used.set_count; diff --git a/src/virtio/vulkan/vn_descriptor_set.h b/src/virtio/vulkan/vn_descriptor_set.h index 240a52d48dd..a06d79924cd 100644 --- a/src/virtio/vulkan/vn_descriptor_set.h +++ b/src/virtio/vulkan/vn_descriptor_set.h @@ -13,12 +13,28 @@ #include "vn_common.h" -/* TODO accommodate new discrete type enums by: - * 1. increase the number of types here - * 2. add a helper to map to continuous array index - */ -#define VN_NUM_DESCRIPTOR_TYPES (VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT + 1) +enum vn_descriptor_type { + VN_DESCRIPTOR_TYPE_SAMPLER, + VN_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + VN_DESCRIPTOR_TYPE_SAMPLED_IMAGE, + VN_DESCRIPTOR_TYPE_STORAGE_IMAGE, + VN_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, + VN_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, + VN_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + VN_DESCRIPTOR_TYPE_STORAGE_BUFFER, + VN_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, + VN_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, + VN_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, + /* add new enum types before this line */ + VN_NUM_DESCRIPTOR_TYPES, +}; + +/* TODO refactor struct to track enum vn_descriptor_type type. + * On VkDescriptorSetLayout creation. When we check against + * VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK, it will be against + * VN_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK instead + */ struct vn_descriptor_set_layout_binding { VkDescriptorType type; uint32_t count;