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_program.h"
8 #include "zink_render_pass.h"
9 #include "zink_resource.h"
10 #include "zink_screen.h"
11
12 #include "util/hash_table.h"
13 #include "util/u_debug.h"
14 #include "util/set.h"
15
16 static void
reset_batch(struct zink_context * ctx,struct zink_batch * batch)17 reset_batch(struct zink_context *ctx, struct zink_batch *batch)
18 {
19 struct zink_screen *screen = zink_screen(ctx->base.screen);
20 batch->descs_left = ZINK_BATCH_DESC_SIZE;
21
22 // cmdbuf hasn't been submitted before
23 if (!batch->fence)
24 return;
25
26 zink_fence_finish(screen, batch->fence, PIPE_TIMEOUT_INFINITE);
27 zink_fence_reference(screen, &batch->fence, NULL);
28
29 zink_render_pass_reference(screen, &batch->rp, NULL);
30 zink_framebuffer_reference(screen, &batch->fb, NULL);
31 set_foreach(batch->programs, entry) {
32 struct zink_gfx_program *prog = (struct zink_gfx_program*)entry->key;
33 zink_gfx_program_reference(screen, &prog, NULL);
34 }
35 _mesa_set_clear(batch->programs, NULL);
36
37 /* unref all used resources */
38 set_foreach(batch->resources, entry) {
39 struct pipe_resource *pres = (struct pipe_resource *)entry->key;
40 pipe_resource_reference(&pres, NULL);
41 }
42 _mesa_set_clear(batch->resources, NULL);
43
44 /* unref all used sampler-views */
45 set_foreach(batch->sampler_views, entry) {
46 struct pipe_sampler_view *pres = (struct pipe_sampler_view *)entry->key;
47 pipe_sampler_view_reference(&pres, NULL);
48 }
49 _mesa_set_clear(batch->sampler_views, NULL);
50
51 util_dynarray_foreach(&batch->zombie_samplers, VkSampler, samp) {
52 vkDestroySampler(screen->dev, *samp, NULL);
53 }
54 util_dynarray_clear(&batch->zombie_samplers);
55
56 if (vkResetDescriptorPool(screen->dev, batch->descpool, 0) != VK_SUCCESS)
57 fprintf(stderr, "vkResetDescriptorPool failed\n");
58 }
59
60 void
zink_start_batch(struct zink_context * ctx,struct zink_batch * batch)61 zink_start_batch(struct zink_context *ctx, struct zink_batch *batch)
62 {
63 reset_batch(ctx, batch);
64
65 VkCommandBufferBeginInfo cbbi = {};
66 cbbi.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
67 cbbi.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
68 if (vkBeginCommandBuffer(batch->cmdbuf, &cbbi) != VK_SUCCESS)
69 debug_printf("vkBeginCommandBuffer failed\n");
70
71 if (!ctx->queries_disabled)
72 zink_resume_queries(ctx, batch);
73 }
74
75 void
zink_end_batch(struct zink_context * ctx,struct zink_batch * batch)76 zink_end_batch(struct zink_context *ctx, struct zink_batch *batch)
77 {
78 if (!ctx->queries_disabled)
79 zink_suspend_queries(ctx, batch);
80
81 if (vkEndCommandBuffer(batch->cmdbuf) != VK_SUCCESS) {
82 debug_printf("vkEndCommandBuffer failed\n");
83 return;
84 }
85
86 assert(batch->fence == NULL);
87 batch->fence = zink_create_fence(ctx->base.screen, batch);
88 if (!batch->fence)
89 return;
90
91 VkSubmitInfo si = {};
92 si.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
93 si.waitSemaphoreCount = 0;
94 si.pWaitSemaphores = NULL;
95 si.signalSemaphoreCount = 0;
96 si.pSignalSemaphores = NULL;
97 si.pWaitDstStageMask = NULL;
98 si.commandBufferCount = 1;
99 si.pCommandBuffers = &batch->cmdbuf;
100
101 if (vkQueueSubmit(ctx->queue, 1, &si, batch->fence->fence) != VK_SUCCESS) {
102 debug_printf("ZINK: vkQueueSubmit() failed\n");
103 ctx->is_device_lost = true;
104
105 if (ctx->reset.reset) {
106 ctx->reset.reset(ctx->reset.data, PIPE_GUILTY_CONTEXT_RESET);
107 }
108 }
109 }
110
111 void
zink_batch_reference_resource_rw(struct zink_batch * batch,struct zink_resource * res,bool write)112 zink_batch_reference_resource_rw(struct zink_batch *batch, struct zink_resource *res, bool write)
113 {
114 unsigned mask = write ? ZINK_RESOURCE_ACCESS_WRITE : ZINK_RESOURCE_ACCESS_READ;
115
116 /* u_transfer_helper unrefs the stencil buffer when the depth buffer is unrefed,
117 * so we add an extra ref here to the stencil buffer to compensate
118 */
119 struct zink_resource *stencil;
120
121 zink_get_depth_stencil_resources((struct pipe_resource*)res, NULL, &stencil);
122
123
124 struct set_entry *entry = _mesa_set_search(batch->resources, res);
125 if (!entry) {
126 entry = _mesa_set_add(batch->resources, res);
127 pipe_reference(NULL, &res->base.reference);
128 if (stencil)
129 pipe_reference(NULL, &stencil->base.reference);
130 }
131 /* the batch_uses value for this batch is guaranteed to not be in use now because
132 * reset_batch() waits on the fence and removes access before resetting
133 */
134 res->batch_uses[batch->batch_id] |= mask;
135
136 if (stencil)
137 stencil->batch_uses[batch->batch_id] |= mask;
138 }
139
140 void
zink_batch_reference_sampler_view(struct zink_batch * batch,struct zink_sampler_view * sv)141 zink_batch_reference_sampler_view(struct zink_batch *batch,
142 struct zink_sampler_view *sv)
143 {
144 struct set_entry *entry = _mesa_set_search(batch->sampler_views, sv);
145 if (!entry) {
146 entry = _mesa_set_add(batch->sampler_views, sv);
147 pipe_reference(NULL, &sv->base.reference);
148 }
149 }
150
151 void
zink_batch_reference_program(struct zink_batch * batch,struct zink_gfx_program * prog)152 zink_batch_reference_program(struct zink_batch *batch,
153 struct zink_gfx_program *prog)
154 {
155 struct set_entry *entry = _mesa_set_search(batch->programs, prog);
156 if (!entry) {
157 entry = _mesa_set_add(batch->programs, prog);
158 pipe_reference(NULL, &prog->reference);
159 }
160 }
161