Skip to content

Commit c09c4f6

Browse files
swapchain but cached version seems to be too old
1 parent c4de9cc commit c09c4f6

File tree

3 files changed

+22
-50
lines changed

3 files changed

+22
-50
lines changed

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,8 @@ await DispatcherHelper.RunAsync(() =>
6767

6868
// Media capture properties
6969
public Windows.Media.Capture.MediaCapture _mediaCapture;
70-
private List<MediaFrameSourceGroup> _mediaFrameSourceGroupList;
71-
private MediaFrameSourceGroup _selectedMediaFrameSourceGroup;
7270
private bool isPreviewing;
7371
private DisplayRequest displayRequest = new DisplayRequest();
74-
private int numFrames = 0;
7572

7673
// Style transfer effect properties
7774
private LearningModel m_model = null;
@@ -104,7 +101,6 @@ public AppModel CurrentApp
104101
{
105102
get { return _appModel; }
106103
}
107-
108104
public float RenderFPS
109105
{
110106
get { return (float)Math.Round(_renderFPS, 2); }
@@ -385,7 +381,6 @@ public async Task ChangeLiveStream()
385381
var settings = new MediaCaptureInitializationSettings
386382
{
387383
VideoDeviceId = device.Id,
388-
SourceGroup = _selectedMediaFrameSourceGroup,
389384
PhotoCaptureSource = PhotoCaptureSource.Auto,
390385
MemoryPreference = UseGpu ? MediaCaptureMemoryPreference.Auto : MediaCaptureMemoryPreference.Cpu,
391386
StreamingCaptureMode = StreamingCaptureMode.Video,

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,15 @@ namespace winrt::StyleTransferEffectCpp::implementation
3838
OutputDebugString(L"Close\n");
3939
if (Binding != nullptr) Binding.Clear();
4040
if (Session != nullptr) Session.Close();
41-
}
42-
43-
std::wstring index(const std::thread::id id)
44-
{
45-
static std::size_t nextindex = 0;
46-
static std::mutex my_mutex;
47-
static std::map<std::thread::id, std::string> ids;
48-
std::lock_guard<std::mutex> lock(my_mutex);
49-
if (ids.find(id) == ids.end()) {
50-
ids[id] = std::to_string((int)nextindex);
51-
nextindex++;
41+
if (bindings != nullptr) {
42+
for (int i = 0; i < swapChainEntryCount; i++) {
43+
bindings[i].binding.Clear();
44+
}
5245
}
53-
return std::wstring(ids[id].begin(), ids[id].end());
5446
}
5547

56-
void StyleTransferEffect::SubmitEval(int swapchaindex, VideoFrame input, VideoFrame output, VideoFrame temp) {
48+
void StyleTransferEffect::SubmitEval(int swapchaindex, VideoFrame input, VideoFrame output) {
49+
//VideoFrame outputTransformed = cachedOutput;
5750
// Different way of waiting for a swapchain index to finish?
5851
// Or would it be just setting the output to be a cached frame?
5952
if (bindings[swapchaindex].activetask == nullptr
@@ -62,7 +55,8 @@ namespace winrt::StyleTransferEffectCpp::implementation
6255
OutputDebugString(L"PF Start new Eval ");
6356
std::wostringstream ss;
6457
ss << swapchaindex;
65-
OutputDebugString(ss.str().c_str());
58+
auto idx = ss.str().c_str();
59+
OutputDebugString(idx);
6660
OutputDebugString(L" | ");
6761

6862
// bind the input and the output buffers by name
@@ -72,51 +66,37 @@ namespace winrt::StyleTransferEffectCpp::implementation
7266
bindings[swapchaindex].activetask = Session.EvaluateAsync(bindings[swapchaindex].binding, ss.str().c_str());
7367
bindings[swapchaindex].activetask.Completed([&](auto&& asyncInfo, winrt::Windows::Foundation::AsyncStatus const args) {
7468
OutputDebugString(L"PF Eval completed | ");
75-
//OutputDebugString(ss.str().c_str());
76-
//OutputDebugString(L" | ");
77-
78-
VideoFrame output = asyncInfo.GetResults().Outputs().Lookup(OutputImageDescription).try_as<VideoFrame>();
79-
OutputDebugString(L"PF Copy | ");
80-
//OutputDebugString(ss.str().c_str());
81-
//OutputDebugString(L" | ");
82-
// second lock to protect shared resource of cachedOutputCopy
83-
output.CopyToAsync(cachedOutputCopy);
69+
VideoFrame evalOutput = asyncInfo.GetResults().Outputs().Lookup(OutputImageDescription).try_as<VideoFrame>();
70+
// second lock to protect shared resource of cachedOutputCopy ?
8471
{
85-
cachedOutput = cachedOutputCopy;
72+
std::lock_guard<mutex> guard{ Copy };
73+
OutputDebugString(L"PF Copy | ");
74+
evalOutput.CopyToAsync(cachedOutputCopy).get();
8675
}
76+
cachedOutput = cachedOutputCopy;
77+
8778
OutputDebugString(L"PF End ");
8879
//OutputDebugString(ss.str().c_str());
8980
//OutputDebugString(L"\n");
9081
});
9182
}
92-
if (temp != nullptr) {
83+
if (cachedOutput != nullptr) {
84+
std::lock_guard<mutex> guard{ Copy };
9385
OutputDebugString(L"\nStart CopyAsync | ");
94-
temp.CopyToAsync(output).get(); // Make sure using reference, ie. shows up in context.OutputFrame()
86+
cachedOutput.CopyToAsync(output).get();
9587
OutputDebugString(L"Stop CopyAsync\n");
9688
}
9789
// return without waiting for the submit to finish, setup the completion handler
9890
}
9991

100-
// Whenever an evaluate async is finished, call this function
101-
void StyleTransferEffect::EvaluateComplete(VideoFrame evalFrame) {
102-
OutputDebugString(L"PF Copy | ");
103-
// second lock to protect shared resource of cachedOutputCopy
104-
evalFrame.CopyToAsync(cachedOutputCopy);
105-
{
106-
cachedOutput = cachedOutputCopy;
107-
}
108-
OutputDebugString(L"PF End\n ");
109-
}
110-
11192
void StyleTransferEffect::ProcessFrame(ProcessVideoFrameContext context) {
11293
OutputDebugString(L"PF Start | ");
11394
//OutputDebugString(index(thread().get_id()).c_str());
11495
auto now = std::chrono::high_resolution_clock::now();
11596
VideoFrame inputFrame = context.InputFrame();
11697
VideoFrame outputFrame = context.OutputFrame();
117-
VideoFrame temp = cachedOutput;
11898

119-
SubmitEval(swapChainIndex, inputFrame, outputFrame, temp);
99+
SubmitEval(swapChainIndex, inputFrame, outputFrame);
120100

121101
swapChainIndex = (++swapChainIndex) % swapChainEntryCount; // move on to the next entry after each call to PF.
122102
auto timePassed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - now);
@@ -154,7 +134,6 @@ namespace winrt::StyleTransferEffectCpp::implementation
154134
for (int i = 0; i < swapChainEntryCount; i++) {
155135
bindings[i].binding = LearningModelBinding(Session);
156136
bindings[i].binding.Bind(OutputImageDescription, VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720));
157-
158137
}
159138
}
160139
}

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.h

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

2422
struct StyleTransferEffect : StyleTransferEffectT<StyleTransferEffect>
@@ -36,7 +34,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
3634
void ProcessFrame(ProcessVideoFrameContext);
3735
void SetEncodingProperties(VideoEncodingProperties, IDirect3DDevice);
3836
void SetProperties(IPropertySet);
39-
void SubmitEval(int, VideoFrame, VideoFrame, VideoFrame);
37+
void SubmitEval(int, VideoFrame, VideoFrame);
4038

4139
private:
4240
LearningModelSession Session;
@@ -56,7 +54,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
5654
hstring InputImageDescription;
5755
hstring OutputImageDescription;
5856
int swapChainIndex = 0;
59-
static const int swapChainEntryCount = 10;
57+
static const int swapChainEntryCount = 5;
6058
SwapChainEntry bindings[swapChainEntryCount];
6159

6260
void EvaluateComplete(VideoFrame);

0 commit comments

Comments
 (0)