internal/graphicsdriver/directx: refactoring

This commit is contained in:
Hajime Hoshi 2022-06-20 23:40:53 +09:00
parent 2f104a35be
commit a641f9ede0

View File

@ -122,7 +122,6 @@ type Graphics struct {
indices [frameCount][]*_ID3D12Resource indices [frameCount][]*_ID3D12Resource
factory *_IDXGIFactory4 factory *_IDXGIFactory4
adapter *_IDXGIAdapter1
swapChain *_IDXGISwapChain4 swapChain *_IDXGISwapChain4
window windows.HWND window windows.HWND
@ -214,19 +213,14 @@ func (g *Graphics) initializeDesktop(useWARP bool, useDebugLayer bool) (ferr err
} }
}() }()
var adapter *_IDXGIAdapter1
if useWARP { if useWARP {
a, err := g.factory.EnumWarpAdapter() a, err := g.factory.EnumWarpAdapter()
if err != nil { if err != nil {
return err return err
} }
defer a.Release()
g.adapter = a adapter = a
defer func() {
if ferr != nil {
g.adapter.Release()
g.adapter = nil
}
}()
} else { } else {
for i := 0; ; i++ { for i := 0; ; i++ {
a, err := g.factory.EnumAdapters1(uint32(i)) a, err := g.factory.EnumAdapters1(uint32(i))
@ -236,35 +230,28 @@ func (g *Graphics) initializeDesktop(useWARP bool, useDebugLayer bool) (ferr err
if err != nil { if err != nil {
return err return err
} }
defer a.Release()
adapter = a
desc, err := a.GetDesc1() desc, err := adapter.GetDesc1()
if err != nil { if err != nil {
return err return err
} }
if desc.Flags&_DXGI_ADAPTER_FLAG_SOFTWARE != 0 { if desc.Flags&_DXGI_ADAPTER_FLAG_SOFTWARE != 0 {
a.Release()
continue continue
} }
if err := _D3D12CreateDevice(unsafe.Pointer(a), _D3D_FEATURE_LEVEL_11_0, &_IID_ID3D12Device, nil); err != nil { if err := _D3D12CreateDevice(unsafe.Pointer(adapter), _D3D_FEATURE_LEVEL_11_0, &_IID_ID3D12Device, nil); err != nil {
a.Release()
continue continue
} }
g.adapter = a
defer func() {
if ferr != nil {
g.adapter.Release()
g.adapter = nil
}
}()
break break
} }
} }
if g.adapter == nil { if adapter == nil {
return errors.New("directx: DirectX 12 is not supported") return errors.New("directx: DirectX 12 is not supported")
} }
if err := _D3D12CreateDevice(unsafe.Pointer(g.adapter), _D3D_FEATURE_LEVEL_11_0, &_IID_ID3D12Device, (*unsafe.Pointer)(unsafe.Pointer(&g.device))); err != nil { if err := _D3D12CreateDevice(unsafe.Pointer(adapter), _D3D_FEATURE_LEVEL_11_0, &_IID_ID3D12Device, (*unsafe.Pointer)(unsafe.Pointer(&g.device))); err != nil {
return err return err
} }