internal/graphicsdriver/directx: use QueryInterface to cast IDXGISwapChain to IDXGISwapChain4

QueryInterface is better in terms of error messages than a dumb
pointer casting.
This commit is contained in:
Hajime Hoshi 2023-03-30 00:44:36 +09:00
parent 7fa65ae983
commit 6858e4b60b
3 changed files with 42 additions and 17 deletions

View File

@ -96,11 +96,12 @@ const (
) )
var ( var (
_IID_IDXGIAdapter1 = windows.GUID{Data1: 0x29038f61, Data2: 0x3839, Data3: 0x4626, Data4: [...]byte{0x91, 0xfd, 0x08, 0x68, 0x79, 0x01, 0x1a, 0x05}} _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_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_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_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_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 ( var (
@ -498,10 +499,6 @@ type _IDXGISwapChain_Vtbl struct {
GetLastPresentCount uintptr 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) { func (i *_IDXGISwapChain) GetBuffer(buffer uint32, riid *windows.GUID) (unsafe.Pointer, error) {
var resource unsafe.Pointer var resource unsafe.Pointer
r, _, _ := syscall.Syscall6(i.vtbl.GetBuffer, 4, uintptr(unsafe.Pointer(i)), 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 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 { func (i *_IDXGISwapChain) Release() uint32 {
r, _, _ := syscall.Syscall(i.vtbl.Release, 1, uintptr(unsafe.Pointer(i)), 0, 0) r, _, _ := syscall.Syscall(i.vtbl.Release, 1, uintptr(unsafe.Pointer(i)), 0, 0)
return uint32(r) return uint32(r)

View File

@ -497,7 +497,11 @@ func (g *graphics12) initSwapChainDesktop(width, height int) error {
return err return err
} }
g.frameIndex = g.graphicsInfra.currentBackBufferIndex() idx, err := g.graphicsInfra.currentBackBufferIndex()
if err != nil {
return err
}
g.frameIndex = idx
return nil return nil
} }
@ -745,7 +749,11 @@ func (g *graphics12) moveToNextFrame() error {
if microsoftgdk.IsXbox() { if microsoftgdk.IsXbox() {
g.frameIndex = (g.frameIndex + 1) % frameCount g.frameIndex = (g.frameIndex + 1) % frameCount
} else { } 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] { if g.fence.GetCompletedValue() < g.fenceValues[g.frameIndex] {

View File

@ -141,8 +141,9 @@ func NewGraphics() (graphicsdriver.Graphics, error) {
} }
type graphicsInfra struct { type graphicsInfra struct {
factory *_IDXGIFactory factory *_IDXGIFactory
swapChain *_IDXGISwapChain swapChain *_IDXGISwapChain
swapChain4 *_IDXGISwapChain4
allowTearing bool allowTearing bool
@ -186,6 +187,10 @@ func (g *graphicsInfra) release() {
g.swapChain.Release() g.swapChain.Release()
g.swapChain = nil g.swapChain = nil
} }
if g.swapChain4 != nil {
g.swapChain4.Release()
g.swapChain4 = nil
}
} }
// appendAdapters appends found adapters to the given adapters. // 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. // MakeWindowAssociation should be called after swap chain creation.
// https://docs.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgifactory-makewindowassociation // 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 { 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 return nil
} }
func (g *graphicsInfra) currentBackBufferIndex() int { func (g *graphicsInfra) currentBackBufferIndex() (int, error) {
var swapChain4 *_IDXGISwapChain4 if g.swapChain4 == nil {
g.swapChain.As(&swapChain4) return 0, fmt.Errorf("directx: IDXGISwapChain4 is not available")
return int(swapChain4.GetCurrentBackBufferIndex()) }
return int(g.swapChain4.GetCurrentBackBufferIndex()), nil
} }
func (g *graphicsInfra) present(vsyncEnabled bool) error { func (g *graphicsInfra) present(vsyncEnabled bool) error {