Skip to content

Commit 2a440b4

Browse files
authored
Merge pull request #11 from damyanp/cleanup_devices
Simplify InitializeDXDevices
2 parents c937091 + 99cd34c commit 2a440b4

File tree

1 file changed

+26
-54
lines changed

1 file changed

+26
-54
lines changed

lib/API/DX/Device.cpp

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -588,68 +588,40 @@ class DXDevice : public hlsltest::Device {
588588
return llvm::Error::success();
589589
}
590590
};
591+
} // namespace
591592

592-
class DirectXContext {
593-
private:
594-
CComPtr<IDXGIFactory2> Factory;
595-
CComPtr<ID3D12Debug> Debug;
596-
CComPtr<ID3D12Debug1> Debug1; // I hate this name!
597-
llvm::SmallVector<std::shared_ptr<DXDevice>> Devices;
598-
599-
DirectXContext() = default;
600-
~DirectXContext() = default;
601-
602-
public:
603-
llvm::Error initialize() {
604-
593+
llvm::Error InitializeDXDevices() {
605594
#ifndef NDEBUG
606-
if (auto Err = HR::toError(D3D12GetDebugInterface(IID_PPV_ARGS(&Debug)),
607-
"failed to create D3D12 Debug Interface"))
608-
return Err;
595+
CComPtr<ID3D12Debug1> Debug1;
609596

610-
if (auto Err = HR::toError(Debug->QueryInterface(IID_PPV_ARGS(&Debug1)),
611-
"Failed to queryy Debug interface"))
612-
return Err;
613-
Debug->EnableDebugLayer();
614-
Debug1->SetEnableGPUBasedValidation(true);
615-
#endif
597+
if (auto Err = HR::toError(D3D12GetDebugInterface(IID_PPV_ARGS(&Debug1)),
598+
"failed to create D3D12 Debug Interface"))
599+
return Err;
616600

617-
if (auto Err = HR::toError(CreateDXGIFactory2(0, IID_PPV_ARGS(&Factory)),
618-
"Failed to create DXGI Factory")) {
619-
return Err;
620-
}
621-
for (unsigned AdapterIndex = 0;;) {
622-
CComPtr<IDXGIAdapter1> Adapter;
623-
624-
HRESULT HR = Factory->EnumAdapters1(AdapterIndex++, &Adapter);
625-
626-
if (DXGI_ERROR_NOT_FOUND == HR)
627-
return llvm::Error::success();
601+
Debug1->EnableDebugLayer();
602+
Debug1->SetEnableGPUBasedValidation(true);
603+
#endif
628604

629-
if (auto Err = HR::toError(HR, ""))
630-
return Err;
631-
auto ExDevice = DXDevice::Create(Adapter);
632-
if (!ExDevice)
633-
return ExDevice.takeError();
634-
auto ShPtr = std::make_shared<DXDevice>(*ExDevice);
635-
Devices.push_back(ShPtr);
636-
Device::registerDevice(std::static_pointer_cast<Device>(ShPtr));
637-
}
638-
return llvm::Error::success();
605+
CComPtr<IDXGIFactory2> Factory;
606+
if (auto Err = HR::toError(CreateDXGIFactory2(0, IID_PPV_ARGS(&Factory)),
607+
"Failed to create DXGI Factory")) {
608+
return Err;
639609
}
610+
for (unsigned AdapterIndex = 0;;) {
611+
CComPtr<IDXGIAdapter1> Adapter;
640612

641-
using iterator = llvm::SmallVector<std::shared_ptr<DXDevice>>::iterator;
613+
HRESULT HR = Factory->EnumAdapters1(AdapterIndex++, &Adapter);
642614

643-
iterator begin() { return Devices.begin(); }
644-
iterator end() { return Devices.end(); }
615+
if (DXGI_ERROR_NOT_FOUND == HR)
616+
return llvm::Error::success();
645617

646-
static DirectXContext &Instance() {
647-
static DirectXContext Ctx;
648-
return Ctx;
618+
if (auto Err = HR::toError(HR, ""))
619+
return Err;
620+
auto ExDevice = DXDevice::Create(Adapter);
621+
if (!ExDevice)
622+
return ExDevice.takeError();
623+
auto ShPtr = std::make_shared<DXDevice>(*ExDevice);
624+
Device::registerDevice(std::static_pointer_cast<Device>(ShPtr));
649625
}
650-
};
651-
} // namespace
652-
653-
llvm::Error InitializeDXDevices() {
654-
return DirectXContext::Instance().initialize();
626+
return llvm::Error::success();
655627
}

0 commit comments

Comments
 (0)