1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "vk_sync_binary.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "vk_util.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_cistatic struct vk_sync_binary *
29bf215546Sopenharmony_cito_vk_sync_binary(struct vk_sync *sync)
30bf215546Sopenharmony_ci{
31bf215546Sopenharmony_ci   assert(sync->type->init == vk_sync_binary_init);
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci   return container_of(sync, struct vk_sync_binary, sync);
34bf215546Sopenharmony_ci}
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ciVkResult
37bf215546Sopenharmony_civk_sync_binary_init(struct vk_device *device,
38bf215546Sopenharmony_ci                    struct vk_sync *sync,
39bf215546Sopenharmony_ci                    uint64_t initial_value)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   struct vk_sync_binary *binary = to_vk_sync_binary(sync);
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   const struct vk_sync_binary_type *btype =
44bf215546Sopenharmony_ci      container_of(binary->sync.type, struct vk_sync_binary_type, sync);
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   assert(!(sync->flags & VK_SYNC_IS_TIMELINE));
47bf215546Sopenharmony_ci   assert(!(sync->flags & VK_SYNC_IS_SHAREABLE));
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   binary->next_point = (initial_value == 0);
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   return vk_sync_init(device, &binary->timeline, btype->timeline_type,
52bf215546Sopenharmony_ci                       VK_SYNC_IS_TIMELINE, 0 /* initial_value */);
53bf215546Sopenharmony_ci}
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_cistatic void
56bf215546Sopenharmony_civk_sync_binary_finish(struct vk_device *device,
57bf215546Sopenharmony_ci                      struct vk_sync *sync)
58bf215546Sopenharmony_ci{
59bf215546Sopenharmony_ci   struct vk_sync_binary *binary = to_vk_sync_binary(sync);
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   vk_sync_finish(device, &binary->timeline);
62bf215546Sopenharmony_ci}
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_cistatic VkResult
65bf215546Sopenharmony_civk_sync_binary_reset(struct vk_device *device,
66bf215546Sopenharmony_ci                     struct vk_sync *sync)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   struct vk_sync_binary *binary = to_vk_sync_binary(sync);
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   binary->next_point++;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   return VK_SUCCESS;
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cistatic VkResult
76bf215546Sopenharmony_civk_sync_binary_signal(struct vk_device *device,
77bf215546Sopenharmony_ci                      struct vk_sync *sync,
78bf215546Sopenharmony_ci                      uint64_t value)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   struct vk_sync_binary *binary = to_vk_sync_binary(sync);
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   assert(value == 0);
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   return vk_sync_signal(device, &binary->timeline, binary->next_point);
85bf215546Sopenharmony_ci}
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_cistatic VkResult
88bf215546Sopenharmony_civk_sync_binary_wait_many(struct vk_device *device,
89bf215546Sopenharmony_ci                         uint32_t wait_count,
90bf215546Sopenharmony_ci                         const struct vk_sync_wait *waits,
91bf215546Sopenharmony_ci                         enum vk_sync_wait_flags wait_flags,
92bf215546Sopenharmony_ci                         uint64_t abs_timeout_ns)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   STACK_ARRAY(struct vk_sync_wait, timeline_waits, wait_count);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   for (uint32_t i = 0; i < wait_count; i++) {
97bf215546Sopenharmony_ci      struct vk_sync_binary *binary = to_vk_sync_binary(waits[i].sync);
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci      timeline_waits[i] = (struct vk_sync_wait) {
100bf215546Sopenharmony_ci         .sync = &binary->timeline,
101bf215546Sopenharmony_ci         .stage_mask = waits[i].stage_mask,
102bf215546Sopenharmony_ci         .wait_value = binary->next_point,
103bf215546Sopenharmony_ci      };
104bf215546Sopenharmony_ci   }
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   VkResult result = vk_sync_wait_many(device, wait_count, timeline_waits,
107bf215546Sopenharmony_ci                                       wait_flags, abs_timeout_ns);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   STACK_ARRAY_FINISH(timeline_waits);
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   return result;
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_cistruct vk_sync_binary_type
115bf215546Sopenharmony_civk_sync_binary_get_type(const struct vk_sync_type *timeline_type)
116bf215546Sopenharmony_ci{
117bf215546Sopenharmony_ci   assert(timeline_type->features & VK_SYNC_FEATURE_TIMELINE);
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   return (struct vk_sync_binary_type) {
120bf215546Sopenharmony_ci      .sync = {
121bf215546Sopenharmony_ci         .size = offsetof(struct vk_sync_binary, timeline) +
122bf215546Sopenharmony_ci                 timeline_type->size,
123bf215546Sopenharmony_ci         .features = VK_SYNC_FEATURE_BINARY |
124bf215546Sopenharmony_ci                     VK_SYNC_FEATURE_GPU_WAIT |
125bf215546Sopenharmony_ci                     VK_SYNC_FEATURE_CPU_WAIT |
126bf215546Sopenharmony_ci                     VK_SYNC_FEATURE_CPU_RESET |
127bf215546Sopenharmony_ci                     VK_SYNC_FEATURE_CPU_SIGNAL |
128bf215546Sopenharmony_ci                     VK_SYNC_FEATURE_WAIT_ANY |
129bf215546Sopenharmony_ci                     VK_SYNC_FEATURE_WAIT_PENDING,
130bf215546Sopenharmony_ci         .init = vk_sync_binary_init,
131bf215546Sopenharmony_ci         .finish = vk_sync_binary_finish,
132bf215546Sopenharmony_ci         .reset = vk_sync_binary_reset,
133bf215546Sopenharmony_ci         .signal = vk_sync_binary_signal,
134bf215546Sopenharmony_ci         .wait_many = vk_sync_binary_wait_many,
135bf215546Sopenharmony_ci      },
136bf215546Sopenharmony_ci      .timeline_type = timeline_type,
137bf215546Sopenharmony_ci   };
138bf215546Sopenharmony_ci}
139