venus: add vn_refcount to vn_descriptor_set_layout
authorChia-I Wu <olvaffe@gmail.com>
Tue, 28 Sep 2021 16:07:13 +0000 (09:07 -0700)
committerMarge Bot <eric+marge@anholt.net>
Thu, 30 Sep 2021 03:16:19 +0000 (03:16 +0000)
The reference count does not go beyond 1 yet.

Signed-off-by: Chia-I Wu <olvaffe@gmail.com>
Reviewed-by: Yiwei Zhang <zzyiwei@chromium.org>
Reviewed-by: Ryan Neph <ryanneph@google.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13090>

src/virtio/vulkan/vn_descriptor_set.c
src/virtio/vulkan/vn_descriptor_set.h

index e60130e..6c32f9d 100644 (file)
 
 #include "vn_device.h"
 
+void
+vn_descriptor_set_layout_destroy(struct vn_device *dev,
+                                 struct vn_descriptor_set_layout *layout)
+{
+   VkDevice dev_handle = vn_device_to_handle(dev);
+   VkDescriptorSetLayout layout_handle =
+      vn_descriptor_set_layout_to_handle(layout);
+   const VkAllocationCallbacks *alloc = &dev->base.base.alloc;
+
+   vn_async_vkDestroyDescriptorSetLayout(dev->instance, dev_handle,
+                                         layout_handle, NULL);
+
+   vn_object_base_fini(&layout->base);
+   vk_free(alloc, layout);
+}
+
 static void
 vn_descriptor_set_destroy(struct vn_device *dev,
                           struct vn_descriptor_set *set,
@@ -66,6 +82,7 @@ vn_descriptor_set_layout_init(
    if (binding_flags && !binding_flags->bindingCount)
       binding_flags = NULL;
 
+   layout->refcount = VN_REFCOUNT_INIT(1);
    layout->last_binding = last_binding;
 
    for (uint32_t i = 0; i < create_info->bindingCount; i++) {
@@ -120,8 +137,8 @@ vn_CreateDescriptorSetLayout(
    VkDescriptorSetLayout *pSetLayout)
 {
    struct vn_device *dev = vn_device_from_handle(device);
-   const VkAllocationCallbacks *alloc =
-      pAllocator ? pAllocator : &dev->base.base.alloc;
+   /* ignore pAllocator as the layout is reference-counted */
+   const VkAllocationCallbacks *alloc = &dev->base.base.alloc;
 
    uint32_t last_binding = 0;
    VkDescriptorSetLayoutBinding *local_bindings = NULL;
@@ -161,9 +178,10 @@ vn_CreateDescriptorSetLayout(
 
    const size_t layout_size =
       offsetof(struct vn_descriptor_set_layout, bindings[last_binding + 1]);
+   /* allocated with the device scope */
    struct vn_descriptor_set_layout *layout =
       vk_zalloc(alloc, layout_size, VN_DEFAULT_ALIGN,
-                VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+                VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
    if (!layout) {
       vk_free(alloc, local_bindings);
       return vn_error(dev->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
@@ -189,17 +207,11 @@ vn_DestroyDescriptorSetLayout(VkDevice device,
    struct vn_device *dev = vn_device_from_handle(device);
    struct vn_descriptor_set_layout *layout =
       vn_descriptor_set_layout_from_handle(descriptorSetLayout);
-   const VkAllocationCallbacks *alloc =
-      pAllocator ? pAllocator : &dev->base.base.alloc;
 
    if (!layout)
       return;
 
-   vn_async_vkDestroyDescriptorSetLayout(dev->instance, device,
-                                         descriptorSetLayout, NULL);
-
-   vn_object_base_fini(&layout->base);
-   vk_free(alloc, layout);
+   vn_descriptor_set_layout_unref(dev, layout);
 }
 
 /* descriptor pool commands */
index 5f6afc4..4f4ea5a 100644 (file)
@@ -28,6 +28,8 @@ struct vn_descriptor_set_layout_binding {
 struct vn_descriptor_set_layout {
    struct vn_object_base base;
 
+   struct vn_refcount refcount;
+
    uint32_t last_binding;
    bool has_variable_descriptor_count;
 
@@ -98,4 +100,24 @@ VK_DEFINE_NONDISP_HANDLE_CASTS(vn_descriptor_update_template,
                                VkDescriptorUpdateTemplate,
                                VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE)
 
+void
+vn_descriptor_set_layout_destroy(struct vn_device *dev,
+                                 struct vn_descriptor_set_layout *layout);
+
+static inline struct vn_descriptor_set_layout *
+vn_descriptor_set_layout_ref(struct vn_device *dev,
+                             struct vn_descriptor_set_layout *layout)
+{
+   vn_refcount_inc(&layout->refcount);
+   return layout;
+}
+
+static inline void
+vn_descriptor_set_layout_unref(struct vn_device *dev,
+                               struct vn_descriptor_set_layout *layout)
+{
+   if (vn_refcount_dec(&layout->refcount))
+      vn_descriptor_set_layout_destroy(dev, layout);
+}
+
 #endif /* VN_DESCRIPTOR_SET_H */