|
| 1 | +#include "LearningModelDeviceHelper.h" |
| 2 | +#include "TypeHelper.h" |
| 3 | +#include "Common.h" |
| 4 | +#include "d3d11.h" |
| 5 | +#include "d3dx12.h" |
| 6 | +#include <Windows.Graphics.DirectX.Direct3D11.interop.h> |
| 7 | +#include "Windows.AI.MachineLearning.Native.h" |
| 8 | +#include <codecvt> |
| 9 | +using namespace winrt::Windows::Graphics::DirectX::Direct3D11; |
| 10 | + |
| 11 | +#ifdef DXCORE_SUPPORTED_BUILD |
| 12 | +HRESULT CreateDXGIFactory2SEH(void** dxgiFactory) |
| 13 | +{ |
| 14 | + // Recover from delay-load module failure. |
| 15 | + HRESULT hr; |
| 16 | + __try |
| 17 | + { |
| 18 | + hr = CreateDXGIFactory2(0, __uuidof(IDXGIFactory4), dxgiFactory); |
| 19 | + } |
| 20 | + __except (GetExceptionCode() == VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND) |
| 21 | + ? EXCEPTION_EXECUTE_HANDLER |
| 22 | + : EXCEPTION_CONTINUE_SEARCH) |
| 23 | + { |
| 24 | + hr = HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND); |
| 25 | + } |
| 26 | + return hr; |
| 27 | +} |
| 28 | +#endif |
| 29 | + |
| 30 | +void PopulateLearningModelDeviceList(CommandLineArgs& args, std::vector<LearningModelDeviceWithMetadata>& deviceList) |
| 31 | +{ |
| 32 | + std::vector<DeviceType> deviceTypes = args.FetchDeviceTypes(); |
| 33 | + std::vector<DeviceCreationLocation> deviceCreationLocations = args.FetchDeviceCreationLocations(); |
| 34 | + for (auto deviceType : deviceTypes) |
| 35 | + { |
| 36 | + for (auto deviceCreationLocation : deviceCreationLocations) |
| 37 | + { |
| 38 | + try |
| 39 | + { |
| 40 | +#ifdef DXCORE_SUPPORTED_BUILD |
| 41 | + const std::wstring& adapterName = args.GetGPUAdapterName(); |
| 42 | +#endif |
| 43 | + if (deviceCreationLocation == DeviceCreationLocation::UserD3DDevice && deviceType != DeviceType::CPU) |
| 44 | + { |
| 45 | + // Enumerate Adapters to pick the requested one. |
| 46 | + com_ptr<IDXGIFactory6> factory; |
| 47 | + HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory6), factory.put_void()); |
| 48 | + THROW_IF_FAILED(hr); |
| 49 | + |
| 50 | + com_ptr<IDXGIAdapter> adapter; |
| 51 | + switch (deviceType) |
| 52 | + { |
| 53 | + case DeviceType::DefaultGPU: |
| 54 | + hr = factory->EnumAdapterByGpuPreference(0, DXGI_GPU_PREFERENCE_UNSPECIFIED, |
| 55 | + __uuidof(IDXGIAdapter), adapter.put_void()); |
| 56 | + break; |
| 57 | + case DeviceType::MinPowerGPU: |
| 58 | + hr = factory->EnumAdapterByGpuPreference(0, DXGI_GPU_PREFERENCE_MINIMUM_POWER, |
| 59 | + __uuidof(IDXGIAdapter), adapter.put_void()); |
| 60 | + break; |
| 61 | + case DeviceType::HighPerfGPU: |
| 62 | + hr = factory->EnumAdapterByGpuPreference(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, |
| 63 | + __uuidof(IDXGIAdapter), adapter.put_void()); |
| 64 | + break; |
| 65 | + default: |
| 66 | + throw hresult(E_INVALIDARG); |
| 67 | + } |
| 68 | + THROW_IF_FAILED(hr); |
| 69 | + |
| 70 | + // Creating the device on the client and using it to create the video frame and initialize the |
| 71 | + // session makes sure that everything is on the same device. This usually avoids an expensive |
| 72 | + // cross-device and cross-videoframe copy via the VideoFrame pipeline. |
| 73 | + com_ptr<ID3D11Device> d3d11Device; |
| 74 | + hr = D3D11CreateDevice(adapter.get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, |
| 75 | + D3D11_CREATE_DEVICE_BGRA_SUPPORT, nullptr, 0, D3D11_SDK_VERSION, |
| 76 | + d3d11Device.put(), nullptr, nullptr); |
| 77 | + THROW_IF_FAILED(hr); |
| 78 | + |
| 79 | + com_ptr<IDXGIDevice> dxgiDevice; |
| 80 | + hr = d3d11Device->QueryInterface(__uuidof(IDXGIDevice), dxgiDevice.put_void()); |
| 81 | + THROW_IF_FAILED(hr); |
| 82 | + |
| 83 | + com_ptr<IInspectable> inspectableDevice; |
| 84 | + hr = CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.get(), inspectableDevice.put()); |
| 85 | + THROW_IF_FAILED(hr); |
| 86 | + deviceList.push_back({ |
| 87 | + LearningModelDevice::CreateFromDirect3D11Device(inspectableDevice.as<IDirect3DDevice>()), |
| 88 | + deviceType, |
| 89 | + deviceCreationLocation |
| 90 | + }); |
| 91 | + } |
| 92 | +#ifdef DXCORE_SUPPORTED_BUILD |
| 93 | + else if ((TypeHelper::GetWinmlDeviceKind(deviceType) != LearningModelDeviceKind::Cpu) && |
| 94 | + !adapterName.empty()) |
| 95 | + { |
| 96 | + com_ptr<IDXCoreAdapterFactory> spFactory; |
| 97 | + THROW_IF_FAILED(DXCoreCreateAdapterFactory(IID_PPV_ARGS(spFactory.put()))); |
| 98 | + |
| 99 | + com_ptr<IDXCoreAdapterList> spAdapterList; |
| 100 | + const GUID dxGUIDs[] = { DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE }; |
| 101 | + |
| 102 | + THROW_IF_FAILED( |
| 103 | + spFactory->CreateAdapterList(ARRAYSIZE(dxGUIDs), dxGUIDs, IID_PPV_ARGS(spAdapterList.put()))); |
| 104 | + |
| 105 | + std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; |
| 106 | + std::string adapterNameStr = converter.to_bytes(adapterName); |
| 107 | + com_ptr<IDXCoreAdapter> spAdapter = nullptr; |
| 108 | + com_ptr<IDXCoreAdapter> currAdapter = nullptr; |
| 109 | + bool chosenAdapterFound = false; |
| 110 | + printf("Printing available adapters..\n"); |
| 111 | + for (UINT i = 0; i < spAdapterList->GetAdapterCount(); i++) |
| 112 | + { |
| 113 | + THROW_IF_FAILED(spAdapterList->GetAdapter(i, currAdapter.put())); |
| 114 | + |
| 115 | + // If the adapter is a software adapter then don't consider it for index selection |
| 116 | + bool isHardware; |
| 117 | + size_t driverDescriptionSize; |
| 118 | + THROW_IF_FAILED(currAdapter->GetPropertySize(DXCoreAdapterProperty::DriverDescription, |
| 119 | + &driverDescriptionSize)); |
| 120 | + CHAR* driverDescription = new CHAR[driverDescriptionSize]; |
| 121 | + THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::IsHardware, &isHardware)); |
| 122 | + THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::DriverDescription, |
| 123 | + driverDescriptionSize, driverDescription)); |
| 124 | + if (isHardware) |
| 125 | + { |
| 126 | + printf("Description: %s\n", driverDescription); |
| 127 | + } |
| 128 | + if (!adapterName.empty() && !chosenAdapterFound) |
| 129 | + { |
| 130 | + std::string driverDescriptionStr = std::string(driverDescription); |
| 131 | + std::transform(driverDescriptionStr.begin(), driverDescriptionStr.end(), |
| 132 | + driverDescriptionStr.begin(), ::tolower); |
| 133 | + std::transform(adapterNameStr.begin(), adapterNameStr.end(), adapterNameStr.begin(), |
| 134 | + ::tolower); |
| 135 | + if (strstr(driverDescriptionStr.c_str(), adapterNameStr.c_str())) |
| 136 | + { |
| 137 | + chosenAdapterFound = true; |
| 138 | + spAdapter = currAdapter; |
| 139 | + } |
| 140 | + } |
| 141 | + currAdapter = nullptr; |
| 142 | + free(driverDescription); |
| 143 | + } |
| 144 | + |
| 145 | + if (spAdapter == nullptr) |
| 146 | + { |
| 147 | + throw hresult_invalid_argument(L"ERROR: No matching adapter with given adapter name: " + |
| 148 | + adapterName); |
| 149 | + } |
| 150 | + size_t driverDescriptionSize; |
| 151 | + THROW_IF_FAILED( |
| 152 | + spAdapter->GetPropertySize(DXCoreAdapterProperty::DriverDescription, &driverDescriptionSize)); |
| 153 | + CHAR* driverDescription = new CHAR[driverDescriptionSize]; |
| 154 | + spAdapter->GetProperty(DXCoreAdapterProperty::DriverDescription, driverDescriptionSize, |
| 155 | + driverDescription); |
| 156 | + printf("Using adapter : %s\n", driverDescription); |
| 157 | + free(driverDescription); |
| 158 | + IUnknown* pAdapter = spAdapter.get(); |
| 159 | + com_ptr<IDXGIAdapter> spDxgiAdapter; |
| 160 | + D3D_FEATURE_LEVEL d3dFeatureLevel = D3D_FEATURE_LEVEL_1_0_CORE; |
| 161 | + D3D12_COMMAND_LIST_TYPE commandQueueType = D3D12_COMMAND_LIST_TYPE_COMPUTE; |
| 162 | + |
| 163 | + // Check if adapter selected has DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS attribute selected. If |
| 164 | + // so, then GPU was selected that has D3D12 and D3D11 capabilities. It would be the most stable |
| 165 | + // to use DXGI to enumerate GPU and use D3D_FEATURE_LEVEL_11_0 so that image tensorization for |
| 166 | + // video frames would be able to happen on the GPU. |
| 167 | + if (spAdapter->IsAttributeSupported(DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS)) |
| 168 | + { |
| 169 | + d3dFeatureLevel = D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0; |
| 170 | + com_ptr<IDXGIFactory4> dxgiFactory4; |
| 171 | + HRESULT hr; |
| 172 | + try |
| 173 | + { |
| 174 | + hr = CreateDXGIFactory2SEH(dxgiFactory4.put_void()); |
| 175 | + } |
| 176 | + catch (...) |
| 177 | + { |
| 178 | + hr = E_FAIL; |
| 179 | + } |
| 180 | + if (hr == S_OK) |
| 181 | + { |
| 182 | + // If DXGI factory creation was successful then get the IDXGIAdapter from the LUID |
| 183 | + // acquired from the selectedAdapter |
| 184 | + std::cout << "Using DXGI for adapter creation.." << std::endl; |
| 185 | + LUID adapterLuid; |
| 186 | + THROW_IF_FAILED(spAdapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, &adapterLuid)); |
| 187 | + THROW_IF_FAILED(dxgiFactory4->EnumAdapterByLuid(adapterLuid, __uuidof(IDXGIAdapter), |
| 188 | + spDxgiAdapter.put_void())); |
| 189 | + pAdapter = spDxgiAdapter.get(); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + // create D3D12Device |
| 194 | + com_ptr<ID3D12Device> d3d12Device; |
| 195 | + THROW_IF_FAILED( |
| 196 | + D3D12CreateDevice(pAdapter, d3dFeatureLevel, __uuidof(ID3D12Device), d3d12Device.put_void())); |
| 197 | + |
| 198 | + // create D3D12 command queue from device |
| 199 | + com_ptr<ID3D12CommandQueue> d3d12CommandQueue; |
| 200 | + D3D12_COMMAND_QUEUE_DESC commandQueueDesc = {}; |
| 201 | + commandQueueDesc.Type = commandQueueType; |
| 202 | + THROW_IF_FAILED(d3d12Device->CreateCommandQueue(&commandQueueDesc, __uuidof(ID3D12CommandQueue), |
| 203 | + d3d12CommandQueue.put_void())); |
| 204 | + |
| 205 | + // create LearningModelDevice from command queue |
| 206 | + auto factory = get_activation_factory<LearningModelDevice, ILearningModelDeviceFactoryNative>(); |
| 207 | + com_ptr<::IUnknown> spUnkLearningModelDevice; |
| 208 | + THROW_IF_FAILED( |
| 209 | + factory->CreateFromD3D12CommandQueue(d3d12CommandQueue.get(), spUnkLearningModelDevice.put())); |
| 210 | + deviceList.push_back({ |
| 211 | + spUnkLearningModelDevice.as<LearningModelDevice>(), |
| 212 | + deviceType, |
| 213 | + deviceCreationLocation |
| 214 | + }); |
| 215 | + } |
| 216 | +#endif |
| 217 | + else |
| 218 | + { |
| 219 | + deviceList.push_back({ |
| 220 | + LearningModelDevice(TypeHelper::GetWinmlDeviceKind(deviceType)), |
| 221 | + deviceType, |
| 222 | + deviceCreationLocation |
| 223 | + }); |
| 224 | + } |
| 225 | + } |
| 226 | + catch (...) |
| 227 | + { |
| 228 | + printf("Creating LearningModelDevice failed!"); |
| 229 | + throw; |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments