1 /*
2 * Copyright © 2021 Intel Corporation
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "vk_sync_binary.h"
25
26 #include "vk_util.h"
27
28 static struct vk_sync_binary *
to_vk_sync_binary(struct vk_sync * sync)29 to_vk_sync_binary(struct vk_sync *sync)
30 {
31 assert(sync->type->init == vk_sync_binary_init);
32
33 return container_of(sync, struct vk_sync_binary, sync);
34 }
35
36 VkResult
vk_sync_binary_init(struct vk_device * device,struct vk_sync * sync,uint64_t initial_value)37 vk_sync_binary_init(struct vk_device *device,
38 struct vk_sync *sync,
39 uint64_t initial_value)
40 {
41 struct vk_sync_binary *binary = to_vk_sync_binary(sync);
42
43 const struct vk_sync_binary_type *btype =
44 container_of(binary->sync.type, struct vk_sync_binary_type, sync);
45
46 assert(!(sync->flags & VK_SYNC_IS_TIMELINE));
47 assert(!(sync->flags & VK_SYNC_IS_SHAREABLE));
48
49 binary->next_point = (initial_value == 0);
50
51 return vk_sync_init(device, &binary->timeline, btype->timeline_type,
52 VK_SYNC_IS_TIMELINE, 0 /* initial_value */);
53 }
54
55 static void
vk_sync_binary_finish(struct vk_device * device,struct vk_sync * sync)56 vk_sync_binary_finish(struct vk_device *device,
57 struct vk_sync *sync)
58 {
59 struct vk_sync_binary *binary = to_vk_sync_binary(sync);
60
61 vk_sync_finish(device, &binary->timeline);
62 }
63
64 static VkResult
vk_sync_binary_reset(struct vk_device * device,struct vk_sync * sync)65 vk_sync_binary_reset(struct vk_device *device,
66 struct vk_sync *sync)
67 {
68 struct vk_sync_binary *binary = to_vk_sync_binary(sync);
69
70 binary->next_point++;
71
72 return VK_SUCCESS;
73 }
74
75 static VkResult
vk_sync_binary_signal(struct vk_device * device,struct vk_sync * sync,uint64_t value)76 vk_sync_binary_signal(struct vk_device *device,
77 struct vk_sync *sync,
78 uint64_t value)
79 {
80 struct vk_sync_binary *binary = to_vk_sync_binary(sync);
81
82 assert(value == 0);
83
84 return vk_sync_signal(device, &binary->timeline, binary->next_point);
85 }
86
87 static VkResult
vk_sync_binary_wait_many(struct vk_device * device,uint32_t wait_count,const struct vk_sync_wait * waits,enum vk_sync_wait_flags wait_flags,uint64_t abs_timeout_ns)88 vk_sync_binary_wait_many(struct vk_device *device,
89 uint32_t wait_count,
90 const struct vk_sync_wait *waits,
91 enum vk_sync_wait_flags wait_flags,
92 uint64_t abs_timeout_ns)
93 {
94 if (wait_count == 0)
95 return VK_SUCCESS;
96
97 STACK_ARRAY(struct vk_sync_wait, timeline_waits, wait_count);
98
99 for (uint32_t i = 0; i < wait_count; i++) {
100 struct vk_sync_binary *binary = to_vk_sync_binary(waits[i].sync);
101
102 timeline_waits[i] = (struct vk_sync_wait) {
103 .sync = &binary->timeline,
104 .stage_mask = waits[i].stage_mask,
105 .wait_value = binary->next_point,
106 };
107 }
108
109 VkResult result = vk_sync_wait_many(device, wait_count, timeline_waits,
110 wait_flags, abs_timeout_ns);
111
112 STACK_ARRAY_FINISH(timeline_waits);
113
114 return result;
115 }
116
117 struct vk_sync_binary_type
vk_sync_binary_get_type(const struct vk_sync_type * timeline_type)118 vk_sync_binary_get_type(const struct vk_sync_type *timeline_type)
119 {
120 assert(timeline_type->features & VK_SYNC_FEATURE_TIMELINE);
121
122 return (struct vk_sync_binary_type) {
123 .sync = {
124 .size = offsetof(struct vk_sync_binary, timeline) +
125 timeline_type->size,
126 .features = VK_SYNC_FEATURE_BINARY |
127 VK_SYNC_FEATURE_GPU_WAIT |
128 VK_SYNC_FEATURE_CPU_WAIT |
129 VK_SYNC_FEATURE_CPU_RESET |
130 VK_SYNC_FEATURE_CPU_SIGNAL |
131 VK_SYNC_FEATURE_WAIT_ANY |
132 VK_SYNC_FEATURE_WAIT_PENDING,
133 .init = vk_sync_binary_init,
134 .finish = vk_sync_binary_finish,
135 .reset = vk_sync_binary_reset,
136 .signal = vk_sync_binary_signal,
137 .wait_many = vk_sync_binary_wait_many,
138 },
139 .timeline_type = timeline_type,
140 };
141 }
142