From bab59efcb53451d346b01e7f4d598743e3ab1d8f Mon Sep 17 00:00:00 2001 From: "Mark.lin" Date: Thu, 13 Aug 2026 10:49:09 +0800 Subject: [PATCH] Diagnose preview stalls and FIFO frame loss - add detailed FIFO loss and frame-hold timing diagnostics - report slow preview calls and per-stage GPU timing - reduce redundant preview pipeline and DXGI setup work - move preview performance metrics into the sample status display - avoid duplicate frame-loss warnings --- helpers/gvfg_preview/CMakeLists.txt | 1 + helpers/gvfg_preview/src/gvfg_preview.cpp | 73 +++++++++++++++++-- samples/gvfg_qt_preview/mainwindow.cpp | 36 ++++----- samples/gvfg_qt_preview/mainwindow.h | 1 + .../pcies2mm/pcies2mm_capture_session.cpp | 43 ++++++++++- .../pcies2mm/pcies2mm_capture_session.h | 3 + sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp | 31 ++++---- 7 files changed, 149 insertions(+), 39 deletions(-) diff --git a/helpers/gvfg_preview/CMakeLists.txt b/helpers/gvfg_preview/CMakeLists.txt index 3c50ae3..04fd0cb 100644 --- a/helpers/gvfg_preview/CMakeLists.txt +++ b/helpers/gvfg_preview/CMakeLists.txt @@ -23,6 +23,7 @@ target_include_directories(gvfg_preview target_compile_definitions(gvfg_preview PRIVATE GVFG_PREVIEW_BUILD + GVFG_INTERNAL_DIAGNOSTICS=$,1,0> ) target_link_libraries(gvfg_preview diff --git a/helpers/gvfg_preview/src/gvfg_preview.cpp b/helpers/gvfg_preview/src/gvfg_preview.cpp index 02bddef..1906889 100644 --- a/helpers/gvfg_preview/src/gvfg_preview.cpp +++ b/helpers/gvfg_preview/src/gvfg_preview.cpp @@ -9,8 +9,10 @@ #include #include #include +#include #include #include +#include #include using Microsoft::WRL::ComPtr; @@ -27,6 +29,9 @@ public: resetPresentStats(); hwnd_ = hwnd; configured_ = hwnd_ != nullptr; + preparedWidth_ = 0; + preparedHeight_ = 0; + preparedBitDepth_ = 0; if (configured_ && ensureDevice()) { if (!pipeline_) @@ -53,6 +58,8 @@ public: bool render(const gvfg_preview_frame_t &frame) { + using Clock = std::chrono::steady_clock; + const auto totalStart = Clock::now(); std::lock_guard lock(mutex_); if (!configured_ || !hwnd_ || !frame.data || frame.width <= 0 || frame.height <= 0) { @@ -68,7 +75,21 @@ public: sourceBitDepth = 10; } - if (!ensureDevice() || !ensurePipeline(frame.width, frame.height, sourceBitDepth)) + if (!ensureDevice()) + { + clearActiveInfo(); + return false; + } + + const bool pipelineMatches = pipeline_ && + preparedWidth_ == frame.width && + preparedHeight_ == frame.height && + preparedBitDepth_ == sourceBitDepth; + const bool pipelineReady = pipelineMatches + ? pipeline_->ensure_preview_swapchain(frame.width, frame.height) + : ensurePipeline(frame.width, frame.height, sourceBitDepth); + const auto ensureEnd = Clock::now(); + if (!pipelineReady) { clearActiveInfo(); return false; @@ -102,10 +123,14 @@ public: default: return false; } + const auto uploadEnd = Clock::now(); - if (!uploaded || - !pipeline_->render_uploaded_yuv_to_fp16(renderFmt, frame.width, frame.height) || - !pipeline_->copy_fp16_to_scene()) + const bool rendered = uploaded && + pipeline_->render_uploaded_yuv_to_fp16(renderFmt, frame.width, frame.height); + const auto renderEnd = Clock::now(); + const bool copied = rendered && pipeline_->copy_fp16_to_scene(); + const auto copyEnd = Clock::now(); + if (!uploaded || !rendered || !copied) { clearActiveInfo(); return false; @@ -114,6 +139,7 @@ public: bool ok = true; if (!pipeline_->preview_swapchain_10bit()) ok = pipeline_->blit_fp16_to_rgba8(frame.width, frame.height); + const auto blitEnd = Clock::now(); if (!ok) { clearActiveInfo(); @@ -122,6 +148,28 @@ public: const gvfg::internal::gvfg_preview_present_result_t presentResult = pipeline_->present_preview(frame.width, frame.height); + const auto presentEnd = Clock::now(); +#if GVFG_INTERNAL_DIAGNOSTICS + const auto milliseconds = [](Clock::duration duration) + { + return std::chrono::duration(duration).count(); + }; + const double totalMs = milliseconds(presentEnd - totalStart); + if (totalMs >= 10.0) + { + char line[512] = {}; + std::snprintf(line, sizeof(line), + "[GVFG][PREVIEW] SLOW frame=%llu total=%.3f lock_ensure=%.3f upload=%.3f render=%.3f copy=%.3f blit=%.3f present=%.3f ms\n", + static_cast(frame.frame_id), totalMs, + milliseconds(ensureEnd - totalStart), + milliseconds(uploadEnd - ensureEnd), + milliseconds(renderEnd - uploadEnd), + milliseconds(copyEnd - renderEnd), + milliseconds(blitEnd - copyEnd), + milliseconds(presentEnd - blitEnd)); + OutputDebugStringA(line); + } +#endif if (presentResult == gvfg::internal::GVFG_PREVIEW_PRESENT_FAILED) { clearActiveInfo(); @@ -153,6 +201,9 @@ public: pipeline_->release_preview_swapchain(); pipeline_.reset(); d3d_.reset(); + preparedWidth_ = 0; + preparedHeight_ = 0; + preparedBitDepth_ = 0; configured_ = false; hwnd_ = nullptr; clearActiveInfo(); @@ -375,8 +426,15 @@ private: desc.swapchain_10bit = gvfg::internal::GVFG_RENDER_PREVIEW_BITDEPTH_AUTO; pipeline_->configurePreview(desc); pipeline_->set_source_bit_depth(sourceBitDepth > 0 ? sourceBitDepth : 8); - return pipeline_->ensure_rt_and_pipeline(width, height) && - pipeline_->ensure_preview_swapchain(width, height); + const bool ready = pipeline_->ensure_rt_and_pipeline(width, height) && + pipeline_->ensure_preview_swapchain(width, height); + if (ready) + { + preparedWidth_ = width; + preparedHeight_ = height; + preparedBitDepth_ = sourceBitDepth > 0 ? sourceBitDepth : 8; + } + return ready; } mutable std::mutex mutex_; @@ -387,6 +445,9 @@ private: std::atomic bitDepth_{0}; std::atomic swapchain10Bit_{false}; std::atomic active_{false}; + int preparedWidth_ = 0; + int preparedHeight_ = 0; + int preparedBitDepth_ = 0; char adapterName_[160] = {}; int adapterIndex_ = -1; std::deque presentTimes_; diff --git a/samples/gvfg_qt_preview/mainwindow.cpp b/samples/gvfg_qt_preview/mainwindow.cpp index af5ac13..43263d5 100644 --- a/samples/gvfg_qt_preview/mainwindow.cpp +++ b/samples/gvfg_qt_preview/mainwindow.cpp @@ -378,6 +378,7 @@ void MainWindow::startCapture() previewFailureCount_ = 0; previewCallAverageMs_.store(0.0, std::memory_order_relaxed); + previewCallMaximumMs_.store(0.0, std::memory_order_relaxed); previewCallSamples_.store(0, std::memory_order_relaxed); #if GVFG_INTERNAL_DIAGNOSTICS haveDebugBaseline_ = false; @@ -501,6 +502,7 @@ void MainWindow::updateSignalStatus() ? QStringLiteral("Measuring") : QStringLiteral("%1 FPS").arg(previewFps); const double previewCallAverageMs = previewCallAverageMs_.load(std::memory_order_relaxed); + const double previewCallMaximumMs = previewCallMaximumMs_.load(std::memory_order_relaxed); const uint64_t previewCallSamples = previewCallSamples_.load(std::memory_order_relaxed); QStringList statusLines; statusLines << (signal.connected @@ -511,9 +513,10 @@ void MainWindow::updateSignalStatus() .arg(signal.channel)); statusLines << (previewInfoOk ? previewCallSamples > 0 - ? QStringLiteral("Preview | %1 | %2 | GPU call avg=%3 ms/frame samples=%4") + ? QStringLiteral("Preview | %1 | %2 | GPU call avg=%3 max=%4 ms/frame samples=%5") .arg(previewState, previewFrame) .arg(previewCallAverageMs, 0, 'f', 3) + .arg(previewCallMaximumMs, 0, 'f', 3) .arg(static_cast(previewCallSamples)) : QStringLiteral("Preview | %1 | %2 | GPU call measuring") .arg(previewState, previewFrame) @@ -555,23 +558,6 @@ void MainWindow::updateSignalStatus() valueOrDash(backendStats.backend_dma_errors))); diagnosticProblemDetected = true; } - if (backendStats.backend_frames_dropped > lastDebugDroppedFrames_) - { - const uint64_t delta = backendStats.backend_frames_dropped - lastDebugDroppedFrames_; - pendingDroppedFrames_ += delta; - } - } - - const qint64 nowMs = QDateTime::currentMSecsSinceEpoch(); - if (pendingDroppedFrames_ > 0 && - (lastDroppedWarningMs_ == 0 || nowMs - lastDroppedWarningMs_ >= 10000)) - { - appendLog(QStringLiteral("WARNING dropped frames +%1, total=%2") - .arg(valueOrDash(pendingDroppedFrames_), - valueOrDash(backendStats.backend_frames_dropped))); - pendingDroppedFrames_ = 0; - lastDroppedWarningMs_ = nowMs; - diagnosticProblemDetected = true; } lastDebugDmaErrors_ = backendStats.backend_dma_errors; @@ -818,6 +804,20 @@ void MainWindow::captureReadLoop() const auto previewEnd = std::chrono::steady_clock::now(); const double previewElapsedMs = std::chrono::duration(previewEnd - previewStart).count(); + double observedMaximum = previewCallMaximumMs_.load(std::memory_order_relaxed); + while (previewElapsedMs > observedMaximum && + !previewCallMaximumMs_.compare_exchange_weak( + observedMaximum, previewElapsedMs, std::memory_order_relaxed)) + { + } + if (previewElapsedMs >= 10.0) + { + QMetaObject::invokeMethod(this, [this, frameId = frame.frame_id, previewElapsedMs]() + { appendLog(QStringLiteral("SLOW PREVIEW frame=%1 elapsed=%2 ms") + .arg(static_cast(frameId)) + .arg(previewElapsedMs, 0, 'f', 3)); }, + Qt::QueuedConnection); + } if (previewStatus != GVFG_PREVIEW_OK) { const uint64_t failures = ++previewFailureCount_; diff --git a/samples/gvfg_qt_preview/mainwindow.h b/samples/gvfg_qt_preview/mainwindow.h index 7118d6c..1acd993 100644 --- a/samples/gvfg_qt_preview/mainwindow.h +++ b/samples/gvfg_qt_preview/mainwindow.h @@ -73,6 +73,7 @@ private: std::thread captureThread_; uint64_t previewFailureCount_ = 0; std::atomic previewCallAverageMs_{0.0}; + std::atomic previewCallMaximumMs_{0.0}; std::atomic previewCallSamples_{0}; QTimer *signalStatusTimer_ = nullptr; QString lastSignalStatusText_; diff --git a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp index bb15a77..3fc03f7 100644 --- a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp +++ b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp @@ -310,6 +310,8 @@ namespace gvfg::internal slot.data.assign(bytes, 0); next_write_slot_ = 0; active_delivery_slot_ = static_cast(-1); + active_delivery_started_ = {}; + last_delivery_started_ = {}; pending_events_ = 0; latest_sequence_ = 0; delivered_sequence_ = 0; @@ -505,6 +507,8 @@ namespace gvfg::internal slot.ready = false; slot.in_use = true; active_delivery_slot_ = readySlot; + active_delivery_started_ = std::chrono::steady_clock::now(); + last_delivery_started_ = active_delivery_started_; delivered_sequence_ = slot.sequence; ++stats_.frames_delivered; @@ -535,6 +539,14 @@ namespace gvfg::internal return PCIES2MM_EINVAL; frame_ring_[active_delivery_slot_].in_use = false; + const double heldMs = active_delivery_started_.time_since_epoch().count() != 0 + ? std::chrono::duration( + std::chrono::steady_clock::now() - active_delivery_started_).count() + : 0.0; + if (heldMs >= 10.0) + PCIES2MM_LOG("slow frame release: frame=%llu held_ms=%.3f", + static_cast(frame.frame_id), heldMs); + active_delivery_started_ = {}; active_delivery_slot_ = static_cast(-1); data_cv_.notify_all(); return PCIES2MM_OK; @@ -841,7 +853,6 @@ namespace gvfg::internal switch (waitResult - WAIT_OBJECT_0) { case 0: - PCIES2MM_LOG("event: DMA"); handle_dma_event(channel); break; case 1: @@ -938,6 +949,30 @@ namespace gvfg::internal { ++stats_.frames_dropped; frameLost = true; +#if GVFG_INTERNAL_DIAGNOSTICS + const auto now = std::chrono::steady_clock::now(); + const double heldMs = active_delivery_started_.time_since_epoch().count() != 0 + ? std::chrono::duration(now - active_delivery_started_).count() + : 0.0; + const double sinceReadMs = last_delivery_started_.time_since_epoch().count() != 0 + ? std::chrono::duration(now - last_delivery_started_).count() + : 0.0; + PCIES2MM_LOG("FIFO LOSS: total=%llu overwrite_slot=%zu overwrite_seq=%llu delivered=%llu latest=%llu active_slot=%zu held_ms=%.3f since_read_ms=%.3f pending=%u", + static_cast(stats_.frames_dropped), + slotIndex, + static_cast(slot.sequence), + static_cast(delivered_sequence_), + static_cast(latest_sequence_), + active_delivery_slot_, heldMs, sinceReadMs, pending_events_); + for (size_t i = 0; i < frame_ring_.size(); ++i) + { + const FrameSlot &diagnosticSlot = frame_ring_[i]; + PCIES2MM_LOG("FIFO slot[%zu]: ready=%d in_use=%d sequence=%llu bytes=%zu", + i, diagnosticSlot.ready ? 1 : 0, diagnosticSlot.in_use ? 1 : 0, + static_cast(diagnosticSlot.sequence), + diagnosticSlot.bytes); + } +#endif } slot.ready = false; slot.in_use = true; @@ -951,7 +986,13 @@ namespace gvfg::internal emit_event(PCIES2MM_EVENT_FRAME_LOSS); const uint32_t frameIndex = doneIndex % kDmaBufferCount; + const auto getFrameStarted = std::chrono::steady_clock::now(); const int ret = get_frame(channel, frameIndex, slotData, bytes); + const double getFrameMs = std::chrono::duration( + std::chrono::steady_clock::now() - getFrameStarted).count(); + if (getFrameMs >= 10.0) + PCIES2MM_LOG("slow GET_FRAME: done_index=%u frame_index=%u elapsed_ms=%.3f ret=%d", + doneIndex, frameIndex, getFrameMs, ret); if (!running_) return; if (ret < 0 || static_cast(ret) != bytes) diff --git a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.h b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.h index 2866c72..81a6d9e 100644 --- a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.h +++ b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.h @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -125,6 +126,8 @@ namespace gvfg::internal uint64_t latest_sequence_ = 0; uint64_t delivered_sequence_ = 0; uint64_t wait_timeout_count_ = 0; + std::chrono::steady_clock::time_point active_delivery_started_{}; + std::chrono::steady_clock::time_point last_delivery_started_{}; bool stream_error_ = false; pcies2mm_stream_stats_t stats_{}; mutable std::string last_error_; diff --git a/sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp b/sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp index bfdab11..4c08676 100644 --- a/sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp +++ b/sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp @@ -1131,20 +1131,6 @@ bool D3DPreviewPipeline::ensure_preview_swapchain(int w, int h) clientH = h; } - ComPtr dxgiDev; - if (!d3d_ || FAILED(d3d_->QueryInterface(IID_PPV_ARGS(&dxgiDev))) || !dxgiDev) - return false; - - ComPtr adapter; - if (FAILED(dxgiDev->GetAdapter(&adapter)) || !adapter) - return false; - - ComPtr factory; - if (FAILED(adapter->GetParent(__uuidof(IDXGIFactory2), - reinterpret_cast(factory.GetAddressOf()))) || - !factory) - return false; - const int effectiveMode = (preview_swapchain_mode_ == GVFG_RENDER_PREVIEW_BITDEPTH_AUTO) ? ((preview_source_bit_depth_ >= 10) ? GVFG_RENDER_PREVIEW_BITDEPTH_10BIT : GVFG_RENDER_PREVIEW_BITDEPTH_8BIT) : preview_swapchain_mode_; @@ -1164,6 +1150,23 @@ bool D3DPreviewPipeline::ensure_preview_swapchain(int w, int h) if (!preview_swapchain_) { + // Factory discovery is needed only while creating a swapchain. It + // used to run for every preview frame even when the existing + // swapchain size and format were unchanged. + ComPtr dxgiDev; + if (FAILED(d3d_->QueryInterface(IID_PPV_ARGS(&dxgiDev))) || !dxgiDev) + return false; + + ComPtr adapter; + if (FAILED(dxgiDev->GetAdapter(&adapter)) || !adapter) + return false; + + ComPtr factory; + if (FAILED(adapter->GetParent(__uuidof(IDXGIFactory2), + reinterpret_cast(factory.GetAddressOf()))) || + !factory) + return false; + DXGI_SWAP_CHAIN_DESC1 sd{}; sd.Width = (UINT)clientW; sd.Height = (UINT)clientH;