Skip to content

Commit

Permalink
ebiten: add ReadDebugInfo for getting debug info (only graphics lib…
Browse files Browse the repository at this point in the history
…ray so far) (#2222)

Closes #2177
  • Loading branch information
RusAD32 authored Jul 30, 2022
1 parent 37fe3cd commit 3ac37e2
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 22 deletions.
27 changes: 27 additions & 0 deletions graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}
13 changes: 13 additions & 0 deletions internal/ui/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ type globalState struct {
fpsMode_ int32
isScreenClearedEveryFrame_ int32
screenFilterEnabled_ int32
graphicsLibrary_ int32
}

func (g *globalState) error() error {
Expand Down Expand Up @@ -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()
}
Expand All @@ -366,3 +375,7 @@ func IsScreenFilterEnabled() bool {
func SetScreenFilterEnabled(enabled bool) {
theGlobalState.setScreenFilterEnabled(enabled)
}

func GetGraphicsLibrary() GraphicsLibrary {
return theGlobalState.graphicsLibrary()
}
8 changes: 6 additions & 2 deletions internal/ui/graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ const (
WindowResizingModeEnabled
)

type GraphicsLibrary int

const (
GraphicsLibraryUnknown GraphicsLibrary = iota
GraphicsLibraryOpenGL
GraphicsLibraryDirectX
GraphicsLibraryMetal
)

type UserInterface struct {
userInterfaceImpl
}
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/ui_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/ui_cbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/ui_glfw_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/ui_glfw_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/ui_glfw_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/ui_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/ui_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3ac37e2

Please sign in to comment.