lavapipe: Reference count pipeline layouts
authorJason Ekstrand <jason.ekstrand@collabora.com>
Thu, 10 Mar 2022 16:56:30 +0000 (10:56 -0600)
committerMarge Bot <emma+marge@anholt.net>
Thu, 10 Mar 2022 21:08:36 +0000 (21:08 +0000)
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Reviewed-by: Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15329>

src/gallium/frontends/lavapipe/lvp_descriptor_set.c
src/gallium/frontends/lavapipe/lvp_private.h

index 9e97c74..a3c93de 100644 (file)
@@ -214,13 +214,14 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_CreatePipelineLayout(
 
    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
 
-   layout = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*layout), 8,
-                       VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+   layout = vk_alloc(&device->vk.alloc, sizeof(*layout), 8,
+                     VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
    if (layout == NULL)
       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
 
    vk_object_base_init(&device->vk, &layout->base,
                        VK_OBJECT_TYPE_PIPELINE_LAYOUT);
+   layout->ref_cnt = 1;
    layout->num_sets = pCreateInfo->setLayoutCount;
 
    for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
@@ -242,6 +243,18 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_CreatePipelineLayout(
    return VK_SUCCESS;
 }
 
+void lvp_pipeline_layout_destroy(struct lvp_device *device,
+                                 struct lvp_pipeline_layout *pipeline_layout)
+{
+   assert(pipeline_layout->ref_cnt == 0);
+
+   for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
+      lvp_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
+
+   vk_object_base_finish(&pipeline_layout->base);
+   vk_free(&device->vk.alloc, pipeline_layout);
+}
+
 VKAPI_ATTR void VKAPI_CALL lvp_DestroyPipelineLayout(
     VkDevice                                    _device,
     VkPipelineLayout                            _pipelineLayout,
@@ -252,11 +265,8 @@ VKAPI_ATTR void VKAPI_CALL lvp_DestroyPipelineLayout(
 
    if (!_pipelineLayout)
      return;
-   for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
-      lvp_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
 
-   vk_object_base_finish(&pipeline_layout->base);
-   vk_free2(&device->vk.alloc, pAllocator, pipeline_layout);
+   lvp_pipeline_layout_unref(device, pipeline_layout);
 }
 
 VkResult
index f1e1ee1..0789922 100644 (file)
@@ -443,6 +443,10 @@ lvp_descriptor_set_destroy(struct lvp_device *device,
 
 struct lvp_pipeline_layout {
    struct vk_object_base base;
+
+   /* Pipeline layouts can be destroyed at almost any time */
+   uint32_t ref_cnt;
+
    struct {
       struct lvp_descriptor_set_layout *layout;
    } set[MAX_SETS];
@@ -454,6 +458,25 @@ struct lvp_pipeline_layout {
    } stage[MESA_SHADER_STAGES];
 };
 
+void lvp_pipeline_layout_destroy(struct lvp_device *device,
+                                 struct lvp_pipeline_layout *layout);
+
+static inline void
+lvp_pipeline_layout_ref(struct lvp_pipeline_layout *layout)
+{
+   assert(layout && layout->ref_cnt >= 1);
+   p_atomic_inc(&layout->ref_cnt);
+}
+
+static inline void
+lvp_pipeline_layout_unref(struct lvp_device *device,
+                          struct lvp_pipeline_layout *layout)
+{
+   assert(layout && layout->ref_cnt >= 1);
+   if (p_atomic_dec_zero(&layout->ref_cnt))
+      lvp_pipeline_layout_destroy(device, layout);
+}
+
 struct lvp_access_info {
    uint32_t images_read;
    uint32_t images_written;