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