~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/microsoft/vulkan/dzn_sync.cpp

  • Committer: mmach
  • Date: 2022-09-22 20:02:48 UTC
  • Revision ID: netbit73@gmail.com-20220922200248-7y4wybmdgipuwdiw
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © Microsoft 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 "dzn_private.h"
25
 
 
26
 
#include "vk_alloc.h"
27
 
#include "vk_debug_report.h"
28
 
#include "vk_util.h"
29
 
 
30
 
#include "util/macros.h"
31
 
#include "util/os_time.h"
32
 
 
33
 
static VkResult
34
 
dzn_sync_init(struct vk_device *device,
35
 
              struct vk_sync *sync,
36
 
              uint64_t initial_value)
37
 
{
38
 
   dzn_sync *dsync = container_of(sync, dzn_sync, vk);
39
 
   dzn_device *ddev = container_of(device, dzn_device, vk);
40
 
 
41
 
   assert(!(sync->flags & VK_SYNC_IS_SHAREABLE));
42
 
 
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);
46
 
 
47
 
   return VK_SUCCESS;
48
 
}
49
 
 
50
 
static void
51
 
dzn_sync_finish(struct vk_device *device,
52
 
                struct vk_sync *sync)
53
 
{
54
 
   dzn_sync *dsync = container_of(sync, dzn_sync, vk);
55
 
 
56
 
   dsync->fence->Release();
57
 
}
58
 
 
59
 
static VkResult
60
 
dzn_sync_signal(struct vk_device *device,
61
 
                struct vk_sync *sync,
62
 
                uint64_t value)
63
 
{
64
 
   dzn_sync *dsync = container_of(sync, dzn_sync, vk);
65
 
 
66
 
   if (!(sync->flags & VK_SYNC_IS_TIMELINE))
67
 
      value = 1;
68
 
 
69
 
   if (FAILED(dsync->fence->Signal(value)))
70
 
      return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
71
 
 
72
 
   return VK_SUCCESS;
73
 
}
74
 
 
75
 
static VkResult
76
 
dzn_sync_get_value(struct vk_device *device,
77
 
                   struct vk_sync *sync,
78
 
                   uint64_t *value)
79
 
{
80
 
   dzn_sync *dsync = container_of(sync, dzn_sync, vk);
81
 
 
82
 
   *value = dsync->fence->GetCompletedValue();
83
 
   return VK_SUCCESS;
84
 
}
85
 
 
86
 
static VkResult
87
 
dzn_sync_reset(struct vk_device *device,
88
 
               struct vk_sync *sync)
89
 
{
90
 
   dzn_sync *dsync = container_of(sync, dzn_sync, vk);
91
 
 
92
 
   if (FAILED(dsync->fence->Signal(0)))
93
 
      return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
94
 
 
95
 
   return VK_SUCCESS;
96
 
}
97
 
 
98
 
static VkResult
99
 
dzn_sync_move(struct vk_device *device,
100
 
              struct vk_sync *dst,
101
 
              struct vk_sync *src)
102
 
{
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;
107
 
 
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);
111
 
 
112
 
   ddst->fence->Release();
113
 
   ddst->fence = dsrc->fence;
114
 
   dsrc->fence = new_fence;
115
 
   return VK_SUCCESS;
116
 
}
117
 
 
118
 
static VkResult
119
 
dzn_sync_wait(struct vk_device *device,
120
 
              uint32_t wait_count,
121
 
              const struct vk_sync_wait *waits,
122
 
              enum vk_sync_wait_flags wait_flags,
123
 
              uint64_t abs_timeout_ns)
124
 
{
125
 
   dzn_device *ddev = container_of(device, dzn_device, vk);
126
 
 
127
 
   HANDLE event = CreateEventA(NULL, FALSE, FALSE, NULL);
128
 
   if (event == NULL)
129
 
      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
130
 
 
131
 
   STACK_ARRAY(ID3D12Fence *, fences, wait_count);
132
 
   STACK_ARRAY(uint64_t, values, wait_count);
133
 
 
134
 
   for (uint32_t i = 0; i < wait_count; i++) {
135
 
      dzn_sync *sync = container_of(waits[i].sync, dzn_sync, vk);
136
 
 
137
 
      fences[i] = sync->fence;
138
 
      values[i] = (sync->vk.flags & VK_SYNC_IS_TIMELINE) ? waits[i].wait_value : 1;
139
 
   }
140
 
 
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;
145
 
 
146
 
   if (FAILED(ddev->dev->SetEventOnMultipleFenceCompletion(fences, values,
147
 
                                                           wait_count, flags,
148
 
                                                           event))) {
149
 
      STACK_ARRAY_FINISH(fences);
150
 
      STACK_ARRAY_FINISH(values);
151
 
      CloseHandle(event);
152
 
      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
153
 
   }
154
 
 
155
 
   DWORD timeout_ms;
156
 
 
157
 
   if (abs_timeout_ns == OS_TIMEOUT_INFINITE) {
158
 
      timeout_ms = INFINITE;
159
 
   } else {
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;
163
 
 
164
 
      timeout_ms = (rel_timeout_ns / 1000000) + (rel_timeout_ns % 1000000 ? 1 : 0);
165
 
   }
166
 
 
167
 
   DWORD res =
168
 
      WaitForSingleObject(event, timeout_ms);
169
 
 
170
 
   CloseHandle(event);
171
 
 
172
 
   STACK_ARRAY_FINISH(fences);
173
 
   STACK_ARRAY_FINISH(values);
174
 
 
175
 
   if (res == WAIT_TIMEOUT)
176
 
      return VK_TIMEOUT;
177
 
   else if (res != WAIT_OBJECT_0)
178
 
      return vk_error(device, VK_ERROR_UNKNOWN);
179
 
 
180
 
   return VK_SUCCESS;
181
 
}
182
 
 
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),
192
 
 
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,
200
 
};