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 maxTextureSizeOnce sync.Once
highp bool highp bool
highpOnce sync.Once highpOnce sync.Once
initOnce sync.Once
contextPlatform contextPlatform
} }
@ -191,25 +190,14 @@ func (c *context) getMaxTextureSize() int {
return c.maxTextureSize return c.maxTextureSize
} }
func (c *context) reset() error { func (c *context) init() error {
var err1 error // Load OpenGL functions after WGL is initialized especially for Windows (#2452).
c.initOnce.Do(func() { if err := c.ctx.LoadFunctions(); err != nil {
// Load OpenGL functions after WGL is initialized especially for Windows (#2452). return err
if err := c.ctx.LoadFunctions(); err != nil {
err1 = err
return
}
})
if err1 != nil {
return err1
} }
c.locationCache = newLocationCache() c.locationCache = newLocationCache()
c.lastTexture = 0
c.lastFramebuffer = invalidFramebuffer c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0
c.lastViewportHeight = 0
c.lastBlend = graphicsdriver.Blend{}
c.ctx.Enable(gl.BLEND) c.ctx.Enable(gl.BLEND)
c.ctx.Enable(gl.SCISSOR_TEST) c.ctx.Enable(gl.SCISSOR_TEST)

View File

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

View File

@ -16,7 +16,6 @@ package opengl
import ( import (
"fmt" "fmt"
"runtime"
"unsafe" "unsafe"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
@ -122,34 +121,6 @@ type openGLState struct {
lastActiveTexture int 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) { func (s *openGLState) setVertices(context *context, vertices []float32, indices []uint16) {
if s.arrayBuffer == 0 { if s.arrayBuffer == 0 {
s.arrayBuffer = context.newArrayBuffer(graphics.IndicesCount * graphics.VertexFloatCount * floatSizeInBytes) s.arrayBuffer = context.newArrayBuffer(graphics.IndicesCount * graphics.VertexFloatCount * floatSizeInBytes)