internal/graphicsdriver/direct: replace Get -> NewGraphics

Updates #2142
This commit is contained in:
Hajime Hoshi 2022-06-17 11:39:43 +09:00
parent 138463e219
commit 9962fc5ee5
9 changed files with 44 additions and 45 deletions

View File

@ -20,7 +20,6 @@ import (
"os"
"reflect"
"strings"
"sync"
"unsafe"
"golang.org/x/sys/windows"
@ -34,28 +33,22 @@ import (
const frameCount = 2
var (
// isDirectXAvailable indicates whether DirectX is available or not.
isDirectXAvailable bool
isDirectXAvailableOnce sync.Once
)
func NewGraphics() (graphicsdriver.Graphics, error) {
const is64bit = uint64(^uintptr(0)) == ^uint64(0)
var theGraphics Graphics
func Get() *Graphics {
isDirectXAvailableOnce.Do(func() {
const is64bit = uint64(^uintptr(0)) == ^uint64(0)
// In 32bit machines, DirectX is not used because
// 1) The functions syscall.Syscall cannot accept 64bit values as one argument
// 2) The struct layouts can be different
// TODO: Support DirectX for 32bit machines (#2088).
isDirectXAvailable = is64bit && theGraphics.initializeDevice() == nil
})
if !isDirectXAvailable {
return nil
// In 32bit machines, DirectX is not used because
// 1) The functions syscall.Syscall cannot accept 64bit values as one argument
// 2) The struct layouts can be different
// TODO: Support DirectX for 32bit machines (#2088).
if !is64bit {
return nil, fmt.Errorf("directx: DirectX is not available on a 32bit machine")
}
return &theGraphics
g := &Graphics{}
if err := g.initializeDevice(); err != nil {
return nil, err
}
return g, nil
}
var inputElementDescs []_D3D12_INPUT_ELEMENT_DESC

View File

@ -24,7 +24,7 @@ import (
type graphicsDriverCreator interface {
newAuto() (graphicsdriver.Graphics, error)
newOpenGL() (graphicsdriver.Graphics, error)
getDirectX() graphicsdriver.Graphics
newDirectX() (graphicsdriver.Graphics, error)
newMetal() (graphicsdriver.Graphics, error)
}
@ -51,10 +51,14 @@ func newGraphicsDriver(creator graphicsDriverCreator) (graphicsdriver.Graphics,
}
return g, nil
case "directx":
if g := creator.getDirectX(); g != nil {
return g, nil
g, err := creator.newDirectX()
if err != nil {
return nil, err
}
return nil, fmt.Errorf("ui: %s=%s is specified but DirectX is not available.", envName, env)
if g == nil {
return nil, fmt.Errorf("ui: %s=%s is specified but DirectX is not available.", envName, env)
}
return g, nil
case "metal":
g, err := creator.newMetal()
if err != nil {

View File

@ -31,8 +31,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
return nil
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
return nil, nil
}
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {

View File

@ -35,8 +35,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
return nil
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
return nil, nil
}
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {

View File

@ -276,8 +276,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
return nil
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
return nil, nil
}
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {

View File

@ -43,8 +43,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
return nil
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
return nil, nil
}
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {

View File

@ -36,24 +36,26 @@ type graphicsDriverCreatorImpl struct {
}
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) {
if d := g.getDirectX(); d != nil {
d, err1 := g.newDirectX()
if err1 == nil {
return d, nil
}
return g.newOpenGL()
o, err2 := g.newOpenGL()
if err2 == nil {
return o, nil
}
return nil, fmt.Errorf("ui: failed to choose graphics drivers: DirectX: %v, OpenGL: %v", err1, err2)
}
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (g *graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
if g.transparent {
return nil
return nil, fmt.Errorf("ui: DirectX is not available with a transparent window")
}
if d := directx.Get(); d != nil {
return d
}
return nil
return directx.NewGraphics()
}
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {

View File

@ -45,8 +45,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
return nil
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
return nil, nil
}
func (g *graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {

View File

@ -35,8 +35,8 @@ func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics()
}
func (*graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics {
return nil
func (*graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
return nil, nil
}
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {