Skip to content

Add support of NVIDIA DLSS4 flip-metering #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions PresentData/ETW/NV_DD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved
// SPDX-License-Identifier: MIT

#pragma once

namespace NvidiaDisplayDriver_Events {

enum class Keyword : uint64_t {
None = 0x00,
};

struct __declspec(uuid("{AE4F8626-8265-40D1-A70B-11B64240E8E9}")) GUID_STRUCT;
static const auto GUID = __uuidof(GUID_STRUCT);

// Event descriptors:
#define EVENT_DESCRIPTOR_DECL(name_, id_, version_, channel_, level_, opcode_, task_, keyword_) struct name_ { \
static uint16_t const Id = id_; \
static uint8_t const Version = version_; \
static uint8_t const Channel = channel_; \
static uint8_t const Level = level_; \
static uint8_t const Opcode = opcode_; \
static uint16_t const Task = task_; \
static Keyword const Keyword = (Keyword) keyword_; \
};

EVENT_DESCRIPTOR_DECL(FlipRequest, 0x0001, 0x00, 0x13, 0x04, 0x0a, 0x0001, 0x1000000000000000)
#undef EVENT_DESCRIPTOR_DECL
}
79 changes: 79 additions & 0 deletions PresentData/NvidiaTraceConsumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved
// SPDX-License-Identifier: MIT

#include "PresentMonTraceConsumer.hpp"
#include "NvidiaTraceConsumer.hpp"

NVTraceConsumer::NVTraceConsumer()
{
// Nothing to do
}

NVTraceConsumer::~NVTraceConsumer()
{
// Nothing to do
}

void NVTraceConsumer::HandleNvidiaDisplayDriverEvent(EVENT_RECORD* const pEventRecord, PMTraceConsumer* const pmConsumer)
{
enum {
FlipRequest = 1
};

auto const& hdr = pEventRecord->EventHeader;
switch (hdr.EventDescriptor.Id) {
case FlipRequest: {
auto alloc = pmConsumer->mMetadata.GetEventData<uint64_t>(pEventRecord, L"alloc");
auto vidPnSourceId = pmConsumer->mMetadata.GetEventData<uint32_t>(pEventRecord, L"vidPnSourceId");
auto& lastFlipTime = mLastFlipTimeByHead[vidPnSourceId];
auto ts = pmConsumer->mMetadata.GetEventData<uint64_t>(pEventRecord, L"ts");
auto token = pmConsumer->mMetadata.GetEventData<uint32_t>(pEventRecord, L"token");
uint64_t proposedFlipTime = 0;
uint64_t delay = 0;

if (token == mLastFlipToken) {
return;
}
mLastFlipToken = token;

if (alloc == 0) {
assert(!delay);
assert(ts);
// proposedFlipTime in number of ticks
proposedFlipTime = ts;
auto t1 = *(uint64_t*)&hdr.TimeStamp;
if (proposedFlipTime >= t1) {
delay = proposedFlipTime - t1;
}

if (proposedFlipTime && lastFlipTime && (proposedFlipTime < lastFlipTime)) {
delay = lastFlipTime - proposedFlipTime;
proposedFlipTime = lastFlipTime;
}
}

lastFlipTime = proposedFlipTime;

{
NvFlipRequest flipRequest;
flipRequest.FlipDelay = delay;
flipRequest.FlipToken = token;
mNvFlipRequestByThreadId.emplace(hdr.ThreadId, flipRequest);
}
break;
}
default:
break;
}
}

void NVTraceConsumer::ApplyFlipDelay(PresentEvent* present, uint32_t threadId)
{
auto flipIter = mNvFlipRequestByThreadId.find(threadId);
if (flipIter != mNvFlipRequestByThreadId.end()) {
present->FlipDelay = flipIter->second.FlipDelay;
present->FlipToken = flipIter->second.FlipToken;
// Clear the map (we want the whole map cleared, not just the element with the thread)
mNvFlipRequestByThreadId.clear();
}
}
37 changes: 37 additions & 0 deletions PresentData/NvidiaTraceConsumer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved
// SPDX-License-Identifier: MIT
#pragma once

#ifndef NOMINMAX
#define NOMINMAX
#endif

#include <unordered_map>
#include <windows.h>
#include <evntcons.h> // must include after windows.h

struct NvFlipRequest {
uint64_t FlipDelay;
uint32_t FlipToken;
};

struct NVTraceConsumer
{
NVTraceConsumer();
~NVTraceConsumer();

NVTraceConsumer(const NVTraceConsumer&) = delete;
NVTraceConsumer& operator=(const NVTraceConsumer&) = delete;
NVTraceConsumer(NVTraceConsumer&&) = delete;
NVTraceConsumer& operator=(NVTraceConsumer&&) = delete;

// ThreaId -> NV FlipRequest
std::unordered_map<uint32_t, NvFlipRequest> mNvFlipRequestByThreadId;
// vidPnSourceId -> flip qpcTime
std::unordered_map<uint32_t, uint64_t> mLastFlipTimeByHead;

void HandleNvidiaDisplayDriverEvent(EVENT_RECORD* const pEventRecord, PMTraceConsumer* const pmConsumer);
void ApplyFlipDelay(PresentEvent* present, uint32_t threadId);

uint32_t mLastFlipToken = 0;
};
3 changes: 3 additions & 0 deletions PresentData/PresentData.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,17 @@
<ClInclude Include="ETW\Microsoft_Windows_Win32k.h" />
<ClInclude Include="ETW\NT_Process.h" />
<ClInclude Include="Debug.hpp" />
<ClInclude Include="ETW\NV_DD.h" />
<ClInclude Include="GpuTrace.hpp" />
<ClInclude Include="NvidiaTraceConsumer.hpp" />
<ClInclude Include="PresentMonTraceConsumer.hpp" />
<ClInclude Include="TraceConsumer.hpp" />
<ClInclude Include="PresentMonTraceSession.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Debug.cpp" />
<ClCompile Include="GpuTrace.cpp" />
<ClCompile Include="NvidiaTraceConsumer.cpp" />
<ClCompile Include="PresentMonTraceConsumer.cpp" />
<ClCompile Include="TraceConsumer.cpp" />
<ClCompile Include="PresentMonTraceSession.cpp" />
Expand Down
5 changes: 5 additions & 0 deletions PresentData/PresentData.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@
<ClInclude Include="ETW\Microsoft_Windows_Dwm_Core_Win7.h">
<Filter>ETW</Filter>
</ClInclude>
<ClInclude Include="ETW\NV_DD.h">
<Filter>ETW</Filter>
</ClInclude>
<ClInclude Include="NvidiaTraceConsumer.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Debug.cpp" />
<ClCompile Include="PresentMonTraceConsumer.cpp" />
<ClCompile Include="TraceConsumer.cpp" />
<ClCompile Include="PresentMonTraceSession.cpp" />
<ClCompile Include="GpuTrace.cpp" />
<ClCompile Include="NvidiaTraceConsumer.cpp" />
</ItemGroup>
<ItemGroup>
<Filter Include="ETW">
Expand Down
12 changes: 11 additions & 1 deletion PresentData/PresentMonTraceConsumer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2017-2024 Intel Corporation
// Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved
// SPDX-License-Identifier: MIT

#include "PresentMonTraceConsumer.hpp"
Expand Down Expand Up @@ -930,6 +931,9 @@ void PMTraceConsumer::HandleDXGKEvent(EVENT_RECORD* pEventRecord)
auto present = FindPresentBySubmitSequence(submitSequence);
if (present != nullptr) {

// Apply Nvidia FlipDelay, if any, to the presentEvent
mNvTraceConsumer.ApplyFlipDelay(present.get(), hdr.ThreadId);

// Complete the GPU tracking for this frame.
//
// For some present modes (e.g., Hardware_Legacy_Flip) this may be
Expand All @@ -952,7 +956,7 @@ void PMTraceConsumer::HandleDXGKEvent(EVENT_RECORD* pEventRecord)
// this the present screen time.
if (FlipEntryStatusAfterFlip != (uint32_t) Microsoft_Windows_DxgKrnl::FlipEntryStatus::FlipWaitHSync) {

SetScreenTime(present, hdr.TimeStamp.QuadPart);
SetScreenTime(present, hdr.TimeStamp.QuadPart + present->FlipDelay);

if (present->PresentMode == PresentMode::Hardware_Legacy_Flip) {
CompletePresent(present);
Expand Down Expand Up @@ -1353,6 +1357,12 @@ void PMTraceConsumer::HandleDXGKEvent(EVENT_RECORD* pEventRecord)
assert(!mFilteredEvents); // Assert that filtering is working if expected
}


void PMTraceConsumer::HandleNvidiaDisplayDriverEvent(EVENT_RECORD* pEventRecord)
{
mNvTraceConsumer.HandleNvidiaDisplayDriverEvent(pEventRecord, this);
}

void PMTraceConsumer::HandleWin7DxgkBlt(EVENT_RECORD* pEventRecord)
{
using namespace Microsoft_Windows_DxgKrnl::Win7;
Expand Down
8 changes: 8 additions & 0 deletions PresentData/PresentMonTraceConsumer.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2017-2024 Intel Corporation
// Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved
// SPDX-License-Identifier: MIT
#pragma once

Expand All @@ -24,6 +25,7 @@
#include "Debug.hpp"
#include "GpuTrace.hpp"
#include "TraceConsumer.hpp"
#include "NvidiaTraceConsumer.hpp"
#include "../IntelPresentMon/CommonUtilities/Hash.h"

// PresentMode represents the different paths a present can take on windows.
Expand Down Expand Up @@ -271,6 +273,9 @@ struct PresentEvent {
// until a PresentFrameType_Info event with a different FrameId).
bool WaitingForFrameId;

// Data from NV DisplayDriver event
uint64_t FlipDelay = 0;
uint32_t FlipToken = 0;

PresentEvent();
PresentEvent(uint32_t fid);
Expand Down Expand Up @@ -487,6 +492,8 @@ struct PMTraceConsumer

std::unordered_map<uint32_t, InputData> mRetrievedInput; // ProcessID -> InputData<InputTime, InputType, isMouseClick>

// Trace consumer that handles events coming from Nvidia DisplayDriver
NVTraceConsumer mNvTraceConsumer;

// -------------------------------------------------------------------------------------------
// Functions for decoding ETW and analysing process and present events.
Expand Down Expand Up @@ -516,6 +523,7 @@ struct PMTraceConsumer
void HandleDWMEvent(EVENT_RECORD* pEventRecord);
void HandleMetadataEvent(EVENT_RECORD* pEventRecord);
void HandleIntelPresentMonEvent(EVENT_RECORD* pEventRecord);
void HandleNvidiaDisplayDriverEvent(EVENT_RECORD* pEventRecord);

void HandleWin7DxgkBlt(EVENT_RECORD* pEventRecord);
void HandleWin7DxgkFlip(EVENT_RECORD* pEventRecord);
Expand Down
15 changes: 15 additions & 0 deletions PresentData/PresentMonTraceSession.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (C) 2017-2024 Intel Corporation
// Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved
// SPDX-License-Identifier: MIT

#include "Debug.hpp"
#include "PresentMonTraceConsumer.hpp"
#include "PresentMonTraceSession.hpp"
#include "NvidiaTraceConsumer.hpp"

#include "ETW/Microsoft_Windows_D3D9.h"
#include "ETW/Microsoft_Windows_Dwm_Core.h"
Expand All @@ -16,6 +18,7 @@
#include "ETW/Microsoft_Windows_Win32k.h"
#include "ETW/NT_Process.h"
#include "ETW/Intel_PresentMon.h"
#include "ETW/NV_DD.h"

namespace {

Expand Down Expand Up @@ -280,6 +283,7 @@ void DisableProviders(TRACEHANDLE sessionHandle)
status = EnableTraceEx2(sessionHandle, &Microsoft_Windows_DxgKrnl::Win7::GUID, EVENT_CONTROL_CODE_DISABLE_PROVIDER, 0, 0, 0, 0, nullptr);
status = EnableTraceEx2(sessionHandle, &Microsoft_Windows_Kernel_Process::GUID, EVENT_CONTROL_CODE_DISABLE_PROVIDER, 0, 0, 0, 0, nullptr);
status = EnableTraceEx2(sessionHandle, &Microsoft_Windows_Win32k::GUID, EVENT_CONTROL_CODE_DISABLE_PROVIDER, 0, 0, 0, 0, nullptr);
status = EnableTraceEx2(sessionHandle, &NvidiaDisplayDriver_Events::GUID, EVENT_CONTROL_CODE_DISABLE_PROVIDER, 0, 0, 0, 0, nullptr);
}

template<
Expand Down Expand Up @@ -363,6 +367,10 @@ void CALLBACK EventRecordCallback(EVENT_RECORD* pEventRecord)
session->mPMConsumer->HandleWin7DxgkMMIOFlip(pEventRecord);
return;
}
if (hdr.ProviderId == NvidiaDisplayDriver_Events::GUID) {
session->mPMConsumer->HandleNvidiaDisplayDriverEvent(pEventRecord);
return;
}
}

if constexpr (TRACK_PRESENTMON) {
Expand Down Expand Up @@ -777,6 +785,13 @@ ULONG EnableProvidersListing(
if (status != ERROR_SUCCESS) return status;
}

// Nvidia_DisplayDriver
//
provider.ClearFilter();
provider.AddEvent<NvidiaDisplayDriver_Events::FlipRequest>();
status = provider.Enable(sessionHandle, NvidiaDisplayDriver_Events::GUID);
if (status != ERROR_SUCCESS) return status;

return ERROR_SUCCESS;
}

1 change: 1 addition & 0 deletions PresentData/PresentMonTraceSession.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2017-2024 Intel Corporation
// Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved
// SPDX-License-Identifier: MIT

struct PMTraceConsumer;
Expand Down
29 changes: 17 additions & 12 deletions PresentMon/CsvOutput.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2017-2024 Intel Corporation
// Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved
// SPDX-License-Identifier: MIT

#include "PresentMon.hpp"
Expand Down Expand Up @@ -156,7 +157,8 @@ void WriteCsvHeader<FrameMetrics1>(FILE* fp)
L",PresentMode"
L",msUntilRenderComplete"
L",msUntilDisplayed"
L",msBetweenDisplayChange");
L",msBetweenDisplayChange"
L",msFlipDelay");
}
if (args.mTrackGPU) {
fwprintf(fp, L",msUntilRenderStart"
Expand Down Expand Up @@ -221,11 +223,12 @@ void WriteCsvRow<FrameMetrics1>(
fwprintf(fp, L",%.*lf,%.*lf", DBL_DIG - 1, metrics.msInPresentApi,
DBL_DIG - 1, metrics.msBetweenPresents);
if (args.mTrackDisplay) {
fwprintf(fp, L",%d,%hs,%.*lf,%.*lf,%.*lf", p.SupportsTearing,
PresentModeToString(p.PresentMode),
DBL_DIG - 1, metrics.msUntilRenderComplete,
DBL_DIG - 1, metrics.msUntilDisplayed,
DBL_DIG - 1, metrics.msBetweenDisplayChange);
fwprintf(fp, L",%d,%hs,%.*lf,%.*lf,%.*lf,%.*lf", p.SupportsTearing,
PresentModeToString(p.PresentMode),
DBL_DIG - 1, metrics.msUntilRenderComplete,
DBL_DIG - 1, metrics.msUntilDisplayed,
DBL_DIG - 1, metrics.msBetweenDisplayChange,
DBL_DIG - 1, metrics.msFlipDelay);
}
if (args.mTrackGPU) {
fwprintf(fp, L",%.*lf,%.*lf", DBL_DIG - 1, metrics.msUntilRenderStart,
Expand Down Expand Up @@ -306,7 +309,8 @@ void WriteCsvHeader<FrameMetrics>(FILE* fp)
fwprintf(fp, L",DisplayLatency"
L",DisplayedTime"
L",AnimationError"
L",AnimationTime");
L",AnimationTime"
L",FlipDelay");
}
if (args.mTrackInput) {
fwprintf(fp, L",AllInputToPhotonLatency");
Expand Down Expand Up @@ -397,12 +401,13 @@ void WriteCsvRow<FrameMetrics>(
}
if (args.mTrackDisplay) {
if (metrics.mDisplayedTime == 0.0) {
fwprintf(fp, L",NA,NA,NA,NA");
fwprintf(fp, L",NA,NA,NA,NA,NA");
} else {
fwprintf(fp, L",%.4lf,%.4lf,%.4lf,%.4lf", metrics.mDisplayLatency,
metrics.mDisplayedTime,
metrics.mAnimationError,
metrics.mAnimationTime);
fwprintf(fp, L",%.4lf,%.4lf,%.4lf,%.4lf,%.4lf", metrics.mDisplayLatency,
metrics.mDisplayedTime,
metrics.mAnimationError,
metrics.mAnimationTime,
metrics.mFlipDelay);
}
}
if (args.mTrackInput) {
Expand Down
Loading