2
* Copyright © Microsoft Corporation
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:
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
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
24
#include "dzn_private.h"
27
#include "vk_debug_report.h"
30
#include "util/macros.h"
31
#include "util/os_time.h"
34
dzn_sync_init(struct vk_device *device,
36
uint64_t initial_value)
38
dzn_sync *dsync = container_of(sync, dzn_sync, vk);
39
dzn_device *ddev = container_of(device, dzn_device, vk);
41
assert(!(sync->flags & VK_SYNC_IS_SHAREABLE));
43
if (FAILED(ddev->dev->CreateFence(initial_value, D3D12_FENCE_FLAG_NONE,
44
IID_PPV_ARGS(&dsync->fence))))
45
return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
51
dzn_sync_finish(struct vk_device *device,
54
dzn_sync *dsync = container_of(sync, dzn_sync, vk);
56
dsync->fence->Release();
60
dzn_sync_signal(struct vk_device *device,
64
dzn_sync *dsync = container_of(sync, dzn_sync, vk);
66
if (!(sync->flags & VK_SYNC_IS_TIMELINE))
69
if (FAILED(dsync->fence->Signal(value)))
70
return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
76
dzn_sync_get_value(struct vk_device *device,
80
dzn_sync *dsync = container_of(sync, dzn_sync, vk);
82
*value = dsync->fence->GetCompletedValue();
87
dzn_sync_reset(struct vk_device *device,
90
dzn_sync *dsync = container_of(sync, dzn_sync, vk);
92
if (FAILED(dsync->fence->Signal(0)))
93
return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
99
dzn_sync_move(struct vk_device *device,
103
dzn_device *ddev = container_of(device, dzn_device, vk);
104
dzn_sync *ddst = container_of(dst, dzn_sync, vk);
105
dzn_sync *dsrc = container_of(src, dzn_sync, vk);
106
ID3D12Fence *new_fence;
108
if (FAILED(ddev->dev->CreateFence(0, D3D12_FENCE_FLAG_NONE,
109
IID_PPV_ARGS(&new_fence))))
110
return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
112
ddst->fence->Release();
113
ddst->fence = dsrc->fence;
114
dsrc->fence = new_fence;
119
dzn_sync_wait(struct vk_device *device,
121
const struct vk_sync_wait *waits,
122
enum vk_sync_wait_flags wait_flags,
123
uint64_t abs_timeout_ns)
125
dzn_device *ddev = container_of(device, dzn_device, vk);
127
HANDLE event = CreateEventA(NULL, FALSE, FALSE, NULL);
129
return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
131
STACK_ARRAY(ID3D12Fence *, fences, wait_count);
132
STACK_ARRAY(uint64_t, values, wait_count);
134
for (uint32_t i = 0; i < wait_count; i++) {
135
dzn_sync *sync = container_of(waits[i].sync, dzn_sync, vk);
137
fences[i] = sync->fence;
138
values[i] = (sync->vk.flags & VK_SYNC_IS_TIMELINE) ? waits[i].wait_value : 1;
141
D3D12_MULTIPLE_FENCE_WAIT_FLAGS flags =
142
(wait_flags & VK_SYNC_WAIT_ANY) ?
143
D3D12_MULTIPLE_FENCE_WAIT_FLAG_ANY :
144
D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL;
146
if (FAILED(ddev->dev->SetEventOnMultipleFenceCompletion(fences, values,
149
STACK_ARRAY_FINISH(fences);
150
STACK_ARRAY_FINISH(values);
152
return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
157
if (abs_timeout_ns == OS_TIMEOUT_INFINITE) {
158
timeout_ms = INFINITE;
160
uint64_t cur_time = os_time_get_nano();
161
uint64_t rel_timeout_ns =
162
abs_timeout_ns > cur_time ? abs_timeout_ns - cur_time : 0;
164
timeout_ms = (rel_timeout_ns / 1000000) + (rel_timeout_ns % 1000000 ? 1 : 0);
168
WaitForSingleObject(event, timeout_ms);
172
STACK_ARRAY_FINISH(fences);
173
STACK_ARRAY_FINISH(values);
175
if (res == WAIT_TIMEOUT)
177
else if (res != WAIT_OBJECT_0)
178
return vk_error(device, VK_ERROR_UNKNOWN);
183
const struct vk_sync_type dzn_sync_type = {
184
.size = sizeof(dzn_sync),
185
.features = (enum vk_sync_features)
186
(VK_SYNC_FEATURE_TIMELINE |
187
VK_SYNC_FEATURE_GPU_WAIT |
188
VK_SYNC_FEATURE_CPU_WAIT |
189
VK_SYNC_FEATURE_CPU_SIGNAL |
190
VK_SYNC_FEATURE_WAIT_ANY |
191
VK_SYNC_FEATURE_WAIT_BEFORE_SIGNAL),
193
.init = dzn_sync_init,
194
.finish = dzn_sync_finish,
195
.signal = dzn_sync_signal,
196
.get_value = dzn_sync_get_value,
197
.reset = dzn_sync_reset,
198
.move = dzn_sync_move,
199
.wait_many = dzn_sync_wait,