mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
internal/graphicsdriver/opengl: move initGL to NewGraphics
As restoring from context lost doesn't happen on browsers, initilizing GL doesn't have to be done at reset.
This commit is contained in:
parent
4082223aec
commit
bae5aa7732
@ -17,8 +17,6 @@ package opengl
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall/js"
|
||||
"unsafe"
|
||||
|
||||
@ -46,17 +44,6 @@ const (
|
||||
invalidUniform = -1
|
||||
)
|
||||
|
||||
func webGL2MightBeAvailable() bool {
|
||||
env := os.Getenv("EBITENGINE_OPENGL")
|
||||
for _, t := range strings.Split(env, ",") {
|
||||
switch strings.TrimSpace(t) {
|
||||
case "webgl1":
|
||||
return false
|
||||
}
|
||||
}
|
||||
return js.Global().Get("WebGL2RenderingContext").Truthy()
|
||||
}
|
||||
|
||||
type contextImpl struct {
|
||||
ctx gl.Context
|
||||
canvas js.Value
|
||||
@ -66,52 +53,6 @@ type contextImpl struct {
|
||||
fnIsContextLost js.Value
|
||||
}
|
||||
|
||||
func (c *context) initGL() error {
|
||||
var glContext js.Value
|
||||
|
||||
if doc := js.Global().Get("document"); doc.Truthy() {
|
||||
canvas := c.canvas
|
||||
attr := js.Global().Get("Object").New()
|
||||
attr.Set("alpha", true)
|
||||
attr.Set("premultipliedAlpha", true)
|
||||
attr.Set("stencil", true)
|
||||
|
||||
if webGL2MightBeAvailable() {
|
||||
glContext = canvas.Call("getContext", "webgl2", attr)
|
||||
if glContext.Truthy() {
|
||||
c.webGL2 = true
|
||||
}
|
||||
}
|
||||
|
||||
// Even though WebGL2RenderingContext exists, getting a webgl2 context might fail (#1738).
|
||||
if !glContext.Truthy() {
|
||||
glContext = canvas.Call("getContext", "webgl", attr)
|
||||
if !glContext.Truthy() {
|
||||
glContext = canvas.Call("getContext", "experimental-webgl", attr)
|
||||
}
|
||||
if glContext.Truthy() {
|
||||
c.webGL2 = false
|
||||
}
|
||||
}
|
||||
|
||||
if !glContext.Truthy() {
|
||||
c.webGL2 = false
|
||||
return fmt.Errorf("opengl: getContext failed")
|
||||
}
|
||||
}
|
||||
|
||||
ctx, err := gl.NewDefaultContext(glContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.ctx = ctx
|
||||
|
||||
c.fnGetExtension = glContext.Get("getExtension").Call("bind", glContext)
|
||||
c.fnIsContextLost = glContext.Get("isContextLost").Call("bind", glContext)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *context) reset() error {
|
||||
c.locationCache = newLocationCache()
|
||||
c.lastTexture = 0
|
||||
@ -120,10 +61,6 @@ func (c *context) reset() error {
|
||||
c.lastViewportHeight = 0
|
||||
c.lastBlend = graphicsdriver.Blend{}
|
||||
|
||||
if err := c.initGL(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.fnIsContextLost.Invoke().Bool() {
|
||||
return graphicsdriver.GraphicsNotReady
|
||||
}
|
||||
|
@ -15,15 +15,66 @@
|
||||
package opengl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
|
||||
)
|
||||
|
||||
// NewGraphics creates an implementation of graphicsdriver.Graphics for OpenGL.
|
||||
// The returned graphics value is nil iff the error is not nil.
|
||||
func NewGraphics(canvas js.Value) (graphicsdriver.Graphics, error) {
|
||||
var glContext js.Value
|
||||
|
||||
attr := js.Global().Get("Object").New()
|
||||
attr.Set("alpha", true)
|
||||
attr.Set("premultipliedAlpha", true)
|
||||
attr.Set("stencil", true)
|
||||
|
||||
var webGL2 bool
|
||||
if webGL2MightBeAvailable() {
|
||||
glContext = canvas.Call("getContext", "webgl2", attr)
|
||||
}
|
||||
|
||||
if glContext.Truthy() {
|
||||
webGL2 = true
|
||||
} else {
|
||||
// Even though WebGL2RenderingContext exists, getting a webgl2 context might fail (#1738).
|
||||
glContext = canvas.Call("getContext", "webgl", attr)
|
||||
if !glContext.Truthy() {
|
||||
glContext = canvas.Call("getContext", "experimental-webgl", attr)
|
||||
}
|
||||
}
|
||||
|
||||
if !glContext.Truthy() {
|
||||
return nil, fmt.Errorf("opengl: getContext failed")
|
||||
}
|
||||
|
||||
ctx, err := gl.NewDefaultContext(glContext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g := &Graphics{}
|
||||
g.context.canvas = canvas
|
||||
g.context.ctx = ctx
|
||||
g.context.webGL2 = webGL2
|
||||
g.context.fnGetExtension = glContext.Get("getExtension").Call("bind", glContext)
|
||||
g.context.fnIsContextLost = glContext.Get("isContextLost").Call("bind", glContext)
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
func webGL2MightBeAvailable() bool {
|
||||
env := os.Getenv("EBITENGINE_OPENGL")
|
||||
for _, t := range strings.Split(env, ",") {
|
||||
switch strings.TrimSpace(t) {
|
||||
case "webgl1":
|
||||
return false
|
||||
}
|
||||
}
|
||||
return js.Global().Get("WebGL2RenderingContext").Truthy()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user