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

View File

@ -24,7 +24,7 @@ import (
type graphicsDriverCreator interface { type graphicsDriverCreator interface {
newAuto() (graphicsdriver.Graphics, error) newAuto() (graphicsdriver.Graphics, error)
newOpenGL() (graphicsdriver.Graphics, error) newOpenGL() (graphicsdriver.Graphics, error)
getDirectX() graphicsdriver.Graphics newDirectX() (graphicsdriver.Graphics, error)
newMetal() (graphicsdriver.Graphics, error) newMetal() (graphicsdriver.Graphics, error)
} }
@ -51,10 +51,14 @@ func newGraphicsDriver(creator graphicsDriverCreator) (graphicsdriver.Graphics,
} }
return g, nil return g, nil
case "directx": case "directx":
if g := creator.getDirectX(); g != nil { g, err := creator.newDirectX()
return g, nil 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": case "metal":
g, err := creator.newMetal() g, err := creator.newMetal()
if err != nil { if err != nil {

View File

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

View File

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

View File

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

View File

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

View File

@ -36,24 +36,26 @@ type graphicsDriverCreatorImpl struct {
} }
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) { func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) {
if d := g.getDirectX(); d != nil { d, err1 := g.newDirectX()
if err1 == nil {
return d, 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) { func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
return opengl.NewGraphics() return opengl.NewGraphics()
} }
func (g *graphicsDriverCreatorImpl) getDirectX() graphicsdriver.Graphics { func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
if g.transparent { 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 directx.NewGraphics()
return d
}
return nil
} }
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) { func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {

View File

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

View File

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