diff --git a/graphics.go b/graphics.go index a9d402afa..ae0d19283 100644 --- a/graphics.go +++ b/graphics.go @@ -16,6 +16,7 @@ package ebiten import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" + "github.com/hajimehoshi/ebiten/v2/internal/ui" ) // Filter represents the type of texture filter to be used when an image is maginified or minified. @@ -82,3 +83,29 @@ const ( // c_out = c_src * c_dst CompositeModeMultiply CompositeMode = CompositeMode(graphicsdriver.CompositeModeMultiply) ) + +// GraphicsLibrary represets graphics libraries supported by the engine. +type GraphicsLibrary = ui.GraphicsLibrary + +const ( + // GraphicsLibraryUnknown represents the state at which graphics library cannot be determined, + // e.g. hasn't loaded yet or failed to initialize. + GraphicsLibraryUnknown = ui.GraphicsLibraryUnknown + // GraphicsLibraryOpenGL current graphics library used is OpenGL. + GraphicsLibraryOpenGL = ui.GraphicsLibraryOpenGL + // GraphicsLibraryDirectX current graphics library used is Microsoft DirectX. + GraphicsLibraryDirectX = ui.GraphicsLibraryDirectX + // GraphicsLibraryMetal current graphics library used is Apple's Metal. + GraphicsLibraryMetal = ui.GraphicsLibraryMetal +) + +// DebugInfo is a struct to store debug info about the graphics. +type DebugInfo struct { + // GraphicsLibrary represents the graphics library currently in use. + GraphicsLibrary GraphicsLibrary +} + +// ReadDebugInfo writes debug info (e.g. current graphics library) into a provided struct. +func ReadDebugInfo(d *DebugInfo) { + d.GraphicsLibrary = ui.GetGraphicsLibrary() +} diff --git a/internal/ui/context.go b/internal/ui/context.go index fe6f1135c..b97694f18 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -294,6 +294,7 @@ type globalState struct { fpsMode_ int32 isScreenClearedEveryFrame_ int32 screenFilterEnabled_ int32 + graphicsLibrary_ int32 } func (g *globalState) error() error { @@ -342,6 +343,14 @@ func (g *globalState) setScreenFilterEnabled(enabled bool) { atomic.StoreInt32(&g.screenFilterEnabled_, v) } +func (g *globalState) setGraphicsLibrary(library GraphicsLibrary) { + atomic.StoreInt32(&g.graphicsLibrary_, int32(library)) +} + +func (g *globalState) graphicsLibrary() GraphicsLibrary { + return GraphicsLibrary(atomic.LoadInt32(&g.graphicsLibrary_)) +} + func FPSMode() FPSModeType { return theGlobalState.fpsMode() } @@ -366,3 +375,7 @@ func IsScreenFilterEnabled() bool { func SetScreenFilterEnabled(enabled bool) { theGlobalState.setScreenFilterEnabled(enabled) } + +func GetGraphicsLibrary() GraphicsLibrary { + return theGlobalState.graphicsLibrary() +} diff --git a/internal/ui/graphics.go b/internal/ui/graphics.go index e33ea1888..552083152 100644 --- a/internal/ui/graphics.go +++ b/internal/ui/graphics.go @@ -22,7 +22,7 @@ import ( ) type graphicsDriverCreator interface { - newAuto() (graphicsdriver.Graphics, error) + newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) newOpenGL() (graphicsdriver.Graphics, error) newDirectX() (graphicsdriver.Graphics, error) newMetal() (graphicsdriver.Graphics, error) @@ -39,13 +39,14 @@ func newGraphicsDriver(creator graphicsDriverCreator) (graphicsdriver.Graphics, switch env { case "", "auto": - g, err := creator.newAuto() + g, lib, err := creator.newAuto() if err != nil { return nil, err } if g == nil { return nil, fmt.Errorf("ui: no graphics library is available") } + theGlobalState.setGraphicsLibrary(lib) return g, nil case "opengl": g, err := creator.newOpenGL() @@ -55,6 +56,7 @@ func newGraphicsDriver(creator graphicsDriverCreator) (graphicsdriver.Graphics, if g == nil { return nil, fmt.Errorf("ui: %s=%s is specified but OpenGL is not available", envName, env) } + theGlobalState.setGraphicsLibrary(GraphicsLibraryOpenGL) return g, nil case "directx": g, err := creator.newDirectX() @@ -64,6 +66,7 @@ func newGraphicsDriver(creator graphicsDriverCreator) (graphicsdriver.Graphics, if g == nil { return nil, fmt.Errorf("ui: %s=%s is specified but DirectX is not available.", envName, env) } + theGlobalState.setGraphicsLibrary(GraphicsLibraryDirectX) return g, nil case "metal": g, err := creator.newMetal() @@ -73,6 +76,7 @@ func newGraphicsDriver(creator graphicsDriverCreator) (graphicsdriver.Graphics, if g == nil { return nil, fmt.Errorf("ui: %s=%s is specified but Metal is not available", envName, env) } + theGlobalState.setGraphicsLibrary(GraphicsLibraryMetal) return g, nil default: return nil, fmt.Errorf("ui: an unsupported graphics library is specified: %s", env) diff --git a/internal/ui/ui.go b/internal/ui/ui.go index a4998529c..dc8d68f11 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -71,6 +71,15 @@ const ( WindowResizingModeEnabled ) +type GraphicsLibrary int + +const ( + GraphicsLibraryUnknown GraphicsLibrary = iota + GraphicsLibraryOpenGL + GraphicsLibraryDirectX + GraphicsLibraryMetal +) + type UserInterface struct { userInterfaceImpl } diff --git a/internal/ui/ui_android.go b/internal/ui/ui_android.go index 732661630..228ed04ee 100644 --- a/internal/ui/ui_android.go +++ b/internal/ui/ui_android.go @@ -23,8 +23,9 @@ type graphicsDriverCreatorImpl struct { gomobileBuild bool } -func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) { - return g.newOpenGL() +func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { + graphics, err := g.newOpenGL() + return graphics, GraphicsLibraryOpenGL, err } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_cbackend.go b/internal/ui/ui_cbackend.go index b9518de14..2ce09c72c 100644 --- a/internal/ui/ui_cbackend.go +++ b/internal/ui/ui_cbackend.go @@ -27,8 +27,9 @@ import ( type graphicsDriverCreatorImpl struct{} -func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) { - return g.newOpenGL() +func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { + graphics, err := g.newOpenGL() + return graphics, GraphicsLibraryOpenGL, err } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_glfw_darwin.go b/internal/ui/ui_glfw_darwin.go index 18ef7174c..63fb532b4 100644 --- a/internal/ui/ui_glfw_darwin.go +++ b/internal/ui/ui_glfw_darwin.go @@ -260,16 +260,16 @@ type graphicsDriverCreatorImpl struct { transparent bool } -func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) { +func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { m, err1 := g.newMetal() if err1 == nil { - return m, nil + return m, GraphicsLibraryMetal, nil } o, err2 := g.newOpenGL() if err2 == nil { - return o, nil + return o, GraphicsLibraryOpenGL, nil } - return nil, fmt.Errorf("ui: failed to choose graphics drivers: Metal: %v, OpenGL: %v", err1, err2) + return nil, GraphicsLibraryUnknown, fmt.Errorf("ui: failed to choose graphics drivers: Metal: %v, OpenGL: %v", err1, err2) } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_glfw_unix.go b/internal/ui/ui_glfw_unix.go index 0f2a482a6..aa00adb16 100644 --- a/internal/ui/ui_glfw_unix.go +++ b/internal/ui/ui_glfw_unix.go @@ -35,8 +35,9 @@ type graphicsDriverCreatorImpl struct { transparent bool } -func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) { - return g.newOpenGL() +func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { + graphics, err := g.newOpenGL() + return graphics, GraphicsLibraryOpenGL, err } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_glfw_windows.go b/internal/ui/ui_glfw_windows.go index 2f6ff6e3a..34fda401f 100644 --- a/internal/ui/ui_glfw_windows.go +++ b/internal/ui/ui_glfw_windows.go @@ -36,16 +36,16 @@ type graphicsDriverCreatorImpl struct { transparent bool } -func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) { +func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { d, err1 := g.newDirectX() if err1 == nil { - return d, nil + return d, GraphicsLibraryDirectX, nil } o, err2 := g.newOpenGL() if err2 == nil { - return o, nil + return o, GraphicsLibraryOpenGL, nil } - return nil, fmt.Errorf("ui: failed to choose graphics drivers: DirectX: %v, OpenGL: %v", err1, err2) + return nil, GraphicsLibraryUnknown, fmt.Errorf("ui: failed to choose graphics drivers: DirectX: %v, OpenGL: %v", err1, err2) } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_ios.go b/internal/ui/ui_ios.go index 6eb9ae62a..40096c4c6 100644 --- a/internal/ui/ui_ios.go +++ b/internal/ui/ui_ios.go @@ -29,16 +29,16 @@ type graphicsDriverCreatorImpl struct { gomobileBuild bool } -func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) { +func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { m, err1 := g.newMetal() if err1 == nil { - return m, nil + return m, GraphicsLibraryMetal, nil } o, err2 := g.newOpenGL() if err2 == nil { - return o, nil + return o, GraphicsLibraryMetal, nil } - return nil, fmt.Errorf("ui: failed to choose graphics drivers: Metal: %v, OpenGL: %v", err1, err2) + return nil, GraphicsLibraryUnknown, fmt.Errorf("ui: failed to choose graphics drivers: Metal: %v, OpenGL: %v", err1, err2) } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) { diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 81243c921..d295db75b 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -27,8 +27,9 @@ import ( type graphicsDriverCreatorImpl struct{} -func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) { - return g.newOpenGL() +func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) { + graphics, err := g.newOpenGL() + return graphics, GraphicsLibraryOpenGL, err } func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {