diff --git a/internal/graphicsdriver/directx/dxgi_windows.go b/internal/graphicsdriver/directx/dxgi_windows.go index afc476691..5f2118d77 100644 --- a/internal/graphicsdriver/directx/dxgi_windows.go +++ b/internal/graphicsdriver/directx/dxgi_windows.go @@ -96,11 +96,12 @@ const ( ) var ( - _IID_IDXGIAdapter1 = windows.GUID{Data1: 0x29038f61, Data2: 0x3839, Data3: 0x4626, Data4: [...]byte{0x91, 0xfd, 0x08, 0x68, 0x79, 0x01, 0x1a, 0x05}} - _IID_IDXGIDevice = windows.GUID{Data1: 0x54ec77fa, Data2: 0x1377, Data3: 0x44e6, Data4: [...]byte{0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8, 0x4c}} - _IID_IDXGIFactory = windows.GUID{Data1: 0x7b7166ec, Data2: 0x21c7, Data3: 0x44ae, Data4: [...]byte{0xb2, 0x1a, 0xc9, 0xae, 0x32, 0x1a, 0xe3, 0x69}} - _IID_IDXGIFactory4 = windows.GUID{Data1: 0x1bc6ea02, Data2: 0xef36, Data3: 0x464f, Data4: [...]byte{0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a}} - _IID_IDXGIFactory5 = windows.GUID{Data1: 0x7632e1f5, Data2: 0xee65, Data3: 0x4dca, Data4: [...]byte{0x87, 0xfd, 0x84, 0xcd, 0x75, 0xf8, 0x83, 0x8d}} + _IID_IDXGIAdapter1 = windows.GUID{Data1: 0x29038f61, Data2: 0x3839, Data3: 0x4626, Data4: [...]byte{0x91, 0xfd, 0x08, 0x68, 0x79, 0x01, 0x1a, 0x05}} + _IID_IDXGIDevice = windows.GUID{Data1: 0x54ec77fa, Data2: 0x1377, Data3: 0x44e6, Data4: [...]byte{0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8, 0x4c}} + _IID_IDXGIFactory = windows.GUID{Data1: 0x7b7166ec, Data2: 0x21c7, Data3: 0x44ae, Data4: [...]byte{0xb2, 0x1a, 0xc9, 0xae, 0x32, 0x1a, 0xe3, 0x69}} + _IID_IDXGIFactory4 = windows.GUID{Data1: 0x1bc6ea02, Data2: 0xef36, Data3: 0x464f, Data4: [...]byte{0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a}} + _IID_IDXGIFactory5 = windows.GUID{Data1: 0x7632e1f5, Data2: 0xee65, Data3: 0x4dca, Data4: [...]byte{0x87, 0xfd, 0x84, 0xcd, 0x75, 0xf8, 0x83, 0x8d}} + _IID_IDXGISwapChain4 = windows.GUID{Data1: 0x3d585d5a, Data2: 0xbd4a, Data3: 0x489e, Data4: [...]byte{0xb1, 0xf4, 0x3d, 0xbc, 0xb6, 0x45, 0x2f, 0xfb}} ) var ( @@ -498,10 +499,6 @@ type _IDXGISwapChain_Vtbl struct { GetLastPresentCount uintptr } -func (i *_IDXGISwapChain) As(swapChain **_IDXGISwapChain4) { - *swapChain = (*_IDXGISwapChain4)(unsafe.Pointer(i)) -} - func (i *_IDXGISwapChain) GetBuffer(buffer uint32, riid *windows.GUID) (unsafe.Pointer, error) { var resource unsafe.Pointer r, _, _ := syscall.Syscall6(i.vtbl.GetBuffer, 4, uintptr(unsafe.Pointer(i)), @@ -536,6 +533,16 @@ func (i *_IDXGISwapChain) Present(syncInterval uint32, flags uint32) (occluded b return false, nil } +func (i *_IDXGISwapChain) QueryInterface(riid *windows.GUID) (unsafe.Pointer, error) { + var v unsafe.Pointer + r, _, _ := syscall.Syscall(i.vtbl.QueryInterface, 3, uintptr(unsafe.Pointer(i)), uintptr(unsafe.Pointer(riid)), uintptr(unsafe.Pointer(&v))) + runtime.KeepAlive(riid) + if uint32(r) != uint32(windows.S_OK) { + return nil, fmt.Errorf("directx: IDXGISwapChain::QueryInterface failed: %w", handleError(windows.Handle(uint32(r)))) + } + return v, nil +} + func (i *_IDXGISwapChain) Release() uint32 { r, _, _ := syscall.Syscall(i.vtbl.Release, 1, uintptr(unsafe.Pointer(i)), 0, 0) return uint32(r) diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index bdc0a1a5d..38ee56e56 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -497,7 +497,11 @@ func (g *graphics12) initSwapChainDesktop(width, height int) error { return err } - g.frameIndex = g.graphicsInfra.currentBackBufferIndex() + idx, err := g.graphicsInfra.currentBackBufferIndex() + if err != nil { + return err + } + g.frameIndex = idx return nil } @@ -745,7 +749,11 @@ func (g *graphics12) moveToNextFrame() error { if microsoftgdk.IsXbox() { g.frameIndex = (g.frameIndex + 1) % frameCount } else { - g.frameIndex = g.graphicsInfra.currentBackBufferIndex() + idx, err := g.graphicsInfra.currentBackBufferIndex() + if err != nil { + return err + } + g.frameIndex = idx } if g.fence.GetCompletedValue() < g.fenceValues[g.frameIndex] { diff --git a/internal/graphicsdriver/directx/graphics_windows.go b/internal/graphicsdriver/directx/graphics_windows.go index 5ec23025c..99a26600f 100644 --- a/internal/graphicsdriver/directx/graphics_windows.go +++ b/internal/graphicsdriver/directx/graphics_windows.go @@ -141,8 +141,9 @@ func NewGraphics() (graphicsdriver.Graphics, error) { } type graphicsInfra struct { - factory *_IDXGIFactory - swapChain *_IDXGISwapChain + factory *_IDXGIFactory + swapChain *_IDXGISwapChain + swapChain4 *_IDXGISwapChain4 allowTearing bool @@ -186,6 +187,10 @@ func (g *graphicsInfra) release() { g.swapChain.Release() g.swapChain = nil } + if g.swapChain4 != nil { + g.swapChain4.Release() + g.swapChain4 = nil + } } // appendAdapters appends found adapters to the given adapters. @@ -272,6 +277,10 @@ func (g *graphicsInfra) initSwapChain(width, height int, device unsafe.Pointer, } }() + if s4, err := g.swapChain.QueryInterface(&_IID_IDXGISwapChain4); err == nil && s4 != nil { + g.swapChain4 = (*_IDXGISwapChain4)(s4) + } + // MakeWindowAssociation should be called after swap chain creation. // https://docs.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgifactory-makewindowassociation if err := g.factory.MakeWindowAssociation(window, _DXGI_MWA_NO_WINDOW_CHANGES|_DXGI_MWA_NO_ALT_ENTER); err != nil { @@ -296,10 +305,11 @@ func (g *graphicsInfra) resizeSwapChain(width, height int) error { return nil } -func (g *graphicsInfra) currentBackBufferIndex() int { - var swapChain4 *_IDXGISwapChain4 - g.swapChain.As(&swapChain4) - return int(swapChain4.GetCurrentBackBufferIndex()) +func (g *graphicsInfra) currentBackBufferIndex() (int, error) { + if g.swapChain4 == nil { + return 0, fmt.Errorf("directx: IDXGISwapChain4 is not available") + } + return int(g.swapChain4.GetCurrentBackBufferIndex()), nil } func (g *graphicsInfra) present(vsyncEnabled bool) error {