internal/graphicsdriver/opengl: remove duplicated code

This commit is contained in:
Hajime Hoshi 2022-11-17 13:11:00 +09:00
parent 8cc6fa82fd
commit d6027a9357
3 changed files with 51 additions and 81 deletions

View File

@ -19,6 +19,7 @@ import (
"sync"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
)
type blendFactor int
@ -87,7 +88,28 @@ func convertBlendOperation(o graphicsdriver.BlendOperation) blendOperation {
}
}
type (
textureNative uint32
renderbufferNative uint32
framebufferNative uint32
shader uint32
program uint32
buffer uint32
)
type (
uniformLocation int32
attribLocation int32
)
const (
invalidFramebuffer = (1 << 32) - 1
invalidUniform = -1
)
type context struct {
ctx gl.Context
locationCache *locationCache
screenFramebuffer framebufferNative // This might not be the default frame buffer '0' (e.g. iOS).
lastFramebuffer framebufferNative
@ -100,6 +122,7 @@ type context struct {
maxTextureSizeOnce sync.Once
highp bool
highpOnce sync.Once
initOnce sync.Once
contextImpl
}
@ -159,3 +182,31 @@ func (c *context) getMaxTextureSize() int {
})
return c.maxTextureSize
}
func (c *context) reset() error {
var err1 error
c.initOnce.Do(func() {
// Load OpenGL functions after WGL is initialized especially for Windows (#2452).
if err := c.ctx.LoadFunctions(); err != nil {
err1 = err
return
}
})
if err1 != nil {
return err1
}
c.locationCache = newLocationCache()
c.lastTexture = 0
c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0
c.lastViewportHeight = 0
c.lastBlend = graphicsdriver.Blend{}
c.ctx.Enable(gl.BLEND)
c.ctx.Enable(gl.SCISSOR_TEST)
c.blend(graphicsdriver.BlendSourceOver)
c.screenFramebuffer = framebufferNative(c.ctx.GetInteger(gl.FRAMEBUFFER_BINDING))
// TODO: Need to update screenFramebufferWidth/Height?
return nil
}

View File

@ -25,48 +25,11 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
type (
textureNative uint32
renderbufferNative uint32
framebufferNative uint32
shader uint32
program uint32
buffer uint32
)
type (
uniformLocation int32
attribLocation int32
)
const (
invalidFramebuffer = (1 << 32) - 1
invalidUniform = -1
)
type contextImpl struct {
ctx gl.Context
canvas js.Value
webGL2 bool
}
func (c *context) reset() error {
c.locationCache = newLocationCache()
c.lastTexture = 0
c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0
c.lastViewportHeight = 0
c.lastBlend = graphicsdriver.Blend{}
c.ctx.Enable(gl.BLEND)
c.ctx.Enable(gl.SCISSOR_TEST)
c.blend(graphicsdriver.BlendSourceOver)
f := c.ctx.GetInteger(gl.FRAMEBUFFER_BINDING)
c.screenFramebuffer = framebufferNative(f)
return nil
}
func (c *context) blend(blend graphicsdriver.Blend) {
if c.lastBlend == blend {
return

View File

@ -26,51 +26,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
type (
textureNative uint32
renderbufferNative uint32
framebufferNative uint32
shader uint32
program uint32
buffer uint32
)
type (
uniformLocation int32
attribLocation int32
)
const (
invalidFramebuffer = (1 << 32) - 1
invalidUniform = -1
)
type contextImpl struct {
ctx gl.Context
init bool
}
func (c *context) reset() error {
if !c.init {
// Load OpenGL functions after WGL is initialized especially for Windows (#2452).
if err := c.ctx.LoadFunctions(); err != nil {
return err
}
c.init = true
}
c.locationCache = newLocationCache()
c.lastTexture = 0
c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0
c.lastViewportHeight = 0
c.lastBlend = graphicsdriver.Blend{}
c.ctx.Enable(gl.BLEND)
c.ctx.Enable(gl.SCISSOR_TEST)
c.blend(graphicsdriver.BlendSourceOver)
c.screenFramebuffer = framebufferNative(c.ctx.GetInteger(gl.FRAMEBUFFER_BINDING))
// TODO: Need to update screenFramebufferWidth/Height?
return nil
}
func (c *context) blend(blend graphicsdriver.Blend) {