From 44b02e58fc074d94056be2f51c2f74ad393eb231 Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Tue, 19 Sep 2023 18:57:53 +0900 Subject: [PATCH] d3d12fence: Check completed value before waiting If currently completed fence value is larger than target value, skip waiting. Part-of: --- .../sys/d3d12/gstd3d12fence.cpp | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/subprojects/gst-plugins-bad/sys/d3d12/gstd3d12fence.cpp b/subprojects/gst-plugins-bad/sys/d3d12/gstd3d12fence.cpp index 418da3f62b..6f24b1d3ba 100644 --- a/subprojects/gst-plugins-bad/sys/d3d12/gstd3d12fence.cpp +++ b/subprojects/gst-plugins-bad/sys/d3d12/gstd3d12fence.cpp @@ -51,7 +51,6 @@ struct _GstD3D12FencePrivate HANDLE event_handle; std::mutex lock; guint64 value = 0; - gboolean can_wait = FALSE; }; /* *INDENT-ON* */ @@ -108,21 +107,15 @@ gst_d3d12_fence_set_event_on_completion_value (GstD3D12Fence * fence, guint64 value) { GstD3D12FencePrivate *priv; - HRESULT hr; g_return_val_if_fail (fence != nullptr, FALSE); priv = fence->priv; std::lock_guard < std::mutex > lk (priv->lock); - hr = priv->fence->SetEventOnCompletion (value, priv->event_handle); - if (!gst_d3d12_result (hr, fence->device)) { - GST_ERROR_OBJECT (fence->device, "Failed to set completion event"); - return FALSE; - } - - priv->value = value; - priv->can_wait = TRUE; + auto current = priv->fence->GetCompletedValue (); + if (value > current) + priv->value = value; return TRUE; } @@ -143,15 +136,27 @@ gst_d3d12_fence_wait_for (GstD3D12Fence * fence, guint timeout_ms) GstD3D12FencePrivate *priv = fence->priv; std::lock_guard < std::mutex > lk (priv->lock); - if (!priv->can_wait) + if (!priv->value) return; - GST_TRACE ("Waiting for fence to be signalled with value %" G_GUINT64_FORMAT, - priv->value); - WaitForSingleObjectEx (priv->event_handle, timeout_ms, FALSE); - GST_TRACE ("Signalled with value %" G_GUINT64_FORMAT, priv->value); + auto current = priv->fence->GetCompletedValue (); + if (current < priv->value) { + HRESULT hr; + GST_TRACE ("Waiting for fence to be signalled with value %" G_GUINT64_FORMAT + ", current: %" G_GUINT64_FORMAT, priv->value, current); - priv->can_wait = FALSE; + hr = priv->fence->SetEventOnCompletion (priv->value, priv->event_handle); + if (!gst_d3d12_result (hr, fence->device)) { + GST_ERROR_OBJECT (fence->device, "Failed to set completion event"); + return; + } + + WaitForSingleObjectEx (priv->event_handle, timeout_ms, FALSE); + GST_TRACE ("Signalled with value %" G_GUINT64_FORMAT, priv->value); + } else { + GST_TRACE ("target %" G_GUINT64_FORMAT " <= target: %" G_GUINT64_FORMAT, + priv->value, current); + } } void