474421081bfa87956146c4aa788d1e5b6ec2960a
[mesa.git] / src / gallium / drivers / zink / zink_batch.c
1 #include "zink_batch.h"
2
3 #include "zink_context.h"
4 #include "zink_fence.h"
5 #include "zink_framebuffer.h"
6 #include "zink_render_pass.h"
7 #include "zink_resource.h"
8 #include "zink_screen.h"
9
10 #include "util/u_debug.h"
11 #include "util/set.h"
12
13 static void
14 reset_batch(struct zink_screen *screen, struct zink_batch *batch)
15 {
16 batch->descs_left = ZINK_BATCH_DESC_SIZE;
17
18 // cmdbuf hasn't been submitted before
19 if (!batch->fence)
20 return;
21
22 zink_fence_finish(screen, batch->fence, PIPE_TIMEOUT_INFINITE);
23 zink_fence_reference(screen, &batch->fence, NULL);
24
25 zink_render_pass_reference(screen, &batch->rp, NULL);
26 zink_framebuffer_reference(screen, &batch->fb, NULL);
27
28 /* unref all used resources */
29 set_foreach(batch->resources, entry) {
30 struct pipe_resource *pres = (struct pipe_resource *)entry->key;
31 pipe_resource_reference(&pres, NULL);
32 }
33 _mesa_set_clear(batch->resources, NULL);
34
35 /* unref all used sampler-views */
36 set_foreach(batch->sampler_views, entry) {
37 struct pipe_sampler_view *pres = (struct pipe_sampler_view *)entry->key;
38 pipe_sampler_view_reference(&pres, NULL);
39 }
40 _mesa_set_clear(batch->sampler_views, NULL);
41
42 util_dynarray_foreach(&batch->zombie_samplers, VkSampler, samp) {
43 vkDestroySampler(screen->dev, *samp, NULL);
44 }
45 util_dynarray_clear(&batch->zombie_samplers);
46
47 if (vkResetDescriptorPool(screen->dev, batch->descpool, 0) != VK_SUCCESS)
48 fprintf(stderr, "vkResetDescriptorPool failed\n");
49 }
50
51 void
52 zink_start_batch(struct zink_context *ctx, struct zink_batch *batch)
53 {
54 reset_batch(zink_screen(ctx->base.screen), batch);
55
56 VkCommandBufferBeginInfo cbbi = {};
57 cbbi.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
58 cbbi.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
59 if (vkBeginCommandBuffer(batch->cmdbuf, &cbbi) != VK_SUCCESS)
60 debug_printf("vkBeginCommandBuffer failed\n");
61 }
62
63 static bool
64 submit_cmdbuf(struct zink_context *ctx, VkCommandBuffer cmdbuf, VkFence fence)
65 {
66 VkPipelineStageFlags wait = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
67
68 VkSubmitInfo si = {};
69 si.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
70 si.waitSemaphoreCount = 0;
71 si.pWaitSemaphores = NULL;
72 si.signalSemaphoreCount = 0;
73 si.pSignalSemaphores = NULL;
74 si.pWaitDstStageMask = &wait;
75 si.commandBufferCount = 1;
76 si.pCommandBuffers = &cmdbuf;
77
78 if (vkQueueSubmit(ctx->queue, 1, &si, fence) != VK_SUCCESS) {
79 debug_printf("vkQueueSubmit failed\n");
80 return false;
81 }
82
83 return true;
84 }
85
86 void
87 zink_end_batch(struct zink_context *ctx, struct zink_batch *batch)
88 {
89 if (vkEndCommandBuffer(batch->cmdbuf) != VK_SUCCESS) {
90 debug_printf("vkEndCommandBuffer failed\n");
91 return;
92 }
93
94 assert(batch->fence == NULL);
95 batch->fence = zink_create_fence(ctx->base.screen);
96 if (!batch->fence ||
97 !submit_cmdbuf(ctx, batch->cmdbuf, batch->fence->fence))
98 return;
99 }
100
101 void
102 zink_batch_reference_resoure(struct zink_batch *batch,
103 struct zink_resource *res)
104 {
105 struct set_entry *entry = _mesa_set_search(batch->resources, res);
106 if (!entry) {
107 struct pipe_resource *tmp = NULL;
108 entry = _mesa_set_add(batch->resources, res);
109 pipe_resource_reference(&tmp, &res->base);
110 }
111 }
112
113 void
114 zink_batch_reference_sampler_view(struct zink_batch *batch,
115 struct zink_sampler_view *sv)
116 {
117 struct set_entry *entry = _mesa_set_search(batch->sampler_views, sv);
118 if (!entry) {
119 struct pipe_sampler_view *tmp = NULL;
120 entry = _mesa_set_add(batch->sampler_views, sv);
121 pipe_sampler_view_reference(&tmp, &sv->base);
122 }
123 }