zink: reference vertex and index buffers
authorErik Faye-Lund <erik.faye-lund@collabora.com>
Tue, 26 Mar 2019 18:35:13 +0000 (19:35 +0100)
committerErik Faye-Lund <erik.faye-lund@collabora.com>
Mon, 28 Oct 2019 08:51:44 +0000 (08:51 +0000)
Acked-by: Jordan Justen <jordan.l.justen@intel.com>
src/gallium/drivers/zink/zink_cmdbuf.c
src/gallium/drivers/zink/zink_cmdbuf.h
src/gallium/drivers/zink/zink_context.c

index aab898f84b1ac85d4980c35968d3c0af43f3a2cd..44c4d941b8de82da83dd044a4dcc9fc1653d157f 100644 (file)
@@ -4,9 +4,11 @@
 #include "zink_fence.h"
 #include "zink_framebuffer.h"
 #include "zink_render_pass.h"
+#include "zink_resource.h"
 #include "zink_screen.h"
 
 #include "util/u_debug.h"
+#include "util/set.h"
 
 static void
 reset_cmdbuf(struct zink_screen *screen, struct zink_cmdbuf *cmdbuf)
@@ -20,6 +22,13 @@ reset_cmdbuf(struct zink_screen *screen, struct zink_cmdbuf *cmdbuf)
 
    zink_render_pass_reference(screen, &cmdbuf->rp, NULL);
    zink_framebuffer_reference(screen, &cmdbuf->fb, NULL);
+
+   /* unref all used resources */
+   set_foreach(cmdbuf->resources, entry) {
+      struct pipe_resource *pres = (struct pipe_resource *)entry->key;
+      pipe_resource_reference(&pres, NULL);
+   }
+   _mesa_set_clear(cmdbuf->resources, NULL);
 }
 
 struct zink_cmdbuf *
@@ -79,3 +88,15 @@ zink_end_cmdbuf(struct zink_context *ctx, struct zink_cmdbuf *cmdbuf)
    if (vkQueueWaitIdle(ctx->queue) != VK_SUCCESS)
       debug_printf("vkQueueWaitIdle failed\n");
 }
+
+void
+zink_cmdbuf_reference_resoure(struct zink_cmdbuf *cmdbuf,
+                              struct zink_resource *res)
+{
+   struct set_entry *entry = _mesa_set_search(cmdbuf->resources, res);
+   if (!entry) {
+      struct pipe_resource *tmp = NULL;
+      entry = _mesa_set_add(cmdbuf->resources, res);
+      pipe_resource_reference(&tmp, &res->base);
+   }
+}
index ff5f4729760219a2f391a7c9f2d9500e478e1aba..f0159d91120c58b768e81d988ac1ebf96c26787e 100644 (file)
@@ -30,6 +30,7 @@ struct zink_context;
 struct zink_fence;
 struct zink_framebuffer;
 struct zink_render_pass;
+struct zink_resource;
 
 struct zink_cmdbuf {
    VkCommandBuffer cmdbuf;
@@ -37,6 +38,8 @@ struct zink_cmdbuf {
 
    struct zink_render_pass *rp;
    struct zink_framebuffer *fb;
+
+   struct set *resources;
 };
 
 struct zink_cmdbuf *
@@ -45,4 +48,8 @@ zink_start_cmdbuf(struct zink_context *ctx);
 void
 zink_end_cmdbuf(struct zink_context *ctx, struct zink_cmdbuf *cmdbuf);
 
+void
+zink_cmdbuf_reference_resoure(struct zink_cmdbuf *cmdbuf,
+                              struct zink_resource *res);
+
 #endif
index b364a12b5384832b59bb2a4b574b30980cd173bf..f87d66fd4fbe3c87e31b39780d7b4cb2f4745b5c 100644 (file)
@@ -743,7 +743,7 @@ zink_primitive_topology(enum pipe_prim_type mode)
 }
 
 static void
-zink_bind_vertex_buffers(VkCommandBuffer cmdbuf, struct zink_context *ctx)
+zink_bind_vertex_buffers(struct zink_cmdbuf *cmdbuf, struct zink_context *ctx)
 {
    VkBuffer buffers[PIPE_MAX_ATTRIBS];
    VkDeviceSize buffer_offsets[PIPE_MAX_ATTRIBS];
@@ -754,10 +754,11 @@ zink_bind_vertex_buffers(VkCommandBuffer cmdbuf, struct zink_context *ctx)
       struct zink_resource *res = zink_resource(vb->buffer.resource);
       buffers[i] = res->buffer;
       buffer_offsets[i] = vb->buffer_offset;
+      zink_cmdbuf_reference_resoure(cmdbuf, res);
    }
 
    if (elems->num_bindings > 0)
-      vkCmdBindVertexBuffers(cmdbuf, 0, elems->num_bindings, buffers, buffer_offsets);
+      vkCmdBindVertexBuffers(cmdbuf->cmdbuf, 0, elems->num_bindings, buffers, buffer_offsets);
 }
 
 static void
@@ -955,12 +956,14 @@ zink_draw_vbo(struct pipe_context *pctx,
    vkCmdBindPipeline(cmdbuf->cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
    vkCmdBindDescriptorSets(cmdbuf->cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS,
                            gfx_program->layout, 0, 1, &desc_set, 0, NULL);
-   zink_bind_vertex_buffers(cmdbuf->cmdbuf, ctx);
+   zink_bind_vertex_buffers(cmdbuf, ctx);
 
    if (dinfo->index_size > 0) {
       assert(dinfo->index_size != 1);
       VkIndexType index_type = dinfo->index_size == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32;
-      vkCmdBindIndexBuffer(cmdbuf->cmdbuf, zink_resource(index_buffer)->buffer, index_offset, index_type);
+      struct zink_resource *res = zink_resource(index_buffer);
+      vkCmdBindIndexBuffer(cmdbuf->cmdbuf, res->buffer, index_offset, index_type);
+      zink_cmdbuf_reference_resoure(cmdbuf, res);
       vkCmdDrawIndexed(cmdbuf->cmdbuf,
          dinfo->count, dinfo->instance_count,
          dinfo->start, dinfo->index_bias, dinfo->start_instance);
@@ -1247,10 +1250,16 @@ zink_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
    cbai.commandPool = ctx->cmdpool;
    cbai.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
    cbai.commandBufferCount = 1;
-   for (int i = 0; i < ARRAY_SIZE(ctx->cmdbufs); ++i)
+   for (int i = 0; i < ARRAY_SIZE(ctx->cmdbufs); ++i) {
       if (vkAllocateCommandBuffers(screen->dev, &cbai, &ctx->cmdbufs[i].cmdbuf) != VK_SUCCESS)
          goto fail;
 
+      ctx->cmdbufs[i].resources = _mesa_set_create(NULL, _mesa_hash_pointer,
+                                                   _mesa_key_pointer_equal);
+      if (!ctx->cmdbufs[i].resources)
+         goto fail;
+   }
+
    VkDescriptorPoolSize sizes[] = {
       {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000}
    };