Skip to content

Commit 99cd34c

Browse files
committed
Cleanup DirectXContext
- we only need the ID3D12Debug1 debug interface - no need to hold on to the factory / debug interfaces - no need to track devices once they've been registered
1 parent a8967dd commit 99cd34c

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
@@ -591,68 +591,40 @@ class DXDevice : public hlsltest::Device {
591591
return llvm::Error::success();
592592
}
593593
};
594+
} // namespace
594595

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

613-
if (auto Err = HR::toError(Debug->QueryInterface(IID_PPV_ARGS(&Debug1)),
614-
"Failed to queryy Debug interface"))
615-
return Err;
616-
Debug->EnableDebugLayer();
617-
Debug1->SetEnableGPUBasedValidation(true);
618-
#endif
600+
if (auto Err = HR::toError(D3D12GetDebugInterface(IID_PPV_ARGS(&Debug1)),
601+
"failed to create D3D12 Debug Interface"))
602+
return Err;
619603

620-
if (auto Err = HR::toError(CreateDXGIFactory2(0, IID_PPV_ARGS(&Factory)),
621-
"Failed to create DXGI Factory")) {
622-
return Err;
623-
}
624-
for (unsigned AdapterIndex = 0;;) {
625-
CComPtr<IDXGIAdapter1> Adapter;
626-
627-
HRESULT HR = Factory->EnumAdapters1(AdapterIndex++, &Adapter);
628-
629-
if (DXGI_ERROR_NOT_FOUND == HR)
630-
return llvm::Error::success();
604+
Debug1->EnableDebugLayer();
605+
Debug1->SetEnableGPUBasedValidation(true);
606+
#endif
631607

632-
if (auto Err = HR::toError(HR, ""))
633-
return Err;
634-
auto ExDevice = DXDevice::Create(Adapter);
635-
if (!ExDevice)
636-
return ExDevice.takeError();
637-
auto ShPtr = std::make_shared<DXDevice>(*ExDevice);
638-
Devices.push_back(ShPtr);
639-
Device::registerDevice(std::static_pointer_cast<Device>(ShPtr));
640-
}
641-
return llvm::Error::success();
608+
CComPtr<IDXGIFactory2> Factory;
609+
if (auto Err = HR::toError(CreateDXGIFactory2(0, IID_PPV_ARGS(&Factory)),
610+
"Failed to create DXGI Factory")) {
611+
return Err;
642612
}
613+
for (unsigned AdapterIndex = 0;;) {
614+
CComPtr<IDXGIAdapter1> Adapter;
643615

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

646-
iterator begin() { return Devices.begin(); }
647-
iterator end() { return Devices.end(); }
618+
if (DXGI_ERROR_NOT_FOUND == HR)
619+
return llvm::Error::success();
648620

649-
static DirectXContext &Instance() {
650-
static DirectXContext Ctx;
651-
return Ctx;
621+
if (auto Err = HR::toError(HR, ""))
622+
return Err;
623+
auto ExDevice = DXDevice::Create(Adapter);
624+
if (!ExDevice)
625+
return ExDevice.takeError();
626+
auto ShPtr = std::make_shared<DXDevice>(*ExDevice);
627+
Device::registerDevice(std::static_pointer_cast<Device>(ShPtr));
652628
}
653-
};
654-
} // namespace
655-
656-
llvm::Error InitializeDXDevices() {
657-
return DirectXContext::Instance().initialize();
629+
return llvm::Error::success();
658630
}

0 commit comments

Comments
 (0)