Path: blob/21.2-virgl/src/gallium/drivers/d3d12/d3d12_fence.cpp
4570 views
/*1* Copyright © Microsoft Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "d3d12_fence.h"2425#include "d3d12_context.h"26#include "d3d12_screen.h"2728#include "util/u_memory.h"2930#ifdef _WIN3231static void32close_event(HANDLE event, int fd)33{34if (event)35CloseHandle(event);36}3738static HANDLE39create_event(int *fd)40{41*fd = -1;42return CreateEvent(NULL, FALSE, FALSE, NULL);43}4445static bool46wait_event(HANDLE event, int event_fd, uint64_t timeout_ns)47{48DWORD timeout_ms = (timeout_ns == PIPE_TIMEOUT_INFINITE) ? INFINITE : timeout_ns / 1000000;49return WaitForSingleObject(event, timeout_ms) == WAIT_OBJECT_0;50}51#else52#include <sys/eventfd.h>53#include <poll.h>54#include <util/libsync.h>5556static void57close_event(HANDLE event, int fd)58{59if (fd != -1)60close(fd);61}6263static HANDLE64create_event(int *fd)65{66*fd = eventfd(0, 0);67return (HANDLE)(size_t)*fd;68}6970static bool71wait_event(HANDLE event, int event_fd, uint64_t timeout_ns)72{73int timeout_ms = (timeout_ns == PIPE_TIMEOUT_INFINITE) ? -1 : timeout_ns / 1000000;74return sync_wait(event_fd, timeout_ms);75}76#endif7778static void79destroy_fence(struct d3d12_fence *fence)80{81close_event(fence->event, fence->event_fd);82FREE(fence);83}8485struct d3d12_fence *86d3d12_create_fence(struct d3d12_screen *screen, struct d3d12_context *ctx)87{88struct d3d12_fence *ret = CALLOC_STRUCT(d3d12_fence);89if (!ret) {90debug_printf("CALLOC_STRUCT failed\n");91return NULL;92}9394ret->cmdqueue_fence = ctx->cmdqueue_fence;95ret->value = ++ctx->fence_value;96ret->event = create_event(&ret->event_fd);97if (FAILED(ctx->cmdqueue_fence->SetEventOnCompletion(ret->value, ret->event)))98goto fail;99if (FAILED(screen->cmdqueue->Signal(ctx->cmdqueue_fence, ret->value)))100goto fail;101102pipe_reference_init(&ret->reference, 1);103return ret;104105fail:106destroy_fence(ret);107return NULL;108}109110void111d3d12_fence_reference(struct d3d12_fence **ptr, struct d3d12_fence *fence)112{113if (pipe_reference(&(*ptr)->reference, &fence->reference))114destroy_fence((struct d3d12_fence *)*ptr);115116*ptr = fence;117}118119static void120fence_reference(struct pipe_screen *pscreen,121struct pipe_fence_handle **pptr,122struct pipe_fence_handle *pfence)123{124d3d12_fence_reference((struct d3d12_fence **)pptr, d3d12_fence(pfence));125}126127bool128d3d12_fence_finish(struct d3d12_fence *fence, uint64_t timeout_ns)129{130if (fence->signaled)131return true;132133bool complete = fence->cmdqueue_fence->GetCompletedValue() >= fence->value;134if (!complete && timeout_ns)135complete = wait_event(fence->event, fence->event_fd, timeout_ns);136137fence->signaled = complete;138return complete;139}140141static bool142fence_finish(struct pipe_screen *pscreen, struct pipe_context *pctx,143struct pipe_fence_handle *pfence, uint64_t timeout_ns)144{145bool ret = d3d12_fence_finish(d3d12_fence(pfence), timeout_ns);146if (ret && pctx) {147struct d3d12_context *ctx = d3d12_context(pctx);148d3d12_foreach_submitted_batch(ctx, batch)149d3d12_reset_batch(ctx, batch, 0);150}151return ret;152}153154void155d3d12_screen_fence_init(struct pipe_screen *pscreen)156{157pscreen->fence_reference = fence_reference;158pscreen->fence_finish = fence_finish;159}160161162