vulkan/meta: Add an object tracking list
authorFaith Ekstrand <faith.ekstrand@collabora.com>
Tue, 31 Jan 2023 02:11:52 +0000 (20:11 -0600)
committerMarge Bot <emma+marge@anholt.net>
Fri, 4 Aug 2023 21:31:57 +0000 (21:31 +0000)
This will let us create objects which get held onto by the command
buffer until it's reset or destroyed.

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

src/vulkan/runtime/vk_command_buffer.c
src/vulkan/runtime/vk_command_buffer.h
src/vulkan/runtime/vk_meta.c
src/vulkan/runtime/vk_meta.h

index 194d389..9b90717 100644 (file)
@@ -44,6 +44,7 @@ vk_command_buffer_init(struct vk_command_pool *pool,
    command_buffer->state = MESA_VK_COMMAND_BUFFER_STATE_INITIAL;
    command_buffer->record_result = VK_SUCCESS;
    vk_cmd_queue_init(&command_buffer->cmd_queue, &pool->alloc);
+   vk_meta_object_list_init(&command_buffer->meta_objects);
    util_dynarray_init(&command_buffer->labels, NULL);
    command_buffer->region_begin = true;
 
@@ -60,6 +61,8 @@ vk_command_buffer_reset(struct vk_command_buffer *command_buffer)
    command_buffer->record_result = VK_SUCCESS;
    vk_command_buffer_reset_render_pass(command_buffer);
    vk_cmd_queue_reset(&command_buffer->cmd_queue);
+   vk_meta_object_list_reset(command_buffer->base.device,
+                             &command_buffer->meta_objects);
    util_dynarray_clear(&command_buffer->labels);
    command_buffer->region_begin = true;
 }
@@ -95,6 +98,8 @@ vk_command_buffer_finish(struct vk_command_buffer *command_buffer)
    vk_command_buffer_reset_render_pass(command_buffer);
    vk_cmd_queue_finish(&command_buffer->cmd_queue);
    util_dynarray_fini(&command_buffer->labels);
+   vk_meta_object_list_finish(command_buffer->base.device,
+                              &command_buffer->meta_objects);
    vk_object_base_finish(&command_buffer->base);
 }
 
index a9cc031..1e22b12 100644 (file)
@@ -27,6 +27,7 @@
 #include "vk_cmd_queue.h"
 #include "vk_graphics_state.h"
 #include "vk_log.h"
+#include "vk_meta.h"
 #include "vk_object.h"
 #include "util/list.h"
 #include "util/u_dynarray.h"
@@ -120,6 +121,9 @@ struct vk_command_buffer {
    /** Command list for emulated secondary command buffers */
    struct vk_cmd_queue cmd_queue;
 
+   /** Object list for meta objects */
+   struct vk_meta_object_list meta_objects;
+
    /**
     * VK_EXT_debug_utils
     *
index 34d5e06..a4752ac 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "vk_meta.h"
 
+#include "vk_command_buffer.h"
 #include "vk_device.h"
 #include "vk_util.h"
 
@@ -338,3 +339,67 @@ vk_meta_create_compute_pipeline(struct vk_device *device,
                                                     (uint64_t)pipeline);
    return VK_SUCCESS;
 }
+
+void
+vk_meta_object_list_init(struct vk_meta_object_list *mol)
+{
+   util_dynarray_init(&mol->arr, NULL);
+}
+
+void
+vk_meta_object_list_reset(struct vk_device *device,
+                          struct vk_meta_object_list *mol)
+{
+   util_dynarray_foreach(&mol->arr, struct vk_object_base *, obj)
+      destroy_object(device, *obj);
+
+   util_dynarray_clear(&mol->arr);
+}
+
+void
+vk_meta_object_list_finish(struct vk_device *device,
+                           struct vk_meta_object_list *mol)
+{
+   vk_meta_object_list_reset(device, mol);
+   util_dynarray_fini(&mol->arr);
+}
+
+VkResult
+vk_meta_create_buffer(struct vk_command_buffer *cmd,
+                      struct vk_meta_device *meta,
+                      const VkBufferCreateInfo *info,
+                      VkBuffer *buffer_out)
+{
+   struct vk_device *device = cmd->base.device;
+   const struct vk_device_dispatch_table *disp = &device->dispatch_table;
+   VkDevice _device = vk_device_to_handle(device);
+
+   VkResult result = disp->CreateBuffer(_device, info, NULL, buffer_out);
+   if (unlikely(result != VK_SUCCESS))
+      return result;
+
+   vk_meta_object_list_add_handle(&cmd->meta_objects,
+                                  VK_OBJECT_TYPE_BUFFER,
+                                  (uint64_t)*buffer_out);
+   return VK_SUCCESS;
+}
+
+VkResult
+vk_meta_create_image_view(struct vk_command_buffer *cmd,
+                          struct vk_meta_device *meta,
+                          const VkImageViewCreateInfo *info,
+                          VkImageView *image_view_out)
+{
+   struct vk_device *device = cmd->base.device;
+   const struct vk_device_dispatch_table *disp = &device->dispatch_table;
+   VkDevice _device = vk_device_to_handle(device);
+
+   VkResult result = disp->CreateImageView(_device, info, NULL, image_view_out);
+   if (unlikely(result != VK_SUCCESS))
+      return result;
+
+   vk_meta_object_list_add_handle(&cmd->meta_objects,
+                                  VK_OBJECT_TYPE_IMAGE_VIEW,
+                                  (uint64_t)*image_view_out);
+   return VK_SUCCESS;
+}
index bc7bf4a..fe66563 100644 (file)
 #ifndef VK_META_H
 #define VK_META_H
 
-#include <vulkan/vulkan_core.h>
-
 #include "vk_limits.h"
+#include "vk_object.h"
 
 #include "util/simple_mtx.h"
+#include "util/u_dynarray.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -121,6 +121,41 @@ vk_meta_create_compute_pipeline(struct vk_device *device,
                                 const void *key_data, size_t key_size,
                                 VkPipeline *pipeline_out);
 
+struct vk_meta_object_list {
+   struct util_dynarray arr;
+};
+
+void vk_meta_object_list_init(struct vk_meta_object_list *mol);
+void vk_meta_object_list_reset(struct vk_device *device,
+                               struct vk_meta_object_list *mol);
+void vk_meta_object_list_finish(struct vk_device *device,
+                                struct vk_meta_object_list *mol);
+
+static inline void
+vk_meta_object_list_add_obj(struct vk_meta_object_list *mol,
+                            struct vk_object_base *obj)
+{
+   util_dynarray_append(&mol->arr, struct vk_object_base *, obj);
+}
+
+static inline void
+vk_meta_object_list_add_handle(struct vk_meta_object_list *mol,
+                               VkObjectType obj_type,
+                               uint64_t handle)
+{
+   vk_meta_object_list_add_obj(mol,
+      vk_object_base_from_u64_handle(handle, obj_type));
+}
+
+VkResult vk_meta_create_buffer(struct vk_command_buffer *cmd,
+                               struct vk_meta_device *meta,
+                               const VkBufferCreateInfo *info,
+                               VkBuffer *buffer_out);
+VkResult vk_meta_create_image_view(struct vk_command_buffer *cmd,
+                                   struct vk_meta_device *meta,
+                                   const VkImageViewCreateInfo *info,
+                                   VkImageView *image_view_out);
+
 #ifdef __cplusplus
 }
 #endif