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