zink: delete view objects when unsetting resource usage in batch reset
authorMike Blumenkrantz <michael.blumenkrantz@gmail.com>
Wed, 12 Oct 2022 19:03:55 +0000 (15:03 -0400)
committerMarge Bot <emma+marge@anholt.net>
Thu, 13 Oct 2022 03:56:02 +0000 (03:56 +0000)
if the resource has no usage, it's guaranteed to be idle, which means view
objects can be pruned to avoid memory ballooning

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19056>

src/gallium/drivers/zink/zink_batch.c

index 5bc756a..39ce61c 100644 (file)
@@ -21,22 +21,31 @@ debug_describe_zink_batch_state(char *buf, const struct zink_batch_state *ptr)
 }
 
 static void
-reset_obj(struct zink_batch_state *bs, struct zink_resource_object *obj)
+reset_obj(struct zink_screen *screen, struct zink_batch_state *bs, struct zink_resource_object *obj)
 {
    if (!zink_resource_object_usage_unset(obj, bs)) {
       obj->unordered_read = false;
       obj->unordered_write = false;
       obj->access = 0;
       obj->access_stage = 0;
+      simple_mtx_lock(&obj->view_lock);
+      if (obj->is_buffer) {
+         while (util_dynarray_contains(&obj->views, VkBufferView))
+            VKSCR(DestroyBufferView)(screen->dev, util_dynarray_pop(&obj->views, VkBufferView), NULL);
+      } else {
+         while (util_dynarray_contains(&obj->views, VkImageView))
+            VKSCR(DestroyImageView)(screen->dev, util_dynarray_pop(&obj->views, VkImageView), NULL);
+      }
+      simple_mtx_unlock(&obj->view_lock);
    }
    util_dynarray_append(&bs->unref_resources, struct zink_resource_object*, obj);
 }
 
 static void
-reset_obj_list(struct zink_batch_state *bs, struct zink_batch_obj_list *list)
+reset_obj_list(struct zink_screen *screen, struct zink_batch_state *bs, struct zink_batch_obj_list *list)
 {
    for (unsigned i = 0; i < list->num_buffers; i++)
-      reset_obj(bs, list->objs[i]);
+      reset_obj(screen, bs, list->objs[i]);
    list->num_buffers = 0;
 }
 
@@ -50,12 +59,12 @@ zink_reset_batch_state(struct zink_context *ctx, struct zink_batch_state *bs)
       mesa_loge("ZINK: vkResetCommandPool failed (%s)", vk_Result_to_str(result));
 
    /* unref all used resources */
-   reset_obj_list(bs, &bs->real_objs);
-   reset_obj_list(bs, &bs->slab_objs);
-   reset_obj_list(bs, &bs->sparse_objs);
+   reset_obj_list(screen, bs, &bs->real_objs);
+   reset_obj_list(screen, bs, &bs->slab_objs);
+   reset_obj_list(screen, bs, &bs->sparse_objs);
    while (util_dynarray_contains(&bs->swapchain_obj, struct zink_resource_object*)) {
       struct zink_resource_object *obj = util_dynarray_pop(&bs->swapchain_obj, struct zink_resource_object*);
-      reset_obj(bs, obj);
+      reset_obj(screen, bs, obj);
    }
 
    for (unsigned i = 0; i < 2; i++) {