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 "d3d12_fence.h"
25
26 #include "d3d12_context.h"
27 #include "d3d12_screen.h"
28
29 #include "util/u_memory.h"
30
31 #ifdef _WIN32
32 static void
close_event(HANDLE event,int fd)33 close_event(HANDLE event, int fd)
34 {
35 if (event)
36 CloseHandle(event);
37 }
38
39 static HANDLE
create_event(int * fd)40 create_event(int *fd)
41 {
42 *fd = -1;
43 return CreateEvent(NULL, FALSE, FALSE, NULL);
44 }
45
46 static bool
wait_event(HANDLE event,int event_fd,uint64_t timeout_ns)47 wait_event(HANDLE event, int event_fd, uint64_t timeout_ns)
48 {
49 DWORD timeout_ms = (timeout_ns == PIPE_TIMEOUT_INFINITE) ? INFINITE : timeout_ns / 1000000;
50 return WaitForSingleObject(event, timeout_ms) == WAIT_OBJECT_0;
51 }
52 #else
53 #include <sys/eventfd.h>
54 #include <poll.h>
55 #include <util/libsync.h>
56
57 static void
close_event(HANDLE event,int fd)58 close_event(HANDLE event, int fd)
59 {
60 if (fd != -1)
61 close(fd);
62 }
63
64 static HANDLE
create_event(int * fd)65 create_event(int *fd)
66 {
67 *fd = eventfd(0, 0);
68 return (HANDLE)(size_t)*fd;
69 }
70
71 static bool
wait_event(HANDLE event,int event_fd,uint64_t timeout_ns)72 wait_event(HANDLE event, int event_fd, uint64_t timeout_ns)
73 {
74 int timeout_ms = (timeout_ns == PIPE_TIMEOUT_INFINITE) ? -1 : timeout_ns / 1000000;
75 return sync_wait(event_fd, timeout_ms) == 0;
76 }
77 #endif
78
79 static void
destroy_fence(struct d3d12_fence * fence)80 destroy_fence(struct d3d12_fence *fence)
81 {
82 close_event(fence->event, fence->event_fd);
83 FREE(fence);
84 }
85
86 struct d3d12_fence *
d3d12_create_fence(struct d3d12_screen * screen,struct d3d12_context * ctx)87 d3d12_create_fence(struct d3d12_screen *screen, struct d3d12_context *ctx)
88 {
89 struct d3d12_fence *ret = CALLOC_STRUCT(d3d12_fence);
90 if (!ret) {
91 debug_printf("CALLOC_STRUCT failed\n");
92 return NULL;
93 }
94
95 ret->cmdqueue_fence = ctx->cmdqueue_fence;
96 ret->value = ++ctx->fence_value;
97 ret->event = create_event(&ret->event_fd);
98 if (FAILED(ctx->cmdqueue_fence->SetEventOnCompletion(ret->value, ret->event)))
99 goto fail;
100 if (FAILED(screen->cmdqueue->Signal(ctx->cmdqueue_fence, ret->value)))
101 goto fail;
102
103 pipe_reference_init(&ret->reference, 1);
104 return ret;
105
106 fail:
107 destroy_fence(ret);
108 return NULL;
109 }
110
111 void
d3d12_fence_reference(struct d3d12_fence ** ptr,struct d3d12_fence * fence)112 d3d12_fence_reference(struct d3d12_fence **ptr, struct d3d12_fence *fence)
113 {
114 if (pipe_reference(&(*ptr)->reference, &fence->reference))
115 destroy_fence((struct d3d12_fence *)*ptr);
116
117 *ptr = fence;
118 }
119
120 static void
fence_reference(struct pipe_screen * pscreen,struct pipe_fence_handle ** pptr,struct pipe_fence_handle * pfence)121 fence_reference(struct pipe_screen *pscreen,
122 struct pipe_fence_handle **pptr,
123 struct pipe_fence_handle *pfence)
124 {
125 d3d12_fence_reference((struct d3d12_fence **)pptr, d3d12_fence(pfence));
126 }
127
128 bool
d3d12_fence_finish(struct d3d12_fence * fence,uint64_t timeout_ns)129 d3d12_fence_finish(struct d3d12_fence *fence, uint64_t timeout_ns)
130 {
131 if (fence->signaled)
132 return true;
133
134 bool complete = fence->cmdqueue_fence->GetCompletedValue() >= fence->value;
135 if (!complete && timeout_ns)
136 complete = wait_event(fence->event, fence->event_fd, timeout_ns);
137
138 fence->signaled = complete;
139 return complete;
140 }
141
142 static bool
fence_finish(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_fence_handle * pfence,uint64_t timeout_ns)143 fence_finish(struct pipe_screen *pscreen, struct pipe_context *pctx,
144 struct pipe_fence_handle *pfence, uint64_t timeout_ns)
145 {
146 bool ret = d3d12_fence_finish(d3d12_fence(pfence), timeout_ns);
147 if (ret && pctx) {
148 struct d3d12_context *ctx = d3d12_context(pctx);
149 d3d12_foreach_submitted_batch(ctx, batch)
150 d3d12_reset_batch(ctx, batch, 0);
151 }
152 return ret;
153 }
154
155 void
d3d12_screen_fence_init(struct pipe_screen * pscreen)156 d3d12_screen_fence_init(struct pipe_screen *pscreen)
157 {
158 pscreen->fence_reference = fence_reference;
159 pscreen->fence_finish = fence_finish;
160 }
161