@@ -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" \n Start 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}
0 commit comments