diff --git a/internal/graphicsdriver/opengl/context.go b/internal/graphicsdriver/opengl/context.go index 328e47204..5ab146b8e 100644 --- a/internal/graphicsdriver/opengl/context.go +++ b/internal/graphicsdriver/opengl/context.go @@ -127,7 +127,7 @@ type context struct { highpOnce sync.Once initOnce sync.Once - contextImpl + contextPlatform } func (c *context) bindTexture(t textureNative) { @@ -156,27 +156,33 @@ func (c *context) bindFramebuffer(f framebufferNative) { func (c *context) setViewport(f *framebuffer) { c.bindFramebuffer(f.native) - if c.lastViewportWidth != f.width || c.lastViewportHeight != f.height { - // On some environments, viewport size must be within the framebuffer size. - // e.g. Edge (#71), Chrome on GPD Pocket (#420), macOS Mojave (#691). - // Use the same size of the framebuffer here. - c.ctx.Viewport(0, 0, int32(f.width), int32(f.height)) + if c.lastViewportWidth == f.width && c.lastViewportHeight == f.height { + return + } - // glViewport must be called at least at every frame on iOS. - // As the screen framebuffer is the last render target, next SetViewport should be - // the first call at a frame. - if f.native == c.screenFramebuffer { - c.lastViewportWidth = 0 - c.lastViewportHeight = 0 - } else { - c.lastViewportWidth = f.width - c.lastViewportHeight = f.height - } + // On some environments, viewport size must be within the framebuffer size. + // e.g. Edge (#71), Chrome on GPD Pocket (#420), macOS Mojave (#691). + // Use the same size of the framebuffer here. + c.ctx.Viewport(0, 0, int32(f.width), int32(f.height)) + + // glViewport must be called at least at every frame on iOS. + // As the screen framebuffer is the last render target, next SetViewport should be + // the first call at a frame. + if f.native == c.screenFramebuffer { + c.lastViewportWidth = 0 + c.lastViewportHeight = 0 + } else { + c.lastViewportWidth = f.width + c.lastViewportHeight = f.height } } -func (c *context) getScreenFramebuffer() framebufferNative { - return c.screenFramebuffer +func (c *context) newScreenFramebuffer(width, height int) *framebuffer { + return &framebuffer{ + native: c.screenFramebuffer, + width: width, + height: height, + } } func (c *context) getMaxTextureSize() int { @@ -334,24 +340,28 @@ func (c *context) deleteRenderbuffer(r renderbufferNative) { c.ctx.DeleteRenderbuffer(uint32(r)) } -func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) { +func (c *context) newFramebuffer(texture textureNative, width, height int) (*framebuffer, error) { f := c.ctx.CreateFramebuffer() if f <= 0 { - return 0, fmt.Errorf("opengl: creating framebuffer failed: the returned value is not positive but %d", f) + return nil, fmt.Errorf("opengl: creating framebuffer failed: the returned value is not positive but %d", f) } c.bindFramebuffer(framebufferNative(f)) c.ctx.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0) if s := c.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE { if s != 0 { - return 0, fmt.Errorf("opengl: creating framebuffer failed: %v", s) + return nil, fmt.Errorf("opengl: creating framebuffer failed: %v", s) } if e := c.ctx.GetError(); e != gl.NO_ERROR { - return 0, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e) + return nil, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e) } - return 0, fmt.Errorf("opengl: creating framebuffer failed: unknown error") + return nil, fmt.Errorf("opengl: creating framebuffer failed: unknown error") } - return framebufferNative(f), nil + return &framebuffer{ + native: framebufferNative(f), + width: width, + height: height, + }, nil } func (c *context) bindStencilBuffer(f framebufferNative, r renderbufferNative) error { @@ -365,6 +375,9 @@ func (c *context) bindStencilBuffer(f framebufferNative, r renderbufferNative) e } func (c *context) deleteFramebuffer(f framebufferNative) { + if f == c.screenFramebuffer { + return + } if !c.ctx.IsFramebuffer(uint32(f)) { return } diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index 98fd27577..b64452fb6 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -16,9 +16,18 @@ package opengl import ( "syscall/js" + + "github.com/hajimehoshi/ebiten/v2/internal/shaderir/glsl" ) -type contextImpl struct { +type contextPlatform struct { canvas js.Value webGL2 bool } + +func (c *context) glslVersion() glsl.GLSLVersion { + if c.webGL2 { + return glsl.GLSLVersionES300 + } + return glsl.GLSLVersionES100 +} diff --git a/internal/graphicsdriver/opengl/context_notjs.go b/internal/graphicsdriver/opengl/context_notjs.go index d69921eb6..cf424e76f 100644 --- a/internal/graphicsdriver/opengl/context_notjs.go +++ b/internal/graphicsdriver/opengl/context_notjs.go @@ -16,5 +16,16 @@ package opengl -type contextImpl struct { +import ( + "github.com/hajimehoshi/ebiten/v2/internal/shaderir/glsl" +) + +type contextPlatform struct { +} + +func (c *context) glslVersion() glsl.GLSLVersion { + if c.ctx.IsES() { + return glsl.GLSLVersionES100 + } + return glsl.GLSLVersionDefault } diff --git a/internal/graphicsdriver/opengl/framebuffer.go b/internal/graphicsdriver/opengl/framebuffer.go deleted file mode 100644 index 18f45813d..000000000 --- a/internal/graphicsdriver/opengl/framebuffer.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2014 Hajime Hoshi -// -// 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 - -// framebuffer is a wrapper of OpenGL's framebuffer. -type framebuffer struct { - graphics *Graphics - native framebufferNative - width int - height int -} - -// newFramebufferFromTexture creates a framebuffer from the given texture. -func newFramebufferFromTexture(context *context, texture textureNative, width, height int) (*framebuffer, error) { - native, err := context.newFramebuffer(texture) - if err != nil { - return nil, err - } - return &framebuffer{ - native: native, - width: width, - height: height, - }, nil -} - -// newScreenFramebuffer creates a framebuffer for the screen. -func newScreenFramebuffer(context *context, width, height int) *framebuffer { - return &framebuffer{ - native: context.getScreenFramebuffer(), - width: width, - height: height, - } -} - -func (f *framebuffer) delete(context *context) { - if f.native != context.getScreenFramebuffer() { - context.deleteFramebuffer(f.native) - } -} diff --git a/internal/graphicsdriver/opengl/gl/default_notpurego.go b/internal/graphicsdriver/opengl/gl/default_cgo.go similarity index 100% rename from internal/graphicsdriver/opengl/gl/default_notpurego.go rename to internal/graphicsdriver/opengl/gl/default_cgo.go diff --git a/internal/graphicsdriver/opengl/image.go b/internal/graphicsdriver/opengl/image.go index 6c846491e..e5bf0fb21 100644 --- a/internal/graphicsdriver/opengl/image.go +++ b/internal/graphicsdriver/opengl/image.go @@ -33,6 +33,14 @@ type Image struct { screen bool } +// framebuffer is a wrapper of OpenGL's framebuffer. +type framebuffer struct { + graphics *Graphics + native framebufferNative + width int + height int +} + func (i *Image) ID() graphicsdriver.ImageID { return i.id } @@ -43,7 +51,7 @@ func (i *Image) IsInvalidated() bool { func (i *Image) Dispose() { if i.framebuffer != nil { - i.framebuffer.delete(&i.graphics.context) + i.graphics.context.deleteFramebuffer(i.framebuffer.native) } if i.texture != 0 { i.graphics.context.deleteTexture(i.texture) @@ -90,10 +98,10 @@ func (i *Image) ensureFramebuffer() error { w, h := i.framebufferSize() if i.screen { - i.framebuffer = newScreenFramebuffer(&i.graphics.context, w, h) + i.framebuffer = i.graphics.context.newScreenFramebuffer(w, h) return nil } - f, err := newFramebufferFromTexture(&i.graphics.context, i.texture, w, h) + f, err := i.graphics.context.newFramebuffer(i.texture, w, h) if err != nil { return err } diff --git a/internal/graphicsdriver/opengl/shader_js.go b/internal/graphicsdriver/opengl/shader_js.go deleted file mode 100644 index 57d238b6e..000000000 --- a/internal/graphicsdriver/opengl/shader_js.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2020 The Ebiten 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 - -import ( - "github.com/hajimehoshi/ebiten/v2/internal/shaderir/glsl" -) - -func (c *context) glslVersion() glsl.GLSLVersion { - if c.webGL2 { - return glsl.GLSLVersionES300 - } - return glsl.GLSLVersionES100 -} diff --git a/internal/graphicsdriver/opengl/shader_notjs.go b/internal/graphicsdriver/opengl/shader_notjs.go deleted file mode 100644 index e44d736f8..000000000 --- a/internal/graphicsdriver/opengl/shader_notjs.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2020 The Ebiten 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 !js - -package opengl - -import ( - "github.com/hajimehoshi/ebiten/v2/internal/shaderir/glsl" -) - -func (c *context) glslVersion() glsl.GLSLVersion { - if c.ctx.IsES() { - return glsl.GLSLVersionES100 - } - return glsl.GLSLVersionDefault -}