2022-06-11 08:02:10 +02:00
|
|
|
// Copyright 2022 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
2022-06-11 08:34:17 +02:00
|
|
|
import (
|
2022-11-17 04:34:53 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-06-11 08:34:17 +02:00
|
|
|
"syscall/js"
|
2022-11-13 07:44:15 +01:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2022-11-17 04:34:53 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
|
2022-06-11 08:34:17 +02:00
|
|
|
)
|
|
|
|
|
2022-11-13 07:25:22 +01:00
|
|
|
// 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) {
|
2022-11-17 04:34:53 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-13 07:25:22 +01:00
|
|
|
g := &Graphics{}
|
|
|
|
g.context.canvas = canvas
|
2022-11-17 04:34:53 +01:00
|
|
|
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)
|
|
|
|
|
2022-11-13 07:25:22 +01:00
|
|
|
return g, nil
|
2022-10-15 15:22:25 +02:00
|
|
|
}
|
2022-11-17 04:34:53 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|