diff --git a/src/dawn_native/d3d12/BackendD3D12.cpp b/src/dawn_native/d3d12/BackendD3D12.cpp index 46dc1b6792..7075093953 100644 --- a/src/dawn_native/d3d12/BackendD3D12.cpp +++ b/src/dawn_native/d3d12/BackendD3D12.cpp @@ -70,6 +70,18 @@ namespace dawn_native { namespace d3d12 { return std::move(factory); } + ResultOrError> CreateAdapterFromIDXGIAdapter( + Backend* backend, + ComPtr dxgiAdapter) { + ComPtr dxgiAdapter3; + DAWN_TRY(CheckHRESULT(dxgiAdapter.As(&dxgiAdapter3), "DXGIAdapter retrieval")); + std::unique_ptr adapter = + std::make_unique(backend, std::move(dxgiAdapter3)); + DAWN_TRY(adapter->Initialize()); + + return {std::move(adapter)}; + } + } // anonymous namespace Backend::Backend(InstanceBase* instance) @@ -127,23 +139,34 @@ namespace dawn_native { namespace d3d12 { } ASSERT(dxgiAdapter != nullptr); - - ComPtr dxgiAdapter3; - HRESULT result = dxgiAdapter.As(&dxgiAdapter3); - ASSERT(SUCCEEDED(result)); - - std::unique_ptr adapter = - std::make_unique(this, std::move(dxgiAdapter3)); - if (GetInstance()->ConsumedError(adapter->Initialize())) { + ResultOrError> adapter = + CreateAdapterFromIDXGIAdapter(this, dxgiAdapter); + if (adapter.IsError()) { + adapter.AcquireError(); continue; } - adapters.push_back(std::move(adapter)); + adapters.push_back(std::move(adapter.AcquireSuccess())); } return adapters; } + ResultOrError>> Backend::DiscoverAdapters( + const AdapterDiscoveryOptionsBase* optionsBase) { + ASSERT(optionsBase->backendType == WGPUBackendType_D3D12); + const AdapterDiscoveryOptions* options = + static_cast(optionsBase); + + ASSERT(options->dxgiAdapter != nullptr); + + std::unique_ptr adapter; + DAWN_TRY_ASSIGN(adapter, CreateAdapterFromIDXGIAdapter(this, options->dxgiAdapter)); + std::vector> adapters; + adapters.push_back(std::move(adapter)); + return std::move(adapters); + } + BackendConnection* Connect(InstanceBase* instance) { Backend* backend = new Backend(instance); diff --git a/src/dawn_native/d3d12/BackendD3D12.h b/src/dawn_native/d3d12/BackendD3D12.h index 403943a9fb..87c2d13f04 100644 --- a/src/dawn_native/d3d12/BackendD3D12.h +++ b/src/dawn_native/d3d12/BackendD3D12.h @@ -35,6 +35,8 @@ namespace dawn_native { namespace d3d12 { const PlatformFunctions* GetFunctions() const; std::vector> DiscoverDefaultAdapters() override; + ResultOrError>> DiscoverAdapters( + const AdapterDiscoveryOptionsBase* optionsBase) override; private: // Keep mFunctions as the first member so that in the destructor it is freed last. Otherwise diff --git a/src/dawn_native/d3d12/D3D12Backend.cpp b/src/dawn_native/d3d12/D3D12Backend.cpp index f78975ef7a..7196599426 100644 --- a/src/dawn_native/d3d12/D3D12Backend.cpp +++ b/src/dawn_native/d3d12/D3D12Backend.cpp @@ -69,4 +69,7 @@ namespace dawn_native { namespace d3d12 { return reinterpret_cast(texture.Detach()); } + AdapterDiscoveryOptions::AdapterDiscoveryOptions(ComPtr adapter) + : AdapterDiscoveryOptionsBase(WGPUBackendType_D3D12), dxgiAdapter(std::move(adapter)) { + } }} // namespace dawn_native::d3d12 diff --git a/src/include/dawn_native/D3D12Backend.h b/src/include/dawn_native/D3D12Backend.h index bdefdf37be..035ad96188 100644 --- a/src/include/dawn_native/D3D12Backend.h +++ b/src/include/dawn_native/D3D12Backend.h @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -52,6 +53,12 @@ namespace dawn_native { namespace d3d12 { DAWN_NATIVE_EXPORT WGPUTexture WrapSharedHandle(WGPUDevice device, const ExternalImageDescriptorDXGISharedHandle* descriptor); + struct DAWN_NATIVE_EXPORT AdapterDiscoveryOptions : public AdapterDiscoveryOptionsBase { + AdapterDiscoveryOptions(Microsoft::WRL::ComPtr adapter); + + Microsoft::WRL::ComPtr dxgiAdapter; + }; + }} // namespace dawn_native::d3d12 #endif // DAWNNATIVE_D3D12BACKEND_H_