d3d12_fence.cpp revision 7ec681f3
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 32static void 33close_event(HANDLE event, int fd) 34{ 35 if (event) 36 CloseHandle(event); 37} 38 39static HANDLE 40create_event(int *fd) 41{ 42 *fd = -1; 43 return CreateEvent(NULL, FALSE, FALSE, NULL); 44} 45 46static bool 47wait_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 57static void 58close_event(HANDLE event, int fd) 59{ 60 if (fd != -1) 61 close(fd); 62} 63 64static HANDLE 65create_event(int *fd) 66{ 67 *fd = eventfd(0, 0); 68 return (HANDLE)(size_t)*fd; 69} 70 71static bool 72wait_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 79static void 80destroy_fence(struct d3d12_fence *fence) 81{ 82 close_event(fence->event, fence->event_fd); 83 FREE(fence); 84} 85 86struct d3d12_fence * 87d3d12_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 106fail: 107 destroy_fence(ret); 108 return NULL; 109} 110 111void 112d3d12_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 120static void 121fence_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 128bool 129d3d12_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 142static bool 143fence_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 155void 156d3d12_screen_fence_init(struct pipe_screen *pscreen) 157{ 158 pscreen->fence_reference = fence_reference; 159 pscreen->fence_finish = fence_finish; 160} 161