nir: Use a single list for all shader variables
[mesa.git] / src / gallium / drivers / zink / zink_fence.c
1 /*
2 * Copyright 2018 Collabora Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "zink_batch.h"
25 #include "zink_fence.h"
26
27 #include "zink_query.h"
28 #include "zink_screen.h"
29
30 #include "util/u_memory.h"
31
32 static void
33 destroy_fence(struct zink_screen *screen, struct zink_fence *fence)
34 {
35 if (fence->fence)
36 vkDestroyFence(screen->dev, fence->fence, NULL);
37 FREE(fence);
38 }
39
40 struct zink_fence *
41 zink_create_fence(struct pipe_screen *pscreen, struct zink_batch *batch)
42 {
43 struct zink_screen *screen = zink_screen(pscreen);
44
45 VkFenceCreateInfo fci = {};
46 fci.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
47
48 struct zink_fence *ret = CALLOC_STRUCT(zink_fence);
49 if (!ret) {
50 debug_printf("CALLOC_STRUCT failed\n");
51 return NULL;
52 }
53
54 if (vkCreateFence(screen->dev, &fci, NULL, &ret->fence) != VK_SUCCESS) {
55 debug_printf("vkCreateFence failed\n");
56 goto fail;
57 }
58 ret->active_queries = batch->active_queries;
59 batch->active_queries = NULL;
60
61 pipe_reference_init(&ret->reference, 1);
62 return ret;
63
64 fail:
65 destroy_fence(screen, ret);
66 return NULL;
67 }
68
69 void
70 zink_fence_reference(struct zink_screen *screen,
71 struct zink_fence **ptr,
72 struct zink_fence *fence)
73 {
74 if (pipe_reference(&(*ptr)->reference, &fence->reference))
75 destroy_fence(screen, *ptr);
76
77 *ptr = fence;
78 }
79
80 static void
81 fence_reference(struct pipe_screen *pscreen,
82 struct pipe_fence_handle **pptr,
83 struct pipe_fence_handle *pfence)
84 {
85 zink_fence_reference(zink_screen(pscreen), (struct zink_fence **)pptr,
86 zink_fence(pfence));
87 }
88
89 bool
90 zink_fence_finish(struct zink_screen *screen, struct zink_fence *fence,
91 uint64_t timeout_ns)
92 {
93 bool success = vkWaitForFences(screen->dev, 1, &fence->fence, VK_TRUE,
94 timeout_ns) == VK_SUCCESS;
95 if (success && fence->active_queries)
96 zink_prune_queries(screen, fence);
97 return success;
98 }
99
100 static bool
101 fence_finish(struct pipe_screen *pscreen, struct pipe_context *pctx,
102 struct pipe_fence_handle *pfence, uint64_t timeout_ns)
103 {
104 return zink_fence_finish(zink_screen(pscreen), zink_fence(pfence),
105 timeout_ns);
106 }
107
108 void
109 zink_screen_fence_init(struct pipe_screen *pscreen)
110 {
111 pscreen->fence_reference = fence_reference;
112 pscreen->fence_finish = fence_finish;
113 }