internal/graphicsdriver/opengl: revert commits for graphics.Indices

This change reverts these commits:

* 13d3a0487b
* 4cd98d512e

Reason: this caused crashes on Android.

Updates #2460
Closes #2610
This commit is contained in:
Hajime Hoshi 2023-03-22 17:26:38 +09:00
parent 13d3a0487b
commit 8cbf545e0f
4 changed files with 54 additions and 33 deletions

View File

@ -48,7 +48,7 @@ const (
) )
const ( const (
IndicesCount = (1 << 16) / 3 * 3 // Adjust num for triangles. TODO: Remove this (#2460). IndicesCount = (1 << 16) / 3 * 3 // Adjust num for triangles.
VertexFloatCount = 8 VertexFloatCount = 8
) )

View File

@ -124,6 +124,7 @@ type context struct {
maxTextureSizeOnce sync.Once maxTextureSizeOnce sync.Once
highp bool highp bool
highpOnce sync.Once highpOnce sync.Once
initOnce sync.Once
contextPlatform contextPlatform
} }
@ -190,14 +191,25 @@ func (c *context) getMaxTextureSize() int {
return c.maxTextureSize return c.maxTextureSize
} }
func (c *context) init() error { func (c *context) reset() error {
// Load OpenGL functions after WGL is initialized especially for Windows (#2452). var err1 error
if err := c.ctx.LoadFunctions(); err != nil { c.initOnce.Do(func() {
return err // 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.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,7 +146,12 @@ func (g *Graphics) removeImage(img *Image) {
} }
func (g *Graphics) Initialize() error { func (g *Graphics) Initialize() error {
return g.context.init() 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)
} }
func (g *Graphics) SetVertices(vertices []float32, indices []uint16) error { func (g *Graphics) SetVertices(vertices []float32, indices []uint16) error {

View File

@ -16,6 +16,7 @@ package opengl
import ( import (
"fmt" "fmt"
"runtime"
"unsafe" "unsafe"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
@ -113,50 +114,50 @@ type openGLState struct {
// arrayBuffer is OpenGL's array buffer (vertices data). // arrayBuffer is OpenGL's array buffer (vertices data).
arrayBuffer buffer arrayBuffer buffer
arrayBufferSizeInBytes int
// elementArrayBuffer is OpenGL's element array buffer (indices data). // elementArrayBuffer is OpenGL's element array buffer (indices data).
elementArrayBuffer buffer elementArrayBuffer buffer
elementArrayBufferSizeInBytes int
lastProgram program lastProgram program
lastUniforms map[string][]uint32 lastUniforms map[string][]uint32
lastActiveTexture int lastActiveTexture int
} }
func pow2(x int) int { // reset resets or initializes the OpenGL state.
p2 := 1 func (s *openGLState) reset(context *context) error {
for p2 < x { if err := context.reset(); err != nil {
p2 *= 2 return err
} }
return p2
}
func (s *openGLState) setVertices(context *context, vertices []float32, indices []uint16) { s.lastProgram = 0
if size := len(vertices) * 4; s.arrayBufferSizeInBytes < size { 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 { if s.arrayBuffer != 0 {
context.ctx.DeleteBuffer(uint32(s.arrayBuffer)) context.ctx.DeleteBuffer(uint32(s.arrayBuffer))
} }
newSize := pow2(size)
// newArrayBuffer calls BindBuffer.
s.arrayBuffer = context.newArrayBuffer(newSize)
s.arrayBufferSizeInBytes = newSize
// Reenable the array buffer layouter explicitly after resetting the array buffer.
theArrayBufferLayout.enable(context)
}
if size := len(indices) * 2; s.elementArrayBufferSizeInBytes < size {
if s.elementArrayBuffer != 0 { if s.elementArrayBuffer != 0 {
context.ctx.DeleteBuffer(uint32(s.elementArrayBuffer)) context.ctx.DeleteBuffer(uint32(s.elementArrayBuffer))
} }
}
s.arrayBuffer = 0
s.elementArrayBuffer = 0
newSize := pow2(size) return nil
// newElementArrayBuffer calls BindBuffer. }
s.elementArrayBuffer = context.newElementArrayBuffer(newSize)
s.elementArrayBufferSizeInBytes = newSize func (s *openGLState) setVertices(context *context, vertices []float32, indices []uint16) {
if s.arrayBuffer == 0 {
s.arrayBuffer = context.newArrayBuffer(graphics.IndicesCount * graphics.VertexFloatCount * floatSizeInBytes)
context.ctx.BindBuffer(gl.ARRAY_BUFFER, uint32(s.arrayBuffer))
}
if s.elementArrayBuffer == 0 {
s.elementArrayBuffer = context.newElementArrayBuffer(graphics.IndicesCount * 2)
context.ctx.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(s.elementArrayBuffer))
} }
// Note that the vertices and the indices passed to BufferSubData is not under GC management in the gl package. // Note that the vertices and the indices passed to BufferSubData is not under GC management in the gl package.
@ -212,6 +213,9 @@ func (g *Graphics) textureVariableName(idx int) string {
func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textures [graphics.ShaderImageCount]textureVariable) error { func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textures [graphics.ShaderImageCount]textureVariable) error {
if g.state.lastProgram != program { if g.state.lastProgram != program {
g.context.ctx.UseProgram(uint32(program)) g.context.ctx.UseProgram(uint32(program))
if g.state.lastProgram == 0 {
theArrayBufferLayout.enable(&g.context)
}
g.state.lastProgram = program g.state.lastProgram = program
for k := range g.state.lastUniforms { for k := range g.state.lastUniforms {