Skip to content

Commit 4facf40

Browse files
committed
Fix custom adapter
1 parent f78631b commit 4facf40

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/backends/dx/DXRuntime/Device.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,40 @@ Device::Device(Context &&ctx, DeviceConfig const *settings)
140140
}
141141
if (extDevice && extDevice->device) {
142142
device = {static_cast<ID3D12Device5 *>(extDevice->device), false};
143-
if (extDevice->adapter) {
144-
adapter = {extDevice->adapter, false};
145-
} else {
146-
IDXGIDevice *pDXGIDevice = nullptr;
147-
auto dispose_pDXGIDevice = vstd::scope_exit([&] {
148-
if (pDXGIDevice)
149-
pDXGIDevice->Release();
150-
});
151-
ThrowIfFailed(device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)));
152-
// Query for IDXGIAdapter from IDXGIDevice
153-
IDXGIAdapter *pAdapter = nullptr;
154-
ThrowIfFailed(pDXGIDevice->GetAdapter(&pAdapter));
155-
adapter = {static_cast<IDXGIAdapter1 *>(pAdapter), true};
156-
}
157143
if (extDevice->factory) {
158144
dxgiFactory = {extDevice->factory, false};
159145
} else {
160-
IDXGIFactory *pDxgiFactory;
161-
ThrowIfFailed(adapter->GetParent(IID_PPV_ARGS(&pDxgiFactory)));
162-
dxgiFactory = {static_cast<IDXGIFactory2 *>(pDxgiFactory), true};
146+
ThrowIfFailed(CreateDXGIFactory2(0, IID_PPV_ARGS(dxgiFactory.GetAddressOf())));
147+
// IDXGIFactory *pDxgiFactory;
148+
// ThrowIfFailed(adapter->GetParent(IID_PPV_ARGS(&pDxgiFactory)));
149+
// dxgiFactory = {static_cast<IDXGIFactory2 *>(pDxgiFactory), true};
150+
}
151+
if (extDevice->adapter) {
152+
adapter = {extDevice->adapter, false};
153+
} else {
154+
DxPtr<IDXGIAdapter1> local_adapter;
155+
auto device_id = device->GetAdapterLuid();
156+
for (auto adapterIndex = 0u; dxgiFactory->EnumAdapters1(adapterIndex, local_adapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND; adapterIndex++) {
157+
DXGI_ADAPTER_DESC1 desc;
158+
local_adapter->GetDesc1(&desc);
159+
if (std::memcmp(&desc.AdapterLuid, &device_id, sizeof(LUID)) == 0) {
160+
adapter = std::move(local_adapter);
161+
break;
162+
}
163+
}
164+
if (!adapter) {
165+
LUISA_ERROR("Adapter not found.");
166+
}
167+
// IDXGIDevice *pDXGIDevice = nullptr;
168+
// auto dispose_pDXGIDevice = vstd::scope_exit([&] {
169+
// if (pDXGIDevice)
170+
// pDXGIDevice->Release();
171+
// });
172+
// ThrowIfFailed(device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)));
173+
// // Query for IDXGIAdapter from IDXGIDevice
174+
// IDXGIAdapter *pAdapter = nullptr;
175+
// ThrowIfFailed(pDXGIDevice->GetAdapter(&pAdapter));
176+
// adapter = {static_cast<IDXGIAdapter1 *>(pAdapter), true};
163177
}
164178
DXGI_ADAPTER_DESC1 desc;
165179
adapter->GetDesc1(&desc);

0 commit comments

Comments
 (0)