Skip to content

Commit 1fe3b3c

Browse files
finishedIdx to show most recent cached output
1 parent c09c4f6 commit 1fe3b3c

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,36 @@ namespace winrt::StyleTransferEffectCpp::implementation
5555
OutputDebugString(L"PF Start new Eval ");
5656
std::wostringstream ss;
5757
ss << swapchaindex;
58-
auto idx = ss.str().c_str();
59-
OutputDebugString(idx);
58+
OutputDebugString(ss.str().c_str());
6059
OutputDebugString(L" | ");
6160

6261
// bind the input and the output buffers by name
6362
bindings[swapchaindex].binding.Bind(InputImageDescription, input);
6463
// submit an eval and wait for it to finish submitting work
6564
std::lock_guard<mutex> guard{ Processing }; // Is this still happening inside of Complete?
6665
bindings[swapchaindex].activetask = Session.EvaluateAsync(bindings[swapchaindex].binding, ss.str().c_str());
67-
bindings[swapchaindex].activetask.Completed([&](auto&& asyncInfo, winrt::Windows::Foundation::AsyncStatus const args) {
68-
OutputDebugString(L"PF Eval completed | ");
66+
bindings[swapchaindex].activetask.Completed([&, swapchaindex](auto&& asyncInfo, winrt::Windows::Foundation::AsyncStatus const args) {
67+
OutputDebugString(L"PF Eval completed |");
6968
VideoFrame evalOutput = asyncInfo.GetResults().Outputs().Lookup(OutputImageDescription).try_as<VideoFrame>();
7069
// second lock to protect shared resource of cachedOutputCopy ?
7170
{
72-
std::lock_guard<mutex> guard{ Copy };
71+
//std::lock_guard<mutex> guard{ Copy };
7372
OutputDebugString(L"PF Copy | ");
74-
evalOutput.CopyToAsync(cachedOutputCopy).get();
73+
evalOutput.CopyToAsync(bindings[swapchaindex].outputCache);
7574
}
76-
cachedOutput = cachedOutputCopy;
77-
75+
//cachedOutput = bindings[swapchaindex].outputCache;
76+
finishedIdx = swapchaindex;
7877
OutputDebugString(L"PF End ");
79-
//OutputDebugString(ss.str().c_str());
80-
//OutputDebugString(L"\n");
8178
});
8279
}
83-
if (cachedOutput != nullptr) {
80+
if (bindings[finishedIdx].outputCache != nullptr) {
81+
std::wostringstream ss;
82+
ss << finishedIdx;
8483
std::lock_guard<mutex> guard{ Copy };
85-
OutputDebugString(L"\nStart CopyAsync | ");
86-
cachedOutput.CopyToAsync(output).get();
87-
OutputDebugString(L"Stop CopyAsync\n");
84+
OutputDebugString(L"\nStart CopyAsync ");
85+
OutputDebugString(ss.str().c_str());
86+
bindings[finishedIdx].outputCache.CopyToAsync(output).get();
87+
OutputDebugString(L" | Stop CopyAsync\n");
8888
}
8989
// return without waiting for the submit to finish, setup the completion handler
9090
}
@@ -97,6 +97,19 @@ namespace winrt::StyleTransferEffectCpp::implementation
9797
VideoFrame outputFrame = context.OutputFrame();
9898

9999
SubmitEval(swapChainIndex, inputFrame, outputFrame);
100+
// Go thorugh swapchain, find most recently completed entry
101+
//context.OutputFrame() = bindings[swapChainIndex].outputCache;
102+
/*for (int i = 0; i < swapChainEntryCount; i++) {
103+
int index = (swapChainIndex - i) % swapChainEntryCount;
104+
if (index < 0) index += swapChainEntryCount;
105+
if (bindings[index].activetask != NULL) {
106+
auto entry = bindings[index].activetask;
107+
if (entry.Status() == Windows::Foundation::AsyncStatus::Completed) {
108+
OutputDebugString(L"PF Find recent | ");
109+
outputFrame = bindings[index].outputCache;
110+
}
111+
}
112+
}*/
100113

101114
swapChainIndex = (++swapChainIndex) % swapChainEntryCount; // move on to the next entry after each call to PF.
102115
auto timePassed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - now);

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ namespace winrt::StyleTransferEffectCpp::implementation
1414
struct SwapChainEntry {
1515
LearningModelBinding binding;
1616
Windows::Foundation::IAsyncOperation<LearningModelEvaluationResult> activetask;
17+
VideoFrame outputCache;
1718
SwapChainEntry() :
1819
binding(nullptr),
19-
activetask(nullptr) {}
20+
activetask(nullptr),
21+
outputCache(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)) {}
2022
};
2123

2224
struct StyleTransferEffect : StyleTransferEffectT<StyleTransferEffect>
@@ -56,8 +58,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
5658
int swapChainIndex = 0;
5759
static const int swapChainEntryCount = 5;
5860
SwapChainEntry bindings[swapChainEntryCount];
59-
60-
void EvaluateComplete(VideoFrame);
61+
int finishedIdx = 0;
6162
};
6263
}
6364

0 commit comments

Comments
 (0)