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