@@ -18,7 +18,10 @@ namespace winrt::StyleTransferEffectCpp::implementation
1818 Session (nullptr ),
1919 Binding (nullptr )
2020 {
21-
21+ for (int i = 0 ; i < swapChainEntryCount; i++)
22+ {
23+ bindings.push_back (std::make_unique<SwapChainEntry>());
24+ }
2225 }
2326
2427 IVectorView<VideoEncodingProperties> StyleTransferEffect::SupportedEncodingProperties () {
@@ -38,19 +41,18 @@ namespace winrt::StyleTransferEffectCpp::implementation
3841 OutputDebugString (L" Close\n " );
3942 if (Binding != nullptr ) Binding.Clear ();
4043 if (Session != nullptr ) Session.Close ();
41- if (bindings != nullptr ) {
42- for (int i = 0 ; i < swapChainEntryCount; i++) {
43- bindings[i].binding .Clear ();
44- }
44+ for (int i = 0 ; i < swapChainEntryCount; i++) {
45+ bindings[i]->binding .Clear ();
4546 }
4647 }
4748
4849 void StyleTransferEffect::SubmitEval (int swapchaindex, VideoFrame input, VideoFrame output) {
50+ auto currentBinding = bindings[0 ].get ();
4951 // VideoFrame outputTransformed = cachedOutput;
5052 // Different way of waiting for a swapchain index to finish?
5153 // Or would it be just setting the output to be a cached frame?
52- if (bindings[swapchaindex]. activetask == nullptr
53- || bindings[swapchaindex]. activetask .Status () != Windows::Foundation::AsyncStatus::Started)
54+ if (currentBinding-> activetask == nullptr
55+ || currentBinding-> activetask .Status () != Windows::Foundation::AsyncStatus::Started)
5456 {
5557 OutputDebugString (L" PF Start new Eval " );
5658 std::wostringstream ss;
@@ -59,31 +61,41 @@ namespace winrt::StyleTransferEffectCpp::implementation
5961 OutputDebugString (L" | " );
6062
6163 // bind the input and the output buffers by name
62- bindings[swapchaindex]. binding .Bind (InputImageDescription, input);
64+ currentBinding-> binding .Bind (InputImageDescription, input);
6365 // submit an eval and wait for it to finish submitting work
64- std::lock_guard<mutex> guard{ Processing }; // Is this still happening inside of Complete?
65- bindings[swapchaindex].activetask = Session.EvaluateAsync (bindings[swapchaindex].binding , ss.str ().c_str ());
66- bindings[swapchaindex].activetask .Completed ([&, swapchaindex](auto && asyncInfo, winrt::Windows::Foundation::AsyncStatus const args) {
66+ {
67+ std::lock_guard<mutex> guard{ Processing };
68+ std::rotate (bindings.begin (), bindings.begin () + 1 , bindings.end ());
69+ finishedIdx = (finishedIdx - 1 + swapChainEntryCount) % swapChainEntryCount;
70+ }
71+
72+ currentBinding->activetask = Session.EvaluateAsync (currentBinding->binding , ss.str ().c_str ());
73+ currentBinding->activetask .Completed ([&, currentBinding](auto && asyncInfo, winrt::Windows::Foundation::AsyncStatus const args) {
6774 OutputDebugString (L" PF Eval completed |" );
6875 VideoFrame evalOutput = asyncInfo.GetResults ().Outputs ().Lookup (OutputImageDescription).try_as <VideoFrame>();
69- // second lock to protect shared resource of cachedOutputCopy ?
76+ int bindingIdx;
77+ {
78+ std::lock_guard<mutex> guard{ Processing };
79+ auto binding = std::find_if (bindings.begin (), bindings.end (), [currentBinding](const auto & b) {return b.get () == currentBinding; });
80+ bindingIdx = std::distance (bindings.begin (), binding);
81+ finishedIdx = bindingIdx >= finishedIdx ? bindingIdx : finishedIdx;
82+ }
83+ if (bindingIdx >= finishedIdx)
7084 {
71- // std::lock_guard<mutex> guard{ Copy };
7285 OutputDebugString (L" PF Copy | " );
73- evalOutput.CopyToAsync (bindings[swapchaindex]. outputCache );
86+ evalOutput.CopyToAsync (currentBinding-> outputCache );
7487 }
75- // cachedOutput = bindings[swapchaindex].outputCache;
76- finishedIdx = swapchaindex;
88+
7789 OutputDebugString (L" PF End " );
7890 });
7991 }
80- if (bindings[finishedIdx]. outputCache != nullptr ) {
92+ if (bindings[finishedIdx]-> outputCache != nullptr ) {
8193 std::wostringstream ss;
8294 ss << finishedIdx;
83- std::lock_guard<mutex> guard{ Copy };
95+ // std::lock_guard<mutex> guard{ Copy };
8496 OutputDebugString (L" \n Start CopyAsync " );
8597 OutputDebugString (ss.str ().c_str ());
86- bindings[finishedIdx]. outputCache .CopyToAsync (output).get ();
98+ bindings[finishedIdx]-> outputCache .CopyToAsync (output).get ();
8799 OutputDebugString (L" | Stop CopyAsync\n " );
88100 }
89101 // return without waiting for the submit to finish, setup the completion handler
@@ -145,8 +157,8 @@ namespace winrt::StyleTransferEffectCpp::implementation
145157
146158 // Create set of bindings to cycle through
147159 for (int i = 0 ; i < swapChainEntryCount; i++) {
148- bindings[i]. binding = LearningModelBinding (Session);
149- bindings[i]. binding .Bind (OutputImageDescription, VideoFrame (Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720 , 720 ));
160+ bindings[i]-> binding = LearningModelBinding (Session);
161+ bindings[i]-> binding .Bind (OutputImageDescription, VideoFrame (Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720 , 720 ));
150162 }
151163 }
152164}
0 commit comments