From e8810495cf361a7bef7745d0fbc3b2bdd7042e42 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 14 Nov 2022 02:03:36 +0900 Subject: [PATCH] internal/graphicsdriver/opengl/gl: reland: refactoring: reduce opengles build tags This is a reland of c9cff69dcba6755b024c30ad6214f0f8ced42267. Updates #292 --- internal/graphicsdriver/opengl/context_js.go | 5 +++++ .../graphicsdriver/opengl/context_notjs.go | 4 ++++ .../opengl/gl/default_android.go | 4 ++++ .../opengl/gl/default_notpurego.go | 4 ++++ .../opengl/gl/default_purego.go | 4 ++++ internal/graphicsdriver/opengl/gl/gomobile.go | 4 ++++ .../graphicsdriver/opengl/gl/interface.go | 1 + .../opengl/gl/procaddr_darwin.go | 6 +++++- .../graphicsdriver/opengl/gl/procaddr_gl.go | 2 ++ .../graphicsdriver/opengl/gl/procaddr_gles.go | 2 ++ .../opengl/gl/procaddr_nintendosdk.go | 2 ++ .../opengl/gl/procaddr_windows.go | 1 + internal/graphicsdriver/opengl/graphics.go | 7 ++++++- .../opengl/needsrestoring_gles.go | 21 ------------------- .../opengl/needsrestoring_notgles.go | 19 ----------------- 15 files changed, 44 insertions(+), 42 deletions(-) delete mode 100644 internal/graphicsdriver/opengl/needsrestoring_gles.go delete mode 100644 internal/graphicsdriver/opengl/needsrestoring_notgles.go diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index b5f654e50..cb88ad8d8 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -608,3 +608,8 @@ func (c *context) endStencilWithEvenOddRule() { c.gl.stencilOp.Invoke(gl.KEEP, gl.KEEP, gl.KEEP) c.gl.colorMask.Invoke(true, true, true, true) } + +func (c *context) isES() bool { + // WebGL is compatible with GLES. + return true +} diff --git a/internal/graphicsdriver/opengl/context_notjs.go b/internal/graphicsdriver/opengl/context_notjs.go index 514483345..1edd2daf2 100644 --- a/internal/graphicsdriver/opengl/context_notjs.go +++ b/internal/graphicsdriver/opengl/context_notjs.go @@ -477,3 +477,7 @@ func (c *context) endStencilWithEvenOddRule() { c.ctx.StencilOp(gl.KEEP, gl.KEEP, gl.KEEP) c.ctx.ColorMask(true, true, true, true) } + +func (c *context) isES() bool { + return c.ctx.IsES() +} diff --git a/internal/graphicsdriver/opengl/gl/default_android.go b/internal/graphicsdriver/opengl/gl/default_android.go index 5e6232615..5090a6ca9 100644 --- a/internal/graphicsdriver/opengl/gl/default_android.go +++ b/internal/graphicsdriver/opengl/gl/default_android.go @@ -41,6 +41,10 @@ func (defaultContext) Init() error { return nil } +func (defaultContext) IsES() bool { + return true +} + func (defaultContext) ActiveTexture(texture uint32) { C.glActiveTexture(C.GLenum(texture)) } diff --git a/internal/graphicsdriver/opengl/gl/default_notpurego.go b/internal/graphicsdriver/opengl/gl/default_notpurego.go index 67305030d..563e1f923 100644 --- a/internal/graphicsdriver/opengl/gl/default_notpurego.go +++ b/internal/graphicsdriver/opengl/gl/default_notpurego.go @@ -406,6 +406,10 @@ func boolToInt(b bool) int { return 0 } +func (c *defaultContext) IsES() bool { + return isES +} + func (c *defaultContext) ActiveTexture(texture uint32) { C.glowActiveTexture(c.gpActiveTexture, (C.GLenum)(texture)) } diff --git a/internal/graphicsdriver/opengl/gl/default_purego.go b/internal/graphicsdriver/opengl/gl/default_purego.go index 1e08bb49f..f6b02befc 100644 --- a/internal/graphicsdriver/opengl/gl/default_purego.go +++ b/internal/graphicsdriver/opengl/gl/default_purego.go @@ -107,6 +107,10 @@ func boolToInt(b bool) int { return 0 } +func (c *defaultContext) IsES() bool { + return isES +} + func (c *defaultContext) ActiveTexture(texture uint32) { purego.SyscallN(c.gpActiveTexture, uintptr(texture)) } diff --git a/internal/graphicsdriver/opengl/gl/gomobile.go b/internal/graphicsdriver/opengl/gl/gomobile.go index ce9db5d4a..45a597a2d 100644 --- a/internal/graphicsdriver/opengl/gl/gomobile.go +++ b/internal/graphicsdriver/opengl/gl/gomobile.go @@ -39,6 +39,10 @@ func (g *gomobileContext) Init() error { return nil } +func (g *gomobileContext) IsES() bool { + return true +} + func (g *gomobileContext) ActiveTexture(texture uint32) { g.ctx.ActiveTexture(gl.Enum(texture)) } diff --git a/internal/graphicsdriver/opengl/gl/interface.go b/internal/graphicsdriver/opengl/gl/interface.go index 82ca63a7a..e5a011f07 100644 --- a/internal/graphicsdriver/opengl/gl/interface.go +++ b/internal/graphicsdriver/opengl/gl/interface.go @@ -20,6 +20,7 @@ package gl // See https://pkg.go.dev/golang.org/x/mobile/gl#Context type Context interface { Init() error + IsES() bool ActiveTexture(texture uint32) AttachShader(program uint32, shader uint32) diff --git a/internal/graphicsdriver/opengl/gl/procaddr_darwin.go b/internal/graphicsdriver/opengl/gl/procaddr_darwin.go index cf88e94d5..dd9b18fff 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_darwin.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_darwin.go @@ -18,11 +18,15 @@ import ( "github.com/ebitengine/purego" ) -var opengl uintptr +var ( + opengl uintptr + isES bool +) func init() { opengl = purego.Dlopen("/System/Library/Frameworks/OpenGLES.framework/Versions/Current/OpenGLES", purego.RTLD_GLOBAL) if opengl != 0 { + isES = true return } opengl = purego.Dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", purego.RTLD_GLOBAL) diff --git a/internal/graphicsdriver/opengl/gl/procaddr_gl.go b/internal/graphicsdriver/opengl/gl/procaddr_gl.go index 36edc228f..fe341ee59 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_gl.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_gl.go @@ -28,6 +28,8 @@ import "C" import "unsafe" +var isES = false + func getProcAddress(namea string) unsafe.Pointer { cname := C.CString(namea) defer C.free(unsafe.Pointer(cname)) diff --git a/internal/graphicsdriver/opengl/gl/procaddr_gles.go b/internal/graphicsdriver/opengl/gl/procaddr_gles.go index 8a0dec9e6..79ca45434 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_gles.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_gles.go @@ -35,6 +35,8 @@ import ( "unsafe" ) +var isES = true + func getProcAddress(namea string) unsafe.Pointer { const ext = "EXT" if strings.HasSuffix(namea, ext) { diff --git a/internal/graphicsdriver/opengl/gl/procaddr_nintendosdk.go b/internal/graphicsdriver/opengl/gl/procaddr_nintendosdk.go index 84babbded..cb2c26786 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_nintendosdk.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_nintendosdk.go @@ -28,6 +28,8 @@ import "C" import "unsafe" +var isES = false + func getProcAddress(namea string) unsafe.Pointer { cname := C.CString(namea) defer C.free(unsafe.Pointer(cname)) diff --git a/internal/graphicsdriver/opengl/gl/procaddr_windows.go b/internal/graphicsdriver/opengl/gl/procaddr_windows.go index e0a946d5b..68bc9b9d9 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_windows.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_windows.go @@ -24,6 +24,7 @@ import ( var ( opengl32 = windows.NewLazySystemDLL("opengl32") procWglGetProcAddress = opengl32.NewProc("wglGetProcAddress") + isES = false ) func getProcAddress(namea string) uintptr { diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index e9dddb310..3496103cf 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -16,6 +16,7 @@ package opengl import ( "fmt" + "runtime" "github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" @@ -265,7 +266,11 @@ func (g *Graphics) SetFullscreen(fullscreen bool) { } func (g *Graphics) NeedsRestoring() bool { - return needsRestoring + // Though it is possible to have a logic to restore the graphics data for GPU, do not use it for performance (#1603). + if runtime.GOOS == "js" { + return false + } + return g.context.isES() } func (g *Graphics) NeedsClearingScreen() bool { diff --git a/internal/graphicsdriver/opengl/needsrestoring_gles.go b/internal/graphicsdriver/opengl/needsrestoring_gles.go deleted file mode 100644 index 0d0f5523c..000000000 --- a/internal/graphicsdriver/opengl/needsrestoring_gles.go +++ /dev/null @@ -1,21 +0,0 @@ -// 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. - -//go:build !android && !ios && !opengles - -package opengl - -// Though it is possible to have a logic to restore the graphics data for GPU, do not use it for performance (#1603). - -const needsRestoring = false diff --git a/internal/graphicsdriver/opengl/needsrestoring_notgles.go b/internal/graphicsdriver/opengl/needsrestoring_notgles.go deleted file mode 100644 index 5399d7a61..000000000 --- a/internal/graphicsdriver/opengl/needsrestoring_notgles.go +++ /dev/null @@ -1,19 +0,0 @@ -// 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. - -//go:build android || ios || opengles - -package opengl - -const needsRestoring = true