internal/graphicsdriver/opengl: skip checkFramebufferStatus on browsers

This commit is contained in:
Hajime Hoshi 2024-07-03 20:34:21 +09:00
parent c5974390a8
commit c29d4b24bb

View File

@ -20,6 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"image" "image"
"runtime"
"sync" "sync"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
@ -336,6 +337,8 @@ func (c *context) newFramebuffer(texture textureNative, width, height int) (*fra
c.bindFramebuffer(framebufferNative(f)) c.bindFramebuffer(framebufferNative(f))
c.ctx.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0) c.ctx.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0)
if shouldCheckFramebufferStatus() {
if s := c.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE { if s := c.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE {
if s != 0 { if s != 0 {
return nil, fmt.Errorf("opengl: creating framebuffer failed: %v", s) return nil, fmt.Errorf("opengl: creating framebuffer failed: %v", s)
@ -345,6 +348,8 @@ func (c *context) newFramebuffer(texture textureNative, width, height int) (*fra
} }
return nil, fmt.Errorf("opengl: creating framebuffer failed: unknown error") return nil, fmt.Errorf("opengl: creating framebuffer failed: unknown error")
} }
}
return &framebuffer{ return &framebuffer{
native: framebufferNative(f), native: framebufferNative(f),
viewportWidth: width, viewportWidth: width,
@ -356,9 +361,13 @@ func (c *context) bindStencilBuffer(f framebufferNative, r renderbufferNative) e
c.bindFramebuffer(f) c.bindFramebuffer(f)
c.ctx.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, uint32(r)) c.ctx.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, uint32(r))
if shouldCheckFramebufferStatus() {
if s := c.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE { if s := c.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE {
return errors.New(fmt.Sprintf("opengl: glFramebufferRenderbuffer failed: %d", s)) return fmt.Errorf("opengl: glFramebufferRenderbuffer failed: %d", s)
} }
}
return nil return nil
} }
@ -496,3 +505,11 @@ func (c *context) glslVersion() glsl.GLSLVersion {
} }
return glsl.GLSLVersionDefault return glsl.GLSLVersionDefault
} }
func shouldCheckFramebufferStatus() bool {
// CheckFramebufferStatus is slow and should be avoided especially in browsers.
// See https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/WebGL_best_practices#avoid_blocking_api_calls_in_production
//
// TODO: Should this be avoided in all environments?
return runtime.GOOS != "js"
}