zink_batch.c 3.5 KB
Newer Older
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
1
#include "zink_batch.h"
2 3 4

#include "zink_context.h"
#include "zink_fence.h"
5
#include "zink_framebuffer.h"
6
#include "zink_query.h"
7
#include "zink_render_pass.h"
8
#include "zink_resource.h"
9 10 11
#include "zink_screen.h"

#include "util/u_debug.h"
12
#include "util/set.h"
13

14
static void
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
15
reset_batch(struct zink_screen *screen, struct zink_batch *batch)
16
{
17 18
   batch->descs_left = ZINK_BATCH_DESC_SIZE;

19
   // cmdbuf hasn't been submitted before
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
20
   if (!batch->fence)
21 22
      return;

Erik Faye-Lund 's avatar
Erik Faye-Lund committed
23 24
   zink_fence_finish(screen, batch->fence, PIPE_TIMEOUT_INFINITE);
   zink_fence_reference(screen, &batch->fence, NULL);
25

Erik Faye-Lund 's avatar
Erik Faye-Lund committed
26 27
   zink_render_pass_reference(screen, &batch->rp, NULL);
   zink_framebuffer_reference(screen, &batch->fb, NULL);
28 29

   /* unref all used resources */
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
30
   set_foreach(batch->resources, entry) {
31 32 33
      struct pipe_resource *pres = (struct pipe_resource *)entry->key;
      pipe_resource_reference(&pres, NULL);
   }
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
34
   _mesa_set_clear(batch->resources, NULL);
35

36 37 38 39 40 41 42
   /* unref all used sampler-views */
   set_foreach(batch->sampler_views, entry) {
      struct pipe_sampler_view *pres = (struct pipe_sampler_view *)entry->key;
      pipe_sampler_view_reference(&pres, NULL);
   }
   _mesa_set_clear(batch->sampler_views, NULL);

Erik Faye-Lund 's avatar
Erik Faye-Lund committed
43
   util_dynarray_foreach(&batch->zombie_samplers, VkSampler, samp) {
44 45
      vkDestroySampler(screen->dev, *samp, NULL);
   }
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
46
   util_dynarray_clear(&batch->zombie_samplers);
47 48 49

   if (vkResetDescriptorPool(screen->dev, batch->descpool, 0) != VK_SUCCESS)
      fprintf(stderr, "vkResetDescriptorPool failed\n");
50 51
}

Erik Faye-Lund 's avatar
Erik Faye-Lund committed
52
void
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
53
zink_start_batch(struct zink_context *ctx, struct zink_batch *batch)
54
{
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
55
   reset_batch(zink_screen(ctx->base.screen), batch);
56 57 58 59

   VkCommandBufferBeginInfo cbbi = {};
   cbbi.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
   cbbi.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
60
   if (vkBeginCommandBuffer(batch->cmdbuf, &cbbi) != VK_SUCCESS)
61
      debug_printf("vkBeginCommandBuffer failed\n");
62 63 64

   if (!ctx->queries_disabled)
      zink_resume_queries(ctx, batch);
65 66
}

67 68
void
zink_end_batch(struct zink_context *ctx, struct zink_batch *batch)
69
{
70 71 72
   if (!ctx->queries_disabled)
      zink_suspend_queries(ctx, batch);

73 74 75 76 77 78 79 80 81 82
   if (vkEndCommandBuffer(batch->cmdbuf) != VK_SUCCESS) {
      debug_printf("vkEndCommandBuffer failed\n");
      return;
   }

   assert(batch->fence == NULL);
   batch->fence = zink_create_fence(ctx->base.screen);
   if (!batch->fence)
      return;

83 84 85 86 87 88
   VkSubmitInfo si = {};
   si.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
   si.waitSemaphoreCount = 0;
   si.pWaitSemaphores = NULL;
   si.signalSemaphoreCount = 0;
   si.pSignalSemaphores = NULL;
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
89
   si.pWaitDstStageMask = NULL;
90
   si.commandBufferCount = 1;
91
   si.pCommandBuffers = &batch->cmdbuf;
92

93
   if (vkQueueSubmit(ctx->queue, 1, &si, batch->fence->fence) != VK_SUCCESS) {
94
      debug_printf("vkQueueSubmit failed\n");
95 96
      abort();
   }
97
}
98 99

void
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
100 101
zink_batch_reference_resoure(struct zink_batch *batch,
                             struct zink_resource *res)
102
{
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
103
   struct set_entry *entry = _mesa_set_search(batch->resources, res);
104 105
   if (!entry) {
      struct pipe_resource *tmp = NULL;
Erik Faye-Lund 's avatar
Erik Faye-Lund committed
106
      entry = _mesa_set_add(batch->resources, res);
107 108 109
      pipe_resource_reference(&tmp, &res->base);
   }
}
110 111 112 113 114 115 116 117 118 119 120 121

void
zink_batch_reference_sampler_view(struct zink_batch *batch,
                                  struct zink_sampler_view *sv)
{
   struct set_entry *entry = _mesa_set_search(batch->sampler_views, sv);
   if (!entry) {
      struct pipe_sampler_view *tmp = NULL;
      entry = _mesa_set_add(batch->sampler_views, sv);
      pipe_sampler_view_reference(&tmp, &sv->base);
   }
}