@@ -110,6 +110,25 @@ HRESULT LoadModel(LearningModel& model, const std::wstring& path, bool capturePe
110110 return S_OK;
111111}
112112
113+ #ifdef DXCORE_SUPPORTED_BUILD
114+ HRESULT CreateDXGIFactory2SEH (void ** dxgiFactory)
115+ {
116+ // Recover from delay-load module failure.
117+ HRESULT hr;
118+ __try
119+ {
120+ hr = CreateDXGIFactory2 (0 , __uuidof (IDXGIFactory4), dxgiFactory);
121+ }
122+ __except (GetExceptionCode () == VcppException (ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND)
123+ ? EXCEPTION_EXECUTE_HANDLER
124+ : EXCEPTION_CONTINUE_SEARCH)
125+ {
126+ hr = HRESULT_FROM_WIN32 (ERROR_MOD_NOT_FOUND);
127+ }
128+ return hr;
129+ }
130+ #endif
131+
113132HRESULT CreateSession (LearningModelSession& session, IDirect3DDevice& winrtDevice, LearningModel& model,
114133 CommandLineArgs& args, OutputHelper& output, DeviceType deviceType,
115134 DeviceCreationLocation deviceCreationLocation, Profiler<WINML_MODEL_TEST_PERF>& profiler)
@@ -253,15 +272,26 @@ HRESULT CreateSession(LearningModelSession& session, IDirect3DDevice& winrtDevic
253272 {
254273 d3dFeatureLevel = D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0;
255274 com_ptr<IDXGIFactory4> dxgiFactory4;
256- THROW_IF_FAILED (CreateDXGIFactory2 (0 , __uuidof (IDXGIFactory4), dxgiFactory4.put_void ()));
257-
258- // If DXGI factory creation was successful then get the IDXGIAdapter from the LUID acquired from the
259- // selectedAdapter
260- LUID adapterLuid;
261- THROW_IF_FAILED (spAdapter->GetLUID (&adapterLuid));
262- THROW_IF_FAILED (
263- dxgiFactory4->EnumAdapterByLuid (adapterLuid, __uuidof (IDXGIAdapter), spDxgiAdapter.put_void ()));
264- pAdapter = spDxgiAdapter.get ();
275+ HRESULT hr;
276+ try
277+ {
278+ hr = CreateDXGIFactory2SEH (dxgiFactory4.put_void ());
279+ }
280+ catch (...)
281+ {
282+ hr = E_FAIL;
283+ }
284+ if (hr == S_OK)
285+ {
286+ // If DXGI factory creation was successful then get the IDXGIAdapter from the LUID acquired from
287+ // the selectedAdapter
288+ std::cout << " Using DXGI for adapter creation.." << std::endl;
289+ LUID adapterLuid;
290+ THROW_IF_FAILED (spAdapter->GetLUID (&adapterLuid));
291+ THROW_IF_FAILED (dxgiFactory4->EnumAdapterByLuid (adapterLuid, __uuidof (IDXGIAdapter),
292+ spDxgiAdapter.put_void ()));
293+ pAdapter = spDxgiAdapter.get ();
294+ }
265295 }
266296 else
267297 {
@@ -559,20 +589,72 @@ std::vector<DeviceCreationLocation> FetchDeviceCreationLocations(const CommandLi
559589 return deviceCreationLocations;
560590}
561591
592+ #if defined(_AMD64_)
593+ void StartPIXCapture (OutputHelper& output)
594+ {
595+ __try
596+ {
597+ // PIX markers only work on AMD64
598+ if (output.GetGraphicsAnalysis ().get ())
599+ {
600+ // If PIX tool is attached to WinMLRunner then begin capture. First capture will include
601+ // session creation, first iteration bind and first iteration evaluate.
602+ output.GetGraphicsAnalysis ()->BeginCapture ();
603+ }
604+ }
605+ __except (GetExceptionCode () == VcppException (ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND)
606+ ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
607+ {
608+ std::cout << " DXGI module not found." << std::endl;
609+ }
610+ }
611+
612+ void EndPIXCapture (OutputHelper& output)
613+ {
614+ __try
615+ {
616+ // PIX markers only work on AMD64
617+ if (output.GetGraphicsAnalysis ().get ())
618+ {
619+ // If PIX tool is attached to WinMLRunner then end capture.
620+ output.GetGraphicsAnalysis ()->EndCapture ();
621+ }
622+ }
623+ __except (GetExceptionCode () == VcppException (ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND)
624+ ? EXCEPTION_EXECUTE_HANDLER
625+ : EXCEPTION_CONTINUE_SEARCH)
626+ {
627+ std::cout << " DXGI module not found." << std::endl;
628+ }
629+ }
630+
631+ void PrintIfPIXToolAttached (OutputHelper& output)
632+ {
633+ __try
634+ {
635+ // PIX markers only work on AMD64
636+ // Check if PIX tool is attached to WinMLRunner
637+ // Try to acquire IDXGraphicsAnalysis - this only succeeds if PIX is attached
638+ if (SUCCEEDED (DXGIGetDebugInterface1 (0 , IID_PPV_ARGS (output.GetGraphicsAnalysis ().put ()))))
639+ {
640+ std::cout << " Detected PIX tool is attached to WinMLRunner" << std::endl;
641+ }
642+ }
643+ __except (GetExceptionCode () == VcppException (ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND)
644+ ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
645+ {
646+ std::cout << " DXGI module not found." << std::endl;
647+ }
648+ }
649+ #endif
562650int run (CommandLineArgs& args, Profiler<WINML_MODEL_TEST_PERF>& profiler) try
563651{
564652 // Initialize COM in a multi-threaded environment.
565653 winrt::init_apartment ();
566654 OutputHelper output (args.NumIterations ());
567655
568656#if defined(_AMD64_)
569- // PIX markers only work on AMD64
570- // Check if PIX tool is attached to WinMLRunner
571- // Try to acquire IDXGraphicsAnalysis - this only succeeds if PIX is attached
572- if (SUCCEEDED (DXGIGetDebugInterface1 (0 , IID_PPV_ARGS (output.GetGraphicsAnalysis ().put ()))))
573- {
574- std::cout << " Detected PIX tool is attached to WinMLRunner" << std::endl;
575- }
657+ PrintIfPIXToolAttached (output);
576658#endif
577659
578660 // Profiler is a wrapper class that captures and stores timing and memory usage data on the
@@ -618,13 +700,7 @@ int run(CommandLineArgs& args, Profiler<WINML_MODEL_TEST_PERF>& profiler) try
618700 for (auto deviceCreationLocation : deviceCreationLocations)
619701 {
620702#if defined(_AMD64_)
621- // PIX markers only work on AMD64
622- if (output.GetGraphicsAnalysis ().get ())
623- {
624- // If PIX tool is attached to WinMLRunner then begin capture. First capture will include
625- // session creation, first iteration bind and first iteration evaluate.
626- output.GetGraphicsAnalysis ()->BeginCapture ();
627- }
703+ StartPIXCapture (output);
628704#endif
629705 LearningModelSession session = nullptr ;
630706 IDirect3DDevice winrtDevice = nullptr ;
@@ -651,9 +727,9 @@ int run(CommandLineArgs& args, Profiler<WINML_MODEL_TEST_PERF>& profiler) try
651727 // If PIX tool was attached then capture already began for the first iteration before
652728 // session creation. This is to begin PIX capture for each iteration after the first
653729 // iteration.
654- if (i > 0 && output. GetGraphicsAnalysis () )
730+ if (i > 0 )
655731 {
656- output. GetGraphicsAnalysis ()-> BeginCapture ( );
732+ StartPIXCapture (output );
657733 }
658734#endif
659735 LearningModelBinding context (session);
@@ -684,12 +760,7 @@ int run(CommandLineArgs& args, Profiler<WINML_MODEL_TEST_PERF>& profiler) try
684760 }
685761 }
686762#if defined(_AMD64_)
687- // PIX markers only work on AMD64
688- if (output.GetGraphicsAnalysis ())
689- {
690- // If PIX tool is attached, then end the capture
691- output.GetGraphicsAnalysis ()->EndCapture ();
692- }
763+ EndPIXCapture (output);
693764#endif
694765 }
695766
0 commit comments