Skip to content

Commit 964064f

Browse files
Merge pull request #326 from GameTechDev/bug/flush-before-capture
flush stale frames that pile up before starting new capture
2 parents 8590065 + e37531f commit 964064f

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

IntelPresentMon/Core/source/pmon/PresentMon.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@
1010

1111
namespace p2c::pmon
1212
{
13+
using namespace ::pmapi;
14+
15+
class FrameEventFlusher
16+
{
17+
public:
18+
FrameEventFlusher(Session& sesh)
19+
{
20+
// register minimal query used for flushing frame events
21+
std::array queryElements{ PM_QUERY_ELEMENT{ PM_METRIC_PRESENT_MODE, PM_STAT_MID_POINT } };
22+
query_ = sesh.RegisterFrameQuery(queryElements);
23+
blobs_ = query_.MakeBlobContainer(50);
24+
}
25+
void Flush(ProcessTracker& tracker)
26+
{
27+
query_.ForEachConsume(tracker, blobs_, [](auto) {});
28+
}
29+
private:
30+
FrameQuery query_;
31+
BlobContainer blobs_;
32+
};
33+
1334
PresentMon::PresentMon(std::optional<std::string> namedPipeName, std::optional<std::string> sharedMemoryName, double window_in, double offset_in, uint32_t telemetrySamplePeriodMs_in)
1435
{
1536
const auto RemoveDoubleQuotes = [](std::string s) {
@@ -39,6 +60,9 @@ namespace p2c::pmon
3960
SetWindow(window_in);
4061
SetOffset(offset_in);
4162
SetGpuTelemetryPeriod(telemetrySamplePeriodMs_in);
63+
64+
// create flusher used to clear out piled-up old frame events before capture
65+
pFlusher = std::make_unique<FrameEventFlusher>(*pSession);
4266
}
4367
PresentMon::~PresentMon() = default;
4468
void PresentMon::StartTracking(uint32_t pid_)
@@ -126,6 +150,10 @@ namespace p2c::pmon
126150
std::shared_ptr<RawFrameDataWriter> PresentMon::MakeRawFrameDataWriter(std::wstring path,
127151
std::optional<std::wstring> statsPath, uint32_t pid, std::wstring procName)
128152
{
153+
// flush any buffered present events before starting capture
154+
pFlusher->Flush(processTracker);
155+
156+
// make the frame data writer
129157
return std::make_shared<RawFrameDataWriter>(std::move(path), processTracker, std::move(procName),
130158
selectedAdapter.value_or(1), *pSession, std::move(statsPath), *pIntrospectionRoot);
131159
}

IntelPresentMon/Core/source/pmon/PresentMon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace pmapi
1919
namespace p2c::pmon
2020
{
2121
class RawFrameDataWriter;
22+
class FrameEventFlusher;
2223

2324
class PresentMon
2425
{
@@ -49,6 +50,7 @@ namespace p2c::pmon
4950
double offset = -1.;
5051
uint32_t telemetrySamplePeriod = 0;
5152
std::unique_ptr<pmapi::Session> pSession;
53+
std::unique_ptr<FrameEventFlusher> pFlusher;
5254
std::shared_ptr<pmapi::intro::Root> pIntrospectionRoot;
5355
pmapi::ProcessTracker processTracker;
5456
std::optional<uint32_t> selectedAdapter;

IntelPresentMon/PresentMonAPIWrapper/FrameQuery.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ namespace pmapi
4646
Consume(tracker, blobs.GetFirst(), blobs.AcquireNumBlobsInRef_());
4747
}
4848

49+
size_t FrameQuery::ForEachConsume(ProcessTracker& tracker, BlobContainer& blobs, std::function<void(const uint8_t*)> frameHandler)
50+
{
51+
size_t nFramesProcessed = 0;
52+
do {
53+
Consume(tracker, blobs);
54+
const auto nPopulated = blobs.GetNumBlobsPopulated();
55+
for (uint32_t i = 0; i < nPopulated; i++) {
56+
frameHandler(blobs[i]);
57+
}
58+
nFramesProcessed += nPopulated;
59+
} while (blobs.AllBlobsPopulated());
60+
return nFramesProcessed;
61+
}
62+
4963
BlobContainer FrameQuery::MakeBlobContainer(uint32_t nBlobs) const
5064
{
5165
assert(!Empty());

IntelPresentMon/PresentMonAPIWrapper/FrameQuery.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "BlobContainer.h"
44
#include "ProcessTracker.h"
55
#include <span>
6+
#include <functional>
67

78
namespace pmapi
89
{
@@ -31,6 +32,9 @@ namespace pmapi
3132
// pBlobs: pointer to memory where frame query data is to be stored for consumed frames
3233
// NOTE: it is preferred to use above version of this function that takes in BlobContainer&
3334
void Consume(const ProcessTracker& tracker, uint8_t* pBlobs, uint32_t& numBlobsInOut);
35+
// consume frame events and invoke frameHandler for each frame consumed, setting active blob each time
36+
// will continue to call consume until all frames have been consumed from the queue
37+
size_t ForEachConsume(ProcessTracker& tracker, BlobContainer& blobs, std::function<void(const uint8_t*)> frameHandler);
3438
// create a blob container whose size is suited to fit this query
3539
// nBlobs: number of frames worth of data that the container can contain
3640
BlobContainer MakeBlobContainer(uint32_t nBlobs) const;

0 commit comments

Comments
 (0)