graphics: Delete programs and buffers when resuming

This commit is contained in:
Hajime Hoshi 2016-07-04 00:23:45 +09:00
parent 2c1bcaf87a
commit d0a779e272
4 changed files with 49 additions and 5 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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)

View File

@ -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++ {