internal/graphicsdriver/directx: refactoring

This commit is contained in:
Hajime Hoshi 2023-03-26 00:49:30 +09:00
parent 902d7244eb
commit 0b46d2b799
2 changed files with 47 additions and 45 deletions

View File

@ -18,8 +18,6 @@ import (
"errors"
"fmt"
"math"
"os"
"strings"
"time"
"unsafe"
@ -122,58 +120,22 @@ type graphics12 struct {
pipelineStates
}
func (g *graphics12) initialize() (ferr error) {
var (
useWARP bool
useDebugLayer bool
)
env := os.Getenv("EBITENGINE_DIRECTX")
if env == "" {
// For backward compatibility, read the EBITEN_ version.
env = os.Getenv("EBITEN_DIRECTX")
}
for _, t := range strings.Split(env, ",") {
switch strings.TrimSpace(t) {
case "warp":
// TODO: Is WARP available on Xbox?
useWARP = true
case "debug":
useDebugLayer = true
}
}
// Specify the level 11 by default.
// Some old cards don't work well with the default feature level (#2447, #2486).
var featureLevel _D3D_FEATURE_LEVEL = _D3D_FEATURE_LEVEL_11_0
if env := os.Getenv("EBITENGINE_DIRECTX_FEATURE_LEVEL"); env != "" {
switch env {
case "11_0":
featureLevel = _D3D_FEATURE_LEVEL_11_0
case "11_1":
featureLevel = _D3D_FEATURE_LEVEL_11_1
case "12_0":
featureLevel = _D3D_FEATURE_LEVEL_12_0
case "12_1":
featureLevel = _D3D_FEATURE_LEVEL_12_1
case "12_2":
featureLevel = _D3D_FEATURE_LEVEL_12_2
}
}
func newGraphics12(useWARP bool, useDebugLayer bool, featureLevel _D3D_FEATURE_LEVEL) (*graphics12, error) {
g := &graphics12{}
// Initialize not only a device but also other members like a fence.
// Even if initializing a device succeeds, initializing a fence might fail (#2142).
if microsoftgdk.IsXbox() {
if err := g.initializeXbox(useWARP, useDebugLayer); err != nil {
return err
return nil, err
}
} else {
if err := g.initializeDesktop(useWARP, useDebugLayer, featureLevel); err != nil {
return err
return nil, err
}
}
return nil
return g, nil
}
func (g *graphics12) initializeDesktop(useWARP bool, useDebugLayer bool, featureLevel _D3D_FEATURE_LEVEL) (ferr error) {

View File

@ -15,15 +15,55 @@
package directx
import (
"os"
"strings"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
)
// NewGraphics creates an implementation of graphicsdriver.Graphics for DirectX.
// The returned graphics value is nil iff the error is not nil.
func NewGraphics() (graphicsdriver.Graphics, error) {
var (
useWARP bool
useDebugLayer bool
)
env := os.Getenv("EBITENGINE_DIRECTX")
if env == "" {
// For backward compatibility, read the EBITEN_ version.
env = os.Getenv("EBITEN_DIRECTX")
}
for _, t := range strings.Split(env, ",") {
switch strings.TrimSpace(t) {
case "warp":
// TODO: Is WARP available on Xbox?
useWARP = true
case "debug":
useDebugLayer = true
}
}
// Specify the level 11 by default.
// Some old cards don't work well with the default feature level (#2447, #2486).
var featureLevel _D3D_FEATURE_LEVEL = _D3D_FEATURE_LEVEL_11_0
if env := os.Getenv("EBITENGINE_DIRECTX_FEATURE_LEVEL"); env != "" {
switch env {
case "11_0":
featureLevel = _D3D_FEATURE_LEVEL_11_0
case "11_1":
featureLevel = _D3D_FEATURE_LEVEL_11_1
case "12_0":
featureLevel = _D3D_FEATURE_LEVEL_12_0
case "12_1":
featureLevel = _D3D_FEATURE_LEVEL_12_1
case "12_2":
featureLevel = _D3D_FEATURE_LEVEL_12_2
}
}
// TODO: Implement a new graphics for DirectX 11 (#2613).
g := &graphics12{}
if err := g.initialize(); err != nil {
g, err := newGraphics12(useWARP, useDebugLayer, featureLevel)
if err != nil {
return nil, err
}
return g, nil