internal/graphicsdriver/opengl: refactoring

This commit is contained in:
Hajime Hoshi 2023-03-21 23:06:49 +09:00
parent 23e90b7e39
commit 4cd98d512e
3 changed files with 5 additions and 51 deletions

View File

@ -124,7 +124,6 @@ type context struct {
maxTextureSizeOnce sync.Once
highp bool
highpOnce sync.Once
initOnce sync.Once
contextPlatform
}
@ -191,25 +190,14 @@ 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
func (c *context) init() error {
// Load OpenGL functions after WGL is initialized especially for Windows (#2452).
if err := c.ctx.LoadFunctions(); err != nil {
return err
}
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)

View File

@ -146,12 +146,7 @@ func (g *Graphics) removeImage(img *Image) {
}
func (g *Graphics) Initialize() error {
return g.state.reset(&g.context)
}
// Reset resets or initializes the current OpenGL state.
func (g *Graphics) Reset() error {
return g.state.reset(&g.context)
return g.context.init()
}
func (g *Graphics) SetVertices(vertices []float32, indices []uint16) error {

View File

@ -16,7 +16,6 @@ package opengl
import (
"fmt"
"runtime"
"unsafe"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
@ -122,34 +121,6 @@ type openGLState struct {
lastActiveTexture int
}
// reset resets or initializes the OpenGL state.
func (s *openGLState) reset(context *context) error {
if err := context.reset(); err != nil {
return err
}
s.lastProgram = 0
context.ctx.UseProgram(0)
for key := range s.lastUniforms {
delete(s.lastUniforms, key)
}
// On browsers (at least Chrome), buffers are already detached from the context
// and must not be deleted by DeleteBuffer.
if runtime.GOOS != "js" {
if s.arrayBuffer != 0 {
context.ctx.DeleteBuffer(uint32(s.arrayBuffer))
}
if s.elementArrayBuffer != 0 {
context.ctx.DeleteBuffer(uint32(s.elementArrayBuffer))
}
}
s.arrayBuffer = 0
s.elementArrayBuffer = 0
return nil
}
func (s *openGLState) setVertices(context *context, vertices []float32, indices []uint16) {
if s.arrayBuffer == 0 {
s.arrayBuffer = context.newArrayBuffer(graphics.IndicesCount * graphics.VertexFloatCount * floatSizeInBytes)