From 46cbd0c4a0b3e127e5e3383b4ecd350ca15d5290 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 16 May 2016 11:54:34 +0900 Subject: [PATCH] graphics: Add a func to delete programs and buffers --- internal/graphics/opengl/context_desktop.go | 15 +++++++++++++++ internal/graphics/opengl/context_js.go | 10 ++++++++++ internal/graphics/program.go | 15 +++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/internal/graphics/opengl/context_desktop.go b/internal/graphics/opengl/context_desktop.go index 54355513b..dbcd33a60 100644 --- a/internal/graphics/opengl/context_desktop.go +++ b/internal/graphics/opengl/context_desktop.go @@ -391,6 +391,13 @@ func (c *Context) DisableVertexAttribArray(p Program, location string) { }) } +func (c *Context) DeleteProgram(p Program) { + c.RunOnContextThread(func() error { + gl.DeleteProgram(uint32(p)) + return nil + }) +} + func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer { var buffer Buffer c.RunOnContextThread(func() error { @@ -425,6 +432,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) { c.RunOnContextThread(func() error { gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(0)) diff --git a/internal/graphics/opengl/context_js.go b/internal/graphics/opengl/context_js.go index 3dafe698a..ab4985ab2 100644 --- a/internal/graphics/opengl/context_js.go +++ b/internal/graphics/opengl/context_js.go @@ -341,6 +341,11 @@ func (c *Context) DisableVertexAttribArray(p Program, location string) { gl.DisableVertexAttribArray(int(l)) } +func (c *Context) DeleteProgram(p Program) { + gl := c.gl + gl.DeleteProgram(p.Object) +} + func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer { gl := c.gl b := gl.CreateBuffer() @@ -359,6 +364,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) { gl := c.gl gl.DrawElements(int(mode), len, gl.UNSIGNED_SHORT, 0) diff --git a/internal/graphics/program.go b/internal/graphics/program.go index 8c0898b5f..d85c78fd4 100644 --- a/internal/graphics/program.go +++ b/internal/graphics/program.go @@ -47,6 +47,10 @@ func Initialize(c *opengl.Context) error { return theOpenGLState.initialize(c) } +func Finalize(c *opengl.Context) error { + return theOpenGLState.finalize(c) +} + func (s *openGLState) initialize(c *opengl.Context) error { var zeroProgram opengl.Program s.lastProgram = zeroProgram @@ -92,6 +96,17 @@ func (s *openGLState) initialize(c *opengl.Context) error { return nil } +func (s *openGLState) finalize(c *opengl.Context) error { + var zeroProgram opengl.Program + s.lastProgram = zeroProgram + s.lastProjectionMatrix = nil + s.lastModelviewMatrix = nil + s.lastColorMatrix = nil + c.DeleteBuffer(s.indexBufferQuads) + c.DeleteProgram(s.programTexture) + return nil +} + func areSameFloat32Array(a, b []float32) bool { if len(a) != len(b) { return false