zink: emit blend-target index
[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_fence.h"
25
26 #include "zink_screen.h"
27
28 #include "util/u_memory.h"
29
30 static void
31 destroy_fence(struct zink_screen *screen, struct zink_fence *fence)
32 {
33 if (fence->fence)
34 vkDestroyFence(screen->dev, fence->fence, NULL);
35 FREE(fence);
36 }
37
38 struct zink_fence *
39 zink_create_fence(struct pipe_screen *pscreen)
40 {
41 struct zink_screen *screen = zink_screen(pscreen);
42
43 VkFenceCreateInfo fci = {};
44 fci.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
45
46 struct zink_fence *ret = CALLOC_STRUCT(zink_fence);
47 if (!ret) {
48 debug_printf("CALLOC_STRUCT failed\n");
49 return NULL;
50 }
51
52 if (vkCreateFence(screen->dev, &fci, NULL, &ret->fence) != VK_SUCCESS) {
53 debug_printf("vkCreateFence failed\n");
54 goto fail;
55 }
56
57 pipe_reference_init(&ret->reference, 1);
58 return ret;
59
60 fail:
61 destroy_fence(screen, ret);
62 return NULL;
63 }
64
65 void
66 zink_fence_reference(struct zink_screen *screen,
67 struct zink_fence **ptr,
68 struct zink_fence *fence)
69 {
70 if (pipe_reference(&(*ptr)->reference, &fence->reference))
71 destroy_fence(screen, *ptr);
72
73 *ptr = fence;
74 }
75
76 static void
77 fence_reference(struct pipe_screen *pscreen,
78 struct pipe_fence_handle **pptr,
79 struct pipe_fence_handle *pfence)
80 {
81 zink_fence_reference(zink_screen(pscreen), (struct zink_fence **)pptr,
82 zink_fence(pfence));
83 }
84
85 bool
86 zink_fence_finish(struct zink_screen *screen, struct zink_fence *fence,
87 uint64_t timeout_ns)
88 {
89 return vkWaitForFences(screen->dev, 1, &fence->fence, VK_TRUE,
90 timeout_ns) == VK_SUCCESS;
91 }
92
93 static bool
94 fence_finish(struct pipe_screen *pscreen, struct pipe_context *pctx,
95 struct pipe_fence_handle *pfence, uint64_t timeout_ns)
96 {
97 return zink_fence_finish(zink_screen(pscreen), zink_fence(pfence),
98 timeout_ns);
99 }
100
101 void
102 zink_screen_fence_init(struct pipe_screen *pscreen)
103 {
104 pscreen->fence_reference = fence_reference;
105 pscreen->fence_finish = fence_finish;
106 }