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#ifndef VK_SYNC_BINARY_H
24#define VK_SYNC_BINARY_H
25
26#include "vk_sync.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32struct vk_sync_binary_type {
33   struct vk_sync_type sync;
34   const struct vk_sync_type *timeline_type;
35};
36
37struct vk_sync_binary_type
38vk_sync_binary_get_type(const struct vk_sync_type *timeline_type);
39
40/** Implements a binary vk_sync type on top of a timeline vk_sync
41 *
42 * This is useful when targeting Windows APIs such as D3D12 which only have
43 * timelines and have no concept of a binary synchronization object.  Because
44 * binary vk_sync emulation requires tracking additional state (the next time
45 * point), fences and semaphores created from this type cannot support any of
46 * the sharing APIs.
47 */
48struct vk_sync_binary {
49   struct vk_sync sync;
50
51   uint64_t next_point;
52
53   struct vk_sync timeline;
54};
55
56VkResult vk_sync_binary_init(struct vk_device *device,
57                             struct vk_sync *sync,
58                             uint64_t initial_value);
59
60static inline bool
61vk_sync_type_is_vk_sync_binary(const struct vk_sync_type *type)
62{
63   return type->init == vk_sync_binary_init;
64}
65
66static inline struct vk_sync_binary *
67vk_sync_as_binary(struct vk_sync *sync)
68{
69   if (!vk_sync_type_is_vk_sync_binary(sync->type))
70      return NULL;
71
72   return container_of(sync, struct vk_sync_binary, sync);
73}
74
75#ifdef __cplusplus
76}
77#endif
78
79#endif /* VK_TIMELINE_H */
80