internal/graphicsdriver/directx: reduce unnecessary usages of IDXGISwapChain4

IDXGISwapChain is enough for DirectX 11.

Updates #2613
This commit is contained in:
Hajime Hoshi 2023-03-30 00:13:58 +09:00
parent c9f1696a5b
commit 7fa65ae983
2 changed files with 44 additions and 37 deletions

View File

@ -502,6 +502,45 @@ 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)),
uintptr(buffer), uintptr(unsafe.Pointer(riid)), uintptr(unsafe.Pointer(&resource)),
0, 0)
runtime.KeepAlive(riid)
if uint32(r) != uint32(windows.S_OK) {
return nil, fmt.Errorf("directx: IDXGISwapChain::GetBuffer failed: %w", handleError(windows.Handle(uint32(r))))
}
return resource, nil
}
func (i *_IDXGISwapChain) ResizeBuffers(bufferCount uint32, width uint32, height uint32, newFormat _DXGI_FORMAT, swapChainFlags uint32) error {
r, _, _ := syscall.Syscall6(i.vtbl.ResizeBuffers, 6,
uintptr(unsafe.Pointer(i)), uintptr(bufferCount), uintptr(width),
uintptr(height), uintptr(newFormat), uintptr(swapChainFlags))
if uint32(r) != uint32(windows.S_OK) {
return fmt.Errorf("directx: IDXGISwapChain::ResizeBuffers failed: %w", handleError(windows.Handle(uint32(r))))
}
return nil
}
func (i *_IDXGISwapChain) Present(syncInterval uint32, flags uint32) (occluded bool, err error) {
r, _, _ := syscall.Syscall(i.vtbl.Present, 3, uintptr(unsafe.Pointer(i)), uintptr(syncInterval), uintptr(flags))
if uint32(r) != uint32(windows.S_OK) {
// During a screen lock, Present fails (#2179).
if uint32(r) == uint32(windows.DXGI_STATUS_OCCLUDED) {
return true, nil
}
return false, fmt.Errorf("directx: IDXGISwapChain::Present failed: %w", handleError(windows.Handle(uint32(r))))
}
return false, nil
}
func (i *_IDXGISwapChain) Release() uint32 {
r, _, _ := syscall.Syscall(i.vtbl.Release, 1, uintptr(unsafe.Pointer(i)), 0, 0)
return uint32(r)
}
type _IDXGISwapChain4 struct {
vtbl *_IDXGISwapChain4_Vtbl
}
@ -552,45 +591,11 @@ type _IDXGISwapChain4_Vtbl struct {
SetHDRMetaData uintptr
}
func (i *_IDXGISwapChain4) GetBuffer(buffer uint32, riid *windows.GUID) (unsafe.Pointer, error) {
var resource unsafe.Pointer
r, _, _ := syscall.Syscall6(i.vtbl.GetBuffer, 4, uintptr(unsafe.Pointer(i)),
uintptr(buffer), uintptr(unsafe.Pointer(riid)), uintptr(unsafe.Pointer(&resource)),
0, 0)
runtime.KeepAlive(riid)
if uint32(r) != uint32(windows.S_OK) {
return nil, fmt.Errorf("directx: IDXGISwapChain4::GetBuffer failed: %w", handleError(windows.Handle(uint32(r))))
}
return resource, nil
}
func (i *_IDXGISwapChain4) GetCurrentBackBufferIndex() uint32 {
r, _, _ := syscall.Syscall(i.vtbl.GetCurrentBackBufferIndex, 1, uintptr(unsafe.Pointer(i)), 0, 0)
return uint32(r)
}
func (i *_IDXGISwapChain4) Present(syncInterval uint32, flags uint32) (occluded bool, err error) {
r, _, _ := syscall.Syscall(i.vtbl.Present, 3, uintptr(unsafe.Pointer(i)), uintptr(syncInterval), uintptr(flags))
if uint32(r) != uint32(windows.S_OK) {
// During a screen lock, Present fails (#2179).
if uint32(r) == uint32(windows.DXGI_STATUS_OCCLUDED) {
return true, nil
}
return false, fmt.Errorf("directx: IDXGISwapChain4::Present failed: %w", handleError(windows.Handle(uint32(r))))
}
return false, nil
}
func (i *_IDXGISwapChain4) ResizeBuffers(bufferCount uint32, width uint32, height uint32, newFormat _DXGI_FORMAT, swapChainFlags uint32) error {
r, _, _ := syscall.Syscall6(i.vtbl.ResizeBuffers, 6,
uintptr(unsafe.Pointer(i)), uintptr(bufferCount), uintptr(width),
uintptr(height), uintptr(newFormat), uintptr(swapChainFlags))
if uint32(r) != uint32(windows.S_OK) {
return fmt.Errorf("directx: IDXGISwapChain4::ResizeBuffers failed: %w", handleError(windows.Handle(uint32(r))))
}
return nil
}
func (i *_IDXGISwapChain4) Release() uint32 {
r, _, _ := syscall.Syscall(i.vtbl.Release, 1, uintptr(unsafe.Pointer(i)), 0, 0)
return uint32(r)

View File

@ -142,7 +142,7 @@ func NewGraphics() (graphicsdriver.Graphics, error) {
type graphicsInfra struct {
factory *_IDXGIFactory
swapChain *_IDXGISwapChain4
swapChain *_IDXGISwapChain
allowTearing bool
@ -265,7 +265,7 @@ func (g *graphicsInfra) initSwapChain(width, height int, device unsafe.Pointer,
if err != nil {
return err
}
s.As(&g.swapChain)
g.swapChain = s
defer func() {
if ferr != nil {
g.release()
@ -297,7 +297,9 @@ func (g *graphicsInfra) resizeSwapChain(width, height int) error {
}
func (g *graphicsInfra) currentBackBufferIndex() int {
return int(g.swapChain.GetCurrentBackBufferIndex())
var swapChain4 *_IDXGISwapChain4
g.swapChain.As(&swapChain4)
return int(swapChain4.GetCurrentBackBufferIndex())
}
func (g *graphicsInfra) present(vsyncEnabled bool) error {