diff --git a/internal/graphics/opengl/context_desktop.go b/internal/graphics/opengl/context_desktop.go index eef44f394..19f19e3b7 100644 --- a/internal/graphics/opengl/context_desktop.go +++ b/internal/graphics/opengl/context_desktop.go @@ -394,6 +394,13 @@ func (c *Context) UseProgram(p Program) { }) } +func (c *Context) DeleteProgram(p Program) { + c.RunOnContextThread(func() error { + gl.DeleteProgram(uint32(p)) + return nil + }) +} + func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation { uniform := uniformLocation(gl.GetUniformLocation(uint32(p), gl.Str(location+"\x00"))) if uniform == -1 { @@ -491,6 +498,14 @@ func (c *Context) BufferSubData(bufferType BufferType, data []int16) { }) } +func (c *Context) DeleteBuffer(b Buffer) { + c.RunOnContextThread(func() error { + bb := uint32(b) + gl.DeleteBuffers(1, &bb) + return nil + }) +} + func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) { c.RunOnContextThread(func() error { gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(offsetInBytes)) diff --git a/internal/graphics/opengl/context_js.go b/internal/graphics/opengl/context_js.go index 8cfaa3694..d85d02a5e 100644 --- a/internal/graphics/opengl/context_js.go +++ b/internal/graphics/opengl/context_js.go @@ -321,6 +321,11 @@ func (c *Context) UseProgram(p Program) { gl.UseProgram(p.Object) } +func (c *Context) DeleteProgram(p Program) { + gl := c.gl + gl.DeleteProgram(p.Object) +} + func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation { gl := c.gl return uniformLocation{gl.GetUniformLocation(p.Object, location)} @@ -386,6 +391,11 @@ func (c *Context) BufferSubData(bufferType BufferType, data []int16) { gl.BufferSubData(int(bufferType), 0, data) } +func (c *Context) DeleteBuffer(b Buffer) { + gl := c.gl + gl.DeleteBuffer(b.Object) +} + func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) { gl := c.gl gl.DrawElements(int(mode), len, gl.UNSIGNED_SHORT, offsetInBytes) diff --git a/internal/graphics/opengl/context_mobile.go b/internal/graphics/opengl/context_mobile.go index e566761b9..7a7087d7a 100644 --- a/internal/graphics/opengl/context_mobile.go +++ b/internal/graphics/opengl/context_mobile.go @@ -295,6 +295,11 @@ func (c *Context) UseProgram(p Program) { gl.UseProgram(mgl.Program(p)) } +func (c *Context) DeleteProgram(p Program) { + gl := c.gl + gl.DeleteProgram(mgl.Program(p)) +} + func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation { gl := c.gl u := uniformLocation(gl.GetUniformLocation(mgl.Program(p), location)) @@ -392,6 +397,11 @@ func (c *Context) BufferSubData(bufferType BufferType, data []int16) { gl.BufferSubData(mgl.Enum(bufferType), 0, int16ToBytes(data)) } +func (c *Context) DeleteBuffer(b Buffer) { + gl := c.gl + gl.DeleteBuffer(mgl.Buffer(b)) +} + func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) { gl := c.gl gl.DrawElements(mgl.Enum(mode), len, mgl.UNSIGNED_SHORT, offsetInBytes) diff --git a/internal/graphics/program.go b/internal/graphics/program.go index 31113f758..ad5e8abe3 100644 --- a/internal/graphics/program.go +++ b/internal/graphics/program.go @@ -21,6 +21,7 @@ import ( ) type openGLState struct { + arrayBuffer opengl.Buffer indexBufferQuads opengl.Buffer programTexture opengl.Program @@ -32,8 +33,10 @@ type openGLState struct { lastTexture opengl.Texture } -var theOpenGLState openGLState var ( + theOpenGLState openGLState + + zeroBuffer opengl.Buffer zeroProgram opengl.Program zeroTexture opengl.Texture ) @@ -63,7 +66,15 @@ func (s *openGLState) initialize(context *opengl.Context) error { s.lastColorMatrixTranslation = nil s.lastTexture = zeroTexture - // TODO: Remove the old programs when resuming? + if s.arrayBuffer != zeroBuffer { + context.DeleteBuffer(s.arrayBuffer) + } + if s.indexBufferQuads != zeroBuffer { + context.DeleteBuffer(s.indexBufferQuads) + } + if s.programTexture != zeroProgram { + context.DeleteProgram(s.programTexture) + } shaderVertexModelviewNative, err := context.NewShader(opengl.VertexShader, shader(context, shaderVertexModelview)) if err != nil { @@ -85,10 +96,8 @@ func (s *openGLState) initialize(context *opengl.Context) error { return err } - // TODO: Remove the old buffers when resuming? - const stride = 8 // (2 [vertices] + 2 [texels]) * 2 [sizeof(int16)/bytes] - context.NewBuffer(opengl.ArrayBuffer, 4*stride*MaxQuads, opengl.DynamicDraw) + s.arrayBuffer = context.NewBuffer(opengl.ArrayBuffer, 4*stride*MaxQuads, opengl.DynamicDraw) indices := make([]uint16, 6*MaxQuads) for i := uint16(0); i < MaxQuads; i++ {